Commit realizado el 12:13:52 08-04-2024
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
/**
|
||||
* The DailyMotion Video.
|
||||
*
|
||||
* @since 2.7.1
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Schema\Video
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMathPro\Schema\Video;
|
||||
|
||||
use MyThemeShop\Helpers\Str;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* DailyMotion class.
|
||||
*/
|
||||
class DailyMotion {
|
||||
|
||||
/**
|
||||
* Match url.
|
||||
*
|
||||
* @param string $url Url to match.
|
||||
* @return bool
|
||||
*/
|
||||
public static function match( $url ) {
|
||||
if ( ! Str::contains( 'dailymotion.com', $url ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$id = strtok( basename( $url ), '_' );
|
||||
if ( empty( $id ) ) {
|
||||
return false;
|
||||
}
|
||||
return self::fetch_data( $id, $url );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch data.
|
||||
*
|
||||
* @param string $video_id Video ID.
|
||||
* @param string $url Video Source.
|
||||
* @return array
|
||||
*
|
||||
* @link https://developer.dailymotion.com/api/#video-fields
|
||||
*/
|
||||
private static function fetch_data( $video_id, $url ) {
|
||||
$data = [
|
||||
'src' => $url,
|
||||
'embed' => true,
|
||||
];
|
||||
|
||||
$response = wp_remote_get( "https://api.dailymotion.com/video/{$video_id}?fields=title,description,duration,thumbnail_url,width,height,created_time" );
|
||||
if (
|
||||
is_wp_error( $response ) ||
|
||||
! in_array( wp_remote_retrieve_response_code( $response ), [ 200, 204 ], true )
|
||||
) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
$content = wp_remote_retrieve_body( $response );
|
||||
$content = json_decode( $content, true );
|
||||
$data = [
|
||||
'name' => ! empty( $content['title'] ) ? $content['title'] : '',
|
||||
'description' => ! empty( $content['description'] ) ? $content['description'] : '',
|
||||
'src' => $url,
|
||||
'embed' => true,
|
||||
'width' => $content['width'],
|
||||
'height' => $content['height'],
|
||||
'isFamilyFriendly' => true,
|
||||
'duration' => 'PT' . $content['duration'] . 'S',
|
||||
'thumbnail' => $content['thumbnail_url'],
|
||||
'uploadDate' => gmdate( 'Y-m-d\TH:i:s', $content['created_time'] ),
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
@@ -0,0 +1,400 @@
|
||||
<?php
|
||||
/**
|
||||
* The Video Parser
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Schema\Video
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMathPro\Schema\Video;
|
||||
|
||||
use RankMath\Helper;
|
||||
use RankMath\Schema\DB;
|
||||
use MyThemeShop\Helpers\Str;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Parser class.
|
||||
*/
|
||||
class Parser {
|
||||
|
||||
/**
|
||||
* Post.
|
||||
*
|
||||
* @var WP_Post
|
||||
*/
|
||||
private $post;
|
||||
|
||||
/**
|
||||
* Stored Video URLs.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $urls;
|
||||
|
||||
/**
|
||||
* The Constructor.
|
||||
*
|
||||
* @param WP_Post $post Post to parse.
|
||||
*/
|
||||
public function __construct( $post ) {
|
||||
$this->post = $post;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save video object.
|
||||
*/
|
||||
public function save() {
|
||||
if (
|
||||
! ( $this->post instanceof \WP_Post ) ||
|
||||
wp_is_post_revision( $this->post->ID ) ||
|
||||
! Helper::get_settings( "titles.pt_{$this->post->post_type}_autodetect_video", 'on' )
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
$content = trim( $this->post->post_content . ' ' . $this->get_custom_fields_data() );
|
||||
if ( empty( $content ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->urls = $this->get_video_urls();
|
||||
$content = apply_filters( 'the_content', $content );
|
||||
$allowed_types = apply_filters( 'media_embedded_in_content_allowed_types', [ 'video', 'embed', 'iframe' ] );
|
||||
$tags = implode( '|', $allowed_types );
|
||||
$videos = [];
|
||||
|
||||
preg_match_all( '#<(?P<tag>' . $tags . ')[^<]*?(?:>[\s\S]*?<\/(?P=tag)>|\s*\/>)#', $content, $matches );
|
||||
if ( ! empty( $matches ) && ! empty( $matches[0] ) ) {
|
||||
foreach ( $matches[0] as $html ) {
|
||||
$videos[] = $this->get_metadata( $html );
|
||||
}
|
||||
}
|
||||
|
||||
$videos = array_merge( $videos, $this->get_links_from_shortcode( $content ) );
|
||||
$videos = array_filter(
|
||||
$videos,
|
||||
function( $video ) {
|
||||
return ! empty( $video['src'] ) ? $video['src'] : false;
|
||||
}
|
||||
);
|
||||
|
||||
if ( empty( $videos ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$schemas = $this->get_default_schema_data();
|
||||
foreach ( $videos as $video ) {
|
||||
$schemas[] = [
|
||||
'@type' => 'VideoObject',
|
||||
'metadata' => [
|
||||
'title' => 'Video',
|
||||
'type' => 'template',
|
||||
'shortcode' => uniqid( 's-' ),
|
||||
'isPrimary' => empty( DB::get_schemas( $this->post->ID ) ),
|
||||
'reviewLocationShortcode' => '[rank_math_rich_snippet]',
|
||||
'category' => '%categories%',
|
||||
'tags' => '%tags%',
|
||||
'isAutoGenerated' => true,
|
||||
],
|
||||
'name' => ! empty( $video['name'] ) ? $video['name'] : '%seo_title%',
|
||||
'description' => ! empty( $video['description'] ) ? $video['description'] : '%seo_description%',
|
||||
'uploadDate' => ! empty( $video['uploadDate'] ) ? $video['uploadDate'] : '%date(Y-m-dTH:i:sP)%',
|
||||
'thumbnailUrl' => ! empty( $video['thumbnail'] ) ? $video['thumbnail'] : '%post_thumbnail%',
|
||||
'embedUrl' => ! empty( $video['embed'] ) ? $video['src'] : '',
|
||||
'contentUrl' => empty( $video['embed'] ) ? $video['src'] : '',
|
||||
'duration' => ! empty( $video['duration'] ) ? $video['duration'] : '',
|
||||
'width' => ! empty( $video['width'] ) ? $video['width'] : '',
|
||||
'height' => ! empty( $video['height'] ) ? $video['height'] : '',
|
||||
'isFamilyFriendly' => ! empty( $video['isFamilyFriendly'] ) ? (bool) $video['isFamilyFriendly'] : true,
|
||||
];
|
||||
}
|
||||
|
||||
foreach ( array_filter( $schemas ) as $schema ) {
|
||||
add_post_meta( $this->post->ID, "rank_math_schema_{$schema['@type']}", $schema );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default schema data.
|
||||
*/
|
||||
private function get_default_schema_data() {
|
||||
if ( ! empty( DB::get_schemas( $this->post->ID ) ) ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$default_type = Helper::get_default_schema_type( $this->post->ID, true );
|
||||
if ( ! $default_type ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$is_article = in_array( $default_type, [ 'Article', 'NewsArticle', 'BlogPosting' ], true );
|
||||
$schema_data = [];
|
||||
if ( $is_article ) {
|
||||
$schema_data = [
|
||||
'headline' => Helper::get_settings( "titles.pt_{$this->post->post_type}_default_snippet_name" ),
|
||||
'description' => Helper::get_settings( "titles.pt_{$this->post->post_type}_default_snippet_desc" ),
|
||||
'datePublished' => '%date(Y-m-dTH:i:sP)%',
|
||||
'dateModified' => '%modified(Y-m-dTH:i:sP)%',
|
||||
'image' => [
|
||||
'@type' => 'ImageObject',
|
||||
'url' => '%post_thumbnail%',
|
||||
],
|
||||
'author' => [
|
||||
'@type' => 'Person',
|
||||
'name' => '%name%',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
$schema_data['@type'] = $default_type;
|
||||
$schema_data['metadata'] = [
|
||||
'title' => Helper::sanitize_schema_title( $default_type ),
|
||||
'type' => 'template',
|
||||
'isPrimary' => true,
|
||||
];
|
||||
|
||||
return [ $schema_data ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Video source from the content.
|
||||
*
|
||||
* @param array $html Video Links.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_metadata( $html ) {
|
||||
preg_match_all( '@src=[\'"]([^"]+)[\'"]@', $html, $matches );
|
||||
if ( empty( $matches ) || empty( $matches[1] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->get_video_metadata( $matches[1][0] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate Video source.
|
||||
*
|
||||
* @param string $url Video Source.
|
||||
* @return array
|
||||
*/
|
||||
private function get_video_metadata( $url ) {
|
||||
$url = preg_replace( '/\?.*/', '', $url ); // Remove query string from URL.
|
||||
if (
|
||||
$url &&
|
||||
(
|
||||
is_array( $this->urls ) &&
|
||||
(
|
||||
in_array( $url, $this->urls, true ) ||
|
||||
in_array( $url . '?feature=oembed', $this->urls, true )
|
||||
)
|
||||
)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->urls[] = $url;
|
||||
$networks = [
|
||||
'Video\Youtube',
|
||||
'Video\Vimeo',
|
||||
'Video\DailyMotion',
|
||||
'Video\TedVideos',
|
||||
'Video\VideoPress',
|
||||
'Video\WordPress',
|
||||
];
|
||||
|
||||
$data = false;
|
||||
foreach ( $networks as $network ) {
|
||||
$data = \call_user_func( [ '\\RankMathPro\\Schema\\' . $network, 'match' ], $url );
|
||||
if ( is_array( $data ) ) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Save image locally.
|
||||
if ( ! empty( $data['thumbnail'] ) ) {
|
||||
$data['thumbnail'] = $this->save_video_thumbnail( $data );
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Video Links from YouTube Embed plugin.
|
||||
*
|
||||
* @param string $content Post Content.
|
||||
* @return array
|
||||
*
|
||||
* Credit ridgerunner (https://stackoverflow.com/users/433790/ridgerunner)
|
||||
*/
|
||||
private function get_links_from_shortcode( $content ) {
|
||||
preg_match_all(
|
||||
'~
|
||||
https?:// # Required scheme. Either http or https.
|
||||
(?:[0-9A-Z-]+\.)? # Optional subdomain.
|
||||
(?: # Group host alternatives.
|
||||
youtu\.be/ # Either youtu.be,
|
||||
| youtube # or youtube.com or
|
||||
(?:-nocookie)? # youtube-nocookie.com
|
||||
\.com # followed by
|
||||
\S*? # Allow anything up to VIDEO_ID,
|
||||
[^\w\s-] # but char before ID is non-ID char.
|
||||
) # End host alternatives.
|
||||
([\w-]{11}) # $1: VIDEO_ID is exactly 11 chars.
|
||||
(?=[^\w-]|$) # Assert next char is non-ID or EOS.
|
||||
(?! # Assert URL is not pre-linked.
|
||||
[?=&+%\w.-]* # Allow URL (query) remainder.
|
||||
(?: # Group pre-linked alternatives.
|
||||
[\'"][^<>]*> # Either inside a start tag,
|
||||
| </a> # or inside <a> element text contents.
|
||||
) # End recognized pre-linked alts.
|
||||
) # End negative lookahead assertion.
|
||||
[?=&+%\w.-]* # Consume any URL (query) remainder.
|
||||
~ix',
|
||||
$content,
|
||||
$matches
|
||||
);
|
||||
|
||||
if ( empty( $matches ) || empty( $matches[1] ) ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$data = [];
|
||||
foreach ( $matches[1] as $video_id ) {
|
||||
$data[] = $this->get_video_metadata( "https://www.youtube.com/embed/{$video_id}" );
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate Video source.
|
||||
*
|
||||
* @param array $data Video data.
|
||||
* @return array
|
||||
*
|
||||
* Credits to m1r0 @ https://gist.github.com/m1r0/f22d5237ee93bcccb0d9
|
||||
*/
|
||||
private function save_video_thumbnail( $data ) {
|
||||
$url = $data['thumbnail'];
|
||||
if ( ! Helper::get_settings( "titles.pt_{$this->post->post_type}_autogenerate_image", 'off' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'WP_Http' ) ) {
|
||||
include_once( ABSPATH . WPINC . '/class-http.php' );
|
||||
}
|
||||
|
||||
$url = explode( '?', $url )[0];
|
||||
$http = new \WP_Http();
|
||||
$response = $http->request( $url );
|
||||
if ( 200 !== $response['response']['code'] ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$image_title = __( 'Video Thumbnail', 'rank-math-pro' );
|
||||
if ( ! empty( $data['name'] ) ) {
|
||||
$image_title = $data['name'];
|
||||
} elseif ( ! empty( $this->post->post_title ) ) {
|
||||
$image_title = $this->post->post_title;
|
||||
}
|
||||
$filename = substr( sanitize_title( $image_title, 'video-thumbnail' ), 0, 32 ) . '.jpg';
|
||||
|
||||
/**
|
||||
* Filter the filename of the video thumbnail.
|
||||
*
|
||||
* @param string $filename The filename of the video thumbnail.
|
||||
* @param array $data The video data.
|
||||
* @param object $post The post object.
|
||||
*/
|
||||
$filename = apply_filters( 'rank_math/schema/video_thumbnail_filename', $filename, $data, $this->post );
|
||||
|
||||
$upload = wp_upload_bits( sanitize_file_name( $filename ), null, $response['body'] );
|
||||
if ( ! empty( $upload['error'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$file_path = $upload['file'];
|
||||
$file_name = basename( $file_path );
|
||||
$file_type = wp_check_filetype( $file_name, null );
|
||||
$wp_upload_dir = wp_upload_dir();
|
||||
|
||||
// Translators: Placeholder is the image title.
|
||||
$attachment_title = sprintf( __( 'Video Thumbnail: %s', 'rank-math-pro' ), $image_title );
|
||||
|
||||
/**
|
||||
* Filter the attachment title of the video thumbnail.
|
||||
*
|
||||
* @param string $attachment_title The attachment title of the video thumbnail.
|
||||
* @param array $data The video data.
|
||||
* @param object $post The post object.
|
||||
*/
|
||||
$attachment_title = apply_filters( 'rank_math/schema/video_thumbnail_attachment_title', $attachment_title, $data, $this->post );
|
||||
|
||||
$post_info = [
|
||||
'guid' => $wp_upload_dir['url'] . '/' . $file_name,
|
||||
'post_mime_type' => $file_type['type'],
|
||||
'post_title' => $attachment_title,
|
||||
'post_content' => '',
|
||||
'post_status' => 'inherit',
|
||||
];
|
||||
|
||||
$attach_id = wp_insert_attachment( $post_info, $file_path, $this->post->ID );
|
||||
|
||||
// Include image.php.
|
||||
require_once( ABSPATH . 'wp-admin/includes/image.php' );
|
||||
|
||||
// Define attachment metadata.
|
||||
$attach_data = wp_generate_attachment_metadata( $attach_id, $file_path );
|
||||
|
||||
// Assign metadata to attachment.
|
||||
wp_update_attachment_metadata( $attach_id, $attach_data );
|
||||
|
||||
return wp_get_attachment_url( $attach_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Video URls stored in VideoObject schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_video_urls() {
|
||||
$schemas = DB::get_schemas( $this->post->ID );
|
||||
if ( empty( $schemas ) ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$urls = [];
|
||||
foreach ( $schemas as $schema ) {
|
||||
if ( empty( $schema['@type'] ) || 'VideoObject' !== $schema['@type'] ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$urls[] = ! empty( $schema['embedUrl'] ) ? $schema['embedUrl'] : '';
|
||||
$urls[] = ! empty( $schema['contentUrl'] ) ? $schema['contentUrl'] : '';
|
||||
}
|
||||
|
||||
return array_filter( $urls );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Custom fields data.
|
||||
*/
|
||||
private function get_custom_fields_data() {
|
||||
$custom_fields = Str::to_arr_no_empty( Helper::get_settings( 'sitemap.video_sitemap_custom_fields' ) );
|
||||
if ( empty( $custom_fields ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$content = '';
|
||||
foreach ( $custom_fields as $custom_field ) {
|
||||
$content = $content . ' ' . get_post_meta( $this->post->ID, $custom_field, true );
|
||||
}
|
||||
|
||||
return trim( $content );
|
||||
}
|
||||
}
|
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
/**
|
||||
* The TedVideos
|
||||
*
|
||||
* @since 2.7.1
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Schema\Video
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMathPro\Schema\Video;
|
||||
|
||||
use RankMath\Helper;
|
||||
use MyThemeShop\Helpers\Str;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* TedVideos class.
|
||||
*/
|
||||
class TedVideos {
|
||||
|
||||
/**
|
||||
* Match url.
|
||||
*
|
||||
* @param string $url Url to match.
|
||||
* @return bool
|
||||
*/
|
||||
public static function match( $url ) {
|
||||
if ( ! Str::contains( 'ted.com', $url ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return self::fetch_data( $url );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch data.
|
||||
*
|
||||
* @param string $url Video Source.
|
||||
* @return array
|
||||
*/
|
||||
private static function fetch_data( $url ) {
|
||||
$data = [
|
||||
'src' => $url,
|
||||
'embed' => true,
|
||||
];
|
||||
|
||||
$response = wp_remote_get( str_replace( 'embed.', '', $url ) );
|
||||
if (
|
||||
is_wp_error( $response ) ||
|
||||
! in_array( wp_remote_retrieve_response_code( $response ), [ 200, 204 ], true )
|
||||
) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
$content = wp_remote_retrieve_body( $response );
|
||||
preg_match_all( "/<meta content='(.*?)' itemprop='(width|height|duration|uploadDate)'>/i", $content, $item_props, PREG_SET_ORDER );
|
||||
|
||||
foreach ( $item_props as $item_prop ) {
|
||||
$data[ $item_prop[2] ] = $item_prop[1];
|
||||
}
|
||||
|
||||
preg_match_all( '/<meta name="(title|description)" content="(.*?)" \/>/i', $content, $item_props, PREG_SET_ORDER );
|
||||
foreach ( $item_props as $item_prop ) {
|
||||
$key = 'title' === $item_prop[1] ? 'name' : $item_prop[1];
|
||||
$data[ $key ] = $item_prop[2];
|
||||
}
|
||||
|
||||
preg_match( '/<meta property="og:image" content="(.*?)" \/>/i', $content, $image );
|
||||
$data['thumbnail'] = ! empty( $image ) && isset( $image[1] ) ? $image[1] : '';
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
/**
|
||||
* The VideoPress.
|
||||
*
|
||||
* @since 2.7.1
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Schema\Video
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMathPro\Schema\Video;
|
||||
|
||||
use MyThemeShop\Helpers\Str;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* VideoPress class.
|
||||
*/
|
||||
class VideoPress {
|
||||
|
||||
/**
|
||||
* Match url.
|
||||
*
|
||||
* @param string $url Url to match.
|
||||
* @return bool
|
||||
*/
|
||||
public static function match( $url ) {
|
||||
if ( ! Str::contains( 'video.wordpress.com', $url ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$video_id = str_replace( 'https://video.wordpress.com/embed/', '', $url );
|
||||
if ( ! $video_id ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return self::fetch_data( $video_id, $url );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch data.
|
||||
*
|
||||
* @param string $video_id Video ID.
|
||||
* @param string $url Video Source.
|
||||
* @return array
|
||||
*
|
||||
* @see https://developer.wordpress.com/docs/api/1/get/videos/%24guid/
|
||||
*/
|
||||
private static function fetch_data( $video_id, $url ) {
|
||||
$data = [
|
||||
'src' => $url,
|
||||
'embed' => true,
|
||||
];
|
||||
|
||||
$response = wp_remote_get( "https://public-api.wordpress.com/rest/v1.1/videos/{$video_id}" );
|
||||
if (
|
||||
is_wp_error( $response ) ||
|
||||
! in_array( wp_remote_retrieve_response_code( $response ), [ 200, 204 ], true )
|
||||
) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
$content = wp_remote_retrieve_body( $response );
|
||||
$content = json_decode( $content, true );
|
||||
$data = [
|
||||
'name' => ! empty( $content['title'] ) ? $content['title'] : '',
|
||||
'description' => ! empty( $content['description'] ) ? $content['description'] : '',
|
||||
'src' => $url,
|
||||
'embed' => true,
|
||||
'width' => isset( $content['width'] ) ? $content['width'] : '',
|
||||
'height' => isset( $content['height'] ) ? $content['height'] : '',
|
||||
'isFamilyFriendly' => true,
|
||||
'duration' => isset( $content['duration'] ) ? 'PT' . $content['duration'] . 'S' : '',
|
||||
'thumbnail' => isset( $content['poster'] ) ? $content['poster'] : '',
|
||||
'uploadDate' => isset( $content['upload_date'] ) ? $content['upload_date'] : '',
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
/**
|
||||
* The Vimeo
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Schema\Video
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMathPro\Schema\Video;
|
||||
|
||||
use RankMath\Helper;
|
||||
use RankMath\Admin\Admin_Helper;
|
||||
use MyThemeShop\Helpers\Str;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Vimeo class.
|
||||
*/
|
||||
class Vimeo {
|
||||
|
||||
/**
|
||||
* Match url.
|
||||
*
|
||||
* @param string $url Url to match.
|
||||
* @return bool
|
||||
*/
|
||||
public static function match( $url ) {
|
||||
if ( ! Str::contains( 'vimeo.com', $url ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
preg_match( '#(https?://)?(www.)?(player.)?vimeo.com/([a-z]*/)*([0-9]{6,11})[?]?.*#', $url, $match );
|
||||
if ( empty( $match[5] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return self::fetch_data( $match[5], $url );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch data.
|
||||
*
|
||||
* @param string $video_id Video ID.
|
||||
* @param string $url Video Source.
|
||||
* @return array
|
||||
*/
|
||||
private static function fetch_data( $video_id, $url ) {
|
||||
$data = [
|
||||
'src' => $url,
|
||||
'embed' => true,
|
||||
];
|
||||
|
||||
$response = wp_remote_get( "http://vimeo.com/api/v2/video/{$video_id}/json" );
|
||||
if (
|
||||
is_wp_error( $response ) ||
|
||||
! in_array( wp_remote_retrieve_response_code( $response ), [ 200, 204 ], true )
|
||||
) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
$content = wp_remote_retrieve_body( $response );
|
||||
$content = json_decode( $content, true );
|
||||
$content = $content[0];
|
||||
|
||||
$data = [
|
||||
'name' => ! empty( $content['title'] ) ? $content['title'] : '',
|
||||
'description' => ! empty( $content['description'] ) ? $content['description'] : '',
|
||||
'src' => $url,
|
||||
'embed' => true,
|
||||
'width' => $content['width'],
|
||||
'height' => $content['height'],
|
||||
'isFamilyFriendly' => true,
|
||||
'duration' => 'PT' . $content['duration'] . 'S',
|
||||
'thumbnail' => $content['thumbnail_large'],
|
||||
'uploadDate' => $content['upload_date'],
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
/**
|
||||
* The WordPress
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Schema\Video
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMathPro\Schema\Video;
|
||||
|
||||
use RankMath\Helper;
|
||||
use RankMath\Admin\Admin_Helper;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* WordPress class.
|
||||
*/
|
||||
class WordPress {
|
||||
|
||||
/**
|
||||
* Match url.
|
||||
*
|
||||
* @param string $url Url to match.
|
||||
* @return bool
|
||||
*/
|
||||
public static function match( $url ) {
|
||||
$type = wp_check_filetype( $url, wp_get_mime_types() );
|
||||
|
||||
if ( ! in_array( strtolower( $type['ext'] ), wp_get_video_extensions(), true ) ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return self::fetch_data( $url );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch data.
|
||||
*
|
||||
* @param string $url Video Source.
|
||||
* @return array
|
||||
*/
|
||||
private static function fetch_data( $url ) {
|
||||
$data = [];
|
||||
$attachment_id = attachment_url_to_postid( $url );
|
||||
if ( $attachment_id ) {
|
||||
$video_details = wp_get_attachment_metadata( $attachment_id );
|
||||
$data = [
|
||||
'width' => ! empty( $video_details['width'] ) ? $video_details['width'] : '',
|
||||
'height' => ! empty( $video_details['height'] ) ? $video_details['height'] : '',
|
||||
];
|
||||
}
|
||||
|
||||
return array_merge(
|
||||
[
|
||||
'src' => $url,
|
||||
'embed' => false,
|
||||
],
|
||||
$data
|
||||
);
|
||||
}
|
||||
}
|
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
/**
|
||||
* The Youtube
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Schema\Video
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMathPro\Schema\Video;
|
||||
|
||||
use RankMath\Helper;
|
||||
use RankMath\Admin\Admin_Helper;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Youtube class.
|
||||
*/
|
||||
class Youtube {
|
||||
|
||||
/**
|
||||
* Match url.
|
||||
*
|
||||
* @param string $url Url to match.
|
||||
* @return bool
|
||||
*/
|
||||
public static function match( $url ) {
|
||||
if ( ! preg_match( '#^https?://(?:www\.)?(?:youtube\.com/|youtu\.be/)#', $url ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
preg_match( '%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match );
|
||||
if ( empty( $match[1] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return self::fetch_data( $match[1], $url );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch data.
|
||||
*
|
||||
* @param string $video_id Video ID.
|
||||
* @param string $url Video Source.
|
||||
* @return array
|
||||
*/
|
||||
private static function fetch_data( $video_id, $url ) {
|
||||
$data = [
|
||||
'src' => $url,
|
||||
'embed' => true,
|
||||
];
|
||||
|
||||
$response = wp_remote_get( "https://www.youtube.com/watch?v={$video_id}" );
|
||||
if (
|
||||
is_wp_error( $response ) ||
|
||||
! in_array( wp_remote_retrieve_response_code( $response ), [ 200, 204 ], true )
|
||||
) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
$content = wp_remote_retrieve_body( $response );
|
||||
preg_match_all( '/<meta itemprop="(width|height|isFamilyFriendly|duration|uploadDate)" content="(.*?)">/i', $content, $item_props, PREG_SET_ORDER );
|
||||
foreach ( $item_props as $item_prop ) {
|
||||
$data[ $item_prop[1] ] = $item_prop[2];
|
||||
}
|
||||
|
||||
preg_match_all( '/<meta name="(title|description)" content="(.*?)">/i', $content, $item_props, PREG_SET_ORDER );
|
||||
foreach ( $item_props as $item_prop ) {
|
||||
$key = 'title' === $item_prop[1] ? 'name' : $item_prop[1];
|
||||
$data[ $key ] = $item_prop[2];
|
||||
}
|
||||
|
||||
preg_match( '/<meta property="og:image" content="(.*?)">/i', $content, $image );
|
||||
$data['thumbnail'] = ! empty( $image ) && isset( $image[1] ) ? $image[1] : '';
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user