PluginProbe
QuickWebP – WebP & AVIF Image Optimizer, Compression & SEO for WordPress / 4.1.1
QuickWebP – WebP & AVIF Image Optimizer, Compression & SEO for WordPress v4.1.1
4.1.2 4.1.1 4.1.0 4.0.1 4.0.0 3.3.1 3.3.0 trunk 1.0.0 2.0.0 2.1.0 3.0.0 3.1.0 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8
quickwebp / licensing / src / Settings.php

Settings.php in QuickWebP – WebP & AVIF Image Optimizer, Compression & SEO for WordPress 4.1.1, at licensing/src/Settings.php

550 lines 17.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace SureCartQuickWebP\Licensing;
3
4 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
5
6 /**
7 * The settings class.
8 */
9 class Settings {
10 /**
11 * SureCartQuickWebP\Licensing\Client
12 *
13 * @var object
14 */
15 protected $client;
16
17 /**
18 * Holds the option key
19 *
20 * @var string
21 */
22 private $option_key;
23
24 /**
25 * Holds the option name
26 *
27 * @var string
28 */
29 private $name;
30
31 /**
32 * Holds the menu arguments
33 *
34 * @var array
35 */
36 private $menu_args;
37
38 /**
39 * Create the pages.
40 *
41 * @param SureCartQuickWebP\Licensing\Client $client The client.
42 */
43 public function __construct( Client $client ) {
44 $this->client = $client;
45 $this->name = strtolower( preg_replace( '/\s+/', '', $this->client->name ) );
46 $this->option_key = $this->name . '_license_options';
47 $this->notices = array();
48 }
49
50 /**
51 * Add the settings page.
52 *
53 * @param array $args Settings page args.
54 *
55 * @return void
56 */
57 public function add_page( $args ) {
58 // store menu args for proper menu creation.
59 $this->menu_args = wp_parse_args(
60 $args,
61 array(
62 'type' => 'menu', // Can be: menu, options, submenu.
63 'page_title' => 'Manage License',
64 'menu_title' => 'Manage License',
65 'capability' => 'manage_options',
66 'menu_slug' => $this->client->slug . '-manage-license',
67 'icon_url' => '',
68 'position' => null,
69 'activated_redirect' => null,
70 'parent_slug' => '',
71 )
72 );
73 add_action( 'admin_menu', array( $this, 'admin_menu' ), 99 );
74 }
75
76 /**
77 * Form action URL
78 */
79 private function form_action_url() {
80 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
81 return apply_filters( 'surecart_client_license_form_action', '' );
82 }
83
84 /**
85 * Set the option key.
86 *
87 * If someone wants to override the default generated key.
88 *
89 * @param string $key The option key.
90 */
91 public function set_option_key( $key ) {
92 $this->option_key = $key;
93 return $this;
94 }
95
96 /**
97 * Add the admin menu
98 *
99 * @return void
100 */
101 public function admin_menu() {
102 switch ( $this->menu_args['type'] ) {
103 case 'menu':
104 $this->create_menu_page();
105 break;
106 case 'submenu':
107 $this->create_submenu_page();
108 break;
109 case 'options':
110 $this->create_options_page();
111 break;
112 }
113 }
114
115 /**
116 * Add license menu page
117 */
118 private function create_menu_page() {
119 call_user_func(
120 'add_menu_page',
121 $this->menu_args['page_title'],
122 $this->menu_args['menu_title'],
123 $this->menu_args['capability'],
124 $this->menu_args['menu_slug'],
125 array( $this, 'settings_output' ),
126 $this->menu_args['icon_url'],
127 $this->menu_args['position']
128 );
129 }
130
131 /**
132 * Add submenu page
133 */
134 private function create_submenu_page() {
135 call_user_func(
136 'add_submenu_page',
137 $this->menu_args['parent_slug'],
138 $this->menu_args['page_title'],
139 $this->menu_args['menu_title'],
140 $this->menu_args['capability'],
141 $this->menu_args['menu_slug'],
142 array( $this, 'settings_output' ),
143 $this->menu_args['position']
144 );
145 }
146
147 /**
148 * Add submenu page
149 */
150 private function create_options_page() {
151 call_user_func(
152 'add_options_page',
153 $this->menu_args['page_title'],
154 $this->menu_args['menu_title'],
155 $this->menu_args['capability'],
156 $this->menu_args['menu_slug'],
157 array( $this, 'settings_output' ),
158 $this->menu_args['position']
159 );
160 }
161
162 /**
163 * Get all options
164 *
165 * @return array
166 */
167 public function get_options() {
168 return (array) get_option( $this->option_key, array() );
169 }
170
171 /**
172 * Clear out the options.
173 *
174 * @return bool
175 */
176 public function clear_options() {
177 return update_option( $this->option_key, array() );
178 }
179
180 /**
181 * Get a specific option
182 *
183 * @param string $name Option name.
184 *
185 * @return mixed
186 */
187 public function get_option( $name ) {
188 $options = $this->get_options();
189 return isset( $options[ $name ] ) ? $options[ $name ] : null;
190 }
191
192 /**
193 * Set the option.
194 *
195 * @param string $name The option name.
196 * @param mixed $value The option value.
197 *
198 * @return bool
199 */
200 public function set_option( $name, $value ) {
201 $options = (array) $this->get_options();
202 $options[ $name ] = $value;
203 return update_option( $this->option_key, $options );
204 }
205
206 /**
207 * The settings page menu output.
208 *
209 * @return void
210 */
211 public function settings_output() {
212 $this->license_form_submit();
213
214 $assets = include QUICKWEBP_PLUGIN_PATH . 'public/assets/build/licensing.asset.php';
215 wp_enqueue_style( 'quickwebp-licensing', QUICKWEBP_PLUGIN_URL . 'public/assets/build/licensing.css', array(), $assets['version'] );
216
217 $activation = $this->get_activation();
218 $action = ! empty( $activation->id ) ? 'deactivate' : 'activate';
219 ?>
220
221 <div class="wrap">
222 <h1></h1>
223 <?php settings_errors(); ?>
224
225 <div class="quickwebp-license">
226 <form class="quickwebp-license__form" method="post" action="<?php echo esc_attr( $this->form_action_url() ); ?>">
227 <input type="hidden" name="_action" value="<?php echo esc_attr( $action ); ?>">
228 <input type="hidden" name="_nonce" value="<?php echo esc_attr( wp_create_nonce( $this->client->name ) ); ?>">
229 <input type="hidden" name="activation_id" value="<?php echo esc_attr( $this->activation_id ); ?>">
230
231 <h2 class="quickwebp-license__form__title"><?php echo esc_html( $this->menu_args['page_title'] ); ?></h2>
232
233 <div class="quickwebp-license__form__state">
234 <div class="quickwebp-license__form__state__left">
235 <?php if ( 'activate' === $action ) : ?>
236 <?php echo esc_html( sprintf(
237 // translators: %s is a placeholder for the name of the client.
238 __( 'Enter your license key to activate %s.', 'quickwebp' ),
239 $this->client->name )
240 ); ?>
241 <?php else : ?>
242 <?php echo esc_html( sprintf(
243 // translators: %s is a placeholder for the name of the client.
244 __( 'Your license is successfully activated for this site.', 'quickwebp' ),
245 $this->client->name )
246 ); ?>
247 <?php endif; ?>
248 </div>
249 <div class="quickwebp-license__form__state__right">
250 <div class="quickwebp-license__form__state__right__text"><?php esc_html_e( 'State', 'quickwebp' ); ?>:</div>
251 <div class="quickwebp-license__form__state__right__icon <?php echo esc_attr( $action ); ?>"></div>
252 </div>
253 </div>
254
255 <div class="quickwebp-license__form__input">
256 <?php if ( 'activate' === $action ) : ?>
257 <input class="widefat" type="password" autocomplete="off" name="license_key" id="license_key" value="<?php echo esc_attr( $this->license_key ); ?>" autofocus placeholder="<?php esc_attr_e( 'Enter the license...', 'quickwebp' ); ?>">
258 <?php else : ?>
259 <input class="widefat license-key-masked" type="text" value="<?php echo esc_attr( substr( $this->license_key, 0, 4 ) . '******************' ); ?>" disabled>
260 <?php endif; ?>
261 </div>
262
263 <div class="quickwebp-license__form__actions">
264 <div class="quickwebp-license__form__actions__left">
265 <?php if ( 'activate' === $action ) : ?>
266 <button name="submit" type="submit" class="quickwebp-license__form__actions__left__activate">
267 <?php echo wp_kses( file_get_contents( QUICKWEBP_PLUGIN_PATH . 'public/assets/svg/key.svg' ), quickwebp_allowed_tags_for_svg_files() ); ?>
268 <?php esc_html_e( 'Activate the license', 'quickwebp' ); ?>
269 </button>
270 <?php else : ?>
271 <button name="submit" type="submit" class="quickwebp-license__form__actions__left__deactivate">
272 <?php echo wp_kses( file_get_contents( QUICKWEBP_PLUGIN_PATH . 'public/assets/svg/key-disable.svg' ), quickwebp_allowed_tags_for_svg_files() ); ?>
273 <?php esc_html_e( 'Deactivate the license', 'quickwebp' ); ?>
274 </button>
275 <?php endif; ?>
276 </div>
277 <div class="quickwebp-license__form__actions__right">
278 <a href="<?php echo esc_url( \Quickwebp_Settings::QUICKWEBP_GET_MY_ACCOUNT_URL ); ?>" target="_blank" class="quickwebp-license__form__actions__right__link">
279 <?php echo wp_kses( file_get_contents( QUICKWEBP_PLUGIN_PATH . 'public/assets/svg/admin-alt.svg' ), quickwebp_allowed_tags_for_svg_files() ); ?>
280 <?php esc_html_e( 'My account', 'quickwebp' ); ?>
281 </a>
282 </div>
283 </div>
284 </form>
285
286 <?php if ( 'activate' === $action ) : ?>
287 <div class="quickwebp-license__pro">
288 <div class="quickwebp-license__pro__header">
289 <div class="quickwebp-license__pro__header__text">
290 <div class="quickwebp-license__pro__header__text__subtitle">
291 <div class="quickwebp-license__pro__header__text__subtitle__tag"><?php esc_html_e( 'Pro', 'quickwebp' ); ?></div>
292 <div class="quickwebp-license__pro__header__text__subtitle__text"><?php esc_html_e( 'AVIF Format', 'quickwebp' ); ?></div>
293 </div>
294 <div class="quickwebp-license__pro__header__text__title">
295 <?php esc_html_e( 'Go further with', 'quickwebp' ); ?>
296 <span class="highlight"> AVIF</span>
297 </div>
298 <div class="quickwebp-license__pro__header__text__desc">
299 <?php esc_html_e( 'The next-generation image format, available with', 'quickwebp' ); ?>
300 <span class="highlight">QuickWebP Pro.</span>
301 </div>
302 </div>
303 </div>
304
305 <div class="quickwebp-license__pro__body">
306 <div class="quickwebp-license__pro__body__top">
307 <div class="quickwebp-license__pro__body__top__item">
308 <div class="quickwebp-license__pro__body__top__item__icon">
309 <?php echo wp_kses( file_get_contents( QUICKWEBP_PLUGIN_PATH . 'public/assets/svg/gauge.svg' ), quickwebp_allowed_tags_for_svg_files() ); ?>
310 </div>
311 <div class="quickwebp-license__pro__body__top__item__text">
312 <div class="quickwebp-license__pro__body__top__item__text__title">-50%</div>
313 <div class="quickwebp-license__pro__body__top__item__text__desc"><?php esc_html_e( 'Vs jpeg/png on average', 'quickwebp' ); ?></div>
314 </div>
315 </div>
316
317 <div class="quickwebp-license__pro__body__top__item second">
318 <div class="quickwebp-license__pro__body__top__item__icon">
319 <?php echo wp_kses( file_get_contents( QUICKWEBP_PLUGIN_PATH . 'public/assets/svg/chart-line.svg' ), quickwebp_allowed_tags_for_svg_files() ); ?>
320 </div>
321 <div class="quickwebp-license__pro__body__top__item__text">
322 <div class="quickwebp-license__pro__body__top__item__text__title">-30%</div>
323 <div class="quickwebp-license__pro__body__top__item__text__desc"><?php esc_html_e( 'Vs webp on average', 'quickwebp' ); ?></div>
324 </div>
325 </div>
326
327 <div class="quickwebp-license__pro__body__top__item third">
328 <div class="quickwebp-license__pro__body__top__item__icon">
329 <?php echo wp_kses( file_get_contents( QUICKWEBP_PLUGIN_PATH . 'public/assets/svg/global.svg' ), quickwebp_allowed_tags_for_svg_files() ); ?>
330 </div>
331 <div class="quickwebp-license__pro__body__top__item__text">
332 <div class="quickwebp-license__pro__body__top__item__text__title">100%</div>
333 <div class="quickwebp-license__pro__body__top__item__text__desc"><?php esc_html_e( 'of modern browsers supported', 'quickwebp' ); ?></div>
334 </div>
335 </div>
336 </div>
337
338 <div class="quickwebp-license__pro__body__middle">
339 <div class="quickwebp-license__pro__body__middle__item">
340 <div class="quickwebp-license__pro__body__middle__item__icon">
341 <?php echo wp_kses( file_get_contents( QUICKWEBP_PLUGIN_PATH . 'public/assets/svg/check-mark.svg' ), quickwebp_allowed_tags_for_svg_files() ); ?>
342 </div>
343 <div class="quickwebp-license__pro__body__middle__item__text">
344 <strong><?php echo wp_kses_post(
345 // translators: %s is a placeholder for the text "</strong>".
346 sprintf( __( 'The best compression available %s images typically half the size of your originals.', 'quickwebp' ), '</strong>' )
347 ); ?>
348 </div>
349 </div>
350
351 <div class="quickwebp-license__pro__body__middle__item">
352 <div class="quickwebp-license__pro__body__middle__item__icon">
353 <?php echo wp_kses( file_get_contents( QUICKWEBP_PLUGIN_PATH . 'public/assets/svg/check-mark.svg' ), quickwebp_allowed_tags_for_svg_files() ); ?>
354 </div>
355 <div class="quickwebp-license__pro__body__middle__item__text">
356 <strong><?php echo wp_kses_post(
357 // translators: %s is a placeholder for the text "</strong>".
358 sprintf( __( 'Sharper images at lower weight %s for faster pages and better Core Web Vitals.', 'quickwebp' ), '</strong>' )
359 ); ?>
360 </div>
361 </div>
362
363 <div class="quickwebp-license__pro__body__middle__item">
364 <div class="quickwebp-license__pro__body__middle__item__icon">
365 <?php echo wp_kses( file_get_contents( QUICKWEBP_PLUGIN_PATH . 'public/assets/svg/check-mark.svg' ), quickwebp_allowed_tags_for_svg_files() ); ?>
366 </div>
367 <div class="quickwebp-license__pro__body__middle__item__text">
368 <strong><?php echo wp_kses_post(
369 // translators: %s is a placeholder for the text "</strong>".
370 sprintf( __( 'Automatic fallback %s so older browsers keep receiving a compatible format.', 'quickwebp' ), '</strong>' )
371 ); ?>
372 </div>
373 </div>
374
375 </div>
376
377 <div class="quickwebp-license__pro__body__bottom">
378 <div class="quickwebp-license__pro__body__bottom__upgrade">
379 <a href="<?php echo esc_url( quickwebp_get_pro_url( 'license-screen-upgrade' ) ); ?>" class="quickwebp-license__pro__body__bottom__upgrade__link" target="_blank">
380 <span class="quickwebp-license__pro__body__bottom__upgrade__link__icon">
381 <?php echo wp_kses( file_get_contents( QUICKWEBP_PLUGIN_PATH . 'public/assets/svg/crown.svg' ), quickwebp_allowed_tags_for_svg_files() ); ?>
382 </span>
383 <?php esc_html_e( 'Upgrade to QuickWebP Pro', 'quickwebp' ); ?>
384 </a>
385 </div>
386 </div>
387 </div>
388
389 </div>
390 <?php endif; ?>
391 </div>
392 </div>
393 <?php
394 }
395
396 /**
397 * Get the activation.
398 *
399 * @return Object|false
400 */
401 public function get_activation() {
402 $activation = false;
403 if ( $this->activation_id ) {
404 $activation = $this->client->activation()->get( $this->activation_id );
405 if ( is_wp_error( $activation ) ) {
406 if ( 'not_found' === $activation->get_error_code() ) {
407 $this->add_error( 'deactivaed', __( 'Your license has been deactivated for this site.', 'quickwebp' ) );
408 $this->clear_options();
409 }
410 }
411 }
412 return $activation;
413 }
414
415 /**
416 * License form submit
417 */
418 public function license_form_submit() {
419 // only if we are submitting.
420 if ( ! isset( $_POST['submit'] ) ) {
421 return;
422 }
423
424 // Check nonce.
425 if ( ! isset( $_POST['_nonce'], $_POST['_action'] ) ) {
426 $this->add_error( 'missing_info', __( 'Please add all information', 'quickwebp' ) );
427 return;
428 }
429
430 // Cerify nonce.
431 if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_nonce'] ) ), $this->client->name ) ) {
432 $this->add_error( 'unauthorized', __( "You don't have permission to manage licenses.", 'quickwebp' ) );
433 return;
434 }
435
436 // handle activation.
437 if ( 'activate' === sanitize_text_field( wp_unslash( $_POST['_action'] ) ) ) {
438 $activated = $this->client->license()->activate( sanitize_text_field( wp_unslash( $_POST['license_key'] ?? '' ) ) );
439 if ( is_wp_error( $activated ) ) {
440 $this->add_error( $activated->get_error_code(), $activated->get_error_message() );
441 return;
442 }
443
444 if ( ! empty( $this->menu_args['activated_redirect'] ) ) {
445 $this->redirect( $this->menu_args['activated_redirect'] );
446 exit;
447 }
448
449 return $this->add_success( 'activated', __( 'This site was successfully activated.', 'quickwebp' ) );
450 }
451
452 // handle deactivation.
453 if ( 'deactivate' === sanitize_text_field( wp_unslash( $_POST['_action'] ) ) ) {
454 $deactivated = $this->client->license()->deactivate( sanitize_text_field( wp_unslash( $_POST['activation_id'] ?? '' ) ) );
455 if ( is_wp_error( $deactivated ) ) {
456 $this->add_error( $deactivated->get_error_code(), $deactivated->get_error_message() );
457 }
458
459 if ( ! empty( $this->menu_args['deactivated_redirect'] ) ) {
460 $this->redirect( $this->menu_args['deactivated_redirect'] );
461 exit;
462 }
463
464 return $this->add_success( 'deactivated', __( 'This site was successfully deactivated.', 'quickwebp' ) );
465 }
466 }
467
468 /**
469 * Redirect to a url client-side.
470 * We need to do this to avoid "headers already sent" messages.
471 *
472 * @param string $url Url to redirect.
473 *
474 * @return void
475 */
476 public function redirect( $url ) {
477 ?>
478 <div class="spinner is-active"></div>
479 <script>
480 window.location.assign("<?php echo esc_url( $url ); ?>");
481 </script>
482 <?php
483 }
484
485 /**
486 * Add a notice.
487 *
488 * @param string $code Notice code.
489 * @param string $message Notice message.
490 * @param string $type Notice type.
491 *
492 * @return void
493 */
494 public function add_notice( $code, $message, $type = 'info' ) {
495 add_settings_error(
496 $this->name . '_license_options', // matches what we registered in `register_setting.
497 $code, // the error code.
498 $message,
499 $type
500 );
501 }
502
503 /**
504 * Add an error.
505 *
506 * @param string $code Error code.
507 * @param string $message Error message.
508 *
509 * @return void
510 */
511 public function add_error( $code, $message ) {
512 $this->add_notice( $code, $message, 'error' );
513 }
514
515 /**
516 * Add an success message
517 *
518 * @param string $code Success code.
519 * @param string $message Success message.
520 *
521 * @return void
522 */
523 public function add_success( $code, $message ) {
524 $this->add_notice( $code, $message, 'success' );
525 }
526
527 /**
528 * Set an option.
529 *
530 * @param string $name Name of option.
531 *
532 * @return mixed
533 */
534 public function __get( $name ) {
535 return $this->get_option( 'sc_' . $name );
536 }
537
538 /**
539 * Set an option
540 *
541 * @param string $name Name of option.
542 * @param mixed $value Value.
543 *
544 * @return bool
545 */
546 public function __set( $name, $value ) {
547 return $this->set_option( 'sc_' . $name, $value );
548 }
549 }
550