| 1 |
<?php |
| 2 |
abstract class Rewrite_Rules_Abstract { |
| 3 |
|
| 4 |
/** |
| 5 |
* Name of the tag used as block delemiter. |
| 6 |
*/ |
| 7 |
protected $tag_name = 'Quickwebp: rewrite rules for webp'; |
| 8 |
|
| 9 |
/** |
| 10 |
* Add new contents to the file. |
| 11 |
*/ |
| 12 |
public function add() { |
| 13 |
$result = $this->insert_contents( $this->get_raw_new_contents() ); |
| 14 |
|
| 15 |
if ( is_wp_error( $result ) ) { |
| 16 |
add_action( 'admin_notices', function() use ( $result ) { |
| 17 |
?> |
| 18 |
<div class="notice notice-error"> |
| 19 |
<p><?php echo $result->get_error_message(); ?></p> |
| 20 |
</div> |
| 21 |
<?php |
| 22 |
}); |
| 23 |
} |
| 24 |
|
| 25 |
return true; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Remove contents from the file. |
| 30 |
*/ |
| 31 |
public function remove() { |
| 32 |
|
| 33 |
$result = $this->insert_contents( '' ); |
| 34 |
|
| 35 |
if ( is_wp_error( $result ) ) { |
| 36 |
add_action( 'admin_notices', function() use ( $result ) { |
| 37 |
?> |
| 38 |
<div class="notice notice-error"> |
| 39 |
<p><?php echo $result->get_error_message(); ?></p> |
| 40 |
</div> |
| 41 |
<?php |
| 42 |
}); |
| 43 |
} |
| 44 |
|
| 45 |
return true; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Insert new contents into the directory conf file. |
| 50 |
* Replaces existing marked info. Creates file if none exists. |
| 51 |
*/ |
| 52 |
protected function insert_contents( $new_contents ) { |
| 53 |
$contents = $this->get_file_contents(); |
| 54 |
|
| 55 |
if ( is_wp_error( $contents ) ) { |
| 56 |
return $contents; |
| 57 |
} |
| 58 |
|
| 59 |
$start_marker = '# BEGIN ' . $this->tag_name; |
| 60 |
$end_marker = '# END ' . $this->tag_name; |
| 61 |
|
| 62 |
// Remove previous rules. |
| 63 |
$contents = preg_replace( '/\s*?' . preg_quote( $start_marker, '/' ) . '.*' . preg_quote( $end_marker, '/' ) . '\s*?/isU', "\n\n", $contents ); |
| 64 |
$contents = trim( $contents ); |
| 65 |
|
| 66 |
if ( $new_contents ) { |
| 67 |
$contents = $new_contents . "\n\n" . $contents; |
| 68 |
} |
| 69 |
|
| 70 |
return $this->put_file_contents( $contents ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Get the path to the site's root. |
| 75 |
* This is an improved version of get_home_path() that *should* work in almost every cases. |
| 76 |
* Because creating a constant like ABSPATH was too simple. |
| 77 |
*/ |
| 78 |
protected function get_site_root() { |
| 79 |
|
| 80 |
$home = set_url_scheme( untrailingslashit( get_option( 'home' ) ), 'http' ); |
| 81 |
$siteurl = set_url_scheme( untrailingslashit( get_option( 'siteurl' ) ), 'http' ); |
| 82 |
|
| 83 |
if ( ! empty( $home ) && 0 !== strcasecmp( $home, $siteurl ) ) { |
| 84 |
$wp_path_rel_to_home = str_ireplace( $home, '', $siteurl ); /* $siteurl - $home */ |
| 85 |
$pos = strripos( str_replace( '\\', '/', ABSPATH ), trailingslashit( $wp_path_rel_to_home ) ); |
| 86 |
$root_path = substr( ABSPATH, 0, $pos ); |
| 87 |
$root_path = trailingslashit( wp_normalize_path( $root_path ) ); |
| 88 |
return $root_path; |
| 89 |
} |
| 90 |
|
| 91 |
if ( ! defined( 'PATH_CURRENT_SITE' ) || ! is_multisite() || is_main_site() ) { |
| 92 |
$root_path = ABSPATH; |
| 93 |
return $root_path; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* For a multisite in its own directory, get_home_path() returns the expected path only for the main site. |
| 98 |
* |
| 99 |
* Friend, each time an attempt is made to improve this method, and especially this part, please increment the following counter. |
| 100 |
* Improvement attempts: 3. |
| 101 |
*/ |
| 102 |
$document_root = realpath( wp_unslash( $_SERVER['DOCUMENT_ROOT'] ) ); // `realpath()` is needed for those cases where $_SERVER['DOCUMENT_ROOT'] is totally different from ABSPATH. |
| 103 |
$document_root = trailingslashit( str_replace( '\\', '/', $document_root ) ); |
| 104 |
$path_current_site = trim( str_replace( '\\', '/', PATH_CURRENT_SITE ), '/' ); |
| 105 |
$root_path = trailingslashit( wp_normalize_path( $document_root . $path_current_site ) ); |
| 106 |
|
| 107 |
return $root_path; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Get the file contents. |
| 112 |
*/ |
| 113 |
protected function get_file_contents() { |
| 114 |
|
| 115 |
$file_path = $this->get_file_path(); |
| 116 |
$file_exists = file_exists( $file_path ); |
| 117 |
if ( ! $file_exists ) { |
| 118 |
$dir_name = dirname( $file_path ); |
| 119 |
if ( ! file_exists( $dir_name ) ){ |
| 120 |
mkdir( $dir_name, 0755, true ); |
| 121 |
} |
| 122 |
touch( $file_path ); |
| 123 |
} |
| 124 |
$writable = wp_is_writable( $file_path ); |
| 125 |
|
| 126 |
if ( is_wp_error( $writable ) ) { |
| 127 |
return $writable; |
| 128 |
} |
| 129 |
|
| 130 |
$contents = @file_get_contents( $file_path ); |
| 131 |
|
| 132 |
if ( false === $contents ) { |
| 133 |
return new \WP_Error( |
| 134 |
'not_read', |
| 135 |
sprintf( |
| 136 |
__( 'The %s file could not be read.', QUICKWEBP_TEXT_DOMAIN ), |
| 137 |
'<code>' . esc_html( $file_path ) . '</code>' |
| 138 |
) |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
return $contents; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Write contents to the file. |
| 147 |
*/ |
| 148 |
protected function put_file_contents( $contents ) { |
| 149 |
|
| 150 |
$file_path = $this->get_file_path(); |
| 151 |
$result = $this->put_contents( $file_path, $contents ); |
| 152 |
|
| 153 |
if ( $result ) { |
| 154 |
return true; |
| 155 |
} |
| 156 |
|
| 157 |
return new \WP_Error( |
| 158 |
'edition_failed', |
| 159 |
sprintf( |
| 160 |
__( 'Could not write into the %s file.', QUICKWEBP_TEXT_DOMAIN ), |
| 161 |
'<code>' . esc_html( $file_path ) . '</code>' |
| 162 |
) |
| 163 |
); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Write contents to the file. |
| 168 |
*/ |
| 169 |
protected function put_contents( $file, $contents, $mode = false ) { |
| 170 |
|
| 171 |
$fp = @fopen( $file, 'wb' ); |
| 172 |
|
| 173 |
if ( ! $fp ) { |
| 174 |
return false; |
| 175 |
} |
| 176 |
|
| 177 |
mbstring_binary_safe_encoding(); |
| 178 |
|
| 179 |
$data_length = strlen( $contents ); |
| 180 |
|
| 181 |
$bytes_written = fwrite( $fp, $contents ); |
| 182 |
|
| 183 |
reset_mbstring_encoding(); |
| 184 |
|
| 185 |
fclose( $fp ); |
| 186 |
|
| 187 |
if ( $data_length !== $bytes_written ) { |
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
$chmod_file = fileperms( ABSPATH . 'index.php' ) & 0777 | 0644; |
| 192 |
chmod( $file, $chmod_file ); |
| 193 |
|
| 194 |
return true; |
| 195 |
} |
| 196 |
|
| 197 |
} |