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

AMP_YouTube_Embed_Handler::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
require_once( AMP__ROOT__ . '/includes/embeds/class-amp-base-embed-handler.php' );
4
5
// Much of this class is borrowed from Jetpack embeds
6
class AMP_YouTube_Embed_Handler extends AMP_Base_Embed_Handler {
7
	const SHORT_URL_HOST = 'youtu.be';
8
	// Only handling single videos. Playlists are handled elsewhere.
9
	const URL_PATTERN = '#https?://(?:www\.)?(?:youtube.com/(?:v/|e/|embed/|watch[/\#?])|youtu\.be/).*#i';
10
	const RATIO = 0.5625;
11
12
	protected $DEFAULT_WIDTH = 600;
13
	protected $DEFAULT_HEIGHT = 338;
14
15
	private static $script_slug = 'amp-youtube';
16
	private static $script_src = 'https://cdn.ampproject.org/v0/amp-youtube-0.1.js';
17
18
	function __construct( $args = array() ) {
19
		parent::__construct( $args );
20
21
		if ( isset( $this->args['content_max_width'] ) ) {
22
			$max_width = $this->args['content_max_width'];
23
			$this->args['width'] = $max_width;
24
			$this->args['height'] = round( $max_width * self::RATIO );
25
		}
26
	}
27
28
	function register_embed() {
29
		wp_embed_register_handler( 'amp-youtube', self::URL_PATTERN, array( $this, 'oembed' ), -1 );
30
		add_shortcode( 'youtube', array( $this, 'shortcode' ) );
31
	}
32
33
	public function unregister_embed() {
34
		wp_embed_unregister_handler( 'amp-youtube', -1 );
35
		remove_shortcode( 'youtube' );
36
	}
37
38
	public function get_scripts() {
39
		if ( ! $this->did_convert_elements ) {
40
			return array();
41
		}
42
43
		return array( self::$script_slug => self::$script_src );
44
	}
45
46
	public function shortcode( $attr ) {
47
		$url = false;
48
		$video_id = false;
0 ignored issues
show
Unused Code introduced by
$video_id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
49
		if ( isset( $attr[0] ) ) {
50
			$url = ltrim( $attr[0] , '=' );
51
		} elseif ( function_exists ( 'shortcode_new_to_old_params' ) ) {
52
			$url = shortcode_new_to_old_params( $attr );
53
		}
54
55
		if ( empty( $url ) ) {
56
			return '';
57
		}
58
59
		$video_id = $this->get_video_id_from_url( $url );
60
61
		return $this->render( array(
62
			'url' => $url,
63
			'video_id' => $video_id,
64
		) );
65
	}
66
67
	public function oembed( $matches, $attr, $url, $rawattr ) {
68
		return $this->shortcode( array( $url ) );
69
	}
70
71
	public function render( $args ) {
72
		$args = wp_parse_args( $args, array(
73
			'video_id' => false,
74
		) );
75
76
		if ( empty( $args['video_id'] ) ) {
77
			return AMP_HTML_Utils::build_tag( 'a', array( 'href' => esc_url( $args['url'] ), 'class' => 'amp-wp-embed-fallback' ), esc_html( $args['url'] ) );
78
		}
79
80
		$this->did_convert_elements = true;
81
82
		return AMP_HTML_Utils::build_tag(
83
			'amp-youtube',
84
			array(
85
				'data-videoid' => $args['video_id'],
86
				'layout' => 'responsive',
87
				'width' => $this->args['width'],
88
				'height' => $this->args['height'],
89
			)
90
		);
91
	}
92
93
	private function get_video_id_from_url( $url ) {
94
		$video_id = false;
95
		$parsed_url = parse_url( $url );
96
97
		if ( self::SHORT_URL_HOST === substr( $parsed_url['host'], -strlen( self::SHORT_URL_HOST ) ) ) {
98
			// youtu.be/{id}
99
			$parts = explode( '/', $parsed_url['path'] );
100
			if ( ! empty( $parts ) ) {
101
				$video_id = $parts[1];
102
			}
103
		} else {
104
			// ?v={id} or ?list={id}
105
			parse_str( $parsed_url['query'], $query_args );
106
107
			if ( isset( $query_args['v'] ) ) {
108
				$video_id = $this->sanitize_v_arg( $query_args['v'] );
109
			}
110
		}
111
112
		if ( empty( $video_id ) ) {
113
			// /(v|e|embed)/{id}
114
			$parts = explode( '/', $parsed_url['path'] );
115
116
			if ( in_array( $parts[1], array( 'v', 'e', 'embed' ) ) ) {
117
				$video_id = $parts[2];
118
			}
119
		}
120
121
		return $video_id;
122
	}
123
124
	private function sanitize_v_arg( $value ) {
125
		// Deal with broken params like `?v=123?rel=0`
126
		if ( false !== strpos( $value, '?' ) ) {
127
			$value = strtok( $value, '?' );
128
		}
129
130
		return $value;
131
	}
132
}
133