Conditions | 14 |
Paths | 100 |
Total Lines | 103 |
Code Lines | 47 |
Lines | 6 |
Ratio | 5.83 % |
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 |
||
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' ); |
||
194 |