Conditions | 42 |
Paths | 968 |
Total Lines | 199 |
Code Lines | 135 |
Lines | 10 |
Ratio | 5.03 % |
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 |
||
9 | static function get( $post_id, $blog_id = 0, $args = array() ) { |
||
10 | $defaults = array( |
||
11 | 'max_words' => 16, |
||
12 | 'max_chars' => 256, |
||
13 | ); |
||
14 | $args = wp_parse_args( $args, $defaults ); |
||
15 | |||
16 | $switched = false; |
||
17 | if ( !empty( $blog_id ) && $blog_id != get_current_blog_id() && function_exists( 'switch_to_blog' ) ) { |
||
18 | switch_to_blog( $blog_id ); |
||
19 | $switched = true; |
||
20 | } else { |
||
21 | $blog_id = get_current_blog_id(); |
||
22 | } |
||
23 | |||
24 | if ( ! class_exists( 'Jetpack_Media_Meta_Extractor' ) ) { |
||
25 | jetpack_require_lib( 'class.media-extractor' ); |
||
26 | } |
||
27 | |||
28 | $post = get_post( $post_id ); |
||
29 | $permalink = get_permalink( $post_id ); |
||
30 | |||
31 | $return = array( |
||
32 | 'type' => 'standard', |
||
33 | 'permalink' => $permalink, |
||
34 | 'image' => '', |
||
35 | 'excerpt' => '', |
||
36 | 'word_count' => 0, |
||
37 | 'secure' => array( |
||
38 | 'image' => '', |
||
39 | ), |
||
40 | 'count' => array( |
||
41 | 'image' => 0, |
||
42 | 'video' => 0, |
||
43 | 'word' => 0, |
||
44 | 'link' => 0, |
||
45 | ), |
||
46 | ); |
||
47 | |||
48 | if ( empty( $post->post_password ) ) { |
||
49 | $return['excerpt'] = self::get_excerpt( $post->post_content, $post->post_excerpt, $args['max_words'], $args['max_chars'] ); |
||
50 | $return['count']['word'] = self::get_word_count( $post->post_content ); |
||
51 | $return['count']['word_remaining'] = self::get_word_remaining_count( $post->post_content, $return['excerpt'] ); |
||
52 | $return['count']['link'] = self::get_link_count( $post->post_content ); |
||
53 | } |
||
54 | |||
55 | $extract = Jetpack_Media_Meta_Extractor::extract( $blog_id, $post_id, Jetpack_Media_Meta_Extractor::ALL ); |
||
56 | |||
57 | if ( empty( $extract['has'] ) ) |
||
58 | return $return; |
||
59 | |||
60 | // Prioritize [some] video embeds |
||
61 | if ( !empty( $extract['has']['shortcode'] ) ) { |
||
62 | foreach ( $extract['shortcode'] as $type => $data ) { |
||
63 | switch ( $type ) { |
||
64 | case 'wpvideo': |
||
65 | if ( 0 == $return['count']['video'] ) { |
||
66 | $return['type'] = 'video'; |
||
67 | $return['video'] = esc_url_raw( 'http://s0.videopress.com/player.swf?guid=' . $extract['shortcode']['wpvideo']['id'][0] . '&isDynamicSeeking=true' ); |
||
68 | $return['image'] = self::get_video_poster( 'videopress', $extract['shortcode']['wpvideo']['id'][0] ); |
||
69 | $return['secure']['video'] = preg_replace( '@http://[^\.]+.videopress.com/@', 'https://v0.wordpress.com/', $return['video'] ); |
||
70 | $return['secure']['image'] = str_replace( 'http://videos.videopress.com', 'https://videos.files.wordpress.com', $return['image'] ); |
||
71 | } |
||
72 | $return['count']['video']++; |
||
73 | break; |
||
74 | case 'youtube': |
||
75 | if ( 0 == $return['count']['video'] ) { |
||
76 | $return['type'] = 'video'; |
||
77 | $return['video'] = esc_url_raw( 'http://www.youtube.com/watch?feature=player_embedded&v=' . $extract['shortcode']['youtube']['id'][0] ); |
||
78 | $return['image'] = self::get_video_poster( 'youtube', $extract['shortcode']['youtube']['id'][0] ); |
||
79 | $return['secure']['video'] = self::https( $return['video'] ); |
||
80 | $return['secure']['image'] = self::https( $return['image'] ); |
||
81 | } |
||
82 | $return['count']['video']++; |
||
83 | break; |
||
84 | case 'vimeo': |
||
85 | if ( 0 == $return['count']['video'] ) { |
||
86 | $return['type'] = 'video'; |
||
87 | $return['video'] = esc_url_raw( 'http://vimeo.com/' . $extract['shortcode']['vimeo']['id'][0] ); |
||
88 | $return['secure']['video'] = self::https( $return['video'] ); |
||
89 | |||
90 | $poster_image = get_post_meta( $post_id, 'vimeo_poster_image', true ); |
||
91 | View Code Duplication | if ( !empty( $poster_image ) ) { |
|
92 | $return['image'] = $poster_image; |
||
93 | $poster_url_parts = parse_url( $poster_image ); |
||
94 | $return['secure']['image'] = 'https://secure-a.vimeocdn.com' . $poster_url_parts['path']; |
||
95 | } |
||
96 | } |
||
97 | $return['count']['video']++; |
||
98 | break; |
||
99 | } |
||
100 | } |
||
101 | |||
102 | } |
||
103 | |||
104 | if ( !empty( $extract['has']['embed'] ) ) { |
||
105 | foreach( $extract['embed']['url'] as $embed ) { |
||
106 | if ( preg_match( '/((youtube|vimeo|dailymotion)\.com|youtu.be)/', $embed ) ) { |
||
107 | if ( 0 == $return['count']['video'] ) { |
||
108 | $return['type'] = 'video'; |
||
109 | $return['video'] = 'http://' . $embed; |
||
110 | $return['secure']['video'] = self::https( $return['video'] ); |
||
111 | if ( false !== strpos( $embed, 'youtube' ) ) { |
||
112 | $return['image'] = self::get_video_poster( 'youtube', jetpack_get_youtube_id( $return['video'] ) ); |
||
113 | $return['secure']['image'] = self::https( $return['image'] ); |
||
114 | } else if ( false !== strpos( $embed, 'youtu.be' ) ) { |
||
115 | $youtube_id = jetpack_get_youtube_id( $return['video'] ); |
||
116 | $return['video'] = 'http://youtube.com/watch?v=' . $youtube_id . '&feature=youtu.be'; |
||
117 | $return['secure']['video'] = self::https( $return['video'] ); |
||
118 | $return['image'] = self::get_video_poster( 'youtube', jetpack_get_youtube_id( $return['video'] ) ); |
||
119 | $return['secure']['image'] = self::https( $return['image'] ); |
||
120 | } else if ( false !== strpos( $embed, 'vimeo' ) ) { |
||
121 | $poster_image = get_post_meta( $post_id, 'vimeo_poster_image', true ); |
||
122 | View Code Duplication | if ( !empty( $poster_image ) ) { |
|
123 | $return['image'] = $poster_image; |
||
124 | $poster_url_parts = parse_url( $poster_image ); |
||
125 | $return['secure']['image'] = 'https://secure-a.vimeocdn.com' . $poster_url_parts['path']; |
||
126 | } |
||
127 | } else if ( false !== strpos( $embed, 'dailymotion' ) ) { |
||
128 | $return['image'] = str_replace( 'dailymotion.com/video/','dailymotion.com/thumbnail/video/', $embed ); |
||
129 | $return['image'] = parse_url( $return['image'], PHP_URL_SCHEME ) === null ? 'http://' . $return['image'] : $return['image']; |
||
130 | $return['secure']['image'] = self::https( $return['image'] ); |
||
131 | } |
||
132 | |||
133 | } |
||
134 | $return['count']['video']++; |
||
135 | } |
||
136 | } |
||
137 | } |
||
138 | |||
139 | // Do we really want to make the video the primary focus of the post? |
||
140 | if ( 'video' == $return['type'] ) { |
||
141 | $content = wpautop( strip_tags( $post->post_content ) ); |
||
142 | $paragraphs = explode( '</p>', $content ); |
||
143 | $number_of_paragraphs = 0; |
||
144 | |||
145 | foreach ( $paragraphs as $i => $paragraph ) { |
||
146 | // Don't include blank lines as a paragraph |
||
147 | if ( '' == trim( $paragraph ) ) { |
||
148 | unset( $paragraphs[$i] ); |
||
149 | continue; |
||
150 | } |
||
151 | $number_of_paragraphs++; |
||
152 | } |
||
153 | |||
154 | $number_of_paragraphs = $number_of_paragraphs - $return['count']['video']; // subtract amount for videos.. |
||
155 | |||
156 | // More than 2 paragraph? The video is not the primary focus so we can do some more analysis |
||
157 | if ( $number_of_paragraphs > 2 ) |
||
158 | $return['type'] = 'standard'; |
||
159 | } |
||
160 | |||
161 | // If we don't have any prioritized embed... |
||
162 | if ( 'standard' == $return['type'] ) { |
||
163 | if ( ( ! empty( $extract['has']['gallery'] ) || ! empty( $extract['shortcode']['gallery']['count'] ) ) && ! empty( $extract['image'] ) ) { |
||
164 | //... Then we prioritize galleries first (multiple images returned) |
||
165 | $return['type'] = 'gallery'; |
||
166 | $return['images'] = $extract['image']; |
||
167 | foreach ( $return['images'] as $image ) { |
||
168 | $return['secure']['images'][] = array( 'url' => self::ssl_img( $image['url'] ) ); |
||
169 | $return['count']['image']++; |
||
170 | } |
||
171 | } else if ( ! empty( $extract['has']['image'] ) ) { |
||
172 | // ... Or we try and select a single image that would make sense |
||
173 | $content = wpautop( strip_tags( $post->post_content ) ); |
||
174 | $paragraphs = explode( '</p>', $content ); |
||
175 | $number_of_paragraphs = 0; |
||
176 | |||
177 | foreach ( $paragraphs as $i => $paragraph ) { |
||
178 | // Don't include 'actual' captions as a paragraph |
||
179 | if ( false !== strpos( $paragraph, '[caption' ) ) { |
||
180 | unset( $paragraphs[$i] ); |
||
181 | continue; |
||
182 | } |
||
183 | // Don't include blank lines as a paragraph |
||
184 | if ( '' == trim( $paragraph ) ) { |
||
185 | unset( $paragraphs[$i] ); |
||
186 | continue; |
||
187 | } |
||
188 | $number_of_paragraphs++; |
||
189 | } |
||
190 | |||
191 | $return['image'] = $extract['image'][0]['url']; |
||
192 | $return['secure']['image'] = self::ssl_img( $return['image'] ); |
||
193 | $return['count']['image']++; |
||
194 | |||
195 | if ( $number_of_paragraphs <= 2 && 1 == count( $extract['image'] ) ) { |
||
196 | // If we have lots of text or images, let's not treat it as an image post, but return its first image |
||
197 | $return['type'] = 'image'; |
||
|
|||
198 | } |
||
199 | } |
||
200 | } |
||
201 | |||
202 | if ( $switched ) { |
||
203 | restore_current_blog(); |
||
204 | } |
||
205 | |||
206 | return $return; |
||
207 | } |
||
208 | |||
282 |
This check looks for improperly formatted assignments.
Every assignment must have exactly one space before and one space after the equals operator.
To illustrate:
will have no issues, while
will report issues in lines 1 and 2.