| 1 |
<?php |
| 2 |
/** |
| 3 |
* CLI commands responsible for the Optimole media. |
| 4 |
*/ |
| 5 |
|
| 6 |
if ( ! class_exists( 'WP_CLI' ) ) { |
| 7 |
return; |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* Class Optml_Cli_Media |
| 12 |
*/ |
| 13 |
class Optml_Cli_Media extends WP_CLI_Command { |
| 14 |
/** |
| 15 |
* Move all existing images to our servers. |
| 16 |
*/ |
| 17 |
public function move_to_optimole() { |
| 18 |
$this->update_images_template( 'offload' ); |
| 19 |
} |
| 20 |
/** |
| 21 |
* Move all existing images from our servers to your media library. |
| 22 |
*/ |
| 23 |
public function rollback_images() { |
| 24 |
$this->update_images_template( 'rollback' ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Template for bulk image processing to avoid duplicate code. |
| 29 |
* |
| 30 |
* @param string $action The action to perform rollback/offload. |
| 31 |
* @return mixed WP_CLI::error If it fails. |
| 32 |
*/ |
| 33 |
private function update_images_template( $action ) { |
| 34 |
$strings = [ |
| 35 |
'offload' => [ |
| 36 |
'info' => __( 'Moving all images to Optimole Cloud', 'optimole-wp' ), |
| 37 |
'success' => __( 'All images have been uploaded to Optimole Cloud', 'optimole-wp' ), |
| 38 |
], |
| 39 |
'rollback' => [ |
| 40 |
'info' => __( 'Moving all images back to your media library', 'optimole-wp' ), |
| 41 |
'success' => __( 'All images have been uploaded to your media library', 'optimole-wp' ), |
| 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' ), |
| 44 |
]; |
| 45 |
$settings = new Optml_Settings(); |
| 46 |
if ( $settings->get( 'offload_media' ) === 'disabled' ) { |
| 47 |
return \WP_CLI::error( __( 'You need to have the offload_media option enabled in order to use this command', 'optimole-wp' ) ); |
| 48 |
} |
| 49 |
|
| 50 |
Optml_Media_Offload::clear_offload_errors_meta(); |
| 51 |
if ( $action === 'rollback' ) { |
| 52 |
Optml_Media_Offload::clear_rollback_errors_meta(); |
| 53 |
} |
| 54 |
WP_CLI::line( $strings[ $action ]['info'] ); |
| 55 |
$number_of_images_for = 'offload_images'; |
| 56 |
if ( $action === 'rollback' ) { |
| 57 |
$number_of_images_for = 'rollback_images'; |
| 58 |
} |
| 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 ); |
| 61 |
$batch = 5; |
| 62 |
if ( $number_of_images === 0 ) { |
| 63 |
WP_CLI::success( $strings[ $action ]['success'] ); |
| 64 |
|
| 65 |
return; |
| 66 |
} |
| 67 |
$possible_batch = ceil( $number_of_images / 10 ); |
| 68 |
if ( $possible_batch < $batch ) { |
| 69 |
$batch = $possible_batch; |
| 70 |
} |
| 71 |
$total_progress = ceil( $number_of_images / $batch ); |
| 72 |
$progress = \WP_CLI\Utils\make_progress_bar( __( 'Progress bar', 'optimole-wp' ), (int) $total_progress ); |
| 73 |
$tick = 0; |
| 74 |
$page = 1; |
| 75 |
while ( $tick < $total_progress ) { |
| 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 |
|
| 95 |
$progress->tick(); |
| 96 |
++$tick; |
| 97 |
} |
| 98 |
$progress->finish(); |
| 99 |
WP_CLI::line( $strings[ $action ]['success'] ); |
| 100 |
} |
| 101 |
} |
| 102 |
|