Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_JSON_API_List_Embeds_Endpoint
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
90
0.00% covered (danger)
0.00%
0 / 1
 callback
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
90
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3if ( ! defined( 'ABSPATH' ) ) {
4    exit( 0 );
5}
6
7/**
8 * List Embeds endpoint.
9 */
10new WPCOM_JSON_API_List_Embeds_Endpoint(
11    array(
12        'description'          => 'Get a list of embeds available on a site. Note: The current user must have publishing access.',
13        'group'                => 'sites',
14        'stat'                 => 'embeds',
15        'method'               => 'GET',
16        'path'                 => '/sites/%s/embeds',
17        'path_labels'          => array(
18            '$site' => '(int|string) Site ID or domain',
19        ),
20        'response_format'      => array(
21            'embeds' => '(array) A list of supported embeds by their regex pattern.',
22        ),
23        'example_request'      => 'https://public-api.wordpress.com/rest/v1/sites/82974409/embeds',
24        'example_request_data' => array(
25            'headers' => array(
26                'authorization' => 'Bearer YOUR_API_TOKEN',
27            ),
28        ),
29    )
30);
31
32/**
33 * List Embeds Endpoint class.
34 *
35 * /sites/%s/embeds -> $blog_id
36 *
37 * @phan-constructor-used-for-side-effects
38 */
39class WPCOM_JSON_API_List_Embeds_Endpoint extends WPCOM_JSON_API_Endpoint {
40    /**
41     * API Callback.
42     *
43     * @param string $path - the path.
44     * @param int    $blog_id - the blog ID.
45     */
46    public function callback( $path = '', $blog_id = 0 ) {
47        $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
48        if ( is_wp_error( $blog_id ) ) {
49            return $blog_id;
50        }
51
52        // permissions check.
53        if ( ! current_user_can( 'edit_posts' ) ) {
54            return new WP_Error( 'unauthorized', 'Your token must have permission to post on this blog.', 403 );
55        }
56
57        // list em.
58        $output = array( 'embeds' => array() );
59
60        if ( ! function_exists( '_wp_oembed_get_object' ) ) {
61            require_once ABSPATH . WPINC . '/class-oembed.php';
62        }
63
64        global $wp_embed;
65        $oembed = _wp_oembed_get_object();
66
67        foreach ( $wp_embed->handlers as $handlers ) {
68            foreach ( $handlers as $handler ) {
69                if ( ! empty( $handler['regex'] ) ) {
70                    $output['embeds'][] = $handler['regex'];
71                }
72            }
73        }
74
75        foreach ( $oembed->providers as $regex => $oembed_info ) {
76            if ( ! empty( $regex ) ) {
77                $output['embeds'][] = $regex;
78            }
79        }
80
81        return $output;
82    }
83}