Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
58.54% |
48 / 82 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| WPCOM_REST_API_V2_Endpoint_Podcast_Player | |
60.76% |
48 / 79 |
|
0.00% |
0 / 6 |
25.84 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| register_routes | |
90.57% |
48 / 53 |
|
0.00% |
0 / 1 |
1.00 | |||
| get_tracks_quantity | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_player_data | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
30 | |||
| prepare_for_response | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| get_item_schema | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Podcast Player API |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | * @since 8.4.0 |
| 7 | */ |
| 8 | |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit( 0 ); |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * Fetch podcast feeds and parse data for the Podcast Player block. |
| 15 | * |
| 16 | * @since 8.4.0 |
| 17 | */ |
| 18 | class WPCOM_REST_API_V2_Endpoint_Podcast_Player extends WP_REST_Controller { |
| 19 | /** |
| 20 | * Constructor. |
| 21 | */ |
| 22 | public function __construct() { |
| 23 | if ( ! class_exists( 'Jetpack_Podcast_Helper' ) ) { |
| 24 | require_once JETPACK__PLUGIN_DIR . '/_inc/lib/class-jetpack-podcast-helper.php'; |
| 25 | } |
| 26 | |
| 27 | $this->namespace = 'wpcom/v2'; |
| 28 | $this->rest_base = 'podcast-player'; |
| 29 | // This endpoint *does not* need to connect directly to Jetpack sites. |
| 30 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Register the route. |
| 35 | */ |
| 36 | public function register_routes() { |
| 37 | // GET /sites/<blog_id>/podcast-player - Returns feed data. |
| 38 | register_rest_route( |
| 39 | $this->namespace, |
| 40 | '/' . $this->rest_base, |
| 41 | array( |
| 42 | array( |
| 43 | 'methods' => WP_REST_Server::READABLE, |
| 44 | 'callback' => array( $this, 'get_player_data' ), |
| 45 | 'permission_callback' => function () { |
| 46 | return current_user_can( 'edit_posts' ); |
| 47 | }, |
| 48 | 'args' => array( |
| 49 | 'url' => array( |
| 50 | 'description' => __( 'The Podcast RSS feed URL.', 'jetpack' ), |
| 51 | 'type' => 'string', |
| 52 | 'required' => 'true', |
| 53 | 'validate_callback' => function ( $param ) { |
| 54 | return wp_http_validate_url( $param ); |
| 55 | }, |
| 56 | ), |
| 57 | 'guids' => array( |
| 58 | 'description' => __( 'A list of unique identifiers for fetching specific podcast episodes.', 'jetpack' ), |
| 59 | 'type' => 'array', |
| 60 | 'required' => 'false', |
| 61 | 'validate_callback' => function ( $guids ) { |
| 62 | return is_array( $guids ); |
| 63 | }, |
| 64 | 'sanitize_callback' => function ( $guids ) { |
| 65 | return array_map( 'sanitize_text_field', $guids ); |
| 66 | }, |
| 67 | ), |
| 68 | 'episode-options' => array( |
| 69 | 'description' => __( 'Whether we should return the episodes list for use in the selection UI', 'jetpack' ), |
| 70 | 'type' => 'boolean', |
| 71 | 'required' => 'false', |
| 72 | ), |
| 73 | ), |
| 74 | 'schema' => array( $this, 'get_public_item_schema' ), |
| 75 | ), |
| 76 | ) |
| 77 | ); |
| 78 | |
| 79 | // GET /sites/<blog_id>/podcast-player/track-quantity - Returns number of tracks. |
| 80 | register_rest_route( |
| 81 | $this->namespace, |
| 82 | '/' . $this->rest_base . '/track-quantity', |
| 83 | array( |
| 84 | array( |
| 85 | 'methods' => WP_REST_Server::READABLE, |
| 86 | 'callback' => array( $this, 'get_tracks_quantity' ), |
| 87 | 'permission_callback' => function () { |
| 88 | return current_user_can( 'edit_posts' ); |
| 89 | }, |
| 90 | ), |
| 91 | ) |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Retrieves tracks quantity |
| 97 | * |
| 98 | * @return Integer number of tracks. |
| 99 | * */ |
| 100 | public function get_tracks_quantity() { |
| 101 | return rest_ensure_response( Jetpack_Podcast_Helper::get_tracks_quantity() ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Retrieves data needed to display a podcast player from RSS feed. |
| 106 | * |
| 107 | * @param WP_REST_Request $request The REST API request data. |
| 108 | * @return WP_REST_Response The REST API response. |
| 109 | */ |
| 110 | public function get_player_data( $request ) { |
| 111 | $helper = new Jetpack_Podcast_Helper( $request['url'] ); |
| 112 | |
| 113 | $args = array(); |
| 114 | |
| 115 | if ( isset( $request['guids'] ) ) { |
| 116 | $args['guids'] = $request['guids']; |
| 117 | } |
| 118 | |
| 119 | if ( isset( $request['episode-options'] ) && $request['episode-options'] ) { |
| 120 | $args['episode-options'] = true; |
| 121 | } |
| 122 | |
| 123 | $player_data = $helper->get_player_data( $args ); |
| 124 | |
| 125 | if ( is_wp_error( $player_data ) ) { |
| 126 | return rest_ensure_response( $player_data ); |
| 127 | } |
| 128 | |
| 129 | $player_data = $this->prepare_for_response( $player_data, $request ); |
| 130 | return rest_ensure_response( $player_data ); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Filters out data based on ?_fields= request parameter |
| 135 | * |
| 136 | * @param array $player_data Data for the player. |
| 137 | * @param WP_REST_Request $request The request. |
| 138 | * @return array filtered $player_data |
| 139 | */ |
| 140 | public function prepare_for_response( $player_data, $request ) { |
| 141 | if ( ! is_callable( array( $this, 'get_fields_for_response' ) ) ) { |
| 142 | return $player_data; |
| 143 | } |
| 144 | |
| 145 | $fields = $this->get_fields_for_response( $request ); |
| 146 | |
| 147 | $response_data = array(); |
| 148 | foreach ( $player_data as $field => $value ) { |
| 149 | if ( in_array( $field, $fields, true ) ) { |
| 150 | $response_data[ $field ] = $value; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | return $response_data; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Retrieves the response schema, conforming to JSON Schema. |
| 159 | * |
| 160 | * @return array |
| 161 | */ |
| 162 | public function get_item_schema() { |
| 163 | return Jetpack_Podcast_Helper::get_player_data_schema(); |
| 164 | } |
| 165 | } |
| 166 | wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Podcast_Player' ); |