'https://schema.org',
'@type' => 'audio' === $type ? 'AudioObject' : 'VideoObject',
'name' => get_the_title( $post_id ),
'description' => $description,
'url' => get_permalink( $post_id ),
'mainEntityOfPage' => get_permalink( $post_id ),
'uploadDate' => get_post_time( DATE_W3C, true, $post_id ),
'dateModified' => get_post_modified_time( DATE_W3C, true, $post_id ),
);
if ( $thumbnail ) {
$schema['thumbnailUrl'] = array( esc_url_raw( $thumbnail ) );
}
if ( ! empty( $media_urls['content_url'] ) ) {
$schema['contentUrl'] = esc_url_raw( $media_urls['content_url'] );
}
if ( ! empty( $media_urls['embed_url'] ) ) {
$schema['embedUrl'] = esc_url_raw( $media_urls['embed_url'] );
}
if ( $views > 0 ) {
$schema['interactionStatistic'] = array(
array(
'@type' => 'InteractionCounter',
'interactionType' => array( '@type' => 'audio' === $type ? 'ListenAction' : 'WatchAction' ),
'userInteractionCount' => $views,
),
);
}
if ( $likes > 0 ) {
$schema['interactionStatistic'] = $schema['interactionStatistic'] ?? array();
$schema['interactionStatistic'][] = array(
'@type' => 'InteractionCounter',
'interactionType' => array( '@type' => 'LikeAction' ),
'userInteractionCount' => $likes,
);
}
$schema = apply_filters( 'sg7g_structured_data', $schema, $post_id );
echo '' . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
public static function output_social_meta(): void {
if ( ! is_singular( 'sg7g_media' ) || self::seo_plugin_active() ) {
return;
}
$post_id = get_queried_object_id();
if ( post_password_required( $post_id ) ) {
return;
}
$post = get_post( $post_id );
$description = $post instanceof WP_Post ? self::description( $post ) : '';
$image = get_the_post_thumbnail_url( $post_id, 'full' );
$title = get_the_title( $post_id );
$url = get_permalink( $post_id );
self::meta_property( 'og:type', 'article' );
self::meta_property( 'og:title', $title );
self::meta_property( 'og:description', $description );
self::meta_property( 'og:url', $url );
self::meta_name( 'twitter:card', $image ? 'summary_large_image' : 'summary' );
self::meta_name( 'twitter:title', $title );
self::meta_name( 'twitter:description', $description );
if ( $image ) {
self::meta_property( 'og:image', $image );
self::meta_name( 'twitter:image', $image );
}
}
private static function description( WP_Post $post ): string {
$text = has_excerpt( $post ) ? $post->post_excerpt : $post->post_content;
$text = wp_strip_all_tags( strip_shortcodes( $text ) );
$text = preg_replace( '/\s+/u', ' ', $text );
$text = trim( (string) $text );
if ( '' === $text ) {
$text = get_the_title( $post );
}
return wp_trim_words( $text, 36, '…' );
}
private static function seo_plugin_active(): bool {
$active = defined( 'WPSEO_VERSION' )
|| defined( 'RANK_MATH_VERSION' )
|| defined( 'AIOSEO_VERSION' )
|| defined( 'SEOPRESS_VERSION' )
|| defined( 'THE_SEO_FRAMEWORK_VERSION' )
|| class_exists( 'WPSEO_Options' )
|| class_exists( 'RankMath' )
|| class_exists( 'AIOSEO\Plugin\AIOSEO' );
return (bool) apply_filters( 'sg7g_seo_plugin_active', $active );
}
private static function meta_property( string $property, string $content ): void {
if ( '' !== $content ) {
printf( '' . "\n", esc_attr( $property ), esc_attr( $content ) );
}
}
private static function meta_name( string $name, string $content ): void {
if ( '' !== $content ) {
printf( '' . "\n", esc_attr( $name ), esc_attr( $content ) );
}
}
}