Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion css/cloudinary.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/cloudinary.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion php/class-connect.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ public function history( $days = 1 ) {
$plan = ! empty( $this->usage['plan'] ) ? $this->usage['plan'] : $this->credentials['cloud_name'];
for ( $i = 1; $i <= $days; $i ++ ) {
$date = date_i18n( 'd-m-Y', strtotime( '- ' . $i . ' days' ) );
if ( ! isset( $history[ $plan ][ $date ] ) ) {
if ( ! isset( $history[ $plan ][ $date ] ) || is_wp_error( $history[ $plan ][ $date ] ) ) {
$history[ $plan ][ $date ] = $this->api->usage( $date );
uksort(
$history[ $plan ],
Expand Down
36 changes: 34 additions & 2 deletions php/class-dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,41 @@ public function register_settings( $pages ) {
'data-text' => 'error_count_hr',
'class' => array(
'cld-stat-text',
'cld-ui-error',
'cld-link-button',
'cld-link-button-secondary',
'cld-toggle',
'hidden',
),
'title' => __( 'View errored assets', 'cloudinary' ),
),
),
array(
'type' => 'tag',
'element' => 'br',
),
array(
'type' => 'tag',
'element' => 'a',
'content' => '&nbsp;',
'attributes' => array(
'href' => add_query_arg(
array(
'page' => 'cloudinary',
'action' => 'clean_up',
'nonce' => wp_create_nonce( 'clean_up' ),
),
admin_url( 'admin.php' )
),
'data-text' => 'error_clean_up',
'class' => array(
'cld-stat-text',
'cld-link-button',
'cld-link-button-secondary',
'cld-link-button-secondary-error',
'cld-toggle',
'hidden',
),
'title' => __( 'Retry sync assets with errors', 'cloudinary' ),
),
),
),
Expand Down Expand Up @@ -334,5 +367,4 @@ public function register_settings( $pages ) {

return $pages;
}

}
41 changes: 41 additions & 0 deletions php/class-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,45 @@ public function get_signature_syncable_type( $signature, $attachment_id ) {
return $signature;
}

/**
* Cleanup errored syncs.
*/
public function maybe_cleanup_errored() {
if ( 'cloudinary' !== Utils::get_sanitized_text( 'page' ) ) {
return;
}

if ( 'cleaned_up' === Utils::get_sanitized_text( 'action' ) ) {
$this->plugin->get_component( 'admin' )->add_admin_notice( 'error_notice', __( 'Sync errors cleaned up', 'cloudinary' ), 'success' );
}

if ( 'clean_up' !== Utils::get_sanitized_text( 'action' ) ) {
return;
}

if ( ! wp_verify_nonce( Utils::get_sanitized_text( 'nonce' ), 'clean_up' ) ) {
return;
}

if ( ! current_user_can( 'manage_options' ) ) {
return;
}

delete_post_meta_by_key( self::META_KEYS['sync_error'] );

wp_redirect(
add_query_arg(
array(
'page' => 'cloudinary',
'action' => 'cleaned_up',
),
admin_url( 'admin.php' )
)
);

exit;
}

/**
* Checks if auto sync feature is enabled.
*
Expand Down Expand Up @@ -1175,6 +1214,8 @@ public function setup() {
add_filter( 'cloudinary_setting_get_value', array( $this, 'filter_get_cloudinary_folder' ), 10, 2 );
add_filter( 'cloudinary_get_signature', array( $this, 'get_signature_syncable_type' ), 10, 2 );

add_action( 'admin_init', array( $this, 'maybe_cleanup_errored' ), 10, 2 );

add_action( 'rest_api_init', array( $this, 'rest_api_is_synced_field' ) );
}
}
Expand Down
9 changes: 5 additions & 4 deletions php/sync/class-sync-queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function setup( $sync ) {
* @return {int}
*/
$queue_threads_count = apply_filters( 'cloudinary_queue_threads', 2 );
for ( $i = 0; $i < $queue_threads_count; $i ++ ) {
for ( $i = 0; $i < $queue_threads_count; ++$i ) {
$this->queue_threads[] = 'queue_sync_thread_' . $i;
}

Expand All @@ -128,7 +128,7 @@ public function setup( $sync ) {
* @return {int}
*/
$autosync_thread_count = apply_filters( 'cloudinary_autosync_threads', 2 );
for ( $i = 0; $i < $autosync_thread_count; $i ++ ) {
for ( $i = 0; $i < $autosync_thread_count; ++$i ) {
$this->autosync_threads[] = 'auto_sync_thread_' . $i;
}
$this->threads = array_merge( $this->queue_threads, $this->autosync_threads );
Expand Down Expand Up @@ -411,7 +411,8 @@ public function get_total_synced_media() {
// Error size: No mockups on what to display here.
'error_count' => $errors_count,
// translators: placeholders are the number of errors.
'error_count_hr' => 0 === $errors_count ? '' : sprintf( _n( '%s error with assets.', '%s errors with assets', $errors_count, 'cloudinary' ), number_format_i18n( $errors_count ) ),
'error_count_hr' => 0 === $errors_count ? '' : sprintf( _n( '%s error with assets', '%s errors with assets', $errors_count, 'cloudinary' ), number_format_i18n( $errors_count ) ),
'error_clean_up' => 0 === $errors_count ? '' : __( 'Retry errored unsynced items', 'cloudinary' ),

// Number of assets.
'total_assets' => $total_assets, // This is a count of the assets in _cloudinary_relationships.
Expand Down Expand Up @@ -502,7 +503,7 @@ public function build_queue() {
do {
$ids = array_merge( $ids, $query->get_posts() );
$args = $query->query_vars;
$args['paged'] ++;
++$args['paged'];
$query = new \WP_Query( $args );
} while ( $query->have_posts() );

Expand Down
48 changes: 37 additions & 11 deletions php/sync/class-upload-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Cloudinary\Sync;

use Cloudinary\Delivery;
use Cloudinary\Sync;
use Cloudinary\Utils;

Expand Down Expand Up @@ -91,7 +90,8 @@ private function register_hooks() {
'bulk_actions-upload',
function ( $actions ) {
$cloudinary_actions = array(
'cloudinary-push' => __( 'Sync with Cloudinary', 'cloudinary' ),
'cloudinary-push' => __( 'Sync with Cloudinary', 'cloudinary' ),
'cloudinary-clean-up' => __( 'Fix Cloudinary Sync Errors', 'cloudinary' ),
);

return array_merge( $cloudinary_actions, $actions );
Expand Down Expand Up @@ -136,18 +136,35 @@ public function add_inline_action( $actions, $post ) {
esc_attr__( 'Sync and deliver from Cloudinary', 'cloudinary' ),
esc_html__( 'Sync and deliver from Cloudinary', 'cloudinary' )
);
} else {
if ( file_exists( get_attached_file( $post->ID ) ) ) {
$actions['cloudinary-push'] = sprintf(
'<a href="%s" aria-label="%s">%s</a>',
$action_url,
esc_attr__( 'Re-sync to Cloudinary', 'cloudinary' ),
esc_html__( 'Re-sync to Cloudinary', 'cloudinary' )
);
}
} else if ( file_exists( get_attached_file( $post->ID ) ) ) {
$actions['cloudinary-push'] = sprintf(
'<a href="%s" aria-label="%s">%s</a>',
$action_url,
esc_attr__( 'Re-sync to Cloudinary', 'cloudinary' ),
esc_html__( 'Re-sync to Cloudinary', 'cloudinary' )
);
}
}

if (
$this->sync->is_syncable( $post->ID )
&& ! empty( get_post_meta( $post->ID, Sync::META_KEYS['sync_error'], true ) )
) {
$actions['cloudinary-clean-up'] = sprintf(
'<a href="%s" aria-label="%s">%s</a>',
add_query_arg(
array(
'action' => 'cloudinary-clean-up',
'media[]' => $post->ID,
'_wpnonce' => wp_create_nonce( 'bulk-media' ),
),
admin_url( 'upload.php' )
),
esc_attr__( 'Fix Cloudinary Sync Error', 'cloudinary' ),
esc_html__( 'Fix Cloudinary Sync Error', 'cloudinary' )
);
}

return $actions;
}

Expand Down Expand Up @@ -188,6 +205,15 @@ public function handle_bulk_actions( $location, $action, $post_ids ) {
}
}
break;

case 'cloudinary-clean-up':
foreach ( $post_ids as $post_id ) {
if ( ! $this->sync->is_syncable( $post_id ) ) {
continue;
}
delete_post_meta( $post_id, Sync::META_KEYS['sync_error'] );
}
break;
}

return $location;
Expand Down
45 changes: 39 additions & 6 deletions php/traits/trait-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,23 @@ trait CLI_Trait {

/**
* Is debug mode on.
* Either verbose or export flags.git
* Either verbose or export flags.
*
* @since 3.0.3
*
* @var bool
*/
protected $is_debug_enabled = false;

/**
* Clean up flag.
*
* @since 3.2.0
*
* @var bool
*/
protected $clean_up = false;

/**
* CLI Cloudinary Setup.
*
Expand Down Expand Up @@ -114,6 +123,16 @@ protected function do_intro() {
*
* wp cloudinary sync
*
* ## OPTIONS
* [--verbose]
* : Whether to show extra information on unsynced and errored attachments.
*
* [--export]
* : Whether to export CSV files with unsynced and errored attachments.
*
* [--clean-up]
* : Whether to clean up all the error flags.
*
* @when after_wp_load
* @since 2.5.1
*
Expand All @@ -133,6 +152,7 @@ public function sync( $args, $assoc_args ) {
$this->is_verbose = ! empty( $assoc_args['verbose'] );
$this->is_export = ! empty( $assoc_args['export'] );
$this->is_debug_enabled = empty( $args ) && ( $this->is_verbose || $this->is_export );
$this->clean_up = ! empty( $assoc_args['clean-up'] );

// Initial Query.
$query_args = $this->base_query_args;
Expand All @@ -152,7 +172,6 @@ public function sync( $args, $assoc_args ) {
\WP_CLI::log( \WP_CLI::colorize( '%gAll assets synced.%n' ) );
delete_option( '_cld_cli_analyzed' );
}

}

/**
Expand All @@ -165,6 +184,9 @@ public function sync( $args, $assoc_args ) {
* [--export]
* : Whether to export CSV files with unsynced and errored attachments.
*
* [--clean-up]
* : Whether to clean up all the error flags.
*
* ## EXAMPLES
*
* wp cloudinary analyze
Expand All @@ -179,11 +201,14 @@ public function sync( $args, $assoc_args ) {
*/
public function analyze( $args = null, $assoc_args = null ) {

static $did_cleanup = false;

// Warmup flags if called as command.
if ( ! is_null( $args ) && ! is_null( $assoc_args ) ) {
$this->is_verbose = ! empty( $assoc_args['verbose'] );
$this->is_export = ! empty( $assoc_args['export'] );
$this->is_debug_enabled = empty( $args ) && ( $this->is_verbose || $this->is_export );
$this->clean_up = ! empty( $assoc_args['clean-up'] );
}

// Initial query.
Expand All @@ -196,6 +221,14 @@ public function analyze( $args = null, $assoc_args = null ) {

// Do process.
$this->do_process( $query, 'analyze' );

// Clean up all the error flags.
if ( $this->clean_up && ! $did_cleanup ) {
$did_cleanup = true;

\WP_CLI::log( \WP_CLI::colorize( '%gCleaning up the error flags.%n' ) );
delete_post_meta_by_key( Sync::META_KEYS['sync_error'] );
}
}

/**
Expand Down Expand Up @@ -239,7 +272,7 @@ protected function do_process( &$query, $process, $paginate = true ) {
// Paginate.
$query_args = $query->query_vars;
if ( true === $paginate ) {
$query_args['paged'] ++;
++$query_args['paged'];
}
$query = new \WP_Query( $query_args );
} while ( $query->have_posts() );
Expand All @@ -261,7 +294,7 @@ protected function process_sync( $posts, $total ) {
$done = 0;
}
foreach ( $posts as $index => $asset ) {
$done ++; // Set $done early to not show 0 of x.
++$done; // Set $done early to not show 0 of x.
$file = get_attached_file( $asset );
$filename = self::pad_name( wp_basename( $file ), 20, ' ', '*' );
$bar->tick( 1, 'Syncing (' . ( $done ) . ' of ' . $total . ') : ' . $filename );
Expand Down Expand Up @@ -312,7 +345,7 @@ protected function process_analyze( $posts, $total ) {
$errored_attachments = array();

foreach ( $posts as $asset ) {
$done ++;
++$done;
$key = '_cld_unsupported';
if (
$this->plugin->get_component( 'media' )->is_uploadable_media( $asset )
Expand Down Expand Up @@ -345,7 +378,7 @@ protected function process_analyze( $posts, $total ) {
}
}
}
$info[ $key ] ++;
++$info[ $key ];
$bar->tick( 1, $done . ' of ' . $total . ' |' );
}
// Done message.
Expand Down
22 changes: 22 additions & 0 deletions src/css/components/ui/_links.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,27 @@
color: #fff;
text-decoration: none;
}

&.hidden {
display: none;
}

&-secondary {
background-color: $color-cld-grey-blue;
color: $color-cld-dark-description;
border: 1px solid $color-cld-blue;

&:hover, &:focus {
background-color: $color-cld-blue;
}

&-error {
border-color: $color-red;

&:hover, &:focus {
background-color: $color-red;
}
}
}
}
}
Loading