Skip to content

Commit 1668424

Browse files
committed
Add in WP CLI sync tools
1 parent e56af00 commit 1668424

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

instance.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,8 @@ function get_plugin_instance() {
2525

2626
return $cloudinary_plugin;
2727
}
28+
29+
if ( defined( 'WP_CLI' ) && WP_CLI ) {
30+
$instance = new CLI( get_plugin_instance() );
31+
\WP_CLI::add_command( 'cloudinary', $instance );
32+
}

php/class-cli.php

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
/**
3+
* Cloudinary CLI.
4+
*
5+
* @package Cloudinary
6+
*/
7+
8+
namespace Cloudinary;
9+
10+
/**
11+
* CLI class.
12+
*/
13+
class CLI {
14+
15+
/**
16+
* Holds the plugin instance.
17+
*
18+
* @since 0.1
19+
*
20+
* @var Plugin Instance of the global plugin.
21+
*/
22+
public $plugin;
23+
24+
/**
25+
* CLI constructor.
26+
*
27+
* @param \Cloudinary\Plugin $plugin The plugin instance.
28+
*/
29+
public function __construct( $plugin ) {
30+
$this->plugin = $plugin;
31+
32+
}
33+
34+
/**
35+
* Syncs assets with Cloudinary.
36+
*
37+
* ## OPTIONS
38+
* [--assets=<assets>]
39+
* : Optional list of assets, comma separated.
40+
*
41+
* ## EXAMPLES
42+
*
43+
* wp cloudinary sync
44+
*
45+
* @when after_wp_load
46+
*
47+
* @param array $args Ignored.
48+
* @param array $assoc_args Passed parameters.
49+
*/
50+
public function sync( $args, $assoc_args ) {
51+
52+
\WP_CLI::log( '' );
53+
\WP_CLI::log( '╔═╗┬ ┌─┐┬ ┬┌┬┐┬┌┐┌┌─┐┬─┐┬ ┬ ╔═╗╦ ╦' );
54+
\WP_CLI::log( '║ │ │ ││ │ ││││││├─┤├┬┘└┬┘ ║ ║ ║' );
55+
\WP_CLI::log( '╚═╝┴─┘└─┘└─┘─┴┘┴┘└┘┴ ┴┴└─ ┴ ╚═╝╩═╝╩' );
56+
57+
$assets = null;
58+
if ( ! empty( $assoc_args['assets'] ) ) {
59+
$assets = explode( ',', $assoc_args['assets'] );
60+
$assets = array_map( 'trim', $assets );
61+
}
62+
$assets = $this->get_all_unsynced( $assets );
63+
64+
if ( empty( $assets ) ) {
65+
return;
66+
}
67+
$total = count( $assets );
68+
\WP_CLI::log( '' );
69+
\WP_CLI::log( \WP_CLI::colorize( '%gStarting Sync:%n' ) );
70+
$bar = \WP_CLI\Utils\make_progress_bar( 'Syncing ' . $total . ' assets', $total, 10 );
71+
foreach ( $assets as $index => $asset ) {
72+
$file = get_attached_file( $asset );
73+
$bar->tick( 0, 'Syncing: ' . basename( $file ) . ' (' . ( $index + 1 ) . ' of ' . $total . ')' );
74+
$this->plugin->get_component( 'sync' )->managers['push']->process_assets( $asset, $bar );
75+
$bar->tick();
76+
}
77+
$bar->tick( 0, 'Sync Completed.' );
78+
$bar->finish();
79+
$this->get_all_unsynced();
80+
\WP_CLI::success( 'Assets all synced' );
81+
}
82+
83+
/**
84+
* Allocate unsynced items to the queue threads.
85+
*
86+
* @param array|bool $include Include already found items.
87+
*
88+
* @return array
89+
*/
90+
private function get_all_unsynced( $include = false ) {
91+
92+
$params = array(
93+
'post_type' => 'attachment',
94+
'post_status' => 'inherit',
95+
'fields' => 'ids',
96+
'posts_per_page' => - 1,
97+
);
98+
99+
if ( ! empty( $include ) ) {
100+
$params['post__in'] = $include;
101+
}
102+
$ids = array();
103+
104+
$query = new \WP_Query( $params );
105+
$posts = $query->get_posts();
106+
\WP_CLI::log( '' );
107+
\WP_CLI::log( \WP_CLI::colorize( '%yAnalysing assets:%n' ) );
108+
$bar = \WP_CLI\Utils\make_progress_bar( '', $query->found_posts, 10 );
109+
$info = array(
110+
'_cld_unsupported' => 0,
111+
'_cld_synced' => 0,
112+
'_cld_unsynced' => 0,
113+
);
114+
foreach ( $posts as $index => $post ) {
115+
delete_post_meta( $post, '_cld_unsupported', true );
116+
delete_post_meta( $post, '_cld_synced', true );
117+
delete_post_meta( $post, '_cld_unsynced', true );
118+
$key = '_cld_unsupported';
119+
if ( $this->plugin->get_component( 'media' )->is_media( $post ) ) {
120+
// Add a key.
121+
$key = '_cld_synced';
122+
if ( ! $this->plugin->get_component( 'sync' )->is_synced( $post ) ) {
123+
$key = '_cld_unsynced';
124+
$ids[] = $post;
125+
}
126+
}
127+
$info[ $key ] ++;
128+
add_post_meta( $post, $key, true, true );
129+
$bar->tick( 1, $index . ' of ' . $query->found_posts . ' |' );
130+
}
131+
$bar->tick( 0, ' ' );
132+
$bar->finish();
133+
134+
\WP_CLI::success( $query->found_posts . ' analysed.' );
135+
\WP_CLI::log( '' );
136+
\WP_CLI::log( \WP_CLI::colorize( '%gSynced%n :' ) . ' ' . $info['_cld_synced'] );
137+
\WP_CLI::log( \WP_CLI::colorize( '%yUn-synced%n :' ) . ' ' . $info['_cld_unsynced'] );
138+
\WP_CLI::log( \WP_CLI::colorize( '%rUnsupported%n :' ) . ' ' . $info['_cld_unsupported'] );
139+
\WP_CLI::log( '' );
140+
if ( ! empty( $info['_cld_unsynced'] ) ) {
141+
\WP_CLI::confirm( 'Sync ' . count( $ids ) . ' assets?' );
142+
} else {
143+
return;
144+
}
145+
146+
return $ids;
147+
}
148+
}

0 commit comments

Comments
 (0)