1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
require_once( AMP__ROOT__ . '/includes/embeds/class-amp-base-embed-handler.php' ); |
4
|
|
|
|
5
|
|
|
class AMP_Facebook_Embed_Handler extends AMP_Base_Embed_Handler { |
6
|
|
|
const URL_PATTERN = '#https?://(www\.)?facebook\.com/.*#i'; |
7
|
|
|
|
8
|
|
|
protected $DEFAULT_WIDTH = 600; |
9
|
|
|
protected $DEFAULT_HEIGHT = 400; |
10
|
|
|
|
11
|
|
|
private static $script_slug = 'amp-facebook'; |
12
|
|
|
private static $script_src = 'https://cdn.ampproject.org/v0/amp-facebook-0.1.js'; |
13
|
|
|
|
14
|
|
|
public function register_embed() { |
15
|
|
|
wp_embed_register_handler( 'amp-facebook', self::URL_PATTERN, array( $this, 'oembed' ), -1 ); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function unregister_embed() { |
19
|
|
|
wp_embed_unregister_handler( 'amp-facebook', -1 ); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function get_scripts() { |
23
|
|
|
if ( ! $this->did_convert_elements ) { |
24
|
|
|
return array(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return array( self::$script_slug => self::$script_src ); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function oembed( $matches, $attr, $url, $rawattr ) { |
31
|
|
|
return $this->render( array( 'url' => $url ) ); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function render( $args ) { |
35
|
|
|
$args = wp_parse_args( $args, array( |
36
|
|
|
'url' => false, |
37
|
|
|
) ); |
38
|
|
|
|
39
|
|
|
if ( empty( $args['url'] ) ) { |
40
|
|
|
return ''; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->did_convert_elements = true; |
44
|
|
|
|
45
|
|
|
return AMP_HTML_Utils::build_tag( |
46
|
|
|
'amp-facebook', |
47
|
|
|
array( |
48
|
|
|
'data-href' => $args['url'], |
49
|
|
|
'layout' => 'responsive', |
50
|
|
|
'width' => $this->args['width'], |
51
|
|
|
'height' => $this->args['height'], |
52
|
|
|
) |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|