| @@ -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,407 @@ | ||
| 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 | + <div style="margin: 5px; padding: 5px;"> | |
| 190 | + <p class="description"> | |
| 191 | + <?php esc_html_e( 'Press the following button to start WebP generation in the background. When finished, you will receive an email.', 'plus-webp' ); ?> | |
| 192 | + </p> | |
| 193 | + </div> | |
| 194 | + <form method="post" action="<?php echo esc_url( $scriptname ); ?>"> | |
| 195 | + <?php wp_nonce_field( 'pwp_gnt', 'pluswebp_gnt' ); ?> | |
| 196 | + <?php submit_button( __( 'Generate Background', 'plus-webp' ), 'primary', 'Generatewebp', false ); ?> | |
| 197 | + </form> | |
| 198 | + </details> | |
| 142 | 199 | |
| 143 | - $pluswebp_settings = get_option( 'pluswebp' ); | |
| 200 | + </div> | |
| 144 | 201 | |
| 145 | - wp_localize_script( | |
| 146 | - 'plus-webp', | |
| 147 | - 'pluswebpsettings_data', | |
| 148 | - array( | |
| 149 | - 'settings' => $pluswebp_settings, | |
| 150 | - ) | |
| 151 | - ); | |
| 202 | + <div class="wrap"> | |
| 203 | + <h2><?php esc_html_e( 'Settings' ); ?></h2> | |
| 152 | 204 | |
| 153 | - wp_set_script_translations( 'plus-webp', 'plus-webp' ); | |
| 205 | + <form method="post" action="<?php echo esc_url( $scriptname ); ?>"> | |
| 206 | + <?php wp_nonce_field( 'pwp_set', 'pluswebp_set' ); ?> | |
| 154 | 207 | |
| 155 | - $this->credit_gutenberg( 'plus-webp' ); | |
| 156 | - } | |
| 208 | + <details style="margin-bottom: 5px;"> | |
| 209 | + <summary style="cursor: pointer; padding: 10px; border: 1px solid #ddd; background: #f4f4f4; color: #000;"><strong><?php esc_html_e( 'Quality', 'plus-webp' ); ?></strong></summary> | |
| 210 | + <div style="margin: 5px; padding: 5px;"> | |
| 211 | + <?php esc_html_e( 'low resolution', 'plus-webp' ); ?> | |
| 212 | + <input type="range" style="vertical-align:middle;" name="quality" value="<?php echo esc_attr( $pluswebp_settings['quality'] ); ?>" min="0" max="100" step="1"> | |
| 213 | + <?php esc_html_e( 'high resolution', 'plus-webp' ); ?> | |
| 214 | + <div style="margin: 5px; padding: 5px;"> | |
| 215 | + <p class="description"> | |
| 216 | + <?php esc_html_e( 'Specifies the quality of WebP. The higher the resolution, the larger the file size.', 'plus-webp' ); ?> | |
| 217 | + </p> | |
| 218 | + </div> | |
| 219 | + </div> | |
| 220 | + </details> | |
| 157 | 221 | |
| 158 | - /** ================================================== | |
| 159 | - * Register Rest API | |
| 160 | - * | |
| 161 | - * @since 4.00 | |
| 162 | - */ | |
| 163 | - public function register_rest() { | |
| 222 | + <details style="margin-bottom: 5px;"> | |
| 223 | + <summary style="cursor: pointer; padding: 10px; border: 1px solid #ddd; background: #f4f4f4; color: #000;"><strong><?php esc_html_e( 'Type' ); ?></strong></summary> | |
| 224 | + <?php | |
| 225 | + $list_types = array( | |
| 226 | + 'image/jpeg', | |
| 227 | + 'image/png', | |
| 228 | + 'image/bmp', | |
| 229 | + 'image/gif', | |
| 230 | + ); | |
| 231 | + foreach ( $list_types as $value ) { | |
| 232 | + if ( in_array( $value, $pluswebp_settings['types'] ) ) { | |
| 233 | + $check = 1; | |
| 234 | + } else { | |
| 235 | + $check = 0; | |
| 236 | + } | |
| 237 | + ?> | |
| 238 | + <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> | |
| 239 | + <?php | |
| 240 | + } | |
| 241 | + ?> | |
| 242 | + <div style="margin: 5px; padding: 5px;"> | |
| 243 | + <p class="description"> | |
| 244 | + <?php esc_html_e( 'Check the images you want to convert to WebP.', 'plus-webp' ); ?> | |
| 245 | + </p> | |
| 246 | + </div> | |
| 247 | + </details> | |
| 164 | 248 | |
| 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 | - ); | |
| 249 | + <details style="margin-bottom: 5px;"> | |
| 250 | + <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> | |
| 251 | + <div style="margin: 5px; padding: 5px;"> | |
| 252 | + <input type="checkbox" name="addext" value="1" <?php checked( $pluswebp_settings['addext'], true ); ?>><?php esc_html_e( 'Apply' ); ?> | |
| 253 | + <div style="margin: 5px; padding: 5px;"> | |
| 254 | + <p class="description"> | |
| 255 | + <?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' ); ?> | |
| 256 | + </p> | |
| 257 | + </div> | |
| 258 | + </div> | |
| 259 | + </details> | |
| 174 | 260 | |
| 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 | - ); | |
| 261 | + <details style="margin-bottom: 5px;"> | |
| 262 | + <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> | |
| 263 | + <div style="margin: 5px; padding: 5px;"> | |
| 264 | + <input type="checkbox" name="replace" value="1" <?php checked( $pluswebp_settings['replace'], true ); ?>><?php esc_html_e( 'Apply' ); ?> | |
| 265 | + <div style="margin: 5px; padding: 5px;"> | |
| 266 | + <p class="description"> | |
| 267 | + <?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' ); ?> | |
| 268 | + </p> | |
| 269 | + </div> | |
| 270 | + </div> | |
| 271 | + </details> | |
| 184 | 272 | |
| 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 | - } | |
| 273 | + <?php submit_button( __( 'Save Changes' ), 'large', 'Manageset', false ); ?> | |
| 274 | + </form> | |
| 275 | + </div> | |
| 195 | 276 | |
| 196 | - /** ================================================== | |
| 197 | - * Rest Permission | |
| 198 | - * | |
| 199 | - * @since 4.00 | |
| 200 | - */ | |
| 201 | - public function rest_permission() { | |
| 277 | + </div> | |
| 202 | 278 | |
| 203 | - return current_user_can( 'manage_options' ); | |
| 279 | + </div> | |
| 280 | + <?php | |
| 204 | 281 | } |
| 205 | 282 | |
| 206 | 283 | /** ================================================== |
| 207 | - * Rest API save for Confirm | |
| 284 | + * Credit | |
| 208 | 285 | * |
| 209 | - * @param object $request changed data. | |
| 210 | - * @since 5.20 | |
| 286 | + * @since 1.00 | |
| 211 | 287 | */ |
| 212 | - public function confirm_api_save( $request ) { | |
| 288 | + private function credit() { | |
| 213 | 289 | |
| 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 ) ); | |
| 290 | + $plugin_name = null; | |
| 291 | + $plugin_ver_num = null; | |
| 292 | + $plugin_path = plugin_dir_path( __DIR__ ); | |
| 293 | + $plugin_dir = untrailingslashit( wp_normalize_path( $plugin_path ) ); | |
| 294 | + $slugs = explode( '/', $plugin_dir ); | |
| 295 | + $slug = end( $slugs ); | |
| 296 | + $files = scandir( $plugin_dir ); | |
| 297 | + foreach ( $files as $file ) { | |
| 298 | + if ( '.' === $file || '..' === $file || is_dir( $plugin_path . $file ) ) { | |
| 299 | + continue; | |
| 226 | 300 | } 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 ) ); | |
| 301 | + $exts = explode( '.', $file ); | |
| 302 | + $ext = strtolower( end( $exts ) ); | |
| 303 | + if ( 'php' === $ext ) { | |
| 304 | + $plugin_datas = get_file_data( | |
| 305 | + $plugin_path . $file, | |
| 306 | + array( | |
| 307 | + 'name' => 'Plugin Name', | |
| 308 | + 'version' => 'Version', | |
| 309 | + ) | |
| 310 | + ); | |
| 311 | + if ( array_key_exists( 'name', $plugin_datas ) && ! empty( $plugin_datas['name'] ) && array_key_exists( 'version', $plugin_datas ) && ! empty( $plugin_datas['version'] ) ) { | |
| 312 | + $plugin_name = $plugin_datas['name']; | |
| 313 | + $plugin_ver_num = $plugin_datas['version']; | |
| 314 | + break; | |
| 315 | + } | |
| 272 | 316 | } |
| 273 | - delete_option( 'pluswebp_messages' ); | |
| 274 | 317 | } |
| 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 | 318 | } |
| 319 | + $plugin_version = __( 'Version:' ) . ' ' . $plugin_ver_num; | |
| 320 | + /* translators: FAQ Link & Slug */ | |
| 321 | + $faq = sprintf( __( 'https://wordpress.org/plugins/%s/faq', 'plus-webp' ), $slug ); | |
| 322 | + $support = 'https://wordpress.org/support/plugin/' . $slug; | |
| 323 | + $review = 'https://wordpress.org/support/view/plugin-reviews/' . $slug; | |
| 324 | + $translate = 'https://translate.wordpress.org/projects/wp-plugins/' . $slug; | |
| 325 | + $facebook = 'https://www.facebook.com/katsushikawamori/'; | |
| 326 | + $twitter = 'https://twitter.com/dodesyo312'; | |
| 327 | + $youtube = 'https://www.youtube.com/channel/UC5zTLeyROkvZm86OgNRcb_w'; | |
| 328 | + $donate = __( 'https://shop.riverforest-wp.info/donate/', 'plus-webp' ); | |
| 282 | 329 | |
| 283 | - return new WP_REST_Response( $args, 200 ); | |
| 284 | - } | |
| 330 | + ?> | |
| 331 | + <span style="font-weight: bold;"> | |
| 332 | + <div> | |
| 333 | + <?php echo esc_html( $plugin_version ); ?> | | |
| 334 | + <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> | |
| 335 | + </div> | |
| 336 | + <div> | |
| 337 | + <a style="text-decoration: none;" href="<?php echo esc_url( $translate ); ?>" target="_blank" rel="noopener noreferrer"> | |
| 338 | + <?php | |
| 339 | + /* translators: Plugin translation link */ | |
| 340 | + echo esc_html( sprintf( __( 'Translations for %s' ), $plugin_name ) ); | |
| 341 | + ?> | |
| 342 | + </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> | |
| 343 | + </div> | |
| 344 | + </span> | |
| 285 | 345 | |
| 286 | - /** ================================================== | |
| 287 | - * Save for Mail messages | |
| 288 | - * | |
| 289 | - * @param string $message Message. | |
| 290 | - * @since 5.21 | |
| 291 | - */ | |
| 292 | - private function write_tmp_messages( $message ) { | |
| 346 | + <div style="width: 250px; height: 180px; margin: 5px; padding: 5px; border: #CCC 2px solid;"> | |
| 347 | + <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> | |
| 348 | + <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> | |
| 349 | + <button type="button" style="margin: 5px; padding: 5px;" onclick="window.open('<?php echo esc_url( $donate ); ?>')"><?php esc_html_e( 'Donate to this plugin »' ); ?></button> | |
| 350 | + </div> | |
| 293 | 351 | |
| 294 | - $temp_dir = get_temp_dir(); | |
| 295 | - $file_path = $temp_dir . 'pluswebp_messages.txt'; | |
| 352 | + <?php | |
| 296 | 353 | |
| 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 | 354 | } |
| 311 | 355 | |
| 312 | 356 | /** ================================================== |
| 313 | - * Read for Mail messages | |
| 357 | + * Get All Images | |
| 314 | 358 | * |
| 315 | - * @return array $messages. | |
| 316 | - * @since 5.21 | |
| 359 | + * @since 2.00 | |
| 317 | 360 | */ |
| 318 | - private function read_tmp_messages() { | |
| 361 | + private function get_allimages() { | |
| 319 | 362 | |
| 320 | - $temp_dir = get_temp_dir(); | |
| 321 | - $file_path = $temp_dir . 'pluswebp_messages.txt'; | |
| 363 | + $pluswebp_settings = get_option( 'pluswebp' ); | |
| 364 | + $args = array( | |
| 365 | + 'post_type' => 'attachment', | |
| 366 | + 'post_status' => 'inherit', | |
| 367 | + 'post_mime_type' => $pluswebp_settings['types'], | |
| 368 | + 'posts_per_page' => -1, | |
| 369 | + 'orderby' => 'date', | |
| 370 | + 'order' => 'DESC', | |
| 371 | + ); | |
| 372 | + $posts = get_posts( $args ); | |
| 322 | 373 | |
| 323 | - $messages = array(); | |
| 374 | + global $wpdb; | |
| 375 | + $webp_titles = $wpdb->get_col( | |
| 376 | + " | |
| 377 | + SELECT post_title | |
| 378 | + FROM {$wpdb->prefix}posts | |
| 379 | + WHERE post_type = 'attachment' | |
| 380 | + AND post_mime_type IN ( 'image/webp' ) | |
| 381 | + AND post_status = 'inherit' | |
| 382 | + " | |
| 383 | + ); | |
| 324 | 384 | |
| 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 | - } | |
| 385 | + $post_ids = array(); | |
| 386 | + foreach ( $posts as $post ) { | |
| 387 | + if ( ! in_array( $post->post_title, $webp_titles ) ) { | |
| 388 | + $post_ids[] = $post->ID; | |
| 348 | 389 | } |
| 349 | 390 | } |
| 350 | 391 | |
| 351 | - if ( $wp_filesystem->exists( $file_path ) ) { | |
| 352 | - $wp_filesystem->delete( $file_path ); | |
| 353 | - } | |
| 392 | + return $post_ids; | |
| 354 | 393 | |
| 355 | - return $messages; | |
| 356 | 394 | } |
| 357 | 395 | |
| 358 | 396 | /** ================================================== |
| 359 | - * Rest API save for Settings | |
| 397 | + * Regenerate All Images | |
| 360 | 398 | * |
| 361 | - * @param object $request changed data. | |
| 362 | - * @since 4.00 | |
| 399 | + * @param string $to mail address. | |
| 400 | + * @since 1.00 | |
| 363 | 401 | */ |
| 364 | - public function settings_api_save( $request ) { | |
| 402 | + public function allimages_regenerate( $to ) { | |
| 365 | 403 | |
| 366 | - $args = $request->get_json_params(); | |
| 404 | + $post_ids = $this->get_allimages(); | |
| 367 | 405 | |
| 368 | - if ( isset( $args['settings'] ) && is_array( $args['settings'] ) ) { | |
| 369 | - $settings = $args['settings']; | |
| 406 | + if ( ! empty( $post_ids ) ) { | |
| 407 | + delete_option( 'pluswebp_generate' ); | |
| 408 | + include_once ABSPATH . 'wp-admin/includes/image.php'; | |
| 409 | + /* Generate WebP */ | |
| 410 | + foreach ( $post_ids as $attach_id ) { | |
| 411 | + $metadata = wp_get_attachment_metadata( $attach_id ); | |
| 412 | + do_action( 'wp_generate_attachment_metadata', $metadata, $attach_id ); | |
| 413 | + } | |
| 414 | + /* Mail send */ | |
| 415 | + do_action( 'pluswebp_mail_send', $to ); | |
| 370 | 416 | } else { |
| 371 | - return new WP_Error( | |
| 372 | - 'invalid_settings', | |
| 373 | - 'Invalid settings.', | |
| 374 | - array( 'status' => 400 ) | |
| 375 | - ); | |
| 417 | + ?> | |
| 418 | + <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>'; | |
| 419 | + <?php | |
| 376 | 420 | } |
| 377 | 421 | |
| 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 | 422 | } |
| 397 | 423 | |
| 398 | 424 | /** ================================================== |
| 399 | - * Credit for Gutenberg | |
| 425 | + * Update wp_options table. | |
| 400 | 426 | * |
| 401 | - * @param string $handle handle. | |
| 402 | - * @since 4.00 | |
| 427 | + * @since 1.02 | |
| 403 | 428 | */ |
| 404 | - private function credit_gutenberg( $handle ) { | |
| 429 | + private function options_updated() { | |
| 405 | 430 | |
| 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, | |
| 431 | + if ( isset( $_POST['Manageset'] ) && ! empty( $_POST['Manageset'] ) ) { | |
| 432 | + if ( check_admin_referer( 'pwp_set', 'pluswebp_set' ) ) { | |
| 433 | + $pluswebp_settings = get_option( 'pluswebp' ); | |
| 434 | + $list_types = array(); | |
| 435 | + if ( isset( $_POST['list_types'] ) && ! empty( $_POST['list_types'] ) ) { | |
| 436 | + $tmps = filter_var( | |
| 437 | + wp_unslash( $_POST['list_types'] ), | |
| 438 | + FILTER_CALLBACK, | |
| 422 | 439 | array( |
| 423 | - 'name' => 'Plugin Name', | |
| 424 | - 'version' => 'Version', | |
| 440 | + 'options' => function( $value ) { | |
| 441 | + return sanitize_text_field( $value ); | |
| 442 | + }, | |
| 425 | 443 | ) |
| 426 | 444 | ); |
| 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; | |
| 445 | + foreach ( $tmps as $key => $value ) { | |
| 446 | + $list_types[] = $key; | |
| 431 | 447 | } |
| 448 | + $pluswebp_settings['types'] = $list_types; | |
| 449 | + } else { | |
| 450 | + $pluswebp_settings['types'] = $list_types; | |
| 432 | 451 | } |
| 452 | + if ( isset( $_POST['addext'] ) && ! empty( $_POST['addext'] ) ) { | |
| 453 | + $pluswebp_settings['addext'] = true; | |
| 454 | + } else { | |
| 455 | + $pluswebp_settings['addext'] = false; | |
| 456 | + } | |
| 457 | + if ( isset( $_POST['replace'] ) && ! empty( $_POST['replace'] ) ) { | |
| 458 | + $pluswebp_settings['replace'] = true; | |
| 459 | + } else { | |
| 460 | + $pluswebp_settings['replace'] = false; | |
| 461 | + } | |
| 462 | + if ( isset( $_POST['quality'] ) ) { | |
| 463 | + $pluswebp_settings['quality'] = intval( $_POST['quality'] ); | |
| 464 | + } | |
| 465 | + update_option( 'pluswebp', $pluswebp_settings ); | |
| 466 | + echo '<div class="notice notice-success is-dismissible"><ul><li>' . esc_html__( 'Settings' ) . ' --> ' . esc_html__( 'Settings saved.' ) . '</li></ul></div>'; | |
| 433 | 467 | } |
| 434 | 468 | } |
| 435 | 469 | |
| 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 »', 'plus-webp' ), | |
| 455 | - ) | |
| 456 | - ); | |
| 457 | 470 | } |
| 458 | 471 | |
| 459 | 472 | /** ================================================== |
| 460 | 473 | * Settings register |
| @@ -479,20 +492,14 @@ | ||
| 479 | 492 | if ( ! array_key_exists( 'addext', $pluswebp_settings ) ) { |
| 480 | 493 | $pluswebp_settings['addext'] = false; |
| 481 | 494 | update_option( 'pluswebp', $pluswebp_settings ); |
| 482 | 495 | } |
| 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 | 496 | } else { |
| 489 | 497 | $pswp_tbl = array( |
| 490 | - 'output_mime' => 'image/webp', | |
| 491 | 498 | 'quality' => 80, |
| 492 | 499 | 'replace' => false, |
| 493 | - 'addext' => false, | |
| 494 | - 'types' => array( | |
| 500 | + 'addext' => false, | |
| 501 | + 'types' => array( | |
| 495 | 502 | 'image/jpeg', |
| 496 | 503 | 'image/png', |
| 497 | 504 | 'image/bmp', |
| 498 | 505 | 'image/gif', |
| @@ -499,6 +506,43 @@ | ||
| 499 | 506 | ), |
| 500 | 507 | ); |
| 501 | 508 | update_option( 'pluswebp', $pswp_tbl ); |
| 502 | 509 | } |
| 510 | + | |
| 503 | 511 | } |
| 512 | + | |
| 513 | + /** ================================================== | |
| 514 | + * Generate notice | |
| 515 | + * | |
| 516 | + * @since 1.09 | |
| 517 | + */ | |
| 518 | + public function generate_notice() { | |
| 519 | + | |
| 520 | + if ( get_option( 'pluswebp_generate_mail' ) ) { | |
| 521 | + $pluswebp_mail_send = get_option( 'pluswebp_generate_mail' ); | |
| 522 | + if ( 0 < $pluswebp_mail_send['count'] ) { | |
| 523 | + ?> | |
| 524 | + <div class="notice notice-success is-dismissible"><ul><li><strong>Plus WebP</strong> | |
| 525 | + <?php | |
| 526 | + /* translators: %1$s Date Time, %2$d File Count */ | |
| 527 | + 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'] ) ); | |
| 528 | + ?> | |
| 529 | + </li></ul></div> | |
| 530 | + <?php | |
| 531 | + } else { | |
| 532 | + ?> | |
| 533 | + <div class="notice notice-error is-dismissible"><ul><li><strong>Plus WebP</strong> | |
| 534 | + <?php | |
| 535 | + /* translators: %1$s Date Time %2$s error message */ | |
| 536 | + 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' ) ) ); | |
| 537 | + ?> | |
| 538 | + </li></ul></div> | |
| 539 | + <?php | |
| 540 | + } | |
| 541 | + delete_option( 'pluswebp_generate_mail' ); | |
| 542 | + } | |
| 543 | + | |
| 544 | + } | |
| 545 | + | |
| 504 | 546 | } |
| 547 | + | |
| 548 | + | |