| @@ -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 |
| @@ -141,13 +137,32 @@ | ||
| 141 | 137 | ); |
| 142 | 138 | |
| 143 | 139 | $pluswebp_settings = get_option( 'pluswebp' ); |
| 144 | 140 | |
| 141 | + list( $post_ids, $no_file_ids ) = apply_filters( 'pluswebp_get_allimages', $pluswebp_settings['types'], $pluswebp_settings['output_mime'] ); | |
| 142 | + | |
| 143 | + $non_generate_description = null; | |
| 144 | + if ( ! empty( $no_file_ids ) ) { | |
| 145 | + /* translators: Count of no file Media */ | |
| 146 | + $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 ) ); | |
| 147 | + } | |
| 148 | + | |
| 145 | 149 | wp_localize_script( |
| 146 | 150 | 'plus-webp', |
| 151 | + 'pluswebpgenerate_data', | |
| 152 | + array( | |
| 153 | + /* translators: Count of Media */ | |
| 154 | + 'generate_description' => sprintf( __( 'Found %1$d media that can be generated.', 'plus-webp' ), count( $post_ids ) ), | |
| 155 | + 'non_generate_description' => $non_generate_description, | |
| 156 | + 'post_ids' => wp_json_encode( $post_ids, JSON_UNESCAPED_SLASHES ), | |
| 157 | + ) | |
| 158 | + ); | |
| 159 | + | |
| 160 | + wp_localize_script( | |
| 161 | + 'plus-webp', | |
| 147 | 162 | 'pluswebpsettings_data', |
| 148 | 163 | array( |
| 149 | - 'settings' => $pluswebp_settings, | |
| 164 | + 'settings' => wp_json_encode( $pluswebp_settings, JSON_UNESCAPED_SLASHES ), | |
| 150 | 165 | ) |
| 151 | 166 | ); |
| 152 | 167 | |
| 153 | 168 | wp_set_script_translations( 'plus-webp', 'plus-webp' ); |
| @@ -162,18 +177,8 @@ | ||
| 162 | 177 | */ |
| 163 | 178 | public function register_rest() { |
| 164 | 179 | |
| 165 | 180 | 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 | - ); | |
| 174 | - | |
| 175 | - register_rest_route( | |
| 176 | 181 | 'rf/plus-webp-generate_api', |
| 177 | 182 | '/token', |
| 178 | 183 | array( |
| 179 | 184 | 'methods' => 'POST', |
| @@ -203,41 +208,8 @@ | ||
| 203 | 208 | return current_user_can( 'manage_options' ); |
| 204 | 209 | } |
| 205 | 210 | |
| 206 | 211 | /** ================================================== |
| 207 | - * Rest API save for Confirm | |
| 208 | - * | |
| 209 | - * @param object $request changed data. | |
| 210 | - * @since 5.20 | |
| 211 | - */ | |
| 212 | - public function confirm_api_save( $request ) { | |
| 213 | - | |
| 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 | - update_option( 'test', $post_ids ); | |
| 223 | - $args['post_ids'] = $post_ids; | |
| 224 | - if ( ! empty( $post_ids ) ) { | |
| 225 | - /* translators: Count of file Media */ | |
| 226 | - $args['ids_desc'] = sprintf( __( 'Found %1$d media that can be generated.', 'plus-webp' ), count( $post_ids ) ); | |
| 227 | - } else { | |
| 228 | - $args['ids_desc'] = __( 'Cannot find any media that can be generated.', 'plus-webp' ); | |
| 229 | - } | |
| 230 | - if ( ! empty( $no_file_ids ) ) { | |
| 231 | - /* translators: Count of no file Media */ | |
| 232 | - $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 ) ); | |
| 233 | - } | |
| 234 | - } | |
| 235 | - | |
| 236 | - return new WP_REST_Response( $args, 200 ); | |
| 237 | - } | |
| 238 | - | |
| 239 | - /** ================================================== | |
| 240 | 212 | * Rest API save for Generate |
| 241 | 213 | * |
| 242 | 214 | * @param object $request changed data. |
| 243 | 215 | * @since 4.00 |
| @@ -243,18 +215,16 @@ | ||
| 243 | 215 | * @since 4.00 |
| 244 | 216 | */ |
| 245 | 217 | public function generate_api_save( $request ) { |
| 246 | 218 | |
| 247 | - $args = $request->get_json_params(); | |
| 219 | + $args = json_decode( $request->get_body(), true ); | |
| 248 | 220 | |
| 249 | 221 | $count = absint( $args['count'] ); |
| 250 | 222 | $max_count = absint( $args['max_count'] ); |
| 251 | 223 | $attach_id = absint( $args['post_id'] ); |
| 252 | 224 | $messages = get_option( 'pluswebp_messages', array() ); |
| 253 | - $sent_mail = boolval( $args['sent_mail'] ); | |
| 254 | - $stop = boolval( $args['stop'] ); | |
| 255 | 225 | |
| 256 | - if ( boolval( $args['generate'] ) ) { | |
| 226 | + if ( $args['generate'] ) { | |
| 257 | 227 | $metadata_org = wp_get_attachment_metadata( $attach_id ); |
| 258 | 228 | |
| 259 | 229 | delete_option( 'pluswebp_generate' ); |
| 260 | 230 | do_action( 'wp_generate_attachment_metadata', $metadata_org, $attach_id ); |
| @@ -265,17 +235,13 @@ | ||
| 265 | 235 | list( $tmp, $messages ) = apply_filters( 'pluswebp_mail_messages', $messages, $webp_id ); |
| 266 | 236 | update_option( 'pluswebp_messages', $messages ); |
| 267 | 237 | } |
| 268 | 238 | if ( $count === $max_count ) { |
| 269 | - if ( $sent_mail && ! empty( get_option( 'pluswebp_messages' ) ) ) { | |
| 270 | - do_action( 'pluswebp_mail_generate_message', get_option( 'pluswebp_messages' ), count( get_option( 'pluswebp_messages' ) ) ); | |
| 271 | - } | |
| 239 | + do_action( 'pluswebp_mail_generate_message', $messages, $max_count ); | |
| 272 | 240 | delete_option( 'pluswebp_messages' ); |
| 273 | 241 | } |
| 274 | - } else if ( ! boolval( $args['generate'] ) && $stop ) { | |
| 275 | - if ( $sent_mail && ! empty( get_option( 'pluswebp_messages' ) ) ) { | |
| 276 | - do_action( 'pluswebp_mail_generate_message', get_option( 'pluswebp_messages' ), count( get_option( 'pluswebp_messages' ) ) ); | |
| 277 | - } | |
| 242 | + } else { | |
| 243 | + do_action( 'pluswebp_mail_generate_message', $messages, count( $messages ) ); | |
| 278 | 244 | delete_option( 'pluswebp_messages' ); |
| 279 | 245 | } |
| 280 | 246 | |
| 281 | 247 | return new WP_REST_Response( $args, 200 ); |
| @@ -288,25 +254,15 @@ | ||
| 288 | 254 | * @since 4.00 |
| 289 | 255 | */ |
| 290 | 256 | public function settings_api_save( $request ) { |
| 291 | 257 | |
| 292 | - $args = $request->get_json_params(); | |
| 258 | + $args = json_decode( $request->get_body(), true ); | |
| 293 | 259 | |
| 294 | - if ( isset( $args['settings'] ) && is_array( $args['settings'] ) ) { | |
| 295 | - $settings = $args['settings']; | |
| 296 | - } else { | |
| 297 | - return new WP_Error( | |
| 298 | - 'invalid_settings', | |
| 299 | - 'Invalid settings.', | |
| 300 | - array( 'status' => 400 ) | |
| 301 | - ); | |
| 302 | - } | |
| 303 | - | |
| 304 | 260 | $options = array(); |
| 305 | - $options['output_mime'] = sanitize_text_field( wp_unslash( $settings['output_mime'] ) ); | |
| 306 | - $options['quality'] = intval( $settings['quality'] ); | |
| 261 | + $options['output_mime'] = sanitize_text_field( wp_unslash( $args['output_mime'] ) ); | |
| 262 | + $options['quality'] = intval( $args['quality'] ); | |
| 307 | 263 | $options['types'] = filter_var( |
| 308 | - wp_unslash( $settings['types'] ), | |
| 264 | + wp_unslash( $args['types'] ), | |
| 309 | 265 | FILTER_CALLBACK, |
| 310 | 266 | array( |
| 311 | 267 | 'options' => function ( $value ) { |
| 312 | 268 | return sanitize_text_field( $value ); |
| @@ -312,10 +268,10 @@ | ||
| 312 | 268 | return sanitize_text_field( $value ); |
| 313 | 269 | }, |
| 314 | 270 | ) |
| 315 | 271 | ); |
| 316 | - $options['addext'] = boolval( $settings['addext'] ); | |
| 317 | - $options['replace'] = boolval( $settings['replace'] ); | |
| 272 | + $options['addext'] = boolval( $args['addext'] ); | |
| 273 | + $options['replace'] = boolval( $args['replace'] ); | |
| 318 | 274 | |
| 319 | 275 | update_option( 'pluswebp', $options ); |
| 320 | 276 | |
| 321 | 277 | return new WP_REST_Response( $args, 200 ); |