PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.3
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
jetpack / _inc / lib / core-api / wpcom-endpoints / class-wpcom-rest-api-v2-endpoint-podcast-player.php

class-wpcom-rest-api-v2-endpoint-podcast-player.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-a.3, at _inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-podcast-player.php

176 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $this->namespace = 'wpcom/v2';
24 $this->rest_base = 'podcast-player';
25 // This endpoint *does not* need to connect directly to Jetpack sites.
26 add_action( 'rest_api_init', array( $this, 'register_routes' ) );
27 }
28
29 /**
30 * Register the route.
31 */
32 public function register_routes() {
33 // GET /sites/<blog_id>/podcast-player - Returns feed data.
34 register_rest_route(
35 $this->namespace,
36 '/' . $this->rest_base,
37 array(
38 array(
39 'methods' => WP_REST_Server::READABLE,
40 'callback' => array( $this, 'get_player_data' ),
41 'permission_callback' => function () {
42 return current_user_can( 'edit_posts' );
43 },
44 'args' => array(
45 'url' => array(
46 'description' => __( 'The Podcast RSS feed URL.', 'jetpack' ),
47 'type' => 'string',
48 'required' => 'true',
49 'validate_callback' => function ( $param ) {
50 return wp_http_validate_url( $param );
51 },
52 ),
53 'guids' => array(
54 'description' => __( 'A list of unique identifiers for fetching specific podcast episodes.', 'jetpack' ),
55 'type' => 'array',
56 'required' => 'false',
57 'validate_callback' => function ( $guids ) {
58 return is_array( $guids );
59 },
60 'sanitize_callback' => function ( $guids ) {
61 return array_map( 'sanitize_text_field', $guids );
62 },
63 ),
64 'episode-options' => array(
65 'description' => __( 'Whether we should return the episodes list for use in the selection UI', 'jetpack' ),
66 'type' => 'boolean',
67 'required' => 'false',
68 ),
69 ),
70 'schema' => array( $this, 'get_public_item_schema' ),
71 ),
72 )
73 );
74
75 // GET /sites/<blog_id>/podcast-player/track-quantity - Returns number of tracks.
76 register_rest_route(
77 $this->namespace,
78 '/' . $this->rest_base . '/track-quantity',
79 array(
80 array(
81 'methods' => WP_REST_Server::READABLE,
82 'callback' => array( $this, 'get_tracks_quantity' ),
83 'permission_callback' => function () {
84 return current_user_can( 'edit_posts' );
85 },
86 ),
87 )
88 );
89 }
90
91 /**
92 * Retrieves tracks quantity
93 *
94 * @return Integer number of tracks.
95 * */
96 public function get_tracks_quantity() {
97 if ( ! class_exists( 'Jetpack_Podcast_Helper' ) ) {
98 require_once JETPACK__PLUGIN_DIR . '/_inc/lib/class-jetpack-podcast-helper.php';
99 }
100
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 if ( ! class_exists( 'Jetpack_Podcast_Helper' ) ) {
112 require_once JETPACK__PLUGIN_DIR . '/_inc/lib/class-jetpack-podcast-helper.php';
113 }
114
115 $helper = new Jetpack_Podcast_Helper( $request['url'] );
116
117 // The editor needs the feed's true state, not a fallback papering over a broken URL.
118 $args = array( 'report_errors' => true );
119
120 if ( isset( $request['guids'] ) ) {
121 $args['guids'] = $request['guids'];
122 }
123
124 if ( isset( $request['episode-options'] ) && $request['episode-options'] ) {
125 $args['episode-options'] = true;
126 }
127
128 $player_data = $helper->get_player_data( $args );
129
130 if ( is_wp_error( $player_data ) ) {
131 return rest_ensure_response( $player_data );
132 }
133
134 $player_data = $this->prepare_for_response( $player_data, $request );
135 return rest_ensure_response( $player_data );
136 }
137
138 /**
139 * Filters out data based on ?_fields= request parameter
140 *
141 * @param array $player_data Data for the player.
142 * @param WP_REST_Request $request The request.
143 * @return array filtered $player_data
144 */
145 public function prepare_for_response( $player_data, $request ) {
146 if ( ! is_callable( array( $this, 'get_fields_for_response' ) ) ) {
147 return $player_data;
148 }
149
150 $fields = $this->get_fields_for_response( $request );
151
152 $response_data = array();
153 foreach ( $player_data as $field => $value ) {
154 if ( in_array( $field, $fields, true ) ) {
155 $response_data[ $field ] = $value;
156 }
157 }
158
159 return $response_data;
160 }
161
162 /**
163 * Retrieves the response schema, conforming to JSON Schema.
164 *
165 * @return array
166 */
167 public function get_item_schema() {
168 if ( ! class_exists( 'Jetpack_Podcast_Helper' ) ) {
169 require_once JETPACK__PLUGIN_DIR . '/_inc/lib/class-jetpack-podcast-helper.php';
170 }
171
172 return Jetpack_Podcast_Helper::get_player_data_schema();
173 }
174 }
175 wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Podcast_Player' );
176