Completed
Push — branch-7.9 ( 09f079...e17f2f )
by Jeremy
22:45 queued 15:15
created

flickr.php ➔ flickr_embed_to_shortcode()   D

Complexity

Conditions 19
Paths 4

Size

Total Lines 91

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
nc 4
nop 1
dl 0
loc 91
rs 4.5166
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
			'show_info' => 0,
129
			'w'         => 400,
130
			'h'         => 300,
131
			'secret'    => 0,
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
		if ( ! is_numeric( $src ) && ! preg_match( '~^(https?:)?//([\da-z\-]+\.)*?((static)?flickr\.com|flic\.kr)/.*~i', $src ) ) {
152
			return '';
153
		}
154
155
		if ( preg_match( '!photos/(([0-9a-zA-Z-_]+)|([0-9]+@N[0-9]+))/([0-9]+)/?$!', $src, $m ) ) {
156
			$atts['photo_id'] = $m[4];
157
		} else {
158
			$atts['photo_id'] = $atts['video'];
159
		}
160
161
		if (
162
			! isset( $atts['show_info'] )
163
			|| in_array( $atts['show_info'], array( 'yes', 'true' ), true )
164
		) {
165
			$atts['show_info'] = 'true';
166
		} elseif ( in_array( $atts['show_info'], array( 'false', 'no' ), true ) ) {
167
			$atts['show_info'] = 'false';
168
		}
169
170
		if ( isset( $atts['secret'] ) ) {
171
			$atts['secret'] = preg_replace( '![^\w]+!i', '', $atts['secret'] );
172
		}
173
174
		return flickr_shortcode_video_markup( $atts );
175
	} elseif ( 'photo' === $showing ) {
176
177
		if ( ! preg_match( '~^(https?:)?//([\da-z\-]+\.)*?((static)?flickr\.com|flic\.kr)/.*~i', $src ) ) {
178
			return '';
179
		}
180
181
		$src = sprintf( '%s/player/', untrailingslashit( $src ) );
182
183
		return sprintf( '<iframe src="%s" height="%s" width="%s"  frameborder="0" allowfullscreen webkitallowfullscreen mozallowfullscreen oallowfullscreen msallowfullscreen></iframe>', esc_url( $src ), esc_attr( $atts['h'] ), esc_attr( $atts['w'] ) );
184
	}
185
186
	return false;
187
}
188
189
/**
190
 * Return HTML markup for a Flickr embed.
191
 *
192
 * @param array $atts Shortcode attributes.
193
 */
194
function flickr_shortcode_video_markup( $atts ) {
195
	$atts = array_map( 'esc_attr', $atts );
196
197
	$photo_vars = "photo_id=$atts[photo_id]";
198
	if ( isset( $atts['secret'] ) ) {
199
		$photo_vars .= "&amp;photo_secret=$atts[secret]";
200
	}
201
202
	return <<<EOD
203
<object type="application/x-shockwave-flash" width="$atts[w]" height="$atts[h]" data="https://www.flickr.com/apps/video/stewart.swf?v=1.161" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"> <param name="flashvars" value="$photo_vars&amp;flickr_show_info_box=$atts[show_info]"></param><param name="movie" value="https://www.flickr.com/apps/video/stewart.swf?v=1.161"></param><param name="bgcolor" value="#000000"></param><param name="allowFullScreen" value="true"></param><param name="wmode" value="opaque"></param><embed type="application/x-shockwave-flash" src="https://www.flickr.com/apps/video/stewart.swf?v=1.161" bgcolor="#000000" allowfullscreen="true" flashvars="$photo_vars&amp;flickr_show_info_box=$atts[show_info]" wmode="opaque" height="$atts[h]" width="$atts[w]"></embed></object>
204
EOD;
205
}
206
207
add_shortcode( 'flickr', 'flickr_shortcode_handler' );
208
209
// Override core's Flickr support because Flickr oEmbed doesn't support web embeds.
210
wp_embed_register_handler( 'flickr', '#https?://(www\.)?flickr\.com/.*#i', 'jetpack_flickr_oembed_handler' );
211
212
/**
213
 * Callback to modify output of embedded Vimeo video using Jetpack's shortcode.
214
 *
215
 * @since 3.9
216
 *
217
 * @param array $matches Regex partial matches against the URL passed.
218
 * @param array $attr    Attributes received in embed response.
219
 * @param array $url     Requested URL to be embedded.
220
 *
221
 * @return string Return output of Vimeo shortcode with the proper markup.
222
 */
223
function jetpack_flickr_oembed_handler( $matches, $attr, $url ) {
224
	/*
225
	 * Legacy slideshow embeds end with /show/
226
	 * e.g. http://www.flickr.com/photos/yarnaholic/sets/72157615194738969/show/
227
	 */
228
	if ( '/show/' !== substr( $url, -strlen( '/show/' ) ) ) {
229
		// These lookups need cached, as they don't use WP_Embed (which caches).
230
		$cache_key   = md5( $url . wp_json_encode( $attr ) );
231
		$cache_group = 'oembed_flickr';
232
233
		$html = wp_cache_get( $cache_key, $cache_group );
234
235
		if ( false === $html ) {
236
			$html = _wp_oembed_get_object()->get_html( $url, $attr );
237
238
			wp_cache_set( $cache_key, $html, $cache_group, 60 * MINUTE_IN_SECONDS );
239
		}
240
241
		return $html;
242
	}
243
244
	return flickr_shortcode_handler( array( 'photo' => $url ) );
245
}
246