Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
69.92% covered (warning)
69.92%
251 / 359
33.33% covered (danger)
33.33%
3 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_JSON_API_Post_Endpoint
70.31% covered (warning)
70.31%
251 / 357
33.33% covered (danger)
33.33%
3 / 9
628.31
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 the_password_form
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_post_by
77.63% covered (warning)
77.63%
229 / 295
0.00% covered (danger)
0.00%
0 / 1
263.10
 get_the_post_content_for_display
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 get_blog_post
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 find_featured_media
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 get_attachment
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
 get_current_user_capabilities
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 get_post_id_by_name
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
42
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3if ( ! defined( 'ABSPATH' ) ) {
4    exit( 0 );
5}
6
7/**
8 * Post Endpoint class.
9 */
10abstract class WPCOM_JSON_API_Post_Endpoint extends WPCOM_JSON_API_Endpoint {
11    /**
12     * Post object format.
13     *
14     * @var array
15     */
16    public $post_object_format = array(
17        // explicitly document and cast all output
18        'ID'               => '(int) The post ID.',
19        'site_ID'          => '(int) The site ID.',
20        'author'           => '(object>author) The author of the post.',
21        'date'             => "(ISO 8601 datetime) The post's creation time.",
22        'modified'         => "(ISO 8601 datetime) The post's most recent update time.",
23        'title'            => '(HTML) <code>context</code> dependent.',
24        'URL'              => '(URL) The full permalink URL to the post.',
25        'short_URL'        => '(URL) The wp.me short URL.',
26        'content'          => '(HTML) <code>context</code> dependent.',
27        'excerpt'          => '(HTML) <code>context</code> dependent.',
28        'slug'             => '(string) The name (slug) for the post, used in URLs.',
29        'guid'             => '(string) The GUID for the post.',
30        'status'           => array(
31            'publish'    => 'The post is published.',
32            'draft'      => 'The post is saved as a draft.',
33            'pending'    => 'The post is pending editorial approval.',
34            'private'    => 'The post is published privately',
35            'future'     => 'The post is scheduled for future publishing.',
36            'trash'      => 'The post is in the trash.',
37            'auto-draft' => 'The post is a placeholder for a new post.',
38        ),
39        'sticky'           => '(bool) Is the post sticky?',
40        'password'         => '(string) The plaintext password protecting the post, or, more likely, the empty string if the post is not password protected.',
41        'parent'           => "(object>post_reference|false) A reference to the post's parent, if it has one.",
42        '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.",
43        'comments_open'    => '(bool) Is the post open for comments?',
44        'pings_open'       => '(bool) Is the post open for pingbacks, trackbacks?',
45        'likes_enabled'    => '(bool) Is the post open to likes?',
46        'sharing_enabled'  => '(bool) Should sharing buttons show on this post?',
47        'comment_count'    => '(int) The number of comments for this post.',
48        'like_count'       => '(int) The number of likes for this post.',
49        'i_like'           => '(bool) Does the current user like this post?',
50        'is_reblogged'     => '(bool) Did the current user reblog this post?',
51        'is_following'     => '(bool) Is the current user following this blog?',
52        'global_ID'        => '(string) A unique WordPress.com-wide representation of a post.',
53        'featured_image'   => '(URL) The URL to the featured image for this post if it has one.',
54        'post_thumbnail'   => '(object>attachment) The attachment object for the featured image if it has one.',
55        'format'           => array(), // see constructor
56        'geo'              => '(object>geo|false)',
57        'menu_order'       => '(int) (Pages Only) The order pages should appear in.',
58        'publicize_URLs'   => '(array:URL) Array of Facebook URLs published by this post.',
59        'tags'             => '(object:tag) Hash of tags (keyed by tag name) applied to the post.',
60        'categories'       => '(object:category) Hash of categories (keyed by category name) applied to the post.',
61        'attachments'      => '(object:attachment) Hash of post attachments (keyed by attachment ID).',
62        'metadata'         => '(array) Array of post metadata keys and values. Users who can edit the post get its unprotected meta keys. Protected meta keys (those starting with an underscore) are returned only when the user can edit that key, which requires an <code>auth_callback</code> registered with <code>register_meta()</code> or an <code>auth_post_meta_{$meta_key}</code> filter. Keys allowed by the <code>rest_api_allowed_public_metadata</code> filter, plus <code>_thumbnail_id</code> and keys starting with <code>geo_</code> or <code>_wpas_</code>, are returned to anyone who can read the post.',
63        'meta'             => '(object) API result meta data',
64        'current_user_can' => '(object) List of permissions. Note, deprecated in favor of `capabilities`',
65        'capabilities'     => '(object) List of post-specific permissions for the user; publish_post, edit_post, delete_post',
66    );
67
68    /**
69     * Constructor function.
70     *
71     * @param string|array|object $args — Args.
72     */
73    public function __construct( $args ) {
74        if ( is_array( $this->post_object_format ) && isset( $this->post_object_format['format'] ) ) {
75            $this->post_object_format['format'] = get_post_format_strings();
76        }
77        if ( ! $this->response_format ) {
78            $this->response_format =& $this->post_object_format;
79        }
80        parent::__construct( $args );
81    }
82
83    /**
84     * Filter to replace the password form with a simple message that the post is protected.
85     *
86     * @return string
87     */
88    public function the_password_form() {
89        return __( 'This post is password protected.', 'jetpack' );
90    }
91
92    /**
93     * Get a post by a specified field and value
94     *
95     * @param string $field - the field.
96     * @param string $field_value - the field value.
97     * @param string $context Post use context (e.g. 'display').
98     * @return array|bool|WP_Error Post
99     **/
100    public function get_post_by( $field, $field_value, $context = 'display' ) {
101        global $blog_id;
102
103        /** This filter is documented in class.json-api-endpoints.php */
104        $is_jetpack = true === apply_filters( 'is_jetpack_site', false, $blog_id );
105
106        if ( defined( 'GEO_LOCATION__CLASS' ) && class_exists( GEO_LOCATION__CLASS ) ) {
107            $geo = call_user_func( array( GEO_LOCATION__CLASS, 'init' ) );
108        } else {
109            $geo = false;
110        }
111
112        if ( 'display' === $context ) {
113            $args = $this->query_args();
114            if ( isset( $args['content_width'] ) && $args['content_width'] ) {
115                $GLOBALS['content_width'] = (int) $args['content_width'];
116            }
117        }
118
119        switch ( $field ) {
120            case 'name':
121                $post_id = $this->get_post_id_by_name( $field_value );
122                if ( is_wp_error( $post_id ) ) {
123                    return $post_id;
124                }
125                break;
126            default:
127                $post_id = (int) $field_value;
128                break;
129        }
130
131        $post = get_post( $post_id, OBJECT, $context );
132
133        if ( ! $post || is_wp_error( $post ) ) {
134            return new WP_Error( 'unknown_post', 'Unknown post', 404 );
135        }
136
137        if ( ! $this->is_post_type_allowed( $post->post_type ) && ( ! function_exists( 'is_post_freshly_pressed' ) || ! is_post_freshly_pressed( $post->ID ) ) ) {
138            return new WP_Error( 'unknown_post', 'Unknown post', 404 );
139        }
140
141        // Permissions
142        $capabilities = $this->get_current_user_capabilities( $post );
143
144        switch ( $context ) {
145            case 'edit':
146                if ( ! $capabilities['edit_post'] ) {
147                    return new WP_Error( 'unauthorized', 'User cannot edit post', 403 );
148                }
149                break;
150            case 'display':
151                break;
152            default:
153                return new WP_Error( 'invalid_context', 'Invalid API CONTEXT', 400 );
154        }
155
156        $can_view = $this->user_can_view_post( $post->ID );
157        if ( ! $can_view || is_wp_error( $can_view ) ) {
158            return $can_view;
159        }
160
161        $GLOBALS['post'] = $post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
162
163        if ( 'display' === $context ) {
164            setup_postdata( $post );
165        }
166
167        $response = array();
168
169        $fields = null;
170        // See WPCOM_JSON_API::filter_fields().
171        if ( 'display' === $context && ! empty( $this->api->query['fields'] ) && is_string( $this->api->query['fields'] ) ) {
172            $fields = array_fill_keys( array_map( 'trim', explode( ',', $this->api->query['fields'] ) ), true );
173        }
174
175        foreach ( array_keys( $this->post_object_format ) as $key ) {
176            if ( $fields !== null && ! isset( $fields[ $key ] ) ) {
177                continue;
178            }
179            switch ( $key ) {
180                case 'ID':
181                    // explicitly cast all output
182                    $response[ $key ] = (int) $post->ID;
183                    break;
184                case 'site_ID':
185                    $response[ $key ] = (int) $this->api->get_blog_id_for_output();
186                    break;
187                case 'author':
188                    $response[ $key ] = (object) $this->get_author( $post, 'edit' === $context && $capabilities['edit_post'] );
189                    break;
190                case 'date':
191                    $response[ $key ] = (string) $this->format_date( $post->post_date_gmt, $post->post_date );
192                    break;
193                case 'modified':
194                    $response[ $key ] = (string) $this->format_date( $post->post_modified_gmt, $post->post_modified );
195                    break;
196                case 'title':
197                    if ( 'display' === $context ) {
198                        $response[ $key ] = (string) get_the_title( $post->ID );
199                    } else {
200                        $response[ $key ] = htmlspecialchars_decode( $post->post_title, ENT_QUOTES );
201                    }
202                    break;
203                case 'URL':
204                    if ( 'revision' === $post->post_type ) {
205                        $response[ $key ] = (string) esc_url_raw( get_permalink( $post->post_parent ) );
206                    } else {
207                        $response[ $key ] = (string) esc_url_raw( get_permalink( $post->ID ) );
208                    }
209                    break;
210                case 'short_URL':
211                    $response[ $key ] = (string) esc_url_raw( wp_get_shortlink( $post->ID ) );
212                    break;
213                case 'content':
214                    if ( 'display' === $context ) {
215                        add_filter( 'the_password_form', array( $this, 'the_password_form' ) );
216                        $response[ $key ] = (string) $this->get_the_post_content_for_display();
217                        remove_filter( 'the_password_form', array( $this, 'the_password_form' ) );
218                    } else {
219                        $response[ $key ] = (string) $post->post_content;
220                    }
221                    break;
222                case 'excerpt':
223                    if ( 'display' === $context ) {
224                        add_filter( 'the_password_form', array( $this, 'the_password_form' ) );
225                        ob_start();
226                        the_excerpt();
227                        $response[ $key ] = (string) ob_get_clean();
228                        remove_filter( 'the_password_form', array( $this, 'the_password_form' ) );
229                    } else {
230                        $response[ $key ] = htmlspecialchars_decode( (string) $post->post_excerpt, ENT_QUOTES );
231                    }
232                    break;
233                case 'status':
234                    $response[ $key ] = (string) get_post_status( $post->ID );
235                    break;
236                case 'sticky':
237                    $response[ $key ] = (bool) is_sticky( $post->ID );
238                    break;
239                case 'slug':
240                    $response[ $key ] = (string) $post->post_name;
241                    break;
242                case 'guid':
243                    $response[ $key ] = (string) $post->guid;
244                    break;
245                case 'password':
246                    $response[ $key ] = (string) $post->post_password;
247                    if ( 'edit' === $context ) {
248                        $response[ $key ] = htmlspecialchars_decode( $response[ $key ], ENT_QUOTES );
249                    }
250                    break;
251                /** (object|false) */
252                case 'parent':
253                    if ( $post->post_parent ) {
254                        $parent = get_post( $post->post_parent );
255                        if ( 'display' === $context ) {
256                            $parent_title = (string) get_the_title( $parent->ID );
257                        } else {
258                            $parent_title = htmlspecialchars_decode( $post->post_title, ENT_QUOTES );
259                        }
260                        $response[ $key ] = (object) array(
261                            'ID'    => (int) $parent->ID,
262                            'type'  => (string) $parent->post_type,
263                            'link'  => (string) $this->links->get_post_link( $this->api->get_blog_id_for_output(), $parent->ID ),
264                            'title' => $parent_title,
265                        );
266                    } else {
267                        $response[ $key ] = false;
268                    }
269                    break;
270                case 'type':
271                    $response[ $key ] = (string) $post->post_type;
272                    break;
273                case 'comments_open':
274                    $response[ $key ] = (bool) comments_open( $post->ID );
275                    break;
276                case 'pings_open':
277                    $response[ $key ] = (bool) pings_open( $post->ID );
278                    break;
279                case 'likes_enabled':
280                    /** This filter is documented in modules/likes.php */
281                    $sitewide_likes_enabled = (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) );
282                    $post_likes_switched    = get_post_meta( $post->ID, 'switch_like_status', true );
283                    $post_likes_enabled     = $post_likes_switched || ( $sitewide_likes_enabled && $post_likes_switched !== '0' );
284                    $response[ $key ]       = $post_likes_enabled;
285                    break;
286                case 'sharing_enabled':
287                    $show = true;
288                    /** This filter is documented in modules/sharedaddy/sharing-service.php */
289                    $show = apply_filters( 'sharing_show', $show, $post );
290
291                    $switched_status = get_post_meta( $post->ID, 'sharing_disabled', false );
292
293                    if ( ! empty( $switched_status ) ) {
294                        $show = false;
295                    }
296                    $response[ $key ] = (bool) $show;
297                    break;
298                case 'comment_count':
299                    $response[ $key ] = (int) $post->comment_count;
300                    break;
301                case 'like_count':
302                    $response[ $key ] = (int) $this->api->post_like_count( $blog_id, $post->ID );
303                    break;
304                case 'i_like':
305                    $response[ $key ] = (bool) $this->api->is_liked( $blog_id, $post->ID );
306                    break;
307                case 'is_reblogged':
308                    $response[ $key ] = (bool) $this->api->is_reblogged( $blog_id, $post->ID );
309                    break;
310                case 'is_following':
311                    $response[ $key ] = (bool) $this->api->is_following( $blog_id );
312                    break;
313                case 'global_ID':
314                    $response[ $key ] = (string) $this->api->add_global_ID( $blog_id, $post->ID );
315                    break;
316                case 'featured_image':
317                    if ( $is_jetpack && ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
318                        $response[ $key ] = get_post_meta( $post->ID, '_jetpack_featured_image', true );
319                    } else {
320                        $image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
321                        if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) {
322                            $response[ $key ] = (string) $image_attributes[0];
323                        } else {
324                            $response[ $key ] = '';
325                        }
326                    }
327                    break;
328                case 'post_thumbnail':
329                    $response[ $key ] = null;
330
331                    $thumb_id = get_post_thumbnail_id( $post->ID );
332                    if ( ! empty( $thumb_id ) ) {
333                        $attachment = get_post( $thumb_id );
334                        if ( ! empty( $attachment ) ) {
335                            $featured_image_object = $this->get_attachment( $attachment );
336                        }
337
338                        if ( ! empty( $featured_image_object ) ) {
339                            $response[ $key ] = (object) $featured_image_object;
340                        }
341                    }
342                    break;
343                case 'format':
344                    $response[ $key ] = (string) get_post_format( $post->ID );
345                    if ( ! $response[ $key ] ) {
346                        $response[ $key ] = 'standard';
347                    }
348                    break;
349                /** (object|false) */
350                case 'geo':
351                    if ( ! $geo ) {
352                        $response[ $key ] = false;
353                    } else {
354                        $geo_data         = $geo->get_geo( 'post', $post->ID );
355                        $response[ $key ] = false;
356                        if ( $geo_data ) {
357                            $geo_data = array_intersect_key(
358                                $geo_data,
359                                array(
360                                    'latitude'  => true,
361                                    'longitude' => true,
362                                    'address'   => true,
363                                    'public'    => true,
364                                )
365                            );
366                            if ( $geo_data ) {
367                                $response[ $key ] = (object) array(
368                                    'latitude'  => isset( $geo_data['latitude'] ) ? (float) $geo_data['latitude'] : 0,
369                                    'longitude' => isset( $geo_data['longitude'] ) ? (float) $geo_data['longitude'] : 0,
370                                    'address'   => isset( $geo_data['address'] ) ? (string) $geo_data['address'] : '',
371                                );
372                            } else {
373                                $response[ $key ] = false;
374                            }
375                            // Private
376                            if ( ! isset( $geo_data['public'] ) || ! $geo_data['public'] ) {
377                                if ( 'edit' !== $context || ! $capabilities['edit_post'] ) {
378                                    // user can't access
379                                    $response[ $key ] = false;
380                                }
381                            }
382                        }
383                    }
384                    break;
385                case 'menu_order':
386                    $response[ $key ] = (int) $post->menu_order;
387                    break;
388                case 'publicize_URLs':
389                    $publicize_urls = array();
390                    $publicize      = get_post_meta( $post->ID, 'publicize_results', true );
391                    if ( $publicize ) {
392                        foreach ( $publicize as $service => $data ) {
393                            switch ( $service ) {
394                                // @todo Explore removing once Twitter has been removed from Publicize.
395                                case 'twitter':
396                                    foreach ( $data as $datum ) {
397                                        $publicize_urls[] = esc_url_raw( "https://twitter.com/{$datum['user_id']}/status/{$datum['post_id']}" );
398                                    }
399                                    break;
400                                case 'fb':
401                                    foreach ( $data as $datum ) {
402                                        $publicize_urls[] = esc_url_raw( "https://www.facebook.com/permalink.php?story_fbid={$datum['post_id']}&id={$datum['user_id']}" );
403                                    }
404                                    break;
405                            }
406                        }
407                    }
408                    $response[ $key ] = $publicize_urls;
409                    break;
410                case 'tags':
411                    $response[ $key ] = array();
412                    $terms            = wp_get_post_tags( $post->ID );
413                    foreach ( $terms as $term ) {
414                        if ( ! empty( $term->name ) ) {
415                            $response[ $key ][ $term->name ] = $this->format_taxonomy( $term, 'post_tag', 'display' );
416                        }
417                    }
418                    $response[ $key ] = (object) $response[ $key ];
419                    break;
420                case 'categories':
421                    $response[ $key ] = array();
422                    $terms            = wp_get_object_terms( $post->ID, 'category', array( 'fields' => 'all' ) );
423                    foreach ( $terms as $term ) {
424                        if ( ! empty( $term->name ) ) {
425                            $response[ $key ][ $term->name ] = $this->format_taxonomy( $term, 'category', 'display' );
426                        }
427                    }
428                    $response[ $key ] = (object) $response[ $key ];
429                    break;
430                case 'attachments':
431                    $response[ $key ] = array();
432                    $_attachments     = get_posts(
433                        array(
434                            'post_parent'    => $post->ID,
435                            'post_status'    => 'inherit',
436                            'post_type'      => 'attachment',
437                            'posts_per_page' => 100,
438                        )
439                    );
440                    foreach ( $_attachments as $attachment ) {
441                        $response[ $key ][ $attachment->ID ] = $this->get_attachment( $attachment );
442                    }
443                    $response[ $key ] = (object) $response[ $key ];
444                    break;
445                /** (array|false) */
446                case 'metadata':
447                    $metadata = array();
448                    foreach ( (array) has_meta( $post_id ) as $meta ) {
449                        // Don't expose protected fields.
450                        $show = false;
451                        if ( WPCOM_JSON_API_Metadata::is_public( $meta['meta_key'] ) ) {
452                            $show = true;
453                        }
454                        if ( current_user_can( 'edit_post_meta', $post_id, $meta['meta_key'] ) ) {
455                            $show = true;
456                        }
457
458                        if (
459                            in_array( $meta['meta_key'], Jetpack_SEO_Posts::POST_META_KEYS_ARRAY, true ) &&
460                            ! Jetpack_SEO_Utils::is_enabled_jetpack_seo()
461                        ) {
462                            $show = false;
463                        }
464
465                        if ( ! $show ) {
466                            continue;
467                        }
468
469                        $metadata[] = array(
470                            'id'    => $meta['meta_id'],
471                            'key'   => $meta['meta_key'],
472                            'value' => maybe_unserialize( $meta['meta_value'] ),
473                        );
474                    }
475
476                    if ( ! empty( $metadata ) ) {
477                        $response[ $key ] = $metadata;
478                    } else {
479                        $response[ $key ] = false;
480                    }
481                    break;
482                case 'meta':
483                    $response[ $key ] = (object) array(
484                        'links' => (object) array(
485                            'self'    => (string) $this->links->get_post_link( $this->api->get_blog_id_for_output(), $post->ID ),
486                            'help'    => (string) $this->links->get_post_link( $this->api->get_blog_id_for_output(), $post->ID, 'help' ),
487                            'site'    => (string) $this->links->get_site_link( $this->api->get_blog_id_for_output() ),
488                            'replies' => (string) $this->links->get_post_link( $this->api->get_blog_id_for_output(), $post->ID, 'replies/' ),
489                            'likes'   => (string) $this->links->get_post_link( $this->api->get_blog_id_for_output(), $post->ID, 'likes/' ),
490                        ),
491                    );
492                    break;
493                case 'current_user_can':
494                    $response[ $key ] = $capabilities;
495                    break;
496                case 'capabilities':
497                    $response[ $key ] = $capabilities;
498                    break;
499
500            }
501        }
502
503        unset( $GLOBALS['post'] );
504        return $response;
505    }
506
507    /**
508     *
509     * Get the post content for display.
510     *
511     * No Blog ID parameter.  No Post ID parameter.  Depends on globals.
512     * Expects setup_postdata() to already have been run.
513     *
514     * @return string|false
515     */
516    public function get_the_post_content_for_display() {
517        global $pages, $page;
518
519        $old_pages = $pages;
520        $old_page  = $page;
521
522        $content = implode( "\n\n", $pages );
523        $content = preg_replace( '/<!--more(.*?)?-->/', '', $content );
524        $pages   = array( $content ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
525        $page    = 1; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
526
527        ob_start();
528        the_content();
529        $return = ob_get_clean();
530
531        $pages = $old_pages; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
532        $page  = $old_page; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
533
534        return $return;
535    }
536
537    /**
538     * Gets the blog post.
539     *
540     * @param int    $blog_id - the blog ID.
541     * @param int    $post_id - the post ID.
542     * @param string $context - the context.
543     * @return array|bool|WP_Error Post
544     */
545    public function get_blog_post( $blog_id, $post_id, $context = 'display' ) {
546        $blog_id = $this->api->get_blog_id( $blog_id );
547        if ( ! $blog_id || is_wp_error( $blog_id ) ) {
548            return $blog_id;
549        }
550        switch_to_blog( $blog_id );
551        $post = $this->get_post_by( 'ID', $post_id, $context );
552        restore_current_blog();
553        return $post;
554    }
555
556    /**
557     * Supporting featured media in post endpoints. Currently on for wpcom blogs
558     * since it's calling WPCOM_JSON_API_Read_Endpoint methods which presently
559     * rely on wpcom specific functionality.
560     *
561     * @param  WP_Post $post - the WP Post object.
562     * @return object list of featured media
563     */
564    public static function find_featured_media( &$post ) {
565
566        if ( class_exists( 'WPCOM_JSON_API_Read_Endpoint' ) ) {
567            return WPCOM_JSON_API_Read_Endpoint::find_featured_worthy_media( (array) $post );
568        } else {
569            return (object) array();
570        }
571    }
572
573    /**
574     * Returns attachment object.
575     *
576     * @param object $attachment attachment row.
577     *
578     * @return object
579     */
580    public function get_attachment( $attachment ) {
581        $metadata = wp_get_attachment_metadata( $attachment->ID );
582
583        $result = array(
584            'ID'        => (int) $attachment->ID,
585            'URL'       => (string) wp_get_attachment_url( $attachment->ID ),
586            'guid'      => (string) $attachment->guid,
587            'mime_type' => (string) $attachment->post_mime_type,
588            'width'     => (int) isset( $metadata['width'] ) ? $metadata['width'] : 0,
589            'height'    => (int) isset( $metadata['height'] ) ? $metadata['height'] : 0,
590        );
591
592        if ( isset( $metadata['duration'] ) ) {
593            $result['duration'] = (int) $metadata['duration'];
594        }
595
596        return (object) apply_filters( 'get_attachment', $result );
597    }
598
599    /**
600     * Get post-specific user capabilities
601     *
602     * @param WP_Post $post - the WP_Post object.
603     *
604     * @return array - array of post-level permissions; 'publish_post', 'delete_post', 'edit_post'
605     */
606    public function get_current_user_capabilities( $post ) {
607        return array(
608            'publish_post' => current_user_can( 'publish_post', $post->ID ),
609            'delete_post'  => current_user_can( 'delete_post', $post->ID ),
610            'edit_post'    => current_user_can( 'edit_post', $post->ID ),
611        );
612    }
613
614    /**
615     * Get post ID by name
616     *
617     * Attempts to match name on post title and page path
618     *
619     * @param string $name - the name of the post.
620     *
621     * @return int|object Post ID on success, WP_Error object on failure
622     **/
623    protected function get_post_id_by_name( $name ) {
624        $name = sanitize_title( $name );
625
626        if ( ! $name ) {
627            return new WP_Error( 'invalid_post', 'Invalid post', 400 );
628        }
629
630        $posts = get_posts(
631            array(
632                'name'        => $name,
633                'numberposts' => 1,
634                'post_type'   => $this->_get_whitelisted_post_types(),
635            )
636        );
637
638        if ( ! $posts || ! isset( $posts[0]->ID ) || ! $posts[0]->ID ) {
639            $page = get_page_by_path( $name );
640
641            if ( ! $page ) {
642                return new WP_Error( 'unknown_post', 'Unknown post', 404 );
643            }
644
645            $post_id = $page->ID;
646        } else {
647            $post_id = (int) $posts[0]->ID;
648        }
649
650        return $post_id;
651    }
652}