Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | abstract class WPCOM_JSON_API_Post_v1_1_Endpoint extends WPCOM_JSON_API_Endpoint { |
||
| 4 | public $post_object_format = array( |
||
| 5 | // explicitly document and cast all output |
||
| 6 | 'ID' => '(int) The post ID.', |
||
| 7 | 'site_ID' => '(int) The site ID.', |
||
| 8 | 'author' => '(object>author) The author of the post.', |
||
| 9 | 'date' => "(ISO 8601 datetime) The post's creation time.", |
||
| 10 | 'modified' => "(ISO 8601 datetime) The post's most recent update time.", |
||
| 11 | 'title' => '(HTML) <code>context</code> dependent.', |
||
| 12 | 'URL' => '(URL) The full permalink URL to the post.', |
||
| 13 | 'short_URL' => '(URL) The wp.me short URL.', |
||
| 14 | 'content' => '(HTML) <code>context</code> dependent.', |
||
| 15 | 'excerpt' => '(HTML) <code>context</code> dependent.', |
||
| 16 | 'slug' => '(string) The name (slug) for the post, used in URLs.', |
||
| 17 | 'guid' => '(string) The GUID for the post.', |
||
| 18 | 'status' => array( |
||
| 19 | 'publish' => 'The post is published.', |
||
| 20 | 'draft' => 'The post is saved as a draft.', |
||
| 21 | 'pending' => 'The post is pending editorial approval.', |
||
| 22 | 'private' => 'The post is published privately', |
||
| 23 | 'future' => 'The post is scheduled for future publishing.', |
||
| 24 | 'trash' => 'The post is in the trash.', |
||
| 25 | 'auto-draft' => 'The post is a placeholder for a new post.', |
||
| 26 | ), |
||
| 27 | 'sticky' => '(bool) Is the post sticky?', |
||
| 28 | 'password' => '(string) The plaintext password protecting the post, or, more likely, the empty string if the post is not password protected.', |
||
| 29 | 'parent' => "(object>post_reference|false) A reference to the post's parent, if it has one.", |
||
| 30 | 'type' => "(string) The post's post_type. Post types besides post, page and revision need to be whitelisted using the <code>rest_api_allowed_post_types</code> filter.", |
||
| 31 | 'discussion' => '(object) Hash of discussion options for the post', |
||
| 32 | 'likes_enabled' => "(bool) Is the post open to likes?", |
||
| 33 | 'sharing_enabled' => "(bool) Should sharing buttons show on this post?", |
||
| 34 | 'like_count' => '(int) The number of likes for this post.', |
||
| 35 | 'i_like' => '(bool) Does the current user like this post?', |
||
| 36 | 'is_reblogged' => '(bool) Did the current user reblog this post?', |
||
| 37 | 'is_following' => '(bool) Is the current user following this blog?', |
||
| 38 | 'global_ID' => '(string) A unique WordPress.com-wide representation of a post.', |
||
| 39 | 'featured_image' => '(URL) The URL to the featured image for this post if it has one.', |
||
| 40 | 'post_thumbnail' => '(object>attachment) The attachment object for the featured image if it has one.', |
||
| 41 | 'format' => array(), // see constructor |
||
| 42 | 'geo' => '(object>geo|false)', |
||
| 43 | 'menu_order' => '(int) (Pages Only) The order pages should appear in.', |
||
| 44 | 'page_template' => '(string) (Pages Only) The page template this page is using.', |
||
| 45 | 'publicize_URLs' => '(array:URL) Array of Twitter and Facebook URLs published by this post.', |
||
| 46 | 'tags' => '(object:tag) Hash of tags (keyed by tag name) applied to the post.', |
||
| 47 | 'categories' => '(object:category) Hash of categories (keyed by category name) applied to the post.', |
||
| 48 | 'attachments' => '(object:attachment) Hash of post attachments (keyed by attachment ID). Returns the most recent 20 attachments. Use the `/sites/$site/media` endpoint to query the attachments beyond the default of 20 that are returned here.', |
||
| 49 | 'attachment_count' => '(int) The total number of attachments for this post. Use the `/sites/$site/media` endpoint to query the attachments beyond the default of 20 that are returned here.', |
||
| 50 | 'metadata' => '(array) Array of post metadata keys and values. All unprotected meta keys are available by default for read requests. Both unprotected and protected meta keys are available for authenticated requests with access. Protected meta keys can be made available with the <code>rest_api_allowed_public_metadata</code> filter.', |
||
| 51 | 'meta' => '(object) API result meta data', |
||
| 52 | 'capabilities' => '(object) List of post-specific permissions for the user; publish_post, edit_post, delete_post', |
||
| 53 | 'revisions' => '(array) List of post revision IDs. Only available for posts retrieved with context=edit.', |
||
| 54 | 'other_URLs' => '(object) List of URLs for this post. Permalink and slug suggestions.', |
||
| 55 | ); |
||
| 56 | |||
| 57 | // public $response_format =& $this->post_object_format; |
||
| 58 | |||
| 59 | View Code Duplication | function __construct( $args ) { |
|
| 60 | if ( is_array( $this->post_object_format ) && isset( $this->post_object_format['format'] ) ) { |
||
| 61 | $this->post_object_format['format'] = get_post_format_strings(); |
||
| 62 | } |
||
| 63 | if ( !$this->response_format ) { |
||
| 64 | $this->response_format =& $this->post_object_format; |
||
| 65 | } |
||
| 66 | parent::__construct( $args ); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Get a post by a specified field and value |
||
| 71 | * |
||
| 72 | * @param string $field |
||
| 73 | * @param string $field_value |
||
| 74 | * @param string $context Post use context (e.g. 'display') |
||
| 75 | * @return array Post |
||
| 76 | **/ |
||
| 77 | function get_post_by( $field, $field_value, $context = 'display' ) { |
||
| 78 | |||
| 79 | // validate input |
||
| 80 | if ( ! in_array( $field, array( 'ID', 'name' ) ) ) { |
||
| 81 | return new WP_Error( 'invalid_field', 'Invalid API FIELD', 400 ); |
||
| 82 | } |
||
| 83 | |||
| 84 | if ( ! in_array( $context, array( 'display', 'edit' ) ) ) { |
||
| 85 | return new WP_Error( 'invalid_context', 'Invalid API CONTEXT', 400 ); |
||
| 86 | } |
||
| 87 | |||
| 88 | View Code Duplication | if ( 'display' === $context ) { |
|
| 89 | $args = $this->query_args(); |
||
| 90 | if ( isset( $args['content_width'] ) && $args['content_width'] ) { |
||
| 91 | $GLOBALS['content_width'] = (int) $args['content_width']; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | View Code Duplication | if ( strpos( $_SERVER['HTTP_USER_AGENT'], 'wp-windows8' ) ) { |
|
| 96 | remove_shortcode( 'gallery', 'gallery_shortcode' ); |
||
| 97 | add_shortcode( 'gallery', array( &$this, 'win8_gallery_shortcode' ) ); |
||
| 98 | } |
||
| 99 | |||
| 100 | // fetch SAL post |
||
| 101 | $post = $this->get_sal_post_by( $field, $field_value, $context ); |
||
| 102 | |||
| 103 | if ( is_wp_error( $post ) ) { |
||
| 104 | return $post; |
||
| 105 | } |
||
| 106 | |||
| 107 | $GLOBALS['post'] = $post; |
||
| 108 | |||
| 109 | // TODO: not sure where this one should go |
||
|
0 ignored issues
–
show
Coding Style
Best Practice
introduced
by
Loading history...
|
|||
| 110 | if ( 'display' === $context ) { |
||
| 111 | setup_postdata( $post ); |
||
| 112 | } |
||
| 113 | |||
| 114 | $response = $this->render_response_keys( $post, $context, array_keys( $this->post_object_format ) ); |
||
| 115 | |||
| 116 | unset( $GLOBALS['post'] ); |
||
| 117 | |||
| 118 | return $response; |
||
| 119 | } |
||
| 120 | |||
| 121 | protected function get_sal_post_by( $field, $field_value, $context ) { |
||
| 122 | global $blog_id; |
||
| 123 | |||
| 124 | $site = $this->get_platform()->get_site( $blog_id ); |
||
| 125 | |||
| 126 | $post = ( $field === 'name' ) ? |
||
| 127 | $site->get_post_by_name( $field_value, $context ) : |
||
| 128 | $site->get_post_by_id( $field_value, $context ); |
||
| 129 | |||
| 130 | return $post; |
||
| 131 | } |
||
| 132 | |||
| 133 | private function render_response_keys( $post, $context, $keys ) { |
||
|
0 ignored issues
–
show
|
|||
| 134 | foreach ( $keys as $key ) { |
||
| 135 | switch ( $key ) { |
||
| 136 | case 'ID' : |
||
| 137 | // explicitly cast all output |
||
| 138 | $response[$key] = (int) $post->ID; |
||
|
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code. Let’s take a look at an example: foreach ($collection as $item) {
$myArray['foo'] = $item->getFoo();
if ($item->hasBar()) {
$myArray['bar'] = $item->getBar();
}
// do something with $myArray
}
As you can see in this example, the array This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop. Loading history...
|
|||
| 139 | break; |
||
| 140 | case 'site_ID' : |
||
| 141 | $response[$key] = $post->site->blog_id; |
||
|
0 ignored issues
–
show
The variable
$response does not seem to be defined for all execution paths leading up to this point.
If you define a variable conditionally, it can happen that it is not defined for all execution paths. Let’s take a look at an example: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined. Available Fixes
Loading history...
|
|||
| 142 | break; |
||
| 143 | case 'author' : |
||
| 144 | $response[$key] = $post->get_author(); |
||
| 145 | break; |
||
| 146 | case 'date' : |
||
| 147 | $response[$key] = $post->get_date(); |
||
| 148 | break; |
||
| 149 | case 'modified' : |
||
| 150 | $response[$key] = $post->get_modified_date(); |
||
| 151 | break; |
||
| 152 | case 'title' : |
||
| 153 | $response[$key] = $post->get_title(); |
||
| 154 | break; |
||
| 155 | case 'URL' : |
||
| 156 | $response[$key] = $post->get_url(); |
||
| 157 | break; |
||
| 158 | case 'short_URL' : |
||
| 159 | $response[$key] = $post->get_shortlink(); |
||
| 160 | break; |
||
| 161 | case 'content' : |
||
| 162 | $response[$key] = $post->get_content(); |
||
| 163 | break; |
||
| 164 | case 'excerpt' : |
||
| 165 | $response[$key] = $post->get_excerpt(); |
||
| 166 | break; |
||
| 167 | case 'status' : |
||
| 168 | $response[$key] = $post->get_status(); |
||
| 169 | break; |
||
| 170 | case 'sticky' : |
||
| 171 | $response[$key] = $post->is_sticky(); |
||
| 172 | break; |
||
| 173 | case 'slug' : |
||
| 174 | $response[$key] = $post->get_slug(); |
||
| 175 | break; |
||
| 176 | case 'guid' : |
||
| 177 | $response[$key] = $post->get_guid(); |
||
| 178 | break; |
||
| 179 | case 'password' : |
||
| 180 | $response[$key] = $post->get_password(); |
||
| 181 | break; |
||
| 182 | case 'parent' : // (object|false) |
||
| 183 | $response[$key] = $post->get_parent(); |
||
| 184 | break; |
||
| 185 | case 'type' : |
||
| 186 | $response[$key] = $post->get_type(); |
||
| 187 | break; |
||
| 188 | case 'discussion' : |
||
| 189 | $response[$key] = $post->get_discussion(); |
||
| 190 | break; |
||
| 191 | case 'likes_enabled' : |
||
| 192 | $response[$key] = $post->is_likes_enabled(); |
||
| 193 | break; |
||
| 194 | case 'sharing_enabled' : |
||
| 195 | $response[$key] = $post->is_sharing_enabled(); |
||
| 196 | break; |
||
| 197 | case 'like_count' : |
||
| 198 | $response[$key] = $post->get_like_count(); |
||
| 199 | break; |
||
| 200 | case 'i_like' : |
||
| 201 | $response[$key] = $post->is_liked(); |
||
| 202 | break; |
||
| 203 | case 'is_reblogged': |
||
| 204 | $response[$key] = $post->is_reblogged(); |
||
| 205 | break; |
||
| 206 | case 'is_following': |
||
| 207 | $response[$key] = $post->is_following(); |
||
| 208 | break; |
||
| 209 | case 'global_ID': |
||
| 210 | $response[$key] = $post->get_global_id(); |
||
| 211 | break; |
||
| 212 | case 'featured_image' : |
||
| 213 | $response[$key] = $post->get_featured_image(); |
||
| 214 | break; |
||
| 215 | case 'post_thumbnail' : |
||
| 216 | $response[$key] = $post->get_post_thumbnail(); |
||
| 217 | break; |
||
| 218 | case 'format' : |
||
| 219 | $response[$key] = $post->get_format(); |
||
| 220 | break; |
||
| 221 | case 'geo' : // (object|false) |
||
| 222 | $response[$key] = $post->get_geo(); |
||
| 223 | break; |
||
| 224 | case 'menu_order': |
||
| 225 | $response[$key] = $post->get_menu_order(); |
||
| 226 | break; |
||
| 227 | case 'page_template': |
||
| 228 | $response[$key] = $post->get_page_template(); |
||
| 229 | break; |
||
| 230 | case 'publicize_URLs' : |
||
| 231 | $response[$key] = $post->get_publicize_urls(); |
||
| 232 | break; |
||
| 233 | case 'tags' : |
||
| 234 | $response[$key] = $post->get_tags(); |
||
| 235 | break; |
||
| 236 | case 'categories': |
||
| 237 | $response[$key] = $post->get_categories(); |
||
| 238 | break; |
||
| 239 | case 'attachments': |
||
| 240 | list( $attachments, $attachment_count ) = $post->get_attachments_and_count(); |
||
| 241 | $response[$key] = $attachments; |
||
| 242 | $response['attachment_count'] = $attachment_count; |
||
| 243 | break; |
||
| 244 | case 'metadata' : // (array|false) |
||
| 245 | $response[$key] = $post->get_metadata(); |
||
| 246 | break; |
||
| 247 | case 'meta' : |
||
| 248 | $response[$key] = $post->get_meta(); |
||
| 249 | break; |
||
| 250 | case 'capabilities' : |
||
| 251 | $response[$key] = $post->get_current_user_capabilities(); |
||
| 252 | break; |
||
| 253 | case 'revisions' : |
||
| 254 | $revisions = $post->get_revisions(); |
||
| 255 | if ( $revisions ) { |
||
| 256 | $response[$key] = $revisions; |
||
| 257 | } |
||
| 258 | break; |
||
| 259 | case 'other_URLs' : |
||
| 260 | $response[$key] = $post->get_other_urls(); |
||
| 261 | break; |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | return $response; |
||
| 266 | } |
||
| 267 | |||
| 268 | // TODO: factor this out |
||
|
0 ignored issues
–
show
|
|||
| 269 | View Code Duplication | function get_blog_post( $blog_id, $post_id, $context = 'display' ) { |
|
| 270 | $blog_id = $this->api->get_blog_id( $blog_id ); |
||
| 271 | if ( !$blog_id || is_wp_error( $blog_id ) ) { |
||
| 272 | return $blog_id; |
||
| 273 | } |
||
| 274 | switch_to_blog( $blog_id ); |
||
| 275 | $post = $this->get_post_by( 'ID', $post_id, $context ); |
||
| 276 | restore_current_blog(); |
||
| 277 | return $post; |
||
| 278 | } |
||
| 279 | |||
| 280 | View Code Duplication | function win8_gallery_shortcode( $attr ) { |
|
| 281 | global $post; |
||
| 282 | |||
| 283 | static $instance = 0; |
||
| 284 | $instance++; |
||
| 285 | |||
| 286 | $output = ''; |
||
| 287 | |||
| 288 | // We're trusting author input, so let's at least make sure it looks like a valid orderby statement |
||
| 289 | if ( isset( $attr['orderby'] ) ) { |
||
| 290 | $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] ); |
||
| 291 | if ( !$attr['orderby'] ) |
||
| 292 | unset( $attr['orderby'] ); |
||
| 293 | } |
||
| 294 | |||
| 295 | extract( shortcode_atts( array( |
||
| 296 | 'order' => 'ASC', |
||
| 297 | 'orderby' => 'menu_order ID', |
||
| 298 | 'id' => $post->ID, |
||
| 299 | 'include' => '', |
||
| 300 | 'exclude' => '', |
||
| 301 | 'slideshow' => false |
||
| 302 | ), $attr, 'gallery' ) ); |
||
| 303 | |||
| 304 | // Custom image size and always use it |
||
| 305 | add_image_size( 'win8app-column', 480 ); |
||
| 306 | $size = 'win8app-column'; |
||
| 307 | |||
| 308 | $id = intval( $id ); |
||
| 309 | if ( 'RAND' === $order ) |
||
| 310 | $orderby = 'none'; |
||
| 311 | |||
| 312 | if ( !empty( $include ) ) { |
||
| 313 | $include = preg_replace( '/[^0-9,]+/', '', $include ); |
||
| 314 | $_attachments = get_posts( array( 'include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) ); |
||
| 315 | $attachments = array(); |
||
| 316 | foreach ( $_attachments as $key => $val ) { |
||
| 317 | $attachments[$val->ID] = $_attachments[$key]; |
||
| 318 | } |
||
| 319 | } elseif ( !empty( $exclude ) ) { |
||
| 320 | $exclude = preg_replace( '/[^0-9,]+/', '', $exclude ); |
||
| 321 | $attachments = get_children( array( 'post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) ); |
||
| 322 | } else { |
||
| 323 | $attachments = get_children( array( 'post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) ); |
||
| 324 | } |
||
| 325 | |||
| 326 | if ( ! empty( $attachments ) ) { |
||
| 327 | foreach ( $attachments as $id => $attachment ) { |
||
| 328 | $link = isset( $attr['link'] ) && 'file' === $attr['link'] ? wp_get_attachment_link( $id, $size, false, false ) : wp_get_attachment_link( $id, $size, true, false ); |
||
| 329 | |||
| 330 | if ( $captiontag && trim($attachment->post_excerpt) ) { |
||
| 331 | $output .= "<div class='wp-caption aligncenter'>$link |
||
| 332 | <p class='wp-caption-text'>" . wptexturize($attachment->post_excerpt) . "</p> |
||
| 333 | </div>"; |
||
| 334 | } else { |
||
| 335 | $output .= $link . ' '; |
||
| 336 | } |
||
| 337 | } |
||
| 338 | } |
||
| 339 | } |
||
| 340 | } |
||
| 341 |