Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deduplicate cover media from content #1211

Merged
merged 6 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions admin/settings/class-admin-apple-settings-section-advanced.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,27 @@ 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' ),
],
'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' ),
],
'html_support' => [
'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' ],
'description' => sprintf(
Expand All @@ -57,26 +62,26 @@ public function __construct( $page ) {
'</a>'
),
],
'in_article_position' => [
'in_article_position' => [
'label' => __( 'Position of In Article Module', 'apple-news' ),
'type' => 'number',
'min' => 3,
'max' => 99,
'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' => 150,
'size' => 100,
'description' => sprintf(
/* translators: %s: <code> tag */
/* translators: %s: <code> 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' ),
'<code>.my-class, #my-id</code>',
),
Expand All @@ -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' ],
Expand Down
38 changes: 38 additions & 0 deletions includes/apple-exporter/builders/class-components.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 );
}
}
1 change: 1 addition & 0 deletions includes/apple-exporter/class-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ],
Expand Down
47 changes: 47 additions & 0 deletions tests/apple-exporter/builders/test-class-components.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => <<<HTML
<!-- wp:paragraph -->
<p>At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti.</p>
<!-- /wp:paragraph -->

<!-- wp:video -->
<figure class="wp-block-video"><video controls src="{$video_url}"></video></figure>
<!-- /wp:video -->
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() );
}
}
Loading