Completed
Push — fix/flickr-shortcode ( d67fa5...6855b1 )
by
unknown
06:55
created

flickr.php ➔ flick_shortcode_video_id()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 1
dl 0
loc 18
rs 9.3554
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
			'photo' => 0,
128
			'w'     => '',
129
			'h'     => '',
130
		),
131
		$atts,
132
		'flickr'
133
	);
134
135
	if ( ! empty( $atts['video'] ) ) {
136
		$showing = 'video';
137
		$src     = $atts['video'];
138
	} elseif ( ! empty( $atts['photo'] ) ) {
139
		$showing = 'photo';
140
		$src     = $atts['photo'];
141
	} else {
142
		return '';
143
	}
144
145
	$src = str_replace( 'http://', 'https://', $src );
146
147
	if ( 'video' === $showing ) {
148
149
		$video_id = flick_shortcode_video_id( $src );
150
151
		if ( empty( $video_id ) ) {
152
			return '';
153
		}
154
155
		$atts = array_map( 'esc_attr', $atts );
156
		return flickr_shortcode_video_markup( $atts, $video_id, $src );
157
	} elseif ( 'photo' === $showing ) {
158
159
		if ( ! preg_match( '~^(https?:)?//([\da-z\-]+\.)*?((static)?flickr\.com|flic\.kr)/.*~i', $src ) ) {
160
			return '';
161
		}
162
163
		$src = sprintf( '%s/player/', untrailingslashit( $src ) );
164
165
		$allow_full_screen = 'allowfullscreen webkitallowfullscreen mozallowfullscreen oallowfullscreen msallowfullscreen';
166
167
		if ( Jetpack_AMP_Support::is_amp_request() ) {
168
			$allow_full_screen = '';
169
		}
170
171
		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 );
172
	}
173
174
	return false;
175
}
176
177
/**
178
 * Return HTML markup for a Flickr embed.
179
 *
180
 * @param array  $atts Shortcode attributes.
181
 * @param string $id Video ID.
182
 * @param string $video_param video param of the shortcode.
183
 */
184
function flickr_shortcode_video_markup( $atts, $id, $video_param ) {
185
186
	$transient_name = "flick_video_$id";
187
	$html           = get_transient( $transient_name );
188
189
	if ( empty( $html ) ) {
190
		$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...
191
		if ( ! is_numeric( $video_param ) ) {
192
			$video_url = $video_param;
193
		} else {
194
			// Get the URL of the video from the page of the video.
195
			$video_page_content = wp_remote_get( "http://flickr.com/photo.gne?id=$video_param" );
196
197
			// Extract the URL from the og:url meta tag.
198
			preg_match( '/property=\"og:url\"\scontent=\"([^\"]+)\"/', $video_page_content['body'], $matches );
199
200
			$video_url = $matches[1];
201
		}
202
203
		$provider = 'https://www.flickr.com/services/oembed/';
204
		$oembed   = _wp_oembed_get_object();
205
		$data     = (array) $oembed->fetch( $provider, $video_url );
206
		$html     = $data['html'];
207
		set_transient( $transient_name, $html, 2592000 ); // 30 days transient.
208
	}
209
210 View Code Duplication
	if ( ! empty( $atts['w'] ) && is_numeric( $atts['w'] ) ) {
211
		$html = preg_replace( '/(width=\")\d+(\")/', '${1}' . $atts['w'] . '${2}', $html );
212
	}
213
214 View Code Duplication
	if ( ! empty( $atts['h'] ) && is_numeric( $atts['h'] ) ) {
215
		$html = preg_replace( '/(height=\")\d+(\")/', '${1}' . $atts['h'] . '${2}', $html );
216
	}
217
218
	return $html;
219
}
220
221
/**
222
 * Extract the id of the flickr video from the video param.
223
 *
224
 * @param string $video_param Video parameter of the shortcode.
225
 */
226
function flick_shortcode_video_id( $video_param ) {
227
	if ( preg_match( '/^https?:\/\/(www\.)?flickr\.com\/.+/', $video_param ) || preg_match( '/^https?:\/\/flic\.kr\/.+/', $video_param ) ) {
228
229
		// Extract the video id from the url.
230
		preg_match( '/\d+/', $video_param, $matches );
231
232
		if ( empty( $matches ) ) {
233
			return false;
234
		}
235
236
		return $matches[0];
237
238
	} elseif ( is_numeric( $video_param ) ) {
239
		return $video_param;
240
	}
241
242
	return false;
243
}
244
245
add_shortcode( 'flickr', 'flickr_shortcode_handler' );
246
247
// Override core's Flickr support because Flickr oEmbed doesn't support web embeds.
248
wp_embed_register_handler( 'flickr', '#https?://(www\.)?flickr\.com/.*#i', 'jetpack_flickr_oembed_handler' );
249
250
/**
251
 * Callback to modify output of embedded Vimeo video using Jetpack's shortcode.
252
 *
253
 * @since 3.9
254
 *
255
 * @param array $matches Regex partial matches against the URL passed.
256
 * @param array $attr    Attributes received in embed response.
257
 * @param array $url     Requested URL to be embedded.
258
 *
259
 * @return string Return output of Vimeo shortcode with the proper markup.
260
 */
261
function jetpack_flickr_oembed_handler( $matches, $attr, $url ) {
262
	/*
263
	 * Legacy slideshow embeds end with /show/
264
	 * e.g. http://www.flickr.com/photos/yarnaholic/sets/72157615194738969/show/
265
	 */
266
	if ( '/show/' !== substr( $url, -strlen( '/show/' ) ) ) {
267
		// These lookups need cached, as they don't use WP_Embed (which caches).
268
		$cache_key   = md5( $url . wp_json_encode( $attr ) );
269
		$cache_group = 'oembed_flickr';
270
271
		$html = wp_cache_get( $cache_key, $cache_group );
272
273
		if ( false === $html ) {
274
			$html = _wp_oembed_get_object()->get_html( $url, $attr );
275
276
			wp_cache_set( $cache_key, $html, $cache_group, 60 * MINUTE_IN_SECONDS );
277
		}
278
279
		return $html;
280
	}
281
282
	return flickr_shortcode_handler( array( 'photo' => $url ) );
283
}
284