| 1 |
<?php |
| 2 |
declare(strict_types=1); |
| 3 |
|
| 4 |
namespace Imagify\Avif; |
| 5 |
|
| 6 |
use Imagify\EventManagement\SubscriberInterface; |
| 7 |
use Imagify\Notices\Notices; |
| 8 |
use Imagify\WriteFile\WriteFileInterface; |
| 9 |
|
| 10 |
/** |
| 11 |
* Display AVIF images on the site using picture tag. |
| 12 |
*/ |
| 13 |
class Display implements SubscriberInterface { |
| 14 |
/** |
| 15 |
* Server conf object. |
| 16 |
* |
| 17 |
* @var WriteFileInterface|null |
| 18 |
* @since 1.9 |
| 19 |
*/ |
| 20 |
protected $server_conf = null; |
| 21 |
|
| 22 |
/** |
| 23 |
* Returns an array of events this subscriber listens to |
| 24 |
* |
| 25 |
* @return array |
| 26 |
*/ |
| 27 |
public static function get_subscribed_events() { |
| 28 |
return [ |
| 29 |
'imagify_settings_on_save' => [ 'maybe_add_rewrite_rules', 12 ], |
| 30 |
'imagify_activation' => 'activate', |
| 31 |
'imagify_deactivation' => 'deactivate', |
| 32 |
]; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* If display Next-Gen images, add the AVIF type to the .htaccess/etc file. |
| 37 |
* |
| 38 |
* @since 1.9 |
| 39 |
* |
| 40 |
* @param array $values The option values. |
| 41 |
* |
| 42 |
* @return array |
| 43 |
*/ |
| 44 |
public function maybe_add_rewrite_rules( $values ) { |
| 45 |
if ( ! $this->get_server_conf() ) { |
| 46 |
return $values; |
| 47 |
} |
| 48 |
|
| 49 |
$enabled = isset( $values['display_nextgen'] ) ? true : false; |
| 50 |
$was_enabled = (bool) get_imagify_option( 'display_nextgen' ); |
| 51 |
|
| 52 |
$result = false; |
| 53 |
|
| 54 |
if ( $enabled && ! $was_enabled ) { |
| 55 |
// Add the WebP file type. |
| 56 |
$result = $this->get_server_conf()->add(); |
| 57 |
} elseif ( ! $enabled && $was_enabled ) { |
| 58 |
// Remove the WebP file type. |
| 59 |
$result = $this->get_server_conf()->remove(); |
| 60 |
} |
| 61 |
|
| 62 |
if ( ! is_wp_error( $result ) ) { |
| 63 |
return $values; |
| 64 |
} |
| 65 |
|
| 66 |
// Display an error message. |
| 67 |
if ( is_multisite() && strpos( wp_get_referer(), network_admin_url( '/' ) ) === 0 ) { |
| 68 |
Notices::get_instance()->add_network_temporary_notice( $result->get_error_message() ); |
| 69 |
|
| 70 |
return $values; |
| 71 |
} |
| 72 |
|
| 73 |
Notices::get_instance()->add_site_temporary_notice( $result->get_error_message() ); |
| 74 |
|
| 75 |
return $values; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Add rules on plugin activation. |
| 80 |
* |
| 81 |
* @since 1.9 |
| 82 |
*/ |
| 83 |
public function activate() { |
| 84 |
$conf = $this->get_server_conf(); |
| 85 |
|
| 86 |
if ( ! $conf ) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
if ( ! get_imagify_option( 'display_nextgen' ) ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
if ( is_wp_error( $conf->is_file_writable() ) ) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
$conf->add(); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Remove rules on plugin deactivation. |
| 103 |
* |
| 104 |
* @since 1.9 |
| 105 |
*/ |
| 106 |
public function deactivate() { |
| 107 |
$conf = $this->get_server_conf(); |
| 108 |
|
| 109 |
if ( ! $conf ) { |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
$file_path = $conf->get_file_path(); |
| 114 |
$filesystem = \Imagify_Filesystem::get_instance(); |
| 115 |
|
| 116 |
if ( ! $filesystem->exists( $file_path ) ) { |
| 117 |
return; |
| 118 |
} |
| 119 |
if ( ! $filesystem->is_writable( $file_path ) ) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
$conf->remove(); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Get the path to the directory conf file. |
| 128 |
* |
| 129 |
* @since 1.9 |
| 130 |
* |
| 131 |
* @param bool $relative True to get a path relative to the site’s root. |
| 132 |
* @return string|bool The file path. False on failure. |
| 133 |
*/ |
| 134 |
public function get_file_path( $relative = false ) { |
| 135 |
if ( ! $this->get_server_conf() ) { |
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
$file_path = $this->get_server_conf()->get_file_path(); |
| 140 |
|
| 141 |
if ( $relative ) { |
| 142 |
return \Imagify_Filesystem::get_instance()->make_path_relative( $file_path ); |
| 143 |
} |
| 144 |
|
| 145 |
return $file_path; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Get the server conf instance. |
| 150 |
* Note: nothing needed for nginx. |
| 151 |
* |
| 152 |
* @since 1.9 |
| 153 |
* |
| 154 |
* @return WriteFileInterface |
| 155 |
*/ |
| 156 |
protected function get_server_conf() { |
| 157 |
global $is_apache, $is_iis7; |
| 158 |
|
| 159 |
if ( isset( $this->server_conf ) ) { |
| 160 |
return $this->server_conf; |
| 161 |
} |
| 162 |
|
| 163 |
if ( $is_apache ) { |
| 164 |
$this->server_conf = new Apache(); |
| 165 |
} elseif ( $is_iis7 ) { |
| 166 |
$this->server_conf = new IIS(); |
| 167 |
} |
| 168 |
|
| 169 |
return $this->server_conf; |
| 170 |
} |
| 171 |
} |
| 172 |
|