Complex classes like WP_REST_Server often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WP_REST_Server, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class WP_REST_Server { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Alias for GET transport method. |
||
| 19 | * |
||
| 20 | * @since 4.4.0 |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | const READABLE = 'GET'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Alias for POST transport method. |
||
| 27 | * |
||
| 28 | * @since 4.4.0 |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | const CREATABLE = 'POST'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Alias for POST, PUT, PATCH transport methods together. |
||
| 35 | * |
||
| 36 | * @since 4.4.0 |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | const EDITABLE = 'POST, PUT, PATCH'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Alias for DELETE transport method. |
||
| 43 | * |
||
| 44 | * @since 4.4.0 |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | const DELETABLE = 'DELETE'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Alias for GET, POST, PUT, PATCH & DELETE transport methods together. |
||
| 51 | * |
||
| 52 | * @since 4.4.0 |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | const ALLMETHODS = 'GET, POST, PUT, PATCH, DELETE'; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Namespaces registered to the server. |
||
| 59 | * |
||
| 60 | * @since 4.4.0 |
||
| 61 | * @access protected |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $namespaces = array(); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Endpoints registered to the server. |
||
| 68 | * |
||
| 69 | * @since 4.4.0 |
||
| 70 | * @access protected |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | protected $endpoints = array(); |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Options defined for the routes. |
||
| 77 | * |
||
| 78 | * @since 4.4.0 |
||
| 79 | * @access protected |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | protected $route_options = array(); |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Instantiates the REST server. |
||
| 86 | * |
||
| 87 | * @since 4.4.0 |
||
| 88 | * @access public |
||
| 89 | */ |
||
| 90 | public function __construct() { |
||
| 91 | $this->endpoints = array( |
||
| 92 | // Meta endpoints. |
||
| 93 | '/' => array( |
||
| 94 | 'callback' => array( $this, 'get_index' ), |
||
| 95 | 'methods' => 'GET', |
||
| 96 | 'args' => array( |
||
| 97 | 'context' => array( |
||
| 98 | 'default' => 'view', |
||
| 99 | ), |
||
| 100 | ), |
||
| 101 | ), |
||
| 102 | ); |
||
| 103 | } |
||
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * Checks the authentication headers if supplied. |
||
| 108 | * |
||
| 109 | * @since 4.4.0 |
||
| 110 | * @access public |
||
| 111 | * |
||
| 112 | * @return WP_Error|null WP_Error indicates unsuccessful login, null indicates successful |
||
| 113 | * or no authentication provided |
||
| 114 | */ |
||
| 115 | public function check_authentication() { |
||
| 116 | /** |
||
| 117 | * Pass an authentication error to the API |
||
| 118 | * |
||
| 119 | * This is used to pass a WP_Error from an authentication method back to |
||
| 120 | * the API. |
||
| 121 | * |
||
| 122 | * Authentication methods should check first if they're being used, as |
||
| 123 | * multiple authentication methods can be enabled on a site (cookies, |
||
| 124 | * HTTP basic auth, OAuth). If the authentication method hooked in is |
||
| 125 | * not actually being attempted, null should be returned to indicate |
||
| 126 | * another authentication method should check instead. Similarly, |
||
| 127 | * callbacks should ensure the value is `null` before checking for |
||
| 128 | * errors. |
||
| 129 | * |
||
| 130 | * A WP_Error instance can be returned if an error occurs, and this should |
||
| 131 | * match the format used by API methods internally (that is, the `status` |
||
| 132 | * data should be used). A callback can return `true` to indicate that |
||
| 133 | * the authentication method was used, and it succeeded. |
||
| 134 | * |
||
| 135 | * @since 4.4.0 |
||
| 136 | * |
||
| 137 | * @param WP_Error|null|bool WP_Error if authentication error, null if authentication |
||
| 138 | * method wasn't used, true if authentication succeeded. |
||
| 139 | */ |
||
| 140 | return apply_filters( 'rest_authentication_errors', null ); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Converts an error to a response object. |
||
| 145 | * |
||
| 146 | * This iterates over all error codes and messages to change it into a flat |
||
| 147 | * array. This enables simpler client behaviour, as it is represented as a |
||
| 148 | * list in JSON rather than an object/map. |
||
| 149 | * |
||
| 150 | * @since 4.4.0 |
||
| 151 | * @access protected |
||
| 152 | * |
||
| 153 | * @param WP_Error $error WP_Error instance. |
||
| 154 | * @return WP_REST_Response List of associative arrays with code and message keys. |
||
| 155 | */ |
||
| 156 | protected function error_to_response( $error ) { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Retrieves an appropriate error representation in JSON. |
||
| 187 | * |
||
| 188 | * Note: This should only be used in WP_REST_Server::serve_request(), as it |
||
| 189 | * cannot handle WP_Error internally. All callbacks and other internal methods |
||
| 190 | * should instead return a WP_Error with the data set to an array that includes |
||
| 191 | * a 'status' key, with the value being the HTTP status to send. |
||
| 192 | * |
||
| 193 | * @since 4.4.0 |
||
| 194 | * @access protected |
||
| 195 | * |
||
| 196 | * @param string $code WP_Error-style code. |
||
| 197 | * @param string $message Human-readable message. |
||
| 198 | * @param int $status Optional. HTTP status code to send. Default null. |
||
| 199 | * @return string JSON representation of the error |
||
| 200 | */ |
||
| 201 | protected function json_error( $code, $message, $status = null ) { |
||
| 202 | if ( $status ) { |
||
|
|
|||
| 203 | $this->set_status( $status ); |
||
| 204 | } |
||
| 205 | |||
| 206 | $error = compact( 'code', 'message' ); |
||
| 207 | |||
| 208 | return wp_json_encode( $error ); |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Handles serving an API request. |
||
| 213 | * |
||
| 214 | * Matches the current server URI to a route and runs the first matching |
||
| 215 | * callback then outputs a JSON representation of the returned value. |
||
| 216 | * |
||
| 217 | * @since 4.4.0 |
||
| 218 | * @access public |
||
| 219 | * |
||
| 220 | * @see WP_REST_Server::dispatch() |
||
| 221 | * |
||
| 222 | * @param string $path Optional. The request route. If not set, `$_SERVER['PATH_INFO']` will be used. |
||
| 223 | * Default null. |
||
| 224 | * @return false|null Null if not served and a HEAD request, false otherwise. |
||
| 225 | */ |
||
| 226 | public function serve_request( $path = null ) { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Converts a response to data to send. |
||
| 409 | * |
||
| 410 | * @since 4.4.0 |
||
| 411 | * @access public |
||
| 412 | * |
||
| 413 | * @param WP_REST_Response $response Response object. |
||
| 414 | * @param bool $embed Whether links should be embedded. |
||
| 415 | * @return array { |
||
| 416 | * Data with sub-requests embedded. |
||
| 417 | * |
||
| 418 | * @type array [$_links] Links. |
||
| 419 | * @type array [$_embedded] Embeddeds. |
||
| 420 | * } |
||
| 421 | */ |
||
| 422 | public function response_to_data( $response, $embed ) { |
||
| 423 | $data = $response->get_data(); |
||
| 424 | $links = $this->get_compact_response_links( $response ); |
||
| 425 | |||
| 426 | if ( ! empty( $links ) ) { |
||
| 427 | // Convert links to part of the data. |
||
| 428 | $data['_links'] = $links; |
||
| 429 | } |
||
| 430 | if ( $embed ) { |
||
| 431 | // Determine if this is a numeric array. |
||
| 432 | if ( wp_is_numeric_array( $data ) ) { |
||
| 433 | $data = array_map( array( $this, 'embed_links' ), $data ); |
||
| 434 | } else { |
||
| 435 | $data = $this->embed_links( $data ); |
||
| 436 | } |
||
| 437 | } |
||
| 438 | |||
| 439 | return $data; |
||
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Retrieves links from a response. |
||
| 444 | * |
||
| 445 | * Extracts the links from a response into a structured hash, suitable for |
||
| 446 | * direct output. |
||
| 447 | * |
||
| 448 | * @since 4.4.0 |
||
| 449 | * @access public |
||
| 450 | * @static |
||
| 451 | * |
||
| 452 | * @param WP_REST_Response $response Response to extract links from. |
||
| 453 | * @return array Map of link relation to list of link hashes. |
||
| 454 | */ |
||
| 455 | public static function get_response_links( $response ) { |
||
| 456 | $links = $response->get_links(); |
||
| 457 | if ( empty( $links ) ) { |
||
| 458 | return array(); |
||
| 459 | } |
||
| 460 | |||
| 461 | // Convert links to part of the data. |
||
| 462 | $data = array(); |
||
| 463 | foreach ( $links as $rel => $items ) { |
||
| 464 | $data[ $rel ] = array(); |
||
| 465 | |||
| 466 | foreach ( $items as $item ) { |
||
| 467 | $attributes = $item['attributes']; |
||
| 468 | $attributes['href'] = $item['href']; |
||
| 469 | $data[ $rel ][] = $attributes; |
||
| 470 | } |
||
| 471 | } |
||
| 472 | |||
| 473 | return $data; |
||
| 474 | } |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Retrieves the CURIEs (compact URIs) used for relations. |
||
| 478 | * |
||
| 479 | * Extracts the links from a response into a structured hash, suitable for |
||
| 480 | * direct output. |
||
| 481 | * |
||
| 482 | * @since 4.5.0 |
||
| 483 | * @access public |
||
| 484 | * @static |
||
| 485 | * |
||
| 486 | * @param WP_REST_Response $response Response to extract links from. |
||
| 487 | * @return array Map of link relation to list of link hashes. |
||
| 488 | */ |
||
| 489 | public static function get_compact_response_links( $response ) { |
||
| 490 | $links = self::get_response_links( $response ); |
||
| 491 | |||
| 492 | if ( empty( $links ) ) { |
||
| 493 | return array(); |
||
| 494 | } |
||
| 495 | |||
| 496 | $curies = $response->get_curies(); |
||
| 497 | $used_curies = array(); |
||
| 498 | |||
| 499 | foreach ( $links as $rel => $items ) { |
||
| 500 | |||
| 501 | // Convert $rel URIs to their compact versions if they exist. |
||
| 502 | foreach ( $curies as $curie ) { |
||
| 503 | $href_prefix = substr( $curie['href'], 0, strpos( $curie['href'], '{rel}' ) ); |
||
| 504 | if ( strpos( $rel, $href_prefix ) !== 0 ) { |
||
| 505 | continue; |
||
| 506 | } |
||
| 507 | |||
| 508 | // Relation now changes from '$uri' to '$curie:$relation' |
||
| 509 | $rel_regex = str_replace( '\{rel\}', '(.+)', preg_quote( $curie['href'], '!' ) ); |
||
| 510 | preg_match( '!' . $rel_regex . '!', $rel, $matches ); |
||
| 511 | if ( $matches ) { |
||
| 512 | $new_rel = $curie['name'] . ':' . $matches[1]; |
||
| 513 | $used_curies[ $curie['name'] ] = $curie; |
||
| 514 | $links[ $new_rel ] = $items; |
||
| 515 | unset( $links[ $rel ] ); |
||
| 516 | break; |
||
| 517 | } |
||
| 518 | } |
||
| 519 | } |
||
| 520 | |||
| 521 | // Push the curies onto the start of the links array. |
||
| 522 | if ( $used_curies ) { |
||
| 523 | $links['curies'] = array_values( $used_curies ); |
||
| 524 | } |
||
| 525 | |||
| 526 | return $links; |
||
| 527 | } |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Embeds the links from the data into the request. |
||
| 531 | * |
||
| 532 | * @since 4.4.0 |
||
| 533 | * @access protected |
||
| 534 | * |
||
| 535 | * @param array $data Data from the request. |
||
| 536 | * @return array { |
||
| 537 | * Data with sub-requests embedded. |
||
| 538 | * |
||
| 539 | * @type array [$_links] Links. |
||
| 540 | * @type array [$_embedded] Embeddeds. |
||
| 541 | * } |
||
| 542 | */ |
||
| 543 | protected function embed_links( $data ) { |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Wraps the response in an envelope. |
||
| 602 | * |
||
| 603 | * The enveloping technique is used to work around browser/client |
||
| 604 | * compatibility issues. Essentially, it converts the full HTTP response to |
||
| 605 | * data instead. |
||
| 606 | * |
||
| 607 | * @since 4.4.0 |
||
| 608 | * @access public |
||
| 609 | * |
||
| 610 | * @param WP_REST_Response $response Response object. |
||
| 611 | * @param bool $embed Whether links should be embedded. |
||
| 612 | * @return WP_REST_Response New response with wrapped data |
||
| 613 | */ |
||
| 614 | public function envelope_response( $response, $embed ) { |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Registers a route to the server. |
||
| 637 | * |
||
| 638 | * @since 4.4.0 |
||
| 639 | * @access public |
||
| 640 | * |
||
| 641 | * @param string $namespace Namespace. |
||
| 642 | * @param string $route The REST route. |
||
| 643 | * @param array $route_args Route arguments. |
||
| 644 | * @param bool $override Optional. Whether the route should be overriden if it already exists. |
||
| 645 | * Default false. |
||
| 646 | */ |
||
| 647 | public function register_route( $namespace, $route, $route_args, $override = false ) { |
||
| 648 | if ( ! isset( $this->namespaces[ $namespace ] ) ) { |
||
| 649 | $this->namespaces[ $namespace ] = array(); |
||
| 650 | |||
| 651 | $this->register_route( $namespace, '/' . $namespace, array( |
||
| 652 | array( |
||
| 653 | 'methods' => self::READABLE, |
||
| 654 | 'callback' => array( $this, 'get_namespace_index' ), |
||
| 655 | 'args' => array( |
||
| 656 | 'namespace' => array( |
||
| 657 | 'default' => $namespace, |
||
| 658 | ), |
||
| 659 | 'context' => array( |
||
| 660 | 'default' => 'view', |
||
| 661 | ), |
||
| 662 | ), |
||
| 663 | ), |
||
| 664 | ) ); |
||
| 665 | } |
||
| 666 | |||
| 667 | // Associative to avoid double-registration. |
||
| 668 | $this->namespaces[ $namespace ][ $route ] = true; |
||
| 669 | $route_args['namespace'] = $namespace; |
||
| 670 | |||
| 671 | if ( $override || empty( $this->endpoints[ $route ] ) ) { |
||
| 672 | $this->endpoints[ $route ] = $route_args; |
||
| 673 | } else { |
||
| 674 | $this->endpoints[ $route ] = array_merge( $this->endpoints[ $route ], $route_args ); |
||
| 675 | } |
||
| 676 | } |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Retrieves the route map. |
||
| 680 | * |
||
| 681 | * The route map is an associative array with path regexes as the keys. The |
||
| 682 | * value is an indexed array with the callback function/method as the first |
||
| 683 | * item, and a bitmask of HTTP methods as the second item (see the class |
||
| 684 | * constants). |
||
| 685 | * |
||
| 686 | * Each route can be mapped to more than one callback by using an array of |
||
| 687 | * the indexed arrays. This allows mapping e.g. GET requests to one callback |
||
| 688 | * and POST requests to another. |
||
| 689 | * |
||
| 690 | * Note that the path regexes (array keys) must have @ escaped, as this is |
||
| 691 | * used as the delimiter with preg_match() |
||
| 692 | * |
||
| 693 | * @since 4.4.0 |
||
| 694 | * @access public |
||
| 695 | * |
||
| 696 | * @return array `'/path/regex' => array( $callback, $bitmask )` or |
||
| 697 | * `'/path/regex' => array( array( $callback, $bitmask ), ...)`. |
||
| 698 | */ |
||
| 699 | public function get_routes() { |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Filters the array of available endpoints. |
||
| 703 | * |
||
| 704 | * @since 4.4.0 |
||
| 705 | * |
||
| 706 | * @param array $endpoints The available endpoints. An array of matching regex patterns, each mapped |
||
| 707 | * to an array of callbacks for the endpoint. These take the format |
||
| 708 | * `'/path/regex' => array( $callback, $bitmask )` or |
||
| 709 | * `'/path/regex' => array( array( $callback, $bitmask ). |
||
| 710 | */ |
||
| 711 | $endpoints = apply_filters( 'rest_endpoints', $this->endpoints ); |
||
| 712 | |||
| 713 | // Normalise the endpoints. |
||
| 714 | $defaults = array( |
||
| 715 | 'methods' => '', |
||
| 716 | 'accept_json' => false, |
||
| 717 | 'accept_raw' => false, |
||
| 718 | 'show_in_index' => true, |
||
| 719 | 'args' => array(), |
||
| 720 | ); |
||
| 721 | |||
| 722 | foreach ( $endpoints as $route => &$handlers ) { |
||
| 723 | |||
| 724 | if ( isset( $handlers['callback'] ) ) { |
||
| 725 | // Single endpoint, add one deeper. |
||
| 726 | $handlers = array( $handlers ); |
||
| 727 | } |
||
| 728 | |||
| 729 | if ( ! isset( $this->route_options[ $route ] ) ) { |
||
| 730 | $this->route_options[ $route ] = array(); |
||
| 731 | } |
||
| 732 | |||
| 733 | foreach ( $handlers as $key => &$handler ) { |
||
| 734 | |||
| 735 | if ( ! is_numeric( $key ) ) { |
||
| 736 | // Route option, move it to the options. |
||
| 737 | $this->route_options[ $route ][ $key ] = $handler; |
||
| 738 | unset( $handlers[ $key ] ); |
||
| 739 | continue; |
||
| 740 | } |
||
| 741 | |||
| 742 | $handler = wp_parse_args( $handler, $defaults ); |
||
| 743 | |||
| 744 | // Allow comma-separated HTTP methods. |
||
| 745 | if ( is_string( $handler['methods'] ) ) { |
||
| 746 | $methods = explode( ',', $handler['methods'] ); |
||
| 747 | } else if ( is_array( $handler['methods'] ) ) { |
||
| 748 | $methods = $handler['methods']; |
||
| 749 | } else { |
||
| 750 | $methods = array(); |
||
| 751 | } |
||
| 752 | |||
| 753 | $handler['methods'] = array(); |
||
| 754 | |||
| 755 | foreach ( $methods as $method ) { |
||
| 756 | $method = strtoupper( trim( $method ) ); |
||
| 757 | $handler['methods'][ $method ] = true; |
||
| 758 | } |
||
| 759 | } |
||
| 760 | } |
||
| 761 | return $endpoints; |
||
| 762 | } |
||
| 763 | |||
| 764 | /** |
||
| 765 | * Retrieves namespaces registered on the server. |
||
| 766 | * |
||
| 767 | * @since 4.4.0 |
||
| 768 | * @access public |
||
| 769 | * |
||
| 770 | * @return array List of registered namespaces. |
||
| 771 | */ |
||
| 772 | public function get_namespaces() { |
||
| 775 | |||
| 776 | /** |
||
| 777 | * Retrieves specified options for a route. |
||
| 778 | * |
||
| 779 | * @since 4.4.0 |
||
| 780 | * @access public |
||
| 781 | * |
||
| 782 | * @param string $route Route pattern to fetch options for. |
||
| 783 | * @return array|null Data as an associative array if found, or null if not found. |
||
| 784 | */ |
||
| 785 | public function get_route_options( $route ) { |
||
| 786 | if ( ! isset( $this->route_options[ $route ] ) ) { |
||
| 787 | return null; |
||
| 788 | } |
||
| 789 | |||
| 790 | return $this->route_options[ $route ]; |
||
| 791 | } |
||
| 792 | |||
| 793 | /** |
||
| 794 | * Matches the request to a callback and call it. |
||
| 795 | * |
||
| 796 | * @since 4.4.0 |
||
| 797 | * @access public |
||
| 798 | * |
||
| 799 | * @param WP_REST_Request $request Request to attempt dispatching. |
||
| 800 | * @return WP_REST_Response Response returned by the callback. |
||
| 801 | */ |
||
| 802 | public function dispatch( $request ) { |
||
| 926 | |||
| 927 | /** |
||
| 928 | * Returns if an error occurred during most recent JSON encode/decode. |
||
| 929 | * |
||
| 930 | * Strings to be translated will be in format like |
||
| 931 | * "Encoding error: Maximum stack depth exceeded". |
||
| 932 | * |
||
| 933 | * @since 4.4.0 |
||
| 934 | * @access protected |
||
| 935 | * |
||
| 936 | * @return bool|string Boolean false or string error message. |
||
| 937 | */ |
||
| 938 | protected function get_json_last_error() { |
||
| 939 | // See https://core.trac.wordpress.org/ticket/27799. |
||
| 940 | if ( ! function_exists( 'json_last_error' ) ) { |
||
| 941 | return false; |
||
| 942 | } |
||
| 943 | |||
| 944 | $last_error_code = json_last_error(); |
||
| 945 | |||
| 946 | if ( ( defined( 'JSON_ERROR_NONE' ) && JSON_ERROR_NONE === $last_error_code ) || empty( $last_error_code ) ) { |
||
| 947 | return false; |
||
| 948 | } |
||
| 949 | |||
| 950 | return json_last_error_msg(); |
||
| 951 | } |
||
| 952 | |||
| 953 | /** |
||
| 954 | * Retrieves the site index. |
||
| 955 | * |
||
| 956 | * This endpoint describes the capabilities of the site. |
||
| 957 | * |
||
| 958 | * @since 4.4.0 |
||
| 959 | * @access public |
||
| 960 | * |
||
| 961 | * @param array $request { |
||
| 962 | * Request. |
||
| 963 | * |
||
| 964 | * @type string $context Context. |
||
| 965 | * } |
||
| 966 | * @return array Index entity |
||
| 967 | */ |
||
| 968 | public function get_index( $request ) { |
||
| 997 | |||
| 998 | /** |
||
| 999 | * Retrieves the index for a namespace. |
||
| 1000 | * |
||
| 1001 | * @since 4.4.0 |
||
| 1002 | * @access public |
||
| 1003 | * |
||
| 1004 | * @param WP_REST_Request $request REST request instance. |
||
| 1005 | * @return WP_REST_Response|WP_Error WP_REST_Response instance if the index was found, |
||
| 1006 | * WP_Error if the namespace isn't set. |
||
| 1007 | */ |
||
| 1008 | public function get_namespace_index( $request ) { |
||
| 1040 | |||
| 1041 | /** |
||
| 1042 | * Retrieves the publicly-visible data for routes. |
||
| 1043 | * |
||
| 1044 | * @since 4.4.0 |
||
| 1045 | * @access public |
||
| 1046 | * |
||
| 1047 | * @param array $routes Routes to get data for. |
||
| 1048 | * @param string $context Optional. Context for data. Accepts 'view' or 'help'. Default 'view'. |
||
| 1049 | * @return array Route data to expose in indexes. |
||
| 1050 | */ |
||
| 1051 | public function get_data_for_routes( $routes, $context = 'view' ) { |
||
| 1085 | |||
| 1086 | /** |
||
| 1087 | * Retrieves publicly-visible data for the route. |
||
| 1088 | * |
||
| 1089 | * @since 4.4.0 |
||
| 1090 | * @access public |
||
| 1091 | * |
||
| 1092 | * @param string $route Route to get data for. |
||
| 1093 | * @param array $callbacks Callbacks to convert to data. |
||
| 1094 | * @param string $context Optional. Context for the data. Accepts 'view' or 'help'. Default 'view'. |
||
| 1095 | * @return array|null Data for the route, or null if no publicly-visible data. |
||
| 1096 | */ |
||
| 1097 | public function get_data_for_route( $route, $callbacks, $context = 'view' ) { |
||
| 1165 | |||
| 1166 | /** |
||
| 1167 | * Sends an HTTP status code. |
||
| 1168 | * |
||
| 1169 | * @since 4.4.0 |
||
| 1170 | * @access protected |
||
| 1171 | * |
||
| 1172 | * @param int $code HTTP status. |
||
| 1173 | */ |
||
| 1174 | protected function set_status( $code ) { |
||
| 1175 | status_header( $code ); |
||
| 1176 | } |
||
| 1177 | |||
| 1178 | /** |
||
| 1179 | * Sends an HTTP header. |
||
| 1180 | * |
||
| 1181 | * @since 4.4.0 |
||
| 1182 | * @access public |
||
| 1183 | * |
||
| 1184 | * @param string $key Header key. |
||
| 1185 | * @param string $value Header value. |
||
| 1186 | */ |
||
| 1187 | public function send_header( $key, $value ) { |
||
| 1188 | /* |
||
| 1189 | * Sanitize as per RFC2616 (Section 4.2): |
||
| 1190 | * |
||
| 1191 | * Any LWS that occurs between field-content MAY be replaced with a |
||
| 1192 | * single SP before interpreting the field value or forwarding the |
||
| 1193 | * message downstream. |
||
| 1194 | */ |
||
| 1195 | $value = preg_replace( '/\s+/', ' ', $value ); |
||
| 1196 | header( sprintf( '%s: %s', $key, $value ) ); |
||
| 1197 | } |
||
| 1198 | |||
| 1199 | /** |
||
| 1200 | * Sends multiple HTTP headers. |
||
| 1201 | * |
||
| 1202 | * @since 4.4.0 |
||
| 1203 | * @access public |
||
| 1204 | * |
||
| 1205 | * @param array $headers Map of header name to header value. |
||
| 1206 | */ |
||
| 1207 | public function send_headers( $headers ) { |
||
| 1208 | foreach ( $headers as $key => $value ) { |
||
| 1209 | $this->send_header( $key, $value ); |
||
| 1210 | } |
||
| 1211 | } |
||
| 1212 | |||
| 1213 | /** |
||
| 1214 | * Retrieves the raw request entity (body). |
||
| 1215 | * |
||
| 1216 | * @since 4.4.0 |
||
| 1217 | * @access public |
||
| 1218 | * |
||
| 1219 | * @global string $HTTP_RAW_POST_DATA Raw post data. |
||
| 1220 | * |
||
| 1221 | * @return string Raw request data. |
||
| 1222 | */ |
||
| 1223 | public static function get_raw_data() { |
||
| 1236 | |||
| 1237 | /** |
||
| 1238 | * Extracts headers from a PHP-style $_SERVER array. |
||
| 1239 | * |
||
| 1240 | * @since 4.4.0 |
||
| 1241 | * @access public |
||
| 1242 | * |
||
| 1243 | * @param array $server Associative array similar to `$_SERVER`. |
||
| 1244 | * @return array Headers extracted from the input. |
||
| 1245 | */ |
||
| 1246 | public function get_headers( $server ) { |
||
| 1247 | $headers = array(); |
||
| 1248 | |||
| 1249 | // CONTENT_* headers are not prefixed with HTTP_. |
||
| 1262 | } |
||
| 1263 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: