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

Jetpack Sync: extract video and audio shortcodes #38556

Merged
merged 5 commits into from
Aug 9, 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
7 changes: 7 additions & 0 deletions projects/plugins/jetpack/_inc/lib/class.media-extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ class Jetpack_Media_Meta_Extractor {
* @var string[]
*/
private static $keeper_shortcodes = array(
'audio',
'youtube',
'vimeo',
'hulu',
'ted',
'video',
'wpvideo',
'videopress',
);
Expand Down Expand Up @@ -197,6 +199,11 @@ public static function extract_from_content( $content, $what_to_extract = self::
$id = call_user_func( $shortcode_get_id_func, $attr );
} elseif ( method_exists( $shortcode_class_name, $shortcode_get_id_method ) ) {
$id = call_user_func( array( $shortcode_class_name, $shortcode_get_id_method ), $attr );
} elseif ( 'video' === $shortcode ) {
$id = $attr['src'] ?? $attr['url'] ?? $attr['mp4'] ?? $attr['m4v'] ?? $attr['webm'] ?? $attr['ogv'] ?? $attr['wmv'] ?? $attr['flv'] ?? null;
} elseif ( 'audio' === $shortcode ) {
preg_match( '#(https?://(?:[^\s"|\']+)\.(?:mp3|ogg|flac|m4a|wav))([ "\'|]|$)#', implode( ' ', $attr ), $audio_matches );
$id = $audio_matches[1] ?? null;
}
if ( ! empty( $id )
&& ( ! isset( $shortcode_details[ $shortcode_name ] ) || ! in_array( $id, $shortcode_details[ $shortcode_name ], true ) ) ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

Support video and audio shortcodes in Media Extractor
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,9 @@ public function test_extract_shortcodes() {
),
'video' => array(
'count' => 1,
'id' => array(
'video-source.mp4',
),
),
'gallery' => array(
'count' => 1,
Expand Down Expand Up @@ -746,4 +749,69 @@ public function test_mediaextractor_alt_text() {

$this->assertEquals( $expected, $result );
}

/**
* Verify that audio shortcode URL is extracted.
*
* @covers Jetpack_Media_Meta_Extractor::extract
*/
public function test_audio_shortcode() {
$post_id = self::factory()->post->create(
array(
'post_content' => 'Sed dapibus ut mauris imperdiet volutpat. [audio http://example.com/files/2011/02/audio-post.mp3|titles=Audio Post] Nullam [audio https://example.com/place/test/filename.flac] in dolor vel nulla pulvinar accumsan facilisis quis lorem. [audio="http://example.com/file.wav"]',
)
);

$expected = array(
'has' => array( 'shortcode' => 3 ),
'shortcode' => array(
'audio' => array(
'count' => 3,
'id' => array(
'http://example.com/files/2011/02/audio-post.mp3',
'https://example.com/place/test/filename.flac',
'http://example.com/file.wav',
),
),
),
'shortcode_types' => array( 'audio' ),
);

$result = Jetpack_Media_Meta_Extractor::extract( get_current_blog_id(), $post_id );

$this->assertEquals( $expected, $result );
}

/**
* Verify that video shortcode URL is extracted.
*
* @covers Jetpack_Media_Meta_Extractor::extract
*/
public function test_video_shortcode() {
$post_id = self::factory()->post->create(
array(
'post_content' => 'Lorem ipsum. [video wmv="http://example.com/video.wmv"][/video] Sed dapibus ut mauris imperdiet volutpat. [video width="1920" height="1080" mp4="https://example.files.wordpress.com/dir1/dir2/file.mp4"][/video] Nullam [video url="https://example.com/promo.mp4"] in dolor vel nulla pulvinar accumsan facilisis quis lorem. [video src="https://example.com/promo.m4v"] ',
)
);

$expected = array(
'has' => array( 'shortcode' => 4 ),
'shortcode' => array(
'video' => array(
'count' => 4,
'id' => array(
'http://example.com/video.wmv',
'https://example.files.wordpress.com/dir1/dir2/file.mp4',
'https://example.com/promo.mp4',
'https://example.com/promo.m4v',
),
),
),
'shortcode_types' => array( 'video' ),
);

$result = Jetpack_Media_Meta_Extractor::extract( get_current_blog_id(), $post_id );

$this->assertEquals( $expected, $result );
}
}
Loading