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