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