Completed
Push — fix/flickr-shortcode ( b107b8...d67fa5 )
by
unknown
08:02
created

flickr.php ➔ flickr_shortcode_handler()   B

Complexity

Conditions 9
Paths 13

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 13
nop 1
dl 0
loc 48
rs 7.5789
c 0
b 0
f 0
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
			'w'     => '',
128
			'h'     => '',
129
		),
130
		$atts,
131
		'flickr'
132
	);
133
134
	if ( ! empty( $atts['video'] ) ) {
135
		$showing = 'video';
136
		$src     = $atts['video'];
137
	} elseif ( ! empty( $atts['photo'] ) ) {
138
		$showing = 'photo';
139
		$src     = $atts['photo'];
140
	} else {
141
		return '';
142
	}
143
144
	$src = str_replace( 'http://', 'https://', $src );
145
146
	if ( 'video' === $showing ) {
147
148
		if ( preg_match( '/^https?:\/\/(www\.)?flickr\.com\/.+/', $atts['video'] ) || preg_match( '/^https?:\/\/flic\.kr\/.+/', $atts['video'] ) ) {
149
			return flickr_shortcode_video_markup( $atts );
150
		}
151
		return '';
152
	} elseif ( 'photo' === $showing ) {
153
154
		if ( ! preg_match( '~^(https?:)?//([\da-z\-]+\.)*?((static)?flickr\.com|flic\.kr)/.*~i', $src ) ) {
155
			return '';
156
		}
157
158
		$src = sprintf( '%s/player/', untrailingslashit( $src ) );
159
160
		$allow_full_screen = 'allowfullscreen webkitallowfullscreen mozallowfullscreen oallowfullscreen msallowfullscreen';
161
162
		if ( Jetpack_AMP_Support::is_amp_request() ) {
163
			$allow_full_screen = '';
164
		}
165
166
		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 );
167
	}
168
169
	return false;
170
}
171
172
/**
173
 * Return HTML markup for a Flickr embed.
174
 *
175
 * @param array $atts Shortcode attributes.
176
 */
177
function flickr_shortcode_video_markup( $atts ) {
178
	$atts = array_map( 'esc_attr', $atts );
179
180
	$provider = 'https://www.flickr.com/services/oembed/';
181
	$oembed   = _wp_oembed_get_object();
182
	$data     = (array) $oembed->fetch( $provider, $atts['video'] );
183
184
	$html = $data['html'];
185
186 View Code Duplication
	if ( ! empty( $atts['w'] ) && is_numeric( $atts['w'] ) ) {
187
		$html = preg_replace( '/(width=\")\d+(\")/', '${1}' . $atts['w'] . '${2}', $html );
188
	}
189
190 View Code Duplication
	if ( ! empty( $atts['h'] ) && is_numeric( $atts['h'] ) ) {
191
		$html = preg_replace( '/(height=\")\d+(\")/', '${1}' . $atts['h'] . '${2}', $html );
192
	}
193
194
	return $html;
195
}
196
197
add_shortcode( 'flickr', 'flickr_shortcode_handler' );
198
199
// Override core's Flickr support because Flickr oEmbed doesn't support web embeds.
200
wp_embed_register_handler( 'flickr', '#https?://(www\.)?flickr\.com/.*#i', 'jetpack_flickr_oembed_handler' );
201
202
/**
203
 * Callback to modify output of embedded Vimeo video using Jetpack's shortcode.
204
 *
205
 * @since 3.9
206
 *
207
 * @param array $matches Regex partial matches against the URL passed.
208
 * @param array $attr    Attributes received in embed response.
209
 * @param array $url     Requested URL to be embedded.
210
 *
211
 * @return string Return output of Vimeo shortcode with the proper markup.
212
 */
213
function jetpack_flickr_oembed_handler( $matches, $attr, $url ) {
214
	/*
215
	 * Legacy slideshow embeds end with /show/
216
	 * e.g. http://www.flickr.com/photos/yarnaholic/sets/72157615194738969/show/
217
	 */
218
	if ( '/show/' !== substr( $url, -strlen( '/show/' ) ) ) {
219
		// These lookups need cached, as they don't use WP_Embed (which caches).
220
		$cache_key   = md5( $url . wp_json_encode( $attr ) );
221
		$cache_group = 'oembed_flickr';
222
223
		$html = wp_cache_get( $cache_key, $cache_group );
224
225
		if ( false === $html ) {
226
			$html = _wp_oembed_get_object()->get_html( $url, $attr );
227
228
			wp_cache_set( $cache_key, $html, $cache_group, 60 * MINUTE_IN_SECONDS );
229
		}
230
231
		return $html;
232
	}
233
234
	return flickr_shortcode_handler( array( 'photo' => $url ) );
235
}
236