PluginProbe
Plus WebP or AVIF / 2.06
Plus WebP or AVIF v2.06
5.21 5.20 5.12 5.11 trunk 1.00 1.01 1.02 1.03 1.04 1.05 1.06 1.07 1.08 1.09 1.10 1.11 1.12 1.13 2.00 2.01 2.02 2.03 2.04 2.05 All 47 releases
plus-webp / lib / class-pluswebpadmin.php

class-pluswebpadmin.php in Plus WebP or AVIF 2.06, at lib/class-pluswebpadmin.php

562 lines 19.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plus WebP
4 *
5 * @package Plus WebP
6 * @subpackage PlusWebpAdmin Management screen
7 Copyright (c) 2019- Katsushi Kawamori (email : dodesyoswift312@gmail.com)
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; version 2 of the License.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 $pluswebpadmin = new PlusWebpAdmin();
23
24 /** ==================================================
25 * Management screen
26 */
27 class PlusWebpAdmin {
28
29 /** ==================================================
30 * Construct
31 *
32 * @since 1.00
33 */
34 public function __construct() {
35
36 add_action( 'admin_init', array( $this, 'register_settings' ) );
37
38 add_action( 'admin_menu', array( $this, 'plugin_menu' ) );
39 add_filter( 'plugin_action_links', array( $this, 'settings_link' ), 10, 2 );
40 add_action( 'admin_notices', array( $this, 'generate_notice' ) );
41
42 add_action( 'AllImagesRegenerateHook', array( $this, 'allimages_regenerate' ), 10, 1 );
43
44 }
45
46 /** ==================================================
47 * Add a "Settings" link to the plugins page
48 *
49 * @param array $links links array.
50 * @param string $file file.
51 * @return array $links links array.
52 * @since 1.00
53 */
54 public function settings_link( $links, $file ) {
55 static $this_plugin;
56 if ( empty( $this_plugin ) ) {
57 $this_plugin = 'plus-webp/pluswebp.php';
58 }
59 if ( $file === $this_plugin ) {
60 $links[] = '<a href="' . admin_url( 'tools.php?page=pluswebp' ) . '">' . esc_html__( 'Settings' ) . ' & ' . esc_html__( 'Bulk Generate', 'plus-webp' ) . '</a>';
61 }
62 return $links;
63 }
64
65 /** ==================================================
66 * Settings page
67 *
68 * @since 1.00
69 */
70 public function plugin_menu() {
71 add_management_page( 'Plus WebP', 'Plus WebP', 'manage_options', 'pluswebp', array( $this, 'plugin_options' ) );
72 }
73
74 /** ==================================================
75 * Settings page
76 *
77 * @since 1.00
78 */
79 public function plugin_options() {
80
81 if ( ! current_user_can( 'manage_options' ) ) {
82 wp_die( esc_html__( 'You do not have sufficient permissions to access this page.' ) );
83 }
84
85 $this->options_updated();
86 $pluswebp_settings = get_option( 'pluswebp' );
87
88 if ( isset( $_POST['Generatewebp'] ) && ! empty( $_POST['Generatewebp'] ) ) {
89 if ( check_admin_referer( 'pwp_gnt', 'pluswebp_gnt' ) ) {
90 if ( ! empty( $pluswebp_settings['types'] ) ) {
91 $user = wp_get_current_user();
92 $to = $user->user_email;
93 $userid = $user->ID;
94 if ( ! wp_next_scheduled( 'AllImagesRegenerateHook', array( $to ) ) ) {
95 wp_schedule_single_event( time(), 'AllImagesRegenerateHook', array( $to ) );
96 echo '<div class="notice notice-success is-dismissible"><ul><li>' . esc_html__( 'Background generation starts.', 'plus-webp' ) . '</li></ul></div>';
97 } else {
98 echo '<div class="notice notice-error is-dismissible"><ul><li>' . esc_html__( 'Currently, there is a process that is being bulk generated.', 'plus-webp' ) . '</li></ul></div>';
99 }
100 } else {
101 echo '<div class="notice notice-error is-dismissible"><ul><li>' . esc_html__( 'The image type for creating WebP is not specified.', 'plus-webp' ) . '</li></ul></div>';
102 }
103 }
104 }
105
106 $scriptname = admin_url( 'tools.php?page=pluswebp' );
107
108 ?>
109 <div class="wrap">
110 <h2>Plus WebP</h2>
111
112 <div id="bulkgeneratewebp-loading-container">
113
114 <details>
115 <summary><strong><?php esc_html_e( 'Various links of this plugin', 'plus-webp' ); ?></strong></summary>
116 <?php $this->credit(); ?>
117 </details>
118
119 <div class="wrap">
120 <h2><?php esc_html_e( 'Bulk Generate', 'plus-webp' ); ?></h2>
121 <details style="margin-bottom: 5px;">
122 <summary style="cursor: pointer; padding: 10px; border: 1px solid #ddd; background: #f4f4f4; color: #000;"><strong><?php esc_html_e( 'Generate', 'plus-webp' ); ?></strong></summary>
123 <?php
124 $post_ids = $this->get_allimages();
125 if ( ! empty( $post_ids ) ) {
126 ?>
127 <div style="margin: 5px; padding: 5px;">
128 <p class="description">
129 <?php esc_html_e( 'Press the following button to start WebP generation with Ajax.', 'plus-webp' ); ?>
130 </p>
131 </div>
132 <?php
133 /* for Ajax */
134 $handle = 'bulkgeneratewebp-js';
135 $action1 = 'bulkgeneratewebp-ajax-action';
136 $action2 = 'bulkgeneratewebp_message';
137 wp_enqueue_script( 'jquery' );
138 wp_enqueue_script( $handle, plugin_dir_url( __DIR__ ) . 'js/jquery.bulkgeneratewebp.js', array( 'jquery' ), '1.00', false );
139
140 wp_localize_script(
141 $handle,
142 'bulkgeneratewebp',
143 array(
144 'ajax_url' => admin_url( 'admin-ajax.php' ),
145 'action' => $action1,
146 'nonce' => wp_create_nonce( $action1 ),
147 )
148 );
149 wp_localize_script(
150 $handle,
151 'bulkgeneratewebp_mes',
152 array(
153 'ajax_url' => admin_url( 'admin-ajax.php' ),
154 'action' => $action2,
155 'nonce' => wp_create_nonce( $action2 ),
156 )
157 );
158 wp_localize_script(
159 $handle,
160 'bulkgeneratewebp_data',
161 array(
162 'count' => count( $post_ids ),
163 'id' => json_encode( $post_ids, JSON_UNESCAPED_SLASHES ),
164 )
165 );
166
167 wp_localize_script(
168 $handle,
169 'bulkgeneratewebp_text',
170 array(
171 'stop_button' => __( 'Stop', 'plus-webp' ),
172 'stop_message' => __( 'Stopping now..', 'plus-webp' ),
173 )
174 );
175 submit_button( __( 'Generate', 'plus-webp' ), 'primary', 'bulkgeneratewebp_ajax_update', false );
176 } else {
177 ?>
178 <div style="margin: 5px; padding: 5px;">
179 <p class="description">
180 <?php esc_html_e( 'There is no media in the media library that can generate WebP.', 'plus-webp' ); ?>
181 </p>
182 </div>
183 <?php
184 }
185 ?>
186 </details>
187 <details style="margin-bottom: 5px;">
188 <summary style="cursor: pointer; padding: 10px; border: 1px solid #ddd; background: #f4f4f4; color: #000;"><strong><?php esc_html_e( 'Generate Background', 'plus-webp' ); ?></strong></summary>
189 <?php
190 if ( ! empty( $post_ids ) ) {
191 ?>
192 <div style="margin: 5px; padding: 5px;">
193 <p class="description">
194 <?php esc_html_e( 'Press the following button to start WebP generation in the background. When finished, you will receive an email.', 'plus-webp' ); ?>
195 </p>
196 </div>
197 <form method="post" action="<?php echo esc_url( $scriptname ); ?>">
198 <?php wp_nonce_field( 'pwp_gnt', 'pluswebp_gnt' ); ?>
199 <?php submit_button( __( 'Generate Background', 'plus-webp' ), 'primary', 'Generatewebp', false ); ?>
200 </form>
201 <?php
202 } else {
203 ?>
204 <div style="margin: 5px; padding: 5px;">
205 <p class="description">
206 <?php esc_html_e( 'There is no media in the media library that can generate WebP.', 'plus-webp' ); ?>
207 </p>
208 </div>
209 <?php
210 }
211 ?>
212 </details>
213 </div>
214
215 <div class="wrap">
216 <h2><?php esc_html_e( 'Settings' ); ?></h2>
217
218 <form method="post" action="<?php echo esc_url( $scriptname ); ?>">
219 <?php wp_nonce_field( 'pwp_set', 'pluswebp_set' ); ?>
220
221 <details style="margin-bottom: 5px;">
222 <summary style="cursor: pointer; padding: 10px; border: 1px solid #ddd; background: #f4f4f4; color: #000;"><strong><?php esc_html_e( 'Quality', 'plus-webp' ); ?></strong></summary>
223 <div style="margin: 5px; padding: 5px;">
224 <?php esc_html_e( 'low resolution', 'plus-webp' ); ?>
225 <input type="range" style="vertical-align:middle;" name="quality" value="<?php echo esc_attr( $pluswebp_settings['quality'] ); ?>" min="0" max="100" step="1">
226 <?php esc_html_e( 'high resolution', 'plus-webp' ); ?>
227 <div style="margin: 5px; padding: 5px;">
228 <p class="description">
229 <?php esc_html_e( 'Specifies the quality of WebP. The higher the resolution, the larger the file size.', 'plus-webp' ); ?>
230 </p>
231 </div>
232 </div>
233 </details>
234
235 <details style="margin-bottom: 5px;">
236 <summary style="cursor: pointer; padding: 10px; border: 1px solid #ddd; background: #f4f4f4; color: #000;"><strong><?php esc_html_e( 'Type' ); ?></strong></summary>
237 <?php
238 $list_types = array(
239 'image/jpeg',
240 'image/png',
241 'image/bmp',
242 'image/gif',
243 );
244 foreach ( $list_types as $value ) {
245 if ( in_array( $value, $pluswebp_settings['types'] ) ) {
246 $check = 1;
247 } else {
248 $check = 0;
249 }
250 ?>
251 <div style="display: block;padding:5px 5px"><input type="checkbox" name="list_types[<?php echo esc_attr( $value ); ?>]" value="1" <?php checked( '1', $check ); ?> /><?php echo esc_html( $value ); ?></div>
252 <?php
253 }
254 ?>
255 <div style="margin: 5px; padding: 5px;">
256 <p class="description">
257 <?php esc_html_e( 'Check the images you want to convert to WebP.', 'plus-webp' ); ?>
258 </p>
259 </div>
260 </details>
261
262 <details style="margin-bottom: 5px;">
263 <summary style="cursor: pointer; padding: 10px; border: 1px solid #ddd; background: #f4f4f4; color: #000;"><strong><?php esc_html_e( 'Append the webp extension to the original filename', 'plus-webp' ); ?></strong></summary>
264 <div style="margin: 5px; padding: 5px;">
265 <input type="checkbox" name="addext" value="1" <?php checked( $pluswebp_settings['addext'], true ); ?>><?php esc_html_e( 'Apply' ); ?>
266 <div style="margin: 5px; padding: 5px;">
267 <p class="description">
268 <?php esc_html_e( 'Checking this setting, the extension webp will be appended to the name of the file, including the extension. Not checking, only the extension is changed.', 'plus-webp' ); ?>
269 </p>
270 </div>
271 </div>
272 </details>
273
274 <details style="margin-bottom: 5px;">
275 <summary style="cursor: pointer; padding: 10px; border: 1px solid #ddd; background: #f4f4f4; color: #000;"><strong><?php esc_html_e( 'WebP replacement of images and contents', 'plus-webp' ); ?></strong></summary>
276 <div style="margin: 5px; padding: 5px;">
277 <input type="checkbox" name="replace" value="1" <?php checked( $pluswebp_settings['replace'], true ); ?>><?php esc_html_e( 'Apply' ); ?>
278 <div style="margin: 5px; padding: 5px;">
279 <p class="description">
280 <?php esc_html_e( 'Checking this setting will replace image files with WebP when adding new media, and delete the original image file. Also, when generating all images, the original image file ID will be overwritten as WebP and the original image file will be deleted. All URLs in the content are also replaced.', 'plus-webp' ); ?>
281 </p>
282 </div>
283 </div>
284 </details>
285
286 <?php submit_button( __( 'Save Changes' ), 'large', 'Manageset', false ); ?>
287 </form>
288 </div>
289
290 </div>
291
292 </div>
293 <?php
294 }
295
296 /** ==================================================
297 * Credit
298 *
299 * @since 1.00
300 */
301 private function credit() {
302
303 $plugin_name = null;
304 $plugin_ver_num = null;
305 $plugin_path = plugin_dir_path( __DIR__ );
306 $plugin_dir = untrailingslashit( wp_normalize_path( $plugin_path ) );
307 $slugs = explode( '/', $plugin_dir );
308 $slug = end( $slugs );
309 $files = scandir( $plugin_dir );
310 foreach ( $files as $file ) {
311 if ( '.' === $file || '..' === $file || is_dir( $plugin_path . $file ) ) {
312 continue;
313 } else {
314 $exts = explode( '.', $file );
315 $ext = strtolower( end( $exts ) );
316 if ( 'php' === $ext ) {
317 $plugin_datas = get_file_data(
318 $plugin_path . $file,
319 array(
320 'name' => 'Plugin Name',
321 'version' => 'Version',
322 )
323 );
324 if ( array_key_exists( 'name', $plugin_datas ) && ! empty( $plugin_datas['name'] ) && array_key_exists( 'version', $plugin_datas ) && ! empty( $plugin_datas['version'] ) ) {
325 $plugin_name = $plugin_datas['name'];
326 $plugin_ver_num = $plugin_datas['version'];
327 break;
328 }
329 }
330 }
331 }
332 $plugin_version = __( 'Version:' ) . ' ' . $plugin_ver_num;
333 /* translators: FAQ Link & Slug */
334 $faq = sprintf( __( 'https://wordpress.org/plugins/%s/faq', 'plus-webp' ), $slug );
335 $support = 'https://wordpress.org/support/plugin/' . $slug;
336 $review = 'https://wordpress.org/support/view/plugin-reviews/' . $slug;
337 $translate = 'https://translate.wordpress.org/projects/wp-plugins/' . $slug;
338 $facebook = 'https://www.facebook.com/katsushikawamori/';
339 $twitter = 'https://twitter.com/dodesyo312';
340 $youtube = 'https://www.youtube.com/channel/UC5zTLeyROkvZm86OgNRcb_w';
341 $donate = __( 'https://shop.riverforest-wp.info/donate/', 'plus-webp' );
342
343 ?>
344 <span style="font-weight: bold;">
345 <div>
346 <?php echo esc_html( $plugin_version ); ?> |
347 <a style="text-decoration: none;" href="<?php echo esc_url( $faq ); ?>" target="_blank" rel="noopener noreferrer">FAQ</a> | <a style="text-decoration: none;" href="<?php echo esc_url( $support ); ?>" target="_blank" rel="noopener noreferrer">Support Forums</a> | <a style="text-decoration: none;" href="<?php echo esc_url( $review ); ?>" target="_blank" rel="noopener noreferrer">Reviews</a>
348 </div>
349 <div>
350 <a style="text-decoration: none;" href="<?php echo esc_url( $translate ); ?>" target="_blank" rel="noopener noreferrer">
351 <?php
352 /* translators: Plugin translation link */
353 echo esc_html( sprintf( __( 'Translations for %s' ), $plugin_name ) );
354 ?>
355 </a> | <a style="text-decoration: none;" href="<?php echo esc_url( $facebook ); ?>" target="_blank" rel="noopener noreferrer"><span class="dashicons dashicons-facebook"></span></a> | <a style="text-decoration: none;" href="<?php echo esc_url( $twitter ); ?>" target="_blank" rel="noopener noreferrer"><span class="dashicons dashicons-twitter"></span></a> | <a style="text-decoration: none;" href="<?php echo esc_url( $youtube ); ?>" target="_blank" rel="noopener noreferrer"><span class="dashicons dashicons-video-alt3"></span></a>
356 </div>
357 </span>
358
359 <div style="width: 250px; height: 180px; margin: 5px; padding: 5px; border: #CCC 2px solid;">
360 <h3><?php esc_html_e( 'Please make a donation if you like my work or would like to further the development of this plugin.', 'plus-webp' ); ?></h3>
361 <div style="text-align: right; margin: 5px; padding: 5px;"><span style="padding: 3px; color: #ffffff; background-color: #008000">Plugin Author</span> <span style="font-weight: bold;">Katsushi Kawamori</span></div>
362 <button type="button" style="margin: 5px; padding: 5px;" onclick="window.open('<?php echo esc_url( $donate ); ?>')"><?php esc_html_e( 'Donate to this plugin &#187;' ); ?></button>
363 </div>
364
365 <?php
366
367 }
368
369 /** ==================================================
370 * Get All Images
371 *
372 * @since 2.00
373 */
374 private function get_allimages() {
375
376 $pluswebp_settings = get_option( 'pluswebp' );
377 $args = array(
378 'post_type' => 'attachment',
379 'post_status' => 'inherit',
380 'post_mime_type' => $pluswebp_settings['types'],
381 'posts_per_page' => -1,
382 'orderby' => 'date',
383 'order' => 'DESC',
384 );
385 $posts = get_posts( $args );
386
387 global $wpdb;
388 $webp_titles = $wpdb->get_col(
389 "
390 SELECT post_title
391 FROM {$wpdb->prefix}posts
392 WHERE post_type = 'attachment'
393 AND post_mime_type IN ( 'image/webp' )
394 AND post_status = 'inherit'
395 "
396 );
397
398 $post_ids = array();
399 foreach ( $posts as $post ) {
400 if ( ! in_array( $post->post_title, $webp_titles ) ) {
401 $post_ids[] = $post->ID;
402 }
403 }
404
405 return $post_ids;
406
407 }
408
409 /** ==================================================
410 * Regenerate All Images
411 *
412 * @param string $to mail address.
413 * @since 1.00
414 */
415 public function allimages_regenerate( $to ) {
416
417 $post_ids = $this->get_allimages();
418
419 if ( ! empty( $post_ids ) ) {
420 delete_option( 'pluswebp_generate' );
421 include_once ABSPATH . 'wp-admin/includes/image.php';
422 /* Generate WebP */
423 foreach ( $post_ids as $attach_id ) {
424 $metadata = wp_get_attachment_metadata( $attach_id );
425 do_action( 'wp_generate_attachment_metadata', $metadata, $attach_id );
426 }
427 /* Mail send */
428 do_action( 'pluswebp_mail_send', $to );
429 } else {
430 ?>
431 <div class="notice notice-error is-dismissible"><ul><li><?php esc_html_e( 'There are no images to convert. Generation stopped.', 'plus-webp' ); ?></li></ul></div>';
432 <?php
433 }
434
435 }
436
437 /** ==================================================
438 * Update wp_options table.
439 *
440 * @since 1.02
441 */
442 private function options_updated() {
443
444 if ( isset( $_POST['Manageset'] ) && ! empty( $_POST['Manageset'] ) ) {
445 if ( check_admin_referer( 'pwp_set', 'pluswebp_set' ) ) {
446 $pluswebp_settings = get_option( 'pluswebp' );
447 $list_types = array();
448 if ( isset( $_POST['list_types'] ) && ! empty( $_POST['list_types'] ) ) {
449 $tmps = filter_var(
450 wp_unslash( $_POST['list_types'] ),
451 FILTER_CALLBACK,
452 array(
453 'options' => function( $value ) {
454 return sanitize_text_field( $value );
455 },
456 )
457 );
458 foreach ( $tmps as $key => $value ) {
459 $list_types[] = $key;
460 }
461 $pluswebp_settings['types'] = $list_types;
462 } else {
463 $pluswebp_settings['types'] = $list_types;
464 }
465 if ( isset( $_POST['addext'] ) && ! empty( $_POST['addext'] ) ) {
466 $pluswebp_settings['addext'] = true;
467 } else {
468 $pluswebp_settings['addext'] = false;
469 }
470 if ( isset( $_POST['replace'] ) && ! empty( $_POST['replace'] ) ) {
471 $pluswebp_settings['replace'] = true;
472 } else {
473 $pluswebp_settings['replace'] = false;
474 }
475 if ( isset( $_POST['quality'] ) ) {
476 $pluswebp_settings['quality'] = intval( $_POST['quality'] );
477 }
478 update_option( 'pluswebp', $pluswebp_settings );
479 echo '<div class="notice notice-success is-dismissible"><ul><li>' . esc_html__( 'Settings' ) . ' --> ' . esc_html__( 'Settings saved.' ) . '</li></ul></div>';
480 }
481 }
482
483 }
484
485 /** ==================================================
486 * Settings register
487 *
488 * @since 1.00
489 */
490 public function register_settings() {
491
492 if ( get_option( 'pluswebp' ) ) {
493 $pluswebp_settings = get_option( 'pluswebp' );
494 /* 'types' from ver 1.08 */
495 if ( ! array_key_exists( 'types', $pluswebp_settings ) ) {
496 $pluswebp_settings['types'] = array(
497 'image/jpeg',
498 'image/png',
499 'image/bmp',
500 'image/gif',
501 );
502 update_option( 'pluswebp', $pluswebp_settings );
503 }
504 /* 'addext' from ver 1.13 */
505 if ( ! array_key_exists( 'addext', $pluswebp_settings ) ) {
506 $pluswebp_settings['addext'] = false;
507 update_option( 'pluswebp', $pluswebp_settings );
508 }
509 } else {
510 $pswp_tbl = array(
511 'quality' => 80,
512 'replace' => false,
513 'addext' => false,
514 'types' => array(
515 'image/jpeg',
516 'image/png',
517 'image/bmp',
518 'image/gif',
519 ),
520 );
521 update_option( 'pluswebp', $pswp_tbl );
522 }
523
524 }
525
526 /** ==================================================
527 * Generate notice
528 *
529 * @since 1.09
530 */
531 public function generate_notice() {
532
533 if ( get_option( 'pluswebp_generate_mail' ) ) {
534 $pluswebp_mail_send = get_option( 'pluswebp_generate_mail' );
535 if ( 0 < $pluswebp_mail_send['count'] ) {
536 ?>
537 <div class="notice notice-success is-dismissible"><ul><li><strong>Plus WebP</strong>
538 <?php
539 /* translators: %1$s Date Time, %2$d File Count */
540 echo wp_kses_post( sprintf( __( ' : %1$s : %2$d WebP files and their thumbnails have been generated. Details have been sent by e-mail.', 'plus-webp' ), $pluswebp_mail_send['datetime'], $pluswebp_mail_send['count'] ) );
541 ?>
542 </li></ul></div>
543 <?php
544 } else {
545 ?>
546 <div class="notice notice-error is-dismissible"><ul><li><strong>Plus WebP</strong>
547 <?php
548 /* translators: %1$s Date Time %2$s error message */
549 echo wp_kses_post( sprintf( __( ' : %1$s : %2$s', 'plus-webp' ), $pluswebp_mail_send['datetime'], __( 'WebP was not generated. A file with the same name already exists.', 'plus-webp' ) ) );
550 ?>
551 </li></ul></div>
552 <?php
553 }
554 delete_option( 'pluswebp_generate_mail' );
555 }
556
557 }
558
559 }
560
561
562