wp/wp-includes/class-wp-oembed-controller.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    29 		 *
    29 		 *
    30 		 * @param int $maxwidth Maximum allowed width. Default 600.
    30 		 * @param int $maxwidth Maximum allowed width. Default 600.
    31 		 */
    31 		 */
    32 		$maxwidth = apply_filters( 'oembed_default_width', 600 );
    32 		$maxwidth = apply_filters( 'oembed_default_width', 600 );
    33 
    33 
    34 		register_rest_route( 'oembed/1.0', '/embed', array(
    34 		register_rest_route(
       
    35 			'oembed/1.0',
       
    36 			'/embed',
    35 			array(
    37 			array(
    36 				'methods'  => WP_REST_Server::READABLE,
    38 				array(
    37 				'callback' => array( $this, 'get_item' ),
    39 					'methods'  => WP_REST_Server::READABLE,
    38 				'args'     => array(
    40 					'callback' => array( $this, 'get_item' ),
    39 					'url'      => array(
    41 					'args'     => array(
    40 						'required'          => true,
    42 						'url'      => array(
    41 						'sanitize_callback' => 'esc_url_raw',
    43 							'required'          => true,
    42 					),
    44 							'sanitize_callback' => 'esc_url_raw',
    43 					'format'   => array(
    45 						),
    44 						'default'           => 'json',
    46 						'format'   => array(
    45 						'sanitize_callback' => 'wp_oembed_ensure_format',
    47 							'default'           => 'json',
    46 					),
    48 							'sanitize_callback' => 'wp_oembed_ensure_format',
    47 					'maxwidth' => array(
    49 						),
    48 						'default'           => $maxwidth,
    50 						'maxwidth' => array(
    49 						'sanitize_callback' => 'absint',
    51 							'default'           => $maxwidth,
       
    52 							'sanitize_callback' => 'absint',
       
    53 						),
    50 					),
    54 					),
    51 				),
    55 				),
    52 			),
    56 			)
    53 		) );
    57 		);
    54 
    58 
    55 		register_rest_route( 'oembed/1.0', '/proxy', array(
    59 		register_rest_route(
       
    60 			'oembed/1.0',
       
    61 			'/proxy',
    56 			array(
    62 			array(
    57 				'methods'  => WP_REST_Server::READABLE,
    63 				array(
    58 				'callback' => array( $this, 'get_proxy_item' ),
    64 					'methods'             => WP_REST_Server::READABLE,
    59 				'permission_callback' => array( $this, 'get_proxy_item_permissions_check' ),
    65 					'callback'            => array( $this, 'get_proxy_item' ),
    60 				'args'     => array(
    66 					'permission_callback' => array( $this, 'get_proxy_item_permissions_check' ),
    61 					'url'      => array(
    67 					'args'                => array(
    62 						'description'       => __( 'The URL of the resource for which to fetch oEmbed data.' ),
    68 						'url'       => array(
    63 						'type'              => 'string',
    69 							'description'       => __( 'The URL of the resource for which to fetch oEmbed data.' ),
    64 						'required'          => true,
    70 							'type'              => 'string',
    65 						'sanitize_callback' => 'esc_url_raw',
    71 							'required'          => true,
    66 					),
    72 							'sanitize_callback' => 'esc_url_raw',
    67 					'format'   => array(
    73 						),
    68 						'description'       => __( 'The oEmbed format to use.' ),
    74 						'format'    => array(
    69 						'type'              => 'string',
    75 							'description' => __( 'The oEmbed format to use.' ),
    70 						'default'           => 'json',
    76 							'type'        => 'string',
    71 						'enum'              => array(
    77 							'default'     => 'json',
    72 							'json',
    78 							'enum'        => array(
    73 							'xml',
    79 								'json',
    74 						),
    80 								'xml',
    75 					),
    81 							),
    76 					'maxwidth' => array(
    82 						),
    77 						'description'       => __( 'The maximum width of the embed frame in pixels.' ),
    83 						'maxwidth'  => array(
    78 						'type'              => 'integer',
    84 							'description'       => __( 'The maximum width of the embed frame in pixels.' ),
    79 						'default'           => $maxwidth,
    85 							'type'              => 'integer',
    80 						'sanitize_callback' => 'absint',
    86 							'default'           => $maxwidth,
    81 					),
    87 							'sanitize_callback' => 'absint',
    82 					'maxheight' => array(
    88 						),
    83 						'description'       => __( 'The maximum height of the embed frame in pixels.' ),
    89 						'maxheight' => array(
    84 						'type'              => 'integer',
    90 							'description'       => __( 'The maximum height of the embed frame in pixels.' ),
    85 						'sanitize_callback' => 'absint',
    91 							'type'              => 'integer',
    86 					),
    92 							'sanitize_callback' => 'absint',
    87 					'discover' => array(
    93 						),
    88 						'description'       => __( 'Whether to perform an oEmbed discovery request for non-whitelisted providers.' ),
    94 						'discover'  => array(
    89 						'type'              => 'boolean',
    95 							'description' => __( 'Whether to perform an oEmbed discovery request for non-whitelisted providers.' ),
    90 						'default'           => true,
    96 							'type'        => 'boolean',
       
    97 							'default'     => true,
       
    98 						),
    91 					),
    99 					),
    92 				),
   100 				),
    93 			),
   101 			)
    94 		) );
   102 		);
    95 	}
   103 	}
    96 
   104 
    97 	/**
   105 	/**
    98 	 * Callback for the embed API endpoint.
   106 	 * Callback for the embed API endpoint.
    99 	 *
   107 	 *
   155 		$args = $request->get_params();
   163 		$args = $request->get_params();
   156 
   164 
   157 		// Serve oEmbed data from cache if set.
   165 		// Serve oEmbed data from cache if set.
   158 		unset( $args['_wpnonce'] );
   166 		unset( $args['_wpnonce'] );
   159 		$cache_key = 'oembed_' . md5( serialize( $args ) );
   167 		$cache_key = 'oembed_' . md5( serialize( $args ) );
   160 		$data = get_transient( $cache_key );
   168 		$data      = get_transient( $cache_key );
   161 		if ( ! empty( $data ) ) {
   169 		if ( ! empty( $data ) ) {
   162 			return $data;
   170 			return $data;
   163 		}
   171 		}
   164 
   172 
   165 		$url = $request['url'];
   173 		$url = $request['url'];
   171 		}
   179 		}
   172 		if ( isset( $args['maxheight'] ) ) {
   180 		if ( isset( $args['maxheight'] ) ) {
   173 			$args['height'] = $args['maxheight'];
   181 			$args['height'] = $args['maxheight'];
   174 		}
   182 		}
   175 
   183 
       
   184 		// Short-circuit process for URLs belonging to the current site.
       
   185 		$data = get_oembed_response_data_for_url( $url, $args );
       
   186 
       
   187 		if ( $data ) {
       
   188 			return $data;
       
   189 		}
       
   190 
   176 		$data = _wp_oembed_get_object()->get_data( $url, $args );
   191 		$data = _wp_oembed_get_object()->get_data( $url, $args );
   177 
   192 
   178 		if ( false === $data ) {
   193 		if ( false === $data ) {
   179 			return new WP_Error( 'oembed_invalid_url', get_status_header_desc( 404 ), array( 'status' => 404 ) );
   194 			return new WP_Error( 'oembed_invalid_url', get_status_header_desc( 404 ), array( 'status' => 404 ) );
   180 		}
   195 		}
       
   196 
       
   197 		/** This filter is documented in wp-includes/class-oembed.php */
       
   198 		$data->html = apply_filters( 'oembed_result', _wp_oembed_get_object()->data2html( (object) $data, $url ), $url, $args );
   181 
   199 
   182 		/**
   200 		/**
   183 		 * Filters the oEmbed TTL value (time to live).
   201 		 * Filters the oEmbed TTL value (time to live).
   184 		 *
   202 		 *
   185 		 * Similar to the {@see 'oembed_ttl'} filter, but for the REST API
   203 		 * Similar to the {@see 'oembed_ttl'} filter, but for the REST API