| 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 |
]; |
| 44 |
$settings = new Optml_Settings(); |
| 45 |
if ( $settings->get( 'offload_media' ) === 'disabled' ) { |
| 46 |
return \WP_CLI::error( __( 'You need to have the offload_media option enabled in order to use this command', 'optimole-wp' ) ); |
| 47 |
} |
| 48 |
WP_CLI::line( $strings[ $action ]['info'] ); |
| 49 |
$number_of_images_for = 'offload_images'; |
| 50 |
if ( $action === 'rollback' ) { |
| 51 |
$number_of_images_for = 'rollback_images'; |
| 52 |
} |
| 53 |
$number_of_images = Optml_Media_Offload::number_of_images_and_pages( $number_of_images_for ); |
| 54 |
$batch = 5; |
| 55 |
$possible_batch = ceil( $number_of_images / 10 ); |
| 56 |
if ( $possible_batch < $batch ) { |
| 57 |
$batch = $possible_batch; |
| 58 |
} |
| 59 |
$total_progress = ceil( $number_of_images / $batch ); |
| 60 |
$progress = \WP_CLI\Utils\make_progress_bar( __( 'Progress bar', 'optimole-wp' ), (int) $total_progress ); |
| 61 |
$tick = 0; |
| 62 |
$page = 1; |
| 63 |
while ( $tick < $total_progress ) { |
| 64 |
$posts_to_update = $action === 'offload' ? [] : Optml_Media_Offload::instance()->update_content( $page, $number_of_images_for, $batch ); |
| 65 |
|
| 66 |
// Kept for backward compatibility with old offloading mechanism where pages were modified. |
| 67 |
if ( $action === 'rollback' && isset( $posts_to_update['page'] ) && $posts_to_update['page'] > $page ) { |
| 68 |
$page = $posts_to_update['page']; |
| 69 |
if ( isset( $posts_to_update['imagesToUpdate'] ) && count( $posts_to_update['imagesToUpdate'] ) ) { |
| 70 |
foreach ( $posts_to_update['imagesToUpdate'] as $post_id => $images ) { |
| 71 |
Optml_Media_Offload::instance()->rollback_and_update_images( $images ); |
| 72 |
Optml_Media_Offload::instance()->update_page( $post_id ); |
| 73 |
} |
| 74 |
} |
| 75 |
} else { |
| 76 |
$action === 'rollback' ? Optml_Media_Offload::instance()->rollback_images( $batch ) : Optml_Media_Offload::instance()->upload_images( $batch ); |
| 77 |
} |
| 78 |
$progress->tick(); |
| 79 |
$tick++; |
| 80 |
} |
| 81 |
$progress->finish(); |
| 82 |
WP_CLI::line( $strings[ $action ]['success'] ); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|