123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php
- if ( ! defined( 'ABSPATH' ) ) {
- exit;
- }
- if ( class_exists( 'ACF_Rest_Request' ) ) {
- return;
- }
- class ACF_Rest_Request {
-
- private $readonly_props = array( 'object_type', 'object_sub_type', 'child_object_type', 'http_method' );
-
- private $http_method;
-
- private $current_route;
-
- private $supported_routes = array();
-
- private $url_params = array();
-
- private $object_type;
-
- private $object_sub_type;
-
- private $child_object_type;
-
- public function parse_request( $request ) {
- $this->set_http_method();
- $this->set_current_route( $request );
- $this->build_supported_routes();
- $this->set_url_params();
- $this->set_object_types();
- }
-
- public function __get( $name ) {
- if ( in_array( $name, $this->readonly_props ) ) {
- return $this->$name;
- }
- return null;
- }
-
- public function get_url_param( $param ) {
- return isset( $this->url_params[ $param ] ) ? $this->url_params[ $param ] : null;
- }
-
- private function set_http_method() {
- $this->http_method = 'GET';
- if ( ! empty( $_SERVER['REQUEST_METHOD'] ) ) {
- $this->http_method = strtoupper( sanitize_text_field( $_SERVER['REQUEST_METHOD'] ) );
- }
-
-
-
-
- if ( isset( $_GET['_method'] ) ) {
- $this->http_method = strtoupper( sanitize_text_field( $_GET['_method'] ) );
- } elseif ( isset( $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] ) ) {
- $this->http_method = strtoupper( sanitize_text_field( $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] ) );
- }
-
- }
-
- private function set_current_route( $request ) {
- if ( $request ) {
- $this->current_route = $request->get_route();
- } else {
- $this->current_route = empty( $GLOBALS['wp']->query_vars['rest_route'] ) ? null : $GLOBALS['wp']->query_vars['rest_route'];
- }
- }
-
- private function build_supported_routes() {
-
-
- foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
- $rest_base = acf_get_object_type_rest_base( $post_type );
- $this->supported_routes[] = "/wp/v2/(?P<rest_base>{$rest_base})";
- $this->supported_routes[] = "/wp/v2/(?P<rest_base>{$rest_base})/(?P<id>[\d]+)";
- if ( post_type_supports( $post_type->name, 'revisions' ) ) {
- $this->supported_routes[] = "/wp/v2/(?P<rest_base>{$rest_base})/(?P<id>[\d]+)/(?P<child_rest_base>revisions)";
- $this->supported_routes[] = "/wp/v2/(?P<rest_base>{$rest_base})/(?P<id>[\d]+)/(?P<child_rest_base>revisions)/(?P<child_id>[\d]+)";
- }
- if ( 'attachment' !== $post_type->name ) {
- $this->supported_routes[] = "/wp/v2/(?P<rest_base>{$rest_base})/(?P<id>[\d]+)/(?P<child_rest_base>autosaves)";
- $this->supported_routes[] = "/wp/v2/(?P<rest_base>{$rest_base})/(?P<id>[\d]+)/(?P<child_rest_base>autosaves)/(?P<child_id>[\d]+)";
- }
- }
-
-
- foreach ( get_taxonomies( array( 'show_in_rest' => true ), 'object' ) as $taxonomy ) {
- $rest_base = acf_get_object_type_rest_base( $taxonomy );
- $this->supported_routes[] = "/wp/v2/(?P<rest_base>{$rest_base})";
- $this->supported_routes[] = "/wp/v2/(?P<rest_base>{$rest_base})/(?P<id>[\d]+)";
- }
-
- $this->supported_routes[] = '/wp/v2/(?P<rest_base>users)';
- $this->supported_routes[] = '/wp/v2/(?P<rest_base>users)/(?P<id>[\d]+)';
- $this->supported_routes[] = '/wp/v2/(?P<rest_base>users)/me';
-
- $this->supported_routes[] = '/wp/v2/(?P<rest_base>comments)';
- $this->supported_routes[] = '/wp/v2/(?P<rest_base>comments)/(?P<id>[\d]+)';
- }
-
- private function set_url_params() {
- if ( ! $this->supported_routes || ! is_string( $this->current_route ) ) {
- return;
- }
-
- foreach ( $this->supported_routes as $route ) {
- $match = preg_match( '@^' . $route . '$@i', $this->current_route, $matches );
- if ( ! $match ) {
- continue;
- }
- foreach ( $matches as $param => $value ) {
- if ( ! is_int( $param ) ) {
- $this->url_params[ $param ] = $value;
- }
- }
- }
- }
-
- private function set_object_types() {
- $base = $this->get_url_param( 'rest_base' );
- $child_base = $this->get_url_param( 'child_rest_base' );
-
- if ( is_null( $base ) ) {
- return;
- }
-
-
- if ( $base === 'users' ) {
- $this->object_type = $this->object_sub_type = 'user';
- } elseif ( $base === 'comments' ) {
- $this->object_type = $this->object_sub_type = 'comment';
- } elseif ( $post_type = $this->get_post_type_by_rest_base( $base ) ) {
- $this->object_type = 'post';
- $this->object_sub_type = $post_type->name;
-
- if ( in_array( $this->get_url_param( 'child_rest_base' ), array( 'revisions', 'autosaves' ) ) ) {
- $this->child_object_type = $this->object_sub_type . '-revision';
- }
- } elseif ( $taxonomy = $this->get_taxonomy_by_rest_base( $base ) ) {
- $this->object_type = 'term';
- $this->object_sub_type = $taxonomy->name;
- }
- }
-
- private function get_post_type_by_rest_base( $rest_base ) {
- $types = get_post_types( array( 'show_in_rest' => true ), 'objects' );
- foreach ( $types as $type ) {
- if ( acf_get_object_type_rest_base( $type ) === $rest_base ) {
- return $type;
- }
- }
- return null;
- }
-
- private function get_taxonomy_by_rest_base( $rest_base ) {
- $taxonomies = get_taxonomies( array( 'show_in_rest' => true ), 'objects' );
- foreach ( $taxonomies as $taxonomy ) {
- if ( acf_get_object_type_rest_base( $taxonomy ) === $rest_base ) {
- return $taxonomy;
- }
- }
- return null;
- }
- }
|