Remove all plugins / install base theme
This commit is contained in:
@@ -1,77 +0,0 @@
|
||||
<?php
|
||||
namespace Imagify\WriteFile;
|
||||
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Abstract class used to add and remove contents to the .htaccess file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
abstract class AbstractApacheDirConfFile extends AbstractWriteDirConfFile {
|
||||
|
||||
/**
|
||||
* Insert new contents into the directory conf file.
|
||||
* Replaces existing marked info. Creates file if none exists.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $new_contents Contents to insert.
|
||||
* @return bool|\WP_Error True on write success, a \WP_Error object on failure.
|
||||
*/
|
||||
protected function insert_contents( $new_contents ) {
|
||||
$contents = $this->get_file_contents();
|
||||
|
||||
if ( is_wp_error( $contents ) ) {
|
||||
return $contents;
|
||||
}
|
||||
|
||||
$start_marker = '# BEGIN ' . static::TAG_NAME;
|
||||
$end_marker = '# END ' . static::TAG_NAME;
|
||||
|
||||
// Remove previous rules.
|
||||
$contents = preg_replace( '/\s*?' . preg_quote( $start_marker, '/' ) . '.*' . preg_quote( $end_marker, '/' ) . '\s*?/isU', "\n\n", $contents );
|
||||
$contents = trim( $contents );
|
||||
|
||||
if ( $new_contents ) {
|
||||
$contents = $new_contents . "\n\n" . $contents;
|
||||
}
|
||||
|
||||
return $this->put_file_contents( $contents );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get new contents to write into the file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_new_contents() {
|
||||
$contents = parent::get_new_contents();
|
||||
|
||||
if ( ! $contents ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return '# BEGIN ' . static::TAG_NAME . "\n" . $contents . "\n# END " . static::TAG_NAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the unfiltered path to the file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_raw_file_path() {
|
||||
return $this->filesystem->get_site_root() . '.htaccess';
|
||||
}
|
||||
}
|
||||
@@ -1,283 +0,0 @@
|
||||
<?php
|
||||
namespace Imagify\WriteFile;
|
||||
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Abstract class used to add and remove contents to the web.config file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
abstract class AbstractIISDirConfFile extends AbstractWriteDirConfFile {
|
||||
|
||||
/**
|
||||
* Insert new contents into the directory conf file.
|
||||
* Replaces existing marked info. Creates file if none exists.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $new_contents Contents to insert.
|
||||
* @return bool|\WP_Error True on write success, a \WP_Error object on failure.
|
||||
*/
|
||||
protected function insert_contents( $new_contents ) {
|
||||
$doc = $this->get_file_contents();
|
||||
|
||||
if ( is_wp_error( $doc ) ) {
|
||||
return $doc;
|
||||
}
|
||||
|
||||
$marker = static::TAG_NAME;
|
||||
$xpath = new \DOMXPath( $doc );
|
||||
|
||||
// Remove previous rules.
|
||||
$old_nodes = $xpath->query( ".//*[starts-with(@name,'$marker')]" );
|
||||
|
||||
if ( $old_nodes->length > 0 ) {
|
||||
foreach ( $old_nodes as $old_node ) {
|
||||
$old_node->parentNode->removeChild( $old_node );
|
||||
}
|
||||
}
|
||||
|
||||
// No new contents? Stop here.
|
||||
if ( ! $new_contents ) {
|
||||
return $this->put_file_contents( $doc );
|
||||
}
|
||||
|
||||
$new_contents = preg_split( '/<!--\s+@parent\s+(.+?)\s+-->/', $new_contents, -1, PREG_SPLIT_DELIM_CAPTURE );
|
||||
unset( $new_contents[0] );
|
||||
$new_contents = array_chunk( $new_contents, 2 );
|
||||
|
||||
foreach ( $new_contents as $i => $new_content ) {
|
||||
$path = rtrim( $new_content[0], '/' );
|
||||
$new_content = trim( $new_content[1] );
|
||||
|
||||
if ( '' === $new_content ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$fragment = $doc->createDocumentFragment();
|
||||
$fragment->appendXML( $new_content );
|
||||
|
||||
$this->get_node( $doc, $xpath, $path, $fragment );
|
||||
}
|
||||
|
||||
return $this->put_file_contents( $doc );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the unfiltered path to the file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_raw_file_path() {
|
||||
return $this->filesystem->get_site_root() . 'web.config';
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** OTHER TOOLS ============================================================================= */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Tell if the file is writable.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool|\WP_Error True if writable. A \WP_Error object if not.
|
||||
*/
|
||||
public function is_file_writable() {
|
||||
$file_path = $this->get_file_path();
|
||||
$file_name = $this->filesystem->make_path_relative( $file_path );
|
||||
|
||||
if ( $this->is_conf_edition_disabled() ) {
|
||||
return new \WP_Error(
|
||||
'edition_disabled',
|
||||
sprintf(
|
||||
/* translators: %s is a file name. */
|
||||
__( 'Edition of the %s file is disabled.', 'imagify' ),
|
||||
'<code>' . esc_html( $file_name ) . '</code>'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if ( ! class_exists( '\DOMDocument' ) ) {
|
||||
return new \WP_Error(
|
||||
'not_domdocument',
|
||||
sprintf(
|
||||
/* translators: 1 is a php class name, 2 is a file name. */
|
||||
__( 'The class %1$s is not present on your server, a %2$s file cannot be created nor edited.', 'imagify' ),
|
||||
'<code>DOMDocument</code>',
|
||||
'<code>' . esc_html( $file_name ) . '</code>'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if ( ! $this->filesystem->exists( $file_path ) ) {
|
||||
$dir_path = $this->filesystem->dir_path( $file_path );
|
||||
|
||||
$this->filesystem->make_dir( $dir_path );
|
||||
|
||||
if ( ! $this->filesystem->is_writable( $dir_path ) ) {
|
||||
return new \WP_Error(
|
||||
'parent_not_writable',
|
||||
sprintf(
|
||||
/* translators: %s is a file name. */
|
||||
__( '%s’s parent folder is not writable.', 'imagify' ),
|
||||
'<code>' . esc_html( $file_name ) . '</code>'
|
||||
)
|
||||
);
|
||||
}
|
||||
if ( ! $this->filesystem->exists( $file_path ) ) {
|
||||
$result = $this->filesystem->put_contents( $file_path, '<configuration/>' );
|
||||
|
||||
if ( ! $result ) {
|
||||
return new \WP_Error(
|
||||
'not_created',
|
||||
sprintf(
|
||||
/* translators: %s is a file name. */
|
||||
__( 'The %s file could not be created.', 'imagify' ),
|
||||
'<code>' . esc_html( $file_name ) . '</code>'
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
} elseif ( ! $this->filesystem->is_writable( $file_path ) ) {
|
||||
return new \WP_Error(
|
||||
'not_writable',
|
||||
sprintf(
|
||||
/* translators: %s is a file name. */
|
||||
__( 'The %s file is not writable.', 'imagify' ),
|
||||
'<code>' . esc_html( $file_name ) . '</code>'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file contents.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return \DOMDocument|\WP_Error A \DOMDocument object on success, a \WP_Error object on failure.
|
||||
*/
|
||||
protected function get_file_contents() {
|
||||
$writable = $this->is_file_writable();
|
||||
|
||||
if ( is_wp_error( $writable ) ) {
|
||||
return $writable;
|
||||
}
|
||||
|
||||
$file_path = $this->get_file_path();
|
||||
$doc = new \DOMDocument();
|
||||
|
||||
$doc->preserveWhiteSpace = false;
|
||||
|
||||
if ( false === $doc->load( $file_path ) ) {
|
||||
$file_path = $this->get_file_path();
|
||||
$file_name = $this->filesystem->make_path_relative( $file_path );
|
||||
|
||||
return new \WP_Error(
|
||||
'not_read',
|
||||
sprintf(
|
||||
/* translators: %s is a file name. */
|
||||
__( 'The %s file could not be read.', 'imagify' ),
|
||||
'<code>' . esc_html( $file_name ) . '</code>'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return $doc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Put new contents into the file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param \DOMDocument $contents A \DOMDocument object.
|
||||
* @return bool|\WP_Error True on success, a \WP_Error object on failure.
|
||||
*/
|
||||
protected function put_file_contents( $contents ) {
|
||||
$contents->encoding = 'UTF-8';
|
||||
$contents->formatOutput = true;
|
||||
|
||||
saveDomDocument( $contents, $this->get_file_path() );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a DOMNode node.
|
||||
* If it does not exist it is created recursively.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param \DOMDocument $doc A \DOMDocument element.
|
||||
* @param \DOMXPath $xpath A \DOMXPath element.
|
||||
* @param string $path Path to the desired node.
|
||||
* @param \DOMNode $child A \DOMNode to be prepended.
|
||||
* @return \DOMNode The \DOMNode node.
|
||||
*/
|
||||
protected function get_node( $doc, $xpath, $path, $child ) {
|
||||
$nodelist = $xpath->query( $path );
|
||||
|
||||
if ( $nodelist->length > 0 ) {
|
||||
return $this->prepend_node( $nodelist->item( 0 ), $child );
|
||||
}
|
||||
|
||||
$path = explode( '/', $path );
|
||||
$node = array_pop( $path );
|
||||
$path = implode( '/', $path );
|
||||
|
||||
$final_node = $doc->createElement( $node );
|
||||
|
||||
if ( $child ) {
|
||||
$final_node->appendChild( $child );
|
||||
}
|
||||
|
||||
return $this->get_node( $doc, $xpath, $path, $final_node );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Prepend a DOMNode node.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param \DOMNode $container_node The \DOMNode that will contain the new node.
|
||||
* @param \DOMNode $new_node The \DOMNode to be prepended.
|
||||
* @return \DOMNode The \DOMNode containing the new node.
|
||||
*/
|
||||
protected function prepend_node( $container_node, $new_node ) {
|
||||
if ( ! $new_node ) {
|
||||
return $container_node;
|
||||
}
|
||||
|
||||
if ( $container_node->hasChildNodes() ) {
|
||||
$container_node->insertBefore( $new_node, $container_node->firstChild );
|
||||
} else {
|
||||
$container_node->appendChild( $new_node );
|
||||
}
|
||||
|
||||
return $container_node;
|
||||
}
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
<?php
|
||||
namespace Imagify\WriteFile;
|
||||
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Abstract class used to add and remove contents to imagify.conf file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
abstract class AbstractNginxDirConfFile extends AbstractWriteDirConfFile {
|
||||
|
||||
/**
|
||||
* Insert new contents into the directory conf file.
|
||||
* Replaces existing marked info. Creates file if none exists.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $new_contents Contents to insert.
|
||||
* @return bool|\WP_Error True on write success, a \WP_Error object on failure.
|
||||
*/
|
||||
protected function insert_contents( $new_contents ) {
|
||||
$contents = $this->get_file_contents();
|
||||
|
||||
if ( is_wp_error( $contents ) ) {
|
||||
return $contents;
|
||||
}
|
||||
|
||||
$start_marker = '# BEGIN ' . static::TAG_NAME;
|
||||
$end_marker = '# END ' . static::TAG_NAME;
|
||||
|
||||
// Remove previous rules.
|
||||
$contents = preg_replace( '/\s*?' . preg_quote( $start_marker, '/' ) . '.*' . preg_quote( $end_marker, '/' ) . '\s*?/isU', "\n\n", $contents );
|
||||
$contents = trim( $contents );
|
||||
|
||||
if ( $new_contents ) {
|
||||
$contents = $new_contents . "\n\n" . $contents;
|
||||
}
|
||||
|
||||
return $this->put_file_contents( $contents );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get new contents to write into the file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_new_contents() {
|
||||
$contents = parent::get_new_contents();
|
||||
|
||||
if ( ! $contents ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return '# BEGIN ' . static::TAG_NAME . "\n" . $contents . "\n# END " . static::TAG_NAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the unfiltered path to the file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_raw_file_path() {
|
||||
return $this->filesystem->get_site_root() . 'conf/imagify.conf';
|
||||
}
|
||||
}
|
||||
@@ -1,395 +0,0 @@
|
||||
<?php
|
||||
namespace Imagify\WriteFile;
|
||||
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Abstract class used to add and remove contents to a directory conf file (.htaccess, etc).
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
abstract class AbstractWriteDirConfFile implements WriteFileInterface {
|
||||
|
||||
/**
|
||||
* Name of the tag used as block delemiter.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
const TAG_NAME = 'Imagify ###';
|
||||
|
||||
/**
|
||||
* Filesystem object.
|
||||
*
|
||||
* @var \Imagify_Filesystem
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
protected $filesystem;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->filesystem = \Imagify_Filesystem::get_instance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add new contents to the file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool|\WP_Error True on success. A \WP_Error object on error.
|
||||
*/
|
||||
public function add() {
|
||||
$result = $this->insert_contents( $this->get_new_contents() );
|
||||
|
||||
if ( ! is_wp_error( $result ) ) {
|
||||
return true;
|
||||
}
|
||||
$file_path = $this->get_file_path();
|
||||
$file_name = $this->filesystem->make_path_relative( $file_path );
|
||||
|
||||
if ( 'edition_disabled' === $result->get_error_code() ) {
|
||||
return new \WP_Error(
|
||||
'edition_disabled',
|
||||
sprintf(
|
||||
/* translators: %s is a file name. */
|
||||
__( 'Imagify did not add contents to the %s file, as its edition is disabled.', 'imagify' ),
|
||||
$file_name
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return new \WP_Error(
|
||||
'add_contents_failure',
|
||||
sprintf(
|
||||
/* translators: 1 is a file name, 2 is an error message. */
|
||||
__( 'Imagify could not insert contents into the %1$s file: %2$s', 'imagify' ),
|
||||
$file_name,
|
||||
$result->get_error_message()
|
||||
),
|
||||
[ 'code' => $result->get_error_code() ]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the related contents from the file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool|\WP_Error True on success. A \WP_Error object on error.
|
||||
*/
|
||||
public function remove() {
|
||||
$result = $this->insert_contents( '' );
|
||||
|
||||
if ( ! is_wp_error( $result ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$file_name = $this->filesystem->make_path_relative( $file_path );
|
||||
|
||||
if ( 'edition_disabled' === $result->get_error_code() ) {
|
||||
return new \WP_Error(
|
||||
'edition_disabled',
|
||||
sprintf(
|
||||
/* translators: %s is a file name. */
|
||||
__( 'Imagify did not remove the contents from the %s file, as its edition is disabled.', 'imagify' ),
|
||||
$file_name
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return new \WP_Error(
|
||||
'add_contents_failure',
|
||||
sprintf(
|
||||
/* translators: 1 is a file name, 2 is an error message. */
|
||||
__( 'Imagify could not remove contents from the %1$s file: %2$s', 'imagify' ),
|
||||
$file_name,
|
||||
$result->get_error_message()
|
||||
),
|
||||
[ 'code' => $result->get_error_code() ]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the path to the file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_file_path() {
|
||||
$file_path = $this->get_raw_file_path();
|
||||
|
||||
/**
|
||||
* Filter the path to the directory conf file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $file_path Path to the file.
|
||||
*/
|
||||
$new_file_path = apply_filters( 'imagify_dir_conf_path', $file_path );
|
||||
|
||||
if ( $new_file_path && is_string( $new_file_path ) ) {
|
||||
return $new_file_path;
|
||||
}
|
||||
|
||||
return $file_path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the file is writable.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool|\WP_Error True if writable. A \WP_Error object if not.
|
||||
*/
|
||||
public function is_file_writable() {
|
||||
$file_path = $this->get_file_path();
|
||||
$file_name = $this->filesystem->make_path_relative( $file_path );
|
||||
|
||||
if ( $this->is_conf_edition_disabled() ) {
|
||||
return new \WP_Error(
|
||||
'edition_disabled',
|
||||
sprintf(
|
||||
/* translators: %s is a file name. */
|
||||
__( 'Edition of the %s file is disabled.', 'imagify' ),
|
||||
'<code>' . esc_html( $file_name ) . '</code>'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if ( ! $this->filesystem->exists( $file_path ) ) {
|
||||
$dir_path = $this->filesystem->dir_path( $file_path );
|
||||
|
||||
$this->filesystem->make_dir( $dir_path );
|
||||
|
||||
if ( ! $this->filesystem->is_writable( $dir_path ) ) {
|
||||
return new \WP_Error(
|
||||
'parent_not_writable',
|
||||
sprintf(
|
||||
/* translators: %s is a file name. */
|
||||
__( '%s’s parent folder is not writable.', 'imagify' ),
|
||||
'<code>' . esc_html( $file_name ) . '</code>'
|
||||
)
|
||||
);
|
||||
}
|
||||
if ( ! $this->filesystem->touch( $file_path ) ) {
|
||||
return new \WP_Error(
|
||||
'not_created',
|
||||
sprintf(
|
||||
/* translators: %s is a file name. */
|
||||
__( 'The %s file could not be created.', 'imagify' ),
|
||||
'<code>' . esc_html( $file_name ) . '</code>'
|
||||
)
|
||||
);
|
||||
}
|
||||
} elseif ( ! $this->filesystem->is_writable( $file_path ) ) {
|
||||
return new \WP_Error(
|
||||
'not_writable',
|
||||
sprintf(
|
||||
/* translators: %s is a file name. */
|
||||
__( 'The %s file is not writable.', 'imagify' ),
|
||||
'<code>' . esc_html( $file_name ) . '</code>'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get new contents to write into the file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_new_contents() {
|
||||
$contents = $this->get_raw_new_contents();
|
||||
|
||||
/**
|
||||
* Filter the contents to add to the directory conf file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $contents The contents.
|
||||
*/
|
||||
$new_contents = apply_filters( 'imagify_dir_conf_contents', $contents );
|
||||
|
||||
if ( $new_contents && is_string( $new_contents ) ) {
|
||||
return $new_contents;
|
||||
}
|
||||
|
||||
return $contents;
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** ABSTRACT METHODS ======================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Insert new contents into the directory conf file.
|
||||
* Replaces existing marked info. Creates file if none exists.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $new_contents Contents to insert.
|
||||
* @return bool|\WP_Error True on write success, a \WP_Error object on failure.
|
||||
*/
|
||||
abstract protected function insert_contents( $new_contents );
|
||||
|
||||
/**
|
||||
* Get the unfiltered path to the file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract protected function get_raw_file_path();
|
||||
|
||||
/**
|
||||
* Get unfiltered new contents to write into the file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract protected function get_raw_new_contents();
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** OTHER TOOLS ============================================================================= */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get the file contents.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return mixed|\WP_Error The file contents on success, a \WP_Error object on failure.
|
||||
*/
|
||||
protected function get_file_contents() {
|
||||
$writable = $this->is_file_writable();
|
||||
|
||||
if ( is_wp_error( $writable ) ) {
|
||||
return $writable;
|
||||
}
|
||||
|
||||
$file_path = $this->get_file_path();
|
||||
|
||||
if ( ! $this->filesystem->exists( $file_path ) ) {
|
||||
// This should not happen.
|
||||
return '';
|
||||
}
|
||||
|
||||
$contents = $this->filesystem->get_contents( $file_path );
|
||||
|
||||
if ( false === $contents ) {
|
||||
return new \WP_Error(
|
||||
'not_read',
|
||||
sprintf(
|
||||
/* translators: %s is a file name. */
|
||||
__( 'The %s file could not be read.', 'imagify' ),
|
||||
'<code>' . esc_html( $file_name ) . '</code>'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return $contents;
|
||||
}
|
||||
|
||||
/**
|
||||
* Put new contents into the file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $contents New contents to add to the file.
|
||||
* @return bool|\WP_Error True on success, a \WP_Error object on failure.
|
||||
*/
|
||||
protected function put_file_contents( $contents ) {
|
||||
$file_path = $this->get_file_path();
|
||||
$result = $this->filesystem->put_contents( $file_path, $contents );
|
||||
|
||||
if ( $result ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$file_name = $this->filesystem->make_path_relative( $file_path );
|
||||
|
||||
return new \WP_Error(
|
||||
'edition_failed',
|
||||
sprintf(
|
||||
/* translators: %s is a file name. */
|
||||
__( 'Could not write into the %s file.', 'imagify' ),
|
||||
'<code>' . esc_html( $file_name ) . '</code>'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if edition of the directory conf file is disabled.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool True to disable, false otherwise.
|
||||
*/
|
||||
protected function is_conf_edition_disabled() {
|
||||
/**
|
||||
* Disable directory conf edition.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param bool $disable True to disable, false otherwise.
|
||||
*/
|
||||
return (bool) apply_filters( 'imagify_disable_dir_conf_edition', false );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a regex pattern to be used to match the supported file extensions.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_extensions_pattern() {
|
||||
$extensions = imagify_get_mime_types( 'image' );
|
||||
$extensions = array_keys( $extensions );
|
||||
|
||||
return implode( '|', $extensions );
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
<?php
|
||||
namespace Imagify\WriteFile;
|
||||
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Interface to add and remove contents to a file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
interface WriteFileInterface {
|
||||
|
||||
/**
|
||||
* Add new contents to the file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool|\WP_Error True on success. A \WP_Error object on error.
|
||||
*/
|
||||
public function add();
|
||||
|
||||
/**
|
||||
* Remove the related contents from the file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool|\WP_Error True on success. A \WP_Error object on error.
|
||||
*/
|
||||
public function remove();
|
||||
|
||||
/**
|
||||
* Get the path to the file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_file_path();
|
||||
|
||||
/**
|
||||
* Tell if the file is writable.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool|\WP_Error True if writable. A \WP_Error object if not.
|
||||
*/
|
||||
public function is_file_writable();
|
||||
|
||||
/**
|
||||
* Get new contents to write into the file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_new_contents();
|
||||
}
|
||||
Reference in New Issue
Block a user