Skip to content

Commit 6873436

Browse files
committed
split CLI for VIP and Standard.
1 parent 66a3959 commit 6873436

File tree

4 files changed

+346
-278
lines changed

4 files changed

+346
-278
lines changed

instance.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,20 @@ function get_plugin_instance() {
2626
return $cloudinary_plugin;
2727
}
2828

29+
/**
30+
* Get an instance of the CLI Class.
31+
*/
32+
function cloudinary_cli_instance() {
33+
if ( class_exists( '\WPCOM_VIP_CLI_Command' ) ) {
34+
return new CLI_VIP();
35+
}
36+
37+
return new CLI();
38+
}
39+
2940
if ( defined( 'WP_CLI' ) && WP_CLI ) {
30-
$instance = new CLI( get_plugin_instance() );
31-
\WP_CLI::add_command( 'cloudinary', $instance );
41+
$plugin_instance = get_plugin_instance();
42+
$cli = cloudinary_cli_instance();
43+
$cli->setup_cloudinary( $plugin_instance );
44+
\WP_CLI::add_command( 'cloudinary', $cli );
3245
}

php/class-cli-vip.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Cloudinary CLI for VIP.
4+
*
5+
* @package Cloudinary
6+
*/
7+
8+
namespace Cloudinary;
9+
10+
use Cloudinary\Traits\CLI_Trait;
11+
12+
/**
13+
* CLI VIP class.
14+
*
15+
* @since 2.5.1
16+
*/
17+
class CLI_VIP extends \WPCOM_VIP_CLI_Command {
18+
19+
use CLI_Trait;
20+
21+
/**
22+
* Output the Intro.
23+
*
24+
* @since 2.5.1
25+
* @link http://patorjk.com/software/taag/#p=display&c=echo&f=Calvin%20S&t=Cloudinary%20CLI
26+
*/
27+
public function do_intro() {
28+
static $intro;
29+
if ( ! $intro ) {
30+
\WP_CLI::log( '' );
31+
\WP_CLI::log( '╔═╗┬ ┌─┐┬ ┬┌┬┐┬┌┐┌┌─┐┬─┐┬ ┬ ╔═╗╦ ╦ ╦ ╦╦╔═╗' );
32+
\WP_CLI::log( '║ │ │ ││ │ ││││││├─┤├┬┘└┬┘ ║ ║ ║ ╚╗╔╝║╠═╝' );
33+
\WP_CLI::log( '╚═╝┴─┘└─┘└─┘─┴┘┴┘└┘┴ ┴┴└─ ┴ ╚═╝╩═╝╩ ╚╝ ╩╩ ' );
34+
$intro = true;
35+
}
36+
}
37+
}

php/class-cli.php

Lines changed: 4 additions & 276 deletions
Original file line numberDiff line numberDiff line change
@@ -7,294 +7,22 @@
77

88
namespace Cloudinary;
99

10-
use Cloudinary\Plugin;
10+
use Cloudinary\Traits\CLI_Trait;
1111

1212
/**
1313
* CLI class.
1414
*
1515
* @since 2.5.1
1616
*/
17-
class CLI {
17+
class CLI extends \WP_CLI_Command { // phpcs:ignore WordPressVIPMinimum.Classes.RestrictedExtendClasses.wp_cli
1818

19-
/**
20-
* Holds the plugin instance.
21-
*
22-
* @since 2.5.1
23-
*
24-
* @var Plugin Instance of the global plugin.
25-
*/
26-
public $plugin;
27-
28-
/**
29-
* Holds the base query args.
30-
*
31-
* @since 2.5.1
32-
*
33-
* @var array
34-
*/
35-
protected $base_query_args = array(
36-
'post_type' => 'attachment',
37-
'post_status' => 'inherit',
38-
'fields' => 'ids',
39-
'posts_per_page' => 100,
40-
'update_post_term_cache' => false,
41-
'update_post_meta_cache' => false,
42-
'paged' => 1,
43-
);
44-
45-
/**
46-
* CLI constructor.
47-
*
48-
* @since 2.5.1
49-
*
50-
* @param Plugin $plugin The plugin instance.
51-
*/
52-
public function __construct( $plugin ) {
53-
$this->plugin = $plugin;
54-
}
55-
56-
/**
57-
* Output the Intro.
58-
*
59-
* @since 2.5.1
60-
*/
61-
public function do_intro() {
62-
static $intro;
63-
if ( ! $intro ) {
64-
\WP_CLI::log( '' );
65-
\WP_CLI::log( '╔═╗┬ ┌─┐┬ ┬┌┬┐┬┌┐┌┌─┐┬─┐┬ ┬ ╔═╗╦ ╦' );
66-
\WP_CLI::log( '║ │ │ ││ │ ││││││├─┤├┬┘└┬┘ ║ ║ ║' );
67-
\WP_CLI::log( '╚═╝┴─┘└─┘└─┘─┴┘┴┘└┘┴ ┴┴└─ ┴ ╚═╝╩═╝╩' );
68-
$intro = true;
69-
}
70-
}
71-
72-
/**
73-
* Syncs assets with Cloudinary.
74-
* ## EXAMPLES
75-
*
76-
* wp cloudinary sync
77-
*
78-
* @when after_wp_load
79-
* @since 2.5.1
80-
*
81-
* @param array $args Ignored.
82-
* @param array $assoc_args Ignored.
83-
*
84-
* @return void
85-
*/
86-
public function sync( $args, $assoc_args ) {
87-
88-
// Check if analyzed first.
89-
if ( empty( get_option( '_cld_cli_analyzed' ) ) ) {
90-
$this->analyze();
91-
}
92-
93-
// Initial Query.
94-
$query_args = $this->base_query_args;
95-
// phpcs:ignore WordPress.DB.SlowDBQuery
96-
$query_args['meta_query'] = array(
97-
'AND',
98-
array(
99-
'key' => '_cld_unsynced',
100-
'compare' => 'EXISTS',
101-
),
102-
);
103-
104-
// Get assets that need to be synced.
105-
$query = new \WP_Query( $query_args );
106-
$this->do_process( $query, 'sync' );
107-
if ( ! $query->have_posts() ) {
108-
\WP_CLI::log( \WP_CLI::colorize( '%gAll assets synced.%n' ) );
109-
delete_option( '_cld_cli_analyzed' );
110-
}
111-
112-
}
113-
114-
/**
115-
* Analyze assets with Cloudinary.
116-
* ## EXAMPLES
117-
*
118-
* wp cloudinary analyze
119-
*
120-
* @when after_wp_load
121-
* @since 2.5.1
122-
*
123-
* @return void
124-
*/
125-
public function analyze() {
126-
127-
// Initial query.
128-
$query_args = $this->base_query_args;
129-
$query = new \WP_Query( $query_args );
130-
131-
// Kill all _cld_ related meta.
132-
delete_post_meta_by_key( '_cld_unsynced' );
133-
delete_option( '_cld_cli_analyzed' );
134-
135-
// Do process.
136-
$this->do_process( $query, 'analyze' );
137-
}
138-
139-
/**
140-
* Do a process on the query.
141-
*
142-
* @since 2.5.1
143-
*
144-
* @param \WP_Query $query The initial query object.
145-
* @param string $process The process to do.
146-
*/
147-
protected function do_process( &$query, $process ) {
148-
$this->do_intro();
149-
150-
// Bail early.
151-
if ( ! method_exists( $this, "process_{$process}" ) ) {
152-
\WP_CLI::log( \WP_CLI::colorize( "%Invalid Process: {$process}.%n" ) );
153-
154-
return;
155-
}
156-
if ( method_exists( $this, $process ) ) {
157-
// Setup process.
158-
$total = $query->found_posts;
159-
$process = "process_{$process}";
160-
do {
161-
$posts = $query->get_posts();
162-
$this->{$process}( $posts, $total );
163-
164-
// Free up memory.
165-
$this->oh_the_humanity();
166-
167-
// Paginate.
168-
$query_args = $query->query_vars;
169-
$query_args['paged'] ++;
170-
$query = new \WP_Query( $query_args );
171-
} while ( $query->have_posts() );
172-
}
173-
\WP_CLI::line( '' );
174-
}
175-
176-
/**
177-
* Sync Assets.
178-
*
179-
* @param array $posts Array of Post IDs to process.
180-
* @param int $total Count of total posts to process.
181-
*/
182-
protected function process_sync( $posts, $total ) {
183-
static $bar, $done;
184-
if ( ! $bar && ! empty( $posts ) ) {
185-
\WP_CLI::log( \WP_CLI::colorize( '%gSyncing assets%n' ) );
186-
$bar = \WP_CLI\Utils\make_progress_bar( 'Syncing ' . $total . ' assets', $total, 10 );
187-
$done = 0;
188-
}
189-
foreach ( $posts as $index => $asset ) {
190-
$done ++; // Set $done early to not show 0 of x.
191-
$file = get_attached_file( $asset );
192-
$filename = self::pad_name( basename( $file ), 20, ' ', '*' );
193-
$bar->tick( 0, 'Syncing (' . ( $done ) . ' of ' . $total . ') : ' . $filename );
194-
if ( ! $this->plugin->get_component( 'sync' )->is_synced( $asset ) ) {
195-
$this->plugin->get_component( 'sync' )->managers['push']->process_assets( $asset, $bar );
196-
}
197-
delete_post_meta( $asset, '_cld_unsynced', true );
198-
$bar->tick();
199-
}
200-
// Done message - reanalyze.
201-
if ( $done === $total ) {
202-
$bar->tick( 0, 'Sync Completed.' );
203-
$bar->finish();
204-
$bar = null;
205-
\WP_CLI::line( '' );
206-
$this->analyze();
207-
delete_option( '_cld_cli_analyzed' );
208-
}
209-
}
210-
211-
/**
212-
* Analyze and mark assets that need to be synced.
213-
*
214-
* @since 2.5.1
215-
*
216-
* @param array $posts Array of Post IDs to process.
217-
* @param int $total Count of total posts to process.
218-
*/
219-
protected function process_analyze( $posts, $total ) {
220-
static $bar, $done, $info;
221-
222-
if ( ! $bar ) {
223-
\WP_CLI::log( \WP_CLI::colorize( '%gAnalyzing ' . $total . ' assets:%n' ) );
224-
$bar = \WP_CLI\Utils\make_progress_bar( '', $total, 10 );
225-
$done = 0;
226-
$info = array(
227-
'_cld_unsupported' => 0,
228-
'_cld_synced' => 0,
229-
'_cld_unsynced' => 0,
230-
);
231-
}
232-
foreach ( $posts as $index => $asset ) {
233-
$done ++;
234-
$key = '_cld_unsupported';
235-
if ( $this->plugin->get_component( 'media' )->is_media( $asset ) ) {
236-
// Add a key.
237-
$key = '_cld_synced';
238-
if ( ! $this->plugin->get_component( 'sync' )->is_synced( $asset ) ) {
239-
$key = '_cld_unsynced';
240-
add_post_meta( $asset, $key, true, true );
241-
}
242-
}
243-
$info[ $key ] ++;
244-
$bar->tick( 1, $done . ' of ' . $total . ' |' );
245-
}
246-
// Done message.
247-
if ( $done === $total ) {
248-
$bar->tick( 0, $total . ' Analyzed |' );
249-
$bar->finish();
250-
$bar = null;
251-
\WP_CLI::log( '' );
252-
\WP_CLI::log( \WP_CLI::colorize( '%gSynced%n :' ) . ' ' . $info['_cld_synced'] );
253-
\WP_CLI::log( \WP_CLI::colorize( '%yUn-synced%n :' ) . ' ' . $info['_cld_unsynced'] );
254-
\WP_CLI::log( \WP_CLI::colorize( '%rUnsupported%n :' ) . ' ' . $info['_cld_unsupported'] );
255-
update_option( '_cld_cli_analyzed', true, false );
256-
}
257-
}
258-
259-
/**
260-
* Pad a file name to fit within max chars.
261-
*
262-
* @param string $name The name to pad.
263-
* @param int $max_length The max length of the filename.
264-
* @param string $pad_char The pad char to use when name is less of the max.
265-
* @param string $concat_char The char to use when shortening names to fit.
266-
*
267-
* @return string
268-
*/
269-
protected static function pad_name( $name, $max_length, $pad_char = '.', $concat_char = '*' ) {
270-
$name_length = strlen( $name );
271-
$prefix = null;
272-
if ( $name_length > $max_length ) {
273-
$diff = $name_length - $max_length;
274-
$concat_length = $diff > 3 ? 3 : $diff;
275-
$usable_length = $max_length - $concat_length;
276-
$front = substr( $name, 0, floor( $usable_length / 2 ) );
277-
$back = substr( $name, strlen( $name ) - ceil( $usable_length / 2 ) );
278-
$name = $front . implode( array_fill( 0, $concat_length, $concat_char ) ) . $back;
279-
}
280-
$used_length = $max_length - strlen( $name );
281-
if ( 0 < $used_length ) {
282-
$prefix = implode( array_fill( 0, $used_length, $pad_char ) );
283-
}
284-
$out = $prefix . $name;
285-
286-
return $out;
287-
}
19+
use CLI_Trait;
28820

28921
/**
29022
* Workaround to prevent memory leaks from growing variables
29123
*/
292-
protected function oh_the_humanity() {
24+
protected function stop_the_insanity() {
29325
global $wpdb, $wp_object_cache;
294-
if ( method_exists( $this, 'stop_the_insanity' ) ) {
295-
return $this->stop_the_insanity();
296-
}
297-
29826
$wpdb->queries = array();
29927
if ( is_object( $wp_object_cache ) ) {
30028
$wp_object_cache->group_ops = array();

0 commit comments

Comments
 (0)