| Conditions | 20 |
| Paths | 12169 |
| Total Lines | 136 |
| Code Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 3 |
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 |
||
| 134 | static function get_post( $post_id, $allowed_post_types = array(), $allowed_post_statuses = array() ) { |
||
| 135 | $post_obj = get_post( $post_id ); |
||
| 136 | if ( ! $post_obj ) { |
||
| 137 | return false; |
||
| 138 | } |
||
| 139 | |||
| 140 | if ( is_null( $allowed_post_types ) ) { |
||
| 141 | $allowed_post_types = self::get_synced_post_types(); |
||
| 142 | } |
||
| 143 | if ( is_null( $allowed_post_types ) ) { |
||
| 144 | $allowed_post_statuses = self::get_synced_post_status(); |
||
| 145 | } |
||
| 146 | |||
| 147 | if ( ! in_array( $post_obj->post_type, $allowed_post_types ) ) { |
||
| 148 | return false; |
||
| 149 | } |
||
| 150 | |||
| 151 | if ( ! in_array( $post_obj->post_status, $allowed_post_statuses ) ) { |
||
| 152 | return false; |
||
| 153 | } |
||
| 154 | |||
| 155 | if ( is_callable( $post_obj, 'to_array' ) ) { |
||
| 156 | // WP >= 3.5 |
||
| 157 | $post = $post_obj->to_array(); |
||
| 158 | } else { |
||
| 159 | // WP < 3.5 |
||
| 160 | $post = get_object_vars( $post_obj ); |
||
| 161 | } |
||
| 162 | |||
| 163 | if ( 0 < strlen( $post['post_password'] ) ) { |
||
| 164 | $post['post_password'] = 'auto-' . wp_generate_password( 10, false ); // We don't want the real password. Just pass something random. |
||
| 165 | } |
||
| 166 | |||
| 167 | // local optimizations |
||
| 168 | unset( |
||
| 169 | $post['filter'], |
||
| 170 | $post['ancestors'], |
||
| 171 | $post['post_content_filtered'], |
||
| 172 | $post['to_ping'], |
||
| 173 | $post['pinged'] |
||
| 174 | ); |
||
| 175 | |||
| 176 | if ( self::is_post_public( $post ) ) { |
||
| 177 | $post['post_is_public'] = Jetpack_Options::get_option( 'public' ); |
||
| 178 | } else { |
||
| 179 | //obscure content |
||
| 180 | $post['post_content'] = ''; |
||
| 181 | $post['post_excerpt'] = ''; |
||
| 182 | $post['post_is_public'] = false; |
||
| 183 | } |
||
| 184 | $post_type_obj = get_post_type_object( $post['post_type'] ); |
||
| 185 | $post['post_is_excluded_from_search'] = $post_type_obj->exclude_from_search; |
||
| 186 | |||
| 187 | $post['tax'] = array(); |
||
| 188 | $taxonomies = get_object_taxonomies( $post_obj ); |
||
| 189 | foreach ( $taxonomies as $taxonomy ) { |
||
| 190 | $terms = get_object_term_cache( $post_obj->ID, $taxonomy ); |
||
| 191 | if ( empty( $terms ) ) { |
||
| 192 | $terms = wp_get_object_terms( $post_obj->ID, $taxonomy ); |
||
| 193 | } |
||
| 194 | $term_names = array(); |
||
| 195 | foreach ( $terms as $term ) { |
||
| 196 | $term_names[] = $term->name; |
||
| 197 | } |
||
| 198 | $post['tax'][ $taxonomy ] = $term_names; |
||
| 199 | } |
||
| 200 | |||
| 201 | $meta = get_post_meta( $post_obj->ID, false ); |
||
| 202 | $post['meta'] = array(); |
||
| 203 | foreach ( $meta as $key => $value ) { |
||
| 204 | $post['meta'][ $key ] = array_map( 'maybe_unserialize', $value ); |
||
| 205 | } |
||
| 206 | |||
| 207 | $post['extra'] = array( |
||
| 208 | 'author' => get_the_author_meta( 'display_name', $post_obj->post_author ), |
||
| 209 | 'author_email' => get_the_author_meta( 'email', $post_obj->post_author ), |
||
| 210 | 'dont_email_post_to_subs' => get_post_meta( $post_obj->ID, '_jetpack_dont_email_post_to_subs', true ), |
||
| 211 | ); |
||
| 212 | |||
| 213 | if ( $attachment_id = get_post_thumbnail_id( $post_id ) ) { |
||
| 214 | $feature = wp_get_attachment_image_src( $attachment_id, 'large' ); |
||
| 215 | if ( ! empty( $feature[0] ) ) { |
||
| 216 | $post['extra']['featured_image'] = $feature[0]; |
||
| 217 | } |
||
| 218 | |||
| 219 | $attachment = get_post( $attachment_id ); |
||
| 220 | if ( ! empty( $attachment ) ) { |
||
| 221 | $metadata = wp_get_attachment_metadata( $attachment_id ); |
||
| 222 | |||
| 223 | $post['extra']['post_thumbnail'] = array( |
||
| 224 | 'ID' => (int) $attachment_id, |
||
| 225 | 'URL' => (string) wp_get_attachment_url( $attachment_id ), |
||
| 226 | 'guid' => (string) $attachment->guid, |
||
| 227 | 'mime_type' => (string) $attachment->post_mime_type, |
||
| 228 | 'width' => (int) isset( $metadata['width'] ) ? $metadata['width'] : 0, |
||
| 229 | 'height' => (int) isset( $metadata['height'] ) ? $metadata['height'] : 0, |
||
| 230 | ); |
||
| 231 | |||
| 232 | if ( isset( $metadata['duration'] ) ) { |
||
| 233 | $post['extra']['post_thumbnail'] = (int) $metadata['duration']; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Filters the Post Thumbnail information returned for a specific post. |
||
| 238 | * |
||
| 239 | * @since 3.3.0 |
||
| 240 | * |
||
| 241 | * @param array $post ['extra']['post_thumbnail'] { |
||
| 242 | * Array of details about the Post Thumbnail. |
||
| 243 | * @param int ID Post Thumbnail ID. |
||
| 244 | * @param string URL Post thumbnail URL. |
||
| 245 | * @param string guid Post thumbnail guid. |
||
| 246 | * @param string mime_type Post thumbnail mime type. |
||
| 247 | * @param int width Post thumbnail width. |
||
| 248 | * @param int height Post thumbnail height. |
||
| 249 | * } |
||
| 250 | */ |
||
| 251 | $post['extra']['post_thumbnail'] = (object) apply_filters( 'get_attachment', $post['extra']['post_thumbnail'] ); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | $post['permalink'] = get_permalink( $post_obj->ID ); |
||
| 256 | $post['shortlink'] = wp_get_shortlink( $post_obj->ID ); |
||
| 257 | /** |
||
| 258 | * Allow modules to send extra info on the sync post process. |
||
| 259 | * |
||
| 260 | * @since 2.8.0 |
||
| 261 | * |
||
| 262 | * @param array $args Array of custom data to attach to a post. |
||
| 263 | * @param Object $post_obj Object returned by get_post() for a given post ID. |
||
| 264 | */ |
||
| 265 | $post['module_custom_data'] = apply_filters( 'jetpack_sync_post_module_custom_data', array(), $post_obj ); |
||
| 266 | $post['module_custom_data']['cpt_publicizeable'] = post_type_supports( $post_obj->post_type, 'publicize' ) ? true : false; |
||
| 267 | |||
| 268 | return $post; |
||
| 269 | } |
||
| 270 | |||
| 291 |
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.