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
plus-webp / lib / class-pluswebpadmin.php

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

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