Completed
Push — revert/videopress-uploader ( 93d651 )
by
unknown
08:30
created

shortcode.php ➔ videopress_shortcode_callback()   C

Complexity

Conditions 12
Paths 25

Size

Total Lines 99
Code Lines 45

Duplication

Lines 6
Ratio 6.06 %

Importance

Changes 0
Metric Value
cc 12
eloc 45
nc 25
nop 1
dl 6
loc 99
rs 5.034
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 View Code Duplication
function videopress_shortcode_callback( $attr ) {
0 ignored issues
show
Best Practice introduced by
The function videopress_shortcode_callback() has been defined more than once; this definition is ignored, only the first definition in modules/videopress-v2/shortcode.php (L21-119) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
	global $content_width;
23
24
	/**
25
	 * We only accept GUIDs as a first unnamed argument.
26
	 */
27
	$guid = $attr[0];
28
29
	/**
30
	 * Make sure the GUID passed in matches how actual GUIDs are formatted.
31
	 */
32
	if ( ! videopress_is_valid_guid( $guid ) ) {
33
		return '';
34
	}
35
36
	/**
37
	 * Set the defaults
38
	 */
39
	$defaults = array(
40
		'w'               => 0,     // Width of the video player, in pixels
41
		'at'              => 0,     // How many seconds in to initially seek to
42
		'hd'              => false, // Whether to display a high definition version
43
		'loop'            => false, // Whether to loop the video repeatedly
44
		'freedom'         => false, // Whether to use only free/libre codecs
45
		'autoplay'        => false, // Whether to autoplay the video on load
46
		'permalink'       => true,  // Whether to display the permalink to the video
47
		'flashonly'       => false, // Whether to support the Flash player exclusively
48
		'defaultlangcode' => false, // Default language code
49
	);
50
51
	$attr = shortcode_atts( $defaults, $attr, 'videopress' );
52
53
	/**
54
	 * Cast the attributes, post-input.
55
	 */
56
	$attr['width']   = absint( $attr['w'] );
57
	$attr['hd']      = (bool) $attr['hd'];
58
	$attr['freedom'] = (bool) $attr['freedom'];
59
60
	/**
61
	 * If the provided width is less than the minimum allowed
62
	 * width, or greater than `$content_width` ignore.
63
	 */
64
	if ( $attr['width'] < VIDEOPRESS_MIN_WIDTH ) {
65
		$attr['width'] = 0;
66
	} elseif ( isset( $content_width ) && $content_width > VIDEOPRESS_MIN_WIDTH && $attr['width'] > $content_width ) {
67
		$attr['width'] = 0;
68
	}
69
70
	/**
71
	 * If there was an invalid or unspecified width, set the width equal to the theme's `$content_width`.
72
	 */
73
	if ( 0 === $attr['width'] && isset( $content_width ) && $content_width >= VIDEOPRESS_MIN_WIDTH ) {
74
		$attr['width'] = $content_width;
75
	}
76
77
	/**
78
	 * If the width isn't an even number, reduce it by one (making it even).
79
	 */
80
	if ( 1 === ( $attr['width'] % 2 ) ) {
81
		$attr['width'] --;
82
	}
83
84
	/**
85
	 * Filter the default VideoPress shortcode options.
86
	 *
87
	 * @module videopress
88
	 *
89
	 * @since 2.5.0
90
	 *
91
	 * @param array $args Array of VideoPress shortcode options.
92
	 */
93
	$options = apply_filters( 'videopress_shortcode_options', array(
94
		'at'              => (int) $attr['at'],
95
		'hd'              => $attr['hd'],
96
		'loop'            => $attr['autoplay'] || $attr['loop'],
97
		'freedom'         => $attr['freedom'],
98
		'autoplay'        => $attr['autoplay'],
99
		'permalink'       => $attr['permalink'],
100
		'force_flash'     => (bool) $attr['flashonly'],
101
		'defaultlangcode' => $attr['defaultlangcode'],
102
		'forcestatic'     => false, // This used to be a displayed option, but now is only
103
		// accessible via the `videopress_shortcode_options` filter.
104
	) );
105
106
	// Register VideoPress scripts
107
	wp_register_script( 'videopress', 'https://v0.wordpress.com/js/videopress.js', array( 'jquery', 'swfobject' ), '1.09' );
108
109
	require_once( dirname( __FILE__ ) . '/class.videopress-video.php' );
110
	require_once( dirname( __FILE__ ) . '/class.videopress-player.php' );
111
112
	$player = new VideoPress_Player( $guid, $attr['width'], $options );
113
114
	if ( is_feed() ) {
115
		return $player->asXML();
116
	} else {
117
		return $player->asHTML();
118
	}
119
}
120
add_shortcode( 'videopress', 'videopress_shortcode_callback' );
121
add_shortcode( 'wpvideo',    'videopress_shortcode_callback' );
122
123
/**
124
 * By explicitly declaring the provider here, we can speed things up by not relying on oEmbed discovery.
125
 */
126
wp_oembed_add_provider( '#^https?://videopress.com/v/.*#', 'http://public-api.wordpress.com/oembed/1.0/', true );
127
128
/**
129
 * Adds a `for` query parameter to the oembed provider request URL.
130
 * @param String $oembed_provider
131
 * @return String $ehnanced_oembed_provider
132
 */
133
function videopress_add_oembed_for_parameter( $oembed_provider ) {
0 ignored issues
show
Best Practice introduced by
The function videopress_add_oembed_for_parameter() has been defined more than once; this definition is ignored, only the first definition in modules/videopress-v2/shortcode.php (L133-138) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
134
	if ( false === stripos( $oembed_provider, 'videopress.com' ) ) {
135
		return $oembed_provider;
136
	}
137
	return add_query_arg( 'for', parse_url( home_url(), PHP_URL_HOST ), $oembed_provider );
138
}
139
add_filter( 'oembed_fetch_url', 'videopress_add_oembed_for_parameter' );
140
141
/**
142
 * An intermediary shortcode parser for the Core `[video]` shortcode.
143
 *
144
 * This lets us convert legacy video embeds over to VideoPress embeds,
145
 * if the video files have been uploaded and transcoded.
146
 *
147
 * @param $attr
148
 *
149
 * @return string|void
150
 * /
151
function videopress_shortcode_override_for_core_shortcode( $raw_attr, $contents, $tag ) {
152
	$attr = $raw_attr;
153
	$videopress_guid = false;
154
155
	if ( isset( $attr['videopress_guid'] ) ) {
156
		$videopress_guid = $attr['videopress_guid'];
157
	}
158
159
	// If we can find a local media item from the provided url…
160
	$media_id = videopress_get_attachment_id_by_url( $attr['src'] );
161
	if ( $media_id ) {
162
		// And that local media item has a VideoPress GUID attached to it…
163
		$videopress_guid = get_post_meta( $media_id, 'videopress_guid', true );
164
	}
165
166
	if ( $videopress_guid ) {
167
		$videopress_attr = array( $videopress_guid );
168
		if ( $attr['width'] ) {
169
			$videopress_attr['w'] = (int) $attr['width'];
170
		}
171
		if ( $attr['autoplay'] ) {
172
			$videopress_attr['autoplay'] = $attr['autoplay'];
173
		}
174
		if ( $attr['loop'] ) {
175
			$videopress_attr['loop'] = $attr['loop'];
176
		}
177
178
		// Then display the VideoPress version of the stored GUID!
179
		return videopress_shortcode_callback( $videopress_attr );
180
	}
181
182
	// Nothing else caught, so fall back to the core shortcode.
183
	return call_user_func( $GLOBALS['vp_original_video_shortcode_callback'], $raw_attr, $contents, $tag );
184
}
185
// The callback should nearly always be `wp_video_shortcode` unless some other plugin
186
// has overridden it similarly to what we're doing here.
187
$GLOBALS['vp_original_video_shortcode_callback'] = $GLOBALS['shortcode_tags']['video'];
188
remove_shortcode( 'video' );
189
add_shortcode( 'video', 'videopress_shortcode_override_for_core_shortcode' );
190
/**/
191