Skip to content

Commit 1840cf3

Browse files
authored
Merge pull request #1211 from alleyinteractive/feature/APPLE10-15/de-duplicate-featured-videos
Deduplicate cover media from content
2 parents 5717353 + 244b5c6 commit 1840cf3

File tree

4 files changed

+104
-9
lines changed

4 files changed

+104
-9
lines changed

admin/settings/class-admin-apple-settings-section-advanced.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,27 @@ public function __construct( $page ) {
3232

3333
// Add the settings.
3434
$this->settings = [
35-
'component_alerts' => [
35+
'component_alerts' => [
3636
'label' => __( 'Component Alerts', 'apple-news' ),
3737
'type' => [ 'none', 'warn', 'fail' ],
3838
'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' ),
3939
],
40-
'use_remote_images' => [
40+
'use_remote_images' => [
4141
'label' => __( 'Use Remote Images?', 'apple-news' ),
4242
'type' => [ 'yes', 'no' ],
4343
'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' ),
4444
],
45-
'full_bleed_images' => [
45+
'full_bleed_images' => [
4646
'label' => __( 'Use Full-Bleed Images?', 'apple-news' ),
4747
'type' => [ 'yes', 'no' ],
4848
'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' ),
4949
],
50-
'html_support' => [
50+
'deduplicate_cover_media' => [
51+
'label' => __( 'Deduplicate Cover Media?', 'apple-news' ),
52+
'type' => [ 'yes', 'no' ],
53+
'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' ),
54+
],
55+
'html_support' => [
5156
'label' => __( 'Enable HTML support?', 'apple-news' ),
5257
'type' => [ 'yes', 'no' ],
5358
'description' => sprintf(
@@ -57,26 +62,26 @@ public function __construct( $page ) {
5762
'</a>'
5863
),
5964
],
60-
'in_article_position' => [
65+
'in_article_position' => [
6166
'label' => __( 'Position of In Article Module', 'apple-news' ),
6267
'type' => 'number',
6368
'min' => 3,
6469
'max' => 99,
6570
'step' => 1,
6671
'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' ),
6772
],
68-
'aside_component_class' => [
73+
'aside_component_class' => [
6974
'label' => __( 'Aside Content CSS Class', 'apple-news' ),
7075
'type' => 'text',
7176
'description' => __( 'Enter a CSS class name that will be used to generate the Aside component. Do not prefix with a period.', 'apple-news' ),
7277
'required' => false,
7378
],
74-
'excluded_selectors' => [
79+
'excluded_selectors' => [
7580
'label' => __( 'Selectors', 'apple-news' ),
7681
'type' => 'text',
77-
'size' => 150,
82+
'size' => 100,
7883
'description' => sprintf(
79-
/* translators: %s: <code> tag */
84+
/* translators: %s: <code> tag */
8085
__( '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' ),
8186
'<code>.my-class, #my-id</code>',
8287
),
@@ -94,6 +99,10 @@ public function __construct( $page ) {
9499
'label' => __( 'Image Settings', 'apple-news' ),
95100
'settings' => [ 'use_remote_images', 'full_bleed_images' ],
96101
],
102+
'cover' => [
103+
'label' => __( 'Cover Media Settings', 'apple-news' ),
104+
'settings' => [ 'deduplicate_cover_media' ],
105+
],
97106
'format' => [
98107
'label' => __( 'Format Settings', 'apple-news' ),
99108
'settings' => [ 'html_support', 'in_article_position' ],

includes/apple-exporter/builders/class-components.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ protected function build() {
7272
// Remove any identifiers that are duplicated.
7373
$components = $this->remove_duplicate_identifiers( $components );
7474

75+
// Remove any components that duplicate the cover media.
76+
$components = $this->remove_cover_from_components( $components );
77+
7578
return $components;
7679
}
7780

@@ -934,4 +937,39 @@ private function split_into_components() {
934937

935938
return $components;
936939
}
940+
941+
/**
942+
* Remove any components that duplicate the cover media.
943+
*
944+
* @param array $components The array of components to remove the cover from.
945+
* @return array The updated array of components.
946+
*/
947+
private function remove_cover_from_components( $components ) {
948+
if ( 'yes' !== $this->get_setting( 'deduplicate_cover_media' ) ) {
949+
return $components;
950+
}
951+
952+
$cover = $this->content_cover();
953+
954+
if ( empty( $cover['url'] ) ) {
955+
return $components;
956+
}
957+
958+
foreach ( $components as $i => $component ) {
959+
// Special case: Don't remove the cover from the header itself.
960+
if ( isset( $component['role'] ) && 'header' === $component['role'] ) {
961+
continue;
962+
}
963+
964+
if ( isset( $component['URL'] ) && $component['URL'] === $cover['url'] ) {
965+
unset( $components[ $i ] );
966+
}
967+
968+
if ( isset( $component['components'] ) && is_array( $component['components'] ) ) {
969+
$components[ $i ]['components'] = $this->remove_cover_from_components( $component['components'] );
970+
}
971+
}
972+
973+
return array_values( $components );
974+
}
937975
}

includes/apple-exporter/class-settings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class Settings {
4444
'apple_news_enable_debugging' => 'no',
4545
'component_alerts' => 'none',
4646
'full_bleed_images' => 'no',
47+
'deduplicate_cover_media' => 'no',
4748
'html_support' => 'yes',
4849
'in_article_position' => 3,
4950
'post_types' => [ 'post' ],

tests/apple-exporter/builders/test-class-components.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,4 +346,51 @@ public function test_meta_component_ordering( $order, $expected, $components ) {
346346
}
347347
}
348348
}
349+
350+
/**
351+
* Tests that when a URL is set as the cover media, it doesn't appear again in the body.
352+
*/
353+
public function test_remove_cover_from_body_components() {
354+
$video_url = 'https://www.example.com/example.mp4';
355+
356+
$post_id = self::factory()->post->create(
357+
[
358+
'post_content' => <<<HTML
359+
<!-- wp:paragraph -->
360+
<p>At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti.</p>
361+
<!-- /wp:paragraph -->
362+
363+
<!-- wp:video -->
364+
<figure class="wp-block-video"><video controls src="{$video_url}"></video></figure>
365+
<!-- /wp:video -->
366+
HTML,
367+
],
368+
);
369+
370+
update_post_meta( $post_id, 'apple_news_cover_media_provider', 'video_url' );
371+
update_post_meta( $post_id, 'apple_news_cover_video_url', $video_url );
372+
373+
$count_of_video_url_in_body = function () use ( $post_id, $video_url ) {
374+
$count = 0;
375+
$json = $this->get_json_for_post( $post_id );
376+
377+
foreach ( $json['components'] as $component ) {
378+
if ( 'container' === $component['role'] ) {
379+
foreach ( $component['components'] as $subcomponent ) {
380+
if ( isset( $subcomponent['URL'] ) && $video_url === $subcomponent['URL'] ) {
381+
$count++;
382+
}
383+
}
384+
}
385+
}
386+
387+
return $count;
388+
};
389+
390+
$this->settings->deduplicate_cover_media = 'no';
391+
$this->assertSame( 1, $count_of_video_url_in_body() );
392+
393+
$this->settings->deduplicate_cover_media = 'yes';
394+
$this->assertSame( 0, $count_of_video_url_in_body() );
395+
}
349396
}

0 commit comments

Comments
 (0)