Completed
Push — feature/videopress-uploader ( 58b703...e9e1f9 )
by
unknown
104:20 queued 95:05
created

modules/shortcodes.php (7 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Module Name: Shortcode Embeds
5
 * Module Description: Embed content from YouTube, Vimeo, SlideShare, and more, no coding necessary.
6
 * Sort Order: 3
7
 * First Introduced: 1.1
8
 * Major Changes In: 1.2
9
 * Requires Connection: No
10
 * Auto Activate: Yes
11
 * Module Tags: Photos and Videos, Social, Writing, Appearance
12
 * Additional Search Queries: shortcodes, shortcode, embeds, media, bandcamp, blip.tv, dailymotion, digg, facebook, flickr, google calendars, google maps, google+, polldaddy, recipe, recipes, scribd, slideshare, slideshow, slideshows, soundcloud, ted, twitter, vimeo, vine, youtube
13
 */
14
15
/**
16
 * Transforms the $atts array into a string that the old functions expected
17
 *
18
 * The old way was:
19
 * [shortcode a=1&b=2&c=3] or [shortcode=1]
20
 * This is parsed as array( a => '1&b=2&c=3' ) and array( 0 => '=1' ), which is useless
21
 *
22
 * @param Array $params
23
 * @param Bool $old_format_support true if [shortcode=foo] format is possible.
24
 * @return String $params
25
 */
26
function shortcode_new_to_old_params( $params, $old_format_support = false ) {
27
	$str = '';
28
29
	if ( $old_format_support && isset( $params[0] ) ) {
30
		$str = ltrim( $params[0], '=' );
31
	} elseif ( is_array( $params ) ) {
32
		foreach ( array_keys( $params ) as $key ) {
33
			if ( ! is_numeric( $key ) )
34
				$str = $key . '=' . $params[$key];
35
		}
36
	}
37
38
	return str_replace( array( '&amp;', '&#038;' ), '&', $str );
39
}
40
41
function jetpack_load_shortcodes() {
42
	global $wp_version;
43
44
	$shortcode_includes = array();
45
46
	foreach ( Jetpack::glob_php( dirname( __FILE__ ) . '/shortcodes' ) as $file ) {
47
		$shortcode_includes[] = $file;
48
	}
49
50
/**
51
 * This filter allows other plugins to override which shortcodes Jetpack loads.
52
 *
53
 * @module shortcodes
54
 *
55
 * @since 2.2.1
56
 *
57
 * @param array $shortcode_includes An array of which shortcodes to include.
58
 */
59
	$shortcode_includes = apply_filters( 'jetpack_shortcodes_to_include', $shortcode_includes );
60
61
	foreach ( $shortcode_includes as $include ) {
62
		if ( version_compare( $wp_version, '3.6-z', '>=' ) && stristr( $include, 'audio.php' ) ) {
63
			continue;
64
		}
65
66
		include $include;
67
	}
68
}
69
70
/**
71
 * Runs preg_replace so that replacements don't happen within open tags.  
72
 * Parameters are the same as preg_replace, with an added optional search param for improved performance
73
 *
74
 * @param String $pattern
75
 * @param String $replacement
76
 * @param String $content
77
 * @param String $search
0 ignored issues
show
Should the type for parameter $search not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
78
 * @return String $content
79
 */
80 View Code Duplication
function jetpack_preg_replace_outside_tags( $pattern, $replacement, $content, $search = null ) {
0 ignored issues
show
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...
81
	if( ! function_exists( 'wp_html_split' ) ) {
82
		return $content;
83
	}
84
85
	if ( $search && false === strpos( $content, $search ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $search of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
86
		return $content;
87
	}
88
	
89
	$textarr = wp_html_split( $content );
90
	unset( $content );
91
	foreach( $textarr as &$element ) {
92
	    if ( '' === $element || '<' === $element{0} )
93
	        continue;
94
	    $element = preg_replace( $pattern, $replacement, $element );
95
	}
96
	
97
	return join( $textarr );
98
}
99
100
/**
101
 * Runs preg_replace_callback so that replacements don't happen within open tags.  
102
 * Parameters are the same as preg_replace, with an added optional search param for improved performance
103
 *
104
 * @param String $pattern
105
 * @param String $replacement
0 ignored issues
show
There is no parameter named $replacement. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
106
 * @param String $content
107
 * @param String $search
0 ignored issues
show
Should the type for parameter $search not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
108
 * @return String $content
109
 */
110 View Code Duplication
function jetpack_preg_replace_callback_outside_tags( $pattern, $callback, $content, $search = null ) {
0 ignored issues
show
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...
111
	if( ! function_exists( 'wp_html_split' ) ) {
112
		return $content;
113
	}
114
115
	if ( $search && false === strpos( $content, $search ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $search of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
116
		return $content;
117
	}
118
	
119
	$textarr = wp_html_split( $content );
120
	unset( $content );
121
	foreach( $textarr as &$element ) {
122
	    if ( '' === $element || '<' === $element{0} )
123
	        continue;
124
	    $element = preg_replace_callback( $pattern, $callback, $element );
125
	}
126
	
127
	return join( $textarr );
128
}
129
130
global $wp_version;
131
132
if ( version_compare( $wp_version, '3.6-z', '>=' ) ) {
133
	add_filter( 'shortcode_atts_audio', 'jetpack_audio_atts_handler', 10, 3 );
134
135
	function jetpack_audio_atts_handler( $out, $pairs, $atts ) {
136
		if( isset( $atts[0] ) )
137
			$out['src'] = $atts[0];
138
139
		return $out;
140
	}
141
142
	function jetpack_shortcode_get_audio_id( $atts ) {
143
		if ( isset( $atts[ 0 ] ) )
144
			return $atts[ 0 ];
145
		else
146
			return 0;
147
	}
148
}
149
150
if ( ! function_exists( 'jetpack_shortcode_get_wpvideo_id' ) ) {
151
	function jetpack_shortcode_get_wpvideo_id( $atts ) {
152
		if ( isset( $atts[ 0 ] ) )
153
			return $atts[ 0 ];
154
		else
155
			return 0;
156
	}
157
}
158
159
jetpack_load_shortcodes();
160