Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/wp-conte.../plugins/wp-smush.../core/webp
File: class-webp-apache.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Smush\Core\Webp;
[2] Fix | Delete
[3] Fix | Delete
use Smush\Core\Server_Utils;
[4] Fix | Delete
[5] Fix | Delete
class Webp_Apache {
[6] Fix | Delete
/**
[7] Fix | Delete
* @var Webp_Dir
[8] Fix | Delete
*/
[9] Fix | Delete
private $webp_dir;
[10] Fix | Delete
[11] Fix | Delete
public function __construct() {
[12] Fix | Delete
$this->webp_dir = new Webp_Dir();
[13] Fix | Delete
}
[14] Fix | Delete
[15] Fix | Delete
public function get_rewrite_rules() {
[16] Fix | Delete
$location = is_multisite() ? 'uploads' : 'root';
[17] Fix | Delete
[18] Fix | Delete
$code = $this->marker_line() . "\n";
[19] Fix | Delete
$code .= $this->get_apache_code( $location );
[20] Fix | Delete
$code .= "\n" . $this->marker_line( true );
[21] Fix | Delete
[22] Fix | Delete
return $code;
[23] Fix | Delete
}
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* Code to use on Apache servers.
[27] Fix | Delete
*
[28] Fix | Delete
* @param string $location Where the .htaccess file is.
[29] Fix | Delete
*
[30] Fix | Delete
* @return string
[31] Fix | Delete
* @since 3.8.0
[32] Fix | Delete
*
[33] Fix | Delete
* @todo Find out what's wrong with the rules. We shouldn't need these two different RewriteRule.
[34] Fix | Delete
*
[35] Fix | Delete
*/
[36] Fix | Delete
private function get_apache_code( $location ) {
[37] Fix | Delete
$uploads_rel_path = trim( $this->webp_dir->get_upload_rel_path(), '/\\' );
[38] Fix | Delete
$webp_rel_path = trim( $this->webp_dir->get_webp_rel_path(), '/\\' );
[39] Fix | Delete
$webp_path = untrailingslashit( $this->webp_dir->get_webp_path() );
[40] Fix | Delete
[41] Fix | Delete
$rewrite_path = $this->is_document_root_matched() ? '%{DOCUMENT_ROOT}/' . $webp_rel_path : $webp_path;
[42] Fix | Delete
[43] Fix | Delete
$code = '<IfModule mod_rewrite.c>
[44] Fix | Delete
RewriteEngine On
[45] Fix | Delete
RewriteCond ' . $rewrite_path . '/disable_smush_webp !-f
[46] Fix | Delete
RewriteCond %{HTTP_ACCEPT} image/webp' . "\n";
[47] Fix | Delete
[48] Fix | Delete
if ( 'root' === $location ) {
[49] Fix | Delete
// This works on single sites at root.
[50] Fix | Delete
$code .= ' RewriteCond ' . $rewrite_path . '/$1.webp -f
[51] Fix | Delete
RewriteRule ' . $uploads_rel_path . '/(.*\.(?:png|jpe?g))$ ' . $webp_rel_path . '/$1.webp [NC,T=image/webp]';
[52] Fix | Delete
} else {
[53] Fix | Delete
// This works at /uploads/.
[54] Fix | Delete
$code .= ' RewriteCond ' . $rewrite_path . '/$1.$2.webp -f
[55] Fix | Delete
RewriteRule ^/?(.+)\.(jpe?g|png)$ /' . $webp_rel_path . '/$1.$2.webp [NC,T=image/webp]';
[56] Fix | Delete
}
[57] Fix | Delete
[58] Fix | Delete
$code .= "\n" . '</IfModule>
[59] Fix | Delete
[60] Fix | Delete
<IfModule mod_headers.c>
[61] Fix | Delete
Header append Vary Accept env=WEBP_image
[62] Fix | Delete
</IfModule>
[63] Fix | Delete
[64] Fix | Delete
<IfModule mod_mime.c>
[65] Fix | Delete
AddType image/webp .webp
[66] Fix | Delete
</IfModule>';
[67] Fix | Delete
[68] Fix | Delete
return apply_filters( 'smush_apache_webp_rules', $code );
[69] Fix | Delete
}
[70] Fix | Delete
[71] Fix | Delete
private function is_document_root_matched() {
[72] Fix | Delete
$document_root = ( new Server_Utils() )->get_document_root();
[73] Fix | Delete
$webp_rel_path = trim( $this->webp_dir->get_webp_rel_path(), '/\\' );
[74] Fix | Delete
$webp_path = trailingslashit( $document_root ) . $webp_rel_path;
[75] Fix | Delete
$real_webp_path = untrailingslashit( $this->webp_dir->get_webp_path() );
[76] Fix | Delete
return $webp_path === $real_webp_path;
[77] Fix | Delete
}
[78] Fix | Delete
[79] Fix | Delete
/**
[80] Fix | Delete
* Get unique string to use as marker comment line in .htaccess or nginx config file.
[81] Fix | Delete
*
[82] Fix | Delete
* @param bool $end whether to use marker after end of the config code.
[83] Fix | Delete
*
[84] Fix | Delete
* @return string
[85] Fix | Delete
*/
[86] Fix | Delete
private function marker_line( $end = false ) {
[87] Fix | Delete
if ( true === $end ) {
[88] Fix | Delete
return '# END ' . $this->marker_suffix();
[89] Fix | Delete
} else {
[90] Fix | Delete
return '# BEGIN ' . $this->marker_suffix();
[91] Fix | Delete
}
[92] Fix | Delete
}
[93] Fix | Delete
[94] Fix | Delete
/**
[95] Fix | Delete
* Get unique string to use at marker comment line in .htaccess or nginx config file.
[96] Fix | Delete
*
[97] Fix | Delete
* @return string
[98] Fix | Delete
* @since 3.8.0
[99] Fix | Delete
*
[100] Fix | Delete
*/
[101] Fix | Delete
private function marker_suffix() {
[102] Fix | Delete
return 'SMUSH-WEBP';
[103] Fix | Delete
}
[104] Fix | Delete
[105] Fix | Delete
/**
[106] Fix | Delete
* Gets the path of .htaccess file for the given location.
[107] Fix | Delete
*
[108] Fix | Delete
* @param string $location Location of the .htaccess file to retrieve. root|uploads.
[109] Fix | Delete
*
[110] Fix | Delete
* @return string
[111] Fix | Delete
*/
[112] Fix | Delete
private function get_htaccess_file_path( $location ) {
[113] Fix | Delete
$base_dir = 'root' === $location ? get_home_path() : $this->webp_dir->get_upload_path();
[114] Fix | Delete
[115] Fix | Delete
return rtrim( $base_dir, '/' ) . '/.htaccess';
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
/**
[119] Fix | Delete
* Check if .htaccess has rules for this module in place.
[120] Fix | Delete
*
[121] Fix | Delete
* @param bool|string $location Location of the .htaccess to check.
[122] Fix | Delete
*
[123] Fix | Delete
* @return bool
[124] Fix | Delete
* @since 3.8.0
[125] Fix | Delete
*
[126] Fix | Delete
*/
[127] Fix | Delete
public function is_htaccess_written( $location = false ) {
[128] Fix | Delete
if ( ! function_exists( 'extract_from_markers' ) ) {
[129] Fix | Delete
require_once ABSPATH . 'wp-admin/includes/misc.php';
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
$has_rules = false;
[133] Fix | Delete
[134] Fix | Delete
// Remove the rules from all the possible places if not specified.
[135] Fix | Delete
$locations = ! $location ? $this->get_htaccess_locations() : array( $location );
[136] Fix | Delete
[137] Fix | Delete
foreach ( $locations as $name ) {
[138] Fix | Delete
$htaccess = $this->get_htaccess_file_path( $name );
[139] Fix | Delete
$has_rules = ! empty( $has_rules ) || array_filter( extract_from_markers( $htaccess, $this->marker_suffix() ) );
[140] Fix | Delete
}
[141] Fix | Delete
[142] Fix | Delete
return $has_rules;
[143] Fix | Delete
}
[144] Fix | Delete
[145] Fix | Delete
/**
[146] Fix | Delete
* Returns the handled locations for the .htaccess.
[147] Fix | Delete
*
[148] Fix | Delete
* @return array
[149] Fix | Delete
* @since 3.8.3
[150] Fix | Delete
*
[151] Fix | Delete
*/
[152] Fix | Delete
public function get_htaccess_locations() {
[153] Fix | Delete
if ( ! is_multisite() ) {
[154] Fix | Delete
$locations[] = 'root';
[155] Fix | Delete
}
[156] Fix | Delete
$locations[] = 'uploads';
[157] Fix | Delete
[158] Fix | Delete
return $locations;
[159] Fix | Delete
}
[160] Fix | Delete
[161] Fix | Delete
public function save_htaccess( $location ) {
[162] Fix | Delete
$htaccess = $this->get_htaccess_file_path( $location );
[163] Fix | Delete
$code = $this->get_apache_code( $location );
[164] Fix | Delete
$code = explode( "\n", $code );
[165] Fix | Delete
return insert_with_markers( $htaccess, $this->marker_suffix(), $code );
[166] Fix | Delete
}
[167] Fix | Delete
[168] Fix | Delete
/**
[169] Fix | Delete
* Remove rules from .htaccess file.
[170] Fix | Delete
*
[171] Fix | Delete
* @param bool|string $location Location of the htaccess to unsave. uploads|root.
[172] Fix | Delete
*
[173] Fix | Delete
* @return string|null Error message or empty on success.
[174] Fix | Delete
* @since 3.8.0
[175] Fix | Delete
*/
[176] Fix | Delete
public function unsave_htaccess( $location = false ) {
[177] Fix | Delete
if ( ! $this->is_htaccess_written( $location ) ) {
[178] Fix | Delete
return esc_html__( "The .htaccess file doesn't contain the WebP rules from Smush.", 'wp-smushit' );
[179] Fix | Delete
}
[180] Fix | Delete
[181] Fix | Delete
$markers_inserted = false;
[182] Fix | Delete
[183] Fix | Delete
// Remove the rules from all the possible places if not specified.
[184] Fix | Delete
$locations = ! $location ? $this->get_htaccess_locations() : array( $location );
[185] Fix | Delete
[186] Fix | Delete
foreach ( $locations as $name ) {
[187] Fix | Delete
$htaccess = $this->get_htaccess_file_path( $name );
[188] Fix | Delete
$markers_inserted = insert_with_markers( $htaccess, $this->marker_suffix(), '' ) || ! empty( $markers_inserted );
[189] Fix | Delete
}
[190] Fix | Delete
[191] Fix | Delete
if ( ! $markers_inserted ) {
[192] Fix | Delete
return esc_html__( 'We were unable to automatically remove the rules. We recommend trying to remove the rules manually. If you don’t have access to the .htaccess file to remove it manually, please consult with your hosting provider to change the configuration on the server.', 'wp-smushit' );
[193] Fix | Delete
}
[194] Fix | Delete
}
[195] Fix | Delete
}
[196] Fix | Delete
[197] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function