From f26e4ece9380ba376076a08832344493bbc61f14 Mon Sep 17 00:00:00 2001 From: David Herrera Date: Mon, 16 Dec 2024 03:04:10 -0500 Subject: [PATCH 1/6] Refactor cover logic --- admin/apple-actions/index/class-export.php | 222 +++++++++++---------- 1 file changed, 116 insertions(+), 106 deletions(-) diff --git a/admin/apple-actions/index/class-export.php b/admin/apple-actions/index/class-export.php index ab5b8843..31014672 100644 --- a/admin/apple-actions/index/class-export.php +++ b/admin/apple-actions/index/class-export.php @@ -122,116 +122,11 @@ public function fetch_exporter() { $excerpt = has_excerpt( $post ) ? wp_strip_all_tags( $post->post_excerpt ) : ''; // Get the cover configuration. - $post_thumb = null; - $fall_back_to_image = false; - $cover_provider = get_post_meta( $this->id, 'apple_news_cover_media_provider', true ); if ( ! is_string( $cover_provider ) || ! $cover_provider ) { $cover_provider = 'image'; } - - if ( 'embedwebvideo' === $cover_provider ) { - $cover_url = get_post_meta( $this->id, 'apple_news_cover_embedwebvideo_url', true ); - - // Test against accepted providers. - if ( preg_match( Embed_Web_Video::YOUTUBE_MATCH, $cover_url, $cover_matches ) ) { - $post_thumb = [ - 'caption' => '', - 'url' => 'https://www.youtube.com/embed/' . end( $cover_matches ), - ]; - } elseif ( preg_match( Embed_Web_Video::VIMEO_MATCH, $cover_url, $cover_matches ) ) { - $post_thumb = [ - 'caption' => '', - 'url' => 'https://player.vimeo.com/video/' . end( $cover_matches ), - ]; - } elseif ( preg_match( Embed_Web_Video::DAILYMOTION_MATCH, $cover_url, $cover_matches ) ) { - $post_thumb = [ - 'caption' => '', - 'url' => 'https://geo.dailymotion.com/player.html?video=' . end( $cover_matches ), - ]; - } else { - $fall_back_to_image = true; - } - } - - if ( 'video_id' === $cover_provider ) { - $video_id = get_post_meta( $this->id, 'apple_news_cover_video_id', true ); - $video_url = (string) wp_get_attachment_url( $video_id ); - - if ( $video_url ) { - $post_thumb = [ - 'caption' => '', - 'url' => $video_url, - ]; - } else { - $fall_back_to_image = true; - } - } - - if ( 'video_url' === $cover_provider ) { - $file_url = get_post_meta( $this->id, 'apple_news_cover_video_url', true ); - $check = wp_check_filetype( $file_url ); - - if ( isset( $check['ext'] ) && 'mp4' === $check['ext'] ) { - $post_thumb = [ - 'caption' => '', - 'url' => $file_url, - ]; - } else { - $fall_back_to_image = true; - } - } - - // Provide fallback behavior so there's still a cover, e.g. if the URL becomes unsupported later. - if ( $fall_back_to_image ) { - $cover_url = ''; - $cover_provider = 'image'; - } - - if ( 'image' === $cover_provider ) { - $cover_meta_id = get_post_meta( $this->id, 'apple_news_coverimage', true ); - $cover_caption = get_post_meta( $this->id, 'apple_news_coverimage_caption', true ); - if ( ! empty( $cover_meta_id ) ) { - if ( empty( $cover_caption ) ) { - $cover_caption = wp_get_attachment_caption( $cover_meta_id ); - } - $post_thumb = [ - 'caption' => ! empty( $cover_caption ) ? $cover_caption : '', - 'url' => wp_get_attachment_url( $cover_meta_id ), - ]; - } else { - $thumb_id = get_post_thumbnail_id( $this->id ); - $post_thumb_url = wp_get_attachment_url( $thumb_id ); - if ( empty( $cover_caption ) ) { - $cover_caption = wp_get_attachment_caption( $thumb_id ); - } - if ( ! empty( $post_thumb_url ) ) { - // If the post thumb URL is root-relative, convert it to fully-qualified. - if ( str_starts_with( $post_thumb_url, '/' ) ) { - $post_thumb_url = home_url( $post_thumb_url ); - } - - // Compile the post_thumb object using the URL and caption from the featured image. - $post_thumb = [ - 'caption' => ! empty( $cover_caption ) ? $cover_caption : '', - 'url' => $post_thumb_url, - ]; - } - } - } - - // If there is a cover caption but not a cover image URL, preserve it, so it can take precedence later. - if ( empty( $post_thumb ) && ! empty( $cover_caption ) ) { - $post_thumb = [ - 'caption' => $cover_caption, - 'url' => '', - ]; - } - - // Attach the final provider slug. - if ( is_array( $post_thumb ) ) { - $post_thumb['provider'] = $cover_provider; - } + $post_thumb = $this->format_cover( $cover_provider ); // Build the byline. $byline = $this->format_byline( $post ); @@ -839,4 +734,119 @@ private function set_theme(): void { } } } + + /** + * Prepare cover URL, caption, and provider type. + * + * @param string $cover_provider The cover provider to use. + * @return array|null + */ + public function format_cover( string $cover_provider ): ?array { + $post_thumb = null; + $fall_back_to_image = false; + + if ( 'embedwebvideo' === $cover_provider ) { + $cover_url = get_post_meta( $this->id, 'apple_news_cover_embedwebvideo_url', true ); + + // Test against accepted providers. + if ( preg_match( Embed_Web_Video::YOUTUBE_MATCH, $cover_url, $cover_matches ) ) { + $post_thumb = [ + 'caption' => '', + 'url' => 'https://www.youtube.com/embed/' . end( $cover_matches ), + ]; + } elseif ( preg_match( Embed_Web_Video::VIMEO_MATCH, $cover_url, $cover_matches ) ) { + $post_thumb = [ + 'caption' => '', + 'url' => 'https://player.vimeo.com/video/' . end( $cover_matches ), + ]; + } elseif ( preg_match( Embed_Web_Video::DAILYMOTION_MATCH, $cover_url, $cover_matches ) ) { + $post_thumb = [ + 'caption' => '', + 'url' => 'https://geo.dailymotion.com/player.html?video=' . end( $cover_matches ), + ]; + } else { + $fall_back_to_image = true; + } + } + + if ( 'video_id' === $cover_provider ) { + $video_id = get_post_meta( $this->id, 'apple_news_cover_video_id', true ); + $video_url = (string) wp_get_attachment_url( $video_id ); + + if ( $video_url ) { + $post_thumb = [ + 'caption' => '', + 'url' => $video_url, + ]; + } else { + $fall_back_to_image = true; + } + } + + if ( 'video_url' === $cover_provider ) { + $file_url = get_post_meta( $this->id, 'apple_news_cover_video_url', true ); + $check = wp_check_filetype( $file_url ); + + if ( isset( $check['ext'] ) && 'mp4' === $check['ext'] ) { + $post_thumb = [ + 'caption' => '', + 'url' => $file_url, + ]; + } else { + $fall_back_to_image = true; + } + } + + // Provide fallback behavior so there's still a cover, e.g. if the URL becomes unsupported later. + if ( $fall_back_to_image ) { + $cover_provider = 'image'; + } + + if ( 'image' === $cover_provider ) { + $cover_meta_id = get_post_meta( $this->id, 'apple_news_coverimage', true ); + $cover_caption = get_post_meta( $this->id, 'apple_news_coverimage_caption', true ); + if ( ! empty( $cover_meta_id ) ) { + if ( empty( $cover_caption ) ) { + $cover_caption = wp_get_attachment_caption( $cover_meta_id ); + } + $post_thumb = [ + 'caption' => ! empty( $cover_caption ) ? $cover_caption : '', + 'url' => wp_get_attachment_url( $cover_meta_id ), + ]; + } else { + $thumb_id = get_post_thumbnail_id( $this->id ); + $post_thumb_url = wp_get_attachment_url( $thumb_id ); + if ( empty( $cover_caption ) ) { + $cover_caption = wp_get_attachment_caption( $thumb_id ); + } + if ( ! empty( $post_thumb_url ) ) { + // If the post thumb URL is root-relative, convert it to fully-qualified. + if ( str_starts_with( $post_thumb_url, '/' ) ) { + $post_thumb_url = home_url( $post_thumb_url ); + } + + // Compile the post_thumb object using the URL and caption from the featured image. + $post_thumb = [ + 'caption' => ! empty( $cover_caption ) ? $cover_caption : '', + 'url' => $post_thumb_url, + ]; + } + } + } + + // If there is a cover caption but not a cover image URL, preserve it, so it can take precedence later. + if ( empty( $post_thumb ) && ! empty( $cover_caption ) ) { + $post_thumb = [ + 'caption' => $cover_caption, + 'url' => '', + ]; + } + + // Attach the final provider slug. + if ( is_array( $post_thumb ) ) { + $post_thumb['provider'] = $cover_provider; + } + + return $post_thumb; + } } From fe50dfb3f2dcec4db49302cc12bb051be19c4118 Mon Sep 17 00:00:00 2001 From: David Herrera Date: Mon, 16 Dec 2024 03:35:24 -0500 Subject: [PATCH 2/6] Deduplicate cover media from content --- ...-admin-apple-settings-section-advanced.php | 9 ++++ .../builders/class-components.php | 38 +++++++++++++++ includes/apple-exporter/class-settings.php | 1 + .../builders/test-class-components.php | 47 +++++++++++++++++++ 4 files changed, 95 insertions(+) diff --git a/admin/settings/class-admin-apple-settings-section-advanced.php b/admin/settings/class-admin-apple-settings-section-advanced.php index a32e91a7..165144c1 100644 --- a/admin/settings/class-admin-apple-settings-section-advanced.php +++ b/admin/settings/class-admin-apple-settings-section-advanced.php @@ -47,6 +47,11 @@ public function __construct( $page ) { 'type' => [ 'yes', 'no' ], 'description' => __( 'If set to yes, images that are centered or have no alignment will span edge-to-edge rather than being constrained within the body margins.', 'apple-news' ), ], + 'deduplicate_cover_media' => [ + 'label' => __( 'Deduplicate Cover Media?', 'apple-news' ), + 'type' => [ 'yes', 'no' ], + 'description' => __( 'If set to yes, any image, video, or other content selected as an article\'s Cover Media will not appear again in the article body in Apple News.', 'apple-news' ), + ], 'html_support' => [ 'label' => __( 'Enable HTML support?', 'apple-news' ), 'type' => [ 'yes', 'no' ], @@ -94,6 +99,10 @@ public function __construct( $page ) { 'label' => __( 'Image Settings', 'apple-news' ), 'settings' => [ 'use_remote_images', 'full_bleed_images' ], ], + 'cover' => [ + 'label' => __( 'Cover Media Settings', 'apple-news' ), + 'settings' => [ 'deduplicate_cover_media' ], + ], 'format' => [ 'label' => __( 'Format Settings', 'apple-news' ), 'settings' => [ 'html_support', 'in_article_position' ], diff --git a/includes/apple-exporter/builders/class-components.php b/includes/apple-exporter/builders/class-components.php index 4cc4b085..d42b5d25 100644 --- a/includes/apple-exporter/builders/class-components.php +++ b/includes/apple-exporter/builders/class-components.php @@ -72,6 +72,9 @@ protected function build() { // Remove any identifiers that are duplicated. $components = $this->remove_duplicate_identifiers( $components ); + // Remove any components that duplicate the cover media. + $components = $this->remove_cover_from_components( $components ); + return $components; } @@ -934,4 +937,39 @@ private function split_into_components() { return $components; } + + /** + * Remove any components that duplicate the cover media. + * + * @param array $components The array of components to remove the cover from. + * @return array The updated array of components. + */ + private function remove_cover_from_components( $components ) { + if ( 'yes' !== $this->get_setting( 'deduplicate_cover_media' ) ) { + return $components; + } + + $cover = $this->content_cover(); + + if ( empty( $cover['url'] ) ) { + return $components; + } + + foreach ( $components as $i => $component ) { + // Special case: Don't remove the cover from the header itself. + if ( isset( $component['role'] ) && 'header' === $component['role'] ) { + continue; + } + + if ( isset( $component['URL'] ) && $component['URL'] === $cover['url'] ) { + unset( $components[ $i ] ); + } + + if ( isset( $component['components'] ) && is_array( $component['components'] ) ) { + $components[ $i ]['components'] = $this->remove_cover_from_components( $component['components'] ); + } + } + + return array_values( $components ); + } } diff --git a/includes/apple-exporter/class-settings.php b/includes/apple-exporter/class-settings.php index dc1d8c7e..20eb163c 100644 --- a/includes/apple-exporter/class-settings.php +++ b/includes/apple-exporter/class-settings.php @@ -44,6 +44,7 @@ class Settings { 'apple_news_enable_debugging' => 'no', 'component_alerts' => 'none', 'full_bleed_images' => 'no', + 'deduplicate_cover_media' => 'no', 'html_support' => 'yes', 'in_article_position' => 3, 'post_types' => [ 'post' ], diff --git a/tests/apple-exporter/builders/test-class-components.php b/tests/apple-exporter/builders/test-class-components.php index e63f11bf..c1761c26 100644 --- a/tests/apple-exporter/builders/test-class-components.php +++ b/tests/apple-exporter/builders/test-class-components.php @@ -346,4 +346,51 @@ public function test_meta_component_ordering( $order, $expected, $components ) { } } } + + /** + * Tests that when a URL is set as the cover media, it doesn't appear again in the body. + */ + public function test_remove_cover_from_body_components() { + $video_url = 'https://www.example.com/example.mp4'; + + $post_id = self::factory()->post->create( + [ + 'post_content' => << +

At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti.

+ + + +
+ +HTML, + ], + ); + + update_post_meta( $post_id, 'apple_news_cover_media_provider', 'video_url' ); + update_post_meta( $post_id, 'apple_news_cover_video_url', $video_url ); + + $count_of_video_url_in_body = function () use ( $post_id, $video_url ) { + $count = 0; + $json = $this->get_json_for_post( $post_id ); + + foreach ( $json['components'] as $component ) { + if ( 'container' === $component['role'] ) { + foreach ( $component['components'] as $subcomponent ) { + if ( isset( $subcomponent['URL'] ) && $video_url === $subcomponent['URL'] ) { + $count++; + } + } + } + } + + return $count; + }; + + $this->settings->deduplicate_cover_media = 'no'; + $this->assertSame( 1, $count_of_video_url_in_body() ); + + $this->settings->deduplicate_cover_media = 'yes'; + $this->assertSame( 0, $count_of_video_url_in_body() ); + } } From c81a3e8dc44aca1f89831c343d2933e5e8ca46a3 Mon Sep 17 00:00:00 2001 From: David Herrera Date: Tue, 17 Dec 2024 05:29:20 -0500 Subject: [PATCH 3/6] Fix text field overflow --- admin/settings/class-admin-apple-settings-section-advanced.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin/settings/class-admin-apple-settings-section-advanced.php b/admin/settings/class-admin-apple-settings-section-advanced.php index 165144c1..387b22b3 100644 --- a/admin/settings/class-admin-apple-settings-section-advanced.php +++ b/admin/settings/class-admin-apple-settings-section-advanced.php @@ -79,7 +79,7 @@ public function __construct( $page ) { 'excluded_selectors' => [ 'label' => __( 'Selectors', 'apple-news' ), 'type' => 'text', - 'size' => 150, + 'size' => 100, 'description' => sprintf( /* translators: %s: tag */ __( 'Enter a comma-separated list of CSS class or ID selectors, like %s. Elements in post content matching these selectors will be removed from the content published to Apple News.', 'apple-news' ), From 57b1f35b307d4f75a6570d11bbcf99fa7b3e407c Mon Sep 17 00:00:00 2001 From: David Herrera Date: Tue, 17 Dec 2024 05:41:26 -0500 Subject: [PATCH 4/6] Spacin' --- ...-admin-apple-settings-section-advanced.php | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/admin/settings/class-admin-apple-settings-section-advanced.php b/admin/settings/class-admin-apple-settings-section-advanced.php index 387b22b3..8fa26886 100644 --- a/admin/settings/class-admin-apple-settings-section-advanced.php +++ b/admin/settings/class-admin-apple-settings-section-advanced.php @@ -32,17 +32,18 @@ public function __construct( $page ) { // Add the settings. $this->settings = [ - 'component_alerts' => [ + 'component_alerts' => [ 'label' => __( 'Component Alerts', 'apple-news' ), 'type' => [ 'none', 'warn', 'fail' ], - 'description' => __( 'If a post has a component that is unsupported by Apple News, choose "none" to generate no alert, "warn" to provide an admin warning notice, or "fail" to generate a notice and stop publishing.', 'apple-news' ), + 'description' => __( 'If a post has a component that is unsupported by Apple News, choose "none" to generate no alert, "warn" to provide an admin warning notice, or "fail" to generate a notice and stop publishing.', + 'apple-news' ), ], - 'use_remote_images' => [ + 'use_remote_images' => [ 'label' => __( 'Use Remote Images?', 'apple-news' ), 'type' => [ 'yes', 'no' ], 'description' => __( 'Allow the Apple News API to retrieve images remotely rather than bundle them. This setting is recommended if you are having any issues with publishing images. If your images are not publicly accessible, such as on a development site, you cannot use this feature.', 'apple-news' ), ], - 'full_bleed_images' => [ + 'full_bleed_images' => [ 'label' => __( 'Use Full-Bleed Images?', 'apple-news' ), 'type' => [ 'yes', 'no' ], 'description' => __( 'If set to yes, images that are centered or have no alignment will span edge-to-edge rather than being constrained within the body margins.', 'apple-news' ), @@ -52,7 +53,7 @@ public function __construct( $page ) { 'type' => [ 'yes', 'no' ], 'description' => __( 'If set to yes, any image, video, or other content selected as an article\'s Cover Media will not appear again in the article body in Apple News.', 'apple-news' ), ], - 'html_support' => [ + 'html_support' => [ 'label' => __( 'Enable HTML support?', 'apple-news' ), 'type' => [ 'yes', 'no' ], 'description' => sprintf( @@ -62,7 +63,7 @@ public function __construct( $page ) { '' ), ], - 'in_article_position' => [ + 'in_article_position' => [ 'label' => __( 'Position of In Article Module', 'apple-news' ), 'type' => 'number', 'min' => 3, @@ -70,18 +71,18 @@ public function __construct( $page ) { 'step' => 1, 'description' => __( 'If you have configured an In Article module via Customize JSON, the position that the module should be inserted into. Defaults to 3, which is after the third content block in the article body (e.g., the third paragraph).', 'apple-news' ), ], - 'aside_component_class' => [ + 'aside_component_class' => [ 'label' => __( 'Aside Content CSS Class', 'apple-news' ), 'type' => 'text', 'description' => __( 'Enter a CSS class name that will be used to generate the Aside component. Do not prefix with a period.', 'apple-news' ), 'required' => false, ], - 'excluded_selectors' => [ + 'excluded_selectors' => [ 'label' => __( 'Selectors', 'apple-news' ), 'type' => 'text', 'size' => 100, 'description' => sprintf( - /* translators: %s: tag */ + /* translators: %s: tag */ __( 'Enter a comma-separated list of CSS class or ID selectors, like %s. Elements in post content matching these selectors will be removed from the content published to Apple News.', 'apple-news' ), '.my-class, #my-id', ), From 527b53ce2ef3e94e5459a32656fefec4bf33c0c5 Mon Sep 17 00:00:00 2001 From: David Herrera Date: Tue, 17 Dec 2024 05:42:20 -0500 Subject: [PATCH 5/6] Revert "Refactor cover logic" This reverts commit f26e4ece9380ba376076a08832344493bbc61f14. --- admin/apple-actions/index/class-export.php | 222 ++++++++++----------- 1 file changed, 106 insertions(+), 116 deletions(-) diff --git a/admin/apple-actions/index/class-export.php b/admin/apple-actions/index/class-export.php index 31014672..ab5b8843 100644 --- a/admin/apple-actions/index/class-export.php +++ b/admin/apple-actions/index/class-export.php @@ -122,11 +122,116 @@ public function fetch_exporter() { $excerpt = has_excerpt( $post ) ? wp_strip_all_tags( $post->post_excerpt ) : ''; // Get the cover configuration. + $post_thumb = null; + $fall_back_to_image = false; + $cover_provider = get_post_meta( $this->id, 'apple_news_cover_media_provider', true ); if ( ! is_string( $cover_provider ) || ! $cover_provider ) { $cover_provider = 'image'; } - $post_thumb = $this->format_cover( $cover_provider ); + + if ( 'embedwebvideo' === $cover_provider ) { + $cover_url = get_post_meta( $this->id, 'apple_news_cover_embedwebvideo_url', true ); + + // Test against accepted providers. + if ( preg_match( Embed_Web_Video::YOUTUBE_MATCH, $cover_url, $cover_matches ) ) { + $post_thumb = [ + 'caption' => '', + 'url' => 'https://www.youtube.com/embed/' . end( $cover_matches ), + ]; + } elseif ( preg_match( Embed_Web_Video::VIMEO_MATCH, $cover_url, $cover_matches ) ) { + $post_thumb = [ + 'caption' => '', + 'url' => 'https://player.vimeo.com/video/' . end( $cover_matches ), + ]; + } elseif ( preg_match( Embed_Web_Video::DAILYMOTION_MATCH, $cover_url, $cover_matches ) ) { + $post_thumb = [ + 'caption' => '', + 'url' => 'https://geo.dailymotion.com/player.html?video=' . end( $cover_matches ), + ]; + } else { + $fall_back_to_image = true; + } + } + + if ( 'video_id' === $cover_provider ) { + $video_id = get_post_meta( $this->id, 'apple_news_cover_video_id', true ); + $video_url = (string) wp_get_attachment_url( $video_id ); + + if ( $video_url ) { + $post_thumb = [ + 'caption' => '', + 'url' => $video_url, + ]; + } else { + $fall_back_to_image = true; + } + } + + if ( 'video_url' === $cover_provider ) { + $file_url = get_post_meta( $this->id, 'apple_news_cover_video_url', true ); + $check = wp_check_filetype( $file_url ); + + if ( isset( $check['ext'] ) && 'mp4' === $check['ext'] ) { + $post_thumb = [ + 'caption' => '', + 'url' => $file_url, + ]; + } else { + $fall_back_to_image = true; + } + } + + // Provide fallback behavior so there's still a cover, e.g. if the URL becomes unsupported later. + if ( $fall_back_to_image ) { + $cover_url = ''; + $cover_provider = 'image'; + } + + if ( 'image' === $cover_provider ) { + $cover_meta_id = get_post_meta( $this->id, 'apple_news_coverimage', true ); + $cover_caption = get_post_meta( $this->id, 'apple_news_coverimage_caption', true ); + if ( ! empty( $cover_meta_id ) ) { + if ( empty( $cover_caption ) ) { + $cover_caption = wp_get_attachment_caption( $cover_meta_id ); + } + $post_thumb = [ + 'caption' => ! empty( $cover_caption ) ? $cover_caption : '', + 'url' => wp_get_attachment_url( $cover_meta_id ), + ]; + } else { + $thumb_id = get_post_thumbnail_id( $this->id ); + $post_thumb_url = wp_get_attachment_url( $thumb_id ); + if ( empty( $cover_caption ) ) { + $cover_caption = wp_get_attachment_caption( $thumb_id ); + } + if ( ! empty( $post_thumb_url ) ) { + // If the post thumb URL is root-relative, convert it to fully-qualified. + if ( str_starts_with( $post_thumb_url, '/' ) ) { + $post_thumb_url = home_url( $post_thumb_url ); + } + + // Compile the post_thumb object using the URL and caption from the featured image. + $post_thumb = [ + 'caption' => ! empty( $cover_caption ) ? $cover_caption : '', + 'url' => $post_thumb_url, + ]; + } + } + } + + // If there is a cover caption but not a cover image URL, preserve it, so it can take precedence later. + if ( empty( $post_thumb ) && ! empty( $cover_caption ) ) { + $post_thumb = [ + 'caption' => $cover_caption, + 'url' => '', + ]; + } + + // Attach the final provider slug. + if ( is_array( $post_thumb ) ) { + $post_thumb['provider'] = $cover_provider; + } // Build the byline. $byline = $this->format_byline( $post ); @@ -734,119 +839,4 @@ private function set_theme(): void { } } } - - /** - * Prepare cover URL, caption, and provider type. - * - * @param string $cover_provider The cover provider to use. - * @return array|null - */ - public function format_cover( string $cover_provider ): ?array { - $post_thumb = null; - $fall_back_to_image = false; - - if ( 'embedwebvideo' === $cover_provider ) { - $cover_url = get_post_meta( $this->id, 'apple_news_cover_embedwebvideo_url', true ); - - // Test against accepted providers. - if ( preg_match( Embed_Web_Video::YOUTUBE_MATCH, $cover_url, $cover_matches ) ) { - $post_thumb = [ - 'caption' => '', - 'url' => 'https://www.youtube.com/embed/' . end( $cover_matches ), - ]; - } elseif ( preg_match( Embed_Web_Video::VIMEO_MATCH, $cover_url, $cover_matches ) ) { - $post_thumb = [ - 'caption' => '', - 'url' => 'https://player.vimeo.com/video/' . end( $cover_matches ), - ]; - } elseif ( preg_match( Embed_Web_Video::DAILYMOTION_MATCH, $cover_url, $cover_matches ) ) { - $post_thumb = [ - 'caption' => '', - 'url' => 'https://geo.dailymotion.com/player.html?video=' . end( $cover_matches ), - ]; - } else { - $fall_back_to_image = true; - } - } - - if ( 'video_id' === $cover_provider ) { - $video_id = get_post_meta( $this->id, 'apple_news_cover_video_id', true ); - $video_url = (string) wp_get_attachment_url( $video_id ); - - if ( $video_url ) { - $post_thumb = [ - 'caption' => '', - 'url' => $video_url, - ]; - } else { - $fall_back_to_image = true; - } - } - - if ( 'video_url' === $cover_provider ) { - $file_url = get_post_meta( $this->id, 'apple_news_cover_video_url', true ); - $check = wp_check_filetype( $file_url ); - - if ( isset( $check['ext'] ) && 'mp4' === $check['ext'] ) { - $post_thumb = [ - 'caption' => '', - 'url' => $file_url, - ]; - } else { - $fall_back_to_image = true; - } - } - - // Provide fallback behavior so there's still a cover, e.g. if the URL becomes unsupported later. - if ( $fall_back_to_image ) { - $cover_provider = 'image'; - } - - if ( 'image' === $cover_provider ) { - $cover_meta_id = get_post_meta( $this->id, 'apple_news_coverimage', true ); - $cover_caption = get_post_meta( $this->id, 'apple_news_coverimage_caption', true ); - if ( ! empty( $cover_meta_id ) ) { - if ( empty( $cover_caption ) ) { - $cover_caption = wp_get_attachment_caption( $cover_meta_id ); - } - $post_thumb = [ - 'caption' => ! empty( $cover_caption ) ? $cover_caption : '', - 'url' => wp_get_attachment_url( $cover_meta_id ), - ]; - } else { - $thumb_id = get_post_thumbnail_id( $this->id ); - $post_thumb_url = wp_get_attachment_url( $thumb_id ); - if ( empty( $cover_caption ) ) { - $cover_caption = wp_get_attachment_caption( $thumb_id ); - } - if ( ! empty( $post_thumb_url ) ) { - // If the post thumb URL is root-relative, convert it to fully-qualified. - if ( str_starts_with( $post_thumb_url, '/' ) ) { - $post_thumb_url = home_url( $post_thumb_url ); - } - - // Compile the post_thumb object using the URL and caption from the featured image. - $post_thumb = [ - 'caption' => ! empty( $cover_caption ) ? $cover_caption : '', - 'url' => $post_thumb_url, - ]; - } - } - } - - // If there is a cover caption but not a cover image URL, preserve it, so it can take precedence later. - if ( empty( $post_thumb ) && ! empty( $cover_caption ) ) { - $post_thumb = [ - 'caption' => $cover_caption, - 'url' => '', - ]; - } - - // Attach the final provider slug. - if ( is_array( $post_thumb ) ) { - $post_thumb['provider'] = $cover_provider; - } - - return $post_thumb; - } } From 244b5c643c6325ee4f760ea7e9243222062313ee Mon Sep 17 00:00:00 2001 From: David Herrera Date: Tue, 17 Dec 2024 05:45:20 -0500 Subject: [PATCH 6/6] Spacin' --- admin/settings/class-admin-apple-settings-section-advanced.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/admin/settings/class-admin-apple-settings-section-advanced.php b/admin/settings/class-admin-apple-settings-section-advanced.php index 8fa26886..1d465725 100644 --- a/admin/settings/class-admin-apple-settings-section-advanced.php +++ b/admin/settings/class-admin-apple-settings-section-advanced.php @@ -35,8 +35,7 @@ public function __construct( $page ) { 'component_alerts' => [ 'label' => __( 'Component Alerts', 'apple-news' ), 'type' => [ 'none', 'warn', 'fail' ], - 'description' => __( 'If a post has a component that is unsupported by Apple News, choose "none" to generate no alert, "warn" to provide an admin warning notice, or "fail" to generate a notice and stop publishing.', - 'apple-news' ), + 'description' => __( 'If a post has a component that is unsupported by Apple News, choose "none" to generate no alert, "warn" to provide an admin warning notice, or "fail" to generate a notice and stop publishing.', 'apple-news' ), ], 'use_remote_images' => [ 'label' => __( 'Use Remote Images?', 'apple-news' ),