| @@ -32,14 +32,15 @@ | ||
| 32 | 32 | * @since 1.00 |
| 33 | 33 | */ |
| 34 | 34 | public function __construct() { |
| 35 | 35 | |
| 36 | + add_action( 'admin_init', array( $this, 'register_settings' ) ); | |
| 37 | + | |
| 36 | 38 | add_action( 'admin_menu', array( $this, 'plugin_menu' ) ); |
| 37 | 39 | add_filter( 'plugin_action_links', array( $this, 'settings_link' ), 10, 2 ); |
| 38 | - add_action( 'admin_notices', array( $this, 'generate_notice' ) ); | |
| 39 | 40 | |
| 40 | - add_action( 'AllImagesRegenerateHook', array( $this, 'allimages_regenerate' ), 10, 1 ); | |
| 41 | - | |
| 41 | + add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ), 10, 1 ); | |
| 42 | + add_action( 'rest_api_init', array( $this, 'register_rest' ) ); | |
| 42 | 43 | } |
| 43 | 44 | |
| 44 | 45 | /** ================================================== |
| 45 | 46 | * Add a "Settings" link to the plugins page |
| @@ -54,9 +55,9 @@ | ||
| 54 | 55 | if ( empty( $this_plugin ) ) { |
| 55 | 56 | $this_plugin = 'plus-webp/pluswebp.php'; |
| 56 | 57 | } |
| 57 | 58 | if ( $file === $this_plugin ) { |
| 58 | - $links[] = '<a href="' . admin_url( 'tools.php?page=pluswebp' ) . '">' . esc_html__( 'Bulk Generate', 'plus-webp' ) . '</a>'; | |
| 59 | + $links[] = '<a href="' . admin_url( 'upload.php?page=plus-webp' ) . '">' . esc_html__( 'Settings' ) . ' & ' . esc_html__( 'Bulk Generate', 'plus-webp' ) . '</a>'; | |
| 59 | 60 | } |
| 60 | 61 | return $links; |
| 61 | 62 | } |
| 62 | 63 | |
| @@ -65,223 +66,235 @@ | ||
| 65 | 66 | * |
| 66 | 67 | * @since 1.00 |
| 67 | 68 | */ |
| 68 | 69 | public function plugin_menu() { |
| 69 | - add_management_page( 'Plus WebP', 'Plus WebP', 'manage_options', 'pluswebp', array( $this, 'plugin_options' ) ); | |
| 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 | + ); | |
| 70 | 78 | } |
| 71 | 79 | |
| 72 | 80 | /** ================================================== |
| 73 | - * Settings page | |
| 81 | + * Generate and Settings page | |
| 74 | 82 | * |
| 75 | - * @since 1.00 | |
| 83 | + * @since 4.00 | |
| 76 | 84 | */ |
| 77 | - public function plugin_options() { | |
| 85 | + public function plus_webp_generate_setteings() { | |
| 78 | 86 | |
| 79 | - if ( ! current_user_can( 'manage_options' ) ) { | |
| 80 | - wp_die( esc_html__( 'You do not have sufficient permissions to access this page.' ) ); | |
| 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; | |
| 81 | 100 | } |
| 82 | 101 | |
| 83 | - $this->options_updated(); | |
| 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 | + | |
| 84 | 119 | $pluswebp_settings = get_option( 'pluswebp' ); |
| 85 | 120 | |
| 86 | - if ( isset( $_POST['Generatewebp'] ) && ! empty( $_POST['Generatewebp'] ) ) { | |
| 87 | - if ( check_admin_referer( 'pwp_gnt', 'pluswebp_gnt' ) ) { | |
| 88 | - if ( ! empty( $pluswebp_settings['types'] ) ) { | |
| 89 | - $user = wp_get_current_user(); | |
| 90 | - $to = $user->user_email; | |
| 91 | - $userid = $user->ID; | |
| 92 | - if ( ! wp_next_scheduled( 'AllImagesRegenerateHook', array( $to ) ) ) { | |
| 93 | - wp_schedule_single_event( time(), 'AllImagesRegenerateHook', array( $to ) ); | |
| 94 | - echo '<div class="notice notice-success is-dismissible"><ul><li>' . esc_html__( 'Background generation starts.', 'plus-webp' ) . '</li></ul></div>'; | |
| 95 | - } else { | |
| 96 | - 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>'; | |
| 97 | - } | |
| 98 | - } else { | |
| 99 | - 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>'; | |
| 100 | - } | |
| 101 | - } | |
| 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 ) ); | |
| 102 | 127 | } |
| 103 | 128 | |
| 104 | - $scriptname = admin_url( 'tools.php?page=pluswebp' ); | |
| 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 | + ); | |
| 105 | 152 | |
| 106 | - ?> | |
| 107 | - <div class="wrap"> | |
| 108 | - <h2>Plus WebP</h2> | |
| 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 | + ); | |
| 109 | 175 | |
| 110 | - <div id="bulkgeneratewebp-loading-container"> | |
| 176 | + $this->credit_gutenberg( 'plus-webp' ); | |
| 177 | + } | |
| 111 | 178 | |
| 112 | - <details> | |
| 113 | - <summary><strong><?php esc_html_e( 'Various links of this plugin', 'plus-webp' ); ?></strong></summary> | |
| 114 | - <?php $this->credit(); ?> | |
| 115 | - </details> | |
| 179 | + /** ================================================== | |
| 180 | + * Register Rest API | |
| 181 | + * | |
| 182 | + * @since 4.00 | |
| 183 | + */ | |
| 184 | + public function register_rest() { | |
| 116 | 185 | |
| 117 | - <div class="wrap"> | |
| 118 | - <h2><?php esc_html_e( 'Bulk Generate', 'plus-webp' ); ?></h2> | |
| 119 | - <details style="margin-bottom: 5px;"> | |
| 120 | - <summary style="cursor: pointer; padding: 10px; border: 1px solid #ddd; background: #f4f4f4; color: #000;"><strong><?php esc_html_e( 'Generate', 'plus-webp' ); ?></strong></summary> | |
| 121 | - <?php | |
| 122 | - $post_ids = $this->get_allimages(); | |
| 123 | - if ( ! empty( $post_ids ) ) { | |
| 124 | - ?> | |
| 125 | - <div style="margin: 5px; padding: 5px;"> | |
| 126 | - <p class="description"> | |
| 127 | - <?php esc_html_e( 'Press the following button to start WebP generation with Ajax.', 'plus-webp' ); ?> | |
| 128 | - </p> | |
| 129 | - </div> | |
| 130 | - <?php | |
| 131 | - /* for Ajax */ | |
| 132 | - $handle = 'bulkgeneratewebp-js'; | |
| 133 | - $action1 = 'bulkgeneratewebp-ajax-action'; | |
| 134 | - $action2 = 'bulkgeneratewebp_message'; | |
| 135 | - wp_enqueue_script( 'jquery' ); | |
| 136 | - wp_enqueue_script( $handle, plugin_dir_url( __DIR__ ) . 'js/jquery.bulkgeneratewebp.js', array( 'jquery' ), '1.00', false ); | |
| 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 | + ); | |
| 137 | 195 | |
| 138 | - wp_localize_script( | |
| 139 | - $handle, | |
| 140 | - 'bulkgeneratewebp', | |
| 141 | - array( | |
| 142 | - 'ajax_url' => admin_url( 'admin-ajax.php' ), | |
| 143 | - 'action' => $action1, | |
| 144 | - 'nonce' => wp_create_nonce( $action1 ), | |
| 145 | - ) | |
| 146 | - ); | |
| 147 | - wp_localize_script( | |
| 148 | - $handle, | |
| 149 | - 'bulkgeneratewebp_mes', | |
| 150 | - array( | |
| 151 | - 'ajax_url' => admin_url( 'admin-ajax.php' ), | |
| 152 | - 'action' => $action2, | |
| 153 | - 'nonce' => wp_create_nonce( $action2 ), | |
| 154 | - ) | |
| 155 | - ); | |
| 156 | - wp_localize_script( | |
| 157 | - $handle, | |
| 158 | - 'bulkgeneratewebp_data', | |
| 159 | - array( | |
| 160 | - 'count' => count( $post_ids ), | |
| 161 | - 'id' => json_encode( $post_ids, JSON_UNESCAPED_SLASHES ), | |
| 162 | - ) | |
| 163 | - ); | |
| 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 | + } | |
| 164 | 206 | |
| 165 | - wp_localize_script( | |
| 166 | - $handle, | |
| 167 | - 'bulkgeneratewebp_text', | |
| 168 | - array( | |
| 169 | - 'stop_button' => __( 'Stop', 'plus-webp' ), | |
| 170 | - 'stop_message' => __( 'Stopping now..', 'plus-webp' ), | |
| 171 | - ) | |
| 172 | - ); | |
| 173 | - submit_button( __( 'Generate', 'plus-webp' ), 'primary', 'bulkgeneratewebp_ajax_update', false ); | |
| 174 | - } | |
| 175 | - ?> | |
| 176 | - </details> | |
| 177 | - <details style="margin-bottom: 5px;"> | |
| 178 | - <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> | |
| 179 | - <div style="margin: 5px; padding: 5px;"> | |
| 180 | - <p class="description"> | |
| 181 | - <?php esc_html_e( 'Press the following button to start WebP generation in the background. When finished, you will receive an email.', 'plus-webp' ); ?> | |
| 182 | - </p> | |
| 183 | - </div> | |
| 184 | - <form method="post" action="<?php echo esc_url( $scriptname ); ?>"> | |
| 185 | - <?php wp_nonce_field( 'pwp_gnt', 'pluswebp_gnt' ); ?> | |
| 186 | - <?php submit_button( __( 'Generate Background', 'plus-webp' ), 'primary', 'Generatewebp', false ); ?> | |
| 187 | - </form> | |
| 188 | - </details> | |
| 207 | + /** ================================================== | |
| 208 | + * Rest Permission | |
| 209 | + * | |
| 210 | + * @since 4.00 | |
| 211 | + */ | |
| 212 | + public function rest_permission() { | |
| 189 | 213 | |
| 190 | - </div> | |
| 214 | + return current_user_can( 'manage_options' ); | |
| 215 | + } | |
| 191 | 216 | |
| 192 | - <div class="wrap"> | |
| 193 | - <h2><?php esc_html_e( 'Settings' ); ?></h2> | |
| 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 ) { | |
| 194 | 224 | |
| 195 | - <form method="post" action="<?php echo esc_url( $scriptname ); ?>"> | |
| 196 | - <?php wp_nonce_field( 'pwp_set', 'pluswebp_set' ); ?> | |
| 225 | + $args = json_decode( $request->get_body(), true ); | |
| 197 | 226 | |
| 198 | - <details style="margin-bottom: 5px;"> | |
| 199 | - <summary style="cursor: pointer; padding: 10px; border: 1px solid #ddd; background: #f4f4f4; color: #000;"><strong><?php esc_html_e( 'Quality', 'plus-webp' ); ?></strong></summary> | |
| 200 | - <div style="margin: 5px; padding: 5px;"> | |
| 201 | - <?php esc_html_e( 'low resolution', 'plus-webp' ); ?> | |
| 202 | - <input type="range" style="vertical-align:middle;" name="quality" value="<?php echo esc_attr( $pluswebp_settings['quality'] ); ?>" min="0" max="100" step="1"> | |
| 203 | - <?php esc_html_e( 'high resolution', 'plus-webp' ); ?> | |
| 204 | - <div style="margin: 5px; padding: 5px;"> | |
| 205 | - <p class="description"> | |
| 206 | - <?php esc_html_e( 'Specifies the quality of WebP. The higher the resolution, the larger the file size.', 'plus-webp' ); ?> | |
| 207 | - </p> | |
| 208 | - </div> | |
| 209 | - </div> | |
| 210 | - </details> | |
| 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() ); | |
| 211 | 231 | |
| 212 | - <details style="margin-bottom: 5px;"> | |
| 213 | - <summary style="cursor: pointer; padding: 10px; border: 1px solid #ddd; background: #f4f4f4; color: #000;"><strong><?php esc_html_e( 'Type' ); ?></strong></summary> | |
| 214 | - <?php | |
| 215 | - $list_types = array( | |
| 216 | - 'image/jpeg', | |
| 217 | - 'image/png', | |
| 218 | - 'image/bmp', | |
| 219 | - 'image/gif', | |
| 220 | - ); | |
| 221 | - foreach ( $list_types as $value ) { | |
| 222 | - if ( in_array( $value, $pluswebp_settings['types'] ) ) { | |
| 223 | - $check = 1; | |
| 224 | - } else { | |
| 225 | - $check = 0; | |
| 226 | - } | |
| 227 | - ?> | |
| 228 | - <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> | |
| 229 | - <?php | |
| 230 | - } | |
| 231 | - ?> | |
| 232 | - <div style="margin: 5px; padding: 5px;"> | |
| 233 | - <p class="description"> | |
| 234 | - <?php esc_html_e( 'Check the images you want to convert to WebP.', 'plus-webp' ); ?> | |
| 235 | - </p> | |
| 236 | - </div> | |
| 237 | - </details> | |
| 232 | + if ( $args['generate'] ) { | |
| 233 | + $metadata_org = wp_get_attachment_metadata( $attach_id ); | |
| 238 | 234 | |
| 239 | - <details style="margin-bottom: 5px;"> | |
| 240 | - <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> | |
| 241 | - <div style="margin: 5px; padding: 5px;"> | |
| 242 | - <input type="checkbox" name="addext" value="1" <?php checked( $pluswebp_settings['addext'], true ); ?>><?php esc_html_e( 'Apply' ); ?> | |
| 243 | - <div style="margin: 5px; padding: 5px;"> | |
| 244 | - <p class="description"> | |
| 245 | - <?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' ); ?> | |
| 246 | - </p> | |
| 247 | - </div> | |
| 248 | - </div> | |
| 249 | - </details> | |
| 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' ); | |
| 250 | 239 | |
| 251 | - <details style="margin-bottom: 5px;"> | |
| 252 | - <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> | |
| 253 | - <div style="margin: 5px; padding: 5px;"> | |
| 254 | - <input type="checkbox" name="replace" value="1" <?php checked( $pluswebp_settings['replace'], true ); ?>><?php esc_html_e( 'Apply' ); ?> | |
| 255 | - <div style="margin: 5px; padding: 5px;"> | |
| 256 | - <p class="description"> | |
| 257 | - <?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' ); ?> | |
| 258 | - </p> | |
| 259 | - </div> | |
| 260 | - </div> | |
| 261 | - </details> | |
| 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 | + } | |
| 262 | 252 | |
| 263 | - <?php submit_button( __( 'Save Changes' ), 'large', 'Manageset', false ); ?> | |
| 264 | - </form> | |
| 265 | - </div> | |
| 253 | + return new WP_REST_Response( $args, 200 ); | |
| 254 | + } | |
| 266 | 255 | |
| 267 | - </div> | |
| 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 ) { | |
| 268 | 263 | |
| 269 | - </div> | |
| 270 | - <?php | |
| 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 ); | |
| 271 | 283 | } |
| 272 | 284 | |
| 273 | 285 | /** ================================================== |
| 274 | - * Credit | |
| 286 | + * Credit for Gutenberg | |
| 275 | 287 | * |
| 276 | - * @since 1.00 | |
| 288 | + * @param string $handle handle. | |
| 289 | + * @since 4.00 | |
| 277 | 290 | */ |
| 278 | - private function credit() { | |
| 291 | + private function credit_gutenberg( $handle ) { | |
| 279 | 292 | |
| 280 | 293 | $plugin_name = null; |
| 281 | 294 | $plugin_ver_num = null; |
| 282 | 295 | $plugin_path = plugin_dir_path( __DIR__ ); |
| 283 | - $plugin_dir = untrailingslashit( $plugin_path ); | |
| 296 | + $plugin_dir = untrailingslashit( wp_normalize_path( $plugin_path ) ); | |
| 284 | 297 | $slugs = explode( '/', $plugin_dir ); |
| 285 | 298 | $slug = end( $slugs ); |
| 286 | 299 | $files = scandir( $plugin_dir ); |
| 287 | 300 | foreach ( $files as $file ) { |
| @@ -305,186 +318,68 @@ | ||
| 305 | 318 | } |
| 306 | 319 | } |
| 307 | 320 | } |
| 308 | 321 | } |
| 309 | - $plugin_version = __( 'Version:' ) . ' ' . $plugin_ver_num; | |
| 310 | - /* translators: FAQ Link & Slug */ | |
| 311 | - $faq = sprintf( esc_html__( 'https://wordpress.org/plugins/%s/faq', '%s' ), $slug ); | |
| 312 | - $support = 'https://wordpress.org/support/plugin/' . $slug; | |
| 313 | - $review = 'https://wordpress.org/support/view/plugin-reviews/' . $slug; | |
| 314 | - $translate = 'https://translate.wordpress.org/projects/wp-plugins/' . $slug; | |
| 315 | - $facebook = 'https://www.facebook.com/katsushikawamori/'; | |
| 316 | - $twitter = 'https://twitter.com/dodesyo312'; | |
| 317 | - $youtube = 'https://www.youtube.com/channel/UC5zTLeyROkvZm86OgNRcb_w'; | |
| 318 | - $donate = sprintf( esc_html__( 'https://shop.riverforest-wp.info/donate/', '%s' ), $slug ); | |
| 319 | 322 | |
| 320 | - ?> | |
| 321 | - <span style="font-weight: bold;"> | |
| 322 | - <div> | |
| 323 | - <?php echo esc_html( $plugin_version ); ?> | | |
| 324 | - <a style="text-decoration: none;" href="<?php echo esc_url( $faq ); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'FAQ' ); ?></a> | <a style="text-decoration: none;" href="<?php echo esc_url( $support ); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Support Forums' ); ?></a> | <a style="text-decoration: none;" href="<?php echo esc_url( $review ); ?>" target="_blank" rel="noopener noreferrer"><?php sprintf( esc_html_e( 'Reviews', '%s' ), $slug ); ?></a> | |
| 325 | - </div> | |
| 326 | - <div> | |
| 327 | - <a style="text-decoration: none;" href="<?php echo esc_url( $translate ); ?>" target="_blank" rel="noopener noreferrer"> | |
| 328 | - <?php | |
| 329 | - /* translators: Plugin translation link */ | |
| 330 | - echo sprintf( esc_html__( 'Translations for %s' ), esc_html( $plugin_name ) ); | |
| 331 | - ?> | |
| 332 | - </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> | |
| 333 | - </div> | |
| 334 | - </span> | |
| 335 | - | |
| 336 | - <div style="width: 250px; height: 180px; margin: 5px; padding: 5px; border: #CCC 2px solid;"> | |
| 337 | - <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> | |
| 338 | - <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> | |
| 339 | - <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> | |
| 340 | - </div> | |
| 341 | - | |
| 342 | - <?php | |
| 343 | - | |
| 344 | - } | |
| 345 | - | |
| 346 | - /** ================================================== | |
| 347 | - * Get All Images | |
| 348 | - * | |
| 349 | - * @since 2.00 | |
| 350 | - */ | |
| 351 | - private function get_allimages() { | |
| 352 | - | |
| 353 | - $pluswebp_settings = get_option( 'pluswebp' ); | |
| 354 | - | |
| 355 | - global $wpdb; | |
| 356 | - | |
| 357 | - $mime_types = null; | |
| 358 | - foreach ( $pluswebp_settings['types'] as $value ) { | |
| 359 | - $mime_types .= "'" . $value . "'" . ','; | |
| 360 | - } | |
| 361 | - $mime_types = rtrim( $mime_types, ',' ); | |
| 362 | - | |
| 363 | - $wpdb->plus_webp_mime_type = 'AND post_mime_type IN (' . $mime_types . ')'; | |
| 364 | - | |
| 365 | - $post_ids = $wpdb->get_col( | |
| 366 | - " | |
| 367 | - SELECT ID | |
| 368 | - FROM $wpdb->posts | |
| 369 | - WHERE post_type = 'attachment' | |
| 370 | - $wpdb->plus_webp_mime_type | |
| 371 | - AND post_status = 'inherit' | |
| 372 | - " | |
| 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 »' ), | |
| 342 | + ) | |
| 373 | 343 | ); |
| 374 | - | |
| 375 | - return $post_ids; | |
| 376 | - | |
| 377 | 344 | } |
| 378 | 345 | |
| 379 | 346 | /** ================================================== |
| 380 | - * Regenerate All Images | |
| 347 | + * Settings register | |
| 381 | 348 | * |
| 382 | - * @param string $to mail address. | |
| 383 | 349 | * @since 1.00 |
| 384 | 350 | */ |
| 385 | - public function allimages_regenerate( $to ) { | |
| 351 | + public function register_settings() { | |
| 386 | 352 | |
| 387 | - $post_ids = $this->get_allimages(); | |
| 388 | - | |
| 389 | - if ( ! empty( $post_ids ) ) { | |
| 390 | - delete_option( 'pluswebp_generate' ); | |
| 391 | - include_once ABSPATH . 'wp-admin/includes/image.php'; | |
| 392 | - /* Generate WebP */ | |
| 393 | - foreach ( $post_ids as $attach_id ) { | |
| 394 | - $metadata = wp_get_attachment_metadata( $attach_id ); | |
| 395 | - do_action( 'wp_generate_attachment_metadata', $metadata, $attach_id ); | |
| 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 ); | |
| 396 | 364 | } |
| 397 | - /* Mail send */ | |
| 398 | - do_action( 'pluswebp_mail_send', $to ); | |
| 399 | - } else { | |
| 400 | - ?> | |
| 401 | - <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>'; | |
| 402 | - <?php | |
| 403 | - } | |
| 404 | - | |
| 405 | - } | |
| 406 | - | |
| 407 | - /** ================================================== | |
| 408 | - * Update wp_options table. | |
| 409 | - * | |
| 410 | - * @since 1.02 | |
| 411 | - */ | |
| 412 | - private function options_updated() { | |
| 413 | - | |
| 414 | - if ( isset( $_POST['Manageset'] ) && ! empty( $_POST['Manageset'] ) ) { | |
| 415 | - if ( check_admin_referer( 'pwp_set', 'pluswebp_set' ) ) { | |
| 416 | - $pluswebp_settings = get_option( 'pluswebp' ); | |
| 417 | - $list_types = array(); | |
| 418 | - if ( isset( $_POST['list_types'] ) && ! empty( $_POST['list_types'] ) ) { | |
| 419 | - $tmps = filter_var( | |
| 420 | - wp_unslash( $_POST['list_types'] ), | |
| 421 | - FILTER_CALLBACK, | |
| 422 | - [ | |
| 423 | - 'options' => function( $value ) { | |
| 424 | - return sanitize_text_field( $value ); | |
| 425 | - }, | |
| 426 | - ] | |
| 427 | - ); | |
| 428 | - foreach ( $tmps as $key => $value ) { | |
| 429 | - $list_types[] = $key; | |
| 430 | - } | |
| 431 | - $pluswebp_settings['types'] = $list_types; | |
| 432 | - } else { | |
| 433 | - $pluswebp_settings['types'] = $list_types; | |
| 434 | - } | |
| 435 | - if ( isset( $_POST['addext'] ) && ! empty( $_POST['addext'] ) ) { | |
| 436 | - $pluswebp_settings['addext'] = true; | |
| 437 | - } else { | |
| 438 | - $pluswebp_settings['addext'] = false; | |
| 439 | - } | |
| 440 | - if ( isset( $_POST['replace'] ) && ! empty( $_POST['replace'] ) ) { | |
| 441 | - $pluswebp_settings['replace'] = true; | |
| 442 | - } else { | |
| 443 | - $pluswebp_settings['replace'] = false; | |
| 444 | - } | |
| 445 | - if ( isset( $_POST['quality'] ) ) { | |
| 446 | - $pluswebp_settings['quality'] = intval( $_POST['quality'] ); | |
| 447 | - } | |
| 365 | + /* 'addext' from ver 1.13 */ | |
| 366 | + if ( ! array_key_exists( 'addext', $pluswebp_settings ) ) { | |
| 367 | + $pluswebp_settings['addext'] = false; | |
| 448 | 368 | update_option( 'pluswebp', $pluswebp_settings ); |
| 449 | - echo '<div class="notice notice-success is-dismissible"><ul><li>' . esc_html__( 'Settings' ) . ' --> ' . esc_html__( 'Settings saved.' ) . '</li></ul></div>'; | |
| 450 | 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 ); | |
| 451 | 383 | } |
| 452 | - | |
| 453 | 384 | } |
| 454 | - | |
| 455 | - /** ================================================== | |
| 456 | - * Generate notice | |
| 457 | - * | |
| 458 | - * @since 1.09 | |
| 459 | - */ | |
| 460 | - public function generate_notice() { | |
| 461 | - | |
| 462 | - if ( get_option( 'pluswebp_generate_mail' ) ) { | |
| 463 | - $pluswebp_mail_send = get_option( 'pluswebp_generate_mail' ); | |
| 464 | - if ( 0 < $pluswebp_mail_send['count'] ) { | |
| 465 | - ?> | |
| 466 | - <div class="notice notice-success is-dismissible"><ul><li><strong>Plus WebP</strong> | |
| 467 | - <?php | |
| 468 | - /* translators: %1$s Date Time, %2$d File Count */ | |
| 469 | - 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'] ) ); | |
| 470 | - ?> | |
| 471 | - </li></ul></div> | |
| 472 | - <?php | |
| 473 | - } else { | |
| 474 | - ?> | |
| 475 | - <div class="notice notice-error is-dismissible"><ul><li><strong>Plus WebP</strong> | |
| 476 | - <?php | |
| 477 | - /* translators: %1$s Date Time %2$s error message */ | |
| 478 | - 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' ) ) ); | |
| 479 | - ?> | |
| 480 | - </li></ul></div> | |
| 481 | - <?php | |
| 482 | - } | |
| 483 | - delete_option( 'pluswebp_generate_mail' ); | |
| 484 | - } | |
| 485 | - | |
| 486 | - } | |
| 487 | - | |
| 488 | 385 | } |
| 489 | - | |
| 490 | - | |