From 64ffb19b802e270f1809352e34772a6013704e0d Mon Sep 17 00:00:00 2001 From: David Cramer Date: Thu, 24 Jun 2021 14:25:53 +0200 Subject: [PATCH 01/10] separate meta system --- php/class-media.php | 104 ++++++++++++++++++------------- php/class-sync.php | 68 +++++++++++++++----- php/media/class-upgrade.php | 8 +-- php/sync/class-download-sync.php | 17 +---- php/sync/class-push-sync.php | 20 +++--- php/sync/class-upload-sync.php | 5 +- php/traits/trait-cli.php | 1 - 7 files changed, 127 insertions(+), 96 deletions(-) diff --git a/php/class-media.php b/php/class-media.php index 51c603675..85971e9a9 100644 --- a/php/class-media.php +++ b/php/class-media.php @@ -1094,7 +1094,7 @@ public function get_cloudinary_folder( $add_trailing_slash = true ) { public function get_public_id( $attachment_id, $suffixed = false ) { // Check for a public_id. if ( $this->has_public_id( $attachment_id ) ) { - $public_id = get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true ); + $public_id = $this->get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true ); if ( $this->is_folder_synced( $attachment_id ) ) { $public_id = $this->get_cloudinary_folder() . pathinfo( $public_id, PATHINFO_BASENAME ); } @@ -1116,7 +1116,10 @@ public function get_public_id( $attachment_id, $suffixed = false ) { * @return bool */ public function has_public_id( $attachment_id ) { - return ! empty( get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true ) ); + $new_id = $this->get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true ); + $id = get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true ); + + return ! empty( $new_id ) || ! empty( $id ); } /** @@ -1156,23 +1159,29 @@ public function get_cloudinary_id( $attachment_id ) { * * @param int $attachment_id The ID to get Cloudinary id for. * - * @return string|null the ID or null if not existing. + * @return string|false the ID or null if not existing. */ public function cloudinary_id( $attachment_id ) { + static $cloudinary_ids = array(); + + // Return cached ID if we've already gotten it before. + if ( isset( $cloudinary_ids[ $attachment_id ] ) ) { + return $cloudinary_ids[ $attachment_id ]; + } if ( ! $this->is_media( $attachment_id ) ) { + $cloudinary_ids[ $attachment_id ] = null; return null; } - // Return cached ID if we've already gotten it before. - if ( isset( $this->cloudinary_ids[ $attachment_id ] ) ) { - return $this->cloudinary_ids[ $attachment_id ]; - } + if ( ! $this->sync->is_synced( $attachment_id ) && ! defined( 'REST_REQUEST' ) ) { $sync_type = $this->sync->maybe_prepare_sync( $attachment_id ); // Check sync type allows for continued rendering. i.e meta update, breakpoints etc, will still allow the URL to work, // Where is type "file" will not since it's still being uploaded. - if ( ! is_null( $sync_type ) && $this->sync->is_required( $sync_type, $attachment_id ) ) { - return null; // Return and render local URLs. + if ( is_null( $sync_type ) || $this->sync->is_required( $sync_type, $attachment_id ) ) { + // Cache ID to prevent multiple lookups. + $cloudinary_ids[ $attachment_id ] = false; + return false; // Return and render local URLs. } } @@ -1195,8 +1204,9 @@ public function cloudinary_id( $attachment_id ) { * @param int $attachment_id The id of the asset. */ do_action( 'cloudinary_id', $cloudinary_id, $attachment_id ); - // Cache ID to prevent multiple lookups. - $this->cloudinary_ids[ $attachment_id ] = $cloudinary_id; + + $cloudinary_ids[ $attachment_id ] = $cloudinary_id; + return $cloudinary_id; } @@ -1480,7 +1490,8 @@ private function create_attachment( $asset, $public_id ) { $sync_key = $asset['sync_key']; // Capture public_id. Use core update_post_meta since this attachment data doesnt exist yet. - update_post_meta( $attachment_id, Sync::META_KEYS['public_id'], $public_id ); + $this->update_post_meta( $attachment_id, Sync::META_KEYS['public_id'], $public_id ); + // Capture version number. $this->update_post_meta( $attachment_id, Sync::META_KEYS['version'], $asset['version'] ); if ( ! empty( $asset['transformations'] ) ) { @@ -1488,10 +1499,7 @@ private function create_attachment( $asset, $public_id ) { $sync_key .= wp_json_encode( $asset['transformations'] ); $this->update_post_meta( $attachment_id, Sync::META_KEYS['transformation'], $asset['transformations'] ); } - // create a trackable key in post meta. - update_post_meta( $attachment_id, '_' . md5( $sync_key ), true ); - // record a base to ensure primary isn't deleted. - update_post_meta( $attachment_id, '_' . md5( 'base_' . $public_id ), true ); + // capture the delivery type. $this->update_post_meta( $attachment_id, Sync::META_KEYS['delivery'], $asset['type'] ); // Capture the ALT Text. @@ -1835,23 +1843,24 @@ public function get_transformation_from_meta( $post_id ) { */ public function get_post_meta( $post_id, $key = '', $single = false ) { - $meta_data = wp_get_attachment_metadata( $post_id, true ); - if ( ! is_array( $meta_data ) ) { - return get_post_meta( $post_id, $key, $single ); - } - if ( ! isset( $meta_data[ Sync::META_KEYS['cloudinary'] ] ) ) { - $meta_data[ Sync::META_KEYS['cloudinary'] ] = array(); + $meta = get_post_meta( $post_id, Sync::META_KEYS['cloudinary'], true ); + if ( empty( $meta ) ) { + $meta = array(); + $old_meta = wp_get_attachment_metadata( $post_id ); + if ( isset( $old_meta[ Sync::META_KEYS['cloudinary'] ] ) ) { + $meta = $old_meta[ Sync::META_KEYS['cloudinary'] ]; + // Add public ID. + $public_id = get_post_meta( $post_id, Sync::META_KEYS['public_id'], true ); + $meta[ Sync::META_KEYS['public_id'] ] = $public_id; + update_post_meta( $post_id, Sync::META_KEYS['cloudinary'], $meta ); + delete_post_meta( $post_id, Sync::META_KEYS['public_id'] ); + } } - - if ( '' === $key ) { - $data = $meta_data[ Sync::META_KEYS['cloudinary'] ]; - } elseif ( ! empty( $meta_data[ Sync::META_KEYS['cloudinary'] ][ $key ] ) ) { - $data = $meta_data[ Sync::META_KEYS['cloudinary'] ][ $key ]; - } else { - $data = $this->build_cached_meta( $post_id, $key, $single ); + if ( '' !== $key ) { + $meta = isset( $meta[ $key ] ) ? $meta[ $key ] : ''; } - return $data; + return $single ? $meta : (array) $meta; } /** @@ -1878,19 +1887,21 @@ public function build_cached_meta( $post_id, $key, $single ) { * @param int $post_id The attachment ID. * @param string $key The meta key to get. * @param string|array $data $the meta data to update. + * + * @return bool */ public function update_post_meta( $post_id, $key, $data ) { - $meta_data = wp_get_attachment_metadata( $post_id, true ); - if ( is_array( $meta_data ) ) { - if ( ! isset( $meta_data[ Sync::META_KEYS['cloudinary'] ] ) ) { - $meta_data[ Sync::META_KEYS['cloudinary'] ] = array(); - } - $meta_data[ Sync::META_KEYS['cloudinary'] ][ $key ] = $data; - wp_update_attachment_metadata( $post_id, $meta_data ); - } else { - // Update core mete data for consistency. - update_post_meta( $post_id, $key, $data ); + + $meta = $this->get_post_meta( $post_id ); + if ( ! isset( $meta[ $key ] ) ) { + $meta[ $key ] = ''; } + + if ( $meta[ $key ] !== $data ) { + $meta[ $key ] = $data; + } + + return update_post_meta( $post_id, Sync::META_KEYS['cloudinary'], $meta ); } /** @@ -1898,14 +1909,17 @@ public function update_post_meta( $post_id, $key, $data ) { * * @param int $post_id The attachment ID. * @param string $key The meta key to get. + * + * @return bool */ public function delete_post_meta( $post_id, $key ) { - $meta_data = wp_get_attachment_metadata( $post_id, true ); - if ( is_array( $meta_data ) && isset( $meta_data[ Sync::META_KEYS['cloudinary'] ] ) && is_array( $meta_data[ Sync::META_KEYS['cloudinary'] ] ) ) { - // Only do this side if has been set before. - unset( $meta_data[ Sync::META_KEYS['cloudinary'] ][ $key ] ); - wp_update_attachment_metadata( $post_id, $meta_data ); + + $meta = $this->get_post_meta( $post_id ); + if ( isset( $meta[ $key ] ) ) { + unset( $meta[ $key ] ); } + + return update_post_meta( $post_id, Sync::META_KEYS['cloudinary'], $meta ); } /** diff --git a/php/class-sync.php b/php/class-sync.php index 31a66b978..a8dadfe19 100644 --- a/php/class-sync.php +++ b/php/class-sync.php @@ -160,24 +160,51 @@ public function been_synced( $attachment_id ) { /** * Checks if an asset is synced and up to date. * - * @param int $post_id The post id to check. + * @param int $post_id The post id to check. + * @param bool $re_check Flag to bypass cache and recheck. * * @return bool */ - public function is_synced( $post_id ) { - $expecting = $this->generate_signature( $post_id ); - - if ( ! is_wp_error( $expecting ) ) { - $signature = $this->get_signature( $post_id ); - // Sort to align orders for comparison. - ksort( $signature ); - ksort( $expecting ); - if ( ! empty( $signature ) && ! empty( $expecting ) && $expecting === $signature ) { - return true; + public function is_synced( $post_id, $re_check = false ) { + static $synced = array(); + if ( isset( $synced[ $post_id ] ) && false === $re_check ) { + return $synced[ $post_id ]; + } + $return = false; + if ( $this->managers['media']->has_public_id( $post_id ) ) { + $expecting = $this->generate_signature( $post_id ); + if ( ! is_wp_error( $expecting ) ) { + $signature = $this->get_signature( $post_id ); + // Sort to align orders for comparison. + ksort( $signature ); + ksort( $expecting ); + if ( ! empty( $signature ) && ! empty( $expecting ) && $expecting === $signature ) { + $return = true; + } } } + $synced[ $post_id ] = $return; - return false; + return $synced[ $post_id ]; + } + + /** + * Log a sync result. + * + * @param int $attachment_id The attachment id. + * @param string $type The sync type. + * @param mixed $result The result. + */ + public function log_sync_result( $attachment_id, $type, $result ) { + $log = $this->managers['media']->get_post_meta( $attachment_id, self::META_KEYS['process_log'], true ); + $keys = array_keys( $this->sync_base_struct ); + if ( empty( $log ) || count( $log ) !== count( $keys ) ) { + $log = array_fill_keys( $keys, array() ); + } + if ( isset( $log[ $type ] ) ) { + $log[ $type ] = $result; + $this->managers['media']->update_post_meta( $attachment_id, self::META_KEYS['process_log'], $log ); + } } /** @@ -512,7 +539,7 @@ public function setup_sync_base_struct() { 'sync' => function ( $attachment_id ) { $meta = $this->managers['media']->get_post_meta( $attachment_id ); foreach ( $meta as $key => $value ) { - if ( self::META_KEYS['public_id'] === $key ) { + if ( Sync::META_KEYS['cloudinary'] === $key ) { $this->managers['media']->delete_post_meta( $attachment_id, $key ); continue; } @@ -665,10 +692,17 @@ public function sync_base( $post ) { * @return string | null */ public function maybe_prepare_sync( $attachment_id ) { - - $type = $this->get_sync_type( $attachment_id ); - if ( $type && $this->can_sync( $attachment_id, $type ) ) { - $this->add_to_sync( $attachment_id ); + $type = null; + if ( $this->can_sync( $attachment_id, $type ) ) { + $type = $this->get_sync_type( $attachment_id ); + if ( $type ) { + $this->add_to_sync( $attachment_id ); + } else { + // if null, and can sync but has no type, realtime syncs may have been applied. so recheck. + if ( $this->is_synced( $attachment_id, true ) ) { + $type = true; + } + } } return $type; diff --git a/php/media/class-upgrade.php b/php/media/class-upgrade.php index 19f3f23dc..a0289c832 100644 --- a/php/media/class-upgrade.php +++ b/php/media/class-upgrade.php @@ -98,7 +98,7 @@ public function convert_cloudinary_version( $attachment_id ) { if ( empty( $public_id ) ) { $public_id = implode( '/', $parts ); } - update_post_meta( $attachment_id, Sync::META_KEYS['public_id'], $public_id ); + $this->media->update_post_meta( $attachment_id, Sync::META_KEYS['public_id'], $public_id ); $this->media->update_post_meta( $attachment_id, Sync::META_KEYS['version'], $asset_version ); if ( ! empty( $asset_transformations ) ) { $this->media->update_post_meta( $attachment_id, Sync::META_KEYS['transformation'], $asset_transformations ); @@ -115,7 +115,7 @@ public function convert_cloudinary_version( $attachment_id ) { } $public_id .= $suffix; $this->media->delete_post_meta( $attachment_id, Sync::META_KEYS['suffix'] ); - update_post_meta( $attachment_id, Sync::META_KEYS['public_id'], $public_id ); + $this->media->update_post_meta( $attachment_id, Sync::META_KEYS['public_id'], $public_id ); } // Check folder sync in order. if ( $this->media->is_folder_synced( $attachment_id ) ) { @@ -142,8 +142,8 @@ public function convert_cloudinary_version( $attachment_id ) { if ( ! empty( $transformations ) ) { $sync_key .= wp_json_encode( $transformations ); } - update_post_meta( $attachment_id, '_' . md5( $sync_key ), true ); - update_post_meta( $attachment_id, '_' . md5( 'base_' . $public_id ), true ); + delete_post_meta( $attachment_id, '_' . md5( $sync_key ), true ); + delete_post_meta( $attachment_id, '_' . md5( 'base_' . $public_id ), true ); // Get a new uncached signature. $this->sync->get_signature( $attachment_id, true ); diff --git a/php/sync/class-download-sync.php b/php/sync/class-download-sync.php index 932e0e5d9..77c05a37b 100644 --- a/php/sync/class-download-sync.php +++ b/php/sync/class-download-sync.php @@ -174,21 +174,8 @@ public function download_asset( $attachment_id, $source = null, $date = null ) { // Prepare the asset. update_attached_file( $attachment_id, $upload['file'] ); - $old_meta = wp_get_attachment_metadata( $attachment_id ); - ob_start(); // Catch possible errors in WordPress's ID3 module when setting meta for transformed videos. - $meta = wp_generate_attachment_metadata( $attachment_id, $upload['file'] ); - $captured_errors = ob_get_clean(); // Capture issues. - // Be sure to record v2 meta. - if ( ! empty( $old_meta[ Sync::META_KEYS['cloudinary'] ] ) ) { - $meta[ Sync::META_KEYS['cloudinary'] ] = $old_meta[ Sync::META_KEYS['cloudinary'] ]; - } else { - // Maybe capture newest meta. - $maybe_new = wp_get_attachment_metadata( $attachment_id ); - if ( ! empty( $maybe_new[ Sync::META_KEYS['cloudinary'] ] ) ) { - $meta[ Sync::META_KEYS['cloudinary'] ] = $maybe_new[ Sync::META_KEYS['cloudinary'] ]; - } - } - wp_update_attachment_metadata( $attachment_id, $meta ); + wp_generate_attachment_metadata( $attachment_id, $upload['file'] ); + // Update the folder synced flag. $public_id = $this->media->get_public_id( $attachment_id ); $asset_folder = strpos( $public_id, '/' ) ? dirname( $public_id ) : '/'; diff --git a/php/sync/class-push-sync.php b/php/sync/class-push-sync.php index 979581c6b..391741ca7 100644 --- a/php/sync/class-push-sync.php +++ b/php/sync/class-push-sync.php @@ -194,8 +194,6 @@ public function process_assets( $attachments = array() ) { $ids = array_map( 'intval', (array) $attachments ); // Handle based on Sync Type. foreach ( $ids as $attachment_id ) { - // Create synced post meta as a way to search for synced / unsynced items. - update_post_meta( $attachment_id, Sync::META_KEYS['public_id'], $this->media->get_public_id( $attachment_id ) ); // Skip external media. if ( ! $this->media->is_local_media( $attachment_id ) ) { @@ -209,21 +207,23 @@ public function process_assets( $attachments = array() ) { update_post_meta( $attachment_id, Sync::META_KEYS['syncing'], time() ); $stat[ $attachment_id ] = array(); while ( $type = $this->sync->get_sync_type( $attachment_id, false ) ) { // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition + // translators: variable is sync type. + $action_message = sprintf( __( 'Sync type: %s', 'cloudinary' ), $type ); + do_action( '_cloudinary_queue_action', $action_message ); if ( isset( $stat[ $attachment_id ][ $type ] ) ) { // Loop prevention. break; } - $stat[ $attachment_id ][ $type ] = $this->sync->run_sync_method( $type, 'sync', $attachment_id ); - } - $prev_stat = $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['process_log'], true ); - if ( empty( $prev_stat ) ) { - $prev_stat = array(); + $stat[ $attachment_id ][ $type ] = true; + $result = $this->sync->run_sync_method( $type, 'sync', $attachment_id ); + if ( ! empty( $result ) ) { + $this->sync->log_sync_result( $attachment_id, $type, $result ); + } } - $stat[ $attachment_id ] = array_merge( $prev_stat, $stat ); + // remove pending. delete_post_meta( $attachment_id, Sync::META_KEYS['pending'] ); - // Record Process log. - $this->media->update_post_meta( $attachment_id, Sync::META_KEYS['process_log'], $stat[ $attachment_id ] ); + // Remove processing flag. delete_post_meta( $attachment_id, Sync::META_KEYS['syncing'] ); diff --git a/php/sync/class-upload-sync.php b/php/sync/class-upload-sync.php index 2362776a9..d9362db4d 100644 --- a/php/sync/class-upload-sync.php +++ b/php/sync/class-upload-sync.php @@ -277,14 +277,11 @@ function ( $is_synced, $post_id ) use ( $attachment_id ) { // Set folder Synced. $this->media->update_post_meta( $attachment_id, Sync::META_KEYS['folder_sync'], $this->media->is_folder_synced( $attachment_id ) ); // Set public_id. - update_post_meta( $attachment_id, Sync::META_KEYS['public_id'], $public_id ); + $this->media->update_post_meta( $attachment_id, Sync::META_KEYS['public_id'], $result['public_id'] ); // Set version. $this->media->update_post_meta( $attachment_id, Sync::META_KEYS['version'], $result['version'] ); // Set the delivery type. $this->media->update_post_meta( $attachment_id, Sync::META_KEYS['delivery'], $result['type'] ); - // Set traceable sync keys. - update_post_meta( $attachment_id, '_' . md5( $options['public_id'] ), true ); - update_post_meta( $attachment_id, '_' . md5( 'base_' . $options['public_id'] ), true ); // Update signature for all that use the same method. $this->sync->sync_signature_by_type( $attachment_id, $type ); // Update options and public_id as well (full sync). diff --git a/php/traits/trait-cli.php b/php/traits/trait-cli.php index 9e6b071d1..9a8180f98 100644 --- a/php/traits/trait-cli.php +++ b/php/traits/trait-cli.php @@ -206,7 +206,6 @@ protected function process_sync( $posts, $total ) { $this->plugin->get_component( 'sync' )->managers['push']->process_assets( $asset, $bar ); } delete_post_meta( $asset, '_cld_unsynced', true ); - $bar->tick(); } // Done message - reanalyze. if ( $done === $total ) { From 3d95d364f0af3fec98a25e437826b634a0672aa6 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Thu, 24 Jun 2021 14:44:31 +0200 Subject: [PATCH 02/10] change meta check --- php/sync/class-sync-queue.php | 2 +- php/ui/component/class-media-status.php | 2 +- php/ui/component/class-sync.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/php/sync/class-sync-queue.php b/php/sync/class-sync-queue.php index 30edb2689..29d006748 100644 --- a/php/sync/class-sync-queue.php +++ b/php/sync/class-sync-queue.php @@ -299,7 +299,7 @@ public function build_queue() { 'compare' => 'NOT EXISTS', ), array( - 'key' => Sync::META_KEYS['public_id'], + 'key' => Sync::META_KEYS['cloudinary'], 'compare' => 'NOT EXISTS', ), array( diff --git a/php/ui/component/class-media-status.php b/php/ui/component/class-media-status.php index 917f82a75..21e204540 100644 --- a/php/ui/component/class-media-status.php +++ b/php/ui/component/class-media-status.php @@ -161,7 +161,7 @@ protected function get_total_synced_media() { 'posts_per_page' => 1, 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery array( - 'key' => Sync::META_KEYS['public_id'], + 'key' => Sync::META_KEYS['cloudinary'], 'compare' => 'EXISTS', ), diff --git a/php/ui/component/class-sync.php b/php/ui/component/class-sync.php index 991ef44f8..95503aa4e 100644 --- a/php/ui/component/class-sync.php +++ b/php/ui/component/class-sync.php @@ -151,7 +151,7 @@ protected function count_to_sync() { 'posts_per_page' => 1, 'meta_query' => array( // phpcs:ignore array( - 'key' => \Cloudinary\Sync::META_KEYS['public_id'], + 'key' => \Cloudinary\Sync::META_KEYS['cloudinary'], 'compare' => 'NOT EXISTS', ), From ce5ae19a536be77f9fbb9a3b530a357fd025e961 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Thu, 24 Jun 2021 15:11:08 +0200 Subject: [PATCH 03/10] upload via url if not a local file --- php/connect/class-api.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/php/connect/class-api.php b/php/connect/class-api.php index 21767968b..45e43b852 100644 --- a/php/connect/class-api.php +++ b/php/connect/class-api.php @@ -396,6 +396,12 @@ function_exists( 'wp_get_original_image_url' ) && } else { $file_url = wp_get_attachment_url( $attachment_id ); } + if ( ! is_local_attachment( $attachment_id ) ) { + $disable_https_fetch = false; // Remote can upload via url. + // translators: variable is thread name and queue size. + $action_message = sprintf( __( 'Uploading remote url: %1$s.', 'cloudinary' ), $file_url ); + do_action( '_cloudinary_queue_action', $action_message ); + } $media = get_plugin_instance()->get_component( 'media' ); $tempfile = false; if ( $media && $media->is_cloudinary_url( $file_url ) ) { From 2e445197b0a99c80c5d5b802a2bf2156ccd240d0 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Sat, 26 Jun 2021 06:56:57 +0200 Subject: [PATCH 04/10] add limit and unfilter meta --- php/class-media.php | 2 +- php/class-sync.php | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/php/class-media.php b/php/class-media.php index 85971e9a9..2c414a2c2 100644 --- a/php/class-media.php +++ b/php/class-media.php @@ -1846,7 +1846,7 @@ public function get_post_meta( $post_id, $key = '', $single = false ) { $meta = get_post_meta( $post_id, Sync::META_KEYS['cloudinary'], true ); if ( empty( $meta ) ) { $meta = array(); - $old_meta = wp_get_attachment_metadata( $post_id ); + $old_meta = wp_get_attachment_metadata( $post_id, true ); if ( isset( $old_meta[ Sync::META_KEYS['cloudinary'] ] ) ) { $meta = $old_meta[ Sync::META_KEYS['cloudinary'] ]; // Add public ID. diff --git a/php/class-sync.php b/php/class-sync.php index a8dadfe19..3a30d168f 100644 --- a/php/class-sync.php +++ b/php/class-sync.php @@ -202,7 +202,11 @@ public function log_sync_result( $attachment_id, $type, $result ) { $log = array_fill_keys( $keys, array() ); } if ( isset( $log[ $type ] ) ) { - $log[ $type ] = $result; + + $log[ $type ][ time() ] = $result; + if ( 5 < count( $log[ $type ] ) ) { + array_shift( $log[ $type ] ); + } $this->managers['media']->update_post_meta( $attachment_id, self::META_KEYS['process_log'], $log ); } } From bd021057f19d340204bc5c6a0eb1c811f6565567 Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Wed, 30 Jun 2021 12:29:45 +0100 Subject: [PATCH 05/10] Cleanup --- php/class-media.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php/class-media.php b/php/class-media.php index 85971e9a9..020db038f 100644 --- a/php/class-media.php +++ b/php/class-media.php @@ -955,7 +955,7 @@ public function default_image_freeform_transformations( $default ) { * @return string The converted URL. */ public function cloudinary_url( $attachment_id, $size = array(), $transformations = array(), $cloudinary_id = null, $overwrite_transformations = false ) { - if ( ! ( $cloudinary_id ) ) { + if ( ! $cloudinary_id ) { $cloudinary_id = $this->cloudinary_id( $attachment_id ); if ( ! $cloudinary_id ) { return null; From f1d56121e158f6479f3a55ac2929dbcbb476f442 Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Wed, 30 Jun 2021 12:30:35 +0100 Subject: [PATCH 06/10] Improve output consistency --- php/class-media.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/php/class-media.php b/php/class-media.php index 020db038f..76015c6a6 100644 --- a/php/class-media.php +++ b/php/class-media.php @@ -1159,7 +1159,7 @@ public function get_cloudinary_id( $attachment_id ) { * * @param int $attachment_id The ID to get Cloudinary id for. * - * @return string|false the ID or null if not existing. + * @return string|false the ID or false if not existing. */ public function cloudinary_id( $attachment_id ) { static $cloudinary_ids = array(); @@ -1170,8 +1170,8 @@ public function cloudinary_id( $attachment_id ) { } if ( ! $this->is_media( $attachment_id ) ) { - $cloudinary_ids[ $attachment_id ] = null; - return null; + $cloudinary_ids[ $attachment_id ] = false; + return false; } if ( ! $this->sync->is_synced( $attachment_id ) && ! defined( 'REST_REQUEST' ) ) { From c714cb09c75722eb8aead5497046f645f1041002 Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Wed, 30 Jun 2021 12:31:26 +0100 Subject: [PATCH 07/10] Default get meta to null --- php/class-media.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php/class-media.php b/php/class-media.php index 76015c6a6..8304a7076 100644 --- a/php/class-media.php +++ b/php/class-media.php @@ -1857,7 +1857,7 @@ public function get_post_meta( $post_id, $key = '', $single = false ) { } } if ( '' !== $key ) { - $meta = isset( $meta[ $key ] ) ? $meta[ $key ] : ''; + $meta = isset( $meta[ $key ] ) ? $meta[ $key ] : null; } return $single ? $meta : (array) $meta; From 9f59afc560ba8e674b5cd07bc27a3e37799ee5d1 Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Wed, 30 Jun 2021 12:32:00 +0100 Subject: [PATCH 08/10] Get single key for delivery type --- php/class-sync.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php/class-sync.php b/php/class-sync.php index a8dadfe19..63589bb54 100644 --- a/php/class-sync.php +++ b/php/class-sync.php @@ -499,7 +499,7 @@ public function setup_sync_base_struct() { 'priority' => 25, 'sync' => array( $this->managers['upload'], 'explicit_update' ), 'validate' => function ( $attachment_id ) { - $delivery = $this->managers['media']->get_post_meta( $attachment_id, self::META_KEYS['delivery'] ); + $delivery = $this->managers['media']->get_post_meta( $attachment_id, self::META_KEYS['delivery'], true ); return empty( $delivery ) || 'upload' === $delivery; }, From 23ab2c0f71e652eb8843d360095546c3b496a331 Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Wed, 30 Jun 2021 12:32:35 +0100 Subject: [PATCH 09/10] Change the source of information into the newly added Cloudinary meta key --- php/class-report.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/php/class-report.php b/php/class-report.php index fe7beb66e..b9de63d41 100644 --- a/php/class-report.php +++ b/php/class-report.php @@ -163,7 +163,8 @@ public function image_meta_viewer() { */ public function render( $post ) { if ( 'attachment' === $post->post_type ) { - $meta = wp_get_attachment_metadata( $post->ID ); + $sync = $this->plugin->get_component( 'sync' ); + $meta = get_post_meta( $post->ID, $sync::META_KEYS['cloudinary'], true ); $args = array( 'type' => 'tag', From fca6001572c136bcc6f002e0006b15500ba654e2 Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Wed, 30 Jun 2021 12:33:06 +0100 Subject: [PATCH 10/10] Use plugin internal logic to check if the media is local --- php/connect/class-api.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/php/connect/class-api.php b/php/connect/class-api.php index 45e43b852..147412e11 100644 --- a/php/connect/class-api.php +++ b/php/connect/class-api.php @@ -396,13 +396,13 @@ function_exists( 'wp_get_original_image_url' ) && } else { $file_url = wp_get_attachment_url( $attachment_id ); } - if ( ! is_local_attachment( $attachment_id ) ) { + $media = get_plugin_instance()->get_component( 'media' ); + if ( ! $media->is_local_media( $attachment_id ) ) { $disable_https_fetch = false; // Remote can upload via url. // translators: variable is thread name and queue size. $action_message = sprintf( __( 'Uploading remote url: %1$s.', 'cloudinary' ), $file_url ); do_action( '_cloudinary_queue_action', $action_message ); } - $media = get_plugin_instance()->get_component( 'media' ); $tempfile = false; if ( $media && $media->is_cloudinary_url( $file_url ) ) { // If this is a Cloudinary URL, then we can use it to fetch from that location.