Conditions | 45 |
Paths | > 20000 |
Total Lines | 342 |
Code Lines | 186 |
Lines | 20 |
Ratio | 5.85 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
36 | function audio_shortcode( $atts ) { |
||
37 | global $ap_playerID; |
||
38 | global $post; |
||
39 | |||
40 | if ( ! is_array( $atts ) ) { |
||
41 | return '<!-- Audio shortcode passed invalid attributes -->'; |
||
42 | } |
||
43 | |||
44 | View Code Duplication | if ( ! isset( $atts[0] ) ) { |
|
45 | if ( isset( $atts['src'] ) ) { |
||
46 | $atts[0] = $atts['src']; |
||
47 | unset( $atts['src'] ); |
||
48 | } else { |
||
49 | return '<!-- Audio shortcode source not set -->'; |
||
50 | } |
||
51 | } |
||
52 | |||
53 | $post_id = 0; |
||
54 | if ( isset( $post ) ) { |
||
55 | $post_id = $post->ID; |
||
56 | } |
||
57 | |||
58 | // add the special .js |
||
59 | wp_enqueue_script( |
||
60 | 'audio-shortcode', |
||
61 | plugins_url( 'js/audio-shortcode.js', __FILE__ ), |
||
62 | array( 'jquery' ), |
||
63 | '1.1', |
||
64 | true); |
||
65 | |||
66 | // alert the infinite scroll renderer that it should try to load the script |
||
67 | self::$add_script = true; |
||
68 | $atts[0] = strip_tags( join( ' ', $atts ) ); |
||
69 | $src = ltrim( $atts[0], '=' ); |
||
70 | |||
71 | /** |
||
72 | * Set the audio player default colors. |
||
73 | * |
||
74 | * @module shortcodes |
||
75 | * |
||
76 | * @since 1.4.0 |
||
77 | * |
||
78 | * @param array $ap_options { |
||
79 | * The default colors for the audio player in hexidecimal format (e.g. 0x#F8F8F8). |
||
80 | * |
||
81 | * @type string $bg Background color. |
||
82 | * @type string $leftbg Left background color. |
||
83 | * @type string $lefticon Left icon color. |
||
84 | * @type string $rightbg Right background color. |
||
85 | * @type string $rightbghover Right background hover color. |
||
86 | * @type string $righticon Right icon color. |
||
87 | * @type string $righticonhover Right icon hover color. |
||
88 | * @type string $text Text color. |
||
89 | * @type string $slider Slider color. |
||
90 | * @type string $track Track color. |
||
91 | * @type string $border Border color. |
||
92 | * @type string $loader Loader color. |
||
93 | */ |
||
94 | $ap_options = apply_filters( |
||
95 | 'audio_player_default_colors', |
||
96 | array( |
||
97 | "bg" => "0xF8F8F8", |
||
98 | "leftbg" => "0xEEEEEE", |
||
99 | "lefticon" => "0x666666", |
||
100 | "rightbg" => "0xCCCCCC", |
||
101 | "rightbghover" => "0x999999", |
||
102 | "righticon" => "0x666666", |
||
103 | "righticonhover" => "0xFFFFFF", |
||
104 | "text" => "0x666666", |
||
105 | "slider" => "0x666666", |
||
106 | "track" => "0xFFFFFF", |
||
107 | "border" => "0x666666", |
||
108 | "loader" => "0x9FFFB8" |
||
109 | ) ); |
||
110 | |||
111 | if ( ! isset( $ap_playerID ) ) { |
||
112 | $ap_playerID = 1; |
||
113 | } else { |
||
114 | $ap_playerID++; |
||
115 | } |
||
116 | |||
117 | if ( ! isset( $load_audio_script ) ) { |
||
118 | $load_audio_script = true; |
||
119 | } |
||
120 | |||
121 | // prep the audio files |
||
122 | $src = trim( $src, ' "' ); |
||
123 | $options = array(); |
||
124 | $data = preg_split( "/\|/", $src ); |
||
125 | $sound_file = $data[0]; |
||
126 | $sound_files = explode( ',', $sound_file ); |
||
127 | |||
128 | if ( is_ssl() ) { |
||
129 | for ( $i = 0; $i < count( $sound_files ); $i++ ) { |
||
130 | $sound_files[ $i ] = preg_replace( '#^http://([^.]+).files.wordpress.com/#', 'https://$1.files.wordpress.com/', $sound_files[ $i ] ); |
||
131 | } |
||
132 | } |
||
133 | |||
134 | $sound_files = array_map( 'trim', $sound_files ); |
||
135 | $sound_files = array_map( array( $this, 'rawurlencode_spaces' ), $sound_files ); |
||
136 | $sound_files = array_map( 'esc_url_raw', $sound_files ); // Ensure each is a valid URL |
||
137 | $num_files = count( $sound_files ); |
||
138 | $sound_types = array( |
||
139 | 'mp3' => 'mpeg', |
||
140 | 'wav' => 'wav', |
||
141 | 'ogg' => 'ogg', |
||
142 | 'oga' => 'ogg', |
||
143 | 'm4a' => 'mp4', |
||
144 | 'aac' => 'mp4', |
||
145 | 'webm' => 'webm' |
||
146 | ); |
||
147 | |||
148 | for ( $i = 1; $i < count( $data ); $i++ ) { |
||
149 | $pair = explode( "=", $data[$i] ); |
||
150 | if ( strtolower( $pair[0] ) != 'autostart' ) { |
||
151 | $options[$pair[0]] = $pair[1]; |
||
152 | } |
||
153 | } |
||
154 | |||
155 | // Merge runtime options to default colour options |
||
156 | // (runtime options overwrite default options) |
||
157 | foreach ( $ap_options as $key => $default ) { |
||
158 | if ( isset( $options[$key] ) ) { |
||
159 | if ( preg_match( '/^(0x)?[a-f0-9]{6}$/i', $default ) && !preg_match( '/^(0x)?[a-f0-9]{6}$/i', $options[$key] ) ) { |
||
160 | // Default is a hex color, but input is not |
||
161 | $options[$key] = $default; |
||
162 | } |
||
163 | } else { |
||
164 | $options[$key] = $default; |
||
165 | } |
||
166 | } |
||
167 | $options['soundFile'] = join( ',', $sound_files ); // Rebuild the option with our now sanitized data |
||
168 | $flash_vars = array(); |
||
169 | foreach ( $options as $key => $value ) { |
||
170 | $flash_vars[] = rawurlencode( $key ) . '=' . rawurlencode( $value ); |
||
171 | } |
||
172 | $flash_vars = implode( '&', $flash_vars ); |
||
173 | $flash_vars = esc_attr( $flash_vars ); |
||
174 | |||
175 | // extract some of the options to insert into the markup |
||
176 | if ( isset( $options['bgcolor'] ) && preg_match( '/^(0x)?[a-f0-9]{6}$/i', $options['bgcolor'] ) ) { |
||
177 | $bgcolor = preg_replace( '/^(0x)?/', '#', $options['bgcolor'] ); |
||
178 | $bgcolor = esc_attr( $bgcolor ); |
||
179 | } else { |
||
180 | $bgcolor = '#FFFFFF'; |
||
181 | } |
||
182 | |||
183 | if ( isset( $options['width'] ) ) { |
||
184 | $width = intval( $options['width'] ); |
||
185 | } else { |
||
186 | $width = 290; |
||
187 | } |
||
188 | |||
189 | $loop = ''; |
||
190 | $script_loop = 'false'; |
||
191 | if ( isset( $options['loop'] ) && 'yes' == $options['loop'] ) { |
||
192 | $script_loop = 'true'; |
||
193 | if ( 1 == $num_files ) { |
||
194 | $loop = 'loop'; |
||
195 | } |
||
196 | } |
||
197 | |||
198 | $volume = 0.6; |
||
199 | if ( isset( $options['initialvolume'] ) && |
||
200 | 0.0 < floatval( $options['initialvolume'] ) && |
||
201 | 100.0 >= floatval( $options['initialvolume'] ) ) { |
||
202 | |||
203 | $volume = floatval( $options['initialvolume'] )/100.0; |
||
204 | } |
||
205 | |||
206 | $file_artists = array_pad( array(), $num_files, '' ); |
||
207 | View Code Duplication | if ( isset( $options['artists'] ) ) { |
|
208 | $artists = preg_split( '/,/', $options['artists'] ); |
||
209 | foreach ( $artists as $i => $artist ) { |
||
210 | $file_artists[$i] = esc_html( $artist ) . ' - '; |
||
211 | } |
||
212 | } |
||
213 | |||
214 | // generate default titles |
||
215 | $file_titles = array(); |
||
216 | for ( $i = 0; $i < $num_files; $i++ ) { |
||
217 | $file_titles[] = 'Track #' . ($i+1); |
||
218 | } |
||
219 | |||
220 | // replace with real titles if they exist |
||
221 | View Code Duplication | if ( isset( $options['titles'] ) ) { |
|
222 | $titles = preg_split( '/,/', $options['titles'] ); |
||
223 | foreach ( $titles as $i => $title ) { |
||
224 | $file_titles[$i] = esc_html( $title ); |
||
225 | } |
||
226 | } |
||
227 | |||
228 | // fallback for the fallback, just a download link |
||
229 | $not_supported = ''; |
||
230 | foreach ( $sound_files as $sfile ) { |
||
231 | $not_supported .= sprintf( |
||
232 | __( 'Download: <a href="%s">%s</a><br />', 'jetpack' ), |
||
233 | esc_url( $sfile ), |
||
234 | esc_html( basename( $sfile ) ) ); |
||
235 | } |
||
236 | |||
237 | // HTML5 audio tag |
||
238 | $html5_audio = ''; |
||
239 | $all_mp3 = true; |
||
240 | $add_audio = true; |
||
241 | $num_good = 0; |
||
242 | $to_remove = array(); |
||
243 | foreach ( $sound_files as $i => $sfile ) { |
||
244 | $file_extension = pathinfo( $sfile, PATHINFO_EXTENSION ); |
||
245 | if ( ! preg_match( '/^(mp3|wav|ogg|oga|m4a|aac|webm)$/i', $file_extension ) ) { |
||
246 | $html5_audio .= '<!-- Audio shortcode unsupported audio format -->'; |
||
247 | if ( 1 == $num_files ) { |
||
248 | $html5_audio .= $not_supported; |
||
249 | } |
||
250 | |||
251 | $to_remove[] = $i; // make a note of the bad files |
||
252 | $all_mp3 = false; |
||
253 | continue; |
||
254 | } elseif ( ! preg_match( '/^mp3$/i', $file_extension ) ) { |
||
255 | $all_mp3 = false; |
||
256 | } |
||
257 | |||
258 | if ( 0 == $i ) { // only need one player |
||
259 | $html5_audio .= <<<AUDIO |
||
260 | <span id="wp-as-{$post_id}_{$ap_playerID}-container"> |
||
261 | <audio id='wp-as-{$post_id}_{$ap_playerID}' controls preload='none' $loop style='background-color:$bgcolor;width:{$width}px;'> |
||
262 | <span id="wp-as-{$post_id}_{$ap_playerID}-nope">$not_supported</span> |
||
263 | </audio> |
||
264 | </span> |
||
265 | <br /> |
||
266 | AUDIO; |
||
267 | } |
||
268 | $num_good++; |
||
269 | } |
||
270 | |||
271 | // player controls, if needed |
||
272 | if ( 1 < $num_files ) { |
||
273 | $html5_audio .= <<<CONTROLS |
||
274 | <span id='wp-as-{$post_id}_{$ap_playerID}-controls' style='display:none;'> |
||
275 | <a id='wp-as-{$post_id}_{$ap_playerID}-prev' |
||
276 | href='javascript:audioshortcode.prev_track( "{$post_id}_{$ap_playerID}" );' |
||
277 | style='font-size:1.5em;'>«</a> |
||
278 | | |
||
279 | <a id='wp-as-{$post_id}_{$ap_playerID}-next' |
||
280 | href='javascript:audioshortcode.next_track( "{$post_id}_{$ap_playerID}", true, $script_loop );' |
||
281 | style='font-size:1.5em;'>»</a> |
||
282 | </span> |
||
283 | CONTROLS; |
||
284 | } |
||
285 | $html5_audio .= "<span id='wp-as-{$post_id}_{$ap_playerID}-playing'></span>"; |
||
286 | |||
287 | /** |
||
288 | * Sets external resource URL. |
||
289 | * |
||
290 | * @module shortcodes |
||
291 | * |
||
292 | * @since 1.4.0 |
||
293 | * |
||
294 | * @param string $args URL of external resource. |
||
295 | * |
||
296 | */ |
||
297 | $swfurl = apply_filters( |
||
298 | 'jetpack_static_url', |
||
299 | set_url_scheme( "http://en.wordpress.com/wp-content/plugins/audio-player/player.swf" ) |
||
300 | ); |
||
301 | |||
302 | // all the fancy javascript is causing Google Reader to break, just include flash in GReader |
||
303 | // override html5 audio code w/ just not supported code |
||
304 | if ( is_feed() ) { |
||
305 | $html5_audio = $not_supported; |
||
306 | } |
||
307 | |||
308 | if ( $all_mp3 ) { |
||
309 | // process regular flash player, inserting HTML5 tags into object as fallback |
||
310 | $audio_tags = <<<FLASH |
||
311 | <object id='wp-as-{$post_id}_{$ap_playerID}-flash' type='application/x-shockwave-flash' data='$swfurl' width='$width' height='24'> |
||
312 | <param name='movie' value='$swfurl' /> |
||
313 | <param name='FlashVars' value='{$flash_vars}' /> |
||
314 | <param name='quality' value='high' /> |
||
315 | <param name='menu' value='false' /> |
||
316 | <param name='bgcolor' value='$bgcolor' /> |
||
317 | <param name='wmode' value='opaque' /> |
||
318 | $html5_audio |
||
319 | </object> |
||
320 | FLASH; |
||
321 | } else { // just HTML5 for non-mp3 versions |
||
322 | $audio_tags = $html5_audio; |
||
323 | } |
||
324 | |||
325 | // strip out all the bad files before it reaches .js |
||
326 | foreach ( $to_remove as $i ) { |
||
327 | array_splice( $sound_files, $i, 1 ); |
||
328 | array_splice( $file_artists, $i, 1 ); |
||
329 | array_splice( $file_titles, $i, 1 ); |
||
330 | } |
||
331 | |||
332 | // mashup the artist/titles for the script |
||
333 | $script_titles = array(); |
||
334 | for ( $i = 0; $i < $num_files; $i++ ) { |
||
335 | if ( isset( $file_artists[ $i ] ) && isset( $file_titles[ $i ] ) ) { |
||
336 | $script_titles[] = $file_artists[ $i ] . $file_titles[ $i ]; |
||
337 | } |
||
338 | } |
||
339 | |||
340 | // javacript to control audio |
||
341 | $script_files = json_encode( $sound_files ); |
||
342 | $script_titles = json_encode( $script_titles ); |
||
343 | $script = <<<SCRIPT |
||
344 | <script type='text/javascript'> |
||
345 | //<![CDATA[ |
||
346 | (function() { |
||
347 | var prep = function() { |
||
348 | if ( 'undefined' === typeof window.audioshortcode ) { return; } |
||
349 | audioshortcode.prep( |
||
350 | '{$post_id}_{$ap_playerID}', |
||
351 | $script_files, |
||
352 | $script_titles, |
||
353 | $volume, |
||
354 | $script_loop |
||
355 | ); |
||
356 | }; |
||
357 | if ( 'undefined' === typeof jQuery ) { |
||
358 | if ( document.addEventListener ) { |
||
359 | window.addEventListener( 'load', prep, false ); |
||
360 | } else if ( document.attachEvent ) { |
||
361 | window.attachEvent( 'onload', prep ); |
||
362 | } |
||
363 | } else { |
||
364 | jQuery(document).on( 'ready as-script-load', prep ); |
||
365 | } |
||
366 | })(); |
||
367 | //]]> |
||
368 | </script> |
||
369 | SCRIPT; |
||
370 | |||
371 | // add the special javascript, if needed |
||
372 | if ( 0 < $num_good && ! is_feed() ) { |
||
373 | $audio_tags .= $script; |
||
374 | } |
||
375 | |||
376 | return "<span style='text-align:left;display:block;'><p>$audio_tags</p></span>"; |
||
377 | } |
||
378 | |||
436 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.