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

387 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 $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
41 add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ), 10, 1 );
42 add_action( 'rest_api_init', array( $this, 'register_rest' ) );
43 }
44
45 /** ==================================================
46 * Add a "Settings" link to the plugins page
47 *
48 * @param array $links links array.
49 * @param string $file file.
50 * @return array $links links array.
51 * @since 1.00
52 */
53 public function settings_link( $links, $file ) {
54 static $this_plugin;
55 if ( empty( $this_plugin ) ) {
56 $this_plugin = 'plus-webp/pluswebp.php';
57 }
58 if ( $file === $this_plugin ) {
59 $links[] = '<a href="' . admin_url( 'upload.php?page=plus-webp' ) . '">' . esc_html__( 'Settings', 'plus-webp' ) . ' & ' . esc_html__( 'Bulk Generate', 'plus-webp' ) . '</a>';
60 }
61 return $links;
62 }
63
64 /** ==================================================
65 * Settings page
66 *
67 * @since 1.00
68 */
69 public function plugin_menu() {
70
71 add_media_page(
72 'Plus WebP or AVIF',
73 'Plus WebP or AVIF',
74 'manage_options',
75 'plus-webp',
76 array( $this, 'plus_webp_generate_setteings' )
77 );
78 }
79
80 /** ==================================================
81 * Generate and Settings page
82 *
83 * @since 4.00
84 */
85 public function plus_webp_generate_setteings() {
86
87 if ( ! current_user_can( 'manage_options' ) ) {
88 wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'plus-webp' ) );
89 }
90
91 global $wp_version;
92 $requires = '6.6';
93 if ( version_compare( $wp_version, $requires, '>=' ) ) {
94 $admin_screen = esc_html__( 'Loading…', 'plus-webp' );
95 } else {
96 /* translators: WordPress requires version */
97 $admin_screen = sprintf( esc_html__( 'WordPress %s or higher is required to view this screen.', 'plus-webp' ), $requires );
98 }
99 printf( '<div class="wrap" id="plus-webp-page">%s</div>', esc_html( $admin_screen ) );
100 }
101
102 /** ==================================================
103 * Load script
104 *
105 * @param string $hook_suffix hook_suffix.
106 * @since 4.00
107 */
108 public function admin_scripts( $hook_suffix ) {
109
110 if ( 'media_page_plus-webp' !== $hook_suffix ) {
111 return;
112 }
113
114 $asset_file = plugin_dir_path( __DIR__ ) . 'guten/build/index.asset.php';
115
116 if ( ! file_exists( $asset_file ) ) {
117 return;
118 }
119
120 $asset = include $asset_file;
121
122 wp_enqueue_style(
123 'plus-webp-style',
124 plugin_dir_url( __DIR__ ) . 'guten/build/index.css',
125 array( 'wp-components' ),
126 '1.0.0',
127 );
128
129 wp_enqueue_script(
130 'plus-webp',
131 plugin_dir_url( __DIR__ ) . 'guten/build/index.js',
132 $asset['dependencies'],
133 $asset['version'],
134 array(
135 'in_footer' => true,
136 )
137 );
138
139 $pluswebp_settings = get_option( 'pluswebp' );
140
141 list( $post_ids, $no_file_ids ) = apply_filters( 'pluswebp_get_allimages', $pluswebp_settings['types'], $pluswebp_settings['output_mime'] );
142
143 $non_generate_description = null;
144 if ( ! empty( $no_file_ids ) ) {
145 /* translators: Count of no file Media */
146 $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 ) );
147 }
148
149 wp_localize_script(
150 'plus-webp',
151 'pluswebpgenerate_data',
152 array(
153 /* translators: Count of Media */
154 'generate_description' => sprintf( __( 'Found %1$d media that can be generated.', 'plus-webp' ), count( $post_ids ) ),
155 'non_generate_description' => $non_generate_description,
156 'post_ids' => wp_json_encode( $post_ids, JSON_UNESCAPED_SLASHES ),
157 )
158 );
159
160 wp_localize_script(
161 'plus-webp',
162 'pluswebpsettings_data',
163 array(
164 'settings' => wp_json_encode( $pluswebp_settings, JSON_UNESCAPED_SLASHES ),
165 )
166 );
167
168 wp_set_script_translations( 'plus-webp', 'plus-webp' );
169
170 $this->credit_gutenberg( 'plus-webp' );
171 }
172
173 /** ==================================================
174 * Register Rest API
175 *
176 * @since 4.00
177 */
178 public function register_rest() {
179
180 register_rest_route(
181 'rf/plus-webp-generate_api',
182 '/token',
183 array(
184 'methods' => 'POST',
185 'callback' => array( $this, 'generate_api_save' ),
186 'permission_callback' => array( $this, 'rest_permission' ),
187 ),
188 );
189
190 register_rest_route(
191 'rf/plus-webp-settings_api',
192 '/token',
193 array(
194 'methods' => 'POST',
195 'callback' => array( $this, 'settings_api_save' ),
196 'permission_callback' => array( $this, 'rest_permission' ),
197 ),
198 );
199 }
200
201 /** ==================================================
202 * Rest Permission
203 *
204 * @since 4.00
205 */
206 public function rest_permission() {
207
208 return current_user_can( 'manage_options' );
209 }
210
211 /** ==================================================
212 * Rest API save for Generate
213 *
214 * @param object $request changed data.
215 * @since 4.00
216 */
217 public function generate_api_save( $request ) {
218
219 $args = json_decode( $request->get_body(), true );
220
221 $count = absint( $args['count'] );
222 $max_count = absint( $args['max_count'] );
223 $attach_id = absint( $args['post_id'] );
224 $messages = get_option( 'pluswebp_messages', array() );
225
226 if ( $args['generate'] ) {
227 $metadata_org = wp_get_attachment_metadata( $attach_id );
228
229 delete_option( 'pluswebp_generate' );
230 do_action( 'wp_generate_attachment_metadata', $metadata_org, $attach_id );
231 $webp_id = get_option( 'pluswebp_generate' );
232 delete_option( 'pluswebp_generate' );
233
234 if ( $webp_id ) {
235 list( $tmp, $messages ) = apply_filters( 'pluswebp_mail_messages', $messages, $webp_id );
236 update_option( 'pluswebp_messages', $messages );
237 }
238 if ( $count === $max_count ) {
239 do_action( 'pluswebp_mail_generate_message', $messages, $max_count );
240 delete_option( 'pluswebp_messages' );
241 }
242 } else {
243 do_action( 'pluswebp_mail_generate_message', $messages, count( $messages ) );
244 delete_option( 'pluswebp_messages' );
245 }
246
247 return new WP_REST_Response( $args, 200 );
248 }
249
250 /** ==================================================
251 * Rest API save for Settings
252 *
253 * @param object $request changed data.
254 * @since 4.00
255 */
256 public function settings_api_save( $request ) {
257
258 $args = json_decode( $request->get_body(), true );
259
260 $options = array();
261 $options['output_mime'] = sanitize_text_field( wp_unslash( $args['output_mime'] ) );
262 $options['quality'] = intval( $args['quality'] );
263 $options['types'] = filter_var(
264 wp_unslash( $args['types'] ),
265 FILTER_CALLBACK,
266 array(
267 'options' => function ( $value ) {
268 return sanitize_text_field( $value );
269 },
270 )
271 );
272 $options['addext'] = boolval( $args['addext'] );
273 $options['replace'] = boolval( $args['replace'] );
274
275 update_option( 'pluswebp', $options );
276
277 return new WP_REST_Response( $args, 200 );
278 }
279
280 /** ==================================================
281 * Credit for Gutenberg
282 *
283 * @param string $handle handle.
284 * @since 4.00
285 */
286 private function credit_gutenberg( $handle ) {
287
288 $plugin_name = null;
289 $plugin_ver_num = null;
290 $plugin_path = plugin_dir_path( __DIR__ );
291 $plugin_dir = untrailingslashit( wp_normalize_path( $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
318 wp_localize_script(
319 $handle,
320 'credit',
321 array(
322 'links' => __( 'Various links of this plugin', 'plus-webp' ),
323 'plugin_version' => __( 'Version:', 'plus-webp' ) . ' ' . $plugin_ver_num,
324 /* translators: FAQ Link & Slug */
325 'faq' => sprintf( __( 'https://wordpress.org/plugins/%1$s/faq', 'plus-webp' ), $slug ),
326 'support' => 'https://wordpress.org/support/plugin/' . $slug,
327 'review' => 'https://wordpress.org/support/view/plugin-reviews/' . $slug,
328 'translate' => 'https://translate.wordpress.org/projects/wp-plugins/' . $slug,
329 /* translators: Plugin translation link */
330 'translate_text' => sprintf( __( 'Translations for %s', 'plus-webp' ), $plugin_name ),
331 'facebook' => 'https://www.facebook.com/katsushikawamori/',
332 'twitter' => 'https://twitter.com/dodesyo312',
333 'youtube' => 'https://www.youtube.com/channel/UC5zTLeyROkvZm86OgNRcb_w',
334 'donate' => __( 'https://shop.riverforest-wp.info/donate/', 'plus-webp' ),
335 'donate_text' => __( 'Please make a donation if you like my work or would like to further the development of this plugin.', 'plus-webp' ),
336 'donate_button' => __( 'Donate to this plugin &#187;', 'plus-webp' ),
337 )
338 );
339 }
340
341 /** ==================================================
342 * Settings register
343 *
344 * @since 1.00
345 */
346 public function register_settings() {
347
348 if ( get_option( 'pluswebp' ) ) {
349 $pluswebp_settings = get_option( 'pluswebp' );
350 /* 'types' from ver 1.08 */
351 if ( ! array_key_exists( 'types', $pluswebp_settings ) ) {
352 $pluswebp_settings['types'] = array(
353 'image/jpeg',
354 'image/png',
355 'image/bmp',
356 'image/gif',
357 );
358 update_option( 'pluswebp', $pluswebp_settings );
359 }
360 /* 'addext' from ver 1.13 */
361 if ( ! array_key_exists( 'addext', $pluswebp_settings ) ) {
362 $pluswebp_settings['addext'] = false;
363 update_option( 'pluswebp', $pluswebp_settings );
364 }
365 /* 'mime' from ver 5.00 */
366 if ( ! array_key_exists( 'output_mime', $pluswebp_settings ) ) {
367 $pluswebp_settings['output_mime'] = 'image/webp';
368 update_option( 'pluswebp', $pluswebp_settings );
369 }
370 } else {
371 $pswp_tbl = array(
372 'output_mime' => 'image/webp',
373 'quality' => 80,
374 'replace' => false,
375 'addext' => false,
376 'types' => array(
377 'image/jpeg',
378 'image/png',
379 'image/bmp',
380 'image/gif',
381 ),
382 );
383 update_option( 'pluswebp', $pswp_tbl );
384 }
385 }
386 }
387