wp/wp-includes/class-wp-oembed-controller.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    34 		register_rest_route(
    34 		register_rest_route(
    35 			'oembed/1.0',
    35 			'oembed/1.0',
    36 			'/embed',
    36 			'/embed',
    37 			array(
    37 			array(
    38 				array(
    38 				array(
    39 					'methods'  => WP_REST_Server::READABLE,
    39 					'methods'             => WP_REST_Server::READABLE,
    40 					'callback' => array( $this, 'get_item' ),
    40 					'callback'            => array( $this, 'get_item' ),
    41 					'args'     => array(
    41 					'permission_callback' => '__return_true',
       
    42 					'args'                => array(
    42 						'url'      => array(
    43 						'url'      => array(
    43 							'required'          => true,
    44 							'description' => __( 'The URL of the resource for which to fetch oEmbed data.' ),
    44 							'sanitize_callback' => 'esc_url_raw',
    45 							'required'    => true,
       
    46 							'type'        => 'string',
       
    47 							'format'      => 'uri',
    45 						),
    48 						),
    46 						'format'   => array(
    49 						'format'   => array(
    47 							'default'           => 'json',
    50 							'default'           => 'json',
    48 							'sanitize_callback' => 'wp_oembed_ensure_format',
    51 							'sanitize_callback' => 'wp_oembed_ensure_format',
    49 						),
    52 						),
    64 					'methods'             => WP_REST_Server::READABLE,
    67 					'methods'             => WP_REST_Server::READABLE,
    65 					'callback'            => array( $this, 'get_proxy_item' ),
    68 					'callback'            => array( $this, 'get_proxy_item' ),
    66 					'permission_callback' => array( $this, 'get_proxy_item_permissions_check' ),
    69 					'permission_callback' => array( $this, 'get_proxy_item_permissions_check' ),
    67 					'args'                => array(
    70 					'args'                => array(
    68 						'url'       => array(
    71 						'url'       => array(
    69 							'description'       => __( 'The URL of the resource for which to fetch oEmbed data.' ),
    72 							'description' => __( 'The URL of the resource for which to fetch oEmbed data.' ),
    70 							'type'              => 'string',
    73 							'required'    => true,
    71 							'required'          => true,
    74 							'type'        => 'string',
    72 							'sanitize_callback' => 'esc_url_raw',
    75 							'format'      => 'uri',
    73 						),
    76 						),
    74 						'format'    => array(
    77 						'format'    => array(
    75 							'description' => __( 'The oEmbed format to use.' ),
    78 							'description' => __( 'The oEmbed format to use.' ),
    76 							'type'        => 'string',
    79 							'type'        => 'string',
    77 							'default'     => 'json',
    80 							'default'     => 'json',
    90 							'description'       => __( 'The maximum height of the embed frame in pixels.' ),
    93 							'description'       => __( 'The maximum height of the embed frame in pixels.' ),
    91 							'type'              => 'integer',
    94 							'type'              => 'integer',
    92 							'sanitize_callback' => 'absint',
    95 							'sanitize_callback' => 'absint',
    93 						),
    96 						),
    94 						'discover'  => array(
    97 						'discover'  => array(
    95 							'description' => __( 'Whether to perform an oEmbed discovery request for non-whitelisted providers.' ),
    98 							'description' => __( 'Whether to perform an oEmbed discovery request for unsanctioned providers.' ),
    96 							'type'        => 'boolean',
    99 							'type'        => 'boolean',
    97 							'default'     => true,
   100 							'default'     => true,
    98 						),
   101 						),
    99 					),
   102 					),
   100 				),
   103 				),
   108 	 * Returns the JSON object for the post.
   111 	 * Returns the JSON object for the post.
   109 	 *
   112 	 *
   110 	 * @since 4.4.0
   113 	 * @since 4.4.0
   111 	 *
   114 	 *
   112 	 * @param WP_REST_Request $request Full data about the request.
   115 	 * @param WP_REST_Request $request Full data about the request.
   113 	 * @return WP_Error|array oEmbed response data or WP_Error on failure.
   116 	 * @return array|WP_Error oEmbed response data or WP_Error on failure.
   114 	 */
   117 	 */
   115 	public function get_item( $request ) {
   118 	public function get_item( $request ) {
   116 		$post_id = url_to_postid( $request['url'] );
   119 		$post_id = url_to_postid( $request['url'] );
   117 
   120 
   118 		/**
   121 		/**
   189 		}
   192 		}
   190 
   193 
   191 		$data = _wp_oembed_get_object()->get_data( $url, $args );
   194 		$data = _wp_oembed_get_object()->get_data( $url, $args );
   192 
   195 
   193 		if ( false === $data ) {
   196 		if ( false === $data ) {
       
   197 			// Try using a classic embed, instead.
       
   198 			global $wp_embed;
       
   199 
       
   200 			/* @var WP_Embed $wp_embed */
       
   201 			$html = $wp_embed->get_embed_handler_html( $args, $url );
       
   202 
       
   203 			if ( $html ) {
       
   204 				global $wp_scripts;
       
   205 				// Check if any scripts were enqueued by the shortcode, and include them in the response.
       
   206 				$enqueued_scripts = array();
       
   207 
       
   208 				foreach ( $wp_scripts->queue as $script ) {
       
   209 					$enqueued_scripts[] = $wp_scripts->registered[ $script ]->src;
       
   210 				}
       
   211 
       
   212 				return (object) array(
       
   213 					'provider_name' => __( 'Embed Handler' ),
       
   214 					'html'          => $html,
       
   215 					'scripts'       => $enqueued_scripts,
       
   216 				);
       
   217 			}
       
   218 
   194 			return new WP_Error( 'oembed_invalid_url', get_status_header_desc( 404 ), array( 'status' => 404 ) );
   219 			return new WP_Error( 'oembed_invalid_url', get_status_header_desc( 404 ), array( 'status' => 404 ) );
   195 		}
   220 		}
   196 
   221 
   197 		/** This filter is documented in wp-includes/class-oembed.php */
   222 		/** This filter is documented in wp-includes/class-wp-oembed.php */
   198 		$data->html = apply_filters( 'oembed_result', _wp_oembed_get_object()->data2html( (object) $data, $url ), $url, $args );
   223 		$data->html = apply_filters( 'oembed_result', _wp_oembed_get_object()->data2html( (object) $data, $url ), $url, $args );
   199 
   224 
   200 		/**
   225 		/**
   201 		 * Filters the oEmbed TTL value (time to live).
   226 		 * Filters the oEmbed TTL value (time to live).
   202 		 *
   227 		 *