Skip to content

Commit

Permalink
Revert "Fix duplicate media creation."
Browse files Browse the repository at this point in the history
This reverts commit 6c8f0f4.
  • Loading branch information
iamdharmesh committed Jan 23, 2025
1 parent 8bcd31f commit 0cbd14d
Showing 1 changed file with 3 additions and 56 deletions.
59 changes: 3 additions & 56 deletions includes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,6 @@ function excluded_meta() {
'dt_original_post_url',
'dt_original_post_id',
'dt_original_blog_id',
'dt_original_media_url',
'dt_original_media_id',
'dt_connection_map',
'_wp_old_slug',
'_wp_old_date',
Expand Down Expand Up @@ -740,8 +738,7 @@ function set_media( $post_id, $media, $args = [] ) {

foreach ( $media as $media_item ) {

$args['original_media_id'] = $media_item['id'];
$args['source_file'] = $media_item['source_file'];
$args['source_file'] = $media_item['source_file'];

// Delete duplicate if it exists (unless filter says otherwise)
/**
Expand Down Expand Up @@ -889,9 +886,8 @@ function process_media( $url, $post_id, $args = [] ) {
$args = wp_parse_args(
$args,
[
'use_filesystem' => false,
'source_file' => '',
'original_media_id' => 0,
'use_filesystem' => false,
'source_file' => '',
]
);

Expand Down Expand Up @@ -947,15 +943,6 @@ function process_media( $url, $post_id, $args = [] ) {
return false;
}

// Check if the media is already existing on the site. If it is, return the media ID.
if ( ! empty( $args['original_media_id'] ) && ! empty( $url ) ) {
$existing_media_id = get_attachment_id_by_original_data( $url, $args['original_media_id'] );

if ( $existing_media_id ) {
return $existing_media_id;
}
}

$file_array = array();
$file_array['name'] = $media_name;

Expand Down Expand Up @@ -1056,46 +1043,6 @@ function process_media( $url, $post_id, $args = [] ) {
return (int) $result;
}

/**
* Get existing media ID based on the original source URL and original media ID.
*
* @param string $original_url The original source URL.
* @param int $original_id The original media ID.
* @return int|bool The existing media ID or false if not found.
*/
function get_attachment_id_by_original_data( $original_url, $original_id ) {
$attachments_query = new \WP_Query(
array(
'post_type' => 'attachment',
'post_status' => 'any',
'posts_per_page' => 1,
'fields' => 'ids',
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
'relation' => 'AND',
array(
'key' => 'dt_original_media_id',
'value' => $original_id,
'compare' => '=',
),
array(
'key' => 'dt_original_media_url',
'value' => $original_url,
'compare' => '=',
),
),
)
);

if ( ! empty( $attachments_query->posts ) && ! empty( $attachments_query->posts[0] ) ) {
return (int) $attachments_query->posts[0];
}

return false;
}

/**
* Find and update an image tag.
*
Expand Down

0 comments on commit 0cbd14d

Please sign in to comment.