Completed
Push — fix/responsive-videos-old-embe... ( 045fc8...c4bf35 )
by Jeremy
08:03
created

responsive-videos.php ➔ jetpack_responsive_videos_remove_wrap_oembed()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 2
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Load the Responsive videos plugin
5
 */
6
function jetpack_responsive_videos_init() {
7
8
	/* If the doesn't theme support 'jetpack-responsive-videos', don't continue */
9
	if ( ! current_theme_supports( 'jetpack-responsive-videos' ) ) {
10
		return;
11
	}
12
13
	/* If the theme does support 'jetpack-responsive-videos', wrap the videos */
14
	add_filter( 'wp_video_shortcode', 'jetpack_responsive_videos_embed_html' );
15
	add_filter( 'video_embed_html', 'jetpack_responsive_videos_embed_html' );
16
17
	/* Only wrap oEmbeds if video */
18
	add_filter( 'embed_oembed_html', 'jetpack_responsive_videos_maybe_wrap_oembed', 10, 2 );
19
	add_filter( 'embed_handler_html', 'jetpack_responsive_videos_maybe_wrap_oembed', 10, 2 );
20
21
	/* Wrap videos in Buddypress */
22
	add_filter( 'bp_embed_oembed_html', 'jetpack_responsive_videos_embed_html' );
23
24
	/* Wrap Slideshare shortcodes */
25
	add_filter( 'jetpack_slideshare_shortcode', 'jetpack_responsive_videos_embed_html' );
26
27
	// Remove the Jetpack Responsive video wrapper in embed blocks on sites that support core Responsive embeds.
28
	if ( current_theme_supports( 'responsive-embeds' ) ) {
29
		add_filter( 'render_block', 'jetpack_responsive_videos_remove_wrap_oembed', 10, 2 );
30
	}
31
}
32
add_action( 'after_setup_theme', 'jetpack_responsive_videos_init', 99 );
33
34
35
/**
36
 * Adds a wrapper to videos and enqueue script
37
 *
38
 * @return string
39
 */
40
function jetpack_responsive_videos_embed_html( $html ) {
41
	if ( empty( $html ) || ! is_string( $html ) ) {
42
		return $html;
43
	}
44
45
	// The customizer video widget wraps videos with a class of wp-video
46
	// mejs as of 4.9 apparently resizes videos too which causes issues
47
	// skip the video if it is wrapped in wp-video.
48
	$video_widget_wrapper = 'class="wp-video"';
49
50
	$mejs_wrapped = strpos( $html, $video_widget_wrapper );
51
52
	// If this is a video widget wrapped by mejs, return the html.
53
	if ( false !== $mejs_wrapped ) {
54
		return $html;
55
	}
56
57
	if ( defined( 'SCRIPT_DEBUG' ) && true == SCRIPT_DEBUG ) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
58
		wp_enqueue_script( 'jetpack-responsive-videos-script', plugins_url( 'responsive-videos/responsive-videos.js', __FILE__ ), array( 'jquery' ), '1.3', true );
59
	} else {
60
		wp_enqueue_script( 'jetpack-responsive-videos-min-script', plugins_url( 'responsive-videos/responsive-videos.min.js', __FILE__ ), array( 'jquery' ), '1.3', true );
61
	}
62
63
	// Enqueue CSS to ensure compatibility with all themes
64
	wp_enqueue_style( 'jetpack-responsive-videos-style', plugins_url( 'responsive-videos/responsive-videos.css', __FILE__ ) );
65
66
	return '<div class="jetpack-video-wrapper">' . $html . '</div>';
67
}
68
69
/**
70
 * Check if oEmbed is a `$video_patterns` provider video before wrapping.
71
 *
72
 * @param mixed  $html The cached HTML result, stored in post meta.
73
 * @param string $url  he attempted embed URL.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $url not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
74
 *
75
 * @return string
76
 */
77
function jetpack_responsive_videos_maybe_wrap_oembed( $html, $url = null ) {
78
	if ( empty( $html ) || ! is_string( $html ) || ! $url ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $url of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
79
		return $html;
80
	}
81
82
	$jetpack_video_wrapper = '<div class="jetpack-video-wrapper">';
83
84
	$already_wrapped = strpos( $html, $jetpack_video_wrapper );
85
86
	// If the oEmbed has already been wrapped, return the html.
87
	if ( false !== $already_wrapped ) {
88
		return $html;
89
	}
90
91
	/**
92
	 * oEmbed Video Providers.
93
	 *
94
	 * A whitelist of oEmbed video provider Regex patterns to check against before wrapping the output.
95
	 *
96
	 * @module theme-tools
97
	 *
98
	 * @since 3.8.0
99
	 *
100
	 * @param array $video_patterns oEmbed video provider Regex patterns.
101
	 */
102
	$video_patterns = apply_filters(
103
		'jetpack_responsive_videos_oembed_videos',
104
		array(
105
			'https?://((m|www)\.)?youtube\.com/watch',
106
			'https?://((m|www)\.)?youtube\.com/playlist',
107
			'https?://youtu\.be/',
108
			'https?://(.+\.)?vimeo\.com/',
109
			'https?://(www\.)?dailymotion\.com/',
110
			'https?://dai.ly/',
111
			'https?://(www\.)?hulu\.com/watch/',
112
			'https?://wordpress.tv/',
113
			'https?://(www\.)?funnyordie\.com/videos/',
114
			'https?://vine.co/v/',
115
			'https?://(www\.)?collegehumor\.com/video/',
116
			'https?://(www\.|embed\.)?ted\.com/talks/',
117
		)
118
	);
119
120
	// Merge patterns to run in a single preg_match call.
121
	$video_patterns = '(' . implode( '|', $video_patterns ) . ')';
122
123
	$is_video = preg_match( $video_patterns, $url );
124
125
	// If the oEmbed is a video, wrap it in the responsive wrapper.
126
	if ( false === $already_wrapped && 1 === $is_video ) {
127
		return jetpack_responsive_videos_embed_html( $html );
128
	}
129
130
	return $html;
131
}
132
133
/**
134
 * Remove the Jetpack Responsive video wrapper in embed blocks.
135
 *
136
 * @since 7.0.0
137
 *
138
 * @param string $block_content The block content about to be appended.
139
 * @param array  $block         The full block, including name and attributes.
140
 *
141
 * @return string $block_content String of rendered HTML.
142
 */
143
function jetpack_responsive_videos_remove_wrap_oembed( $block_content, $block ) {
144
	if (
145
		isset( $block['blockName'] )
146
		&& false !== strpos( $block['blockName'], 'core-embed' )
147
	) {
148
		$block_content = preg_replace( '#<div class="jetpack-video-wrapper">(.*?)</div>#', '${1}', $block_content );
149
	}
150
151
	return $block_content;
152
}
153