optimization.php
72 lines
| 1 | <?php |
| 2 | |
| 3 | // Exit if accessed directly |
| 4 | if ( ! defined( 'ABSPATH' ) ) { |
| 5 | exit; |
| 6 | } |
| 7 | |
| 8 | /** |
| 9 | * Переоптимизация аттачмента |
| 10 | */ |
| 11 | function wbcr_riop_reoptimizeImage() { |
| 12 | if ( ! check_ajax_referer( 'reoptimize', false, false ) || ! current_user_can( 'manage_options' ) ) { |
| 13 | wp_send_json_error( [ 'message' => 'Unauthorized' ], 403 ); |
| 14 | } |
| 15 | |
| 16 | $image_id = (int) $_POST['id']; |
| 17 | $backup = WRIOP_Backup::get_instance(); |
| 18 | $backup_origin_images = WRIO_Plugin::app()->getPopulateOption( 'backup_origin_images', false ); |
| 19 | $nextgen_gallery = WRIO_Nextgen_Gallery::get_instance(); |
| 20 | if ( $backup_origin_images && ! $backup->isBackupWritable() ) { |
| 21 | echo $nextgen_gallery->getMediaColumnContent( $image_id ); |
| 22 | die(); |
| 23 | } |
| 24 | wp_suspend_cache_addition( true ); |
| 25 | $default_level = WRIO_Plugin::app()->getPopulateOption( 'image_optimization_level', 'normal' ); |
| 26 | $level = isset( $_POST['level'] ) ? sanitize_text_field( $_POST['level'] ) : $default_level; |
| 27 | |
| 28 | $optimized_data = $nextgen_gallery->optimizeNextgenImage( $image_id, $level ); |
| 29 | |
| 30 | if ( $optimized_data && isset( $optimized_data['processing'] ) ) { |
| 31 | echo 'processing'; |
| 32 | die(); |
| 33 | } |
| 34 | |
| 35 | echo $nextgen_gallery->getMediaColumnContent( $image_id ); |
| 36 | die(); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Восстановление |
| 41 | */ |
| 42 | function wbcr_riop_restoreImage() { |
| 43 | if ( ! check_ajax_referer( 'restore', false, false ) || ! current_user_can( 'manage_options' ) ) { |
| 44 | wp_send_json_error( [ 'message' => 'Unauthorized' ], 403 ); |
| 45 | } |
| 46 | |
| 47 | wp_suspend_cache_addition( true ); |
| 48 | $image_id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0; |
| 49 | |
| 50 | $nextgen_gallery = WRIO_Nextgen_Gallery::get_instance(); |
| 51 | $nextgen_image = $nextgen_gallery->getNextgenImage( $image_id ); |
| 52 | $image_statistics = WRIO_Image_Statistic_Nextgen::get_instance(); |
| 53 | if ( $nextgen_image->isOptimized() ) { |
| 54 | $restored = $nextgen_image->restore(); |
| 55 | |
| 56 | if ( ! is_wp_error( $restored ) ) { |
| 57 | $optimization_data = $nextgen_image->getOptimizationData(); |
| 58 | $optimized_size = $optimization_data->get_final_size(); |
| 59 | $original_size = $optimization_data->get_original_size(); |
| 60 | $webp_optimized_size = $optimization_data->get_extra_data()->get_webp_main_size(); |
| 61 | $image_statistics->deductFromField( 'webp_optimized_size', $webp_optimized_size ); |
| 62 | $image_statistics->deductFromField( 'optimized_size', $optimized_size ); |
| 63 | $image_statistics->deductFromField( 'original_size', $original_size ); |
| 64 | $image_statistics->save(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | $nextgen_gallery = WRIO_Nextgen_Gallery::get_instance(); |
| 69 | echo $nextgen_gallery->getMediaColumnContent( $image_id ); |
| 70 | die(); |
| 71 | } |
| 72 |