PluginProbe
Plus WebP or AVIF / 4.11
Plus WebP or AVIF v4.11
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
← All changes | lib/class-pluswebpadmin.php +78 -197 trunk4.11 View file →
@@ -1,9 +1,9 @@
1 1 <?php
2 2 /**
3 - * Plus WebP or AVIF
3 + * Plus WebP
4 4 *
5 - * @package Plus WebP or AVIF
5 + * @package Plus WebP
6 6 * @subpackage PlusWebpAdmin Management screen
7 7 Copyright (c) 2019- Katsushi Kawamori (email : dodesyoswift312@gmail.com)
8 8 This program is free software; you can redistribute it and/or modify
9 9 it under the terms of the GNU General Public License as published by
@@ -18,12 +18,8 @@
18 18 along with this program; if not, write to the Free Software
19 19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 20 */
21 21
22 -if ( ! defined( 'ABSPATH' ) ) {
23 - exit;
24 -}
25 -
26 22 $pluswebpadmin = new PlusWebpAdmin();
27 23
28 24 /** ==================================================
29 25 * Management screen
@@ -59,9 +55,9 @@
59 55 if ( empty( $this_plugin ) ) {
60 56 $this_plugin = 'plus-webp/pluswebp.php';
61 57 }
62 58 if ( $file === $this_plugin ) {
63 - $links[] = '<a href="' . admin_url( 'upload.php?page=plus-webp' ) . '">' . esc_html__( 'Settings', 'plus-webp' ) . ' & ' . esc_html__( 'Bulk Generate', 'plus-webp' ) . '</a>';
59 + $links[] = '<a href="' . admin_url( 'upload.php?page=plus-webp' ) . '">' . esc_html__( 'Settings' ) . ' & ' . esc_html__( 'Bulk Generate', 'plus-webp' ) . '</a>';
64 60 }
65 61 return $links;
66 62 }
67 63
@@ -72,10 +68,10 @@
72 68 */
73 69 public function plugin_menu() {
74 70
75 71 add_media_page(
76 - 'Plus WebP or AVIF',
77 - 'Plus WebP or AVIF',
72 + 'Plus WebP',
73 + 'Plus WebP',
78 74 'manage_options',
79 75 'plus-webp',
80 76 array( $this, 'plus_webp_generate_setteings' )
81 77 );
@@ -87,21 +83,9 @@
87 83 * @since 4.00
88 84 */
89 85 public function plus_webp_generate_setteings() {
90 86
91 - if ( ! current_user_can( 'manage_options' ) ) {
92 - wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'plus-webp' ) );
93 - }
94 -
95 - global $wp_version;
96 - $requires = '6.6';
97 - if ( version_compare( $wp_version, $requires, '>=' ) ) {
98 - $admin_screen = esc_html__( 'Loading…', 'plus-webp' );
99 - } else {
100 - /* translators: WordPress requires version */
101 - $admin_screen = sprintf( esc_html__( 'WordPress %s or higher is required to view this screen.', 'plus-webp' ), $requires );
102 - }
103 - printf( '<div class="wrap" id="plus-webp-page">%s</div>', esc_html( $admin_screen ) );
87 + echo '<div id="plus-webp-page"></div>';
104 88 }
105 89
106 90 /** ==================================================
107 91 * Load script
@@ -114,19 +98,13 @@
114 98 if ( 'media_page_plus-webp' !== $hook_suffix ) {
115 99 return;
116 100 }
117 101
118 - $asset_file = plugin_dir_path( __DIR__ ) . 'guten/build/index.asset.php';
102 + $asset_file = include plugin_dir_path( __DIR__ ) . 'guten/dist/plus-webp.asset.php';
119 103
120 - if ( ! file_exists( $asset_file ) ) {
121 - return;
122 - }
123 -
124 - $asset = include $asset_file;
125 -
126 104 wp_enqueue_style(
127 105 'plus-webp-style',
128 - plugin_dir_url( __DIR__ ) . 'guten/build/index.css',
106 + plugin_dir_url( __DIR__ ) . 'guten/dist/plus-webp.css',
129 107 array( 'wp-components' ),
130 108 '1.0.0',
131 109 );
132 110
@@ -131,28 +109,71 @@
131 109 );
132 110
133 111 wp_enqueue_script(
134 112 'plus-webp',
135 - plugin_dir_url( __DIR__ ) . 'guten/build/index.js',
136 - $asset['dependencies'],
137 - $asset['version'],
113 + plugin_dir_url( __DIR__ ) . 'guten/dist/plus-webp.js',
114 + $asset_file['dependencies'],
115 + $asset_file['version'],
116 + true
117 + );
118 +
119 + $pluswebp_settings = get_option( 'pluswebp' );
120 +
121 + list( $post_ids, $no_file_ids ) = apply_filters( 'pluswebp_get_allimages', $pluswebp_settings['types'] );
122 +
123 + $non_generate_description = null;
124 + if ( ! empty( $no_file_ids ) ) {
125 + /* translators: Count of no file Media */
126 + $non_generate_description = sprintf( __( 'Found %1$d media(id: %2$s), exists in the database, but the file does not, so the conversion is not performed.', 'plus-webp' ), count( $no_file_ids ), implode( ',', $no_file_ids ) );
127 + }
128 +
129 + wp_localize_script(
130 + 'plus-webp',
131 + 'pluswebpgenerate_data',
138 132 array(
139 - 'in_footer' => true,
133 + 'bulk_label' => __( 'Bulk Generate', 'plus-webp' ),
134 + 'description_label' => __( 'To perform "Bulk Generate" after changing the following settings, press "Check" and then press "Generate".', 'plus-webp' ),
135 + 'wp_cli_label' => __( 'WP-CLI commands are available. If you have a large number of files, WP-CLI commands is more reliable. Command line options allow you to specify whether or not email is sent and the source media ID.', 'plus-webp' ),
136 + 'generate_label' => __( 'Generate', 'plus-webp' ),
137 + 'generate_description_label' => __( 'Notified by email with details of the generate results.', 'plus-webp' ),
138 + 'generate_end_label' => __( 'Generation has been completed.', 'plus-webp' ),
139 + 'stop_label' => __( 'Stop', 'plus-webp' ),
140 + 'check_label' => __( 'Check', 'plus-webp' ),
141 + /* translators: Count of Media */
142 + 'generate_description' => sprintf( __( 'Found %1$d media that can be generated.', 'plus-webp' ), count( $post_ids ) ),
143 + 'non_description' => __( 'Cannot find any media that can be generated.', 'plus-webp' ),
144 + 'non_generate_description' => $non_generate_description,
145 + 'title_label' => __( 'Title' ) . ': ',
146 + 'filename_label' => __( 'File name:' ) . ' ',
147 + 'datetime_label' => __( 'Date/Time' ) . ': ',
148 + 'filesize_label' => __( 'File size:' ) . ' ',
149 + 'post_ids' => wp_json_encode( $post_ids, JSON_UNESCAPED_SLASHES ),
140 150 )
141 151 );
142 152
143 - $pluswebp_settings = get_option( 'pluswebp' );
144 -
145 153 wp_localize_script(
146 154 'plus-webp',
147 155 'pluswebpsettings_data',
148 156 array(
149 - 'settings' => $pluswebp_settings,
157 + 'settings_label' => __( 'Settings' ),
158 + 'quality_label' => __( 'Quality', 'plus-webp' ),
159 + 'lowreso_label' => __( 'low resolution', 'plus-webp' ),
160 + 'highreso_label' => __( 'high resolution', 'plus-webp' ),
161 + 'quality_description_label' => __( 'Specifies the quality of WebP. The higher the number, the better the quality and the larger the file size.', 'plus-webp' ),
162 + 'type_label' => __( 'Type' ),
163 + 'type_description_label' => __( 'Check the images you want to convert to WebP.', 'plus-webp' ),
164 + 'append_label' => __( 'Append the webp extension to the original filename', 'plus-webp' ),
165 + 'apply_label' => __( 'Apply' ),
166 + 'append_description_label' => __( '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' ),
167 + 'replace_label' => __( 'WebP replacement of images and contents', 'plus-webp' ),
168 + 'replace_description_label' => __( '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' ),
169 + 'replace_advanced_description_label' => __( 'If you want to replace other databases besides content, use the "plus_webp_advanced_change_db" filter hook.', 'plus-webp' ),
170 + 'replace_advanced_link' => __( 'https://wordpress.org/plugins/plus-webp/', 'plus-webp' ),
171 + 'replace_advanced_text' => __( '"plus_webp_advanced_change_db" filter hook', 'plus-webp' ),
172 + 'settings' => wp_json_encode( $pluswebp_settings, JSON_UNESCAPED_SLASHES ),
150 173 )
151 174 );
152 175
153 - wp_set_script_translations( 'plus-webp', 'plus-webp' );
154 -
155 176 $this->credit_gutenberg( 'plus-webp' );
156 177 }
157 178
158 179 /** ==================================================
@@ -162,18 +183,8 @@
162 183 */
163 184 public function register_rest() {
164 185
165 186 register_rest_route(
166 - 'rf/plus-webp-confirm_api',
167 - '/token',
168 - array(
169 - 'methods' => 'POST',
170 - 'callback' => array( $this, 'confirm_api_save' ),
171 - 'permission_callback' => array( $this, 'rest_permission' ),
172 - ),
173 - );
174 -
175 - register_rest_route(
176 187 'rf/plus-webp-generate_api',
177 188 '/token',
178 189 array(
179 190 'methods' => 'POST',
@@ -203,40 +214,8 @@
203 214 return current_user_can( 'manage_options' );
204 215 }
205 216
206 217 /** ==================================================
207 - * Rest API save for Confirm
208 - *
209 - * @param object $request changed data.
210 - * @since 5.20
211 - */
212 - public function confirm_api_save( $request ) {
213 -
214 - $args = $request->get_json_params();
215 -
216 - $args['post_ids'] = array();
217 - $args['ids_desc'] = null;
218 - $args['non_ids_desc'] = null;
219 - if ( $args['submit'] ) {
220 - $pluswebp_settings = get_option( 'pluswebp' );
221 - list( $post_ids, $no_file_ids ) = apply_filters( 'pluswebp_get_allimages', $pluswebp_settings['types'], $pluswebp_settings['output_mime'] );
222 - $args['post_ids'] = $post_ids;
223 - if ( ! empty( $post_ids ) ) {
224 - /* translators: Count of file Media */
225 - $args['ids_desc'] = sprintf( __( 'Found %1$d media that can be generated.', 'plus-webp' ), count( $post_ids ) );
226 - } else {
227 - $args['ids_desc'] = __( 'Cannot find any media that can be generated.', 'plus-webp' );
228 - }
229 - if ( ! empty( $no_file_ids ) ) {
230 - /* translators: Count of no file Media */
231 - $args['non_ids_desc'] = sprintf( __( 'Found %1$d media(id: %2$s), exists in the database, but the file does not, so the conversion is not performed.', 'plus-webp' ), count( $no_file_ids ), implode( ',', $no_file_ids ) );
232 - }
233 - }
234 -
235 - return new WP_REST_Response( $args, 200 );
236 - }
237 -
238 - /** ==================================================
239 218 * Rest API save for Generate
240 219 *
241 220 * @param object $request changed data.
242 221 * @since 4.00
@@ -242,18 +221,16 @@
242 221 * @since 4.00
243 222 */
244 223 public function generate_api_save( $request ) {
245 224
246 - $args = $request->get_json_params();
225 + $args = json_decode( $request->get_body(), true );
247 226
248 227 $count = absint( $args['count'] );
249 228 $max_count = absint( $args['max_count'] );
250 229 $attach_id = absint( $args['post_id'] );
251 - $messages = array();
252 - $sent_mail = boolval( $args['sent_mail'] );
253 - $stop = boolval( $args['stop'] );
230 + $messages = get_option( 'pluswebp_messages', array() );
254 231
255 - if ( boolval( $args['generate'] ) ) {
232 + if ( $args['generate'] ) {
256 233 $metadata_org = wp_get_attachment_metadata( $attach_id );
257 234
258 235 delete_option( 'pluswebp_generate' );
259 236 do_action( 'wp_generate_attachment_metadata', $metadata_org, $attach_id );
@@ -260,24 +237,17 @@
260 237 $webp_id = get_option( 'pluswebp_generate' );
261 238 delete_option( 'pluswebp_generate' );
262 239
263 240 if ( $webp_id ) {
264 - list( $message, $tmp ) = apply_filters( 'pluswebp_mail_messages', $messages, $webp_id );
265 - update_option( 'pluswebp_messages', true );
266 - $this->write_tmp_messages( $message );
241 + list( $tmp, $messages ) = apply_filters( 'pluswebp_mail_messages', $messages, $webp_id );
242 + update_option( 'pluswebp_messages', $messages );
267 243 }
268 244 if ( $count === $max_count ) {
269 - if ( $sent_mail && get_option( 'pluswebp_messages' ) ) {
270 - $messages = $this->read_tmp_messages();
271 - do_action( 'pluswebp_mail_generate_message', $messages, count( $messages ) );
272 - }
245 + do_action( 'pluswebp_mail_generate_message', $messages, $max_count );
273 246 delete_option( 'pluswebp_messages' );
274 247 }
275 - } else if ( ! boolval( $args['generate'] ) && $stop ) {
276 - if ( $sent_mail && get_option( 'pluswebp_messages' ) ) {
277 - $messages = $this->read_tmp_messages();
278 - do_action( 'pluswebp_mail_generate_message', $messages, count( $messages ) );
279 - }
248 + } else {
249 + do_action( 'pluswebp_mail_generate_message', $messages, count( $messages ) );
280 250 delete_option( 'pluswebp_messages' );
281 251 }
282 252
283 253 return new WP_REST_Response( $args, 200 );
@@ -283,80 +253,8 @@
283 253 return new WP_REST_Response( $args, 200 );
284 254 }
285 255
286 256 /** ==================================================
287 - * Save for Mail messages
288 - *
289 - * @param string $message Message.
290 - * @since 5.21
291 - */
292 - private function write_tmp_messages( $message ) {
293 -
294 - $temp_dir = get_temp_dir();
295 - $file_path = $temp_dir . 'pluswebp_messages.txt';
296 -
297 - global $wp_filesystem;
298 - if ( empty( $wp_filesystem ) ) {
299 - require_once ABSPATH . 'wp-admin/includes/file.php';
300 - WP_Filesystem();
301 - }
302 -
303 - $safe_message = bin2hex( $message ) . "\n";
304 - if ( $wp_filesystem->exists( $file_path ) ) {
305 - $existing_content = $wp_filesystem->get_contents( $file_path );
306 - $wp_filesystem->put_contents( $file_path, $existing_content . $safe_message, FS_CHMOD_FILE );
307 - } else {
308 - $wp_filesystem->put_contents( $file_path, $safe_message, FS_CHMOD_FILE );
309 - }
310 - }
311 -
312 - /** ==================================================
313 - * Read for Mail messages
314 - *
315 - * @return array $messages.
316 - * @since 5.21
317 - */
318 - private function read_tmp_messages() {
319 -
320 - $temp_dir = get_temp_dir();
321 - $file_path = $temp_dir . 'pluswebp_messages.txt';
322 -
323 - $messages = array();
324 -
325 - global $wp_filesystem;
326 - if ( empty( $wp_filesystem ) ) {
327 - require_once ABSPATH . 'wp-admin/includes/file.php';
328 - WP_Filesystem();
329 - }
330 -
331 - if ( ! $wp_filesystem->exists( $file_path ) ) {
332 - return $messages;
333 - }
334 -
335 - $content = $wp_filesystem->get_contents( $file_path );
336 - if ( $content ) {
337 - $lines = explode( "\n", $content );
338 - foreach ( $lines as $line ) {
339 - $line = trim( $line );
340 - if ( '' === $line ) {
341 - continue;
342 - }
343 -
344 - $decoded = hex2bin( $line );
345 - if ( false !== $decoded && '' !== $decoded ) {
346 - $messages[] = $decoded;
347 - }
348 - }
349 - }
350 -
351 - if ( $wp_filesystem->exists( $file_path ) ) {
352 - $wp_filesystem->delete( $file_path );
353 - }
354 -
355 - return $messages;
356 - }
357 -
358 - /** ==================================================
359 257 * Rest API save for Settings
360 258 *
361 259 * @param object $request changed data.
362 260 * @since 4.00
@@ -362,25 +260,14 @@
362 260 * @since 4.00
363 261 */
364 262 public function settings_api_save( $request ) {
365 263
366 - $args = $request->get_json_params();
264 + $args = json_decode( $request->get_body(), true );
367 265
368 - if ( isset( $args['settings'] ) && is_array( $args['settings'] ) ) {
369 - $settings = $args['settings'];
370 - } else {
371 - return new WP_Error(
372 - 'invalid_settings',
373 - 'Invalid settings.',
374 - array( 'status' => 400 )
375 - );
376 - }
377 -
378 266 $options = array();
379 - $options['output_mime'] = sanitize_text_field( wp_unslash( $settings['output_mime'] ) );
380 - $options['quality'] = intval( $settings['quality'] );
267 + $options['quality'] = intval( $args['quality'] );
381 268 $options['types'] = filter_var(
382 - wp_unslash( $settings['types'] ),
269 + wp_unslash( $args['types'] ),
383 270 FILTER_CALLBACK,
384 271 array(
385 272 'options' => function ( $value ) {
386 273 return sanitize_text_field( $value );
@@ -386,10 +273,10 @@
386 273 return sanitize_text_field( $value );
387 274 },
388 275 )
389 276 );
390 - $options['addext'] = boolval( $settings['addext'] );
391 - $options['replace'] = boolval( $settings['replace'] );
277 + $options['addext'] = boolval( $args['addext'] );
278 + $options['replace'] = boolval( $args['replace'] );
392 279
393 280 update_option( 'pluswebp', $options );
394 281
395 282 return new WP_REST_Response( $args, 200 );
@@ -437,9 +324,9 @@
437 324 $handle,
438 325 'credit',
439 326 array(
440 327 'links' => __( 'Various links of this plugin', 'plus-webp' ),
441 - 'plugin_version' => __( 'Version:', 'plus-webp' ) . ' ' . $plugin_ver_num,
328 + 'plugin_version' => __( 'Version:' ) . ' ' . $plugin_ver_num,
442 329 /* translators: FAQ Link & Slug */
443 330 'faq' => sprintf( __( 'https://wordpress.org/plugins/%1$s/faq', 'plus-webp' ), $slug ),
444 331 'support' => 'https://wordpress.org/support/plugin/' . $slug,
445 332 'review' => 'https://wordpress.org/support/view/plugin-reviews/' . $slug,
@@ -444,15 +331,15 @@
444 331 'support' => 'https://wordpress.org/support/plugin/' . $slug,
445 332 'review' => 'https://wordpress.org/support/view/plugin-reviews/' . $slug,
446 333 'translate' => 'https://translate.wordpress.org/projects/wp-plugins/' . $slug,
447 334 /* translators: Plugin translation link */
448 - 'translate_text' => sprintf( __( 'Translations for %s', 'plus-webp' ), $plugin_name ),
335 + 'translate_text' => sprintf( __( 'Translations for %s' ), $plugin_name ),
449 336 'facebook' => 'https://www.facebook.com/katsushikawamori/',
450 337 'twitter' => 'https://twitter.com/dodesyo312',
451 338 'youtube' => 'https://www.youtube.com/channel/UC5zTLeyROkvZm86OgNRcb_w',
452 339 'donate' => __( 'https://shop.riverforest-wp.info/donate/', 'plus-webp' ),
453 340 'donate_text' => __( 'Please make a donation if you like my work or would like to further the development of this plugin.', 'plus-webp' ),
454 - 'donate_button' => __( 'Donate to this plugin &#187;', 'plus-webp' ),
341 + 'donate_button' => __( 'Donate to this plugin &#187;' ),
455 342 )
456 343 );
457 344 }
458 345
@@ -479,20 +366,14 @@
479 366 if ( ! array_key_exists( 'addext', $pluswebp_settings ) ) {
480 367 $pluswebp_settings['addext'] = false;
481 368 update_option( 'pluswebp', $pluswebp_settings );
482 369 }
483 - /* 'mime' from ver 5.00 */
484 - if ( ! array_key_exists( 'output_mime', $pluswebp_settings ) ) {
485 - $pluswebp_settings['output_mime'] = 'image/webp';
486 - update_option( 'pluswebp', $pluswebp_settings );
487 - }
488 370 } else {
489 371 $pswp_tbl = array(
490 - 'output_mime' => 'image/webp',
491 372 'quality' => 80,
492 373 'replace' => false,
493 - 'addext' => false,
494 - 'types' => array(
374 + 'addext' => false,
375 + 'types' => array(
495 376 'image/jpeg',
496 377 'image/png',
497 378 'image/bmp',
498 379 'image/gif',