PluginProbe
Plus WebP or AVIF / 2.05
Plus WebP or AVIF v2.05
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.05, at lib/class-pluswebpadmin.php

549 lines 19.4 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 <div style="margin: 5px; padding: 5px;">
190 <p class="description">
191 <?php esc_html_e( 'Press the following button to start WebP generation in the background. When finished, you will receive an email.', 'plus-webp' ); ?>
192 </p>
193 </div>
194 <form method="post" action="<?php echo esc_url( $scriptname ); ?>">
195 <?php wp_nonce_field( 'pwp_gnt', 'pluswebp_gnt' ); ?>
196 <?php submit_button( __( 'Generate Background', 'plus-webp' ), 'primary', 'Generatewebp', false ); ?>
197 </form>
198 </details>
199
200 </div>
201
202 <div class="wrap">
203 <h2><?php esc_html_e( 'Settings' ); ?></h2>
204
205 <form method="post" action="<?php echo esc_url( $scriptname ); ?>">
206 <?php wp_nonce_field( 'pwp_set', 'pluswebp_set' ); ?>
207
208 <details style="margin-bottom: 5px;">
209 <summary style="cursor: pointer; padding: 10px; border: 1px solid #ddd; background: #f4f4f4; color: #000;"><strong><?php esc_html_e( 'Quality', 'plus-webp' ); ?></strong></summary>
210 <div style="margin: 5px; padding: 5px;">
211 <?php esc_html_e( 'low resolution', 'plus-webp' ); ?>
212 <input type="range" style="vertical-align:middle;" name="quality" value="<?php echo esc_attr( $pluswebp_settings['quality'] ); ?>" min="0" max="100" step="1">
213 <?php esc_html_e( 'high resolution', 'plus-webp' ); ?>
214 <div style="margin: 5px; padding: 5px;">
215 <p class="description">
216 <?php esc_html_e( 'Specifies the quality of WebP. The higher the resolution, the larger the file size.', 'plus-webp' ); ?>
217 </p>
218 </div>
219 </div>
220 </details>
221
222 <details style="margin-bottom: 5px;">
223 <summary style="cursor: pointer; padding: 10px; border: 1px solid #ddd; background: #f4f4f4; color: #000;"><strong><?php esc_html_e( 'Type' ); ?></strong></summary>
224 <?php
225 $list_types = array(
226 'image/jpeg',
227 'image/png',
228 'image/bmp',
229 'image/gif',
230 );
231 foreach ( $list_types as $value ) {
232 if ( in_array( $value, $pluswebp_settings['types'] ) ) {
233 $check = 1;
234 } else {
235 $check = 0;
236 }
237 ?>
238 <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>
239 <?php
240 }
241 ?>
242 <div style="margin: 5px; padding: 5px;">
243 <p class="description">
244 <?php esc_html_e( 'Check the images you want to convert to WebP.', 'plus-webp' ); ?>
245 </p>
246 </div>
247 </details>
248
249 <details style="margin-bottom: 5px;">
250 <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>
251 <div style="margin: 5px; padding: 5px;">
252 <input type="checkbox" name="addext" value="1" <?php checked( $pluswebp_settings['addext'], true ); ?>><?php esc_html_e( 'Apply' ); ?>
253 <div style="margin: 5px; padding: 5px;">
254 <p class="description">
255 <?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' ); ?>
256 </p>
257 </div>
258 </div>
259 </details>
260
261 <details style="margin-bottom: 5px;">
262 <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>
263 <div style="margin: 5px; padding: 5px;">
264 <input type="checkbox" name="replace" value="1" <?php checked( $pluswebp_settings['replace'], true ); ?>><?php esc_html_e( 'Apply' ); ?>
265 <div style="margin: 5px; padding: 5px;">
266 <p class="description">
267 <?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' ); ?>
268 </p>
269 </div>
270 </div>
271 </details>
272
273 <?php submit_button( __( 'Save Changes' ), 'large', 'Manageset', false ); ?>
274 </form>
275 </div>
276
277 </div>
278
279 </div>
280 <?php
281 }
282
283 /** ==================================================
284 * Credit
285 *
286 * @since 1.00
287 */
288 private function credit() {
289
290 $plugin_name = null;
291 $plugin_ver_num = null;
292 $plugin_path = plugin_dir_path( __DIR__ );
293 $plugin_dir = untrailingslashit( wp_normalize_path( $plugin_path ) );
294 $slugs = explode( '/', $plugin_dir );
295 $slug = end( $slugs );
296 $files = scandir( $plugin_dir );
297 foreach ( $files as $file ) {
298 if ( '.' === $file || '..' === $file || is_dir( $plugin_path . $file ) ) {
299 continue;
300 } else {
301 $exts = explode( '.', $file );
302 $ext = strtolower( end( $exts ) );
303 if ( 'php' === $ext ) {
304 $plugin_datas = get_file_data(
305 $plugin_path . $file,
306 array(
307 'name' => 'Plugin Name',
308 'version' => 'Version',
309 )
310 );
311 if ( array_key_exists( 'name', $plugin_datas ) && ! empty( $plugin_datas['name'] ) && array_key_exists( 'version', $plugin_datas ) && ! empty( $plugin_datas['version'] ) ) {
312 $plugin_name = $plugin_datas['name'];
313 $plugin_ver_num = $plugin_datas['version'];
314 break;
315 }
316 }
317 }
318 }
319 $plugin_version = __( 'Version:' ) . ' ' . $plugin_ver_num;
320 /* translators: FAQ Link & Slug */
321 $faq = sprintf( __( 'https://wordpress.org/plugins/%s/faq', 'plus-webp' ), $slug );
322 $support = 'https://wordpress.org/support/plugin/' . $slug;
323 $review = 'https://wordpress.org/support/view/plugin-reviews/' . $slug;
324 $translate = 'https://translate.wordpress.org/projects/wp-plugins/' . $slug;
325 $facebook = 'https://www.facebook.com/katsushikawamori/';
326 $twitter = 'https://twitter.com/dodesyo312';
327 $youtube = 'https://www.youtube.com/channel/UC5zTLeyROkvZm86OgNRcb_w';
328 $donate = __( 'https://shop.riverforest-wp.info/donate/', 'plus-webp' );
329
330 ?>
331 <span style="font-weight: bold;">
332 <div>
333 <?php echo esc_html( $plugin_version ); ?> |
334 <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>
335 </div>
336 <div>
337 <a style="text-decoration: none;" href="<?php echo esc_url( $translate ); ?>" target="_blank" rel="noopener noreferrer">
338 <?php
339 /* translators: Plugin translation link */
340 echo esc_html( sprintf( __( 'Translations for %s' ), $plugin_name ) );
341 ?>
342 </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>
343 </div>
344 </span>
345
346 <div style="width: 250px; height: 180px; margin: 5px; padding: 5px; border: #CCC 2px solid;">
347 <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>
348 <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>
349 <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>
350 </div>
351
352 <?php
353
354 }
355
356 /** ==================================================
357 * Get All Images
358 *
359 * @since 2.00
360 */
361 private function get_allimages() {
362
363 $pluswebp_settings = get_option( 'pluswebp' );
364 $args = array(
365 'post_type' => 'attachment',
366 'post_status' => 'inherit',
367 'post_mime_type' => $pluswebp_settings['types'],
368 'posts_per_page' => -1,
369 'orderby' => 'date',
370 'order' => 'DESC',
371 );
372 $posts = get_posts( $args );
373
374 global $wpdb;
375 $webp_titles = $wpdb->get_col(
376 "
377 SELECT post_title
378 FROM {$wpdb->prefix}posts
379 WHERE post_type = 'attachment'
380 AND post_mime_type IN ( 'image/webp' )
381 AND post_status = 'inherit'
382 "
383 );
384
385 $post_ids = array();
386 foreach ( $posts as $post ) {
387 if ( ! in_array( $post->post_title, $webp_titles ) ) {
388 $post_ids[] = $post->ID;
389 }
390 }
391
392 return $post_ids;
393
394 }
395
396 /** ==================================================
397 * Regenerate All Images
398 *
399 * @param string $to mail address.
400 * @since 1.00
401 */
402 public function allimages_regenerate( $to ) {
403
404 $post_ids = $this->get_allimages();
405
406 if ( ! empty( $post_ids ) ) {
407 delete_option( 'pluswebp_generate' );
408 include_once ABSPATH . 'wp-admin/includes/image.php';
409 /* Generate WebP */
410 foreach ( $post_ids as $attach_id ) {
411 $metadata = wp_get_attachment_metadata( $attach_id );
412 do_action( 'wp_generate_attachment_metadata', $metadata, $attach_id );
413 }
414 /* Mail send */
415 do_action( 'pluswebp_mail_send', $to );
416 } else {
417 ?>
418 <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>';
419 <?php
420 }
421
422 }
423
424 /** ==================================================
425 * Update wp_options table.
426 *
427 * @since 1.02
428 */
429 private function options_updated() {
430
431 if ( isset( $_POST['Manageset'] ) && ! empty( $_POST['Manageset'] ) ) {
432 if ( check_admin_referer( 'pwp_set', 'pluswebp_set' ) ) {
433 $pluswebp_settings = get_option( 'pluswebp' );
434 $list_types = array();
435 if ( isset( $_POST['list_types'] ) && ! empty( $_POST['list_types'] ) ) {
436 $tmps = filter_var(
437 wp_unslash( $_POST['list_types'] ),
438 FILTER_CALLBACK,
439 array(
440 'options' => function( $value ) {
441 return sanitize_text_field( $value );
442 },
443 )
444 );
445 foreach ( $tmps as $key => $value ) {
446 $list_types[] = $key;
447 }
448 $pluswebp_settings['types'] = $list_types;
449 } else {
450 $pluswebp_settings['types'] = $list_types;
451 }
452 if ( isset( $_POST['addext'] ) && ! empty( $_POST['addext'] ) ) {
453 $pluswebp_settings['addext'] = true;
454 } else {
455 $pluswebp_settings['addext'] = false;
456 }
457 if ( isset( $_POST['replace'] ) && ! empty( $_POST['replace'] ) ) {
458 $pluswebp_settings['replace'] = true;
459 } else {
460 $pluswebp_settings['replace'] = false;
461 }
462 if ( isset( $_POST['quality'] ) ) {
463 $pluswebp_settings['quality'] = intval( $_POST['quality'] );
464 }
465 update_option( 'pluswebp', $pluswebp_settings );
466 echo '<div class="notice notice-success is-dismissible"><ul><li>' . esc_html__( 'Settings' ) . ' --> ' . esc_html__( 'Settings saved.' ) . '</li></ul></div>';
467 }
468 }
469
470 }
471
472 /** ==================================================
473 * Settings register
474 *
475 * @since 1.00
476 */
477 public function register_settings() {
478
479 if ( get_option( 'pluswebp' ) ) {
480 $pluswebp_settings = get_option( 'pluswebp' );
481 /* 'types' from ver 1.08 */
482 if ( ! array_key_exists( 'types', $pluswebp_settings ) ) {
483 $pluswebp_settings['types'] = array(
484 'image/jpeg',
485 'image/png',
486 'image/bmp',
487 'image/gif',
488 );
489 update_option( 'pluswebp', $pluswebp_settings );
490 }
491 /* 'addext' from ver 1.13 */
492 if ( ! array_key_exists( 'addext', $pluswebp_settings ) ) {
493 $pluswebp_settings['addext'] = false;
494 update_option( 'pluswebp', $pluswebp_settings );
495 }
496 } else {
497 $pswp_tbl = array(
498 'quality' => 80,
499 'replace' => false,
500 'addext' => false,
501 'types' => array(
502 'image/jpeg',
503 'image/png',
504 'image/bmp',
505 'image/gif',
506 ),
507 );
508 update_option( 'pluswebp', $pswp_tbl );
509 }
510
511 }
512
513 /** ==================================================
514 * Generate notice
515 *
516 * @since 1.09
517 */
518 public function generate_notice() {
519
520 if ( get_option( 'pluswebp_generate_mail' ) ) {
521 $pluswebp_mail_send = get_option( 'pluswebp_generate_mail' );
522 if ( 0 < $pluswebp_mail_send['count'] ) {
523 ?>
524 <div class="notice notice-success is-dismissible"><ul><li><strong>Plus WebP</strong>
525 <?php
526 /* translators: %1$s Date Time, %2$d File Count */
527 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'] ) );
528 ?>
529 </li></ul></div>
530 <?php
531 } else {
532 ?>
533 <div class="notice notice-error is-dismissible"><ul><li><strong>Plus WebP</strong>
534 <?php
535 /* translators: %1$s Date Time %2$s error message */
536 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' ) ) );
537 ?>
538 </li></ul></div>
539 <?php
540 }
541 delete_option( 'pluswebp_generate_mail' );
542 }
543
544 }
545
546 }
547
548
549