Plugin Updates
This commit is contained in:
@@ -1,100 +1,80 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Imagify\Webp\RewriteRules;
|
||||
|
||||
use Imagify\EventManagement\SubscriberInterface;
|
||||
use Imagify\Notices\Notices;
|
||||
use Imagify\Traits\InstanceGetterTrait;
|
||||
use Imagify\WriteFile\AbstractWriteDirConfFile;
|
||||
use Imagify\WriteFile\WriteFileInterface;
|
||||
|
||||
/**
|
||||
* Display WebP images on the site with rewrite rules.
|
||||
*
|
||||
* @since 1.9
|
||||
*/
|
||||
class Display {
|
||||
use InstanceGetterTrait;
|
||||
|
||||
class Display implements SubscriberInterface {
|
||||
/**
|
||||
* Configuration file writer.
|
||||
*
|
||||
* @var AbstractWriteDirConfFile
|
||||
* @var WriteFileInterface|null
|
||||
*/
|
||||
protected $server_conf;
|
||||
protected $server_conf = null;
|
||||
|
||||
/**
|
||||
* Option value.
|
||||
*
|
||||
* @var string
|
||||
* @var string
|
||||
* @since 1.9
|
||||
*/
|
||||
const OPTION_VALUE = 'rewrite';
|
||||
|
||||
/**
|
||||
* Init.
|
||||
* Returns an array of events this subscriber listens to
|
||||
*
|
||||
* @since 1.9
|
||||
* @return array
|
||||
*/
|
||||
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' ] );
|
||||
public static function get_subscribed_events() {
|
||||
return [
|
||||
'imagify_settings_on_save' => [ 'maybe_add_rewrite_rules', 10 ],
|
||||
'imagify_settings_webp_info' => 'maybe_add_webp_info',
|
||||
'imagify_activation' => 'activate',
|
||||
'imagify_deactivation' => '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.
|
||||
* @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'] );
|
||||
$was_enabled = (bool) get_imagify_option( 'display_nextgen' );
|
||||
$is_enabled = ! empty( $values['display_nextgen'] );
|
||||
|
||||
// Which method?
|
||||
$old_value = get_imagify_option( 'display_webp_method' );
|
||||
$new_value = ! empty( $values['display_webp_method'] ) ? $values['display_webp_method'] : '';
|
||||
$old_value = get_imagify_option( 'display_nextgen_method' );
|
||||
$new_value = ! empty( $values['display_nextgen_method'] ) ? $values['display_nextgen_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;
|
||||
$is_rewrite = self::OPTION_VALUE === $new_value;
|
||||
$was_rewrite = self::OPTION_VALUE === $old_value;
|
||||
|
||||
if ( ! $this->get_server_conf() ) {
|
||||
return $values;
|
||||
}
|
||||
|
||||
$result = 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 {
|
||||
$result = $this->get_server_conf()->add();
|
||||
} elseif ( $was_enabled && $was_rewrite && ( ! $is_enabled || ! $is_rewrite ) ) {
|
||||
// Remove the rewrite rules.
|
||||
$result = $rules->remove();
|
||||
$result = $this->get_server_conf()->remove();
|
||||
}
|
||||
|
||||
if ( ! is_wp_error( $result ) ) {
|
||||
@@ -104,10 +84,12 @@ class Display {
|
||||
// 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;
|
||||
}
|
||||
|
||||
Notices::get_instance()->add_site_temporary_notice( $result->get_error_message() );
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
@@ -162,10 +144,11 @@ class Display {
|
||||
if ( ! $conf ) {
|
||||
return;
|
||||
}
|
||||
if ( ! get_imagify_option( 'display_webp' ) ) {
|
||||
|
||||
if ( ! get_imagify_option( 'display_nextgen' ) ) {
|
||||
return;
|
||||
}
|
||||
if ( self::OPTION_VALUE !== get_imagify_option( 'display_webp_method' ) ) {
|
||||
if ( self::OPTION_VALUE !== get_imagify_option( 'display_nextgen_method' ) ) {
|
||||
return;
|
||||
}
|
||||
if ( is_wp_error( $conf->is_file_writable() ) ) {
|
||||
@@ -186,10 +169,10 @@ class Display {
|
||||
if ( ! $conf ) {
|
||||
return;
|
||||
}
|
||||
if ( ! get_imagify_option( 'display_webp' ) ) {
|
||||
if ( ! get_imagify_option( 'display_nextgen' ) ) {
|
||||
return;
|
||||
}
|
||||
if ( self::OPTION_VALUE !== get_imagify_option( 'display_webp_method' ) ) {
|
||||
if ( self::OPTION_VALUE !== get_imagify_option( 'display_nextgen_method' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -206,17 +189,14 @@ class Display {
|
||||
$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.
|
||||
*
|
||||
* @return string|bool The file path. False on failure.
|
||||
*/
|
||||
public function get_file_path( $relative = false ) {
|
||||
if ( ! $this->get_server_conf() ) {
|
||||
@@ -237,7 +217,7 @@ class Display {
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @return \Imagify\WriteFile\WriteFileInterface
|
||||
* @return WriteFileInterface
|
||||
*/
|
||||
protected function get_server_conf() {
|
||||
global $is_apache, $is_iis7, $is_nginx;
|
||||
@@ -252,8 +232,6 @@ class Display {
|
||||
$this->server_conf = new IIS();
|
||||
} elseif ( $is_nginx ) {
|
||||
$this->server_conf = new Nginx();
|
||||
} else {
|
||||
$this->server_conf = false;
|
||||
}
|
||||
|
||||
return $this->server_conf;
|
||||
|
||||
Reference in New Issue
Block a user