rebase from live enviornment
This commit is contained in:
38
wp/plugins/imagify/classes/Webp/Apache.php
Normal file
38
wp/plugins/imagify/classes/Webp/Apache.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
namespace Imagify\Webp;
|
||||
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Add and remove contents to the .htaccess file to display WebP images on the site.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
class Apache extends \Imagify\WriteFile\AbstractApacheDirConfFile {
|
||||
|
||||
/**
|
||||
* Name of the tag used as block delemiter.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
const TAG_NAME = 'Imagify: webp file type';
|
||||
|
||||
/**
|
||||
* Get unfiltered new contents to write into the file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_raw_new_contents() {
|
||||
return trim( '
|
||||
<IfModule mod_mime.c>
|
||||
AddType image/webp .webp
|
||||
</IfModule>' );
|
||||
}
|
||||
}
|
||||
239
wp/plugins/imagify/classes/Webp/Display.php
Normal file
239
wp/plugins/imagify/classes/Webp/Display.php
Normal file
@@ -0,0 +1,239 @@
|
||||
<?php
|
||||
namespace Imagify\Webp;
|
||||
|
||||
use Imagify\Notices\Notices;
|
||||
use Imagify\Traits\InstanceGetterTrait;
|
||||
|
||||
/**
|
||||
* Display WebP images on the site.
|
||||
*
|
||||
* @since 1.9
|
||||
*/
|
||||
class Display {
|
||||
use InstanceGetterTrait;
|
||||
|
||||
/**
|
||||
* Server conf object.
|
||||
*
|
||||
* @var \Imagify\WriteFile\WriteFileInterface
|
||||
* @since 1.9
|
||||
*/
|
||||
protected $server_conf;
|
||||
|
||||
/**
|
||||
* Init.
|
||||
*
|
||||
* @since 1.9
|
||||
*/
|
||||
public function init() {
|
||||
add_filter( 'imagify_settings_on_save', [ $this, 'maybe_add_rewrite_rules' ] );
|
||||
add_action( 'imagify_settings_webp_info', [ $this, 'maybe_add_webp_info' ] );
|
||||
add_action( 'imagify_activation', [ $this, 'activate' ] );
|
||||
add_action( 'imagify_deactivation', [ $this, 'deactivate' ] );
|
||||
|
||||
Picture\Display::get_instance()->init();
|
||||
RewriteRules\Display::get_instance()->init();
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** HOOKS =================================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* If display WebP images, add the WebP type to the .htaccess/etc file.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param array $values The option values.
|
||||
* @return array
|
||||
*/
|
||||
public function maybe_add_rewrite_rules( $values ) {
|
||||
$old_value = (bool) get_imagify_option( 'display_webp' );
|
||||
// See \Imagify_Options->validate_values_on_update() for why we use 'convert_to_webp' here.
|
||||
$new_value = ! empty( $values['display_webp'] ) && ! empty( $values['convert_to_webp'] );
|
||||
|
||||
if ( $old_value === $new_value ) {
|
||||
// No changes.
|
||||
return $values;
|
||||
}
|
||||
|
||||
if ( ! $this->get_server_conf() ) {
|
||||
return $values;
|
||||
}
|
||||
|
||||
if ( $new_value ) {
|
||||
// Add the WebP file type.
|
||||
$result = $this->get_server_conf()->add();
|
||||
} else {
|
||||
// Remove the WebP file type.
|
||||
$result = $this->get_server_conf()->remove();
|
||||
}
|
||||
|
||||
if ( ! is_wp_error( $result ) ) {
|
||||
return $values;
|
||||
}
|
||||
|
||||
// Display an error message.
|
||||
if ( is_multisite() && strpos( wp_get_referer(), network_admin_url( '/' ) ) === 0 ) {
|
||||
Notices::get_instance()->add_network_temporary_notice( $result->get_error_message() );
|
||||
} else {
|
||||
Notices::get_instance()->add_site_temporary_notice( $result->get_error_message() );
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the conf file is not writable, add a warning.
|
||||
*
|
||||
* @since 1.9
|
||||
*/
|
||||
public function maybe_add_webp_info() {
|
||||
$conf = $this->get_server_conf();
|
||||
|
||||
if ( ! $conf ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$writable = $conf->is_file_writable();
|
||||
|
||||
if ( ! is_wp_error( $writable ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$rules = $conf->get_new_contents();
|
||||
|
||||
if ( ! $rules ) {
|
||||
// Uh?
|
||||
return;
|
||||
}
|
||||
|
||||
echo '<br/>';
|
||||
|
||||
printf(
|
||||
/* translators: %s is a file name. */
|
||||
esc_html__( 'Imagify does not seem to be able to edit or create a %s file, you will have to add the following lines manually to it:', 'imagify' ),
|
||||
'<code>' . $this->get_file_path( true ) . '</code>'
|
||||
);
|
||||
|
||||
echo '<pre class="code">' . esc_html( $rules ) . '</pre>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Add rules on plugin activation.
|
||||
*
|
||||
* @since 1.9
|
||||
*/
|
||||
public function activate() {
|
||||
$conf = $this->get_server_conf();
|
||||
|
||||
if ( ! $conf ) {
|
||||
return;
|
||||
}
|
||||
if ( ! get_imagify_option( 'display_webp' ) ) {
|
||||
return;
|
||||
}
|
||||
if ( is_wp_error( $conf->is_file_writable() ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$conf->add();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove rules on plugin deactivation.
|
||||
*
|
||||
* @since 1.9
|
||||
*/
|
||||
public function deactivate() {
|
||||
$conf = $this->get_server_conf();
|
||||
|
||||
if ( ! $conf ) {
|
||||
return;
|
||||
}
|
||||
if ( ! get_imagify_option( 'display_webp' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$file_path = $conf->get_file_path();
|
||||
$filesystem = \Imagify_Filesystem::get_instance();
|
||||
|
||||
if ( ! $filesystem->exists( $file_path ) ) {
|
||||
return;
|
||||
}
|
||||
if ( ! $filesystem->is_writable( $file_path ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$conf->remove();
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** TOOLS =================================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get the path to the directory conf file.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param bool $relative True to get a path relative to the site’s root.
|
||||
* @return string|bool The file path. False on failure.
|
||||
*/
|
||||
public function get_file_path( $relative = false ) {
|
||||
if ( ! $this->get_server_conf() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$file_path = $this->get_server_conf()->get_file_path();
|
||||
|
||||
if ( $relative ) {
|
||||
return \Imagify_Filesystem::get_instance()->make_path_relative( $file_path );
|
||||
}
|
||||
|
||||
return $file_path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the WebP display method by validating the given value.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param array $values The option values.
|
||||
* @return string 'picture' or 'rewrite'.
|
||||
*/
|
||||
public function get_display_webp_method( $values ) {
|
||||
$options = \Imagify_Options::get_instance();
|
||||
$default = $options->get_default_values();
|
||||
$default = $default['display_webp_method'];
|
||||
$method = ! empty( $values['display_webp_method'] ) ? $values['display_webp_method'] : '';
|
||||
|
||||
return $options->sanitize_and_validate( 'display_webp_method', $method, $default );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the server conf instance.
|
||||
* Note: nothing needed for nginx.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @return \Imagify\WriteFile\WriteFileInterface
|
||||
*/
|
||||
protected function get_server_conf() {
|
||||
global $is_apache, $is_iis7;
|
||||
|
||||
if ( isset( $this->server_conf ) ) {
|
||||
return $this->server_conf;
|
||||
}
|
||||
|
||||
if ( $is_apache ) {
|
||||
$this->server_conf = new Apache();
|
||||
} elseif ( $is_iis7 ) {
|
||||
$this->server_conf = new IIS();
|
||||
} else {
|
||||
$this->server_conf = false;
|
||||
}
|
||||
|
||||
return $this->server_conf;
|
||||
}
|
||||
}
|
||||
39
wp/plugins/imagify/classes/Webp/IIS.php
Normal file
39
wp/plugins/imagify/classes/Webp/IIS.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
namespace Imagify\Webp;
|
||||
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Add and remove contents to the web.config file to display WebP images on the site.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
class IIS extends \Imagify\WriteFile\AbstractIISDirConfFile {
|
||||
|
||||
/**
|
||||
* Name of the tag used as block delemiter.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
const TAG_NAME = 'Imagify: webp file type';
|
||||
|
||||
/**
|
||||
* Get unfiltered new contents to write into the file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_raw_new_contents() {
|
||||
return trim( '
|
||||
<!-- @parent /configuration/system.webServer -->
|
||||
<staticContent name="' . esc_attr( static::TAG_NAME ) . ' 1">
|
||||
<mimeMap fileExtension=".webp" mimeType="image/webp" />
|
||||
</staticContent>' );
|
||||
}
|
||||
}
|
||||
800
wp/plugins/imagify/classes/Webp/Picture/Display.php
Normal file
800
wp/plugins/imagify/classes/Webp/Picture/Display.php
Normal file
@@ -0,0 +1,800 @@
|
||||
<?php
|
||||
namespace Imagify\Webp\Picture;
|
||||
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Display WebP images on the site with <picture> tags.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
class Display {
|
||||
use \Imagify\Traits\InstanceGetterTrait;
|
||||
|
||||
/**
|
||||
* Option value.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
const OPTION_VALUE = 'picture';
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
|
||||
/**
|
||||
* Init.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public function init() {
|
||||
add_action( 'template_redirect', [ $this, 'start_content_process' ], -1000 );
|
||||
add_filter( 'imagify_process_webp_content', [ $this, 'process_content' ] );
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** ADD <PICTURE> TAGS TO THE PAGE ========================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Start buffering the page content.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public function start_content_process() {
|
||||
if ( ! get_imagify_option( 'display_webp' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( self::OPTION_VALUE !== get_imagify_option( 'display_webp_method' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prevent the replacement of <img> tags into <picture> tags.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param bool $allow True to allow the use of <picture> tags (default). False to prevent their use.
|
||||
*/
|
||||
$allow = apply_filters( 'imagify_allow_picture_tags_for_webp', true );
|
||||
|
||||
if ( ! $allow ) {
|
||||
return;
|
||||
}
|
||||
|
||||
ob_start( [ $this, 'maybe_process_buffer' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe process the page content.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $buffer The buffer content.
|
||||
* @return string
|
||||
*/
|
||||
public function maybe_process_buffer( $buffer ) {
|
||||
if ( ! $this->is_html( $buffer ) ) {
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
if ( strlen( $buffer ) <= 255 ) {
|
||||
// Buffer length must be > 255 (IE does not read pages under 255 c).
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
$buffer = $this->process_content( $buffer );
|
||||
|
||||
/**
|
||||
* Filter the page content after Imagify.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $buffer The page content.
|
||||
*/
|
||||
$buffer = (string) apply_filters( 'imagify_buffer', $buffer );
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the content.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $content The content.
|
||||
* @return string
|
||||
*/
|
||||
public function process_content( $content ) {
|
||||
$html_no_picture_tags = $this->remove_picture_tags( $content );
|
||||
$images = $this->get_images( $html_no_picture_tags );
|
||||
|
||||
if ( ! $images ) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
foreach ( $images as $image ) {
|
||||
$tag = $this->build_picture_tag( $image );
|
||||
$content = str_replace( $image['tag'], $tag, $content );
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove pre-existing <picture> tags.
|
||||
*
|
||||
* We shouldn't replace images already nested inside picture tags
|
||||
* that are already in the page.
|
||||
*
|
||||
* @since 1.10.0
|
||||
*
|
||||
* @param string $html Content of the page.
|
||||
*
|
||||
* @return string HTML content without pre-existing <picture> tags.
|
||||
*/
|
||||
private function remove_picture_tags( $html ) {
|
||||
$replace = preg_replace( '#<picture[^>]*>.*?<\/picture\s*>#mis', '', $html );
|
||||
|
||||
if ( null === $replace ) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
return $replace;
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** BUILD HTML TAGS AND ATTRIBUTES ========================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Build the <picture> tag to insert.
|
||||
*
|
||||
* @since 1.9
|
||||
* @see $this->process_image()
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $image An array of data.
|
||||
* @return string A <picture> tag.
|
||||
*/
|
||||
protected function build_picture_tag( $image ) {
|
||||
$to_remove = [
|
||||
'alt' => '',
|
||||
'height' => '',
|
||||
'width' => '',
|
||||
'data-lazy-src' => '',
|
||||
'data-src' => '',
|
||||
'src' => '',
|
||||
'data-lazy-srcset' => '',
|
||||
'data-srcset' => '',
|
||||
'srcset' => '',
|
||||
'data-lazy-sizes' => '',
|
||||
'data-sizes' => '',
|
||||
'sizes' => '',
|
||||
];
|
||||
|
||||
$attributes = array_diff_key( $image['attributes'], $to_remove );
|
||||
|
||||
/**
|
||||
* Filter the attributes to be added to the <picture> tag.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $attributes A list of attributes to be added to the <picture> tag.
|
||||
* @param array $data Data built from the originale <img> tag. See $this->process_image().
|
||||
*/
|
||||
$attributes = apply_filters( 'imagify_picture_attributes', $attributes, $image );
|
||||
|
||||
/**
|
||||
* Remove Gutenberg specific attributes from picture tag, leave them on img tag.
|
||||
* Optional: $attributes['class'] = 'imagify-webp-cover-wrapper'; for website admin styling ease.
|
||||
*/
|
||||
if ( ! empty( $image['attributes']['class'] ) && strpos( $image['attributes']['class'], 'wp-block-cover__image-background' ) !== false ) {
|
||||
unset( $attributes['style'] );
|
||||
unset( $attributes['class'] );
|
||||
unset( $attributes['data-object-fit'] );
|
||||
unset( $attributes['data-object-position'] );
|
||||
}
|
||||
|
||||
$output = '<picture' . $this->build_attributes( $attributes ) . ">\n";
|
||||
/**
|
||||
* Allow to add more <source> tags to the <picture> tag.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $more_source_tags Additional <source> tags.
|
||||
* @param array $data Data built from the originale <img> tag. See $this->process_image().
|
||||
*/
|
||||
$output .= apply_filters( 'imagify_additional_source_tags', '', $image );
|
||||
$output .= $this->build_source_tag( $image );
|
||||
$output .= $this->build_img_tag( $image );
|
||||
$output .= "</picture>\n";
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the <source> tag to insert in the <picture>.
|
||||
*
|
||||
* @since 1.9
|
||||
* @see $this->process_image()
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $image An array of data.
|
||||
* @return string A <source> tag.
|
||||
*/
|
||||
protected function build_source_tag( $image ) {
|
||||
$srcset_source = ! empty( $image['srcset_attribute'] ) ? $image['srcset_attribute'] : $image['src_attribute'] . 'set';
|
||||
$attributes = [
|
||||
'type' => 'image/webp',
|
||||
$srcset_source => [],
|
||||
];
|
||||
|
||||
if ( ! empty( $image['srcset'] ) ) {
|
||||
foreach ( $image['srcset'] as $srcset ) {
|
||||
if ( empty( $srcset['webp_url'] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$attributes[ $srcset_source ][] = $srcset['webp_url'] . ' ' . $srcset['descriptor'];
|
||||
}
|
||||
}
|
||||
|
||||
if ( empty( $attributes[ $srcset_source ] ) ) {
|
||||
$attributes[ $srcset_source ][] = $image['src']['webp_url'];
|
||||
}
|
||||
|
||||
$attributes[ $srcset_source ] = implode( ', ', $attributes[ $srcset_source ] );
|
||||
|
||||
foreach ( [ 'data-lazy-srcset', 'data-srcset', 'srcset' ] as $srcset_attr ) {
|
||||
if ( ! empty( $image['attributes'][ $srcset_attr ] ) && $srcset_attr !== $srcset_source ) {
|
||||
$attributes[ $srcset_attr ] = $image['attributes'][ $srcset_attr ];
|
||||
}
|
||||
}
|
||||
|
||||
if ( 'srcset' !== $srcset_source && empty( $attributes['srcset'] ) && ! empty( $image['attributes']['src'] ) ) {
|
||||
// Lazyload: the "src" attr should contain a placeholder (a data image or a blank.gif ).
|
||||
$attributes['srcset'] = $image['attributes']['src'];
|
||||
}
|
||||
|
||||
foreach ( [ 'data-lazy-sizes', 'data-sizes', 'sizes' ] as $sizes_attr ) {
|
||||
if ( ! empty( $image['attributes'][ $sizes_attr ] ) ) {
|
||||
$attributes[ $sizes_attr ] = $image['attributes'][ $sizes_attr ];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the attributes to be added to the <source> tag.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $attributes A list of attributes to be added to the <source> tag.
|
||||
* @param array $data Data built from the original <img> tag. See $this->process_image().
|
||||
*/
|
||||
$attributes = apply_filters( 'imagify_picture_source_attributes', $attributes, $image );
|
||||
|
||||
return '<source' . $this->build_attributes( $attributes ) . "/>\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the <img> tag to insert in the <picture>.
|
||||
*
|
||||
* @since 1.9
|
||||
* @see $this->process_image()
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $image An array of data.
|
||||
* @return string A <img> tag.
|
||||
*/
|
||||
protected function build_img_tag( $image ) {
|
||||
/**
|
||||
* Gutenberg fix.
|
||||
* Check for the 'wp-block-cover__image-background' class on the original image, and leave that class and style attributes if found.
|
||||
*/
|
||||
if ( ! empty( $image['attributes']['class'] ) && strpos( $image['attributes']['class'], 'wp-block-cover__image-background' ) !== false ) {
|
||||
$to_remove = [
|
||||
'id' => '',
|
||||
'title' => '',
|
||||
];
|
||||
|
||||
$attributes = array_diff_key( $image['attributes'], $to_remove );
|
||||
} else {
|
||||
$to_remove = [
|
||||
'class' => '',
|
||||
'id' => '',
|
||||
'style' => '',
|
||||
'title' => '',
|
||||
];
|
||||
|
||||
$attributes = array_diff_key( $image['attributes'], $to_remove );
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the attributes to be added to the <img> tag.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $attributes A list of attributes to be added to the <img> tag.
|
||||
* @param array $data Data built from the originale <img> tag. See $this->process_image().
|
||||
*/
|
||||
$attributes = apply_filters( 'imagify_picture_img_attributes', $attributes, $image );
|
||||
|
||||
return '<img' . $this->build_attributes( $attributes ) . "/>\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Create HTML attributes from an array.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $attributes A list of attribute pairs.
|
||||
* @return string HTML attributes.
|
||||
*/
|
||||
protected function build_attributes( $attributes ) {
|
||||
if ( ! $attributes || ! is_array( $attributes ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$out = '';
|
||||
|
||||
foreach ( $attributes as $attribute => $value ) {
|
||||
$out .= ' ' . $attribute . '="' . esc_attr( $value ) . '"';
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** VARIOUS TOOLS =========================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get a list of images in a content.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $content The content.
|
||||
* @return array
|
||||
*/
|
||||
protected function get_images( $content ) {
|
||||
// Remove comments.
|
||||
$content = preg_replace( '/<!--(.*)-->/Uis', '', $content );
|
||||
|
||||
if ( ! preg_match_all( '/<img\s.*>/isU', $content, $matches ) ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$images = array_map( [ $this, 'process_image' ], $matches[0] );
|
||||
$images = array_filter( $images );
|
||||
|
||||
/**
|
||||
* Filter the images to display with a <picture> tag.
|
||||
*
|
||||
* @since 1.9
|
||||
* @see $this->process_image()
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $images A list of arrays.
|
||||
* @param string $content The page content.
|
||||
*/
|
||||
$images = apply_filters( 'imagify_webp_picture_images_to_display', $images, $content );
|
||||
|
||||
if ( ! $images || ! is_array( $images ) ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
foreach ( $images as $i => $image ) {
|
||||
if ( empty( $image['src']['webp_exists'] ) || empty( $image['src']['webp_url'] ) ) {
|
||||
unset( $images[ $i ] );
|
||||
continue;
|
||||
}
|
||||
|
||||
unset( $images[ $i ]['src']['webp_path'], $images[ $i ]['src']['webp_exists'] );
|
||||
|
||||
if ( empty( $image['srcset'] ) || ! is_array( $image['srcset'] ) ) {
|
||||
unset( $images[ $i ]['srcset'] );
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ( $image['srcset'] as $j => $srcset ) {
|
||||
if ( ! is_array( $srcset ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( empty( $srcset['webp_exists'] ) || empty( $srcset['webp_url'] ) ) {
|
||||
unset( $images[ $i ]['srcset'][ $j ]['webp_url'] );
|
||||
}
|
||||
|
||||
unset( $images[ $i ]['srcset'][ $j ]['webp_path'], $images[ $i ]['srcset'][ $j ]['webp_exists'] );
|
||||
}
|
||||
}
|
||||
|
||||
return $images;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process an image tag and get an array containing some data.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $image An image html tag.
|
||||
* @return array|false {
|
||||
* An array of data if the image has a WebP version. False otherwise.
|
||||
*
|
||||
* @type string $tag The image tag.
|
||||
* @type array $attributes The image attributes (minus src and srcset).
|
||||
* @type array $src {
|
||||
* @type string $url URL to the original image.
|
||||
* @type string $webp_url URL to the WebP version.
|
||||
* }
|
||||
* @type array $srcset {
|
||||
* An array or arrays. Not set if not applicable.
|
||||
*
|
||||
* @type string $url URL to the original image.
|
||||
* @type string $webp_url URL to the WebP version. Not set if not applicable.
|
||||
* @type string $descriptor A src descriptor.
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
protected function process_image( $image ) {
|
||||
static $extensions;
|
||||
|
||||
$atts_pattern = '/(?<name>[^\s"\']+)\s*=\s*(["\'])\s*(?<value>.*?)\s*\2/s';
|
||||
|
||||
if ( ! preg_match_all( $atts_pattern, $image, $tmp_attributes, PREG_SET_ORDER ) ) {
|
||||
// No attributes?
|
||||
return false;
|
||||
}
|
||||
|
||||
$attributes = [];
|
||||
|
||||
foreach ( $tmp_attributes as $attribute ) {
|
||||
$attributes[ $attribute['name'] ] = $attribute['value'];
|
||||
}
|
||||
|
||||
if ( ! empty( $attributes['class'] ) && strpos( $attributes['class'], 'imagify-no-webp' ) !== false ) {
|
||||
// Has the 'imagify-no-webp' class.
|
||||
return false;
|
||||
}
|
||||
|
||||
// Deal with the src attribute.
|
||||
$src_source = false;
|
||||
|
||||
foreach ( [ 'data-lazy-src', 'data-src', 'src' ] as $src_attr ) {
|
||||
if ( ! empty( $attributes[ $src_attr ] ) ) {
|
||||
$src_source = $src_attr;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $src_source ) {
|
||||
// No src attribute.
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! isset( $extensions ) ) {
|
||||
$extensions = imagify_get_mime_types( 'image' );
|
||||
$extensions = array_keys( $extensions );
|
||||
$extensions = implode( '|', $extensions );
|
||||
}
|
||||
|
||||
if ( ! preg_match( '@^(?<src>(?:(?:https?:)?//|/).+\.(?<extension>' . $extensions . '))(?<query>\?.*)?$@i', $attributes[ $src_source ], $src ) ) {
|
||||
// Not a supported image format.
|
||||
return false;
|
||||
}
|
||||
|
||||
$webp_url = imagify_path_to_webp( $src['src'] );
|
||||
$webp_path = $this->url_to_path( $webp_url );
|
||||
$webp_url .= ! empty( $src['query'] ) ? $src['query'] : '';
|
||||
|
||||
$data = [
|
||||
'tag' => $image,
|
||||
'attributes' => $attributes,
|
||||
'src_attribute' => $src_source,
|
||||
'src' => [
|
||||
'url' => $attributes[ $src_source ],
|
||||
'webp_url' => $webp_url,
|
||||
'webp_path' => $webp_path,
|
||||
'webp_exists' => $webp_path && $this->filesystem->exists( $webp_path ),
|
||||
],
|
||||
'srcset_attribute' => false,
|
||||
'srcset' => [],
|
||||
];
|
||||
|
||||
// Deal with the srcset attribute.
|
||||
$srcset_source = false;
|
||||
|
||||
foreach ( [ 'data-lazy-srcset', 'data-srcset', 'srcset' ] as $srcset_attr ) {
|
||||
if ( ! empty( $attributes[ $srcset_attr ] ) ) {
|
||||
$srcset_source = $srcset_attr;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $srcset_source ) {
|
||||
$data['srcset_attribute'] = $srcset_source;
|
||||
|
||||
$srcset = explode( ',', $attributes[ $srcset_source ] );
|
||||
|
||||
foreach ( $srcset as $srcs ) {
|
||||
$srcs = preg_split( '/\s+/', trim( $srcs ) );
|
||||
|
||||
if ( count( $srcs ) > 2 ) {
|
||||
// Not a good idea to have space characters in file name.
|
||||
$descriptor = array_pop( $srcs );
|
||||
$srcs = [ implode( ' ', $srcs ), $descriptor ];
|
||||
}
|
||||
|
||||
if ( empty( $srcs[1] ) ) {
|
||||
$srcs[1] = '1x';
|
||||
}
|
||||
|
||||
if ( ! preg_match( '@^(?<src>(?:https?:)?//.+\.(?<extension>' . $extensions . '))(?<query>\?.*)?$@i', $srcs[0], $src ) ) {
|
||||
// Not a supported image format.
|
||||
$data['srcset'][] = [
|
||||
'url' => $srcs[0],
|
||||
'descriptor' => $srcs[1],
|
||||
];
|
||||
continue;
|
||||
}
|
||||
|
||||
$webp_url = imagify_path_to_webp( $src['src'] );
|
||||
$webp_path = $this->url_to_path( $webp_url );
|
||||
$webp_url .= ! empty( $src['query'] ) ? $src['query'] : '';
|
||||
|
||||
$data['srcset'][] = [
|
||||
'url' => $srcs[0],
|
||||
'descriptor' => $srcs[1],
|
||||
'webp_url' => $webp_url,
|
||||
'webp_path' => $webp_path,
|
||||
'webp_exists' => $webp_path && $this->filesystem->exists( $webp_path ),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter a processed image tag.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $data An array of data for this image.
|
||||
* @param string $image An image html tag.
|
||||
*/
|
||||
$data = apply_filters( 'imagify_webp_picture_process_image', $data, $image );
|
||||
|
||||
if ( ! $data || ! is_array( $data ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! isset( $data['tag'], $data['attributes'], $data['src_attribute'], $data['src'], $data['srcset_attribute'], $data['srcset'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if a content is HTML.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $content The content.
|
||||
* @return bool
|
||||
*/
|
||||
protected function is_html( $content ) {
|
||||
return preg_match( '/<\/html>/i', $content );
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a file URL to an absolute path.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $url A file URL.
|
||||
* @return string|bool The file path. False on failure.
|
||||
*/
|
||||
protected function url_to_path( $url ) {
|
||||
static $uploads_url;
|
||||
static $uploads_dir;
|
||||
static $root_url;
|
||||
static $root_dir;
|
||||
static $cdn_url;
|
||||
static $domain_url;
|
||||
|
||||
/**
|
||||
* $url, $uploads_url, $root_url, and $cdn_url are passed through `set_url_scheme()` only to make sure `stripos()` doesn't fail over a stupid http/https difference.
|
||||
*/
|
||||
if ( ! isset( $uploads_url ) ) {
|
||||
$uploads_url = set_url_scheme( $this->filesystem->get_upload_baseurl() );
|
||||
$uploads_dir = $this->filesystem->get_upload_basedir( true );
|
||||
$root_url = set_url_scheme( $this->filesystem->get_site_root_url() );
|
||||
$root_dir = $this->filesystem->get_site_root();
|
||||
$cdn_url = $this->get_cdn_source();
|
||||
$cdn_url = $cdn_url['url'] ? set_url_scheme( $cdn_url['url'] ) : false;
|
||||
$domain_url = wp_parse_url( $root_url );
|
||||
|
||||
if ( ! empty( $domain_url['scheme'] ) && ! empty( $domain_url['host'] ) ) {
|
||||
$domain_url = $domain_url['scheme'] . '://' . $domain_url['host'] . '/';
|
||||
} else {
|
||||
$domain_url = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Get the right URL format.
|
||||
if ( $domain_url && strpos( $url, '/' ) === 0 ) {
|
||||
// URL like `/path/to/image.jpg.webp`.
|
||||
$url = $domain_url . ltrim( $url, '/' );
|
||||
}
|
||||
|
||||
$url = set_url_scheme( $url );
|
||||
|
||||
if ( $cdn_url && $domain_url && stripos( $url, $cdn_url ) === 0 ) {
|
||||
// CDN.
|
||||
$url = str_ireplace( $cdn_url, $domain_url, $url );
|
||||
}
|
||||
|
||||
// Return the path.
|
||||
if ( stripos( $url, $uploads_url ) === 0 ) {
|
||||
return str_ireplace( $uploads_url, $uploads_dir, $url );
|
||||
}
|
||||
|
||||
if ( stripos( $url, $root_url ) === 0 ) {
|
||||
return str_ireplace( $root_url, $root_dir, $url );
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the CDN "source".
|
||||
*
|
||||
* @since 1.9.3
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $option_url An URL to use instead of the one stored in the option. It is used only if no constant/filter.
|
||||
* @return array {
|
||||
* @type string $source Where does it come from? Possible values are 'constant', 'filter', or 'option'.
|
||||
* @type string $name Who? Can be a constant name, a plugin name, or an empty string.
|
||||
* @type string $url The CDN URL, with a trailing slash. An empty string if no URL is set.
|
||||
* }
|
||||
*/
|
||||
public function get_cdn_source( $option_url = '' ) {
|
||||
if ( defined( 'IMAGIFY_CDN_URL' ) && IMAGIFY_CDN_URL && is_string( IMAGIFY_CDN_URL ) ) {
|
||||
// Use a constant.
|
||||
$source = [
|
||||
'source' => 'constant',
|
||||
'name' => 'IMAGIFY_CDN_URL',
|
||||
'url' => IMAGIFY_CDN_URL,
|
||||
];
|
||||
} else {
|
||||
// Maybe use a filter.
|
||||
$filter_source = [
|
||||
'name' => null,
|
||||
'url' => null,
|
||||
];
|
||||
|
||||
/**
|
||||
* Provide a custom CDN source.
|
||||
*
|
||||
* @since 1.9.3
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $filter_source {
|
||||
* @type $name string The name of which provides the URL (plugin name, etc).
|
||||
* @type $url string The CDN URL.
|
||||
* }
|
||||
*/
|
||||
$filter_source = apply_filters( 'imagify_cdn_source', $filter_source );
|
||||
|
||||
if ( ! empty( $filter_source['url'] ) ) {
|
||||
$source = [
|
||||
'source' => 'filter',
|
||||
'name' => ! empty( $filter_source['name'] ) ? $filter_source['name'] : '',
|
||||
'url' => $filter_source['url'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
if ( empty( $source['url'] ) ) {
|
||||
// No constant, no filter: use the option.
|
||||
$source = [
|
||||
'source' => 'option',
|
||||
'name' => '',
|
||||
'url' => $option_url && is_string( $option_url ) ? $option_url : get_imagify_option( 'cdn_url' ),
|
||||
];
|
||||
}
|
||||
|
||||
if ( empty( $source['url'] ) ) {
|
||||
// Nothing set.
|
||||
return [
|
||||
'source' => 'option',
|
||||
'name' => '',
|
||||
'url' => '',
|
||||
];
|
||||
}
|
||||
|
||||
$source['url'] = $this->sanitize_cdn_url( $source['url'] );
|
||||
|
||||
if ( empty( $source['url'] ) ) {
|
||||
// Not an URL.
|
||||
return [
|
||||
'source' => 'option',
|
||||
'name' => '',
|
||||
'url' => '',
|
||||
];
|
||||
}
|
||||
|
||||
return $source;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize the CDN URL value.
|
||||
*
|
||||
* @since 1.9.3
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $url The URL to sanitize.
|
||||
* @return string
|
||||
*/
|
||||
public function sanitize_cdn_url( $url ) {
|
||||
$url = sanitize_text_field( $url );
|
||||
|
||||
if ( ! $url || ! preg_match( '@^https?://.+\.[^.]+@i', $url ) ) {
|
||||
// Not an URL.
|
||||
return '';
|
||||
}
|
||||
|
||||
return trailingslashit( $url );
|
||||
}
|
||||
}
|
||||
62
wp/plugins/imagify/classes/Webp/RewriteRules/Apache.php
Normal file
62
wp/plugins/imagify/classes/Webp/RewriteRules/Apache.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
namespace Imagify\Webp\RewriteRules;
|
||||
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Add and remove rewrite rules to the .htaccess file to display WebP images on the site.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
class Apache extends \Imagify\WriteFile\AbstractApacheDirConfFile {
|
||||
|
||||
/**
|
||||
* Name of the tag used as block delemiter.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
const TAG_NAME = 'Imagify: rewrite rules for webp';
|
||||
|
||||
/**
|
||||
* Get unfiltered new contents to write into the file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @source https://github.com/vincentorback/WebP-images-with-htaccess
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_raw_new_contents() {
|
||||
$extensions = $this->get_extensions_pattern();
|
||||
$home_root = wp_parse_url( home_url( '/' ) );
|
||||
$home_root = $home_root['path'];
|
||||
|
||||
return trim( '
|
||||
<IfModule mod_setenvif.c>
|
||||
# Vary: Accept for all the requests to jpeg, png, and gif.
|
||||
SetEnvIf Request_URI "\.(' . $extensions . ')$" REQUEST_image
|
||||
</IfModule>
|
||||
|
||||
<IfModule mod_rewrite.c>
|
||||
RewriteEngine On
|
||||
RewriteBase ' . $home_root . '
|
||||
|
||||
# Check if browser supports WebP images.
|
||||
RewriteCond %{HTTP_ACCEPT} image/webp
|
||||
|
||||
# Check if WebP replacement image exists.
|
||||
RewriteCond %{REQUEST_FILENAME}.webp -f
|
||||
|
||||
# Serve WebP image instead.
|
||||
RewriteRule (.+)\.(' . $extensions . ')$ $1.$2.webp [T=image/webp,NC]
|
||||
</IfModule>
|
||||
|
||||
<IfModule mod_headers.c>
|
||||
Header append Vary Accept env=REQUEST_image
|
||||
</IfModule>' );
|
||||
}
|
||||
}
|
||||
261
wp/plugins/imagify/classes/Webp/RewriteRules/Display.php
Normal file
261
wp/plugins/imagify/classes/Webp/RewriteRules/Display.php
Normal file
@@ -0,0 +1,261 @@
|
||||
<?php
|
||||
namespace Imagify\Webp\RewriteRules;
|
||||
|
||||
use Imagify\Notices\Notices;
|
||||
use Imagify\Traits\InstanceGetterTrait;
|
||||
use Imagify\WriteFile\AbstractWriteDirConfFile;
|
||||
|
||||
/**
|
||||
* Display WebP images on the site with rewrite rules.
|
||||
*
|
||||
* @since 1.9
|
||||
*/
|
||||
class Display {
|
||||
use InstanceGetterTrait;
|
||||
|
||||
/**
|
||||
* Configuration file writer.
|
||||
*
|
||||
* @var AbstractWriteDirConfFile
|
||||
*/
|
||||
protected $server_conf;
|
||||
|
||||
/**
|
||||
* Option value.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.9
|
||||
*/
|
||||
const OPTION_VALUE = 'rewrite';
|
||||
|
||||
/**
|
||||
* Init.
|
||||
*
|
||||
* @since 1.9
|
||||
*/
|
||||
public function init() {
|
||||
add_filter( 'imagify_settings_on_save', [ $this, 'maybe_add_rewrite_rules' ] );
|
||||
add_action( 'imagify_settings_webp_info', [ $this, 'maybe_add_webp_info' ] );
|
||||
add_action( 'imagify_activation', [ $this, 'activate' ] );
|
||||
add_action( 'imagify_deactivation', [ $this, 'deactivate' ] );
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** HOOKS =================================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* If display WebP images via rewrite rules, add the rules to the .htaccess/etc file.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param array $values The option values.
|
||||
* @return array
|
||||
*/
|
||||
public function maybe_add_rewrite_rules( $values ) {
|
||||
global $is_apache, $is_iis7, $is_nginx;
|
||||
|
||||
// Display WebP?
|
||||
$was_enabled = (bool) get_imagify_option( 'display_webp' );
|
||||
// See \Imagify_Options->validate_values_on_update() for why we use 'convert_to_webp' here.
|
||||
$is_enabled = ! empty( $values['display_webp'] ) && ! empty( $values['convert_to_webp'] );
|
||||
|
||||
// Which method?
|
||||
$old_value = get_imagify_option( 'display_webp_method' );
|
||||
$new_value = ! empty( $values['display_webp_method'] ) ? $values['display_webp_method'] : '';
|
||||
|
||||
// Decide when to add or remove rules.
|
||||
$is_rewrite = self::OPTION_VALUE === $new_value;
|
||||
$was_rewrite = self::OPTION_VALUE === $old_value;
|
||||
$add_or_remove = false;
|
||||
|
||||
if ( $is_enabled && $is_rewrite && ( ! $was_enabled || ! $was_rewrite ) ) {
|
||||
// Display WebP & use rewrite method, but only if one of the values changed: add rules.
|
||||
$add_or_remove = 'add';
|
||||
} elseif ( $was_enabled && $was_rewrite && ( ! $is_enabled || ! $is_rewrite ) ) {
|
||||
// Was displaying WebP & was using rewrite method, but only if one of the values changed: remove rules.
|
||||
$add_or_remove = 'remove';
|
||||
} else {
|
||||
return $values;
|
||||
}
|
||||
|
||||
if ( $is_apache ) {
|
||||
$rules = new Apache();
|
||||
} elseif ( $is_iis7 ) {
|
||||
$rules = new IIS();
|
||||
} elseif ( $is_nginx ) {
|
||||
$rules = new Nginx();
|
||||
} else {
|
||||
return $values;
|
||||
}
|
||||
|
||||
if ( 'add' === $add_or_remove ) {
|
||||
// Add the rewrite rules.
|
||||
$result = $rules->add();
|
||||
} else {
|
||||
// Remove the rewrite rules.
|
||||
$result = $rules->remove();
|
||||
}
|
||||
|
||||
if ( ! is_wp_error( $result ) ) {
|
||||
return $values;
|
||||
}
|
||||
|
||||
// Display an error message.
|
||||
if ( is_multisite() && strpos( wp_get_referer(), network_admin_url( '/' ) ) === 0 ) {
|
||||
Notices::get_instance()->add_network_temporary_notice( $result->get_error_message() );
|
||||
} else {
|
||||
Notices::get_instance()->add_site_temporary_notice( $result->get_error_message() );
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the conf file is not writable, add a warning.
|
||||
*
|
||||
* @since 1.9
|
||||
*/
|
||||
public function maybe_add_webp_info() {
|
||||
global $is_nginx;
|
||||
|
||||
$conf = $this->get_server_conf();
|
||||
|
||||
if ( ! $conf ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$writable = $conf->is_file_writable();
|
||||
|
||||
if ( is_wp_error( $writable ) ) {
|
||||
$rules = $conf->get_new_contents();
|
||||
|
||||
if ( ! $rules ) {
|
||||
// Uh?
|
||||
return;
|
||||
}
|
||||
|
||||
printf(
|
||||
/* translators: %s is a file name. */
|
||||
esc_html__( 'If you choose to use rewrite rules, you will have to add the following lines manually to the %s file:', 'imagify' ),
|
||||
'<code>' . $this->get_file_path( true ) . '</code>'
|
||||
);
|
||||
|
||||
echo '<pre class="code">' . esc_html( $rules ) . '</pre>';
|
||||
} elseif ( $is_nginx ) {
|
||||
printf(
|
||||
/* translators: %s is a file name. */
|
||||
esc_html__( 'If you choose to use rewrite rules, the file %s will be created and must be included into the server’s configuration file (then restart the server).', 'imagify' ),
|
||||
'<code>' . $this->get_file_path( true ) . '</code>'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add rules on plugin activation.
|
||||
*
|
||||
* @since 1.9
|
||||
*/
|
||||
public function activate() {
|
||||
$conf = $this->get_server_conf();
|
||||
|
||||
if ( ! $conf ) {
|
||||
return;
|
||||
}
|
||||
if ( ! get_imagify_option( 'display_webp' ) ) {
|
||||
return;
|
||||
}
|
||||
if ( self::OPTION_VALUE !== get_imagify_option( 'display_webp_method' ) ) {
|
||||
return;
|
||||
}
|
||||
if ( is_wp_error( $conf->is_file_writable() ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$conf->add();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove rules on plugin deactivation.
|
||||
*
|
||||
* @since 1.9
|
||||
*/
|
||||
public function deactivate() {
|
||||
$conf = $this->get_server_conf();
|
||||
|
||||
if ( ! $conf ) {
|
||||
return;
|
||||
}
|
||||
if ( ! get_imagify_option( 'display_webp' ) ) {
|
||||
return;
|
||||
}
|
||||
if ( self::OPTION_VALUE !== get_imagify_option( 'display_webp_method' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$file_path = $conf->get_file_path();
|
||||
$filesystem = \Imagify_Filesystem::get_instance();
|
||||
|
||||
if ( ! $filesystem->exists( $file_path ) ) {
|
||||
return;
|
||||
}
|
||||
if ( ! $filesystem->is_writable( $file_path ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$conf->remove();
|
||||
}
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** TOOLS =================================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get the path to the directory conf file.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param bool $relative True to get a path relative to the site’s root.
|
||||
* @return string|bool The file path. False on failure.
|
||||
*/
|
||||
public function get_file_path( $relative = false ) {
|
||||
if ( ! $this->get_server_conf() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$file_path = $this->get_server_conf()->get_file_path();
|
||||
|
||||
if ( $relative ) {
|
||||
return \Imagify_Filesystem::get_instance()->make_path_relative( $file_path );
|
||||
}
|
||||
|
||||
return $file_path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the server conf instance.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @return \Imagify\WriteFile\WriteFileInterface
|
||||
*/
|
||||
protected function get_server_conf() {
|
||||
global $is_apache, $is_iis7, $is_nginx;
|
||||
|
||||
if ( isset( $this->server_conf ) ) {
|
||||
return $this->server_conf;
|
||||
}
|
||||
|
||||
if ( $is_apache ) {
|
||||
$this->server_conf = new Apache();
|
||||
} elseif ( $is_iis7 ) {
|
||||
$this->server_conf = new IIS();
|
||||
} elseif ( $is_nginx ) {
|
||||
$this->server_conf = new Nginx();
|
||||
} else {
|
||||
$this->server_conf = false;
|
||||
}
|
||||
|
||||
return $this->server_conf;
|
||||
}
|
||||
}
|
||||
63
wp/plugins/imagify/classes/Webp/RewriteRules/IIS.php
Normal file
63
wp/plugins/imagify/classes/Webp/RewriteRules/IIS.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
namespace Imagify\Webp\RewriteRules;
|
||||
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Add and remove rewrite rules to the web.config file to display WebP images on the site.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
class IIS extends \Imagify\WriteFile\AbstractIISDirConfFile {
|
||||
|
||||
/**
|
||||
* Name of the tag used as block delemiter.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
const TAG_NAME = 'Imagify: rewrite rules for webp';
|
||||
|
||||
/**
|
||||
* Get unfiltered new contents to write into the file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @source https://github.com/igrigorik/webp-detect/blob/master/iis.config
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_raw_new_contents() {
|
||||
$extensions = $this->get_extensions_pattern();
|
||||
$home_root = wp_parse_url( home_url( '/' ) );
|
||||
$home_root = $home_root['path'];
|
||||
|
||||
return trim( '
|
||||
<!-- @parent /configuration/system.webServer/rewrite/rules -->
|
||||
<rule name="' . esc_attr( static::TAG_NAME ) . ' 2">
|
||||
<match url="^(' . $home_root . '.+)\.(' . $extensions . ')$" ignoreCase="true" />
|
||||
<conditions logicalGrouping="MatchAll">
|
||||
<add input="{HTTP_ACCEPT}" pattern="image/webp" ignoreCase="false" />
|
||||
<add input="{DOCUMENT_ROOT}/{R:1}{R:2}.webp" matchType="IsFile" />
|
||||
</conditions>
|
||||
<action type="Rewrite" url="{R:1}{R:2}.webp" logRewrittenUrl="true" />
|
||||
<serverVariables>
|
||||
<set name="ACCEPTS_WEBP" value="true" />
|
||||
</serverVariables>
|
||||
</rule>
|
||||
|
||||
<!-- @parent /configuration/system.webServer/rewrite/outboundRules -->
|
||||
<rule preCondition="IsWebp" name="' . esc_attr( static::TAG_NAME ) . ' 3">
|
||||
<match serverVariable="RESPONSE_Vary" pattern=".*" />
|
||||
<action type="Rewrite" value="Accept"/>
|
||||
</rule>
|
||||
<preConditions name="' . esc_attr( static::TAG_NAME ) . ' 4">
|
||||
<preCondition name="IsWebp">
|
||||
<add input="{ACCEPTS_WEBP}" pattern="true" ignoreCase="false" />
|
||||
</preCondition>
|
||||
</preConditions>' );
|
||||
}
|
||||
}
|
||||
52
wp/plugins/imagify/classes/Webp/RewriteRules/Nginx.php
Normal file
52
wp/plugins/imagify/classes/Webp/RewriteRules/Nginx.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
namespace Imagify\Webp\RewriteRules;
|
||||
|
||||
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
|
||||
|
||||
/**
|
||||
* Add and remove rewrite rules to the imagify.conf file to display WebP images on the site.
|
||||
*
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
class Nginx extends \Imagify\WriteFile\AbstractNginxDirConfFile {
|
||||
|
||||
/**
|
||||
* Name of the tag used as block delemiter.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.9
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
const TAG_NAME = 'Imagify: rewrite rules for webp';
|
||||
|
||||
/**
|
||||
* Get unfiltered new contents to write into the file.
|
||||
*
|
||||
* @since 1.9
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_raw_new_contents() {
|
||||
$extensions = $this->get_extensions_pattern();
|
||||
$home_root = wp_parse_url( home_url( '/' ) );
|
||||
$home_root = $home_root['path'];
|
||||
|
||||
return trim( '
|
||||
location ~* ^(' . $home_root . '.+)\.(' . $extensions . ')$ {
|
||||
add_header Vary Accept;
|
||||
|
||||
if ($http_accept ~* "webp"){
|
||||
set $imwebp A;
|
||||
}
|
||||
if (-f $request_filename.webp) {
|
||||
set $imwebp "${imwebp}B";
|
||||
}
|
||||
if ($imwebp = AB) {
|
||||
rewrite ^(.*) $1.webp;
|
||||
}
|
||||
}' );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user