Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Solar committed Jun 24, 2024
1 parent 09d10c9 commit 969872c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions includes/class-windows-azure-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ static public function put_media_to_blob_storage( $container_name, $blob_name, $
* @param string $source_path Local path.
* @param string $account_name Account name.
* @param string $account_key Account key.
* @param int $cache Max-age cache
*
* @return bool|string|WP_Error False or WP_Error on failure URI on success.
*/
Expand Down
32 changes: 27 additions & 5 deletions includes/class-windows-azure-replace-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class Windows_Azure_Replace_Media {
*/
private $allowed_types = [];

/**
* Container name from plugin config
*
* @var string
*/
private $container_name = '';

/**
Expand All @@ -64,10 +69,17 @@ public function __construct() {
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_replace_media_script' ) );

// ajax event to replace media
add_action( 'wp_ajax_nopriv_azure-storage-media-replace', array( $this, 'process_media_replacement' ) );
add_action( 'wp_ajax_azure-storage-media-replace', array( $this, 'process_media_replacement' ) );

// Set allowed mime types to be replaced
/**
* Set a list of mime types allowed to be replaced.
*
* azure_blob_storage_allowed_types_replace filters the default list of mime types allowed to be replaced, for now just common images and pdf files.
*
* @since 4.2.3
*
* @param array $types array of allowed mime types
*/
$this->allowed_types = apply_filters(
'azure_blob_storage_allowed_types_replace',
array(
Expand Down Expand Up @@ -115,7 +127,7 @@ public function register_azure_fields_attachment_editor( $form_fields, $post ) {
* @return void
*/
public function enqueue_replace_media_script() {
$js_ext = ( ! defined( 'SCRIPT_DEBUG' ) || false === SCRIPT_DEBUG ) ? '.min.js' : '.js';
$js_ext = ( ! defined( 'SCRIPT_DEBUG' ) || false === SCRIPT_DEBUG ) ? '.min.js' : '.js';
wp_enqueue_script( 'windows-azure-storage-media-replace', MSFT_AZURE_PLUGIN_URL . 'js/windows-azure-storage-media-replace' . $js_ext, array( 'jquery', 'media-editor' ), MSFT_AZURE_PLUGIN_VERSION, true );

wp_localize_script(
Expand Down Expand Up @@ -300,8 +312,6 @@ public function process_media_thumbnails( $source_data, $replace_data ) {
}

if ( ! empty( $replace_data['meta_data']['sizes'] ) ) {
// Remove previous thumbnails
// $this->delete_previous_thumbnails( $source_data );

// Let's replace the file remotely
foreach ( $sizes as $source_size => $sizes_source_data ) {
Expand Down Expand Up @@ -363,6 +373,14 @@ public function delete_previous_thumbnails( $data ) {
* @return array
*/
private function find_nearest_size( $source_sizes, $target_sizes ) {
// Bail early if no attachment meta field
if ( empty( $source_sizes['meta_data'] ) || empty( $target_sizes['meta_data'] ) ) {
return;
}

if ( empty( $source_sizes['meta_data']['file'] ) || empty( $target_sizes['meta_data']['file'] ) ) {
return;
}

$convert_sizes = array();
$filename_path = $source_sizes['meta_data']['file'];
Expand All @@ -371,6 +389,10 @@ private function find_nearest_size( $source_sizes, $target_sizes ) {
$target_filename = $target_sizes['meta_data']['file'];
$target_file_path = dirname( $target_filename );

if ( empty( $source_sizes['meta_data']['sizes'] ) || empty( $target_sizes['meta_data']['sizes'] ) ) {
return;
}

foreach ( $source_sizes['meta_data']['sizes'] as $size => $size_data ) {
$target_width = ! empty( $target_sizes['meta_data']['sizes'][ $size ] ) ? $target_sizes['meta_data']['sizes'][ $size ]['width'] : 0;
$source_width = $size_data['width'];
Expand Down

0 comments on commit 969872c

Please sign in to comment.