Completed
Push — fix/responsive-videos-old-embe... ( 045fc8 )
by Jeremy
37:26 queued 28:10
created

responsive-videos.php ➔ jetpack_responsive_videos_maybe_wrap_oembed()   C

Complexity

Conditions 12
Paths 9

Size

Total Lines 75

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
nc 9
nop 4
dl 0
loc 75
rs 6.1186
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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, 4 );
19
	add_filter( 'embed_handler_html', 'jetpack_responsive_videos_maybe_wrap_oembed', 10, 3 );
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
add_action( 'after_setup_theme', 'jetpack_responsive_videos_init', 99 );
28
29
/**
30
 * Adds a wrapper to videos and enqueue script
31
 *
32
 * @return string
33
 */
34
function jetpack_responsive_videos_embed_html( $html ) {
35
	if ( empty( $html ) || ! is_string( $html ) ) {
36
		return $html;
37
	}
38
39
	// The customizer video widget wraps videos with a class of wp-video
40
	// mejs as of 4.9 apparently resizes videos too which causes issues
41
	// skip the video if it is wrapped in wp-video.
42
	$video_widget_wrapper = 'class="wp-video"';
43
44
	$mejs_wrapped = strpos( $html, $video_widget_wrapper );
45
46
	// If this is a video widget wrapped by mejs, return the html.
47
	if ( false !== $mejs_wrapped ) {
48
		return $html;
49
	}
50
51
	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...
52
		wp_enqueue_script( 'jetpack-responsive-videos-script', plugins_url( 'responsive-videos/responsive-videos.js', __FILE__ ), array( 'jquery' ), '1.3', true );
53
	} else {
54
		wp_enqueue_script( 'jetpack-responsive-videos-min-script', plugins_url( 'responsive-videos/responsive-videos.min.js', __FILE__ ), array( 'jquery' ), '1.3', true );
55
	}
56
57
	// Enqueue CSS to ensure compatibility with all themes
58
	wp_enqueue_style( 'jetpack-responsive-videos-style', plugins_url( 'responsive-videos/responsive-videos.css', __FILE__ ) );
59
60
	return '<div class="jetpack-video-wrapper">' . $html . '</div>';
61
}
62
63
/**
64
 * Check if oEmbed is a `$video_patterns` provider video before wrapping.
65
 *
66
 * @param mixed  $html    The cached HTML result, stored in post meta.
67
 * @param string $url     The 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...
68
 * @param array  $attr    An array of shortcode attributes.
69
 * @param int    $post_ID Post ID.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $post_ID not be integer|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...
70
 *
71
 * @return string
72
 */
73
function jetpack_responsive_videos_maybe_wrap_oembed( $html, $url = null, $attr, $post_ID = null ) {
74
	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...
75
		return $html;
76
	}
77
78
	$jetpack_video_wrapper = '<div class="jetpack-video-wrapper">';
79
80
	$already_wrapped = strpos( $html, $jetpack_video_wrapper );
81
82
	// If the oEmbed has already been wrapped, return the html.
83
	if ( false !== $already_wrapped ) {
84
		return $html;
85
	}
86
87
	/**
88
	 * oEmbed Video Providers.
89
	 *
90
	 * A whitelist of oEmbed video provider Regex patterns to check against before wrapping the output.
91
	 *
92
	 * @module theme-tools
93
	 *
94
	 * @since 3.8.0
95
	 *
96
	 * @param array $video_patterns oEmbed video provider Regex patterns.
97
	 */
98
	$video_patterns = apply_filters(
99
		'jetpack_responsive_videos_oembed_videos',
100
		array(
101
			'https?://((m|www)\.)?youtube\.com/watch',
102
			'https?://((m|www)\.)?youtube\.com/playlist',
103
			'https?://youtu\.be/',
104
			'https?://(.+\.)?vimeo\.com/',
105
			'https?://(www\.)?dailymotion\.com/',
106
			'https?://dai.ly/',
107
			'https?://(www\.)?hulu\.com/watch/',
108
			'https?://wordpress.tv/',
109
			'https?://(www\.)?funnyordie\.com/videos/',
110
			'https?://vine.co/v/',
111
			'https?://(www\.)?collegehumor\.com/video/',
112
			'https?://(www\.|embed\.)?ted\.com/talks/',
113
		)
114
	);
115
116
	// Merge patterns to run in a single preg_match call.
117
	$video_patterns = '(' . implode( '|', $video_patterns ) . ')';
118
119
	$is_video = preg_match( $video_patterns, $url );
120
121
	/**
122
	 * Do we have info about the post? Let's check if it has a video block.
123
	 * This is only possible in the block editor.
124
	 */
125
	if (
126
		! empty( $post_ID )
127
		&& function_exists( 'parse_blocks' )
128
	) {
129
		$post_content = get_post_field( 'post_content', $post_ID );
130
		$post_blocks  = parse_blocks( $post_content );
131
		if ( ! empty( $post_blocks ) ) {
132
			foreach ( $post_blocks as $block ) {
133
				// If we have embed blocks, do not apply responsive videos.
134
				if ( false !== strpos( $block['blockName'], 'core-embed' ) ) {
135
					return $html;
136
				}
137
			}
138
		}
139
	}
140
141
	// If the oEmbed is a video, wrap it in the responsive wrapper.
142
	if ( false === $already_wrapped && 1 === $is_video ) {
143
		return jetpack_responsive_videos_embed_html( $html );
144
	}
145
146
	return $html;
147
}
148