Completed
Push — fix/flickr-shortcode ( 6fcc06...5711ab )
by
unknown
07:08
created

flickr.php ➔ flickr_embed_to_shortcode()   C

Complexity

Conditions 13
Paths 81

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
nc 81
nop 1
dl 0
loc 60
rs 6.6166
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=www.flickr.com/photos/kalakeli/49931239842]
8
 * [flickr video=49931239842]
9
 * [flickr video=49931239842 w=200 h=150]
10
 * [flickr video=2402990826 autoplay="yes" controls="no"]
11
 * [flickr video=2402990826 autoplay="no" controls=yes w=200 h=150]
12
 *
13
 * @package Jetpack
14
 */
15
16
/*
17
 * <div class="flick_video" style="max-width: 100%;width: 500px;height: 300px;"><video src="https://www.flickr.com/photos/kalakeli/49931239842/play/360p/183f75d545/" controls autoplay ></video></div>
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 ) ) {
27
		return $content;
28
	}
29
30
	$type = '';
31
32
	if ( false !== strpos( $content, '<div class="flickr_video"' ) && false !== strpos( $content, '<video' ) ) {
33
		$type = 'video';
34
	}
35
36
	if ( preg_match( '/<iframe src="(https?:)?\/\/([\da-z\-]+\.)*?((static)?flickr\.com|flic\.kr)\/[^\"]+\"/', $content ) ) {
37
		$type = 'photo';
38
	}
39
40
	if ( empty( $type ) ) {
41
		return $content;
42
	}
43
44
	if ( 'video' === $type ) {
45
		// Get video src.
46
		preg_match( '/<video src=\"([^\"]+)\"/', $content, $matches );
47
48
		if ( empty( $matches[1] ) ) {
49
			return $content;
50
		}
51
52
		preg_match( '/\d+/', $matches[1], $matches );
53
54
		$video_id = $matches[0];
55
56
		// Get width.
57
		preg_match( '/width:\s(\d+)px/', $content, $matches );
58
59
		$width = empty( $matches[1] ) ? '' : 'w=' . $matches[1];
60
		// Get height.
61
		preg_match( '/height:\s(\d+)px/', $content, $matches );
62
63
		$height = empty( $matches[1] ) ? '' : 'h=' . $matches[1];
64
65
		$controls = false !== strpos( $content, 'controls' ) ? 'yes' : 'no';
66
67
		$autoplay = false !== strpos( $content, 'autoplay' ) ? 'yes' : 'no';
68
69
		return '[flickr video="' . $video_id . '" ' . $width . ' ' . $height . ' controls="' . $controls . '" autoplay="' . $autoplay . '"]';
70
71
	}
72
73
	preg_match( '/<iframe src=\"([^\"]+)\"\s+height=\"([^\"]+)\"\s+width=\"([^\"]+)\"/', $content, $matches );
74
75
	if ( empty( $matches[1] ) ) {
76
		return $content;
77
	}
78
79
	$src    = str_replace( 'player/', '', $matches[1] );
80
	$width  = $matches[2];
81
	$height = $matches[3];
82
83
	return '[flickr photo="' . $src . '" w=' . $width . ' h=' . $height . ']';
84
}
85
add_filter( 'pre_kses', 'flickr_embed_to_shortcode' );
86
87
/**
88
 * Flickr Shortcode handler.
89
 *
90
 * @param array $atts Shortcode attributes.
91
 */
92
function flickr_shortcode_handler( $atts ) {
93
	$atts = shortcode_atts(
94
		array(
95
			'video'    => 0,
96
			'photo'    => 0,
97
			'w'        => '',
98
			'h'        => '',
99
			'controls' => 'yes',
100
			'autoplay' => '',
101
		),
102
		$atts,
103
		'flickr'
104
	);
105
106
	if ( ! empty( $atts['video'] ) ) {
107
		$showing = 'video';
108
		$src     = $atts['video'];
109
	} elseif ( ! empty( $atts['photo'] ) ) {
110
		$showing = 'photo';
111
		$src     = $atts['photo'];
112
	} else {
113
		return '';
114
	}
115
116
	$src = str_replace( 'http://', 'https://', $src );
117
118
	if ( 'video' === $showing ) {
119
120
		$video_id = flick_shortcode_video_id( $src );
121
122
		if ( empty( $video_id ) ) {
123
			return '';
124
		}
125
126
		$atts = array_map( 'esc_attr', $atts );
127
		return flickr_shortcode_video_markup( $atts, $video_id, $src );
128
	} elseif ( 'photo' === $showing ) {
129
130
		if ( ! preg_match( '~^(https?:)?//([\da-z\-]+\.)*?((static)?flickr\.com|flic\.kr)/.*~i', $src ) ) {
131
			return '';
132
		}
133
134
		$height = empty( $atts['h'] ) ? 'auto' : esc_attr( $atts['h'] );
135
136
		$src = sprintf( '%s/player/', untrailingslashit( $src ) );
137
138
		$allow_full_screen = 'allowfullscreen webkitallowfullscreen mozallowfullscreen oallowfullscreen msallowfullscreen';
139
140
		if ( Jetpack_AMP_Support::is_amp_request() ) {
141
			$allow_full_screen = '';
142
		}
143
144
		return sprintf( '<iframe src="%s" height="%s" width="%s"  frameborder="0" %s></iframe>', esc_url( $src ), $height, esc_attr( $atts['w'] ), $allow_full_screen );
145
	}
146
147
	return false;
148
}
149
150
/**
151
 * Return HTML markup for a Flickr embed.
152
 *
153
 * @param array  $atts Shortcode attributes.
154
 * @param string $id Video ID.
155
 * @param string $video_param video param of the shortcode.
156
 */
157
function flickr_shortcode_video_markup( $atts, $id, $video_param ) {
158
159
	$transient_name = "flickr_video_$id";
160
	$video_src      = get_transient( $transient_name );
161
162
	if ( empty( $video_src ) ) {
163
		$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...
164
		if ( ! is_numeric( $video_param ) ) {
165
			$video_url = $video_param;
166
		} else {
167
			// Get the URL of the video from the page of the video.
168
			$video_page_content = wp_remote_get( "http://flickr.com/photo.gne?id=$video_param" );
169
170
			// Extract the URL from the og:url meta tag.
171
			preg_match( '/property=\"og:url\"\scontent=\"([^\"]+)\"/', $video_page_content['body'], $matches );
172
			if ( empty( $matches[1] ) ) {
173
				return '';
174
			}
175
			$video_url = $matches[1];
176
		}
177
178
		$provider = 'https://www.flickr.com/services/oembed/';
179
		$oembed   = _wp_oembed_get_object();
180
		$data     = (array) $oembed->fetch( $provider, $video_url );
181
		if ( empty( $data['html'] ) ) {
182
			return '';
183
		}
184
185
		// Get the embed url.
186
		preg_match( '/src=\"([^\"]+)\"/', $data['html'], $matches );
187
188
		$embed_url = $matches[1];
189
190
		$embed_page = wp_remote_get( $embed_url );
191
192
		// Get the video url from embed html markup.
193
194
		preg_match( '/video.+src=\"([^\"]+)\"/', $embed_page['body'], $matches );
195
196
		$video_src = $matches[1];
197
198
		set_transient( $transient_name, $video_src, 2592000 ); // 30 days transient.
199
	}
200
201
	$style = 'max-width: 100%;';
202
203
	if ( ! empty( $atts['w'] ) && is_numeric( $atts['w'] ) ) {
204
		$style .= sprintf( 'width: %dpx;', $atts['w'] );
205
	}
206
207
	if ( ! empty( $atts['h'] ) && is_numeric( $atts['h'] ) ) {
208
		$style .= sprintf( 'height: %dpx;', $atts['h'] );
209
	}
210
211
	$controls = 'yes' === $atts['controls'] ? 'controls' : '';
212
	$autoplay = 'yes' === $atts['autoplay'] ? 'autoplay' : '';
213
214
	return sprintf(
215
		'<div class="flick_video" style="%s"><video src="%s" %s %s /></div>',
216
		$style,
217
		$video_src,
218
		$controls,
219
		$autoplay
220
	);
221
}
222
223
/**
224
 * Extract the id of the flickr video from the video param.
225
 *
226
 * @param string $video_param Video parameter of the shortcode.
227
 */
228
function flick_shortcode_video_id( $video_param ) {
229
	if ( preg_match( '/^https?:\/\/(www\.)?flickr\.com\/.+/', $video_param ) || preg_match( '/^https?:\/\/flic\.kr\/.+/', $video_param ) ) {
230
231
		// Extract the video id from the url.
232
		preg_match( '/\d+/', $video_param, $matches );
233
234
		if ( empty( $matches ) ) {
235
			return false;
236
		}
237
238
		return $matches[0];
239
240
	} elseif ( is_numeric( $video_param ) ) {
241
		return $video_param;
242
	}
243
244
	return false;
245
}
246
247
add_shortcode( 'flickr', 'flickr_shortcode_handler' );
248
249
// Override core's Flickr support because Flickr oEmbed doesn't support web embeds.
250
wp_embed_register_handler( 'flickr', '#https?://(www\.)?flickr\.com/.*#i', 'jetpack_flickr_oembed_handler' );
251
252
/**
253
 * Callback to modify output of embedded Vimeo video using Jetpack's shortcode.
254
 *
255
 * @since 3.9
256
 *
257
 * @param array $matches Regex partial matches against the URL passed.
258
 * @param array $attr    Attributes received in embed response.
259
 * @param array $url     Requested URL to be embedded.
260
 *
261
 * @return string Return output of Vimeo shortcode with the proper markup.
262
 */
263
function jetpack_flickr_oembed_handler( $matches, $attr, $url ) {
264
	/*
265
	 * Legacy slideshow embeds end with /show/
266
	 * e.g. http://www.flickr.com/photos/yarnaholic/sets/72157615194738969/show/
267
	 */
268
	if ( '/show/' !== substr( $url, -strlen( '/show/' ) ) ) {
269
		// These lookups need cached, as they don't use WP_Embed (which caches).
270
		$cache_key   = md5( $url . wp_json_encode( $attr ) );
271
		$cache_group = 'oembed_flickr';
272
273
		$html = wp_cache_get( $cache_key, $cache_group );
274
275
		if ( false === $html ) {
276
			$html = _wp_oembed_get_object()->get_html( $url, $attr );
277
278
			wp_cache_set( $cache_key, $html, $cache_group, 60 * MINUTE_IN_SECONDS );
279
		}
280
281
		return $html;
282
	}
283
284
	return flickr_shortcode_handler( array( 'photo' => $url ) );
285
}
286