wp/wp-includes/rest-api/class-wp-rest-server.php
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
   634 			$data[ $rel ] = array();
   634 			$data[ $rel ] = array();
   635 
   635 
   636 			foreach ( $items as $item ) {
   636 			foreach ( $items as $item ) {
   637 				$attributes         = $item['attributes'];
   637 				$attributes         = $item['attributes'];
   638 				$attributes['href'] = $item['href'];
   638 				$attributes['href'] = $item['href'];
   639 				$data[ $rel ][]     = $attributes;
   639 
       
   640 				if ( 'self' !== $rel ) {
       
   641 					$data[ $rel ][] = $attributes;
       
   642 					continue;
       
   643 				}
       
   644 
       
   645 				$target_hints = self::get_target_hints_for_link( $attributes );
       
   646 				if ( $target_hints ) {
       
   647 					$attributes['targetHints'] = $target_hints;
       
   648 				}
       
   649 
       
   650 				$data[ $rel ][] = $attributes;
   640 			}
   651 			}
   641 		}
   652 		}
   642 
   653 
   643 		return $data;
   654 		return $data;
       
   655 	}
       
   656 
       
   657 	/**
       
   658 	 * Gets the target links for a REST API Link.
       
   659 	 *
       
   660 	 * @since 6.7.0
       
   661 	 *
       
   662 	 * @param array $link
       
   663 	 *
       
   664 	 * @return array|null
       
   665 	 */
       
   666 	protected static function get_target_hints_for_link( $link ) {
       
   667 		// Prefer targetHints that were specifically designated by the developer.
       
   668 		if ( isset( $link['targetHints']['allow'] ) ) {
       
   669 			return null;
       
   670 		}
       
   671 
       
   672 		$request = WP_REST_Request::from_url( $link['href'] );
       
   673 		if ( ! $request ) {
       
   674 			return null;
       
   675 		}
       
   676 
       
   677 		$server = rest_get_server();
       
   678 		$match  = $server->match_request_to_handler( $request );
       
   679 
       
   680 		if ( is_wp_error( $match ) ) {
       
   681 			return null;
       
   682 		}
       
   683 
       
   684 		if ( is_wp_error( $request->has_valid_params() ) ) {
       
   685 			return null;
       
   686 		}
       
   687 
       
   688 		if ( is_wp_error( $request->sanitize_params() ) ) {
       
   689 			return null;
       
   690 		}
       
   691 
       
   692 		$target_hints = array();
       
   693 
       
   694 		$response = new WP_REST_Response();
       
   695 		$response->set_matched_route( $match[0] );
       
   696 		$response->set_matched_handler( $match[1] );
       
   697 		$headers = rest_send_allow_header( $response, $server, $request )->get_headers();
       
   698 
       
   699 		foreach ( $headers as $name => $value ) {
       
   700 			$name = WP_REST_Request::canonicalize_header_name( $name );
       
   701 
       
   702 			$target_hints[ $name ] = array_map( 'trim', explode( ',', $value ) );
       
   703 		}
       
   704 
       
   705 		return $target_hints;
   644 	}
   706 	}
   645 
   707 
   646 	/**
   708 	/**
   647 	 * Retrieves the CURIEs (compact URIs) used for relations.
   709 	 * Retrieves the CURIEs (compact URIs) used for relations.
   648 	 *
   710 	 *
  1307 			'description'     => get_option( 'blogdescription' ),
  1369 			'description'     => get_option( 'blogdescription' ),
  1308 			'url'             => get_option( 'siteurl' ),
  1370 			'url'             => get_option( 'siteurl' ),
  1309 			'home'            => home_url(),
  1371 			'home'            => home_url(),
  1310 			'gmt_offset'      => get_option( 'gmt_offset' ),
  1372 			'gmt_offset'      => get_option( 'gmt_offset' ),
  1311 			'timezone_string' => get_option( 'timezone_string' ),
  1373 			'timezone_string' => get_option( 'timezone_string' ),
       
  1374 			'page_for_posts'  => (int) get_option( 'page_for_posts' ),
       
  1375 			'page_on_front'   => (int) get_option( 'page_on_front' ),
       
  1376 			'show_on_front'   => get_option( 'show_on_front' ),
  1312 			'namespaces'      => array_keys( $this->namespaces ),
  1377 			'namespaces'      => array_keys( $this->namespaces ),
  1313 			'authentication'  => array(),
  1378 			'authentication'  => array(),
  1314 			'routes'          => $this->get_data_for_routes( $this->get_routes(), $request['context'] ),
  1379 			'routes'          => $this->get_data_for_routes( $this->get_routes(), $request['context'] ),
  1315 		);
  1380 		);
  1316 
  1381 
  1665 			}
  1730 			}
  1666 
  1731 
  1667 			$single_request = new WP_REST_Request( isset( $args['method'] ) ? $args['method'] : 'POST', $parsed_url['path'] );
  1732 			$single_request = new WP_REST_Request( isset( $args['method'] ) ? $args['method'] : 'POST', $parsed_url['path'] );
  1668 
  1733 
  1669 			if ( ! empty( $parsed_url['query'] ) ) {
  1734 			if ( ! empty( $parsed_url['query'] ) ) {
  1670 				$query_args = null; // Satisfy linter.
  1735 				$query_args = array();
  1671 				wp_parse_str( $parsed_url['query'], $query_args );
  1736 				wp_parse_str( $parsed_url['query'], $query_args );
  1672 				$single_request->set_query_params( $query_args );
  1737 				$single_request->set_query_params( $query_args );
  1673 			}
  1738 			}
  1674 
  1739 
  1675 			if ( ! empty( $args['body'] ) ) {
  1740 			if ( ! empty( $args['body'] ) ) {