Completed
Push — add/related-posts-customize ( 4a306a...1c5144 )
by
unknown
43:30 queued 35:07
created

shortcode.php ➔ videopress_shortcode_callback()   D

Complexity

Conditions 14
Paths 100

Size

Total Lines 103
Code Lines 47

Duplication

Lines 6
Ratio 5.83 %

Importance

Changes 0
Metric Value
cc 14
eloc 47
nc 100
nop 1
dl 6
loc 103
rs 4.9516
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
/**
4
 * VideoPress Shortcode Handler
5
 *
6
 * This file may or may not be included from the Jetpack VideoPress module.
7
 */
8
9
/**
10
 * Translate a 'videopress' or 'wpvideo' shortcode and arguments into a video player display.
11
 *
12
 * Expected input formats:
13
 *
14
 * [videopress OcobLTqC]
15
 * [wpvideo OcobLTqC]
16
 *
17
 * @link http://codex.wordpress.org/Shortcode_API Shortcode API
18
 * @param array $attr shortcode attributes
19
 * @return string HTML markup or blank string on fail
20
 */
21
function videopress_shortcode_callback( $attr ) {
22
	global $content_width;
23
24
	/**
25
	 * We only accept GUIDs as a first unnamed argument.
26
	 */
27
	$guid = isset( $attr[0] ) ? $attr[0] : null;
28
29
	if ( isset( $attr['postid'] ) ) {
30
		$guid = get_post_meta( $attr['postid'], 'videopress_guid', true );
31
	}
32
33
	/**
34
	 * Make sure the GUID passed in matches how actual GUIDs are formatted.
35
	 */
36
	if ( ! videopress_is_valid_guid( $guid ) ) {
37
		return '';
38
	}
39
40
	/**
41
	 * Set the defaults
42
	 */
43
	$defaults = array(
44
		'w'               => 0,     // Width of the video player, in pixels
45
		'at'              => 0,     // How many seconds in to initially seek to
46
		'hd'              => true,  // Whether to display a high definition version
47
		'loop'            => false, // Whether to loop the video repeatedly
48
		'freedom'         => false, // Whether to use only free/libre codecs
49
		'autoplay'        => false, // Whether to autoplay the video on load
50
		'permalink'       => true,  // Whether to display the permalink to the video
51
		'flashonly'       => false, // Whether to support the Flash player exclusively
52
		'defaultlangcode' => false, // Default language code
53
	);
54
55
	$attr = shortcode_atts( $defaults, $attr, 'videopress' );
56
57
	/**
58
	 * Cast the attributes, post-input.
59
	 */
60
	$attr['width']   = absint( $attr['w'] );
61
	$attr['hd']      = (bool) $attr['hd'];
62
	$attr['freedom'] = (bool) $attr['freedom'];
63
64
	/**
65
	 * If the provided width is less than the minimum allowed
66
	 * width, or greater than `$content_width` ignore.
67
	 */
68
	if ( $attr['width'] < VIDEOPRESS_MIN_WIDTH ) {
69
		$attr['width'] = 0;
70 View Code Duplication
	} elseif ( isset( $content_width ) && $content_width > VIDEOPRESS_MIN_WIDTH && $attr['width'] > $content_width ) {
71
		$attr['width'] = 0;
72
	}
73
74
	/**
75
	 * If there was an invalid or unspecified width, set the width equal to the theme's `$content_width`.
76
	 */
77 View Code Duplication
	if ( 0 === $attr['width'] && isset( $content_width ) && $content_width >= VIDEOPRESS_MIN_WIDTH ) {
78
		$attr['width'] = $content_width;
79
	}
80
81
	/**
82
	 * If the width isn't an even number, reduce it by one (making it even).
83
	 */
84
	if ( 1 === ( $attr['width'] % 2 ) ) {
85
		$attr['width'] --;
86
	}
87
88
	/**
89
	 * Filter the default VideoPress shortcode options.
90
	 *
91
	 * @module videopress
92
	 *
93
	 * @since 2.5.0
94
	 *
95
	 * @param array $args Array of VideoPress shortcode options.
96
	 */
97
	$options = apply_filters( 'videopress_shortcode_options', array(
98
		'at'              => (int) $attr['at'],
99
		'hd'              => $attr['hd'],
100
		'loop'            => $attr['autoplay'] || $attr['loop'],
101
		'freedom'         => $attr['freedom'],
102
		'autoplay'        => $attr['autoplay'],
103
		'permalink'       => $attr['permalink'],
104
		'force_flash'     => (bool) $attr['flashonly'],
105
		'defaultlangcode' => $attr['defaultlangcode'],
106
		'forcestatic'     => false, // This used to be a displayed option, but now is only
107
		// accessible via the `videopress_shortcode_options` filter.
108
	) );
109
110
	// Register VideoPress scripts
111
	wp_register_script( 'videopress', 'https://v0.wordpress.com/js/videopress.js', array( 'jquery', 'swfobject' ), '1.09' );
112
113
	require_once( dirname( __FILE__ ) . '/class.videopress-video.php' );
114
	require_once( dirname( __FILE__ ) . '/class.videopress-player.php' );
115
116
	$player = new VideoPress_Player( $guid, $attr['width'], $options );
117
118
	if ( is_feed() ) {
119
		return $player->asXML();
120
	} else {
121
		return $player->asHTML();
122
	}
123
}
124
add_shortcode( 'videopress', 'videopress_shortcode_callback' );
125
add_shortcode( 'wpvideo',    'videopress_shortcode_callback' );
126
127
/**
128
 * By explicitly declaring the provider here, we can speed things up by not relying on oEmbed discovery.
129
 */
130
wp_oembed_add_provider( '#^https?://videopress.com/v/.*#', 'http://public-api.wordpress.com/oembed/1.0/', true );
131
132
/**
133
 * Adds a `for` query parameter to the oembed provider request URL.
134
 * @param String $oembed_provider
135
 * @return String $ehnanced_oembed_provider
136
 */
137
function videopress_add_oembed_for_parameter( $oembed_provider ) {
138
	if ( false === stripos( $oembed_provider, 'videopress.com' ) ) {
139
		return $oembed_provider;
140
	}
141
	return add_query_arg( 'for', parse_url( home_url(), PHP_URL_HOST ), $oembed_provider );
142
}
143
add_filter( 'oembed_fetch_url', 'videopress_add_oembed_for_parameter' );
144
145
/**
146
 * An intermediary shortcode parser for the Core `[video]` shortcode.
147
 *
148
 * This lets us convert legacy video embeds over to VideoPress embeds,
149
 * if the video files have been uploaded and transcoded.
150
 *
151
 * @param $attr
152
 *
153
 * @return string|void
154
 */
155
function videopress_shortcode_override_for_core_shortcode( $raw_attr, $contents, $tag ) {
156
	$attr = $raw_attr;
157
	$videopress_guid = null;
158
159
	if ( isset( $attr['videopress_guid'] ) ) {
160
		$videopress_guid = $attr['videopress_guid'];
161
162
	} elseif ( isset( $attr['mp4'] ) ) {
163
		$url = $attr['mp4'];
164
165
		if ( preg_match( '@videos.videopress.com/([a-z0-9]{8})/@', $url, $matches ) ) {
166
			$videopress_guid = $matches[1];
167
		}
168
	}
169
170
	if ( $videopress_guid ) {
171
		$videopress_attr = array( $videopress_guid );
172
		if ( $attr['width'] ) {
173
			$videopress_attr['w'] = (int) $attr['width'];
174
		}
175
		if ( $attr['autoplay'] ) {
176
			$videopress_attr['autoplay'] = $attr['autoplay'];
177
		}
178
		if ( $attr['loop'] ) {
179
			$videopress_attr['loop'] = $attr['loop'];
180
		}
181
182
		// Then display the VideoPress version of the stored GUID!
183
		return videopress_shortcode_callback( $videopress_attr );
184
	}
185
186
	// Nothing else caught, so fall back to the core shortcode.
187
	return call_user_func( $GLOBALS['vp_original_video_shortcode_callback'], $raw_attr, $contents, $tag );
188
}
189
// The callback should nearly always be `wp_video_shortcode` unless some other plugin
190
// has overridden it similarly to what we're doing here.
191
$GLOBALS['vp_original_video_shortcode_callback'] = $GLOBALS['shortcode_tags']['video'];
192
remove_shortcode( 'video' );
193
add_shortcode( 'video', 'videopress_shortcode_override_for_core_shortcode' );
194