| Conditions | 20 |
| Paths | 12169 |
| Total Lines | 136 |
| Code Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 8 | ||
| 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 |
||
| 70 | static function get_post( $post_id, $allowed_post_types = array(), $allowed_post_statuses = array() ) { |
||
| 71 | $post_obj = get_post( $post_id ); |
||
| 72 | if ( ! $post_obj ) { |
||
| 73 | return false; |
||
| 74 | } |
||
| 75 | |||
| 76 | if ( is_null( $allowed_post_types ) ) { |
||
| 77 | $allowed_post_types = self::get_synced_post_types(); |
||
| 78 | } |
||
| 79 | if ( is_null( $allowed_post_types ) ) { |
||
| 80 | $allowed_post_statuses = self::get_synced_post_status(); |
||
| 81 | } |
||
| 82 | |||
| 83 | if ( ! in_array( $post_obj->post_type, $allowed_post_types ) ) { |
||
| 84 | return false; |
||
| 85 | } |
||
| 86 | |||
| 87 | if ( ! in_array( $post_obj->post_status, $allowed_post_statuses ) ) { |
||
| 88 | return false; |
||
| 89 | } |
||
| 90 | |||
| 91 | if ( is_callable( $post_obj, 'to_array' ) ) { |
||
| 92 | // WP >= 3.5 |
||
| 93 | $post = $post_obj->to_array(); |
||
| 94 | } else { |
||
| 95 | // WP < 3.5 |
||
| 96 | $post = get_object_vars( $post_obj ); |
||
| 97 | } |
||
| 98 | |||
| 99 | if ( 0 < strlen( $post['post_password'] ) ) { |
||
| 100 | $post['post_password'] = 'auto-' . wp_generate_password( 10, false ); // We don't want the real password. Just pass something random. |
||
| 101 | } |
||
| 102 | |||
| 103 | // local optimizations |
||
| 104 | unset( |
||
| 105 | $post['filter'], |
||
| 106 | $post['ancestors'], |
||
| 107 | $post['post_content_filtered'], |
||
| 108 | $post['to_ping'], |
||
| 109 | $post['pinged'] |
||
| 110 | ); |
||
| 111 | |||
| 112 | if ( self::is_post_public( $post ) ) { |
||
| 113 | $post['post_is_public'] = Jetpack_Options::get_option( 'public' ); |
||
| 114 | } else { |
||
| 115 | //obscure content |
||
| 116 | $post['post_content'] = ''; |
||
| 117 | $post['post_excerpt'] = ''; |
||
| 118 | $post['post_is_public'] = false; |
||
| 119 | } |
||
| 120 | $post_type_obj = get_post_type_object( $post['post_type'] ); |
||
| 121 | $post['post_is_excluded_from_search'] = $post_type_obj->exclude_from_search; |
||
| 122 | |||
| 123 | $post['tax'] = array(); |
||
| 124 | $taxonomies = get_object_taxonomies( $post_obj ); |
||
| 125 | foreach ( $taxonomies as $taxonomy ) { |
||
| 126 | $terms = get_object_term_cache( $post_obj->ID, $taxonomy ); |
||
| 127 | if ( empty( $terms ) ) { |
||
| 128 | $terms = wp_get_object_terms( $post_obj->ID, $taxonomy ); |
||
| 129 | } |
||
| 130 | $term_names = array(); |
||
| 131 | foreach ( $terms as $term ) { |
||
| 132 | $term_names[] = $term->name; |
||
| 133 | } |
||
| 134 | $post['tax'][ $taxonomy ] = $term_names; |
||
| 135 | } |
||
| 136 | |||
| 137 | $meta = get_post_meta( $post_obj->ID, false ); |
||
| 138 | $post['meta'] = array(); |
||
| 139 | foreach ( $meta as $key => $value ) { |
||
| 140 | $post['meta'][ $key ] = array_map( 'maybe_unserialize', $value ); |
||
| 141 | } |
||
| 142 | |||
| 143 | $post['extra'] = array( |
||
| 144 | 'author' => get_the_author_meta( 'display_name', $post_obj->post_author ), |
||
| 145 | 'author_email' => get_the_author_meta( 'email', $post_obj->post_author ), |
||
| 146 | 'dont_email_post_to_subs' => get_post_meta( $post_obj->ID, '_jetpack_dont_email_post_to_subs', true ), |
||
| 147 | ); |
||
| 148 | |||
| 149 | if ( $attachment_id = get_post_thumbnail_id( $post_id ) ) { |
||
| 150 | $feature = wp_get_attachment_image_src( $attachment_id, 'large' ); |
||
| 151 | if ( ! empty( $feature[0] ) ) { |
||
| 152 | $post['extra']['featured_image'] = $feature[0]; |
||
| 153 | } |
||
| 154 | |||
| 155 | $attachment = get_post( $attachment_id ); |
||
| 156 | if ( ! empty( $attachment ) ) { |
||
| 157 | $metadata = wp_get_attachment_metadata( $attachment_id ); |
||
| 158 | |||
| 159 | $post['extra']['post_thumbnail'] = array( |
||
| 160 | 'ID' => (int) $attachment_id, |
||
| 161 | 'URL' => (string) wp_get_attachment_url( $attachment_id ), |
||
| 162 | 'guid' => (string) $attachment->guid, |
||
| 163 | 'mime_type' => (string) $attachment->post_mime_type, |
||
| 164 | 'width' => (int) isset( $metadata['width'] ) ? $metadata['width'] : 0, |
||
| 165 | 'height' => (int) isset( $metadata['height'] ) ? $metadata['height'] : 0, |
||
| 166 | ); |
||
| 167 | |||
| 168 | if ( isset( $metadata['duration'] ) ) { |
||
| 169 | $post['extra']['post_thumbnail'] = (int) $metadata['duration']; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Filters the Post Thumbnail information returned for a specific post. |
||
| 174 | * |
||
| 175 | * @since 3.3.0 |
||
| 176 | * |
||
| 177 | * @param array $post ['extra']['post_thumbnail'] { |
||
| 178 | * Array of details about the Post Thumbnail. |
||
| 179 | * @param int ID Post Thumbnail ID. |
||
| 180 | * @param string URL Post thumbnail URL. |
||
| 181 | * @param string guid Post thumbnail guid. |
||
| 182 | * @param string mime_type Post thumbnail mime type. |
||
| 183 | * @param int width Post thumbnail width. |
||
| 184 | * @param int height Post thumbnail height. |
||
| 185 | * } |
||
| 186 | */ |
||
| 187 | $post['extra']['post_thumbnail'] = (object) apply_filters( 'get_attachment', $post['extra']['post_thumbnail'] ); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | $post['permalink'] = get_permalink( $post_obj->ID ); |
||
| 192 | $post['shortlink'] = wp_get_shortlink( $post_obj->ID ); |
||
| 193 | /** |
||
| 194 | * Allow modules to send extra info on the sync post process. |
||
| 195 | * |
||
| 196 | * @since 2.8.0 |
||
| 197 | * |
||
| 198 | * @param array $args Array of custom data to attach to a post. |
||
| 199 | * @param Object $post_obj Object returned by get_post() for a given post ID. |
||
| 200 | */ |
||
| 201 | $post['module_custom_data'] = apply_filters( 'jetpack_sync_post_module_custom_data', array(), $post_obj ); |
||
| 202 | $post['module_custom_data']['cpt_publicizeable'] = post_type_supports( $post_obj->post_type, 'publicize' ) ? true : false; |
||
| 203 | |||
| 204 | return $post; |
||
| 205 | } |
||
| 206 | |||
| 227 |
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.