Completed
Push — ignore/lazy-images-linting-pac... ( 3c044f...b5c515 )
by Jeremy
367:06 queued 352:42
created

dailymotion.php ➔ dailymotion_shortcode()   F

Complexity

Conditions 41
Paths > 20000

Size

Total Lines 152

Duplication

Lines 15
Ratio 9.87 %

Importance

Changes 0
Metric Value
cc 41
nc 204842
nop 1
dl 15
loc 152
rs 0
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
 * Dailymotion code
4
 *
5
 * @package Jetpack
6
 */
7
8
/**
9
 * Original codes:
10
 *
11
 * <embed height="270" type="application/x-shockwave-flash" width="480" src="http&#58;//www.dailymotion.com/swf/video/xekmrq?additionalInfos=0" wmode="opaque" pluginspage="http&#58;//www.macromedia.com/go/getflashplayer" allowscriptaccess="never" allownetworking="internal" />
12
 *
13
 * <object width="480" height="240"><param name="movie" value="http://www.dailymotion.com/swf/video/xen4ms_ghinzu-cold-love-mirror-mirror_music?additionalInfos=0"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param>
14
 *  <embed type="application/x-shockwave-flash" src="http://www.dailymotion.com/swf/video/xen4ms_ghinzu-cold-love-mirror-mirror_music?additionalInfos=0" width="480" height="240" allowfullscreen="true" allowscriptaccess="always"></embed>
15
 * </object><br /><b><a href="http://www.dailymotion.com/video/xen4ms_ghinzu-cold-love-mirror-mirror_music">Ghinzu - Cold Love (Mirror Mirror)</a></b><br /><i>Uploaded by <a href="http://www.dailymotion.com/GhinzuTV">GhinzuTV</a>. - <a href="http://www.dailymotion.com/us/channel/music">Watch more music videos, in HD!</a></i>
16
 *
17
 * Code as of 01.01.11:
18
 * <object width="560" height="421"><param name="movie" value="http://www.dailymotion.com/swf/video/xaose5?width=560&theme=denim&foreground=%2392ADE0&highlight=%23A2ACBF&background=%23202226&start=&animatedTitle=&iframe=0&additionalInfos=0&autoPlay=0&hideInfos=0"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed type="application/x-shockwave-flash" src="http://www.dailymotion.com/swf/video/xaose5?width=560&theme=denim&foreground=%2392ADE0&highlight=%23A2ACBF&background=%23202226&start=&animatedTitle=&iframe=0&additionalInfos=0&autoPlay=0&hideInfos=0" width="560" height="421" allowfullscreen="true" allowscriptaccess="always"></embed></object><br /><b><a href="http://www.dailymotion.com/video/x29zm17_funny-videos-of-cats-and-babies-compilation-2015_fun">Funny cats and babies!</a></b><br /><i>Uploaded by <a href="http://www.dailymotion.com/GilLavie">GilLavie</a>. - <a target="_self" href="http://www.dailymotion.com/channel/funny/featured/1">Find more funny videos.</a></i>
19
 * movie param enforces anti-xss protection
20
 *
21
 * Scroll down for the new <iframe> embed code handler.
22
 *
23
 * @param string $content Post content.
24
 */
25
function dailymotion_embed_to_shortcode( $content ) {
26
	if ( ! is_string( $content ) || false === stripos( $content, 'www.dailymotion.com/swf/' ) ) {
27
		return $content;
28
	}
29
30
	$regexp     = '!<object.*>\s*(<param.*></param>\s*)*<embed((?:\s+\w+="[^"]*")*)\s+src="http(?:\:|&#0*58;)//(www\.dailymotion\.com/swf/[^"]*)"((?:\s+\w+="[^"]*")*)\s*(?:/>|>\s*</embed>)\s*</object><br /><b><a .*>.*</a></b><br /><i>.*</i>!';
31
	$regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $regexp, ENT_NOQUOTES ) );
32
33
	foreach ( compact( 'regexp', 'regexp_ent' ) as $reg => $regexp ) {
34
		if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) {
35
			continue;
36
		}
37
38
		foreach ( $matches as $match ) {
39
			$src    = html_entity_decode( $match[3] );
40
			$params = $match[2] . $match[4];
41
42
			if ( 'regexp_ent' === $reg ) {
43
				$src    = html_entity_decode( $src );
44
				$params = html_entity_decode( $params );
45
			}
46
47
			$params = wp_kses_hair( $params, array( 'http' ) );
48
49
			if ( ! isset( $params['type'] ) || 'application/x-shockwave-flash' !== $params['type']['value'] ) {
50
				continue;
51
			}
52
53
			$id = basename( substr( $src, strlen( 'www.dailymotion.com/swf' ) ) );
54
			$id = preg_replace( '/[^a-z0-9].*$/is', '', $id );
55
56
			$content = str_replace( $match[0], "[dailymotion id=$id]", $content );
57
			/** This action is documented in modules/shortcodes/youtube.php */
58
			do_action( 'jetpack_embed_to_shortcode', 'dailymotion', $id );
59
		}
60
	}
61
	return $content;
62
}
63
add_filter( 'pre_kses', 'dailymotion_embed_to_shortcode' );
64
65
/**
66
 * DailyMotion shortcode
67
 *
68
 * The documented shortcode is:
69
 * [dailymotion id=x8oma9]
70
 *
71
 * Possibilities, according to the old parsing regexp:
72
 * [dailymotion x8oma9]
73
 * [dailymotion=x8oma9]
74
 *
75
 * Hypothetical option, according to the old shortcode function is
76
 * [dailymotion id=1&title=2&user=3&video=4]
77
 *
78
 * The new style is now:
79
 * [dailymotion id=x8oma9 title=2 user=3 video=4]
80
 *
81
 * Supported parameters for player customization: width, height,
82
 * autoplay, endscreen-enable, mute, sharing-enabled, start, subtitles-default,
83
 * ui-highlight, ui-logo, ui-start-screen-info, ui-theme
84
 * see https://developer.dailymotion.com/player#player-parameters
85
 *
86
 * @todo: Update code to sniff for iframe embeds and convert those to shortcodes.
87
 *
88
 * @param array $atts Shortcode attributes.
89
 *
90
 * @return string html
91
 */
92
function dailymotion_shortcode( $atts ) {
93
	global $content_width;
94
95
	if ( isset( $atts[0] ) ) {
96
		$id         = ltrim( $atts[0], '=' );
97
		$atts['id'] = $id;
98
99
	} else {
100
		$params = shortcode_new_to_old_params( $atts );
101
		parse_str( $params, $atts_new );
102
103
		foreach ( $atts_new as $k => $v ) {
0 ignored issues
show
Bug introduced by
The expression $atts_new of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
104
			$atts[ $k ] = $v;
105
		}
106
	}
107
108
	$atts = shortcode_atts(
109
		array(
110
			'id'                   => '', // string.
111
			'width'                => '', // int.
112
			'height'               => '', // int.
113
			'title'                => '', // string.
114
			'user'                 => '', // string.
115
			'video'                => '', // string.
116
			'autoplay'             => 0,  // int.
117
			'endscreen-enable'     => 1,  // int.
118
			'mute'                 => 0,  // int.
119
			'sharing-enable'       => 1,  // int.
120
			'start'                => '', // int.
121
			'subtitles-default'    => '', // string.
122
			'ui-highlight'         => '', // string.
123
			'ui-logo'              => 1,  // int.
124
			'ui-start-screen-info' => 0,  // int.
125
			'ui-theme'             => '', // string.
126
		),
127
		$atts,
128
		'dailymotion'
129
	);
130
131
	if ( isset( $atts['id'] ) && ! empty( $atts['id'] ) ) {
132
		$id = rawurlencode( $atts['id'] );
133
	} else {
134
		return '<!--Dailymotion error: bad or missing ID-->';
135
	}
136
137
	/*set width and height using provided parameters if any */
138
	$width  = isset( $atts['width'] ) ? (int) $atts['width'] : 0;
139
	$height = isset( $atts['height'] ) ? (int) $atts['height'] : 0;
140
141
	if ( ! $width && ! $height ) {
142
		if ( ! empty( $content_width ) ) {
143
			$width = absint( $content_width );
144
		} else {
145
			$width = 425;
146
		}
147
		$height = $width / 425 * 334;
148
	} elseif ( ! $height ) {
149
		$height = $width / 425 * 334;
150
	} elseif ( ! $width ) {
151
		$width = $height / 334 * 425;
152
	}
153
154
	if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
155
		return sprintf(
156
			'<amp-dailymotion data-videoid="%1$s" layout="responsive" width="%2$d" height="%3$d"></amp-dailymotion>',
157
			esc_attr( $id ),
158
			absint( $width ),
159
			absint( $height )
160
		);
161
	}
162
163
	/**
164
	 * Let's add parameters if needed.
165
	 *
166
	 * @see https://developer.dailymotion.com/player
167
	 */
168
	$player_params = array();
169
170 View Code Duplication
	if ( isset( $atts['autoplay'] ) && '1' === $atts['autoplay'] ) {
171
		$player_params['autoplay'] = '1';
172
	}
173
	if ( isset( $atts['endscreen-enable'] ) && '0' === $atts['endscreen-enable'] ) {
174
		$player_params['endscreen-enable'] = '0';
175
	}
176 View Code Duplication
	if ( isset( $atts['mute'] ) && '1' === $atts['mute'] ) {
177
		$player_params['mute'] = '1';
178
	}
179
	if ( isset( $atts['sharing-enable'] ) && '0' === $atts['sharing-enable'] ) {
180
		$player_params['sharing-enable'] = '0';
181
	}
182
	if ( isset( $atts['start'] ) && ! empty( $atts['start'] ) ) {
183
		$player_params['start'] = abs( (int) $atts['start'] );
184
	}
185 View Code Duplication
	if ( isset( $atts['subtitles-default'] ) && ! empty( $atts['subtitles-default'] ) ) {
186
		$player_params['subtitles-default'] = esc_attr( $atts['subtitles-default'] );
187
	}
188 View Code Duplication
	if ( isset( $atts['ui-highlight'] ) && ! empty( $atts['ui-highlight'] ) ) {
189
		$player_params['ui-highlight'] = esc_attr( $atts['ui-highlight'] );
190
	}
191
	if ( isset( $atts['ui-logo'] ) && '0' === $atts['ui-logo'] ) {
192
		$player_params['ui-logo'] = '0';
193
	}
194
	if ( isset( $atts['ui-start-screen-info'] ) && '0' === $atts['ui-start-screen-info'] ) {
195
		$player_params['ui-start-screen-info'] = '0';
196
	}
197 View Code Duplication
	if ( isset( $atts['ui-theme'] ) && in_array( strtolower( $atts['ui-theme'] ), array( 'dark', 'light' ), true ) ) {
198
		$player_params['ui-theme'] = esc_attr( $atts['ui-theme'] );
199
	}
200
201
	// Add those parameters to the Video URL.
202
	$video_url = add_query_arg(
203
		$player_params,
204
		'https://www.dailymotion.com/embed/video/' . $id
205
	);
206
207
	$output = '';
208
209
	if ( preg_match( '/^[A-Za-z0-9]+$/', $id ) ) {
210
		$output .= '<iframe width="' . esc_attr( $width ) . '" height="' . esc_attr( $height ) . '" src="' . esc_url( $video_url ) . '" style="border:0;" allowfullscreen></iframe>';
211
212
		$video = preg_replace( '/[^-a-z0-9_]/i', '', $atts['video'] );
213
		$title = wp_kses( $atts['title'], array() );
214
		if (
215
			array_key_exists( 'video', $atts )
216
			&& $video
217
			&& array_key_exists( 'title', $atts )
218
			&& $title
219
		) {
220
			$output .= '<br /><strong><a href="' . esc_url( 'https://www.dailymotion.com/video/' . $video ) . '" target="_blank">' . esc_html( $title ) . '</a></strong>';
221
		}
222
223
		$user = preg_replace( '/[^-a-z0-9_]/i', '', $atts['user'] );
224
		if ( array_key_exists( 'user', $atts ) && $user ) {
225
			/* translators: %s is a Dailymotion user name */
226
			$output .= '<br /><em>' . wp_kses(
227
				sprintf(
228
					/* Translators: placeholder is a Dailymotion username, linking to a Dailymotion profile page. */
229
					__( 'Uploaded by %s', 'jetpack' ),
230
					'<a href="' . esc_url( 'https://www.dailymotion.com/' . $user ) . '" target="_blank">' . esc_html( $user ) . '</a>'
231
				),
232
				array(
233
					'a' => array(
234
						'href'   => true,
235
						'target' => true,
236
					),
237
				)
238
			) . '</em>';
239
		}
240
	}
241
242
	return $output;
243
}
244
add_shortcode( 'dailymotion', 'dailymotion_shortcode' );
245
246
/**
247
 * DailyMotion Channel Shortcode
248
 *
249
 * Examples:
250
 * [dailymotion-channel user=MatthewDominick]
251
 * [dailymotion-channel user=MatthewDominick type=grid] (supports grid, carousel, badge/default)
252
 *
253
 * @param array $atts Shortcode attributes.
254
 */
255
function dailymotion_channel_shortcode( $atts ) {
256
	$username = $atts['user'];
257
258
	switch ( $atts['type'] ) {
259 View Code Duplication
		case 'grid':
260
			$channel_iframe = '<iframe sandbox="allow-popups allow-scripts allow-same-origin allow-presentation" width="300px" height="264px" scrolling="no" style="border:0;" src="' . esc_url( '//www.dailymotion.com/badge/user/' . $username . '?type=grid' ) . '"></iframe>';
261
			break;
262 View Code Duplication
		case 'carousel':
263
			$channel_iframe = '<iframe sandbox="allow-popups allow-scripts allow-same-origin allow-presentation" width="300px" height="360px" scrolling="no" style="border:0;" src="' . esc_url( '//www.dailymotion.com/badge/user/' . $username . '?type=carousel' ) . '"></iframe>';
264
			break;
265
		default:
266
			$channel_iframe = '<iframe sandbox="allow-popups allow-scripts allow-same-origin allow-presentation" width="300px" height="78px" scrolling="no" style="border:0;" src="' . esc_url( '//www.dailymotion.com/badge/user/' . $username ) . '"></iframe>';
267
	}
268
269
	return $channel_iframe;
270
}
271
add_shortcode( 'dailymotion-channel', 'dailymotion_channel_shortcode' );
272
273
/**
274
 * Embed Reversal for Badge/Channel
275
 *
276
 * @param string $content Post content.
277
 */
278
function dailymotion_channel_reversal( $content ) {
279
	if ( ! is_string( $content ) || false === stripos( $content, 'dailymotion.com/badge/' ) ) {
280
		return $content;
281
	}
282
283
	/*
284
	 * Sample embed code:
285
	 * <iframe width="300px" height="360px" scrolling="no" frameborder="0" src="http://www.dailymotion.com/badge/user/Dailymotion?type=carousel"></iframe>
286
	*/
287
288
	$regexes = array();
289
290
	$regexes[] = '#<iframe[^>]+?src=" (?:https?:)?//(?:www\.)?dailymotion\.com/badge/user/([^"\'/]++) "[^>]*+></iframe>#ix';
291
292
	// Let's play nice with the visual editor too.
293
	$regexes[] = '#&lt;iframe(?:[^&]|&(?!gt;))+?src=" (?:https?:)?//(?:www\.)?dailymotion\.com/badge/user/([^"\'/]++) "(?:[^&]|&(?!gt;))*+&gt;&lt;/iframe&gt;#ix';
294
295
	foreach ( $regexes as $regex ) {
296
		if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) {
297
			continue;
298
		}
299
300
		foreach ( $matches as $match ) {
301
			$url_pieces = wp_parse_url( $match[1] );
302
303
			if ( 'type=carousel' === $url_pieces['query'] ) {
304
				$type = 'carousel';
305
			} elseif ( 'type=grid' === $url_pieces['query'] ) {
306
				$type = 'grid';
307
			} else {
308
				$type = 'badge';
309
			}
310
311
			$shortcode     = '[dailymotion-channel user=' . esc_attr( $url_pieces['path'] ) . ' type=' . esc_attr( $type ) . ']';
312
			$replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $match[0], '#' ) );
313
			$content       = preg_replace( $replace_regex, sprintf( "\n\n%s\n\n", $shortcode ), $content );
314
		}
315
	}
316
317
	return $content;
318
}
319
add_filter( 'pre_kses', 'dailymotion_channel_reversal' );
320
321
/**
322
 * Dailymotion Embed Reversal (with new iframe code as of 17.09.2014)
323
 *
324
 * Converts a generic HTML embed code from Dailymotion into an
325
 * oEmbeddable URL.
326
 *
327
 * @param string $content Post content.
328
 */
329
function jetpack_dailymotion_embed_reversal( $content ) {
330
	if ( ! is_string( $content ) || false === stripos( $content, 'dailymotion.com/embed' ) ) {
331
		return $content;
332
	}
333
334
	/*
335
	 * Sample embed code as of Sep 17th 2014:
336
	 * <iframe frameborder="0" width="480" height="270" src="//www.dailymotion.com/embed/video/x25x71x" allowfullscreen></iframe><br /><a href="http://www.dailymotion.com/video/x25x71x_dog-with-legs-in-casts-learns-how-to-enter-the-front-door_animals" target="_blank">Dog with legs in casts learns how to enter the...</a> <i>by <a href="http://www.dailymotion.com/videobash" target="_blank">videobash</a></i>
337
	*/
338
	$regexes = array();
339
340
	// I'm Konstantin and I love regex.
341
	$regexes[] = '#<iframe[^>]+?src=" (?:https?:)?//(?:www\.)?dailymotion\.com/embed/video/([^"\'/]++) "[^>]*+>\s*+</iframe>\s*+(?:<br\s*+/>)?\s*+
342
	(?: <a[^>]+?href=" (?:https?:)?//(?:www\.)?dailymotion\.com/[^"\']++ "[^>]*+>.+?</a>\s*+ )?
343
	(?: <i>.*?<a[^>]+?href=" (?:https?:)?//(?:www\.)?dailymotion\.com/[^"\']++ "[^>]*+>.+?</a>\s*+</i> )?#ix';
344
345
	$regexes[] = '#&lt;iframe(?:[^&]|&(?!gt;))+?src=" (?:https?:)?//(?:www\.)?dailymotion\.com/embed/video/([^"\'/]++) "(?:[^&]|&(?!gt;))*+&gt;\s*+&lt;/iframe&gt;\s*+(?:&lt;br\s*+/&gt;)?\s*+
346
	(?: &lt;a(?:[^&]|&(?!gt;))+?href=" (?:https?:)?//(?:www\.)?dailymotion\.com/[^"\']++ "(?:[^&]|&(?!gt;))*+&gt;.+?&lt;/a&gt;\s*+ )?
347
	(?: &lt;i&gt;.*?&lt;a(?:[^&]|&(?!gt;))+?href=" (?:https?:)?//(?:www\.)?dailymotion\.com/[^"\']++ "(?:[^&]|&(?!gt;))*+&gt;.+?&lt;/a&gt;\s*+&lt;/i&gt; )?#ix';
348
349 View Code Duplication
	foreach ( $regexes as $regex ) {
350
		if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) {
351
			continue;
352
		}
353
354
		foreach ( $matches as $match ) {
355
			$url           = esc_url( sprintf( 'https://dailymotion.com/video/%s', $match[1] ) );
356
			$replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $match[0], '#' ) );
357
			$content       = preg_replace( $replace_regex, sprintf( "\n\n%s\n\n", $url ), $content );
358
359
			/** This action is documented in modules/shortcodes/youtube.php */
360
			do_action( 'jetpack_embed_to_shortcode', 'dailymotion', $url );
361
		}
362
	}
363
364
	return $content;
365
}
366
add_filter( 'pre_kses', 'jetpack_dailymotion_embed_reversal' );
367