| 1 |
<?php |
| 2 |
/** |
| 3 |
* The admin-specific functionality of the plugin. |
| 4 |
* |
| 5 |
* @link http://webdeclic.com |
| 6 |
* @since 1.0.0 |
| 7 |
* |
| 8 |
* @package Quickwebp |
| 9 |
* @subpackage Quickwebp/admin |
| 10 |
*/ |
| 11 |
class Quickwebp_Settings { |
| 12 |
|
| 13 |
const QUICKWEBP_GET_LICENSE_URL = 'https://solutions.leyoweb.com/products/quickwebp/'; |
| 14 |
const QUICKWEBP_GET_MY_ACCOUNT_URL = 'https://solutions.leyoweb.com/customer-dashboard/'; |
| 15 |
|
| 16 |
/** |
| 17 |
* The ID of this plugin. |
| 18 |
* |
| 19 |
* @since 1.0.0 |
| 20 |
* @access private |
| 21 |
* @var string $plugin_name The ID of this plugin. |
| 22 |
*/ |
| 23 |
private $plugin_name; |
| 24 |
|
| 25 |
/** |
| 26 |
* The version of this plugin. |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
* @access private |
| 30 |
* @var string $version The current version of this plugin. |
| 31 |
*/ |
| 32 |
private $version; |
| 33 |
|
| 34 |
/** |
| 35 |
* Initialize the class and set its properties. |
| 36 |
* |
| 37 |
* @since 1.0.0 |
| 38 |
* @param string $plugin_name The name of this plugin. |
| 39 |
* @param string $version The version of this plugin. |
| 40 |
*/ |
| 41 |
public function __construct( $plugin_name, $version ) { |
| 42 |
|
| 43 |
$this->plugin_name = $plugin_name; |
| 44 |
$this->version = $version; |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Enqueue scripts and styles |
| 50 |
* |
| 51 |
*/ |
| 52 |
public function enqueue_scripts_styles( $hook_suffix ) { |
| 53 |
if ( $hook_suffix == 'toplevel_page_quickwebp-settings' || $hook_suffix == 'media_page_quickwebp-settings' ) { |
| 54 |
|
| 55 |
$admin_main_settings_assets = include( QUICKWEBP_PLUGIN_PATH . 'public/assets/build/admin-main-settings.asset.php' ); |
| 56 |
wp_enqueue_style( 'quickwebpoi_admin_main_settings', QUICKWEBP_PLUGIN_URL . 'public/assets/build/admin-main-settings.css', array(), $admin_main_settings_assets['version'], 'all' ); |
| 57 |
wp_enqueue_script( 'quickwebpoi_admin_main_settings', QUICKWEBP_PLUGIN_URL . 'public/assets/build/admin-main-settings.js', $admin_main_settings_assets['dependencies'], $admin_main_settings_assets['version'], true ); |
| 58 |
wp_localize_script( 'quickwebpoi_admin_main_settings', 'QUICKWEBP_ADMIN_SETTINGS', array( |
| 59 |
'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
| 60 |
'nonce' => wp_create_nonce( 'image_optimize_nonce' ), |
| 61 |
'preview_image_data' => $this->get_preview_image_data(), |
| 62 |
'default_image_url' => QUICKWEBP_PLUGIN_URL . 'public/assets/img/preview.jpg', |
| 63 |
)); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* add_settings_menu |
| 69 |
* |
| 70 |
* @return void |
| 71 |
*/ |
| 72 |
public function add_settings_menu() { |
| 73 |
add_submenu_page( |
| 74 |
'upload.php', |
| 75 |
__('QuickWebP Settings', 'quickwebp'), |
| 76 |
__('QuickWebP', 'quickwebp'), |
| 77 |
'manage_options', |
| 78 |
'quickwebp-settings', |
| 79 |
array( $this, 'render_settings_page' ) |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* render_settings_page |
| 85 |
* |
| 86 |
* @return void |
| 87 |
*/ |
| 88 |
public function render_settings_page() { |
| 89 |
global $is_nginx, $quickwebp_surecart_client; |
| 90 |
|
| 91 |
$php_version_valid = version_compare( PHP_VERSION, '8.1', '>=' ); |
| 92 |
$license_valid = false; |
| 93 |
if ( $quickwebp_surecart_client ) { |
| 94 |
$license_valid = $quickwebp_surecart_client->license()->is_valid(); |
| 95 |
} |
| 96 |
|
| 97 |
//phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 98 |
if ( isset( $_POST['quickwebp_settings_conversion'] ) ) { |
| 99 |
update_option( 'quickwebp_settings_onboarding_completed', '1' ); |
| 100 |
} |
| 101 |
|
| 102 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound |
| 103 |
$wpmtk_is_active = in_array( 'wpmastertoolkit/wp-mastertoolkit.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ); |
| 104 |
$show_onboarding = '1' !== (string) get_option( 'quickwebp_settings_onboarding_completed', '0' ); |
| 105 |
$conversion = get_option( 'quickwebp_settings_conversion', quickwebp_settings_default( 'quickwebp_settings_conversion' ) ); |
| 106 |
$conversion_enabled = '1' === (string) $conversion; |
| 107 |
$save_original = get_option( 'quickwebp_settings_conversion_save_original', quickwebp_settings_default( 'quickwebp_settings_conversion_save_original' ) ); |
| 108 |
$save_original = is_array( $save_original ) ? $save_original : array(); |
| 109 |
$save_original_enabled = in_array( 'checked', $save_original, true ); |
| 110 |
$display_mode = get_option( 'quickwebp_settings_conversion_display_webp_mode', quickwebp_settings_default( 'quickwebp_settings_conversion_display_webp_mode' ) ); |
| 111 |
$conversion_quality = get_option( 'quickwebp_settings_conversion_quality', quickwebp_settings_default( 'quickwebp_settings_conversion_quality' ) ); |
| 112 |
|
| 113 |
$profile_label = __( 'Custom profile', 'quickwebp' ); |
| 114 |
if ( $conversion_enabled && ! $save_original_enabled ) { |
| 115 |
$profile_label = __( 'New site profile', 'quickwebp' ); |
| 116 |
} |
| 117 |
if ( $conversion_enabled && $save_original_enabled ) { |
| 118 |
$profile_label = __( 'Existing site profile', 'quickwebp' ); |
| 119 |
} |
| 120 |
|
| 121 |
$preview_image_data = $this->get_preview_image_data(); |
| 122 |
$preview_image_data_webp = $preview_image_data['1_' . $conversion_quality] ?? $preview_image_data['1_medium']; |
| 123 |
$preview_image_data_avif = $preview_image_data['2_' . $conversion_quality] ?? $preview_image_data['2_medium']; |
| 124 |
|
| 125 |
require_once QUICKWEBP_PLUGIN_PATH . 'admin/templates/page-settings.php'; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* render_component |
| 130 |
* |
| 131 |
* @param mixed $data |
| 132 |
* @return void |
| 133 |
*/ |
| 134 |
public function render_component( $data = array() ) { |
| 135 |
$data['type'] = $data['type'] ?? 'text'; |
| 136 |
$data['name'] = $data['name'] ?? ''; |
| 137 |
$file_name = $data['type'] == 'text' || $data['type'] == 'email' || $data['type'] == 'tel' || $data['type'] == 'number' ? 'text' : $data['type']; |
| 138 |
$path_to_component = QUICKWEBP_PLUGIN_PATH . 'admin/components/' . $file_name . '.php'; |
| 139 |
|
| 140 |
if( file_exists( $path_to_component ) ) { |
| 141 |
?> |
| 142 |
<tr> |
| 143 |
<th> |
| 144 |
<label for="<?php echo esc_attr( $data['name'] ?? '' ); ?>"><?php echo esc_html( $data['label'] ?? '' ); ?></label> |
| 145 |
</th> |
| 146 |
<td class="<?php echo esc_attr( $data['name'] ?? '' ); ?>-container"> |
| 147 |
<?php include $path_to_component; ?> |
| 148 |
<?php if( isset( $data['description'] ) ) { |
| 149 |
?> |
| 150 |
<p class="description"><?php echo esc_html( $data['description'] ); ?></p> |
| 151 |
<?php |
| 152 |
} |
| 153 |
?> |
| 154 |
</td> |
| 155 |
</tr> |
| 156 |
<?php |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Add the settings link |
| 162 |
*/ |
| 163 |
public function add_settings_link( $plugin_actions, $plugin_file ) { |
| 164 |
|
| 165 |
$new_actions = array(); |
| 166 |
|
| 167 |
if ( 'quickwebp/quickwebp.php' === $plugin_file ) { |
| 168 |
|
| 169 |
$new_actions['settings'] = sprintf( |
| 170 |
// translators: %s is a placeholder for the link to the settings page. |
| 171 |
__( '<a href="%s">Settings</a>', 'quickwebp' ), |
| 172 |
esc_url( admin_url( 'upload.php?page=quickwebp-settings' ) ) |
| 173 |
); |
| 174 |
} |
| 175 |
|
| 176 |
return array_merge( $new_actions, $plugin_actions ); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Display web mode changed |
| 181 |
*/ |
| 182 |
public function add_rewrite_rules( $values ) { |
| 183 |
global $is_apache, $is_iis7, $is_nginx; |
| 184 |
|
| 185 |
include_once QUICKWEBP_PLUGIN_PATH . 'admin/rewrite-rules/class-apache.php'; |
| 186 |
include_once QUICKWEBP_PLUGIN_PATH . 'admin/rewrite-rules/class-nginx.php'; |
| 187 |
include_once QUICKWEBP_PLUGIN_PATH . 'admin/rewrite-rules/class-iis.php'; |
| 188 |
|
| 189 |
$old_value = get_option( 'quickwebp_settings_conversion_display_webp_mode', quickwebp_settings_default('quickwebp_settings_conversion_display_webp_mode') ); |
| 190 |
$is_rewrite = 'rewrite' === $values; |
| 191 |
$was_rewrite = 'rewrite' === $old_value; |
| 192 |
$add_or_remove = false; |
| 193 |
|
| 194 |
if ( $is_rewrite && !$was_rewrite ) { |
| 195 |
$add_or_remove = 'add'; |
| 196 |
} elseif ( !$is_rewrite && $was_rewrite ) { |
| 197 |
$add_or_remove = 'remove'; |
| 198 |
} else { |
| 199 |
return $values; |
| 200 |
} |
| 201 |
|
| 202 |
if ( $is_apache ) { |
| 203 |
$rules = new Quickwebp_Apache(); |
| 204 |
} elseif ( $is_iis7 ) { |
| 205 |
$rules = new Quickwebp_IIS(); |
| 206 |
} elseif ( $is_nginx ) { |
| 207 |
$rules = new Quickwebp_Nginx(); |
| 208 |
} else { |
| 209 |
return $values; |
| 210 |
} |
| 211 |
|
| 212 |
if ( 'add' === $add_or_remove ) { |
| 213 |
$result = $rules->add(); |
| 214 |
} else { |
| 215 |
$result = $rules->remove(); |
| 216 |
} |
| 217 |
|
| 218 |
if ( is_wp_error( $result ) ) { |
| 219 |
add_action( 'admin_notices', function() use ( $result ) { |
| 220 |
?> |
| 221 |
<div class="notice notice-error"> |
| 222 |
<p><?php echo esc_html( $result->get_error_message() ); ?></p> |
| 223 |
</div> |
| 224 |
<?php |
| 225 |
}); |
| 226 |
} |
| 227 |
|
| 228 |
return $values; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Ensure display mode remains consistent with conversion mode. |
| 233 |
*/ |
| 234 |
public function sanitize_display_mode_for_consistency( $value, $option = '', $original_value = '' ) { |
| 235 |
|
| 236 |
$allowed_modes = array( 'disabled', 'picture', 'rewrite' ); |
| 237 |
$value = sanitize_text_field( (string) $value ); |
| 238 |
|
| 239 |
if ( ! in_array( $value, $allowed_modes, true ) ) { |
| 240 |
$value = 'disabled'; |
| 241 |
} |
| 242 |
|
| 243 |
$conversion_enabled = $this->is_conversion_enabled_from_request_or_option(); |
| 244 |
if ( ! $conversion_enabled ) { |
| 245 |
return 'disabled'; |
| 246 |
} |
| 247 |
|
| 248 |
$save_original = $this->is_save_original_enabled_from_request_or_option(); |
| 249 |
if ( ! $save_original ) { |
| 250 |
return 'disabled'; |
| 251 |
} |
| 252 |
|
| 253 |
if ( 'disabled' === $value ) { |
| 254 |
return 'picture'; |
| 255 |
} |
| 256 |
|
| 257 |
return $value; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Get whether original images should be preserved. |
| 262 |
*/ |
| 263 |
private function is_save_original_enabled_from_request_or_option() { |
| 264 |
|
| 265 |
//phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 266 |
if ( isset( $_POST['quickwebp_settings_conversion_save_original'] ) ) { |
| 267 |
//phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 268 |
$raw_value = wp_unslash( $_POST['quickwebp_settings_conversion_save_original'] ); |
| 269 |
|
| 270 |
if ( is_array( $raw_value ) ) { |
| 271 |
$raw_value = array_map( 'sanitize_text_field', $raw_value ); |
| 272 |
return in_array( 'checked', $raw_value, true ); |
| 273 |
} |
| 274 |
|
| 275 |
return false; |
| 276 |
} |
| 277 |
|
| 278 |
$saved_value = get_option( |
| 279 |
'quickwebp_settings_conversion_save_original', |
| 280 |
quickwebp_settings_default( 'quickwebp_settings_conversion_save_original' ) |
| 281 |
); |
| 282 |
|
| 283 |
$saved_value = is_array( $saved_value ) ? $saved_value : array(); |
| 284 |
return in_array( 'checked', $saved_value, true ); |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Get whether image conversion is enabled. |
| 289 |
*/ |
| 290 |
private function is_conversion_enabled_from_request_or_option() { |
| 291 |
|
| 292 |
//phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 293 |
if ( isset( $_POST['quickwebp_settings_conversion'] ) ) { |
| 294 |
//phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 295 |
$raw_value = wp_unslash( $_POST['quickwebp_settings_conversion'] ); |
| 296 |
$raw_value = sanitize_text_field( (string) $raw_value ); |
| 297 |
|
| 298 |
return '1' === $raw_value; |
| 299 |
} |
| 300 |
|
| 301 |
$saved_value = get_option( |
| 302 |
'quickwebp_settings_conversion', |
| 303 |
quickwebp_settings_default( 'quickwebp_settings_conversion' ) |
| 304 |
); |
| 305 |
|
| 306 |
return '1' === (string) $saved_value; |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Data for the preview image used in the settings page. |
| 311 |
*/ |
| 312 |
private function get_preview_image_data() { |
| 313 |
return array( |
| 314 |
'name' => 'preview.jpg', |
| 315 |
'size' => '423.17 KB', |
| 316 |
'dimensions' => '1264 x 848', |
| 317 |
'1_low' => array( |
| 318 |
'size' => '175.32 KB', |
| 319 |
'save' => '247.85 KB', |
| 320 |
'save_percent' => '59%', |
| 321 |
'percent' => '41%', |
| 322 |
), |
| 323 |
'1_medium' => array( |
| 324 |
'size' => '196.08 KB', |
| 325 |
'save' => '227.09 KB', |
| 326 |
'save_percent' => '54%', |
| 327 |
'percent' => '46%', |
| 328 |
), |
| 329 |
'1_high' => array( |
| 330 |
'size' => '229.29 KB', |
| 331 |
'save' => '193.88 KB', |
| 332 |
'save_percent' => '46%', |
| 333 |
'percent' => '54%', |
| 334 |
), |
| 335 |
'1_extra_high' => array( |
| 336 |
'size' => '387.46 KB', |
| 337 |
'save' => '35.71 KB', |
| 338 |
'save_percent' => '8%', |
| 339 |
'percent' => '92%', |
| 340 |
), |
| 341 |
'2_low' => array( |
| 342 |
'size' => '73.44 KB', |
| 343 |
'save' => '349.73 KB', |
| 344 |
'save_percent' => '83%', |
| 345 |
'percent' => '17%', |
| 346 |
), |
| 347 |
'2_medium' => array( |
| 348 |
'size' => '120.06 KB', |
| 349 |
'save' => '303.11 KB', |
| 350 |
'save_percent' => '72%', |
| 351 |
'percent' => '28%', |
| 352 |
), |
| 353 |
'2_high' => array( |
| 354 |
'size' => '183.21 KB', |
| 355 |
'save' => '239.96 KB', |
| 356 |
'save_percent' => '57%', |
| 357 |
'percent' => '43%', |
| 358 |
), |
| 359 |
'2_extra_high' => array( |
| 360 |
'size' => '290.54 KB', |
| 361 |
'save' => '132.63 KB', |
| 362 |
'save_percent' => '31%', |
| 363 |
'percent' => '69%', |
| 364 |
), |
| 365 |
); |
| 366 |
} |
| 367 |
} |
| 368 |
|