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

AMP_Vine_Embed_Handler::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 14

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 1
dl 22
loc 22
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
require_once( AMP__ROOT__ . '/includes/embeds/class-amp-base-embed-handler.php' );
4
5
class AMP_Vine_Embed_Handler extends AMP_Base_Embed_Handler {
6
	const URL_PATTERN = '#https?://vine\.co/v/([^/?]+)#i';
7
8
	protected $DEFAULT_WIDTH = 400;
9
	protected $DEFAULT_HEIGHT = 400;
10
11
	private static $script_slug = 'amp-vine';
12
	private static $script_src = 'https://cdn.ampproject.org/v0/amp-vine-0.1.js';
13
14
	public function register_embed() {
15
		wp_embed_register_handler( 'amp-vine', self::URL_PATTERN, array( $this, 'oembed' ), -1 );
16
	}
17
18
	public function unregister_embed() {
19
		wp_embed_unregister_handler( 'amp-vine', -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, 'vine_id' =>  end( $matches ) ) );
32
	}
33
34 View Code Duplication
	public function render( $args ) {
35
		$args = wp_parse_args( $args, array(
36
			'url' => false,
37
			'vine_id' => false,
38
		) );
39
40
		if ( empty( $args['vine_id'] ) ) {
41
			return AMP_HTML_Utils::build_tag( 'a', array( 'href' => esc_url( $args['url'] ), 'class' => 'amp-wp-embed-fallback' ), esc_html( $args['url'] ) );
42
		}
43
44
		$this->did_convert_elements = true;
45
46
		return AMP_HTML_Utils::build_tag(
47
			'amp-vine',
48
			array(
49
				'data-vineid' => $args['vine_id'],
50
				'layout' => 'responsive',
51
				'width' => $this->args['width'],
52
				'height' => $this->args['height'],
53
			)
54
		);
55
	}
56
}
57