71 * |
71 * |
72 * @see register_rest_route() |
72 * @see register_rest_route() |
73 */ |
73 */ |
74 public function register_routes() { |
74 public function register_routes() { |
75 |
75 |
76 register_rest_route( $this->namespace, '/' . $this->rest_base, array( |
76 register_rest_route( |
|
77 $this->namespace, |
|
78 '/' . $this->rest_base, |
77 array( |
79 array( |
78 'methods' => WP_REST_Server::READABLE, |
80 array( |
79 'callback' => array( $this, 'get_items' ), |
81 'methods' => WP_REST_Server::READABLE, |
80 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
82 'callback' => array( $this, 'get_items' ), |
81 'args' => $this->get_collection_params(), |
83 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
82 ), |
84 'args' => $this->get_collection_params(), |
|
85 ), |
|
86 array( |
|
87 'methods' => WP_REST_Server::CREATABLE, |
|
88 'callback' => array( $this, 'create_item' ), |
|
89 'permission_callback' => array( $this, 'create_item_permissions_check' ), |
|
90 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), |
|
91 ), |
|
92 'schema' => array( $this, 'get_public_item_schema' ), |
|
93 ) |
|
94 ); |
|
95 |
|
96 register_rest_route( |
|
97 $this->namespace, |
|
98 '/' . $this->rest_base . '/(?P<id>[\d]+)', |
83 array( |
99 array( |
84 'methods' => WP_REST_Server::CREATABLE, |
100 'args' => array( |
85 'callback' => array( $this, 'create_item' ), |
101 'id' => array( |
86 'permission_callback' => array( $this, 'create_item_permissions_check' ), |
102 'description' => __( 'Unique identifier for the term.' ), |
87 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), |
103 'type' => 'integer', |
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.' ), |
|
122 ), |
104 ), |
123 ), |
105 ), |
124 ), |
106 array( |
125 'schema' => array( $this, 'get_public_item_schema' ), |
107 'methods' => WP_REST_Server::READABLE, |
126 ) ); |
108 'callback' => array( $this, 'get_item' ), |
|
109 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
|
110 'args' => array( |
|
111 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
|
112 ), |
|
113 ), |
|
114 array( |
|
115 'methods' => WP_REST_Server::EDITABLE, |
|
116 'callback' => array( $this, 'update_item' ), |
|
117 'permission_callback' => array( $this, 'update_item_permissions_check' ), |
|
118 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
|
119 ), |
|
120 array( |
|
121 'methods' => WP_REST_Server::DELETABLE, |
|
122 'callback' => array( $this, 'delete_item' ), |
|
123 'permission_callback' => array( $this, 'delete_item_permissions_check' ), |
|
124 'args' => array( |
|
125 'force' => array( |
|
126 'type' => 'boolean', |
|
127 'default' => false, |
|
128 'description' => __( 'Required to be true, as terms do not support trashing.' ), |
|
129 ), |
|
130 ), |
|
131 ), |
|
132 'schema' => array( $this, 'get_public_item_schema' ), |
|
133 ) |
|
134 ); |
127 } |
135 } |
128 |
136 |
129 /** |
137 /** |
130 * Checks if a request has access to read terms in the specified taxonomy. |
138 * Checks if a request has access to read terms in the specified taxonomy. |
131 * |
139 * |
233 * passed to get_terms(). |
241 * passed to get_terms(). |
234 * @param WP_REST_Request $request The current request. |
242 * @param WP_REST_Request $request The current request. |
235 */ |
243 */ |
236 $prepared_args = apply_filters( "rest_{$this->taxonomy}_query", $prepared_args, $request ); |
244 $prepared_args = apply_filters( "rest_{$this->taxonomy}_query", $prepared_args, $request ); |
237 |
245 |
238 if ( ! empty( $prepared_args['post'] ) ) { |
246 if ( ! empty( $prepared_args['post'] ) ) { |
239 $query_result = wp_get_object_terms( $prepared_args['post'], $this->taxonomy, $prepared_args ); |
247 $query_result = wp_get_object_terms( $prepared_args['post'], $this->taxonomy, $prepared_args ); |
240 |
248 |
241 // Used when calling wp_count_terms() below. |
249 // Used when calling wp_count_terms() below. |
242 $prepared_args['object_ids'] = $prepared_args['post']; |
250 $prepared_args['object_ids'] = $prepared_args['post']; |
243 } else { |
251 } else { |
453 if ( is_wp_error( $fields_update ) ) { |
466 if ( is_wp_error( $fields_update ) ) { |
454 return $fields_update; |
467 return $fields_update; |
455 } |
468 } |
456 |
469 |
457 $request->set_param( 'context', 'view' ); |
470 $request->set_param( 'context', 'view' ); |
|
471 |
|
472 /** |
|
473 * Fires after a single term is completely created or updated via the REST API. |
|
474 * |
|
475 * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug. |
|
476 * |
|
477 * @since 5.0.0 |
|
478 * |
|
479 * @param WP_Term $term Inserted or updated term object. |
|
480 * @param WP_REST_Request $request Request object. |
|
481 * @param bool $creating True when creating a term, false when updating. |
|
482 */ |
|
483 do_action( "rest_after_insert_{$this->taxonomy}", $term, $request, true ); |
458 |
484 |
459 $response = $this->prepare_item_for_response( $term, $request ); |
485 $response = $this->prepare_item_for_response( $term, $request ); |
460 $response = rest_ensure_response( $response ); |
486 $response = rest_ensure_response( $response ); |
461 |
487 |
462 $response->set_status( 201 ); |
488 $response->set_status( 201 ); |
821 '$schema' => 'http://json-schema.org/draft-04/schema#', |
859 '$schema' => 'http://json-schema.org/draft-04/schema#', |
822 'title' => 'post_tag' === $this->taxonomy ? 'tag' : $this->taxonomy, |
860 'title' => 'post_tag' === $this->taxonomy ? 'tag' : $this->taxonomy, |
823 'type' => 'object', |
861 'type' => 'object', |
824 'properties' => array( |
862 'properties' => array( |
825 'id' => array( |
863 'id' => array( |
826 'description' => __( 'Unique identifier for the term.' ), |
864 'description' => __( 'Unique identifier for the term.' ), |
827 'type' => 'integer', |
865 'type' => 'integer', |
828 'context' => array( 'view', 'embed', 'edit' ), |
866 'context' => array( 'view', 'embed', 'edit' ), |
829 'readonly' => true, |
867 'readonly' => true, |
830 ), |
868 ), |
831 'count' => array( |
869 'count' => array( |
832 'description' => __( 'Number of published posts for the term.' ), |
870 'description' => __( 'Number of published posts for the term.' ), |
833 'type' => 'integer', |
871 'type' => 'integer', |
834 'context' => array( 'view', 'edit' ), |
872 'context' => array( 'view', 'edit' ), |
835 'readonly' => true, |
873 'readonly' => true, |
836 ), |
874 ), |
837 'description' => array( |
875 'description' => array( |
838 'description' => __( 'HTML description of the term.' ), |
876 'description' => __( 'HTML description of the term.' ), |
839 'type' => 'string', |
877 'type' => 'string', |
840 'context' => array( 'view', 'edit' ), |
878 'context' => array( 'view', 'edit' ), |
841 ), |
879 ), |
842 'link' => array( |
880 'link' => array( |
843 'description' => __( 'URL of the term.' ), |
881 'description' => __( 'URL of the term.' ), |
844 'type' => 'string', |
882 'type' => 'string', |
845 'format' => 'uri', |
883 'format' => 'uri', |
846 'context' => array( 'view', 'embed', 'edit' ), |
884 'context' => array( 'view', 'embed', 'edit' ), |
847 'readonly' => true, |
885 'readonly' => true, |
848 ), |
886 ), |
849 'name' => array( |
887 'name' => array( |
850 'description' => __( 'HTML title for the term.' ), |
888 'description' => __( 'HTML title for the term.' ), |
851 'type' => 'string', |
889 'type' => 'string', |
852 'context' => array( 'view', 'embed', 'edit' ), |
890 'context' => array( 'view', 'embed', 'edit' ), |
853 'arg_options' => array( |
891 'arg_options' => array( |
854 'sanitize_callback' => 'sanitize_text_field', |
892 'sanitize_callback' => 'sanitize_text_field', |
855 ), |
893 ), |
856 'required' => true, |
894 'required' => true, |
857 ), |
895 ), |
858 'slug' => array( |
896 'slug' => array( |
859 'description' => __( 'An alphanumeric identifier for the term unique to its type.' ), |
897 'description' => __( 'An alphanumeric identifier for the term unique to its type.' ), |
860 'type' => 'string', |
898 'type' => 'string', |
861 'context' => array( 'view', 'embed', 'edit' ), |
899 'context' => array( 'view', 'embed', 'edit' ), |
862 'arg_options' => array( |
900 'arg_options' => array( |
863 'sanitize_callback' => array( $this, 'sanitize_slug' ), |
901 'sanitize_callback' => array( $this, 'sanitize_slug' ), |
864 ), |
902 ), |
865 ), |
903 ), |
866 'taxonomy' => array( |
904 'taxonomy' => array( |
867 'description' => __( 'Type attribution for the term.' ), |
905 'description' => __( 'Type attribution for the term.' ), |
868 'type' => 'string', |
906 'type' => 'string', |
869 'enum' => array_keys( get_taxonomies() ), |
907 'enum' => array_keys( get_taxonomies() ), |
870 'context' => array( 'view', 'embed', 'edit' ), |
908 'context' => array( 'view', 'embed', 'edit' ), |
871 'readonly' => true, |
909 'readonly' => true, |
872 ), |
910 ), |
873 ), |
911 ), |
874 ); |
912 ); |
875 |
913 |
876 $taxonomy = get_taxonomy( $this->taxonomy ); |
914 $taxonomy = get_taxonomy( $this->taxonomy ); |
877 |
915 |
878 if ( $taxonomy->hierarchical ) { |
916 if ( $taxonomy->hierarchical ) { |
879 $schema['properties']['parent'] = array( |
917 $schema['properties']['parent'] = array( |
880 'description' => __( 'The parent term ID.' ), |
918 'description' => __( 'The parent term ID.' ), |
881 'type' => 'integer', |
919 'type' => 'integer', |
882 'context' => array( 'view', 'edit' ), |
920 'context' => array( 'view', 'edit' ), |
883 ); |
921 ); |
884 } |
922 } |
885 |
923 |
886 $schema['properties']['meta'] = $this->meta->get_field_schema(); |
924 $schema['properties']['meta'] = $this->meta->get_field_schema(); |
887 |
925 |
895 * |
933 * |
896 * @return array Collection parameters. |
934 * @return array Collection parameters. |
897 */ |
935 */ |
898 public function get_collection_params() { |
936 public function get_collection_params() { |
899 $query_params = parent::get_collection_params(); |
937 $query_params = parent::get_collection_params(); |
900 $taxonomy = get_taxonomy( $this->taxonomy ); |
938 $taxonomy = get_taxonomy( $this->taxonomy ); |
901 |
939 |
902 $query_params['context']['default'] = 'view'; |
940 $query_params['context']['default'] = 'view'; |
903 |
941 |
904 $query_params['exclude'] = array( |
942 $query_params['exclude'] = array( |
905 'description' => __( 'Ensure result set excludes specific IDs.' ), |
943 'description' => __( 'Ensure result set excludes specific IDs.' ), |
906 'type' => 'array', |
944 'type' => 'array', |
907 'items' => array( |
945 'items' => array( |
908 'type' => 'integer', |
946 'type' => 'integer', |
909 ), |
947 ), |
910 'default' => array(), |
948 'default' => array(), |
911 ); |
949 ); |
912 |
950 |
913 $query_params['include'] = array( |
951 $query_params['include'] = array( |
914 'description' => __( 'Limit result set to specific IDs.' ), |
952 'description' => __( 'Limit result set to specific IDs.' ), |
915 'type' => 'array', |
953 'type' => 'array', |
916 'items' => array( |
954 'items' => array( |
917 'type' => 'integer', |
955 'type' => 'integer', |
918 ), |
956 ), |
919 'default' => array(), |
957 'default' => array(), |
920 ); |
958 ); |
921 |
959 |
922 if ( ! $taxonomy->hierarchical ) { |
960 if ( ! $taxonomy->hierarchical ) { |
923 $query_params['offset'] = array( |
961 $query_params['offset'] = array( |
924 'description' => __( 'Offset the result set by a specific number of items.' ), |
962 'description' => __( 'Offset the result set by a specific number of items.' ), |
925 'type' => 'integer', |
963 'type' => 'integer', |
926 ); |
964 ); |
927 } |
965 } |
928 |
966 |
929 $query_params['order'] = array( |
967 $query_params['order'] = array( |
930 'description' => __( 'Order sort attribute ascending or descending.' ), |
968 'description' => __( 'Order sort attribute ascending or descending.' ), |
931 'type' => 'string', |
969 'type' => 'string', |
932 'default' => 'asc', |
970 'default' => 'asc', |
933 'enum' => array( |
971 'enum' => array( |
934 'asc', |
972 'asc', |
935 'desc', |
973 'desc', |
936 ), |
974 ), |
937 ); |
975 ); |
938 |
976 |
939 $query_params['orderby'] = array( |
977 $query_params['orderby'] = array( |
940 'description' => __( 'Sort collection by term attribute.' ), |
978 'description' => __( 'Sort collection by term attribute.' ), |
941 'type' => 'string', |
979 'type' => 'string', |
942 'default' => 'name', |
980 'default' => 'name', |
943 'enum' => array( |
981 'enum' => array( |
944 'id', |
982 'id', |
945 'include', |
983 'include', |
946 'name', |
984 'name', |
947 'slug', |
985 'slug', |
948 'include_slugs', |
986 'include_slugs', |
951 'count', |
989 'count', |
952 ), |
990 ), |
953 ); |
991 ); |
954 |
992 |
955 $query_params['hide_empty'] = array( |
993 $query_params['hide_empty'] = array( |
956 'description' => __( 'Whether to hide terms not assigned to any posts.' ), |
994 'description' => __( 'Whether to hide terms not assigned to any posts.' ), |
957 'type' => 'boolean', |
995 'type' => 'boolean', |
958 'default' => false, |
996 'default' => false, |
959 ); |
997 ); |
960 |
998 |
961 if ( $taxonomy->hierarchical ) { |
999 if ( $taxonomy->hierarchical ) { |
962 $query_params['parent'] = array( |
1000 $query_params['parent'] = array( |
963 'description' => __( 'Limit result set to terms assigned to a specific parent.' ), |
1001 'description' => __( 'Limit result set to terms assigned to a specific parent.' ), |
964 'type' => 'integer', |
1002 'type' => 'integer', |
965 ); |
1003 ); |
966 } |
1004 } |
967 |
1005 |
968 $query_params['post'] = array( |
1006 $query_params['post'] = array( |
969 'description' => __( 'Limit result set to terms assigned to a specific post.' ), |
1007 'description' => __( 'Limit result set to terms assigned to a specific post.' ), |
970 'type' => 'integer', |
1008 'type' => 'integer', |
971 'default' => null, |
1009 'default' => null, |
972 ); |
1010 ); |
973 |
1011 |
974 $query_params['slug'] = array( |
1012 $query_params['slug'] = array( |
975 'description' => __( 'Limit result set to terms with one or more specific slugs.' ), |
1013 'description' => __( 'Limit result set to terms with one or more specific slugs.' ), |
976 'type' => 'array', |
1014 'type' => 'array', |
977 'items' => array( |
1015 'items' => array( |
978 'type' => 'string' |
1016 'type' => 'string', |
979 ), |
1017 ), |
980 ); |
1018 ); |
981 |
1019 |
982 /** |
1020 /** |
983 * Filter collection parameters for the terms controller. |
1021 * Filter collection parameters for the terms controller. |