Completed
Push — add/amp-pwa-experiment ( efea12 )
by
unknown
11:53
created

AMP_Facebook_Embed_Handler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A register_embed() 0 3 1
A unregister_embed() 0 3 1
A get_scripts() 0 7 2
A oembed() 0 3 1
A render() 0 21 2
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