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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,19 @@
max-width : 300px;
display : none;
}

&-status {
&-disabled {
color : $color-red;
}

&-enabled {
color : $color-green;
}

&-button.button {
vertical-align : baseline;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -119,30 +119,30 @@ public function verify_connection( $data ) {
if ( empty( $data['cloudinary_url'] ) ) {
delete_option( 'cloudinary_connection_signature' );

add_settings_error(
'cloudinary_connect',
'connection_error',
__( 'Connection to Cloudinary has been removed.', 'cloudinary' ),
'notice-warning'
add_settings_error(
'cloudinary_connect',
'connection_error',
__( 'Connection to Cloudinary has been removed.', 'cloudinary' ),
'notice-warning'
);

return $data;
}

$data['cloudinary_url'] = str_replace( 'CLOUDINARY_URL=', '', $data['cloudinary_url'] );
$current = $this->plugin->config['settings']['connect'];
$current = $this->plugin->config['settings']['connect'];

if ( $current['cloudinary_url'] === $data['cloudinary_url'] ) {
return $data;
}

// Pattern match to ensure validity of the provided url
if ( ! preg_match( '~^(?:CLOUDINARY_URL=)?cloudinary://[0-9]+:[A-Za-z_0-9]+@[A-Za-z]+~', $data['cloudinary_url'] ) ) {
add_settings_error(
'cloudinary_connect',
'format_mismatch',
__( 'The environment variable URL must be in this format: cloudinary://API_KEY:API_SECRET@CLOUD_NAME', 'cloudinary' ),
'error'
add_settings_error(
'cloudinary_connect',
'format_mismatch',
__( 'The environment variable URL must be in this format: cloudinary://API_KEY:API_SECRET@CLOUD_NAME', 'cloudinary' ),
'error'
);

return $current;
Expand All @@ -152,6 +152,7 @@ public function verify_connection( $data ) {

if ( ! empty( $result['message'] ) ) {
add_settings_error( 'cloudinary_connect', $result['type'], $result['message'], 'error' );

return $current;
}

Expand Down Expand Up @@ -318,6 +319,7 @@ public function setup() {
public function get_config() {
$signature = get_option( 'cloudinary_connection_signature', null );
$version = get_option( 'cloudinary_version' );

if ( empty( $signature ) || version_compare( $this->plugin->version, $version, '>' ) ) {
// Check if there's a previous version, or missing signature.
$cld_url = get_option( 'cloudinary_url', null );
Expand All @@ -332,10 +334,21 @@ public function get_config() {
$data = array(
'cloudinary_url' => $cld_url,
);
// Set auto sync off.
$sync = get_option( 'cloudinary_sync_media' );
if ( empty( $sync ) ) {
$sync = array(
'auto_sync' => '',
'cloudinary_folder' => '',
);
}
$sync['auto_sync'] = 'off';
update_option( 'cloudinary_sync_media', $sync );
delete_option( 'cloudinary_settings_cache' ); // remove the cache.
}

$data['cloudinary_url'] = str_replace( 'CLOUDINARY_URL=', '', $data['cloudinary_url'] );
$test = $this->test_connection( $data['cloudinary_url'] );
$test = $this->test_connection( $data['cloudinary_url'] );

if ( 'connection_success' === $test['type'] ) {
$signature = md5( $data['cloudinary_url'] );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1072,8 +1072,8 @@ public function media_column_value( $column_name, $attachment_id ) {
if ( 'cld_status' === $column_name ) {
if ( $this->is_media( $attachment_id ) ) {
$status = array(
'state' => 'success',
'note' => esc_html__( 'Synced', 'cloudinary' ),
'state' => 'inactive',
'note' => esc_html__( 'Not Synced', 'cloudinary' ),
);
if ( false === $this->cloudinary_id( $attachment_id ) ) {
// If false, lets check why by seeing if the file size is too large.
Expand All @@ -1085,6 +1085,11 @@ public function media_column_value( $column_name, $attachment_id ) {
$status['note'] = sprintf( __( 'File size exceeds the maximum of %s. This media asset will be served from WordPress.', 'cloudinary' ), $max_size_hr );
$status['state'] = 'error';
}
} else {
$status = array(
'state' => 'success',
'note' => esc_html__( 'Synced', 'cloudinary' ),
);
}
// filter status.
$status = apply_filters( 'cloudinary_media_status', $status, $attachment_id );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public function can_delete_asset( $all_caps, $caps, $args ) {
list( $request_cap, , $post_id ) = $args;

if ( 'delete_post' === $request_cap && ! empty( $all_caps['delete_posts'] ) && 'attachment' === get_post_type( $post_id ) ) {
if ( ! $this->plugin->components['sync']->is_synced( $post_id ) ) {

// Check if is pending.
if ( ! $this->plugin->components['sync']->is_synced( $post_id ) && $this->plugin->components['sync']->managers['upload']->is_pending( $post_id ) ) {
// Check for errors.
$has_error = $this->plugin->components['media']->get_post_meta( $post_id, Sync::META_KEYS['sync_error'], true );
if ( empty( $has_error ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Upload_Sync {
* @param bool $enabled Is this feature enabled.
* @param object $pusher An object that implements `push_attachments`. Default: null.
*/
public function __construct( \Cloudinary\Plugin $plugin, $enabled = true, $pusher = null ) {
public function __construct( \Cloudinary\Plugin $plugin, $enabled = false, $pusher = null ) {
$this->plugin = $plugin;
$this->pusher = $pusher;
$this->enabled = $enabled;
Expand All @@ -71,6 +71,94 @@ private function register_hooks() {
add_filter( 'cloudinary_media_status', array( $this, 'filter_status' ), 10, 2 );
// Hook for on demand upload push.
add_action( 'shutdown', array( $this, 'init_background_upload' ) );
// Hook into auto upload sync.
add_filter( 'cloudinary_on_demand_sync_enabled', array( $this, 'auto_sync_enabled' ) );
// Handle bulk and inline actions.
add_filter( 'handle_bulk_actions-upload', array( $this, 'handle_bulk_actions' ), 10, 3 );
// Add inline action.
add_filter( 'media_row_actions', array( $this, 'add_inline_action' ), 10, 2 );

// Add Bulk actions.
add_filter( 'bulk_actions-upload', function ( $actions ) {
$cloudinary_actions = array(
'cloudinary-push' => __( 'Push to Cloudinary', 'cloudinary' ),
);

return array_merge( $cloudinary_actions, $actions );
} );
}

/**
* Add an inline action for manual sync.
*
* @param array $actions All actions.
* @param \WP_Post $post The current post object.
*
* @return array
*/
function add_inline_action( $actions, $post ) {
if ( ! $this->plugin->components['sync']->is_synced( $post->ID ) ) {
if ( current_user_can( 'delete_post', $post->ID ) ) {
$action_url = add_query_arg(
array(
'action' => 'cloudinary-push',
'media[]' => $post->ID,
'_wpnonce' => wp_create_nonce( 'bulk-media' ),
),
'upload.php'
);

$actions['cloudinary-push'] = sprintf(
'<a href="%s" aria-label="%s">%s</a>',
$action_url,
/* translators: %s: Attachment title. */
esc_attr( sprintf( __( 'Push to Cloudinary &#8220;%s&#8221;' ), 'asd' ) ),
__( 'Push to Cloudinary', 'cloudinary' )
);
}
}

return $actions;
}

/**
* Handles bulk actions for attachments.
*
* @param string $location The location to redirect after.
* @param string $action The action to handle.
* @param array $post_ids Post ID's to action.
*
* @return string
*/
public function handle_bulk_actions( $location, $action, $post_ids ) {

switch ( $action ) {
case 'cloudinary-push' :
foreach ( $post_ids as $post_id ) {
if ( ! $this->plugin->components['sync']->is_synced( $post_id ) ) {
$this->prep_upload( $post_id );
}
}
break;
}

return $location;

}

/**
* Check if auto-sync is enabled.
*
* @param bool $enabled Flag to determine if autosync is enabled.
*
* @return bool
*/
public function auto_sync_enabled( $enabled ) {
if ( isset( $this->plugin->config['settings']['sync_media']['auto_sync'] ) && 'on' === $this->plugin->config['settings']['sync_media']['auto_sync'] ) {
$enabled = true;
}

return $enabled;
}

/**
Expand Down Expand Up @@ -114,25 +202,34 @@ public function prep_on_demand_upload( $cloudinary_id, $attachment_id ) {
if ( $attachment_id && false === $cloudinary_id ) {
// Check that this has not already been prepared for upload.
if ( ! $this->is_pending( $attachment_id ) && apply_filters( 'cloudinary_on_demand_sync_enabled', $this->enabled ) ) {
$max_size = ( wp_attachment_is_image( $attachment_id ) ? 'max_image_size' : 'max_video_size' );
$file = get_attached_file( $attachment_id );
// Get the file size to make sure it can exist in cloudinary.
if ( file_exists( $file ) && filesize( $file ) < $this->plugin->components['connect']->usage[ $max_size ] ) {
$this->add_to_sync( $attachment_id );
} else {
// Check if the src is a url.
$file = get_post_meta( $attachment_id, '_wp_attached_file', true );
if ( $this->plugin->components['media']->is_cloudinary_url( $file ) ) {
// Download sync.
$this->add_to_sync( $attachment_id );
}
}
$this->prep_upload( $attachment_id );
}
}

return $cloudinary_id;
}

/**
* Prep an attachment for upload.
*
* @param int $attachment_id The attachment ID to prep for upload.
*/
public function prep_upload( $attachment_id ) {
$max_size = ( wp_attachment_is_image( $attachment_id ) ? 'max_image_size' : 'max_video_size' );
$file = get_attached_file( $attachment_id );
// Get the file size to make sure it can exist in cloudinary.
if ( file_exists( $file ) && filesize( $file ) < $this->plugin->components['connect']->usage[ $max_size ] ) {
$this->add_to_sync( $attachment_id );
} else {
// Check if the src is a url.
$file = get_post_meta( $attachment_id, '_wp_attached_file', true );
if ( $this->plugin->components['media']->is_cloudinary_url( $file ) ) {
// Download sync.
$this->add_to_sync( $attachment_id );
}
}
}

/**
* Add an attachment ID to the to_sync array.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@
'hide_button' => true,
'requires_config' => true,
'fields' => array(
'auto_sync' => array(
'label' => __( 'Auto-Sync', 'cloudinary' ),
'suffix' => __( 'Enable Auto-Sync', 'cloudinary' ) . ' <button type="submit" class="button button-small sync-status-button">' . __( 'Update Auto-Sync', 'cloudinary' ) . '</button>',
'description' => __( 'When enabled, all assets will be automatically pushed to your Cloudinary account on demand.', 'cloudinary' ),
'type' => 'checkbox',
'default' => 'on',
),
'cloudinary_folder' => array(
'label' => __( 'Cloudinary folder path', 'cloudinary' ),
'placeholder' => __( 'e.g.: wordpress_assets/', 'cloudinary' ),
'placeholder' => __( 'e.g.: wordpress_assets/', 'cloudinary' ),
'description' => __( 'Specify the folder in your Cloudinary account where WordPress assets are uploaded to. All assets uploaded to WordPress from this point on will be synced to the specified folder in Cloudinary. Leave blank to use the root of your Cloudinary library.', 'cloudinary' ),
'sanitize_callback' => array( '\Cloudinary\Media', 'sanitize_cloudinary_folder' ),
'suffix' => '<button type="submit" class="button button-primary">' . __( 'Save folder settings', 'cloudinary' ) . '</button>',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,25 @@
* @package Cloudinary
*/

$autosync = false;
if ( isset( $this->plugin->config['settings']['sync_media']['auto_sync'] ) && 'on' === $this->plugin->config['settings']['sync_media']['auto_sync'] ) {
$autosync = true;
}
?>
<?php if ( ! empty( $this->plugin->config['connect'] ) ) : ?>
<div class="settings-tab-section-card">
<div class="settings-tab-section-fields-dashboard-success">
<?php esc_html_e( 'Auto Sync', 'cloudinary' ); ?>
<p class="description">
<?php esc_html_e( 'All of your assets will be kept in-sync with Cloudinary automatically when you connect your account. Existing WordPress assets will be uploaded to the Cloudinary folder specified below and any assets that exist in the Cloudinary folder will sync back to local storage on WordPress. Auto-syncing allows all assets to be available for delivery from your WordPress Media Library in case the plugin is disabled.', 'cloudinary' ); ?><br>
</p>
<?php if ( true === $autosync ) : ?>
<span class="sync-status-enabled"><?php esc_html_e( 'Auto Sync is on', 'cloudinary' ); ?></span>
<p class="description">
<?php esc_html_e( 'All of your assets will be kept in-sync with Cloudinary automatically when you connect your account. Existing WordPress assets will be uploaded to the Cloudinary folder specified below and any assets that exist in the Cloudinary folder will sync back to local storage on WordPress. Auto-syncing allows all assets to be available for delivery from your WordPress Media Library in case the plugin is disabled.', 'cloudinary' ); ?><br>
</p>
<?php else: ?>
<span class="sync-status-disabled"><?php esc_html_e( 'Auto Sync is off', 'cloudinary' ); ?></span>
<p class="description">
<?php esc_html_e( 'Only selected assets will be kept in-sync with Cloudinary when you connect your account. Selected assets will be uploaded to the Cloudinary folder specified below. Selected assets to be available for delivery from your WordPress Media Library in case the plugin is disabled.', 'cloudinary' ); ?><br>
</p>
<?php endif; ?>
</div>
</div>
<?php endif; ?>