- Timestamp:
- 11/30/2017 11:09:33 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php
r41760 r42343 57 57 */ 58 58 public function __construct( $taxonomy ) { 59 $this->taxonomy = $taxonomy;59 $this->taxonomy = $taxonomy; 60 60 $this->namespace = 'wp/v2'; 61 $tax_obj = get_taxonomy( $taxonomy );61 $tax_obj = get_taxonomy( $taxonomy ); 62 62 $this->rest_base = ! empty( $tax_obj->rest_base ) ? $tax_obj->rest_base : $tax_obj->name; 63 63 … … 74 74 public function register_routes() { 75 75 76 register_rest_route( $this->namespace, '/' . $this->rest_base, array( 77 array( 78 'methods' => WP_REST_Server::READABLE, 79 'callback' => array( $this, 'get_items' ), 80 'permission_callback' => array( $this, 'get_items_permissions_check' ), 81 'args' => $this->get_collection_params(), 82 ), 83 array( 84 'methods' => WP_REST_Server::CREATABLE, 85 'callback' => array( $this, 'create_item' ), 86 'permission_callback' => array( $this, 'create_item_permissions_check' ), 87 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), 88 ), 89 'schema' => array( $this, 'get_public_item_schema' ), 90 ) ); 91 92 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( 93 'args' => array( 94 'id' => array( 95 'description' => __( 'Unique identifier for the term.' ), 96 'type' => 'integer', 97 ), 98 ), 99 array( 100 'methods' => WP_REST_Server::READABLE, 101 'callback' => array( $this, 'get_item' ), 102 'permission_callback' => array( $this, 'get_item_permissions_check' ), 103 'args' => array( 104 'context' => $this->get_context_param( array( 'default' => 'view' ) ), 105 ), 106 ), 107 array( 108 'methods' => WP_REST_Server::EDITABLE, 109 'callback' => array( $this, 'update_item' ), 110 'permission_callback' => array( $this, 'update_item_permissions_check' ), 111 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), 112 ), 113 array( 114 'methods' => WP_REST_Server::DELETABLE, 115 'callback' => array( $this, 'delete_item' ), 116 'permission_callback' => array( $this, 'delete_item_permissions_check' ), 117 'args' => array( 118 'force' => array( 119 'type' => 'boolean', 120 'default' => false, 121 'description' => __( 'Required to be true, as terms do not support trashing.' ), 76 register_rest_route( 77 $this->namespace, '/' . $this->rest_base, array( 78 array( 79 'methods' => WP_REST_Server::READABLE, 80 'callback' => array( $this, 'get_items' ), 81 'permission_callback' => array( $this, 'get_items_permissions_check' ), 82 'args' => $this->get_collection_params(), 83 ), 84 array( 85 'methods' => WP_REST_Server::CREATABLE, 86 'callback' => array( $this, 'create_item' ), 87 'permission_callback' => array( $this, 'create_item_permissions_check' ), 88 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), 89 ), 90 'schema' => array( $this, 'get_public_item_schema' ), 91 ) 92 ); 93 94 register_rest_route( 95 $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( 96 'args' => array( 97 'id' => array( 98 'description' => __( 'Unique identifier for the term.' ), 99 'type' => 'integer', 122 100 ), 123 101 ), 124 ), 125 'schema' => array( $this, 'get_public_item_schema' ), 126 ) ); 102 array( 103 'methods' => WP_REST_Server::READABLE, 104 'callback' => array( $this, 'get_item' ), 105 'permission_callback' => array( $this, 'get_item_permissions_check' ), 106 'args' => array( 107 'context' => $this->get_context_param( array( 'default' => 'view' ) ), 108 ), 109 ), 110 array( 111 'methods' => WP_REST_Server::EDITABLE, 112 'callback' => array( $this, 'update_item' ), 113 'permission_callback' => array( $this, 'update_item_permissions_check' ), 114 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), 115 ), 116 array( 117 'methods' => WP_REST_Server::DELETABLE, 118 'callback' => array( $this, 'delete_item' ), 119 'permission_callback' => array( $this, 'delete_item_permissions_check' ), 120 'args' => array( 121 'force' => array( 122 'type' => 'boolean', 123 'default' => false, 124 'description' => __( 'Required to be true, as terms do not support trashing.' ), 125 ), 126 ), 127 ), 128 'schema' => array( $this, 'get_public_item_schema' ), 129 ) 130 ); 127 131 } 128 132 … … 236 240 $prepared_args = apply_filters( "rest_{$this->taxonomy}_query", $prepared_args, $request ); 237 241 238 if ( ! empty( $prepared_args['post'] ) ) {242 if ( ! empty( $prepared_args['post'] ) ) { 239 243 $query_result = wp_get_object_terms( $prepared_args['post'], $this->taxonomy, $prepared_args ); 240 244 … … 259 263 260 264 foreach ( $query_result as $term ) { 261 $data = $this->prepare_item_for_response( $term, $request );265 $data = $this->prepare_item_for_response( $term, $request ); 262 266 $response[] = $this->prepare_response_for_collection( $data ); 263 267 } … … 602 606 603 607 $response = new WP_REST_Response(); 604 $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) ); 608 $response->set_data( 609 array( 610 'deleted' => true, 611 'previous' => $previous->get_data(), 612 ) 613 ); 605 614 606 615 /** … … 755 764 */ 756 765 protected function prepare_links( $term ) { 757 $base = $this->namespace . '/' . $this->rest_base;766 $base = $this->namespace . '/' . $this->rest_base; 758 767 $links = array( 759 768 'self' => array( … … 794 803 } 795 804 796 $rest_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;805 $rest_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name; 797 806 $post_type_links[] = array( 798 807 'href' => add_query_arg( $this->rest_base, $term->term_id, rest_url( sprintf( 'wp/v2/%s', $rest_base ) ) ), … … 821 830 'properties' => array( 822 831 'id' => array( 823 'description' => __( 'Unique identifier for the term.' ),824 'type' => 'integer',825 'context' => array( 'view', 'embed', 'edit' ),826 'readonly' => true,832 'description' => __( 'Unique identifier for the term.' ), 833 'type' => 'integer', 834 'context' => array( 'view', 'embed', 'edit' ), 835 'readonly' => true, 827 836 ), 828 837 'count' => array( 829 'description' => __( 'Number of published posts for the term.' ),830 'type' => 'integer',831 'context' => array( 'view', 'edit' ),832 'readonly' => true,838 'description' => __( 'Number of published posts for the term.' ), 839 'type' => 'integer', 840 'context' => array( 'view', 'edit' ), 841 'readonly' => true, 833 842 ), 834 843 'description' => array( 835 'description' => __( 'HTML description of the term.' ),836 'type' => 'string',837 'context' => array( 'view', 'edit' ),844 'description' => __( 'HTML description of the term.' ), 845 'type' => 'string', 846 'context' => array( 'view', 'edit' ), 838 847 ), 839 848 'link' => array( 840 'description' => __( 'URL of the term.' ),841 'type' => 'string',842 'format' => 'uri',843 'context' => array( 'view', 'embed', 'edit' ),844 'readonly' => true,849 'description' => __( 'URL of the term.' ), 850 'type' => 'string', 851 'format' => 'uri', 852 'context' => array( 'view', 'embed', 'edit' ), 853 'readonly' => true, 845 854 ), 846 855 'name' => array( 847 'description' => __( 'HTML title for the term.' ),848 'type' => 'string',849 'context' => array( 'view', 'embed', 'edit' ),850 'arg_options' => array(856 'description' => __( 'HTML title for the term.' ), 857 'type' => 'string', 858 'context' => array( 'view', 'embed', 'edit' ), 859 'arg_options' => array( 851 860 'sanitize_callback' => 'sanitize_text_field', 852 861 ), 853 'required' => true,862 'required' => true, 854 863 ), 855 864 'slug' => array( 856 'description' => __( 'An alphanumeric identifier for the term unique to its type.' ),857 'type' => 'string',858 'context' => array( 'view', 'embed', 'edit' ),859 'arg_options' => array(865 'description' => __( 'An alphanumeric identifier for the term unique to its type.' ), 866 'type' => 'string', 867 'context' => array( 'view', 'embed', 'edit' ), 868 'arg_options' => array( 860 869 'sanitize_callback' => array( $this, 'sanitize_slug' ), 861 870 ), 862 871 ), 863 872 'taxonomy' => array( 864 'description' => __( 'Type attribution for the term.' ),865 'type' => 'string',866 'enum' => array_keys( get_taxonomies() ),867 'context' => array( 'view', 'embed', 'edit' ),868 'readonly' => true,873 'description' => __( 'Type attribution for the term.' ), 874 'type' => 'string', 875 'enum' => array_keys( get_taxonomies() ), 876 'context' => array( 'view', 'embed', 'edit' ), 877 'readonly' => true, 869 878 ), 870 879 ), … … 875 884 if ( $taxonomy->hierarchical ) { 876 885 $schema['properties']['parent'] = array( 877 'description' => __( 'The parent term ID.' ),878 'type' => 'integer',879 'context' => array( 'view', 'edit' ),886 'description' => __( 'The parent term ID.' ), 887 'type' => 'integer', 888 'context' => array( 'view', 'edit' ), 880 889 ); 881 890 } … … 895 904 public function get_collection_params() { 896 905 $query_params = parent::get_collection_params(); 897 $taxonomy = get_taxonomy( $this->taxonomy );906 $taxonomy = get_taxonomy( $this->taxonomy ); 898 907 899 908 $query_params['context']['default'] = 'view'; 900 909 901 910 $query_params['exclude'] = array( 902 'description' => __( 'Ensure result set excludes specific IDs.' ),903 'type' => 'array',904 'items' => array(905 'type' => 'integer',911 'description' => __( 'Ensure result set excludes specific IDs.' ), 912 'type' => 'array', 913 'items' => array( 914 'type' => 'integer', 906 915 ), 907 'default' => array(),916 'default' => array(), 908 917 ); 909 918 910 919 $query_params['include'] = array( 911 'description' => __( 'Limit result set to specific IDs.' ),912 'type' => 'array',913 'items' => array(914 'type' => 'integer',920 'description' => __( 'Limit result set to specific IDs.' ), 921 'type' => 'array', 922 'items' => array( 923 'type' => 'integer', 915 924 ), 916 'default' => array(),925 'default' => array(), 917 926 ); 918 927 919 928 if ( ! $taxonomy->hierarchical ) { 920 929 $query_params['offset'] = array( 921 'description' => __( 'Offset the result set by a specific number of items.' ),922 'type' => 'integer',930 'description' => __( 'Offset the result set by a specific number of items.' ), 931 'type' => 'integer', 923 932 ); 924 933 } 925 934 926 935 $query_params['order'] = array( 927 'description' => __( 'Order sort attribute ascending or descending.' ),928 'type' => 'string',929 'default' => 'asc',930 'enum' => array(936 'description' => __( 'Order sort attribute ascending or descending.' ), 937 'type' => 'string', 938 'default' => 'asc', 939 'enum' => array( 931 940 'asc', 932 941 'desc', … … 935 944 936 945 $query_params['orderby'] = array( 937 'description' => __( 'Sort collection by term attribute.' ),938 'type' => 'string',939 'default' => 'name',940 'enum' => array(946 'description' => __( 'Sort collection by term attribute.' ), 947 'type' => 'string', 948 'default' => 'name', 949 'enum' => array( 941 950 'id', 942 951 'include', … … 951 960 952 961 $query_params['hide_empty'] = array( 953 'description' => __( 'Whether to hide terms not assigned to any posts.' ),954 'type' => 'boolean',955 'default' => false,962 'description' => __( 'Whether to hide terms not assigned to any posts.' ), 963 'type' => 'boolean', 964 'default' => false, 956 965 ); 957 966 958 967 if ( $taxonomy->hierarchical ) { 959 968 $query_params['parent'] = array( 960 'description' => __( 'Limit result set to terms assigned to a specific parent.' ),961 'type' => 'integer',969 'description' => __( 'Limit result set to terms assigned to a specific parent.' ), 970 'type' => 'integer', 962 971 ); 963 972 } 964 973 965 974 $query_params['post'] = array( 966 'description' => __( 'Limit result set to terms assigned to a specific post.' ),967 'type' => 'integer',968 'default' => null,975 'description' => __( 'Limit result set to terms assigned to a specific post.' ), 976 'type' => 'integer', 977 'default' => null, 969 978 ); 970 979 971 980 $query_params['slug'] = array( 972 'description' => __( 'Limit result set to terms with one or more specific slugs.' ),973 'type' => 'array',974 'items' => array(975 'type' => 'string'981 'description' => __( 'Limit result set to terms with one or more specific slugs.' ), 982 'type' => 'array', 983 'items' => array( 984 'type' => 'string', 976 985 ), 977 986 );
Note: See TracChangeset
for help on using the changeset viewer.