This commit is contained in:
Tony Volpe
2024-06-17 14:41:24 -04:00
parent f885e93ca8
commit a00f379f7f
11158 changed files with 0 additions and 1781316 deletions

View File

@@ -1,36 +0,0 @@
<?php
declare(strict_types=1);
namespace Imagify\Webp;
use Imagify\WriteFile\AbstractApacheDirConfFile;
/**
* Add and remove contents to the .htaccess file to display WebP images on the site.
*
* @since 1.9
*/
class Apache extends AbstractApacheDirConfFile {
/**
* Name of the tag used as block delemiter.
*
* @var string
* @since 1.9
*/
const TAG_NAME = 'Imagify: webp file type';
/**
* Get unfiltered new contents to write into the file.
*
* @since 1.9
*
* @return string
*/
protected function get_raw_new_contents() {
return trim( '
<IfModule mod_mime.c>
AddType image/webp .webp
</IfModule>' );
}
}

View File

@@ -1,213 +0,0 @@
<?php
declare(strict_types=1);
namespace Imagify\Webp;
use Imagify\EventManagement\SubscriberInterface;
use Imagify\Notices\Notices;
use Imagify\Traits\InstanceGetterTrait;
use Imagify\WriteFile\WriteFileInterface;
/**
* Display WebP images on the site using picture tag.
*
* @since 1.9
*/
class Display implements SubscriberInterface {
use InstanceGetterTrait;
/**
* Server conf object.
*
* @var WriteFileInterface|null
* @since 1.9
*/
protected $server_conf = null;
/**
* Returns an array of events this subscriber listens to
*
* @return array
*/
public static function get_subscribed_events() {
return [
'imagify_settings_on_save' => [ 'maybe_add_rewrite_rules', 13 ],
'imagify_settings_webp_info' => 'maybe_add_webp_info',
'imagify_activation' => 'activate',
'imagify_deactivation' => 'deactivate',
];
}
/**
* 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 ) {
if ( ! $this->get_server_conf() ) {
return $values;
}
$enabled = isset( $values['display_nextgen'] ) ? true : false;
$was_enabled = (bool) get_imagify_option( 'display_nextgen' );
$result = false;
if ( $enabled && ! $was_enabled ) {
// Add the WebP file type.
$result = $this->get_server_conf()->add();
} elseif ( ! $enabled && $was_enabled ) {
// 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() );
return $values;
}
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_nextgen' ) ) {
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;
}
$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();
}
/**
* Get the path to the directory conf file.
*
* @since 1.9
*
* @param bool $relative True to get a path relative to the sites 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.
* Note: nothing needed for nginx.
*
* @since 1.9
*
* @return 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();
}
return $this->server_conf;
}
}

View File

@@ -1,37 +0,0 @@
<?php
declare(strict_types=1);
namespace Imagify\Webp;
use Imagify\WriteFile\AbstractIISDirConfFile;
/**
* Add and remove contents to the web.config file to display WebP images on the site.
*
* @since 1.9
*/
class IIS extends AbstractIISDirConfFile {
/**
* Name of the tag used as block delemiter.
*
* @var string
* @since 1.9
*/
const TAG_NAME = 'Imagify: webp file type';
/**
* Get unfiltered new contents to write into the file.
*
* @since 1.9
*
* @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>' );
}
}

View File

@@ -1,61 +0,0 @@
<?php
declare(strict_types=1);
namespace Imagify\Webp\RewriteRules;
use Imagify\WriteFile\AbstractApacheDirConfFile;
/**
* Add and remove rewrite rules to the .htaccess file to display WebP images on the site.
*
* @since 1.9
*/
class Apache extends AbstractApacheDirConfFile {
/**
* Name of the tag used as block delemiter.
*
* @var string
* @since 1.9
*/
const TAG_NAME = 'Imagify: rewrite rules for webp';
/**
* Get unfiltered new contents to write into the file.
*
* @since 1.9
* @source https://github.com/vincentorback/WebP-images-with-htaccess
*
* @return string
*/
protected function get_raw_new_contents() {
$extensions = $this->get_extensions_pattern();
$extensions = str_replace( '|webp', '', $extensions );
$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>' );
}
}

View File

@@ -1,239 +0,0 @@
<?php
declare(strict_types=1);
namespace Imagify\Webp\RewriteRules;
use Imagify\EventManagement\SubscriberInterface;
use Imagify\Notices\Notices;
use Imagify\WriteFile\WriteFileInterface;
/**
* Display WebP images on the site with rewrite rules.
*
* @since 1.9
*/
class Display implements SubscriberInterface {
/**
* Configuration file writer.
*
* @var WriteFileInterface|null
*/
protected $server_conf = null;
/**
* Option value.
*
* @var string
* @since 1.9
*/
const OPTION_VALUE = 'rewrite';
/**
* Returns an array of events this subscriber listens to
*
* @return array
*/
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',
];
}
/**
* 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 ) {
$was_enabled = (bool) get_imagify_option( 'display_nextgen' );
$is_enabled = ! empty( $values['display_nextgen'] );
// Which 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;
if ( ! $this->get_server_conf() ) {
return $values;
}
$result = false;
if ( $is_enabled && $is_rewrite && ( ! $was_enabled || ! $was_rewrite ) ) {
// Add the rewrite rules.
$result = $this->get_server_conf()->add();
} elseif ( $was_enabled && $was_rewrite && ( ! $is_enabled || ! $is_rewrite ) ) {
// Remove the rewrite rules.
$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() );
return $values;
}
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 servers 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_nextgen' ) ) {
return;
}
if ( self::OPTION_VALUE !== get_imagify_option( 'display_nextgen_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_nextgen' ) ) {
return;
}
if ( self::OPTION_VALUE !== get_imagify_option( 'display_nextgen_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();
}
/**
* Get the path to the directory conf file.
*
* @since 1.9
*
* @param bool $relative True to get a path relative to the sites 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 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();
}
return $this->server_conf;
}
}

View File

@@ -1,62 +0,0 @@
<?php
declare(strict_types=1);
namespace Imagify\Webp\RewriteRules;
use Imagify\WriteFile\AbstractIISDirConfFile;
/**
* Add and remove rewrite rules to the web.config file to display WebP images on the site.
*
* @since 1.9
*/
class IIS extends AbstractIISDirConfFile {
/**
* Name of the tag used as block delemiter.
*
* @var string
* @since 1.9
*/
const TAG_NAME = 'Imagify: rewrite rules for webp';
/**
* Get unfiltered new contents to write into the file.
*
* @since 1.9
* @source https://github.com/igrigorik/webp-detect/blob/master/iis.config
*
* @return string
*/
protected function get_raw_new_contents() {
$extensions = $this->get_extensions_pattern();
$extensions = str_replace( '|webp', '', $extensions );
$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>' );
}
}

View File

@@ -1,70 +0,0 @@
<?php
declare(strict_types=1);
namespace Imagify\Webp\RewriteRules;
use Imagify\WriteFile\AbstractNginxDirConfFile;
/**
* Add and remove rewrite rules to the imagify.conf file to display WebP images on the site.
*
* @since 1.9
*/
class Nginx extends AbstractNginxDirConfFile {
/**
* Name of the tag used as block delemiter.
*
* @var string
* @since 1.9
*/
const TAG_NAME = 'Imagify: rewrite rules for webp';
/**
* Get unfiltered new contents to write into the file.
*
* @since 1.9
*
* @return string
*/
protected function get_raw_new_contents() {
$extensions = $this->get_extensions_pattern() . '|avif';
$home_root = wp_parse_url( home_url( '/' ) );
$home_root = $home_root['path'];
return trim( '
location ~* ^(' . $home_root . '.+)\.(' . $extensions . ')$ {
add_header Vary Accept;
set $canavif 1;
if ($http_accept !~* "avif"){
set $canavif 0;
}
if (!-f $request_filename.avif) {
set $canavif 0;
}
if ($canavif = 1){
rewrite ^(.*) $1.avif;
break;
}
set $canwebp 1;
if ($http_accept !~* "webp"){
set $canwebp 0;
}
if (!-f $request_filename.webp) {
set $canwebp 0;
}
if ($canwebp = 1){
rewrite ^(.*) $1.webp;
break;
}
}' );
}
}

View File

@@ -1,51 +0,0 @@
<?php
declare(strict_types=1);
namespace Imagify\Webp;
use Imagify\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
use Imagify\Webp\RewriteRules\Display as RewriteRules;
/**
* Service provider for WebP rewrite rules
*/
class ServiceProvider extends AbstractServiceProvider {
/**
* Services provided by this provider
*
* @var array
*/
protected $provides = [
'webp_display',
'webp_rewrite_rules',
];
/**
* Subscribers provided by this provider
*
* @var array
*/
public $subscribers = [
'webp_display',
'webp_rewrite_rules',
];
/**
* Registers the provided classes
*
* @return void
*/
public function register() {
$this->getContainer()->share( 'webp_display', Display::class );
$this->getContainer()->share( 'webp_rewrite_rules', RewriteRules::class );
}
/**
* Returns the subscribers array
*
* @return array
*/
public function get_subscribers() {
return $this->subscribers;
}
}