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

Allow videos in cover component #1202

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
assets/js/bulk-export.js
assets/js/cover-image.js
assets/js/cover-media.js
assets/js/export-table.js
assets/js/json.js
assets/js/meta-boxes.js
Expand Down
156 changes: 127 additions & 29 deletions admin/apple-actions/index/class-export.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require_once dirname( __DIR__, 3 ) . '/includes/apple-exporter/autoload.php';

use Apple_Actions\Action;
use Apple_Exporter\Components\Embed_Web_Video;
use Apple_Exporter\Exporter;
use Apple_Exporter\Exporter_Content;
use Apple_Exporter\Exporter_Content_Settings;
Expand Down Expand Up @@ -121,34 +122,101 @@ public function fetch_exporter() {
$excerpt = has_excerpt( $post ) ? wp_strip_all_tags( $post->post_excerpt ) : '';

// Get the cover configuration.
$post_thumb = null;
$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 = 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;
}
$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 ( '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 ( ! 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.
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' => $post_thumb_url,
'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,
];
}
}
}

Expand All @@ -160,6 +228,11 @@ public function fetch_exporter() {
];
}

// Attach the final provider slug.
if ( is_array( $post_thumb ) ) {
$post_thumb['provider'] = $cover_provider;
}

// Build the byline.
$byline = $this->format_byline( $post );

Expand Down Expand Up @@ -208,15 +281,39 @@ public function fetch_exporter() {
*/
$excerpt = apply_filters( 'apple_news_exporter_excerpt', $excerpt, $post->ID );

$cover_url = ! empty( $post_thumb['url'] ) ? $post_thumb['url'] : null;

if ( isset( $post_thumb['provider'] ) && 'image' === $post_thumb['provider'] ) {
/**
* Filters the cover image URL of an article before it is sent to Apple News.
*
* The cover image URL is used for the Cover component, if it is active.
*
* @deprecated 2.7.0 Use the `apple_news_exporter_cover_url` filter, which includes all provider types.
*
* @param string|null $url The cover image URL for the post.
* @param int $post_id The ID of the post.
*/
$cover_url = apply_filters_deprecated(
'apple_news_exporter_post_thumb',
[ $cover_url, $post->ID ],
'2.7.0',
'apple_news_exporter_cover_url',
);
}

/**
* Filters the cover image URL of an article before it is sent to Apple News.
* Filters the URL to the cover media of an article before it is sent to Apple News.
*
* The cover image URL is used for the Cover component, if it is active.
* The URL may be an image URL, video file URL, or a URL to an embeddable external video, depending on the provider
* of cover media for the article.
*
* @param string|null $url The cover image URL for the post.
* @param int $post_id The ID of the post.
* @param string|null $url The cover media URL for the post.
* @param string|null $provider The provider of the cover media. Possible values include 'image', 'video_url',
* 'video_id', and 'embedwebvideo'.
* @param int $post_id The ID of the post.
*/
$cover_url = apply_filters( 'apple_news_exporter_post_thumb', ! empty( $post_thumb['url'] ) ? $post_thumb['url'] : null, $post->ID );
$cover_url = apply_filters( 'apple_news_exporter_cover_url', $cover_url, $post_thumb['provider'] ?? null, $post->ID );

/**
* Filters the byline of an article before it is sent to Apple News.
Expand Down Expand Up @@ -296,8 +393,9 @@ public function fetch_exporter() {
$cover_caption = apply_filters( 'apple_news_exporter_cover_caption', $cover_caption, $post->ID );

$post_thumb = [
'caption' => $cover_caption,
'url' => $cover_url,
'provider' => $cover_provider,
'caption' => $cover_caption,
'url' => $cover_url,
];
} else {
$post_thumb = null;
Expand Down
26 changes: 15 additions & 11 deletions admin/class-admin-apple-meta-boxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,21 @@ public static function save_post_meta( $post_id ) {

// Save straightforward fields.
$fields = [
'apple_news_coverimage' => 'integer',
'apple_news_coverimage_caption' => 'textarea',
'apple_news_is_hidden' => 'string',
'apple_news_is_paid' => 'string',
'apple_news_is_preview' => 'string',
'apple_news_is_sponsored' => 'string',
'apple_news_pullquote' => 'string',
'apple_news_pullquote_position' => 'string',
'apple_news_slug' => 'string',
'apple_news_suppress_video_url' => 'boolean',
'apple_news_use_image_component' => 'boolean',
'apple_news_coverimage' => 'integer',
'apple_news_coverimage_caption' => 'textarea',
'apple_news_cover_embedwebvideo_url' => 'string',
'apple_news_cover_media_provider' => 'string',
'apple_news_cover_video_id' => 'integer',
'apple_news_cover_video_url' => 'string',
'apple_news_is_hidden' => 'string',
'apple_news_is_paid' => 'string',
'apple_news_is_preview' => 'string',
'apple_news_is_sponsored' => 'string',
'apple_news_pullquote' => 'string',
'apple_news_pullquote_position' => 'string',
'apple_news_slug' => 'string',
'apple_news_suppress_video_url' => 'boolean',
'apple_news_use_image_component' => 'boolean',
];
foreach ( $fields as $meta_key => $type ) {
switch ( $type ) {
Expand Down
52 changes: 33 additions & 19 deletions admin/class-admin-apple-news.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
require_once dirname( __DIR__ ) . '/includes/REST/apple-news-delete.php';
require_once dirname( __DIR__ ) . '/includes/REST/apple-news-get-published-state.php';
require_once dirname( __DIR__ ) . '/includes/REST/apple-news-get-settings.php';
require_once dirname( __DIR__ ) . '/includes/REST/apple-news-is-valid-cover-media.php';
require_once dirname( __DIR__ ) . '/includes/REST/apple-news-modify-post.php';
require_once dirname( __DIR__ ) . '/includes/REST/apple-news-publish.php';
require_once dirname( __DIR__ ) . '/includes/REST/apple-news-sections.php';
Expand Down Expand Up @@ -99,44 +100,57 @@ public function __construct() {

// Define custom postmeta fields to register.
$postmeta = [
'apple_news_api_created_at' => [
'apple_news_api_created_at' => [
'default' => '',
],
'apple_news_api_id' => [
'apple_news_api_id' => [
'default' => '',
],
'apple_news_api_modified_at' => [
'apple_news_api_modified_at' => [
'default' => '',
],
'apple_news_api_revision' => [
'apple_news_api_revision' => [
'default' => '',
],
'apple_news_api_share_url' => [
'apple_news_api_share_url' => [
'default' => '',
],
'apple_news_coverimage' => [
'apple_news_cover_media_provider' => [
'default' => 'image',
],
'apple_news_coverimage' => [
'default' => 0,
'type' => 'integer',
],
'apple_news_coverimage_caption' => [
'default' => '',
],
'apple_news_cover_video_id' => [
'default' => 0,
'type' => 'integer',
],
'apple_news_coverimage_caption' => [
'apple_news_cover_video_url' => [
'default' => '',
],
'apple_news_cover_embedwebvideo_url' => [
'default' => '',
],
'apple_news_is_hidden' => [
'apple_news_is_hidden' => [
'default' => '',
],
'apple_news_is_paid' => [
'apple_news_is_paid' => [
'default' => '',
],
'apple_news_is_preview' => [
'apple_news_is_preview' => [
'default' => '',
],
'apple_news_is_sponsored' => [
'apple_news_is_sponsored' => [
'default' => '',
],
'apple_news_maturity_rating' => [
'apple_news_maturity_rating' => [
'default' => '',
],
'apple_news_metadata' => [
'apple_news_metadata' => [
'default' => '',
'sanitize_callback' => function ( $value ) {
return ! empty( $value ) && is_string( $value ) ? json_decode( $value, true ) : $value;
Expand All @@ -146,16 +160,16 @@ public function __construct() {
],
'type' => 'string',
],
'apple_news_pullquote' => [
'apple_news_pullquote' => [
'default' => '',
],
'apple_news_pullquote_position' => [
'apple_news_pullquote_position' => [
'default' => '',
],
'apple_news_slug' => [
'apple_news_slug' => [
'default' => '',
],
'apple_news_sections' => [
'apple_news_sections' => [
'default' => [],
'show_in_rest' => [
'schema' => [
Expand All @@ -167,11 +181,11 @@ public function __construct() {
],
'type' => 'array',
],
'apple_news_suppress_video_url' => [
'apple_news_suppress_video_url' => [
'default' => false,
'type' => 'boolean',
],
'apple_news_use_image_component' => [
'apple_news_use_image_component' => [
'default' => false,
'type' => 'boolean',
],
Expand Down
6 changes: 4 additions & 2 deletions admin/class-admin-apple-post-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,10 @@ public function do_publish( $id, $post ) {

// Proceed based on the current settings for auto publish and update.
$updated = get_post_meta( $id, 'apple_news_api_id', true );
if ( ( $updated && 'yes' !== $this->settings->api_autosync_update )
|| ( ! $updated && 'yes' !== $this->settings->api_autosync ) ) {
if (
( $updated && 'yes' !== $this->settings->api_autosync_update )
|| ( ! $updated && 'yes' !== $this->settings->api_autosync )
) {
return;
}

Expand Down
Loading