| 1 |
<?php |
| 2 |
namespace Imagify\Webp; |
| 3 |
|
| 4 |
use Imagify\Notices\Notices; |
| 5 |
use Imagify\Traits\InstanceGetterTrait; |
| 6 |
|
| 7 |
/** |
| 8 |
* Display WebP images on the site. |
| 9 |
* |
| 10 |
* @since 1.9 |
| 11 |
*/ |
| 12 |
class Display { |
| 13 |
use InstanceGetterTrait; |
| 14 |
|
| 15 |
/** |
| 16 |
* Server conf object. |
| 17 |
* |
| 18 |
* @var \Imagify\WriteFile\WriteFileInterface |
| 19 |
* @since 1.9 |
| 20 |
*/ |
| 21 |
protected $server_conf; |
| 22 |
|
| 23 |
/** |
| 24 |
* Init. |
| 25 |
* |
| 26 |
* @since 1.9 |
| 27 |
*/ |
| 28 |
public function init() { |
| 29 |
add_filter( 'imagify_settings_on_save', [ $this, 'maybe_add_rewrite_rules' ] ); |
| 30 |
add_action( 'imagify_settings_webp_info', [ $this, 'maybe_add_webp_info' ] ); |
| 31 |
add_action( 'imagify_activation', [ $this, 'activate' ] ); |
| 32 |
add_action( 'imagify_deactivation', [ $this, 'deactivate' ] ); |
| 33 |
|
| 34 |
Picture\Display::get_instance()->init(); |
| 35 |
RewriteRules\Display::get_instance()->init(); |
| 36 |
} |
| 37 |
|
| 38 |
/** ----------------------------------------------------------------------------------------- */ |
| 39 |
/** HOOKS =================================================================================== */ |
| 40 |
/** ----------------------------------------------------------------------------------------- */ |
| 41 |
|
| 42 |
/** |
| 43 |
* If display WebP images, add the WebP type to the .htaccess/etc file. |
| 44 |
* |
| 45 |
* @since 1.9 |
| 46 |
* |
| 47 |
* @param array $values The option values. |
| 48 |
* @return array |
| 49 |
*/ |
| 50 |
public function maybe_add_rewrite_rules( $values ) { |
| 51 |
$old_value = (bool) get_imagify_option( 'display_webp' ); |
| 52 |
// See \Imagify_Options->validate_values_on_update() for why we use 'convert_to_webp' here. |
| 53 |
$new_value = ! empty( $values['display_webp'] ) && ! empty( $values['convert_to_webp'] ); |
| 54 |
|
| 55 |
if ( $old_value === $new_value ) { |
| 56 |
// No changes. |
| 57 |
return $values; |
| 58 |
} |
| 59 |
|
| 60 |
if ( ! $this->get_server_conf() ) { |
| 61 |
return $values; |
| 62 |
} |
| 63 |
|
| 64 |
if ( $new_value ) { |
| 65 |
// Add the WebP file type. |
| 66 |
$result = $this->get_server_conf()->add(); |
| 67 |
} else { |
| 68 |
// Remove the WebP file type. |
| 69 |
$result = $this->get_server_conf()->remove(); |
| 70 |
} |
| 71 |
|
| 72 |
if ( ! is_wp_error( $result ) ) { |
| 73 |
return $values; |
| 74 |
} |
| 75 |
|
| 76 |
// Display an error message. |
| 77 |
if ( is_multisite() && strpos( wp_get_referer(), network_admin_url( '/' ) ) === 0 ) { |
| 78 |
Notices::get_instance()->add_network_temporary_notice( $result->get_error_message() ); |
| 79 |
} else { |
| 80 |
Notices::get_instance()->add_site_temporary_notice( $result->get_error_message() ); |
| 81 |
} |
| 82 |
|
| 83 |
return $values; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* If the conf file is not writable, add a warning. |
| 88 |
* |
| 89 |
* @since 1.9 |
| 90 |
*/ |
| 91 |
public function maybe_add_webp_info() { |
| 92 |
$conf = $this->get_server_conf(); |
| 93 |
|
| 94 |
if ( ! $conf ) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
$writable = $conf->is_file_writable(); |
| 99 |
|
| 100 |
if ( ! is_wp_error( $writable ) ) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
$rules = $conf->get_new_contents(); |
| 105 |
|
| 106 |
if ( ! $rules ) { |
| 107 |
// Uh? |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
echo '<br/>'; |
| 112 |
|
| 113 |
printf( |
| 114 |
/* translators: %s is a file name. */ |
| 115 |
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' ), |
| 116 |
'<code>' . $this->get_file_path( true ) . '</code>' |
| 117 |
); |
| 118 |
|
| 119 |
echo '<pre class="code">' . esc_html( $rules ) . '</pre>'; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Add rules on plugin activation. |
| 124 |
* |
| 125 |
* @since 1.9 |
| 126 |
*/ |
| 127 |
public function activate() { |
| 128 |
$conf = $this->get_server_conf(); |
| 129 |
|
| 130 |
if ( ! $conf ) { |
| 131 |
return; |
| 132 |
} |
| 133 |
if ( ! get_imagify_option( 'display_webp' ) ) { |
| 134 |
return; |
| 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 |
if ( ! get_imagify_option( 'display_webp' ) ) { |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
$file_path = $conf->get_file_path(); |
| 159 |
$filesystem = \Imagify_Filesystem::get_instance(); |
| 160 |
|
| 161 |
if ( ! $filesystem->exists( $file_path ) ) { |
| 162 |
return; |
| 163 |
} |
| 164 |
if ( ! $filesystem->is_writable( $file_path ) ) { |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
$conf->remove(); |
| 169 |
} |
| 170 |
|
| 171 |
/** ----------------------------------------------------------------------------------------- */ |
| 172 |
/** TOOLS =================================================================================== */ |
| 173 |
/** ----------------------------------------------------------------------------------------- */ |
| 174 |
|
| 175 |
/** |
| 176 |
* Get the path to the directory conf file. |
| 177 |
* |
| 178 |
* @since 1.9 |
| 179 |
* |
| 180 |
* @param bool $relative True to get a path relative to the site’s root. |
| 181 |
* @return string|bool The file path. False on failure. |
| 182 |
*/ |
| 183 |
public function get_file_path( $relative = false ) { |
| 184 |
if ( ! $this->get_server_conf() ) { |
| 185 |
return false; |
| 186 |
} |
| 187 |
|
| 188 |
$file_path = $this->get_server_conf()->get_file_path(); |
| 189 |
|
| 190 |
if ( $relative ) { |
| 191 |
return \Imagify_Filesystem::get_instance()->make_path_relative( $file_path ); |
| 192 |
} |
| 193 |
|
| 194 |
return $file_path; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Get the WebP display method by validating the given value. |
| 199 |
* |
| 200 |
* @since 1.9 |
| 201 |
* |
| 202 |
* @param array $values The option values. |
| 203 |
* @return string 'picture' or 'rewrite'. |
| 204 |
*/ |
| 205 |
public function get_display_webp_method( $values ) { |
| 206 |
$options = \Imagify_Options::get_instance(); |
| 207 |
$default = $options->get_default_values(); |
| 208 |
$default = $default['display_webp_method']; |
| 209 |
$method = ! empty( $values['display_webp_method'] ) ? $values['display_webp_method'] : ''; |
| 210 |
|
| 211 |
return $options->sanitize_and_validate( 'display_webp_method', $method, $default ); |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Get the server conf instance. |
| 216 |
* Note: nothing needed for nginx. |
| 217 |
* |
| 218 |
* @since 1.9 |
| 219 |
* |
| 220 |
* @return \Imagify\WriteFile\WriteFileInterface |
| 221 |
*/ |
| 222 |
protected function get_server_conf() { |
| 223 |
global $is_apache, $is_iis7; |
| 224 |
|
| 225 |
if ( isset( $this->server_conf ) ) { |
| 226 |
return $this->server_conf; |
| 227 |
} |
| 228 |
|
| 229 |
if ( $is_apache ) { |
| 230 |
$this->server_conf = new Apache(); |
| 231 |
} elseif ( $is_iis7 ) { |
| 232 |
$this->server_conf = new IIS(); |
| 233 |
} else { |
| 234 |
$this->server_conf = false; |
| 235 |
} |
| 236 |
|
| 237 |
return $this->server_conf; |
| 238 |
} |
| 239 |
} |
| 240 |
|