| @@ -27,45 +27,75 @@ | ||
| 27 | 27 | /** |
| 28 | 28 | * Template for bulk image processing to avoid duplicate code. |
| 29 | 29 | * |
| 30 | 30 | * @param string $action The action to perform rollback/offload. |
| 31 | - * @return WP_CLI::error If it fails. | |
| 31 | + * @return mixed WP_CLI::error If it fails. | |
| 32 | 32 | */ |
| 33 | 33 | private function update_images_template( $action ) { |
| 34 | 34 | $strings = [ |
| 35 | 35 | 'offload' => [ |
| 36 | - 'info' => __( 'Moving all images to our servers', 'optimole-wp' ), | |
| 37 | - 'success' => __( 'All images have been uploaded to our servers', 'optimole-wp' ), | |
| 36 | + 'info' => __( 'Moving all images to Optimole Cloud', 'optimole-wp' ), | |
| 37 | + 'success' => __( 'All images have been uploaded to Optimole Cloud', 'optimole-wp' ), | |
| 38 | 38 | ], |
| 39 | 39 | 'rollback' => [ |
| 40 | 40 | 'info' => __( 'Moving all images back to your media library', 'optimole-wp' ), |
| 41 | 41 | 'success' => __( 'All images have been uploaded to your media library', 'optimole-wp' ), |
| 42 | 42 | ], |
| 43 | + 'limit_exceeded' => __( 'You have reached the maximum offloading limit of images. To increase the offload limit and for more information, contact our support.', 'optimole-wp' ), | |
| 43 | 44 | ]; |
| 44 | 45 | $settings = new Optml_Settings(); |
| 45 | 46 | if ( $settings->get( 'offload_media' ) === 'disabled' ) { |
| 46 | 47 | return \WP_CLI::error( __( 'You need to have the offload_media option enabled in order to use this command', 'optimole-wp' ) ); |
| 47 | 48 | } |
| 49 | + | |
| 50 | + Optml_Media_Offload::clear_offload_errors_meta(); | |
| 51 | + if ( $action === 'rollback' ) { | |
| 52 | + Optml_Media_Offload::clear_rollback_errors_meta(); | |
| 53 | + } | |
| 48 | 54 | WP_CLI::line( $strings[ $action ]['info'] ); |
| 49 | 55 | $number_of_images_for = 'offload_images'; |
| 50 | 56 | if ( $action === 'rollback' ) { |
| 51 | 57 | $number_of_images_for = 'rollback_images'; |
| 52 | 58 | } |
| 53 | - $number_of_images = Optml_Media_Offload::number_of_library_images( $number_of_images_for ); | |
| 59 | + $number_of_images = Optml_Media_Offload::number_of_images_and_pages( $number_of_images_for ); | |
| 60 | + Optml_Media_Offload::record_process_meta( $number_of_images ); | |
| 54 | 61 | $batch = 5; |
| 62 | + if ( $number_of_images === 0 ) { | |
| 63 | + WP_CLI::success( $strings[ $action ]['success'] ); | |
| 64 | + | |
| 65 | + return; | |
| 66 | + } | |
| 55 | 67 | $possible_batch = ceil( $number_of_images / 10 ); |
| 56 | 68 | if ( $possible_batch < $batch ) { |
| 57 | 69 | $batch = $possible_batch; |
| 58 | 70 | } |
| 59 | 71 | $total_progress = ceil( $number_of_images / $batch ); |
| 60 | - $progress = \WP_CLI\Utils\make_progress_bar( __( 'Progress bar', 'optimole-wp' ), $total_progress ); | |
| 72 | + $progress = \WP_CLI\Utils\make_progress_bar( __( 'Progress bar', 'optimole-wp' ), (int) $total_progress ); | |
| 61 | 73 | $tick = 0; |
| 74 | + $page = 1; | |
| 62 | 75 | while ( $tick < $total_progress ) { |
| 63 | - $action === 'rollback' ? Optml_Media_Offload::rollback_images( $batch ) : Optml_Media_Offload::upload_images( $batch ); | |
| 76 | + $posts_to_update = $action === 'offload' ? [] : Optml_Media_Offload::instance()->update_content( $page, $number_of_images_for, $batch ); | |
| 77 | + | |
| 78 | + // Kept for backward compatibility with old offloading mechanism where pages were modified. | |
| 79 | + if ( $action === 'rollback' && isset( $posts_to_update['page'] ) && $posts_to_update['page'] > $page ) { | |
| 80 | + $page = $posts_to_update['page']; | |
| 81 | + if ( isset( $posts_to_update['imagesToUpdate'] ) && count( $posts_to_update['imagesToUpdate'] ) ) { | |
| 82 | + foreach ( $posts_to_update['imagesToUpdate'] as $post_id => $images ) { | |
| 83 | + Optml_Media_Offload::instance()->rollback_and_update_images( $images ); | |
| 84 | + Optml_Media_Offload::instance()->update_page( $post_id ); | |
| 85 | + } | |
| 86 | + } | |
| 87 | + } else { | |
| 88 | + $action === 'rollback' ? Optml_Media_Offload::instance()->rollback_images( $batch ) : Optml_Media_Offload::instance()->upload_images( $batch ); | |
| 89 | + } | |
| 90 | + | |
| 91 | + if ( Optml_Media_Offload::instance()->settings->is_offload_limit_reached() ) { | |
| 92 | + WP_CLI::error( $strings['limit_exceeded'], true ); | |
| 93 | + } | |
| 94 | + | |
| 64 | 95 | $progress->tick(); |
| 65 | - $tick++; | |
| 96 | + ++$tick; | |
| 66 | 97 | } |
| 67 | 98 | $progress->finish(); |
| 68 | 99 | WP_CLI::line( $strings[ $action ]['success'] ); |
| 69 | 100 | } |
| 70 | 101 | } |
| 71 | - | |