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