PluginProbe
Plus WebP or AVIF / 2.06
Plus WebP or AVIF v2.06
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
← All changes | lib/class-pluswebpadmin.php +381 -324 5.212.06 View file →
@@ -1,9 +1,9 @@
1 1 <?php
2 2 /**
3 - * Plus WebP or AVIF
3 + * Plus WebP
4 4 *
5 - * @package Plus WebP or AVIF
5 + * @package Plus WebP
6 6 * @subpackage PlusWebpAdmin Management screen
7 7 Copyright (c) 2019- Katsushi Kawamori (email : dodesyoswift312@gmail.com)
8 8 This program is free software; you can redistribute it and/or modify
9 9 it under the terms of the GNU General Public License as published by
@@ -18,12 +18,8 @@
18 18 along with this program; if not, write to the Free Software
19 19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 20 */
21 21
22 -if ( ! defined( 'ABSPATH' ) ) {
23 - exit;
24 -}
25 -
26 22 $pluswebpadmin = new PlusWebpAdmin();
27 23
28 24 /** ==================================================
29 25 * Management screen
@@ -40,11 +36,12 @@
40 36 add_action( 'admin_init', array( $this, 'register_settings' ) );
41 37
42 38 add_action( 'admin_menu', array( $this, 'plugin_menu' ) );
43 39 add_filter( 'plugin_action_links', array( $this, 'settings_link' ), 10, 2 );
40 + add_action( 'admin_notices', array( $this, 'generate_notice' ) );
44 41
45 - add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ), 10, 1 );
46 - add_action( 'rest_api_init', array( $this, 'register_rest' ) );
42 + add_action( 'AllImagesRegenerateHook', array( $this, 'allimages_regenerate' ), 10, 1 );
43 +
47 44 }
48 45
49 46 /** ==================================================
50 47 * Add a "Settings" link to the plugins page
@@ -59,9 +56,9 @@
59 56 if ( empty( $this_plugin ) ) {
60 57 $this_plugin = 'plus-webp/pluswebp.php';
61 58 }
62 59 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>';
60 + $links[] = '<a href="' . admin_url( 'tools.php?page=pluswebp' ) . '">' . esc_html__( 'Settings' ) . ' & ' . esc_html__( 'Bulk Generate', 'plus-webp' ) . '</a>';
64 61 }
65 62 return $links;
66 63 }
67 64
@@ -70,391 +67,420 @@
70 67 *
71 68 * @since 1.00
72 69 */
73 70 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 - );
71 + add_management_page( 'Plus WebP', 'Plus WebP', 'manage_options', 'pluswebp', array( $this, 'plugin_options' ) );
82 72 }
83 73
84 74 /** ==================================================
85 - * Generate and Settings page
75 + * Settings page
86 76 *
87 - * @since 4.00
77 + * @since 1.00
88 78 */
89 - public function plus_webp_generate_setteings() {
79 + public function plugin_options() {
90 80
91 81 if ( ! current_user_can( 'manage_options' ) ) {
92 - wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'plus-webp' ) );
82 + wp_die( esc_html__( 'You do not have sufficient permissions to access this page.' ) );
93 83 }
94 84
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 );
85 + $this->options_updated();
86 + $pluswebp_settings = get_option( 'pluswebp' );
87 +
88 + if ( isset( $_POST['Generatewebp'] ) && ! empty( $_POST['Generatewebp'] ) ) {
89 + if ( check_admin_referer( 'pwp_gnt', 'pluswebp_gnt' ) ) {
90 + if ( ! empty( $pluswebp_settings['types'] ) ) {
91 + $user = wp_get_current_user();
92 + $to = $user->user_email;
93 + $userid = $user->ID;
94 + if ( ! wp_next_scheduled( 'AllImagesRegenerateHook', array( $to ) ) ) {
95 + wp_schedule_single_event( time(), 'AllImagesRegenerateHook', array( $to ) );
96 + echo '<div class="notice notice-success is-dismissible"><ul><li>' . esc_html__( 'Background generation starts.', 'plus-webp' ) . '</li></ul></div>';
97 + } else {
98 + 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>';
99 + }
100 + } else {
101 + 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>';
102 + }
103 + }
102 104 }
103 - printf( '<div class="wrap" id="plus-webp-page">%s</div>', esc_html( $admin_screen ) );
104 - }
105 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 ) {
106 + $scriptname = admin_url( 'tools.php?page=pluswebp' );
113 107
114 - if ( 'media_page_plus-webp' !== $hook_suffix ) {
115 - return;
116 - }
108 + ?>
109 + <div class="wrap">
110 + <h2>Plus WebP</h2>
117 111
118 - $asset_file = plugin_dir_path( __DIR__ ) . 'guten/build/index.asset.php';
112 + <div id="bulkgeneratewebp-loading-container">
119 113
120 - if ( ! file_exists( $asset_file ) ) {
121 - return;
122 - }
114 + <details>
115 + <summary><strong><?php esc_html_e( 'Various links of this plugin', 'plus-webp' ); ?></strong></summary>
116 + <?php $this->credit(); ?>
117 + </details>
123 118
124 - $asset = include $asset_file;
119 + <div class="wrap">
120 + <h2><?php esc_html_e( 'Bulk Generate', 'plus-webp' ); ?></h2>
121 + <details style="margin-bottom: 5px;">
122 + <summary style="cursor: pointer; padding: 10px; border: 1px solid #ddd; background: #f4f4f4; color: #000;"><strong><?php esc_html_e( 'Generate', 'plus-webp' ); ?></strong></summary>
123 + <?php
124 + $post_ids = $this->get_allimages();
125 + if ( ! empty( $post_ids ) ) {
126 + ?>
127 + <div style="margin: 5px; padding: 5px;">
128 + <p class="description">
129 + <?php esc_html_e( 'Press the following button to start WebP generation with Ajax.', 'plus-webp' ); ?>
130 + </p>
131 + </div>
132 + <?php
133 + /* for Ajax */
134 + $handle = 'bulkgeneratewebp-js';
135 + $action1 = 'bulkgeneratewebp-ajax-action';
136 + $action2 = 'bulkgeneratewebp_message';
137 + wp_enqueue_script( 'jquery' );
138 + wp_enqueue_script( $handle, plugin_dir_url( __DIR__ ) . 'js/jquery.bulkgeneratewebp.js', array( 'jquery' ), '1.00', false );
125 139
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 - );
140 + wp_localize_script(
141 + $handle,
142 + 'bulkgeneratewebp',
143 + array(
144 + 'ajax_url' => admin_url( 'admin-ajax.php' ),
145 + 'action' => $action1,
146 + 'nonce' => wp_create_nonce( $action1 ),
147 + )
148 + );
149 + wp_localize_script(
150 + $handle,
151 + 'bulkgeneratewebp_mes',
152 + array(
153 + 'ajax_url' => admin_url( 'admin-ajax.php' ),
154 + 'action' => $action2,
155 + 'nonce' => wp_create_nonce( $action2 ),
156 + )
157 + );
158 + wp_localize_script(
159 + $handle,
160 + 'bulkgeneratewebp_data',
161 + array(
162 + 'count' => count( $post_ids ),
163 + 'id' => json_encode( $post_ids, JSON_UNESCAPED_SLASHES ),
164 + )
165 + );
132 166
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 - );
167 + wp_localize_script(
168 + $handle,
169 + 'bulkgeneratewebp_text',
170 + array(
171 + 'stop_button' => __( 'Stop', 'plus-webp' ),
172 + 'stop_message' => __( 'Stopping now..', 'plus-webp' ),
173 + )
174 + );
175 + submit_button( __( 'Generate', 'plus-webp' ), 'primary', 'bulkgeneratewebp_ajax_update', false );
176 + } else {
177 + ?>
178 + <div style="margin: 5px; padding: 5px;">
179 + <p class="description">
180 + <?php esc_html_e( 'There is no media in the media library that can generate WebP.', 'plus-webp' ); ?>
181 + </p>
182 + </div>
183 + <?php
184 + }
185 + ?>
186 + </details>
187 + <details style="margin-bottom: 5px;">
188 + <summary style="cursor: pointer; padding: 10px; border: 1px solid #ddd; background: #f4f4f4; color: #000;"><strong><?php esc_html_e( 'Generate Background', 'plus-webp' ); ?></strong></summary>
189 + <?php
190 + if ( ! empty( $post_ids ) ) {
191 + ?>
192 + <div style="margin: 5px; padding: 5px;">
193 + <p class="description">
194 + <?php esc_html_e( 'Press the following button to start WebP generation in the background. When finished, you will receive an email.', 'plus-webp' ); ?>
195 + </p>
196 + </div>
197 + <form method="post" action="<?php echo esc_url( $scriptname ); ?>">
198 + <?php wp_nonce_field( 'pwp_gnt', 'pluswebp_gnt' ); ?>
199 + <?php submit_button( __( 'Generate Background', 'plus-webp' ), 'primary', 'Generatewebp', false ); ?>
200 + </form>
201 + <?php
202 + } else {
203 + ?>
204 + <div style="margin: 5px; padding: 5px;">
205 + <p class="description">
206 + <?php esc_html_e( 'There is no media in the media library that can generate WebP.', 'plus-webp' ); ?>
207 + </p>
208 + </div>
209 + <?php
210 + }
211 + ?>
212 + </details>
213 + </div>
142 214
143 - $pluswebp_settings = get_option( 'pluswebp' );
215 + <div class="wrap">
216 + <h2><?php esc_html_e( 'Settings' ); ?></h2>
144 217
145 - wp_localize_script(
146 - 'plus-webp',
147 - 'pluswebpsettings_data',
148 - array(
149 - 'settings' => $pluswebp_settings,
150 - )
151 - );
218 + <form method="post" action="<?php echo esc_url( $scriptname ); ?>">
219 + <?php wp_nonce_field( 'pwp_set', 'pluswebp_set' ); ?>
152 220
153 - wp_set_script_translations( 'plus-webp', 'plus-webp' );
221 + <details style="margin-bottom: 5px;">
222 + <summary style="cursor: pointer; padding: 10px; border: 1px solid #ddd; background: #f4f4f4; color: #000;"><strong><?php esc_html_e( 'Quality', 'plus-webp' ); ?></strong></summary>
223 + <div style="margin: 5px; padding: 5px;">
224 + <?php esc_html_e( 'low resolution', 'plus-webp' ); ?>
225 + <input type="range" style="vertical-align:middle;" name="quality" value="<?php echo esc_attr( $pluswebp_settings['quality'] ); ?>" min="0" max="100" step="1">
226 + <?php esc_html_e( 'high resolution', 'plus-webp' ); ?>
227 + <div style="margin: 5px; padding: 5px;">
228 + <p class="description">
229 + <?php esc_html_e( 'Specifies the quality of WebP. The higher the resolution, the larger the file size.', 'plus-webp' ); ?>
230 + </p>
231 + </div>
232 + </div>
233 + </details>
154 234
155 - $this->credit_gutenberg( 'plus-webp' );
156 - }
235 + <details style="margin-bottom: 5px;">
236 + <summary style="cursor: pointer; padding: 10px; border: 1px solid #ddd; background: #f4f4f4; color: #000;"><strong><?php esc_html_e( 'Type' ); ?></strong></summary>
237 + <?php
238 + $list_types = array(
239 + 'image/jpeg',
240 + 'image/png',
241 + 'image/bmp',
242 + 'image/gif',
243 + );
244 + foreach ( $list_types as $value ) {
245 + if ( in_array( $value, $pluswebp_settings['types'] ) ) {
246 + $check = 1;
247 + } else {
248 + $check = 0;
249 + }
250 + ?>
251 + <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>
252 + <?php
253 + }
254 + ?>
255 + <div style="margin: 5px; padding: 5px;">
256 + <p class="description">
257 + <?php esc_html_e( 'Check the images you want to convert to WebP.', 'plus-webp' ); ?>
258 + </p>
259 + </div>
260 + </details>
157 261
158 - /** ==================================================
159 - * Register Rest API
160 - *
161 - * @since 4.00
162 - */
163 - public function register_rest() {
262 + <details style="margin-bottom: 5px;">
263 + <summary style="cursor: pointer; padding: 10px; border: 1px solid #ddd; background: #f4f4f4; color: #000;"><strong><?php esc_html_e( 'Append the webp extension to the original filename', 'plus-webp' ); ?></strong></summary>
264 + <div style="margin: 5px; padding: 5px;">
265 + <input type="checkbox" name="addext" value="1" <?php checked( $pluswebp_settings['addext'], true ); ?>><?php esc_html_e( 'Apply' ); ?>
266 + <div style="margin: 5px; padding: 5px;">
267 + <p class="description">
268 + <?php esc_html_e( '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' ); ?>
269 + </p>
270 + </div>
271 + </div>
272 + </details>
164 273
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 - );
274 + <details style="margin-bottom: 5px;">
275 + <summary style="cursor: pointer; padding: 10px; border: 1px solid #ddd; background: #f4f4f4; color: #000;"><strong><?php esc_html_e( 'WebP replacement of images and contents', 'plus-webp' ); ?></strong></summary>
276 + <div style="margin: 5px; padding: 5px;">
277 + <input type="checkbox" name="replace" value="1" <?php checked( $pluswebp_settings['replace'], true ); ?>><?php esc_html_e( 'Apply' ); ?>
278 + <div style="margin: 5px; padding: 5px;">
279 + <p class="description">
280 + <?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' ); ?>
281 + </p>
282 + </div>
283 + </div>
284 + </details>
174 285
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 - );
286 + <?php submit_button( __( 'Save Changes' ), 'large', 'Manageset', false ); ?>
287 + </form>
288 + </div>
184 289
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 - }
290 + </div>
195 291
196 - /** ==================================================
197 - * Rest Permission
198 - *
199 - * @since 4.00
200 - */
201 - public function rest_permission() {
202 -
203 - return current_user_can( 'manage_options' );
292 + </div>
293 + <?php
204 294 }
205 295
206 296 /** ==================================================
207 - * Rest API save for Confirm
297 + * Credit
208 298 *
209 - * @param object $request changed data.
210 - * @since 5.20
299 + * @since 1.00
211 300 */
212 - public function confirm_api_save( $request ) {
301 + private function credit() {
213 302
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 ) );
303 + $plugin_name = null;
304 + $plugin_ver_num = null;
305 + $plugin_path = plugin_dir_path( __DIR__ );
306 + $plugin_dir = untrailingslashit( wp_normalize_path( $plugin_path ) );
307 + $slugs = explode( '/', $plugin_dir );
308 + $slug = end( $slugs );
309 + $files = scandir( $plugin_dir );
310 + foreach ( $files as $file ) {
311 + if ( '.' === $file || '..' === $file || is_dir( $plugin_path . $file ) ) {
312 + continue;
226 313 } 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 ) );
314 + $exts = explode( '.', $file );
315 + $ext = strtolower( end( $exts ) );
316 + if ( 'php' === $ext ) {
317 + $plugin_datas = get_file_data(
318 + $plugin_path . $file,
319 + array(
320 + 'name' => 'Plugin Name',
321 + 'version' => 'Version',
322 + )
323 + );
324 + if ( array_key_exists( 'name', $plugin_datas ) && ! empty( $plugin_datas['name'] ) && array_key_exists( 'version', $plugin_datas ) && ! empty( $plugin_datas['version'] ) ) {
325 + $plugin_name = $plugin_datas['name'];
326 + $plugin_ver_num = $plugin_datas['version'];
327 + break;
328 + }
272 329 }
273 - delete_option( 'pluswebp_messages' );
274 330 }
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 331 }
332 + $plugin_version = __( 'Version:' ) . ' ' . $plugin_ver_num;
333 + /* translators: FAQ Link & Slug */
334 + $faq = sprintf( __( 'https://wordpress.org/plugins/%s/faq', 'plus-webp' ), $slug );
335 + $support = 'https://wordpress.org/support/plugin/' . $slug;
336 + $review = 'https://wordpress.org/support/view/plugin-reviews/' . $slug;
337 + $translate = 'https://translate.wordpress.org/projects/wp-plugins/' . $slug;
338 + $facebook = 'https://www.facebook.com/katsushikawamori/';
339 + $twitter = 'https://twitter.com/dodesyo312';
340 + $youtube = 'https://www.youtube.com/channel/UC5zTLeyROkvZm86OgNRcb_w';
341 + $donate = __( 'https://shop.riverforest-wp.info/donate/', 'plus-webp' );
282 342
283 - return new WP_REST_Response( $args, 200 );
284 - }
343 + ?>
344 + <span style="font-weight: bold;">
345 + <div>
346 + <?php echo esc_html( $plugin_version ); ?> |
347 + <a style="text-decoration: none;" href="<?php echo esc_url( $faq ); ?>" target="_blank" rel="noopener noreferrer">FAQ</a> | <a style="text-decoration: none;" href="<?php echo esc_url( $support ); ?>" target="_blank" rel="noopener noreferrer">Support Forums</a> | <a style="text-decoration: none;" href="<?php echo esc_url( $review ); ?>" target="_blank" rel="noopener noreferrer">Reviews</a>
348 + </div>
349 + <div>
350 + <a style="text-decoration: none;" href="<?php echo esc_url( $translate ); ?>" target="_blank" rel="noopener noreferrer">
351 + <?php
352 + /* translators: Plugin translation link */
353 + echo esc_html( sprintf( __( 'Translations for %s' ), $plugin_name ) );
354 + ?>
355 + </a> | <a style="text-decoration: none;" href="<?php echo esc_url( $facebook ); ?>" target="_blank" rel="noopener noreferrer"><span class="dashicons dashicons-facebook"></span></a> | <a style="text-decoration: none;" href="<?php echo esc_url( $twitter ); ?>" target="_blank" rel="noopener noreferrer"><span class="dashicons dashicons-twitter"></span></a> | <a style="text-decoration: none;" href="<?php echo esc_url( $youtube ); ?>" target="_blank" rel="noopener noreferrer"><span class="dashicons dashicons-video-alt3"></span></a>
356 + </div>
357 + </span>
285 358
286 - /** ==================================================
287 - * Save for Mail messages
288 - *
289 - * @param string $message Message.
290 - * @since 5.21
291 - */
292 - private function write_tmp_messages( $message ) {
359 + <div style="width: 250px; height: 180px; margin: 5px; padding: 5px; border: #CCC 2px solid;">
360 + <h3><?php esc_html_e( 'Please make a donation if you like my work or would like to further the development of this plugin.', 'plus-webp' ); ?></h3>
361 + <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>
362 + <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>
363 + </div>
293 364
294 - $temp_dir = get_temp_dir();
295 - $file_path = $temp_dir . 'pluswebp_messages.txt';
365 + <?php
296 366
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 367 }
311 368
312 369 /** ==================================================
313 - * Read for Mail messages
370 + * Get All Images
314 371 *
315 - * @return array $messages.
316 - * @since 5.21
372 + * @since 2.00
317 373 */
318 - private function read_tmp_messages() {
374 + private function get_allimages() {
319 375
320 - $temp_dir = get_temp_dir();
321 - $file_path = $temp_dir . 'pluswebp_messages.txt';
376 + $pluswebp_settings = get_option( 'pluswebp' );
377 + $args = array(
378 + 'post_type' => 'attachment',
379 + 'post_status' => 'inherit',
380 + 'post_mime_type' => $pluswebp_settings['types'],
381 + 'posts_per_page' => -1,
382 + 'orderby' => 'date',
383 + 'order' => 'DESC',
384 + );
385 + $posts = get_posts( $args );
322 386
323 - $messages = array();
387 + global $wpdb;
388 + $webp_titles = $wpdb->get_col(
389 + "
390 + SELECT post_title
391 + FROM {$wpdb->prefix}posts
392 + WHERE post_type = 'attachment'
393 + AND post_mime_type IN ( 'image/webp' )
394 + AND post_status = 'inherit'
395 + "
396 + );
324 397
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 - }
398 + $post_ids = array();
399 + foreach ( $posts as $post ) {
400 + if ( ! in_array( $post->post_title, $webp_titles ) ) {
401 + $post_ids[] = $post->ID;
348 402 }
349 403 }
350 404
351 - if ( $wp_filesystem->exists( $file_path ) ) {
352 - $wp_filesystem->delete( $file_path );
353 - }
405 + return $post_ids;
354 406
355 - return $messages;
356 407 }
357 408
358 409 /** ==================================================
359 - * Rest API save for Settings
410 + * Regenerate All Images
360 411 *
361 - * @param object $request changed data.
362 - * @since 4.00
412 + * @param string $to mail address.
413 + * @since 1.00
363 414 */
364 - public function settings_api_save( $request ) {
415 + public function allimages_regenerate( $to ) {
365 416
366 - $args = $request->get_json_params();
417 + $post_ids = $this->get_allimages();
367 418
368 - if ( isset( $args['settings'] ) && is_array( $args['settings'] ) ) {
369 - $settings = $args['settings'];
419 + if ( ! empty( $post_ids ) ) {
420 + delete_option( 'pluswebp_generate' );
421 + include_once ABSPATH . 'wp-admin/includes/image.php';
422 + /* Generate WebP */
423 + foreach ( $post_ids as $attach_id ) {
424 + $metadata = wp_get_attachment_metadata( $attach_id );
425 + do_action( 'wp_generate_attachment_metadata', $metadata, $attach_id );
426 + }
427 + /* Mail send */
428 + do_action( 'pluswebp_mail_send', $to );
370 429 } else {
371 - return new WP_Error(
372 - 'invalid_settings',
373 - 'Invalid settings.',
374 - array( 'status' => 400 )
375 - );
430 + ?>
431 + <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>';
432 + <?php
376 433 }
377 434
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 435 }
397 436
398 437 /** ==================================================
399 - * Credit for Gutenberg
438 + * Update wp_options table.
400 439 *
401 - * @param string $handle handle.
402 - * @since 4.00
440 + * @since 1.02
403 441 */
404 - private function credit_gutenberg( $handle ) {
442 + private function options_updated() {
405 443
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,
444 + if ( isset( $_POST['Manageset'] ) && ! empty( $_POST['Manageset'] ) ) {
445 + if ( check_admin_referer( 'pwp_set', 'pluswebp_set' ) ) {
446 + $pluswebp_settings = get_option( 'pluswebp' );
447 + $list_types = array();
448 + if ( isset( $_POST['list_types'] ) && ! empty( $_POST['list_types'] ) ) {
449 + $tmps = filter_var(
450 + wp_unslash( $_POST['list_types'] ),
451 + FILTER_CALLBACK,
422 452 array(
423 - 'name' => 'Plugin Name',
424 - 'version' => 'Version',
453 + 'options' => function( $value ) {
454 + return sanitize_text_field( $value );
455 + },
425 456 )
426 457 );
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;
458 + foreach ( $tmps as $key => $value ) {
459 + $list_types[] = $key;
431 460 }
461 + $pluswebp_settings['types'] = $list_types;
462 + } else {
463 + $pluswebp_settings['types'] = $list_types;
432 464 }
465 + if ( isset( $_POST['addext'] ) && ! empty( $_POST['addext'] ) ) {
466 + $pluswebp_settings['addext'] = true;
467 + } else {
468 + $pluswebp_settings['addext'] = false;
469 + }
470 + if ( isset( $_POST['replace'] ) && ! empty( $_POST['replace'] ) ) {
471 + $pluswebp_settings['replace'] = true;
472 + } else {
473 + $pluswebp_settings['replace'] = false;
474 + }
475 + if ( isset( $_POST['quality'] ) ) {
476 + $pluswebp_settings['quality'] = intval( $_POST['quality'] );
477 + }
478 + update_option( 'pluswebp', $pluswebp_settings );
479 + echo '<div class="notice notice-success is-dismissible"><ul><li>' . esc_html__( 'Settings' ) . ' --> ' . esc_html__( 'Settings saved.' ) . '</li></ul></div>';
433 480 }
434 481 }
435 482
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 483 }
458 484
459 485 /** ==================================================
460 486 * Settings register
@@ -479,20 +505,14 @@
479 505 if ( ! array_key_exists( 'addext', $pluswebp_settings ) ) {
480 506 $pluswebp_settings['addext'] = false;
481 507 update_option( 'pluswebp', $pluswebp_settings );
482 508 }
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 509 } else {
489 510 $pswp_tbl = array(
490 - 'output_mime' => 'image/webp',
491 511 'quality' => 80,
492 512 'replace' => false,
493 - 'addext' => false,
494 - 'types' => array(
513 + 'addext' => false,
514 + 'types' => array(
495 515 'image/jpeg',
496 516 'image/png',
497 517 'image/bmp',
498 518 'image/gif',
@@ -499,6 +519,43 @@
499 519 ),
500 520 );
501 521 update_option( 'pluswebp', $pswp_tbl );
502 522 }
523 +
503 524 }
525 +
526 + /** ==================================================
527 + * Generate notice
528 + *
529 + * @since 1.09
530 + */
531 + public function generate_notice() {
532 +
533 + if ( get_option( 'pluswebp_generate_mail' ) ) {
534 + $pluswebp_mail_send = get_option( 'pluswebp_generate_mail' );
535 + if ( 0 < $pluswebp_mail_send['count'] ) {
536 + ?>
537 + <div class="notice notice-success is-dismissible"><ul><li><strong>Plus WebP</strong>
538 + <?php
539 + /* translators: %1$s Date Time, %2$d File Count */
540 + 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'] ) );
541 + ?>
542 + </li></ul></div>
543 + <?php
544 + } else {
545 + ?>
546 + <div class="notice notice-error is-dismissible"><ul><li><strong>Plus WebP</strong>
547 + <?php
548 + /* translators: %1$s Date Time %2$s error message */
549 + 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' ) ) );
550 + ?>
551 + </li></ul></div>
552 + <?php
553 + }
554 + delete_option( 'pluswebp_generate_mail' );
555 + }
556 +
557 + }
558 +
504 559 }
560 +
561 +