| 1 |
<?php |
| 2 |
/** |
| 3 |
* WP CLI functionality |
| 4 |
* |
| 5 |
* @link https://vcore.au |
| 6 |
* |
| 7 |
* @package CF_Images |
| 8 |
* @subpackage CF_Images/App |
| 9 |
* @author Anton Vanyukov <a.vanyukov@vcore.ru> |
| 10 |
* @since 1.5.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace CF_Images\App; |
| 14 |
|
| 15 |
use CF_Images\App\Traits\Ajax; |
| 16 |
use CF_Images\App\Traits\Helpers; |
| 17 |
use WP_CLI; |
| 18 |
use WP_CLI_Command; |
| 19 |
use WP_Query; |
| 20 |
|
| 21 |
if ( ! defined( 'WPINC' ) ) { |
| 22 |
die; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Offload images to Cloudflare Images service. |
| 27 |
* |
| 28 |
* @since 1.5.0 |
| 29 |
*/ |
| 30 |
class CLI extends WP_CLI_Command { |
| 31 |
use Ajax; |
| 32 |
use Helpers; |
| 33 |
|
| 34 |
/** |
| 35 |
* Offload images to Cloudflare Images. |
| 36 |
* |
| 37 |
* ## OPTIONS |
| 38 |
* |
| 39 |
* [--id=<ID>] |
| 40 |
* : Attachment ID to offload. |
| 41 |
* --- |
| 42 |
* default: 0 |
| 43 |
* --- |
| 44 |
* |
| 45 |
* ## EXAMPLES |
| 46 |
* |
| 47 |
* # Offload all images. |
| 48 |
* $ wp cf-images offload |
| 49 |
* |
| 50 |
* # Offload single image, attachment ID = 10. |
| 51 |
* $ wp cf-images offload --id=10 |
| 52 |
* |
| 53 |
* @param array $args Positional arguments. |
| 54 |
* @param array $assoc_args Arguments, defined like --key=value or --flag or --no-flag. |
| 55 |
*/ |
| 56 |
public function offload( $args, $assoc_args ) { |
| 57 |
if ( ! $this->is_set_up() ) { |
| 58 |
WP_CLI::error( __( 'Plugin is not connected to Cloudflare.', 'cf-images' ) ); |
| 59 |
} |
| 60 |
|
| 61 |
$attachment_id = (int) $assoc_args['id']; |
| 62 |
|
| 63 |
if ( $attachment_id && $attachment_id > 0 ) { |
| 64 |
$this->offload_single( $attachment_id ); |
| 65 |
} |
| 66 |
|
| 67 |
$this->offload_all(); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Offload single image. |
| 72 |
* |
| 73 |
* @since 1.5.0 |
| 74 |
* |
| 75 |
* @param int $attachment_id Attachment ID. |
| 76 |
*/ |
| 77 |
private function offload_single( int $attachment_id ) { |
| 78 |
WP_CLI::line( |
| 79 |
sprintf( /* translators: %d - attachment ID */ |
| 80 |
esc_html__( 'Offloading image with ID %d.', 'cf-images' ), |
| 81 |
$attachment_id |
| 82 |
) |
| 83 |
); |
| 84 |
|
| 85 |
$metadata = wp_get_attachment_metadata( $attachment_id ); |
| 86 |
if ( false === $metadata ) { |
| 87 |
WP_CLI::error( __( 'Image metadata not found.', 'cf-images' ) ); |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
( new Media() )->upload_image( $metadata, $attachment_id ); |
| 92 |
|
| 93 |
if ( is_wp_error( Core::get_error() ) ) { |
| 94 |
WP_CLI::error( Core::get_error()->get_error_message() ); |
| 95 |
} else { |
| 96 |
WP_CLI::success( __( 'Image offloaded.', 'cf-images' ) ); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Offload all images. |
| 102 |
* |
| 103 |
* @since 1.5.0 |
| 104 |
*/ |
| 105 |
private function offload_all() { |
| 106 |
$defaults = array( |
| 107 |
'fields' => 'ids', |
| 108 |
'no_found_rows' => true, |
| 109 |
'posts_per_page' => -1, |
| 110 |
'update_post_meta_cache' => false, |
| 111 |
'update_post_term_cache' => false, |
| 112 |
); |
| 113 |
|
| 114 |
$args = $this->get_wp_query_args( 'upload' ); |
| 115 |
$args = wp_parse_args( $args, $defaults ); |
| 116 |
|
| 117 |
// Look for images that have been offloaded. |
| 118 |
$images = new WP_Query( $args ); |
| 119 |
|
| 120 |
if ( ! $images->have_posts() ) { |
| 121 |
WP_CLI::success( __( 'All images have already been offloaded.', 'cf-images' ) ); |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
$errors = array(); |
| 126 |
$progress = WP_CLI\Utils\make_progress_bar( __( 'Offloading images', 'cf-images' ), $images->found_posts ); |
| 127 |
|
| 128 |
foreach ( $images->posts as $attachment ) { |
| 129 |
$post = get_post( $attachment ); |
| 130 |
|
| 131 |
/** This filter is documented in app/class-media.php */ |
| 132 |
$handled = apply_filters( 'cf_images_bulk_process_post', false, $post, 'upload' ); |
| 133 |
|
| 134 |
if ( ! $handled ) { |
| 135 |
$metadata = wp_get_attachment_metadata( $attachment ); |
| 136 |
if ( false === $metadata ) { |
| 137 |
$errors[] = sprintf( /* translators: %d - attachment ID */ |
| 138 |
esc_html__( 'Image metadata not found (attachment ID: %d).', 'cf-images' ), |
| 139 |
$attachment |
| 140 |
); |
| 141 |
} else { |
| 142 |
( new Media() )->upload_image( $metadata, $attachment ); |
| 143 |
|
| 144 |
if ( is_wp_error( Core::get_error() ) ) { |
| 145 |
$errors[] = sprintf( /* translators: %1$s - error message, %2$d - attachment ID */ |
| 146 |
esc_html__( '%1$s (attachment ID: %2$d).', 'cf-images' ), |
| 147 |
esc_html( Core::get_error()->get_error_message() ), |
| 148 |
$attachment |
| 149 |
); |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
$progress->tick(); |
| 155 |
} |
| 156 |
|
| 157 |
$progress->finish(); |
| 158 |
|
| 159 |
if ( empty( $errors ) ) { |
| 160 |
WP_CLI::success( __( 'All images offloaded', 'cf-images' ) ); |
| 161 |
} else { |
| 162 |
foreach ( $errors as $error ) { |
| 163 |
WP_CLI::error( $error ); |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|