| Conditions | 12 |
| Paths | 81 |
| Total Lines | 108 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 9 | ||
| 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 |
||
| 66 | static function get_post( $post_id, $allowed_post_types = null, $allowed_post_statuses = null ) { |
||
| 67 | require_once JETPACK__PLUGIN_DIR . 'sal/class.json-api-platform.php'; |
||
| 68 | $sal = wpcom_get_sal_platform(); |
||
| 69 | $site = $sal->get_site( get_current_blog_id() ); |
||
| 70 | $post_obj = $site->get_post_by_id( $post_id, 'display' ); |
||
| 71 | |||
| 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 | $allowed_post_statuses = self::get_synced_post_status(); |
||
| 79 | } |
||
| 80 | |||
| 81 | error_log( print_r( $post_obj, 1 ) ); |
||
| 82 | error_log( print_r( $allowed_post_types, 1 ) ); |
||
| 83 | error_log( print_r( $allowed_post_statuses, 1 ) ); |
||
| 84 | |||
| 85 | if ( ! in_array( $post_obj->get_type(), $allowed_post_types ) ) { |
||
| 86 | return false; |
||
| 87 | } |
||
| 88 | |||
| 89 | if ( ! in_array( $post_obj->get_status(), $allowed_post_statuses ) ) { |
||
| 90 | return false; |
||
| 91 | } |
||
| 92 | |||
| 93 | $post = $post_obj->to_array(); |
||
| 94 | |||
| 95 | // local optimizations |
||
| 96 | unset( |
||
| 97 | $post['post_password'], |
||
| 98 | $post['filter'], |
||
| 99 | $post['ancestors'], |
||
| 100 | $post['post_content_filtered'], |
||
| 101 | $post['to_ping'], |
||
| 102 | $post['pinged'] |
||
| 103 | ); |
||
| 104 | |||
| 105 | $post['post_is_public'] = $post_obj->is_public(); |
||
| 106 | $post['post_is_excluded_from_search'] = $post_obj->is_excluded_from_search(); |
||
| 107 | |||
| 108 | $post['tax'] = $post_obj->get_taxonomies(); |
||
| 109 | $post['meta'] = $post_obj->get_meta(); |
||
| 110 | |||
| 111 | $post['extra'] = array( |
||
| 112 | 'author' => get_the_author_meta( 'display_name', $post_obj->post_author ), |
||
| 113 | 'author_email' => get_the_author_meta( 'email', $post_obj->post_author ), |
||
| 114 | 'dont_email_post_to_subs' => get_post_meta( $post_obj->ID, '_jetpack_dont_email_post_to_subs', true ), |
||
| 115 | ); |
||
| 116 | |||
| 117 | if ( $attachment_id = get_post_thumbnail_id( $post_id ) ) { |
||
| 118 | $feature = wp_get_attachment_image_src( $attachment_id, 'large' ); |
||
| 119 | if ( ! empty( $feature[0] ) ) { |
||
| 120 | $post['extra']['featured_image'] = $feature[0]; |
||
| 121 | } |
||
| 122 | |||
| 123 | $attachment = get_post( $attachment_id ); |
||
| 124 | if ( ! empty( $attachment ) ) { |
||
| 125 | $metadata = wp_get_attachment_metadata( $attachment_id ); |
||
| 126 | |||
| 127 | $post['extra']['post_thumbnail'] = array( |
||
| 128 | 'ID' => (int) $attachment_id, |
||
| 129 | 'URL' => (string) wp_get_attachment_url( $attachment_id ), |
||
| 130 | 'guid' => (string) $attachment->guid, |
||
| 131 | 'mime_type' => (string) $attachment->post_mime_type, |
||
| 132 | 'width' => (int) isset( $metadata['width'] ) ? $metadata['width'] : 0, |
||
| 133 | 'height' => (int) isset( $metadata['height'] ) ? $metadata['height'] : 0, |
||
| 134 | ); |
||
| 135 | |||
| 136 | if ( isset( $metadata['duration'] ) ) { |
||
| 137 | $post['extra']['post_thumbnail'] = (int) $metadata['duration']; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Filters the Post Thumbnail information returned for a specific post. |
||
| 142 | * |
||
| 143 | * @since 3.3.0 |
||
| 144 | * |
||
| 145 | * @param array $post ['extra']['post_thumbnail'] { |
||
| 146 | * Array of details about the Post Thumbnail. |
||
| 147 | * @param int ID Post Thumbnail ID. |
||
| 148 | * @param string URL Post thumbnail URL. |
||
| 149 | * @param string guid Post thumbnail guid. |
||
| 150 | * @param string mime_type Post thumbnail mime type. |
||
| 151 | * @param int width Post thumbnail width. |
||
| 152 | * @param int height Post thumbnail height. |
||
| 153 | * } |
||
| 154 | */ |
||
| 155 | $post['extra']['post_thumbnail'] = (object) apply_filters( 'get_attachment', $post['extra']['post_thumbnail'] ); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | $post['permalink'] = get_permalink( $post_obj->ID ); |
||
| 160 | $post['shortlink'] = wp_get_shortlink( $post_obj->ID ); |
||
| 161 | /** |
||
| 162 | * Allow modules to send extra info on the sync post process. |
||
| 163 | * |
||
| 164 | * @since 2.8.0 |
||
| 165 | * |
||
| 166 | * @param array $args Array of custom data to attach to a post. |
||
| 167 | * @param Object $post_obj Object returned by get_post() for a given post ID. |
||
| 168 | */ |
||
| 169 | $post['module_custom_data'] = apply_filters( 'jetpack_sync_post_module_custom_data', array(), $post_obj ); |
||
| 170 | $post['module_custom_data']['cpt_publicizeable'] = post_type_supports( $post_obj->post_type, 'publicize' ) ? true : false; |
||
| 171 | |||
| 172 | return $post; |
||
| 173 | } |
||
| 174 | |||
| 176 |
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.