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

443 lines 16.1 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_menu', array( $this, 'plugin_menu' ) );
37 add_action( 'admin_enqueue_scripts', array( $this, 'load_custom_wp_admin_style' ) );
38 add_filter( 'plugin_action_links', array( $this, 'settings_link' ), 10, 2 );
39 add_action( 'admin_notices', array( $this, 'generate_notice' ) );
40
41 add_action( 'AllImagesRegenerateHook', array( $this, 'allimages_regenerate' ), 10, 1 );
42
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( 'tools.php?page=pluswebp' ) . '">' . 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 add_management_page( 'Plus WebP', 'Plus WebP', 'manage_options', 'pluswebp', array( $this, 'plugin_options' ) );
71 }
72
73 /** ==================================================
74 * Add Css and Script
75 *
76 * @since 1.00
77 */
78 public function load_custom_wp_admin_style() {
79 if ( $this->is_my_plugin_screen() ) {
80 wp_enqueue_style( 'jquery-responsiveTabs', plugin_dir_url( __DIR__ ) . 'css/responsive-tabs.css', array(), '1.4.0' );
81 wp_enqueue_style( 'jquery-responsiveTabs-style', plugin_dir_url( __DIR__ ) . 'css/style.css', array(), '1.4.0' );
82 wp_enqueue_script( 'jquery' );
83 wp_enqueue_script( 'jquery-responsiveTabs', plugin_dir_url( __DIR__ ) . 'js/jquery.responsiveTabs.min.js', array(), '1.4.0', false );
84 wp_enqueue_script( 'pluswebp-admin-js', plugin_dir_url( __DIR__ ) . 'js/jquery.pluswebp.admin.js', array( 'jquery' ), array(), '1.00', false );
85 }
86 }
87
88 /** ==================================================
89 * For only admin style
90 *
91 * @since 1.00
92 */
93 private function is_my_plugin_screen() {
94 $screen = get_current_screen();
95 if ( is_object( $screen ) && 'tools_page_pluswebp' === $screen->id ) {
96 return true;
97 } else {
98 return false;
99 }
100 }
101
102 /** ==================================================
103 * Settings page
104 *
105 * @since 1.00
106 */
107 public function plugin_options() {
108
109 if ( ! current_user_can( 'manage_options' ) ) {
110 wp_die( esc_html__( 'You do not have sufficient permissions to access this page.' ) );
111 }
112
113 $pluswebp_settings = get_option( 'pluswebp' );
114
115 if ( isset( $_POST['Generatewebp'] ) && ! empty( $_POST['Generatewebp'] ) ) {
116 if ( check_admin_referer( 'pwp_gnt', 'pluswebp_gnt' ) ) {
117 if ( ! empty( $pluswebp_settings['types'] ) ) {
118 $user = wp_get_current_user();
119 $to = $user->user_email;
120 $userid = $user->ID;
121 if ( ! wp_next_scheduled( 'AllImagesRegenerateHook', array( $to ) ) ) {
122 wp_schedule_single_event( time(), 'AllImagesRegenerateHook', array( $to ) );
123 echo '<div class="notice notice-success is-dismissible"><ul><li>' . esc_html__( 'Background generation starts.', 'plus-webp' ) . '</li></ul></div>';
124 } else {
125 echo '<div class="notice notice-error is-dismissible"><ul><li>' . esc_html__( 'Currently, there is a process that is being bulk generated.', 'plus-webp' ) . '</li></ul></div>';
126 }
127 } else {
128 echo '<div class="notice notice-error is-dismissible"><ul><li>' . esc_html__( 'The image type for creating WebP is not specified.', 'plus-webp' ) . '</li></ul></div>';
129 }
130 }
131 }
132
133 $this->options_updated();
134
135 $scriptname = admin_url( 'tools.php?page=pluswebp' );
136
137 ?>
138 <div class="wrap">
139 <h2>Plus WebP</h2>
140
141 <div id="pluswebp-admin-tabs">
142 <ul>
143 <li><a href="#pluswebp-admin-tabs-1"><?php esc_html_e( 'Bulk Generate', 'plus-webp' ); ?></a></li>
144 <li><a href="#pluswebp-admin-tabs-2"><?php esc_html_e( 'Settings' ); ?></a></li>
145 <li><a href="#pluswebp-admin-tabs-3"><?php esc_html_e( 'Donate to this plugin &#187;' ); ?></a></li>
146 </ul>
147 <div id="pluswebp-admin-tabs-1">
148 <div class="wrap">
149 <h2><?php esc_html_e( 'Bulk Generate', 'plus-webp' ); ?></h2>
150
151 <div style="margin: 5px; padding: 5px;">
152 <p class="description">
153 <?php esc_html_e( 'Press the following button to start WebP generation in the background. When finished, you will receive an email.', 'plus-webp' ); ?>
154 </p>
155 </div>
156
157 <form method="post" action="<?php echo esc_url( $scriptname ); ?>">
158 <?php wp_nonce_field( 'pwp_gnt', 'pluswebp_gnt' ); ?>
159 <?php submit_button( __( 'Generate', 'plus-webp' ), 'primary', 'Generatewebp', true ); ?>
160 </form>
161 </div>
162 </div>
163
164 <div id="pluswebp-admin-tabs-2">
165 <div class="wrap">
166 <h2><?php esc_html_e( 'Settings' ); ?></h2>
167
168 <form method="post" action="<?php echo esc_url( $scriptname ) . '#pluswebp-admin-tabs-2'; ?>">
169 <?php wp_nonce_field( 'pwp_set', 'pluswebp_set' ); ?>
170
171 <div style="width: 100%; height: 100%; margin: 5px; padding: 5px; border: #CCC 2px solid;">
172 <h3><?php esc_html_e( 'Quality', 'plus-webp' ); ?></h3>
173 <div style="margin: 5px; padding: 5px;">
174 <?php esc_html_e( 'low resolution', 'plus-webp' ); ?>
175 <input type="range" style="vertical-align:middle;" name="quality" value="<?php echo esc_attr( $pluswebp_settings['quality'] ); ?>" min="0" max="100" step="1">
176 <?php esc_html_e( 'high resolution', 'plus-webp' ); ?>
177 <div style="margin: 5px; padding: 5px;">
178 <p class="description">
179 <?php esc_html_e( 'Specifies the quality of WebP. The higher the resolution, the larger the file size.', 'plus-webp' ); ?>
180 </p>
181 </div>
182 </div>
183 </div>
184
185 <div style="width: 100%; height: 100%; margin: 5px; padding: 5px; border: #CCC 2px solid;">
186 <h3><?php esc_html_e( 'Type' ); ?></h3>
187 <?php
188 $list_types = array(
189 'image/jpeg',
190 'image/png',
191 'image/bmp',
192 'image/gif',
193 );
194 foreach ( $list_types as $value ) {
195 if ( in_array( $value, $pluswebp_settings['types'] ) ) {
196 $check = 1;
197 } else {
198 $check = 0;
199 }
200 ?>
201 <div style="display: block;padding:5px 5px"><input type="checkbox" name="list_types[<?php echo esc_attr( $value ); ?>]" value="1" <?php checked( '1', $check ); ?> /><?php echo esc_html( $value ); ?></div>
202 <?php
203 }
204 ?>
205 <div style="margin: 5px; padding: 5px;">
206 <p class="description">
207 <?php esc_html_e( 'Check the images you want to convert to WebP.', 'plus-webp' ); ?>
208 </p>
209 </div>
210 </div>
211
212 <div style="width: 100%; height: 100%; margin: 5px; padding: 5px; border: #CCC 2px solid;">
213 <h3><?php esc_html_e( 'WebP replacement of images and contents', 'plus-webp' ); ?></h3>
214 <div style="margin: 5px; padding: 5px;">
215 <input type="checkbox" name="replace" value="1" <?php checked( '1', $pluswebp_settings['replace'] ); ?>><?php esc_html_e( 'Apply' ); ?>
216 <div style="margin: 5px; padding: 5px;">
217 <p class="description">
218 <?php esc_html_e( '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' ); ?>
219 </p>
220 </div>
221 </div>
222 </div>
223
224 <?php submit_button( __( 'Save Changes' ), 'large', 'Manageset', false ); ?>
225 </form>
226 </div>
227 </div>
228
229 <div id="pluswebp-admin-tabs-3">
230 <div class="wrap">
231 <?php $this->credit(); ?>
232 </div>
233 </div>
234
235 </div>
236
237 </div>
238 <?php
239 }
240
241 /** ==================================================
242 * Credit
243 *
244 * @since 1.00
245 */
246 private function credit() {
247
248 $plugin_name = null;
249 $plugin_ver_num = null;
250 $plugin_path = plugin_dir_path( __DIR__ );
251 $plugin_dir = untrailingslashit( $plugin_path );
252 $slugs = explode( '/', $plugin_dir );
253 $slug = end( $slugs );
254 $files = scandir( $plugin_dir );
255 foreach ( $files as $file ) {
256 if ( '.' === $file || '..' === $file || is_dir( $plugin_path . $file ) ) {
257 continue;
258 } else {
259 $exts = explode( '.', $file );
260 $ext = strtolower( end( $exts ) );
261 if ( 'php' === $ext ) {
262 $plugin_datas = get_file_data(
263 $plugin_path . $file,
264 array(
265 'name' => 'Plugin Name',
266 'version' => 'Version',
267 )
268 );
269 if ( array_key_exists( 'name', $plugin_datas ) && ! empty( $plugin_datas['name'] ) && array_key_exists( 'version', $plugin_datas ) && ! empty( $plugin_datas['version'] ) ) {
270 $plugin_name = $plugin_datas['name'];
271 $plugin_ver_num = $plugin_datas['version'];
272 break;
273 }
274 }
275 }
276 }
277 $plugin_version = __( 'Version:' ) . ' ' . $plugin_ver_num;
278 /* translators: FAQ Link & Slug */
279 $faq = sprintf( esc_html__( 'https://wordpress.org/plugins/%s/faq', '%s' ), $slug );
280 $support = 'https://wordpress.org/support/plugin/' . $slug;
281 $review = 'https://wordpress.org/support/view/plugin-reviews/' . $slug;
282 $translate = 'https://translate.wordpress.org/projects/wp-plugins/' . $slug;
283 $facebook = 'https://www.facebook.com/katsushikawamori/';
284 $twitter = 'https://twitter.com/dodesyo312';
285 $youtube = 'https://www.youtube.com/channel/UC5zTLeyROkvZm86OgNRcb_w';
286 $donate = sprintf( esc_html__( 'https://shop.riverforest-wp.info/donate/', '%s' ), $slug );
287
288 ?>
289 <span style="font-weight: bold;">
290 <div>
291 <?php echo esc_html( $plugin_version ); ?> |
292 <a style="text-decoration: none;" href="<?php echo esc_url( $faq ); ?>" target="_blank"><?php esc_html_e( 'FAQ' ); ?></a> | <a style="text-decoration: none;" href="<?php echo esc_url( $support ); ?>" target="_blank"><?php esc_html_e( 'Support Forums' ); ?></a> | <a style="text-decoration: none;" href="<?php echo esc_url( $review ); ?>" target="_blank"><?php sprintf( esc_html_e( 'Reviews', '%s' ), $slug ); ?></a>
293 </div>
294 <div>
295 <a style="text-decoration: none;" href="<?php echo esc_url( $translate ); ?>" target="_blank">
296 <?php
297 /* translators: Plugin translation link */
298 echo sprintf( esc_html__( 'Translations for %s' ), esc_html( $plugin_name ) );
299 ?>
300 </a> | <a style="text-decoration: none;" href="<?php echo esc_url( $facebook ); ?>" target="_blank"><span class="dashicons dashicons-facebook"></span></a> | <a style="text-decoration: none;" href="<?php echo esc_url( $twitter ); ?>" target="_blank"><span class="dashicons dashicons-twitter"></span></a> | <a style="text-decoration: none;" href="<?php echo esc_url( $youtube ); ?>" target="_blank"><span class="dashicons dashicons-video-alt3"></span></a>
301 </div>
302 </span>
303
304 <div style="width: 250px; height: 180px; margin: 5px; padding: 5px; border: #CCC 2px solid;">
305 <h3><?php sprintf( esc_html_e( 'Please make a donation if you like my work or would like to further the development of this plugin.', '%s' ), $slug ); ?></h3>
306 <div style="text-align: right; margin: 5px; padding: 5px;"><span style="padding: 3px; color: #ffffff; background-color: #008000">Plugin Author</span> <span style="font-weight: bold;">Katsushi Kawamori</span></div>
307 <button type="button" style="margin: 5px; padding: 5px;" onclick="window.open('<?php echo esc_url( $donate ); ?>')"><?php esc_html_e( 'Donate to this plugin &#187;' ); ?></button>
308 </div>
309
310 <?php
311
312 }
313
314 /** ==================================================
315 * Regenerate All Images
316 *
317 * @param string $to mail address.
318 * @since 1.00
319 */
320 public function allimages_regenerate( $to ) {
321
322 $pluswebp_settings = get_option( 'pluswebp' );
323
324 global $wpdb;
325
326 $mime_types = null;
327 foreach ( $pluswebp_settings['types'] as $value ) {
328 $mime_types .= "'" . $value . "'" . ',';
329 }
330 $mime_types = rtrim( $mime_types, ',' );
331
332 $wpdb->plus_webp_mime_type = 'AND post_mime_type IN (' . $mime_types . ')';
333
334 $post_ids = $wpdb->get_col(
335 "
336 SELECT ID
337 FROM $wpdb->posts
338 WHERE post_type = 'attachment'
339 $wpdb->plus_webp_mime_type
340 AND post_status = 'inherit'
341 "
342 );
343
344 if ( ! empty( $post_ids ) ) {
345 delete_option( 'pluswebp_generate' );
346 include_once ABSPATH . 'wp-admin/includes/image.php';
347 /* Generate WebP */
348 foreach ( $post_ids as $attach_id ) {
349 $metadata = wp_get_attachment_metadata( $attach_id );
350 do_action( 'wp_generate_attachment_metadata', $metadata, $attach_id );
351 }
352 /* Mail send */
353 do_action( 'pluswebp_mail_send', $to );
354 } else {
355 ?>
356 <div class="notice notice-error is-dismissible"><ul><li><?php esc_html_e( 'There are no images to convert. Generation stopped.', 'plus-webp' ); ?></li></ul></div>';
357 <?php
358 }
359
360 wp_clear_scheduled_hook( 'AllImagesRegenerateHook' );
361
362 }
363
364 /** ==================================================
365 * Update wp_options table.
366 *
367 * @since 1.02
368 */
369 private function options_updated() {
370
371 if ( isset( $_POST['Manageset'] ) && ! empty( $_POST['Manageset'] ) ) {
372 if ( check_admin_referer( 'pwp_set', 'pluswebp_set' ) ) {
373 $pluswebp_settings = get_option( 'pluswebp' );
374 $list_types = array();
375 if ( isset( $_POST['list_types'] ) && ! empty( $_POST['list_types'] ) ) {
376 $tmps = filter_var(
377 wp_unslash( $_POST['list_types'] ),
378 FILTER_CALLBACK,
379 [
380 'options' => function( $value ) {
381 return sanitize_text_field( $value );
382 },
383 ]
384 );
385 foreach ( $tmps as $key => $value ) {
386 $list_types[] = $key;
387 }
388 $pluswebp_settings['types'] = $list_types;
389 } else {
390 $pluswebp_settings['types'] = $list_types;
391 }
392 if ( isset( $_POST['replace'] ) && ! empty( $_POST['replace'] ) ) {
393 $pluswebp_settings['replace'] = true;
394 } else {
395 $pluswebp_settings['replace'] = false;
396 }
397 if ( isset( $_POST['quality'] ) ) {
398 $pluswebp_settings['quality'] = intval( $_POST['quality'] );
399 }
400 update_option( 'pluswebp', $pluswebp_settings );
401 echo '<div class="notice notice-success is-dismissible"><ul><li>' . esc_html__( 'Settings' ) . ' --> ' . esc_html__( 'Settings saved.' ) . '</li></ul></div>';
402 }
403 }
404
405 }
406
407 /** ==================================================
408 * Generate notice
409 *
410 * @since 1.09
411 */
412 public function generate_notice() {
413
414 if ( get_option( 'pluswebp_generate_mail' ) ) {
415 $pluswebp_mail_send = get_option( 'pluswebp_generate_mail' );
416 if ( 0 < $pluswebp_mail_send['count'] ) {
417 ?>
418 <div class="notice notice-success is-dismissible"><ul><li><strong>Plus WebP</strong>
419 <?php
420 /* translators: %1$s Date Time, %2$d File Count */
421 echo wp_kses_post( sprintf( __( ' : %1$s : %2$d WebP files and their thumbnails have been generated. Details have been sent by e-mail.', 'plus-webp' ), $pluswebp_mail_send['datetime'], $pluswebp_mail_send['count'] ) );
422 ?>
423 </li></ul></div>
424 <?php
425 } else {
426 ?>
427 <div class="notice notice-error is-dismissible"><ul><li><strong>Plus WebP</strong>
428 <?php
429 /* translators: %1$s Date Time %2$s error message */
430 echo wp_kses_post( sprintf( __( ' : %1$s : %2$s', 'plus-webp' ), $pluswebp_mail_send['datetime'], __( 'WebP was not generated. A file with the same name already exists.', 'plus-webp' ) ) );
431 ?>
432 </li></ul></div>
433 <?php
434 }
435 delete_option( 'pluswebp_generate_mail' );
436 }
437
438 }
439
440 }
441
442
443