Skip to content

Commit 621ac33

Browse files
authored
Merge pull request #351 from cloudinary/fix/sync-cli-issues
revise file signature and validator.
2 parents f7f3667 + 7b34f1c commit 621ac33

File tree

3 files changed

+70
-8
lines changed

3 files changed

+70
-8
lines changed

php/class-cli.php

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,25 @@ class CLI {
5050
* @param Plugin $plugin The plugin instance.
5151
*/
5252
public function __construct( $plugin ) {
53-
\WP_CLI::log( '' );
54-
\WP_CLI::log( '╔═╗┬ ┌─┐┬ ┬┌┬┐┬┌┐┌┌─┐┬─┐┬ ┬ ╔═╗╦ ╦' );
55-
\WP_CLI::log( '║ │ │ ││ │ ││││││├─┤├┬┘└┬┘ ║ ║ ║' );
56-
\WP_CLI::log( '╚═╝┴─┘└─┘└─┘─┴┘┴┘└┘┴ ┴┴└─ ┴ ╚═╝╩═╝╩' );
5753
$this->plugin = $plugin;
5854
}
5955

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+
6072
/**
6173
* Syncs assets with Cloudinary.
6274
* ## EXAMPLES
@@ -113,7 +125,7 @@ public function analyze() {
113125

114126
// Initial query.
115127
$query_args = $this->base_query_args;
116-
$query = new \WP_Query( $query_args );
128+
$query = new \WP_Query( $query_args );
117129

118130
// Kill all _cld_ related meta.
119131
delete_post_meta_by_key( '_cld_unsynced' );
@@ -132,6 +144,8 @@ public function analyze() {
132144
* @param string $process The process to do.
133145
*/
134146
protected function do_process( &$query, $process ) {
147+
$this->do_intro();
148+
135149
// Bail early.
136150
if ( ! method_exists( $this, "process_{$process}" ) ) {
137151
\WP_CLI::log( \WP_CLI::colorize( "%Invalid Process: {$process}.%n" ) );
@@ -175,8 +189,9 @@ protected function process_sync( $posts, $total ) {
175189
}
176190
foreach ( $posts as $index => $asset ) {
177191
$done ++; // Set $done early to not show 0 of x.
178-
$file = get_attached_file( $asset );
179-
$bar->tick( 0, 'Syncing: ' . basename( $file ) . ' (' . ( $done ) . ' of ' . $total . ')' );
192+
$file = get_attached_file( $asset );
193+
$filename = self::pad_name( basename( $file ), 20, ' ', '*' );
194+
$bar->tick( 0, 'Syncing (' . ( $done ) . ' of ' . $total . ') : ' . $filename );
180195
if ( ! $this->plugin->get_component( 'sync' )->is_synced( $asset ) ) {
181196
$this->plugin->get_component( 'sync' )->managers['push']->process_assets( $asset, $bar );
182197
}
@@ -241,4 +256,34 @@ protected function process_analyze( $posts, $total ) {
241256
update_option( '_cld_cli_analyzed', true, false );
242257
}
243258
}
259+
260+
/**
261+
* Pad a file name to fit within max chars.
262+
*
263+
* @param string $name The name to pad.
264+
* @param int $max_length The max length of the filename.
265+
* @param string $pad_char The pad char to use when name is less of the max.
266+
* @param string $concat_char The char to use when shortening names to fit.
267+
*
268+
* @return string
269+
*/
270+
protected static function pad_name( $name, $max_length, $pad_char = '.', $concat_char = '*' ) {
271+
$name_length = strlen( $name );
272+
$prefix = null;
273+
if ( $name_length > $max_length ) {
274+
$diff = $name_length - $max_length;
275+
$concat_length = $diff > 3 ? 3 : $diff;
276+
$usable_length = $max_length - $concat_length;
277+
$front = substr( $name, 0, floor( $usable_length / 2 ) );
278+
$back = substr( $name, strlen( $name ) - ceil( $usable_length / 2 ) );
279+
$name = $front . implode( array_fill( 0, $concat_length, $concat_char ) ) . $back;
280+
}
281+
$used_length = $max_length - strlen( $name );
282+
if ( 0 < $used_length ) {
283+
$prefix = implode( array_fill( 0, $used_length, $pad_char ) );
284+
}
285+
$out = $prefix . $name;
286+
287+
return $out;
288+
}
244289
}

php/class-sync.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,12 @@ public function setup_sync_base_struct() {
383383
'note' => __( 'Downloading from Cloudinary', 'cloudinary' ),
384384
),
385385
'file' => array(
386-
'generate' => 'get_attached_file',
386+
'generate' => array( $this, 'generate_file_signature' ),
387387
'priority' => 5.1,
388388
'sync' => array( $this->managers['upload'], 'upload_asset' ),
389+
'validate' => function ( $attachment_id ) {
390+
return ! $this->managers['media']->has_public_id( $attachment_id );
391+
},
389392
'state' => 'uploading',
390393
'note' => __( 'Uploading to Cloudinary', 'cloudinary' ),
391394
'required' => true, // Required to complete URL render flag.
@@ -933,6 +936,19 @@ public function settings() {
933936
return $args;
934937
}
935938

939+
/**
940+
* Generate the real file attachment path for the file sync type signature.
941+
*
942+
* @param int $attachment_id The attachment ID.
943+
*
944+
* @return string
945+
*/
946+
public function generate_file_signature( $attachment_id ) {
947+
$path = get_attached_file( $attachment_id );
948+
949+
return basename( $path );
950+
}
951+
936952
/**
937953
* Register the setting under media.
938954
*/

php/sync/class-upload-sync.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ public function handle_bulk_actions( $location, $action, $post_ids ) {
152152
case 'cloudinary-push':
153153
foreach ( $post_ids as $post_id ) {
154154
$this->sync->set_signature_item( $post_id, 'file', '' );
155+
$this->media->delete_post_meta( $post_id, Sync::META_KEYS['public_id'] );
155156
$this->sync->add_to_sync( $post_id );
156157
}
157158
break;

0 commit comments

Comments
 (0)