From efb9537c247dfdc802e845c0ae63b1c06907c5cf Mon Sep 17 00:00:00 2001 From: avag Date: Tue, 11 Jun 2019 17:00:22 +0400 Subject: [PATCH 1/4] Feature - localize distributed attachments in post content --- distributor.php | 3 +- includes/post-specific-content-handler.php | 179 +++++++++++++++++++++ includes/utils.php | 5 + 3 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 includes/post-specific-content-handler.php diff --git a/distributor.php b/distributor.php index 77dc18194..50b73877d 100644 --- a/distributor.php +++ b/distributor.php @@ -106,6 +106,7 @@ function( $response ) { require_once __DIR__ . '/includes/distributed-post-ui.php'; require_once __DIR__ . '/includes/settings.php'; require_once __DIR__ . '/includes/template-tags.php'; +require_once __DIR__ . '/includes/post-specific-content-handler.php'; if ( \Distributor\Utils\is_vip_com() ) { add_filter( 'dt_network_site_connection_enabled', '__return_false', 9 ); @@ -178,4 +179,4 @@ function() { \Distributor\SyndicatedPostUI\setup(); \Distributor\DistributedPostUI\setup(); \Distributor\Settings\setup(); - +\Distributor\PostSpecificContentHandler\setup(); diff --git a/includes/post-specific-content-handler.php b/includes/post-specific-content-handler.php new file mode 100644 index 000000000..1dce33e18 --- /dev/null +++ b/includes/post-specific-content-handler.php @@ -0,0 +1,179 @@ +ID ); + $content = $new_post->post_content; + + // Replace with the website local images + $content = preg_replace_callback( + '|]+>|', + __NAMESPACE__ . '\localize_images', + $content + ); + + wp_update_post( + array( + 'ID' => $post->ID, + 'post_content' => $content, + ) + ); +} + +/** + * Reference distributed post images to local attachments + * + * @param array $matches Matches array + * + * @return string + */ +function localize_images( $matches ) { + // Convert string to array consisting attributes' names as keys and their values as array of values + $exploded = preg_split( '/(?<= attributes before modifications + * + * @param array $attrs Attributes array + */ + $attrs = apply_filters( 'dt_post_attachment_attributes_before_modify', $attrs ); + + // Get attachment size and original id + $orig_img_id = 0; + $img_size = ''; + if ( ! empty( $attrs['class'] ) ) { + foreach ( $attrs['class'] as $c ) { + preg_match( '/wp-image-(\d+)/', $c, $m ); + if ( ! empty( $m[1] ) ) { + $orig_img_id = $m[1]; + } + + preg_match( '/size-(\w+)/', $c, $m2 ); + if ( ! empty( $m2 ) ) { + $img_size = $m2[1]; + } + } + } + + if ( 0 === $orig_img_id ) { + return $matches[0]; + } + + // Get the mapped image id + $mapped_img_id = get_distributed_image_id( $orig_img_id ); + + if ( null === $mapped_img_id ) { + return $matches[0]; + } + + if ( '' === $img_size ) { + $img_src = wp_get_attachment_url( $mapped_img_id ); + } else { + $img_src = wp_get_attachment_image_url( $mapped_img_id, $img_size ); + } + + // Replace attachment url + if ( ! empty( $attrs['src'] ) ) { + $attrs['src'][0] = $img_src; + } + + // Replace image id with the appropriate one in the class + foreach ( $attrs['class'] as &$v ) { + if ( strpos( $v, 'wp-image-' ) !== false ) { + $v = 'wp-image-' . $mapped_img_id; + } + } + unset( $v ); // break the reference with the last element + + /** + * Filters the attributes after implemented modifications + * + * @param array $attrs Attributes array + */ + $attrs = apply_filters( 'dt_post_attachment_attributes_after_modify', $attrs ); + + // Re-assemble the tag + $img_tag = ''; + foreach ( $attrs as $attr_name => $attr_values ) { + $img_tag .= $attr_name; + if ( ! empty( $attr_values ) ) { + $img_tag .= '="'; + foreach ( $attr_values as $attr_value ) { + $img_tag .= trim( $attr_value, '"' ) . ' '; + } + $img_tag = rtrim( $img_tag ); + $img_tag .= '"'; + } + + $img_tag .= ' '; + } + + return rtrim( $img_tag ); +} + +/** + * Get distributed attachment id by original attachment id + * + * @param int $id Image remote original id. + * + * @return null|int + */ +function get_distributed_image_id( $id ) { + global $wpdb; + $result = $wpdb->get_col( $wpdb->prepare( "SELECT post_id from $wpdb->postmeta WHERE meta_key = 'dt_original_media_id' AND meta_value = %d", $id ) ); + + if ( 1 === count( $result ) ) { + return $result[0]; + } + + if ( count( $result ) > 1 ) { + // We have a trashed post, dodge it + return $wpdb->get_var( + $wpdb->prepare( + " SELECT post_id + FROM $wpdb->postmeta + INNER JOIN `$wpdb->posts` + ON `$wpdb->posts`.`ID` = `$wpdb->postmeta`.`post_id` + AND `$wpdb->posts`.`post_status` != 'trash' + WHERE meta_key = 'dt_original_media_id' + AND meta_value = %d + LIMIT 1", + $id + ) + ); + } + + return null; +} diff --git a/includes/utils.php b/includes/utils.php index e656617a4..33d284f25 100644 --- a/includes/utils.php +++ b/includes/utils.php @@ -781,8 +781,13 @@ function get_processed_content( $post_content ) { * Remove autoembed filter so that actual URL will be pushed and not the generated markup. */ remove_filter( 'the_content', [ $wp_embed, 'autoembed' ], 8 ); + /** + * Remove the 'wp_make_content_images_responsive' filter, so the generated 'srcset' won't be pushed + */ + remove_filter( 'the_content', 'wp_make_content_images_responsive' ); $post_content = apply_filters( 'the_content', $post_content ); add_filter( 'the_content', [ $wp_embed, 'autoembed' ], 8 ); + add_filter( 'the_content', 'wp_make_content_images_responsive' ); return $post_content; } From 2f1c41c92c346aea0f7b6a1801a00035104449ca Mon Sep 17 00:00:00 2001 From: avag Date: Wed, 12 Jun 2019 14:35:04 +0400 Subject: [PATCH 2/4] Feature - handle shoercodes distribution in post content --- includes/post-specific-content-handler.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/includes/post-specific-content-handler.php b/includes/post-specific-content-handler.php index 1dce33e18..8cece8c4f 100644 --- a/includes/post-specific-content-handler.php +++ b/includes/post-specific-content-handler.php @@ -38,6 +38,28 @@ function save_post_specific_content( $post, $request ) { $content ); + // Handle shortcode distribution + // We need just to get all shortcodes in post content regardless their type or hierarchy + $pattern = "/\[(\[?)([^\]]+)(\]?)\]/"; + + $content = preg_replace_callback( + $pattern, + function ( $matches ) { + $shortcode = $matches[0]; + + /** + * Filters the the shortcode tags in the post content + * + * @param string $shortcode Whole shortcode tag including wrapped content, if there is any + * @param array $matches the array of matches + */ + $shortcode = apply_filters( 'dt_post_content_shortcode_tags', $shortcode, $matches ); + + return $shortcode; + }, + $content + ); + wp_update_post( array( 'ID' => $post->ID, From a770a74dde5c099e72af858f60574ca6601fc467 Mon Sep 17 00:00:00 2001 From: avag Date: Thu, 13 Jun 2019 15:51:13 +0400 Subject: [PATCH 3/4] Fix linter errors, unit test errors, failures --- includes/post-specific-content-handler.php | 2 +- tests/php/SubscriptionsTest.php | 4 ++-- tests/php/UtilsTest.php | 10 ++++++++-- tests/php/WordPressExternalConnectionTest.php | 2 +- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/includes/post-specific-content-handler.php b/includes/post-specific-content-handler.php index 8cece8c4f..848b270f6 100644 --- a/includes/post-specific-content-handler.php +++ b/includes/post-specific-content-handler.php @@ -40,7 +40,7 @@ function save_post_specific_content( $post, $request ) { // Handle shortcode distribution // We need just to get all shortcodes in post content regardless their type or hierarchy - $pattern = "/\[(\[?)([^\]]+)(\]?)\]/"; + $pattern = '/\[(\[?)([^\]]+)(\]?)\]/'; $content = preg_replace_callback( $pattern, diff --git a/tests/php/SubscriptionsTest.php b/tests/php/SubscriptionsTest.php index bb7b5f14f..b4768f0e3 100644 --- a/tests/php/SubscriptionsTest.php +++ b/tests/php/SubscriptionsTest.php @@ -360,7 +360,7 @@ public function test_send_notifications_no_remote_post() { \WP_Mock::userFunction( 'remove_filter', [ - 'times' => 1, + 'times' => 2, ] ); @@ -523,7 +523,7 @@ public function test_send_notifications_remote_post_exists() { \WP_Mock::userFunction( 'remove_filter', [ - 'times' => 1, + 'times' => 2, ] ); diff --git a/tests/php/UtilsTest.php b/tests/php/UtilsTest.php index 3703df5f6..63e7b6498 100644 --- a/tests/php/UtilsTest.php +++ b/tests/php/UtilsTest.php @@ -447,7 +447,7 @@ public function test_format_media_not_featured() { \WP_Mock::userFunction( 'remove_filter', [ - 'times' => 1, + 'times' => 2, ] ); @@ -527,7 +527,7 @@ public function test_format_media_featured() { \WP_Mock::userFunction( 'remove_filter', [ - 'times' => 1, + 'times' => 2, ] ); @@ -668,6 +668,12 @@ public function test_set_media() { ] ); + \WP_Mock::userFunction( + 'remove_filter', [ + 'times' => 2, + ] + ); + Utils\set_media( $post_id, [ $media_item ] ); } diff --git a/tests/php/WordPressExternalConnectionTest.php b/tests/php/WordPressExternalConnectionTest.php index eafccb765..f8c80c6e8 100644 --- a/tests/php/WordPressExternalConnectionTest.php +++ b/tests/php/WordPressExternalConnectionTest.php @@ -141,7 +141,7 @@ public function test_push() { \WP_Mock::userFunction( 'remove_filter', [ - 'times' => 2, + 'times' => 4, ] ); From e490a036edb5bf2303d0e8605901623cfa591bbf Mon Sep 17 00:00:00 2001 From: avag Date: Thu, 13 Jun 2019 15:57:01 +0400 Subject: [PATCH 4/4] Fix unit test errors --- tests/php/UtilsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/php/UtilsTest.php b/tests/php/UtilsTest.php index 63e7b6498..e310f3fdd 100644 --- a/tests/php/UtilsTest.php +++ b/tests/php/UtilsTest.php @@ -670,7 +670,7 @@ public function test_set_media() { \WP_Mock::userFunction( 'remove_filter', [ - 'times' => 2, + 'times' => 0, ] );