forked from Automattic/fb-instant-articles
-
Notifications
You must be signed in to change notification settings - Fork 1
/
embeds.php
106 lines (90 loc) · 3.5 KB
/
embeds.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* Facebook Instant Articles for WP.
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*
* @package default
*/
/**
* Remove all extra oembed html filters added by themes and plugins.
*/
remove_all_filters( 'embed_oembed_html' );
/**
* Filter the oembed results to see if we should do some extra handling
*
* @since 0.1
* @param string $html The original HTML returned from the external oembed provider.
* @param string $url The URL found in the content.
* @param mixed $attr An array with extra attributes.
* @param int $post_id The post ID.
* @return string The potentially filtered HTML.
*/
function instant_articles_embed_oembed_html( $html, $url, $attr, $post_id ) {
if ( ! class_exists( 'WP_oEmbed' ) ) {
include_once( ABSPATH . WPINC . '/class-oembed.php' );
}
// Instead of checking all possible URL variants, use the provider list from WP_oEmbed.
$wp_oembed = new WP_oEmbed();
$provider_url = $wp_oembed->get_provider( $url );
$provider_name = false;
if ( false !== strpos( $provider_url, 'instagram.com' ) ) {
$provider_name = 'instagram';
} elseif ( false !== strpos( $provider_url, 'twitter.com' ) ) {
$provider_name = 'twitter';
} elseif ( false !== strpos( $provider_url, 'youtube.com' ) ) {
$provider_name = 'youtube';
} elseif( false !== strpos( $provider_url, 'vimeo.com' ) ) {
$provider_name = 'vimeo';
} elseif( false !== strpos( $provider_url, 'vine.co' ) ) {
$provider_name = 'vine';
} elseif( false !== strpos( $provider_url, 'facebook.com' ) ) {
$provider_name = 'facebook';
}
$provider_name = apply_filters( 'instant_articles_social_embed_type', $provider_name, $url );
if ( $provider_name ) {
$html = instant_articles_embed_get_html( $provider_name, $html, $url, $attr, $post_id );
}
return $html;
}
add_filter( 'embed_oembed_html', 'instant_articles_embed_oembed_html', 10, 4 );
/**
* Filter the embed results for embeds.
*
* @since 0.1
* @param string $provider_name The name of the embed provider. E.g. “instagram” or “youtube”.
* @param string $html The original HTML returned from the external oembed/embed provider.
* @param string $url The URL found in the content.
* @param mixed $attr An array with extra attributes.
* @param int $post_id The post ID.
* @return string The filtered HTML.
*/
function instant_articles_embed_get_html( $provider_name, $html, $url, $attr, $post_id ) {
// Don't try to fix embeds unless we're in Instant Articles context.
// This prevents mangled output on frontend.
if ( ! is_transforming_instant_article() ) {
return $html;
}
/**
* Filter the HTML that will go into the Instant Article Social Embed markup.
*
* @since 0.1
* @param string $html The HTML.
* @param string $url The URL found in the content.
* @param mixed $attr An array with extra attributes.
* @param int $post_id The post ID.
*/
$html = apply_filters( "instant_articles_social_embed_{$provider_name}", $html, $url, $attr, $post_id );
$html = sprintf( '<div class="embed">%s</div>', $html );
/**
* Filter the Instant Article Social Embed markup.
*
* @since 0.1
* @param string $html The Social Embed markup.
* @param string $url The URL found in the content.
* @param mixed $attr An array with extra attributes.
* @param int $post_id The post ID.
*/
$html = apply_filters( 'instant_articles_social_embed', $html, $url, $attr, $post_id );
return $html;
}