business-hours.php
4 years ago
class-wpcom-rest-api-v2-endpoint-admin-menu.php
2 years ago
class-wpcom-rest-api-v2-endpoint-ai.php
2 years ago
class-wpcom-rest-api-v2-endpoint-app-media.php
2 years ago
class-wpcom-rest-api-v2-endpoint-blog-stats.php
2 years ago
class-wpcom-rest-api-v2-endpoint-external-media.php
2 years ago
class-wpcom-rest-api-v2-endpoint-following.php
2 years ago
class-wpcom-rest-api-v2-endpoint-goodreads.php
2 years ago
class-wpcom-rest-api-v2-endpoint-google-docs.php
4 years ago
class-wpcom-rest-api-v2-endpoint-instagram-gallery.php
3 years ago
class-wpcom-rest-api-v2-endpoint-mailchimp.php
3 years ago
class-wpcom-rest-api-v2-endpoint-newsletter-categories-list.php
2 years ago
class-wpcom-rest-api-v2-endpoint-newsletter-categories-subscriptions-count.php
2 years ago
class-wpcom-rest-api-v2-endpoint-podcast-player.php
2 years ago
class-wpcom-rest-api-v2-endpoint-publicize-share-post.php
3 years ago
class-wpcom-rest-api-v2-endpoint-related-posts.php
2 years ago
class-wpcom-rest-api-v2-endpoint-resolve-redirect.php
2 years ago
class-wpcom-rest-api-v2-endpoint-search.php
4 years ago
class-wpcom-rest-api-v2-endpoint-send-email-preview.php
2 years ago
class-wpcom-rest-api-v2-endpoint-template-loader.php
3 years ago
class-wpcom-rest-api-v2-endpoint-top-posts.php
2 years ago
class-wpcom-rest-api-v2-endpoint-transient.php
5 years ago
class-wpcom-rest-api-v3-endpoint-blogging-prompts.php
2 years ago
gutenberg-available-extensions.php
2 years ago
hello.php
4 years ago
memberships.php
2 years ago
publicize-connection-test-results.php
3 years ago
publicize-connections.php
2 years ago
publicize-services.php
3 years ago
service-api-keys.php
2 years ago
sites-posts-featured-media-url.php
2 years ago
subscribers.php
3 years ago
trait-wpcom-rest-api-proxy-request-trait.php
2 years ago
sites-posts-featured-media-url.php
55 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * WPCOM Add Featured Media URL |
| 4 | * Adds `jetpack_featured_media_url` to post responses |
| 5 | * |
| 6 | * @package automattic/jetpack |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Add featured media url to API post responses. |
| 11 | */ |
| 12 | class WPCOM_REST_API_V2_Sites_Posts_Add_Featured_Media_URL { |
| 13 | /** |
| 14 | * Constructor. |
| 15 | */ |
| 16 | public function __construct() { |
| 17 | add_action( 'rest_api_init', array( $this, 'add_featured_media_url' ) ); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Add featured media url to post responses. |
| 22 | */ |
| 23 | public function add_featured_media_url() { |
| 24 | register_rest_field( |
| 25 | 'post', |
| 26 | 'jetpack_featured_media_url', |
| 27 | array( |
| 28 | 'get_callback' => array( $this, 'get_featured_media_url' ), |
| 29 | 'update_callback' => null, |
| 30 | 'schema' => null, |
| 31 | ) |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Get featured media url. |
| 37 | * |
| 38 | * @param mixed $object What the endpoint returns. |
| 39 | * @param string $field_name Should always match `->field_name`. |
| 40 | * @param WP_REST_Request $request WP API request. |
| 41 | */ |
| 42 | public function get_featured_media_url( $object, $field_name, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 43 | $featured_media_url = ''; |
| 44 | $image_attributes = wp_get_attachment_image_src( |
| 45 | get_post_thumbnail_id( $object['id'] ), |
| 46 | 'full' |
| 47 | ); |
| 48 | if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) { |
| 49 | $featured_media_url = (string) $image_attributes[0]; |
| 50 | } |
| 51 | return $featured_media_url; |
| 52 | } |
| 53 | } |
| 54 | wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Sites_Posts_Add_Featured_Media_URL' ); |
| 55 |