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

505 lines 14.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plus WebP or AVIF
4 *
5 * @package Plus WebP or AVIF
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 if ( ! defined( 'ABSPATH' ) ) {
23 exit;
24 }
25
26 $pluswebpadmin = new PlusWebpAdmin();
27
28 /** ==================================================
29 * Management screen
30 */
31 class PlusWebpAdmin {
32
33 /** ==================================================
34 * Construct
35 *
36 * @since 1.00
37 */
38 public function __construct() {
39
40 add_action( 'admin_init', array( $this, 'register_settings' ) );
41
42 add_action( 'admin_menu', array( $this, 'plugin_menu' ) );
43 add_filter( 'plugin_action_links', array( $this, 'settings_link' ), 10, 2 );
44
45 add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ), 10, 1 );
46 add_action( 'rest_api_init', array( $this, 'register_rest' ) );
47 }
48
49 /** ==================================================
50 * Add a "Settings" link to the plugins page
51 *
52 * @param array $links links array.
53 * @param string $file file.
54 * @return array $links links array.
55 * @since 1.00
56 */
57 public function settings_link( $links, $file ) {
58 static $this_plugin;
59 if ( empty( $this_plugin ) ) {
60 $this_plugin = 'plus-webp/pluswebp.php';
61 }
62 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>';
64 }
65 return $links;
66 }
67
68 /** ==================================================
69 * Settings page
70 *
71 * @since 1.00
72 */
73 public function plugin_menu() {
74
75 add_media_page(
76 'Plus WebP or AVIF',
77 'Plus WebP or AVIF',
78 'manage_options',
79 'plus-webp',
80 array( $this, 'plus_webp_generate_setteings' )
81 );
82 }
83
84 /** ==================================================
85 * Generate and Settings page
86 *
87 * @since 4.00
88 */
89 public function plus_webp_generate_setteings() {
90
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 ) );
104 }
105
106 /** ==================================================
107 * Load script
108 *
109 * @param string $hook_suffix hook_suffix.
110 * @since 4.00
111 */
112 public function admin_scripts( $hook_suffix ) {
113
114 if ( 'media_page_plus-webp' !== $hook_suffix ) {
115 return;
116 }
117
118 $asset_file = plugin_dir_path( __DIR__ ) . 'guten/build/index.asset.php';
119
120 if ( ! file_exists( $asset_file ) ) {
121 return;
122 }
123
124 $asset = include $asset_file;
125
126 wp_enqueue_style(
127 'plus-webp-style',
128 plugin_dir_url( __DIR__ ) . 'guten/build/index.css',
129 array( 'wp-components' ),
130 '1.0.0',
131 );
132
133 wp_enqueue_script(
134 'plus-webp',
135 plugin_dir_url( __DIR__ ) . 'guten/build/index.js',
136 $asset['dependencies'],
137 $asset['version'],
138 array(
139 'in_footer' => true,
140 )
141 );
142
143 $pluswebp_settings = get_option( 'pluswebp' );
144
145 wp_localize_script(
146 'plus-webp',
147 'pluswebpsettings_data',
148 array(
149 'settings' => $pluswebp_settings,
150 )
151 );
152
153 wp_set_script_translations( 'plus-webp', 'plus-webp' );
154
155 $this->credit_gutenberg( 'plus-webp' );
156 }
157
158 /** ==================================================
159 * Register Rest API
160 *
161 * @since 4.00
162 */
163 public function register_rest() {
164
165 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 'rf/plus-webp-generate_api',
177 '/token',
178 array(
179 'methods' => 'POST',
180 'callback' => array( $this, 'generate_api_save' ),
181 'permission_callback' => array( $this, 'rest_permission' ),
182 ),
183 );
184
185 register_rest_route(
186 'rf/plus-webp-settings_api',
187 '/token',
188 array(
189 'methods' => 'POST',
190 'callback' => array( $this, 'settings_api_save' ),
191 'permission_callback' => array( $this, 'rest_permission' ),
192 ),
193 );
194 }
195
196 /** ==================================================
197 * Rest Permission
198 *
199 * @since 4.00
200 */
201 public function rest_permission() {
202
203 return current_user_can( 'manage_options' );
204 }
205
206 /** ==================================================
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 * Rest API save for Generate
240 *
241 * @param object $request changed data.
242 * @since 4.00
243 */
244 public function generate_api_save( $request ) {
245
246 $args = $request->get_json_params();
247
248 $count = absint( $args['count'] );
249 $max_count = absint( $args['max_count'] );
250 $attach_id = absint( $args['post_id'] );
251 $messages = array();
252 $sent_mail = boolval( $args['sent_mail'] );
253 $stop = boolval( $args['stop'] );
254
255 if ( boolval( $args['generate'] ) ) {
256 $metadata_org = wp_get_attachment_metadata( $attach_id );
257
258 delete_option( 'pluswebp_generate' );
259 do_action( 'wp_generate_attachment_metadata', $metadata_org, $attach_id );
260 $webp_id = get_option( 'pluswebp_generate' );
261 delete_option( 'pluswebp_generate' );
262
263 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 );
267 }
268 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 }
273 delete_option( 'pluswebp_messages' );
274 }
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 }
280 delete_option( 'pluswebp_messages' );
281 }
282
283 return new WP_REST_Response( $args, 200 );
284 }
285
286 /** ==================================================
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 * Rest API save for Settings
360 *
361 * @param object $request changed data.
362 * @since 4.00
363 */
364 public function settings_api_save( $request ) {
365
366 $args = $request->get_json_params();
367
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 $options = array();
379 $options['output_mime'] = sanitize_text_field( wp_unslash( $settings['output_mime'] ) );
380 $options['quality'] = intval( $settings['quality'] );
381 $options['types'] = filter_var(
382 wp_unslash( $settings['types'] ),
383 FILTER_CALLBACK,
384 array(
385 'options' => function ( $value ) {
386 return sanitize_text_field( $value );
387 },
388 )
389 );
390 $options['addext'] = boolval( $settings['addext'] );
391 $options['replace'] = boolval( $settings['replace'] );
392
393 update_option( 'pluswebp', $options );
394
395 return new WP_REST_Response( $args, 200 );
396 }
397
398 /** ==================================================
399 * Credit for Gutenberg
400 *
401 * @param string $handle handle.
402 * @since 4.00
403 */
404 private function credit_gutenberg( $handle ) {
405
406 $plugin_name = null;
407 $plugin_ver_num = null;
408 $plugin_path = plugin_dir_path( __DIR__ );
409 $plugin_dir = untrailingslashit( wp_normalize_path( $plugin_path ) );
410 $slugs = explode( '/', $plugin_dir );
411 $slug = end( $slugs );
412 $files = scandir( $plugin_dir );
413 foreach ( $files as $file ) {
414 if ( '.' === $file || '..' === $file || is_dir( $plugin_path . $file ) ) {
415 continue;
416 } else {
417 $exts = explode( '.', $file );
418 $ext = strtolower( end( $exts ) );
419 if ( 'php' === $ext ) {
420 $plugin_datas = get_file_data(
421 $plugin_path . $file,
422 array(
423 'name' => 'Plugin Name',
424 'version' => 'Version',
425 )
426 );
427 if ( array_key_exists( 'name', $plugin_datas ) && ! empty( $plugin_datas['name'] ) && array_key_exists( 'version', $plugin_datas ) && ! empty( $plugin_datas['version'] ) ) {
428 $plugin_name = $plugin_datas['name'];
429 $plugin_ver_num = $plugin_datas['version'];
430 break;
431 }
432 }
433 }
434 }
435
436 wp_localize_script(
437 $handle,
438 'credit',
439 array(
440 'links' => __( 'Various links of this plugin', 'plus-webp' ),
441 'plugin_version' => __( 'Version:', 'plus-webp' ) . ' ' . $plugin_ver_num,
442 /* translators: FAQ Link & Slug */
443 'faq' => sprintf( __( 'https://wordpress.org/plugins/%1$s/faq', 'plus-webp' ), $slug ),
444 'support' => 'https://wordpress.org/support/plugin/' . $slug,
445 'review' => 'https://wordpress.org/support/view/plugin-reviews/' . $slug,
446 'translate' => 'https://translate.wordpress.org/projects/wp-plugins/' . $slug,
447 /* translators: Plugin translation link */
448 'translate_text' => sprintf( __( 'Translations for %s', 'plus-webp' ), $plugin_name ),
449 'facebook' => 'https://www.facebook.com/katsushikawamori/',
450 'twitter' => 'https://twitter.com/dodesyo312',
451 'youtube' => 'https://www.youtube.com/channel/UC5zTLeyROkvZm86OgNRcb_w',
452 'donate' => __( 'https://shop.riverforest-wp.info/donate/', 'plus-webp' ),
453 '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' ),
455 )
456 );
457 }
458
459 /** ==================================================
460 * Settings register
461 *
462 * @since 1.00
463 */
464 public function register_settings() {
465
466 if ( get_option( 'pluswebp' ) ) {
467 $pluswebp_settings = get_option( 'pluswebp' );
468 /* 'types' from ver 1.08 */
469 if ( ! array_key_exists( 'types', $pluswebp_settings ) ) {
470 $pluswebp_settings['types'] = array(
471 'image/jpeg',
472 'image/png',
473 'image/bmp',
474 'image/gif',
475 );
476 update_option( 'pluswebp', $pluswebp_settings );
477 }
478 /* 'addext' from ver 1.13 */
479 if ( ! array_key_exists( 'addext', $pluswebp_settings ) ) {
480 $pluswebp_settings['addext'] = false;
481 update_option( 'pluswebp', $pluswebp_settings );
482 }
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 } else {
489 $pswp_tbl = array(
490 'output_mime' => 'image/webp',
491 'quality' => 80,
492 'replace' => false,
493 'addext' => false,
494 'types' => array(
495 'image/jpeg',
496 'image/png',
497 'image/bmp',
498 'image/gif',
499 ),
500 );
501 update_option( 'pluswebp', $pswp_tbl );
502 }
503 }
504 }
505