Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:10up/distributor into feature/up…
Browse files Browse the repository at this point in the history
…date-in-content-image-urls
  • Loading branch information
iamdharmesh committed Jan 31, 2025
2 parents 45afeb2 + 813961a commit 9ec968a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 16 deletions.
56 changes: 52 additions & 4 deletions includes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@ 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 @@ -737,22 +739,21 @@ function set_media( $post_id, $media, $args = [] ) {
$image_urls_to_update = [];

foreach ( $media as $media_item ) {

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

// Delete duplicate if it exists (unless filter says otherwise)
// Delete duplicate if it exists (if filter set to true)
/**
* Filter whether media should be deleted and replaced if it already exists.
*
* @since 1.0.0
* @hook dt_sync_media_delete_and_replace
*
* @param {bool} true Whether pre-existing media should be deleted and replaced. Default `true`.
* @param {bool} true Whether pre-existing media should be deleted and replaced. Default `false`.
* @param {int} $post_id The post ID.
*
* @return {bool} Whether pre-existing media should be deleted and replaced.
*/
if ( apply_filters( 'dt_sync_media_delete_and_replace', true, $post_id ) ) {
if ( apply_filters( 'dt_sync_media_delete_and_replace', false, $post_id ) ) {
if ( ! empty( $current_media[ $media_item['source_url'] ] ) ) {
wp_delete_attachment( $current_media[ $media_item['source_url'] ], true );
}
Expand All @@ -761,6 +762,13 @@ function set_media( $post_id, $media, $args = [] ) {
} else {
if ( ! empty( $current_media[ $media_item['source_url'] ] ) ) {
$image_id = $current_media[ $media_item['source_url'] ];
} elseif ( ! empty( $media_item['id'] ) && ! empty( $media_item['source_url'] ) ) {
// Check if the media is already existing on the site. If it is, return the media ID.
$image_id = get_attachment_id_by_original_data( $media_item['id'], $media_item['source_url'] );

if ( ! $image_id ) {
$image_id = process_media( $media_item['source_url'], $post_id, $args );
}
} else {
$image_id = process_media( $media_item['source_url'], $post_id, $args );
}
Expand Down Expand Up @@ -1213,6 +1221,46 @@ function update_content_image_urls( int $post_id, array $images ) {
);
}

/**
* Get existing media ID based on the original source URL and original media ID.
*
* @param int $original_id The original media ID.
* @param string $original_url The original source URL.
* @return int|bool The existing media ID or false if not found.
*/
function get_attachment_id_by_original_data( $original_id, $original_url ) {
$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' => basename( $original_url ),
'compare' => 'LIKE', // Using LIKE to account different source URLs for the same media based on from where it was pulled/pushed. see https://core.trac.wordpress.org/ticket/25650
),
),
)
);

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

return false;
}

/**
* Return whether a post type is compatible with the block editor.
*
Expand Down
41 changes: 29 additions & 12 deletions tests/php/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -777,13 +777,16 @@ public function test_format_media_no_attachment_meta() {
* @since 1.0
* @group Utils
* @runInSeparateProcess
* @dataProvider data_set_media
*
* @param int $new_image_id ID of the new image.
* @param string $source_url Source URL of the media item.
* @param int $existing_media_id Existing media ID to test for existing media.
*/
public function test_set_media() {
public function test_set_media( $new_image_id, $source_url, $existing_media_id ) {
$post_id = 1;
$media_item = $this->test_format_media_featured();

$new_image_id = 5;

$attached_media_post = new \stdClass();
$attached_media_post->ID = 3;
$attached_media_post->post_parent = 10;
Expand Down Expand Up @@ -826,18 +829,11 @@ public function test_set_media() {
]
);

\WP_Mock::userFunction(
'wp_delete_attachment', [
'times' => 1,
'args' => [ $attached_media_post->ID, true ],
]
);

\WP_Mock::userFunction(
'get_post_meta', [
'times' => 1,
'args' => [ $attached_media_post->ID, 'dt_original_media_url', true ],
'return' => 'http://mediaitem.com',
'return' => $source_url,
]
);

Expand All @@ -851,7 +847,6 @@ public function test_set_media() {

\WP_Mock::userFunction(
'Distributor\Utils\process_media', [
'times' => 1,
'args' => [ $media_item['source_url'], $post_id,
[
'source_file' => $media_item['source_file'],
Expand All @@ -862,6 +857,13 @@ public function test_set_media() {
]
);

\WP_Mock::userFunction(
'Distributor\Utils\get_attachment_id_by_original_data', [
'args' => [ $media_item['id'], $media_item['source_url'] ],
'return' => $existing_media_id,
]
);

\WP_Mock::userFunction(
'update_post_meta', [
'times' => 1,
Expand Down Expand Up @@ -938,6 +940,21 @@ public function test_set_media() {
Utils\set_media( $post_id, [ $media_item ], [ 'use_filesystem' => false ] );
}

/**
* Data provider for test_set_media
*
* @group Utils
*
* @return array[] Data provider.
*/
public function data_set_media() {
return [
[ 5, 'http://mediaitem.com/mediaitem.jpg', 0 ],
[ 3, 'http://mediaitem.com', 0 ],
[ 3, 'http://mediaitem.com/mediaitem.jpg', 3 ],
];
}

/**
* Todo finish test_set_media
*/
Expand Down

0 comments on commit 9ec968a

Please sign in to comment.