| 1 |
<?php |
| 2 |
namespace Imagify\WriteFile; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); |
| 5 |
|
| 6 |
/** |
| 7 |
* Abstract class used to add and remove contents to imagify.conf file. |
| 8 |
* |
| 9 |
* @since 1.9 |
| 10 |
* @author Grégory Viguier |
| 11 |
*/ |
| 12 |
abstract class AbstractNginxDirConfFile extends AbstractWriteDirConfFile { |
| 13 |
|
| 14 |
/** |
| 15 |
* Insert new contents into the directory conf file. |
| 16 |
* Replaces existing marked info. Creates file if none exists. |
| 17 |
* |
| 18 |
* @since 1.9 |
| 19 |
* @access protected |
| 20 |
* @author Grégory Viguier |
| 21 |
* |
| 22 |
* @param string $new_contents Contents to insert. |
| 23 |
* @return bool|\WP_Error True on write success, a \WP_Error object on failure. |
| 24 |
*/ |
| 25 |
protected function insert_contents( $new_contents ) { |
| 26 |
$contents = $this->get_file_contents(); |
| 27 |
|
| 28 |
if ( is_wp_error( $contents ) ) { |
| 29 |
return $contents; |
| 30 |
} |
| 31 |
|
| 32 |
$start_marker = '# BEGIN ' . static::TAG_NAME; |
| 33 |
$end_marker = '# END ' . static::TAG_NAME; |
| 34 |
|
| 35 |
// Remove previous rules. |
| 36 |
$contents = preg_replace( '/\s*?' . preg_quote( $start_marker, '/' ) . '.*' . preg_quote( $end_marker, '/' ) . '\s*?/isU', "\n\n", $contents ); |
| 37 |
$contents = trim( $contents ); |
| 38 |
|
| 39 |
if ( $new_contents ) { |
| 40 |
$contents = $new_contents . "\n\n" . $contents; |
| 41 |
} |
| 42 |
|
| 43 |
return $this->put_file_contents( $contents ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Get new contents to write into the file. |
| 48 |
* |
| 49 |
* @since 1.9 |
| 50 |
* @access public |
| 51 |
* @author Grégory Viguier |
| 52 |
* |
| 53 |
* @return string |
| 54 |
*/ |
| 55 |
public function get_new_contents() { |
| 56 |
$contents = parent::get_new_contents(); |
| 57 |
|
| 58 |
if ( ! $contents ) { |
| 59 |
return ''; |
| 60 |
} |
| 61 |
|
| 62 |
return '# BEGIN ' . static::TAG_NAME . "\n" . $contents . "\n# END " . static::TAG_NAME; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Get the unfiltered path to the file. |
| 67 |
* |
| 68 |
* @since 1.9 |
| 69 |
* @access protected |
| 70 |
* @author Grégory Viguier |
| 71 |
* |
| 72 |
* @return string |
| 73 |
*/ |
| 74 |
protected function get_raw_file_path() { |
| 75 |
return $this->filesystem->get_site_root() . 'conf/imagify.conf'; |
| 76 |
} |
| 77 |
} |
| 78 |
|