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

516 lines 18.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_menu', array( $this, 'plugin_menu' ) );
37 add_filter( 'plugin_action_links', array( $this, 'settings_link' ), 10, 2 );
38 add_action( 'admin_notices', array( $this, 'generate_notice' ) );
39
40 add_action( 'AllImagesRegenerateHook', array( $this, 'allimages_regenerate' ), 10, 1 );
41
42 }
43
44 /** ==================================================
45 * Add a "Settings" link to the plugins page
46 *
47 * @param array $links links array.
48 * @param string $file file.
49 * @return array $links links array.
50 * @since 1.00
51 */
52 public function settings_link( $links, $file ) {
53 static $this_plugin;
54 if ( empty( $this_plugin ) ) {
55 $this_plugin = 'plus-webp/pluswebp.php';
56 }
57 if ( $file === $this_plugin ) {
58 $links[] = '<a href="' . admin_url( 'tools.php?page=pluswebp' ) . '">' . esc_html__( 'Settings' ) . ' & ' . esc_html__( 'Bulk Generate', 'plus-webp' ) . '</a>';
59 }
60 return $links;
61 }
62
63 /** ==================================================
64 * Settings page
65 *
66 * @since 1.00
67 */
68 public function plugin_menu() {
69 add_management_page( 'Plus WebP', 'Plus WebP', 'manage_options', 'pluswebp', array( $this, 'plugin_options' ) );
70 }
71
72 /** ==================================================
73 * Settings page
74 *
75 * @since 1.00
76 */
77 public function plugin_options() {
78
79 if ( ! current_user_can( 'manage_options' ) ) {
80 wp_die( esc_html__( 'You do not have sufficient permissions to access this page.' ) );
81 }
82
83 $this->options_updated();
84 $pluswebp_settings = get_option( 'pluswebp' );
85
86 if ( isset( $_POST['Generatewebp'] ) && ! empty( $_POST['Generatewebp'] ) ) {
87 if ( check_admin_referer( 'pwp_gnt', 'pluswebp_gnt' ) ) {
88 if ( ! empty( $pluswebp_settings['types'] ) ) {
89 $user = wp_get_current_user();
90 $to = $user->user_email;
91 $userid = $user->ID;
92 if ( ! wp_next_scheduled( 'AllImagesRegenerateHook', array( $to ) ) ) {
93 wp_schedule_single_event( time(), 'AllImagesRegenerateHook', array( $to ) );
94 echo '<div class="notice notice-success is-dismissible"><ul><li>' . esc_html__( 'Background generation starts.', 'plus-webp' ) . '</li></ul></div>';
95 } else {
96 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>';
97 }
98 } else {
99 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>';
100 }
101 }
102 }
103
104 $scriptname = admin_url( 'tools.php?page=pluswebp' );
105
106 ?>
107 <div class="wrap">
108 <h2>Plus WebP</h2>
109
110 <div id="bulkgeneratewebp-loading-container">
111
112 <details>
113 <summary><strong><?php esc_html_e( 'Various links of this plugin', 'plus-webp' ); ?></strong></summary>
114 <?php $this->credit(); ?>
115 </details>
116
117 <div class="wrap">
118 <h2><?php esc_html_e( 'Bulk Generate', 'plus-webp' ); ?></h2>
119 <details style="margin-bottom: 5px;">
120 <summary style="cursor: pointer; padding: 10px; border: 1px solid #ddd; background: #f4f4f4; color: #000;"><strong><?php esc_html_e( 'Generate', 'plus-webp' ); ?></strong></summary>
121 <?php
122 $post_ids = $this->get_allimages();
123 if ( ! empty( $post_ids ) ) {
124 ?>
125 <div style="margin: 5px; padding: 5px;">
126 <p class="description">
127 <?php esc_html_e( 'Press the following button to start WebP generation with Ajax.', 'plus-webp' ); ?>
128 </p>
129 </div>
130 <?php
131 /* for Ajax */
132 $handle = 'bulkgeneratewebp-js';
133 $action1 = 'bulkgeneratewebp-ajax-action';
134 $action2 = 'bulkgeneratewebp_message';
135 wp_enqueue_script( 'jquery' );
136 wp_enqueue_script( $handle, plugin_dir_url( __DIR__ ) . 'js/jquery.bulkgeneratewebp.js', array( 'jquery' ), '1.00', false );
137
138 wp_localize_script(
139 $handle,
140 'bulkgeneratewebp',
141 array(
142 'ajax_url' => admin_url( 'admin-ajax.php' ),
143 'action' => $action1,
144 'nonce' => wp_create_nonce( $action1 ),
145 )
146 );
147 wp_localize_script(
148 $handle,
149 'bulkgeneratewebp_mes',
150 array(
151 'ajax_url' => admin_url( 'admin-ajax.php' ),
152 'action' => $action2,
153 'nonce' => wp_create_nonce( $action2 ),
154 )
155 );
156 wp_localize_script(
157 $handle,
158 'bulkgeneratewebp_data',
159 array(
160 'count' => count( $post_ids ),
161 'id' => json_encode( $post_ids, JSON_UNESCAPED_SLASHES ),
162 )
163 );
164
165 wp_localize_script(
166 $handle,
167 'bulkgeneratewebp_text',
168 array(
169 'stop_button' => __( 'Stop', 'plus-webp' ),
170 'stop_message' => __( 'Stopping now..', 'plus-webp' ),
171 )
172 );
173 submit_button( __( 'Generate', 'plus-webp' ), 'primary', 'bulkgeneratewebp_ajax_update', false );
174 } else {
175 ?>
176 <div style="margin: 5px; padding: 5px;">
177 <p class="description">
178 <?php esc_html_e( 'There is no media in the media library that can generate WebP.', 'plus-webp' ); ?>
179 </p>
180 </div>
181 <?php
182 }
183 ?>
184 </details>
185 <details style="margin-bottom: 5px;">
186 <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>
187 <div style="margin: 5px; padding: 5px;">
188 <p class="description">
189 <?php esc_html_e( 'Press the following button to start WebP generation in the background. When finished, you will receive an email.', 'plus-webp' ); ?>
190 </p>
191 </div>
192 <form method="post" action="<?php echo esc_url( $scriptname ); ?>">
193 <?php wp_nonce_field( 'pwp_gnt', 'pluswebp_gnt' ); ?>
194 <?php submit_button( __( 'Generate Background', 'plus-webp' ), 'primary', 'Generatewebp', false ); ?>
195 </form>
196 </details>
197
198 </div>
199
200 <div class="wrap">
201 <h2><?php esc_html_e( 'Settings' ); ?></h2>
202
203 <form method="post" action="<?php echo esc_url( $scriptname ); ?>">
204 <?php wp_nonce_field( 'pwp_set', 'pluswebp_set' ); ?>
205
206 <details style="margin-bottom: 5px;">
207 <summary style="cursor: pointer; padding: 10px; border: 1px solid #ddd; background: #f4f4f4; color: #000;"><strong><?php esc_html_e( 'Quality', 'plus-webp' ); ?></strong></summary>
208 <div style="margin: 5px; padding: 5px;">
209 <?php esc_html_e( 'low resolution', 'plus-webp' ); ?>
210 <input type="range" style="vertical-align:middle;" name="quality" value="<?php echo esc_attr( $pluswebp_settings['quality'] ); ?>" min="0" max="100" step="1">
211 <?php esc_html_e( 'high resolution', 'plus-webp' ); ?>
212 <div style="margin: 5px; padding: 5px;">
213 <p class="description">
214 <?php esc_html_e( 'Specifies the quality of WebP. The higher the resolution, the larger the file size.', 'plus-webp' ); ?>
215 </p>
216 </div>
217 </div>
218 </details>
219
220 <details style="margin-bottom: 5px;">
221 <summary style="cursor: pointer; padding: 10px; border: 1px solid #ddd; background: #f4f4f4; color: #000;"><strong><?php esc_html_e( 'Type' ); ?></strong></summary>
222 <?php
223 $list_types = array(
224 'image/jpeg',
225 'image/png',
226 'image/bmp',
227 'image/gif',
228 );
229 foreach ( $list_types as $value ) {
230 if ( in_array( $value, $pluswebp_settings['types'] ) ) {
231 $check = 1;
232 } else {
233 $check = 0;
234 }
235 ?>
236 <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>
237 <?php
238 }
239 ?>
240 <div style="margin: 5px; padding: 5px;">
241 <p class="description">
242 <?php esc_html_e( 'Check the images you want to convert to WebP.', 'plus-webp' ); ?>
243 </p>
244 </div>
245 </details>
246
247 <details style="margin-bottom: 5px;">
248 <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>
249 <div style="margin: 5px; padding: 5px;">
250 <input type="checkbox" name="addext" value="1" <?php checked( $pluswebp_settings['addext'], true ); ?>><?php esc_html_e( 'Apply' ); ?>
251 <div style="margin: 5px; padding: 5px;">
252 <p class="description">
253 <?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' ); ?>
254 </p>
255 </div>
256 </div>
257 </details>
258
259 <details style="margin-bottom: 5px;">
260 <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>
261 <div style="margin: 5px; padding: 5px;">
262 <input type="checkbox" name="replace" value="1" <?php checked( $pluswebp_settings['replace'], true ); ?>><?php esc_html_e( 'Apply' ); ?>
263 <div style="margin: 5px; padding: 5px;">
264 <p class="description">
265 <?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' ); ?>
266 </p>
267 </div>
268 </div>
269 </details>
270
271 <?php submit_button( __( 'Save Changes' ), 'large', 'Manageset', false ); ?>
272 </form>
273 </div>
274
275 </div>
276
277 </div>
278 <?php
279 }
280
281 /** ==================================================
282 * Credit
283 *
284 * @since 1.00
285 */
286 private function credit() {
287
288 $plugin_name = null;
289 $plugin_ver_num = null;
290 $plugin_path = plugin_dir_path( __DIR__ );
291 $plugin_dir = untrailingslashit( $plugin_path );
292 $slugs = explode( '/', $plugin_dir );
293 $slug = end( $slugs );
294 $files = scandir( $plugin_dir );
295 foreach ( $files as $file ) {
296 if ( '.' === $file || '..' === $file || is_dir( $plugin_path . $file ) ) {
297 continue;
298 } else {
299 $exts = explode( '.', $file );
300 $ext = strtolower( end( $exts ) );
301 if ( 'php' === $ext ) {
302 $plugin_datas = get_file_data(
303 $plugin_path . $file,
304 array(
305 'name' => 'Plugin Name',
306 'version' => 'Version',
307 )
308 );
309 if ( array_key_exists( 'name', $plugin_datas ) && ! empty( $plugin_datas['name'] ) && array_key_exists( 'version', $plugin_datas ) && ! empty( $plugin_datas['version'] ) ) {
310 $plugin_name = $plugin_datas['name'];
311 $plugin_ver_num = $plugin_datas['version'];
312 break;
313 }
314 }
315 }
316 }
317 $plugin_version = __( 'Version:' ) . ' ' . $plugin_ver_num;
318 /* translators: FAQ Link & Slug */
319 $faq = sprintf( esc_html__( 'https://wordpress.org/plugins/%s/faq', '%s' ), $slug );
320 $support = 'https://wordpress.org/support/plugin/' . $slug;
321 $review = 'https://wordpress.org/support/view/plugin-reviews/' . $slug;
322 $translate = 'https://translate.wordpress.org/projects/wp-plugins/' . $slug;
323 $facebook = 'https://www.facebook.com/katsushikawamori/';
324 $twitter = 'https://twitter.com/dodesyo312';
325 $youtube = 'https://www.youtube.com/channel/UC5zTLeyROkvZm86OgNRcb_w';
326 $donate = sprintf( esc_html__( 'https://shop.riverforest-wp.info/donate/', '%s' ), $slug );
327
328 ?>
329 <span style="font-weight: bold;">
330 <div>
331 <?php echo esc_html( $plugin_version ); ?> |
332 <a style="text-decoration: none;" href="<?php echo esc_url( $faq ); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'FAQ' ); ?></a> | <a style="text-decoration: none;" href="<?php echo esc_url( $support ); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Support Forums' ); ?></a> | <a style="text-decoration: none;" href="<?php echo esc_url( $review ); ?>" target="_blank" rel="noopener noreferrer"><?php sprintf( esc_html_e( 'Reviews', '%s' ), $slug ); ?></a>
333 </div>
334 <div>
335 <a style="text-decoration: none;" href="<?php echo esc_url( $translate ); ?>" target="_blank" rel="noopener noreferrer">
336 <?php
337 /* translators: Plugin translation link */
338 echo sprintf( esc_html__( 'Translations for %s' ), esc_html( $plugin_name ) );
339 ?>
340 </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>
341 </div>
342 </span>
343
344 <div style="width: 250px; height: 180px; margin: 5px; padding: 5px; border: #CCC 2px solid;">
345 <h3><?php sprintf( esc_html_e( 'Please make a donation if you like my work or would like to further the development of this plugin.', '%s' ), $slug ); ?></h3>
346 <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>
347 <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>
348 </div>
349
350 <?php
351
352 }
353
354 /** ==================================================
355 * Get All Images
356 *
357 * @since 2.00
358 */
359 private function get_allimages() {
360
361 $pluswebp_settings = get_option( 'pluswebp' );
362
363 global $wpdb;
364
365 $mime_types = null;
366 foreach ( $pluswebp_settings['types'] as $value ) {
367 $mime_types .= "'" . $value . "'" . ',';
368 }
369 $mime_types = rtrim( $mime_types, ',' );
370
371 $wpdb->plus_webp_mime_type = 'AND post_mime_type IN (' . $mime_types . ')';
372
373 $post_obj = $wpdb->get_results(
374 "
375 SELECT ID, post_title
376 FROM $wpdb->posts
377 WHERE post_type = 'attachment'
378 $wpdb->plus_webp_mime_type
379 AND post_status = 'inherit'
380 "
381 );
382
383 $webp_titles = $wpdb->get_col(
384 "
385 SELECT post_title
386 FROM $wpdb->posts
387 WHERE post_type = 'attachment'
388 AND post_mime_type IN ( 'image/webp' )
389 AND post_status = 'inherit'
390 "
391 );
392
393 $post_ids = array();
394 foreach ( $post_obj as $post ) {
395 if ( ! in_array( $post->post_title, $webp_titles ) ) {
396 $post_ids[] = $post->ID;
397 }
398 }
399
400 return $post_ids;
401
402 }
403
404 /** ==================================================
405 * Regenerate All Images
406 *
407 * @param string $to mail address.
408 * @since 1.00
409 */
410 public function allimages_regenerate( $to ) {
411
412 $post_ids = $this->get_allimages();
413
414 if ( ! empty( $post_ids ) ) {
415 delete_option( 'pluswebp_generate' );
416 include_once ABSPATH . 'wp-admin/includes/image.php';
417 /* Generate WebP */
418 foreach ( $post_ids as $attach_id ) {
419 $metadata = wp_get_attachment_metadata( $attach_id );
420 do_action( 'wp_generate_attachment_metadata', $metadata, $attach_id );
421 }
422 /* Mail send */
423 do_action( 'pluswebp_mail_send', $to );
424 } else {
425 ?>
426 <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>';
427 <?php
428 }
429
430 }
431
432 /** ==================================================
433 * Update wp_options table.
434 *
435 * @since 1.02
436 */
437 private function options_updated() {
438
439 if ( isset( $_POST['Manageset'] ) && ! empty( $_POST['Manageset'] ) ) {
440 if ( check_admin_referer( 'pwp_set', 'pluswebp_set' ) ) {
441 $pluswebp_settings = get_option( 'pluswebp' );
442 $list_types = array();
443 if ( isset( $_POST['list_types'] ) && ! empty( $_POST['list_types'] ) ) {
444 $tmps = filter_var(
445 wp_unslash( $_POST['list_types'] ),
446 FILTER_CALLBACK,
447 [
448 'options' => function( $value ) {
449 return sanitize_text_field( $value );
450 },
451 ]
452 );
453 foreach ( $tmps as $key => $value ) {
454 $list_types[] = $key;
455 }
456 $pluswebp_settings['types'] = $list_types;
457 } else {
458 $pluswebp_settings['types'] = $list_types;
459 }
460 if ( isset( $_POST['addext'] ) && ! empty( $_POST['addext'] ) ) {
461 $pluswebp_settings['addext'] = true;
462 } else {
463 $pluswebp_settings['addext'] = false;
464 }
465 if ( isset( $_POST['replace'] ) && ! empty( $_POST['replace'] ) ) {
466 $pluswebp_settings['replace'] = true;
467 } else {
468 $pluswebp_settings['replace'] = false;
469 }
470 if ( isset( $_POST['quality'] ) ) {
471 $pluswebp_settings['quality'] = intval( $_POST['quality'] );
472 }
473 update_option( 'pluswebp', $pluswebp_settings );
474 echo '<div class="notice notice-success is-dismissible"><ul><li>' . esc_html__( 'Settings' ) . ' --> ' . esc_html__( 'Settings saved.' ) . '</li></ul></div>';
475 }
476 }
477
478 }
479
480 /** ==================================================
481 * Generate notice
482 *
483 * @since 1.09
484 */
485 public function generate_notice() {
486
487 if ( get_option( 'pluswebp_generate_mail' ) ) {
488 $pluswebp_mail_send = get_option( 'pluswebp_generate_mail' );
489 if ( 0 < $pluswebp_mail_send['count'] ) {
490 ?>
491 <div class="notice notice-success is-dismissible"><ul><li><strong>Plus WebP</strong>
492 <?php
493 /* translators: %1$s Date Time, %2$d File Count */
494 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'] ) );
495 ?>
496 </li></ul></div>
497 <?php
498 } else {
499 ?>
500 <div class="notice notice-error is-dismissible"><ul><li><strong>Plus WebP</strong>
501 <?php
502 /* translators: %1$s Date Time %2$s error message */
503 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' ) ) );
504 ?>
505 </li></ul></div>
506 <?php
507 }
508 delete_option( 'pluswebp_generate_mail' );
509 }
510
511 }
512
513 }
514
515
516