PluginProbe
Plus WebP or AVIF / 5.11
Plus WebP or AVIF v5.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
plus-webp / lib / class-pluswebpadmin.php

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

391 lines 11.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 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 list( $post_ids, $no_file_ids ) = apply_filters( 'pluswebp_get_allimages', $pluswebp_settings['types'], $pluswebp_settings['output_mime'] );
146
147 $non_generate_description = null;
148 if ( ! empty( $no_file_ids ) ) {
149 /* translators: Count of no file Media */
150 $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 ) );
151 }
152
153 wp_localize_script(
154 'plus-webp',
155 'pluswebpgenerate_data',
156 array(
157 /* translators: Count of Media */
158 'generate_description' => sprintf( __( 'Found %1$d media that can be generated.', 'plus-webp' ), count( $post_ids ) ),
159 'non_generate_description' => $non_generate_description,
160 'post_ids' => wp_json_encode( $post_ids, JSON_UNESCAPED_SLASHES ),
161 )
162 );
163
164 wp_localize_script(
165 'plus-webp',
166 'pluswebpsettings_data',
167 array(
168 'settings' => wp_json_encode( $pluswebp_settings, JSON_UNESCAPED_SLASHES ),
169 )
170 );
171
172 wp_set_script_translations( 'plus-webp', 'plus-webp' );
173
174 $this->credit_gutenberg( 'plus-webp' );
175 }
176
177 /** ==================================================
178 * Register Rest API
179 *
180 * @since 4.00
181 */
182 public function register_rest() {
183
184 register_rest_route(
185 'rf/plus-webp-generate_api',
186 '/token',
187 array(
188 'methods' => 'POST',
189 'callback' => array( $this, 'generate_api_save' ),
190 'permission_callback' => array( $this, 'rest_permission' ),
191 ),
192 );
193
194 register_rest_route(
195 'rf/plus-webp-settings_api',
196 '/token',
197 array(
198 'methods' => 'POST',
199 'callback' => array( $this, 'settings_api_save' ),
200 'permission_callback' => array( $this, 'rest_permission' ),
201 ),
202 );
203 }
204
205 /** ==================================================
206 * Rest Permission
207 *
208 * @since 4.00
209 */
210 public function rest_permission() {
211
212 return current_user_can( 'manage_options' );
213 }
214
215 /** ==================================================
216 * Rest API save for Generate
217 *
218 * @param object $request changed data.
219 * @since 4.00
220 */
221 public function generate_api_save( $request ) {
222
223 $args = json_decode( $request->get_body(), true );
224
225 $count = absint( $args['count'] );
226 $max_count = absint( $args['max_count'] );
227 $attach_id = absint( $args['post_id'] );
228 $messages = get_option( 'pluswebp_messages', array() );
229
230 if ( $args['generate'] ) {
231 $metadata_org = wp_get_attachment_metadata( $attach_id );
232
233 delete_option( 'pluswebp_generate' );
234 do_action( 'wp_generate_attachment_metadata', $metadata_org, $attach_id );
235 $webp_id = get_option( 'pluswebp_generate' );
236 delete_option( 'pluswebp_generate' );
237
238 if ( $webp_id ) {
239 list( $tmp, $messages ) = apply_filters( 'pluswebp_mail_messages', $messages, $webp_id );
240 update_option( 'pluswebp_messages', $messages );
241 }
242 if ( $count === $max_count ) {
243 do_action( 'pluswebp_mail_generate_message', $messages, $max_count );
244 delete_option( 'pluswebp_messages' );
245 }
246 } else {
247 do_action( 'pluswebp_mail_generate_message', $messages, count( $messages ) );
248 delete_option( 'pluswebp_messages' );
249 }
250
251 return new WP_REST_Response( $args, 200 );
252 }
253
254 /** ==================================================
255 * Rest API save for Settings
256 *
257 * @param object $request changed data.
258 * @since 4.00
259 */
260 public function settings_api_save( $request ) {
261
262 $args = json_decode( $request->get_body(), true );
263
264 $options = array();
265 $options['output_mime'] = sanitize_text_field( wp_unslash( $args['output_mime'] ) );
266 $options['quality'] = intval( $args['quality'] );
267 $options['types'] = filter_var(
268 wp_unslash( $args['types'] ),
269 FILTER_CALLBACK,
270 array(
271 'options' => function ( $value ) {
272 return sanitize_text_field( $value );
273 },
274 )
275 );
276 $options['addext'] = boolval( $args['addext'] );
277 $options['replace'] = boolval( $args['replace'] );
278
279 update_option( 'pluswebp', $options );
280
281 return new WP_REST_Response( $args, 200 );
282 }
283
284 /** ==================================================
285 * Credit for Gutenberg
286 *
287 * @param string $handle handle.
288 * @since 4.00
289 */
290 private function credit_gutenberg( $handle ) {
291
292 $plugin_name = null;
293 $plugin_ver_num = null;
294 $plugin_path = plugin_dir_path( __DIR__ );
295 $plugin_dir = untrailingslashit( wp_normalize_path( $plugin_path ) );
296 $slugs = explode( '/', $plugin_dir );
297 $slug = end( $slugs );
298 $files = scandir( $plugin_dir );
299 foreach ( $files as $file ) {
300 if ( '.' === $file || '..' === $file || is_dir( $plugin_path . $file ) ) {
301 continue;
302 } else {
303 $exts = explode( '.', $file );
304 $ext = strtolower( end( $exts ) );
305 if ( 'php' === $ext ) {
306 $plugin_datas = get_file_data(
307 $plugin_path . $file,
308 array(
309 'name' => 'Plugin Name',
310 'version' => 'Version',
311 )
312 );
313 if ( array_key_exists( 'name', $plugin_datas ) && ! empty( $plugin_datas['name'] ) && array_key_exists( 'version', $plugin_datas ) && ! empty( $plugin_datas['version'] ) ) {
314 $plugin_name = $plugin_datas['name'];
315 $plugin_ver_num = $plugin_datas['version'];
316 break;
317 }
318 }
319 }
320 }
321
322 wp_localize_script(
323 $handle,
324 'credit',
325 array(
326 'links' => __( 'Various links of this plugin', 'plus-webp' ),
327 'plugin_version' => __( 'Version:', 'plus-webp' ) . ' ' . $plugin_ver_num,
328 /* translators: FAQ Link & Slug */
329 'faq' => sprintf( __( 'https://wordpress.org/plugins/%1$s/faq', 'plus-webp' ), $slug ),
330 'support' => 'https://wordpress.org/support/plugin/' . $slug,
331 'review' => 'https://wordpress.org/support/view/plugin-reviews/' . $slug,
332 'translate' => 'https://translate.wordpress.org/projects/wp-plugins/' . $slug,
333 /* translators: Plugin translation link */
334 'translate_text' => sprintf( __( 'Translations for %s', 'plus-webp' ), $plugin_name ),
335 'facebook' => 'https://www.facebook.com/katsushikawamori/',
336 'twitter' => 'https://twitter.com/dodesyo312',
337 'youtube' => 'https://www.youtube.com/channel/UC5zTLeyROkvZm86OgNRcb_w',
338 'donate' => __( 'https://shop.riverforest-wp.info/donate/', 'plus-webp' ),
339 'donate_text' => __( 'Please make a donation if you like my work or would like to further the development of this plugin.', 'plus-webp' ),
340 'donate_button' => __( 'Donate to this plugin &#187;', 'plus-webp' ),
341 )
342 );
343 }
344
345 /** ==================================================
346 * Settings register
347 *
348 * @since 1.00
349 */
350 public function register_settings() {
351
352 if ( get_option( 'pluswebp' ) ) {
353 $pluswebp_settings = get_option( 'pluswebp' );
354 /* 'types' from ver 1.08 */
355 if ( ! array_key_exists( 'types', $pluswebp_settings ) ) {
356 $pluswebp_settings['types'] = array(
357 'image/jpeg',
358 'image/png',
359 'image/bmp',
360 'image/gif',
361 );
362 update_option( 'pluswebp', $pluswebp_settings );
363 }
364 /* 'addext' from ver 1.13 */
365 if ( ! array_key_exists( 'addext', $pluswebp_settings ) ) {
366 $pluswebp_settings['addext'] = false;
367 update_option( 'pluswebp', $pluswebp_settings );
368 }
369 /* 'mime' from ver 5.00 */
370 if ( ! array_key_exists( 'output_mime', $pluswebp_settings ) ) {
371 $pluswebp_settings['output_mime'] = 'image/webp';
372 update_option( 'pluswebp', $pluswebp_settings );
373 }
374 } else {
375 $pswp_tbl = array(
376 'output_mime' => 'image/webp',
377 'quality' => 80,
378 'replace' => false,
379 'addext' => false,
380 'types' => array(
381 'image/jpeg',
382 'image/png',
383 'image/bmp',
384 'image/gif',
385 ),
386 );
387 update_option( 'pluswebp', $pswp_tbl );
388 }
389 }
390 }
391