Completed
Push — fix/flickr-shortcode ( 5e66df...21fefc )
by
unknown
06:50
created

flickr.php ➔ flickr_shortcode_video_markup()   C

Complexity

Conditions 11
Paths 51

Size

Total Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 51
nop 3
dl 0
loc 65
rs 6.6169
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
 * Flickr Short Code
4
 * Author: kellan
5
 * License: BSD/GPL/public domain (take your pick)
6
 *
7
 * [flickr video=http://www.flickr.com/photos/chaddles/2402990826]
8
 * [flickr video=2402990826]
9
 * [flickr video=2402990826 show_info=no]
10
 * [flickr video=2402990826 w=200 h=150]
11
 * [flickr video=2402990826 secret=846d9c1b39]
12
 *
13
 * @package Jetpack
14
 */
15
16
/*
17
 * <object type="application/x-shockwave-flash" width="400" height="300" data="http://www.flickr.com/apps/video/stewart.swf?v=71377" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"> <param name="flashvars" value="intl_lang=en-us&photo_secret=846d9c1be9&photo_id=2345938910"></param> <param name="movie" value="http://www.flickr.com/apps/video/stewart.swf?v=71377"></param> <param name="bgcolor" value="#000000"></param> <param name="allowFullScreen" value="true"></param><embed type="application/x-shockwave-flash" src="http://www.flickr.com/apps/video/stewart.swf?v=71377" bgcolor="#000000" allowfullscreen="true" flashvars="intl_lang=en-us&photo_secret=846d9c1be9&photo_id=2345938910" height="300" width="400"></embed></object>
18
 */
19
20
/**
21
 * Transform embed to shortcode on save.
22
 *
23
 * @param string $content Post content.
24
 */
25
function flickr_embed_to_shortcode( $content ) {
26
	if ( ! is_string( $content ) || false === stripos( $content, '/www.flickr.com/apps/video/stewart.swf' ) ) {
27
		return $content;
28
	}
29
30
	$regexp     = '%(<object.*?(?:<(?!/?(?:object|embed)\s+).*?)*?)?<embed((?:\s+\w+="[^"]*")*)\s+src="http(?:\:|&#0*58;)//www.flickr.com/apps/video/stewart.swf[^"]*"((?:\s+\w+="[^"]*")*)\s*(?:/>|>\s*</embed>)(?(1)\s*</object>)%';
31
	$regexp_ent = str_replace(
32
		array(
33
			'&amp;#0*58;',
34
			'[^&gt;]*',
35
			'[^&lt;]*',
36
		),
37
		array(
38
			'&amp;#0*58;|&#0*58;',
39
			'[^&]*(?:&(?!gt;)[^&]*)*',
40
			'[^&]*(?:&(?!lt;)[^&]*)*',
41
		),
42
		htmlspecialchars( $regexp, ENT_NOQUOTES )
43
	);
44
45
	foreach ( compact( 'regexp', 'regexp_ent' ) as $reg => $regexp ) {
46
		if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) {
47
			continue;
48
		}
49
		foreach ( $matches as $match ) {
50
			$params = $match[2] . $match[3];
51
52
			if ( 'regexp_ent' === $reg ) {
53
				$params = html_entity_decode( $params );
54
			}
55
56
			$params = wp_kses_hair( $params, array( 'http' ) );
57
			if (
58
				! isset( $params['type'] )
59
				|| 'application/x-shockwave-flash' !== $params['type']['value']
60
				|| ! isset( $params['flashvars'] )
61
			) {
62
				continue;
63
			}
64
65
			$flashvars = array();
66
			wp_parse_str( html_entity_decode( $params['flashvars']['value'] ), $flashvars );
67
68
			if ( ! isset( $flashvars['photo_id'] ) ) {
69
				continue;
70
			}
71
72
			$photo_id = preg_replace( '#[^A-Za-z0-9_./@+-]+#', '', $flashvars['photo_id'] );
73
74
			if ( ! strlen( $photo_id ) ) {
75
				continue;
76
			}
77
78
			$code_atts = array( 'video' => $photo_id );
79
80
			if (
81
				isset( $flashvars['flickr_show_info_box'] )
82
				&& 'true' === $flashvars['flickr_show_info_box']
83
			) {
84
				$code_atts['show_info'] = 'true';
85
			}
86
87
			if ( ! empty( $flashvars['photo_secret'] ) ) {
88
				$photo_secret = preg_replace( '#[^A-Za-z0-9_./@+-]+#', '', $flashvars['photo_secret'] );
89
				if ( strlen( $photo_secret ) ) {
90
					$code_atts['secret'] = $photo_secret;
91
				}
92
			}
93
94
			if ( ! empty( $params['width']['value'] ) ) {
95
				$code_atts['w'] = (int) $params['width']['value'];
96
			}
97
98
			if ( ! empty( $params['height']['value'] ) ) {
99
				$code_atts['h'] = (int) $params['height']['value'];
100
			}
101
102
			$code = '[flickr';
103
			foreach ( $code_atts as $k => $v ) {
104
				$code .= " $k=$v";
105
			}
106
			$code .= ']';
107
108
			$content = str_replace( $match[0], $code, $content );
109
			/** This action is documented in modules/shortcodes/youtube.php */
110
			do_action( 'jetpack_embed_to_shortcode', 'flickr_video', $flashvars['photo_id'] );
111
		}
112
	}
113
114
	return $content;
115
}
116
add_filter( 'pre_kses', 'flickr_embed_to_shortcode' );
117
118
/**
119
 * Flickr Shortcode handler.
120
 *
121
 * @param array $atts Shortcode attributes.
122
 */
123
function flickr_shortcode_handler( $atts ) {
124
	$atts = shortcode_atts(
125
		array(
126
			'video'    => 0,
127
			'photo'    => 0,
128
			'w'        => '',
129
			'h'        => '',
130
			'controls' => 'yes',
131
			'autoplay' => '',
132
		),
133
		$atts,
134
		'flickr'
135
	);
136
137
	if ( ! empty( $atts['video'] ) ) {
138
		$showing = 'video';
139
		$src     = $atts['video'];
140
	} elseif ( ! empty( $atts['photo'] ) ) {
141
		$showing = 'photo';
142
		$src     = $atts['photo'];
143
	} else {
144
		return '';
145
	}
146
147
	$src = str_replace( 'http://', 'https://', $src );
148
149
	if ( 'video' === $showing ) {
150
151
		$video_id = flick_shortcode_video_id( $src );
152
153
		if ( empty( $video_id ) ) {
154
			return '';
155
		}
156
157
		$atts = array_map( 'esc_attr', $atts );
158
		return flickr_shortcode_video_markup( $atts, $video_id, $src );
159
	} elseif ( 'photo' === $showing ) {
160
161
		if ( ! preg_match( '~^(https?:)?//([\da-z\-]+\.)*?((static)?flickr\.com|flic\.kr)/.*~i', $src ) ) {
162
			return '';
163
		}
164
165
		$src = sprintf( '%s/player/', untrailingslashit( $src ) );
166
167
		$allow_full_screen = 'allowfullscreen webkitallowfullscreen mozallowfullscreen oallowfullscreen msallowfullscreen';
168
169
		if ( Jetpack_AMP_Support::is_amp_request() ) {
170
			$allow_full_screen = '';
171
		}
172
173
		return sprintf( '<iframe src="%s" height="%s" width="%s"  frameborder="0" %s></iframe>', esc_url( $src ), esc_attr( $atts['h'] ), esc_attr( $atts['w'] ), $allow_full_screen );
174
	}
175
176
	return false;
177
}
178
179
/**
180
 * Return HTML markup for a Flickr embed.
181
 *
182
 * @param array  $atts Shortcode attributes.
183
 * @param string $id Video ID.
184
 * @param string $video_param video param of the shortcode.
185
 */
186
function flickr_shortcode_video_markup( $atts, $id, $video_param ) {
187
188
	$transient_name = "flickr_video_$id";
189
	$video_src      = get_transient( $transient_name );
190
191
	if ( empty( $video_src ) ) {
192
		$video_url = '';
0 ignored issues
show
Unused Code introduced by
$video_url 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...
193
		if ( ! is_numeric( $video_param ) ) {
194
			$video_url = $video_param;
195
		} else {
196
			// Get the URL of the video from the page of the video.
197
			$video_page_content = wp_remote_get( "http://flickr.com/photo.gne?id=$video_param" );
198
199
			// Extract the URL from the og:url meta tag.
200
			preg_match( '/property=\"og:url\"\scontent=\"([^\"]+)\"/', $video_page_content['body'], $matches );
201
			if ( empty( $matches[1] ) ) {
202
				return '';
203
			}
204
			$video_url = $matches[1];
205
		}
206
207
		$provider = 'https://www.flickr.com/services/oembed/';
208
		$oembed   = _wp_oembed_get_object();
209
		$data     = (array) $oembed->fetch( $provider, $video_url );
210
		if ( empty( $data['html'] ) ) {
211
			return '';
212
		}
213
214
		// Get the embed url.
215
		preg_match( '/src=\"([^\"]+)\"/', $data['html'], $matches );
216
217
		$embed_url = $matches[1];
218
219
		$embed_page = wp_remote_get( $embed_url );
220
221
		// Get the video url from embed html markup.
222
223
		preg_match( '/video.+src=\"([^\"]+)\"/', $embed_page['body'], $matches );
224
225
		$video_src = $matches[1];
226
227
		set_transient( $transient_name, $video_src, 2592000 ); // 30 days transient.
228
	}
229
230
	$style = 'max-width: 100%;';
231
232
	if ( ! empty( $atts['w'] ) && is_numeric( $atts['w'] ) ) {
233
		$style .= sprintf( 'width: %dpx;', $atts['w'] );
234
	}
235
236
	if ( ! empty( $atts['h'] ) && is_numeric( $atts['h'] ) ) {
237
		$style .= sprintf( 'height: %dpx;', $atts['h'] );
238
	}
239
240
	$controls = 'yes' === $atts['controls'] ? 'controls' : '';
241
	$autoplay = 'yes' === $atts['autoplay'] ? 'autoplay' : '';
242
243
	return sprintf(
244
		'<div class="flick_video" style="%s"><video src="%s" %s %s /></div>',
245
		$style,
246
		$video_src,
247
		$controls,
248
		$autoplay
249
	);
250
}
251
252
/**
253
 * Extract the id of the flickr video from the video param.
254
 *
255
 * @param string $video_param Video parameter of the shortcode.
256
 */
257
function flick_shortcode_video_id( $video_param ) {
258
	if ( preg_match( '/^https?:\/\/(www\.)?flickr\.com\/.+/', $video_param ) || preg_match( '/^https?:\/\/flic\.kr\/.+/', $video_param ) ) {
259
260
		// Extract the video id from the url.
261
		preg_match( '/\d+/', $video_param, $matches );
262
263
		if ( empty( $matches ) ) {
264
			return false;
265
		}
266
267
		return $matches[0];
268
269
	} elseif ( is_numeric( $video_param ) ) {
270
		return $video_param;
271
	}
272
273
	return false;
274
}
275
276
add_shortcode( 'flickr', 'flickr_shortcode_handler' );
277
278
// Override core's Flickr support because Flickr oEmbed doesn't support web embeds.
279
wp_embed_register_handler( 'flickr', '#https?://(www\.)?flickr\.com/.*#i', 'jetpack_flickr_oembed_handler' );
280
281
/**
282
 * Callback to modify output of embedded Vimeo video using Jetpack's shortcode.
283
 *
284
 * @since 3.9
285
 *
286
 * @param array $matches Regex partial matches against the URL passed.
287
 * @param array $attr    Attributes received in embed response.
288
 * @param array $url     Requested URL to be embedded.
289
 *
290
 * @return string Return output of Vimeo shortcode with the proper markup.
291
 */
292
function jetpack_flickr_oembed_handler( $matches, $attr, $url ) {
293
	/*
294
	 * Legacy slideshow embeds end with /show/
295
	 * e.g. http://www.flickr.com/photos/yarnaholic/sets/72157615194738969/show/
296
	 */
297
	if ( '/show/' !== substr( $url, -strlen( '/show/' ) ) ) {
298
		// These lookups need cached, as they don't use WP_Embed (which caches).
299
		$cache_key   = md5( $url . wp_json_encode( $attr ) );
300
		$cache_group = 'oembed_flickr';
301
302
		$html = wp_cache_get( $cache_key, $cache_group );
303
304
		if ( false === $html ) {
305
			$html = _wp_oembed_get_object()->get_html( $url, $attr );
306
307
			wp_cache_set( $cache_key, $html, $cache_group, 60 * MINUTE_IN_SECONDS );
308
		}
309
310
		return $html;
311
	}
312
313
	return flickr_shortcode_handler( array( 'photo' => $url ) );
314
}
315