Skip to content

Commit

Permalink
Add image parsing from post content for the classic editor.
Browse files Browse the repository at this point in the history
  • Loading branch information
iamdharmesh committed Jan 23, 2025
1 parent 6c8f0f4 commit 0fbf1f1
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 22 deletions.
48 changes: 48 additions & 0 deletions includes/classes/DistributorPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,11 @@ protected function get_media() {
$raw_media = $this->parse_media_blocks();
} else {
$raw_media = get_attached_media( get_allowed_mime_types(), $post_id );
// Parse images from post content.
$parsed_media = $this->parse_images_from_post_content();
foreach ( $parsed_media as $media_post ) {
$raw_media[ $media_post->ID ] = $media_post;
}
}

$featured_image_id = $this->get_post_thumbnail_id();
Expand All @@ -652,6 +657,49 @@ protected function get_media() {
return $media_array;
}

/**
* Parse the post's content to obtain media items by image tags.
*
* @return array<WP_Post> Array of media posts.
*/
protected function parse_images_from_post_content() {
$processor = new \WP_HTML_Tag_Processor( $this->post->post_content );

$media = array();
while ( $processor->next_tag( 'img' ) ) {
$classes = explode( ' ', $processor->get_attribute( 'class' ) ?? ' ' );

if ( ! is_array( $classes ) ) {
continue;
}

// Filter out classes that are not image classes.
$classes = array_filter(
$classes,
function( $class ) {
return strpos( $class, 'wp-image-' ) === 0;
}
);

if ( empty( $classes ) ) {
continue;
}

$image_id = (int) str_replace( 'wp-image-', '', current( $classes ) );
$media_post = get_post( $image_id );

if ( empty( $media_post ) ) {
continue;
}

$media[ $media_post->ID ] = $media_post;
}

$media = array_filter( $media );

return $media;
}

/**
* Parse the post's content to obtain media items.
*
Expand Down
46 changes: 24 additions & 22 deletions includes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ function process_media( $url, $post_id, $args = [] ) {
[
'use_filesystem' => false,
'source_file' => '',
'original_media_id' => 0
'original_media_id' => 0,
]
);

Expand Down Expand Up @@ -1064,28 +1064,30 @@ function process_media( $url, $post_id, $args = [] ) {
* @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(
'relation' => 'AND',
array(
'key' => 'dt_original_media_id',
'value' => $original_id,
'compare' => '=',
$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' => '=',
),
),
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];
Expand Down

0 comments on commit 0fbf1f1

Please sign in to comment.