PluginProbe
Same Category Posts / trunk
Same Category Posts vtrunk
1.2.0 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 trunk 1.0 1.0.1 1.0.10 1.0.11 1.0.12 All 36 releases
same-category-posts / same-posts-rest.php

same-posts-rest.php in Same Category Posts trunk, at same-posts-rest.php

129 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * REST route that feeds the block editor's taxonomy and term filters.
4 *
5 * The classic widget's form builds its "Filter" section on the server: it
6 * walks the public taxonomies, groups them by post type and lists the terms of
7 * each. The block editor needs the same data, and it needs it grouped the same
8 * way, or the two forms would offer different choices.
9 *
10 * Rather than rebuild that grouping in JavaScript from several core routes,
11 * this route hands over the finished structure and reuses
12 * Widget::initPostTypesAndTaxes() for the taxonomy-to-post-type mapping and
13 * for the default taxonomy per post type -- the same method the widget uses.
14 *
15 * @package samePosts
16 *
17 * @since 1.2.0
18 */
19
20 namespace samePosts;
21
22 const REST_NAMESPACE = 'same-posts/v1';
23
24 /**
25 * Registers the route.
26 */
27 function register_rest_routes() {
28 register_rest_route(
29 REST_NAMESPACE,
30 '/taxonomies',
31 array(
32 'methods' => 'GET',
33 'callback' => __NAMESPACE__ . '\rest_taxonomies',
34 'permission_callback' => __NAMESPACE__ . '\rest_can_edit_posts',
35 )
36 );
37 }
38 add_action( 'rest_api_init', __NAMESPACE__ . '\register_rest_routes' );
39
40 /**
41 * The route only serves the editor, so it needs the editor's capability.
42 *
43 * @return bool
44 */
45 function rest_can_edit_posts() {
46 return current_user_can( 'edit_posts' );
47 }
48
49 /**
50 * Builds the post type => taxonomies => terms structure.
51 *
52 * Mirrors Widget::form(): only public taxonomies, only those that have terms,
53 * terms including empty ones, grouped under the post type that
54 * initPostTypesAndTaxes() associated with the taxonomy.
55 *
56 * @return array<int, array<string, mixed>> One entry per post type that has
57 * at least one taxonomy with terms.
58 */
59 function taxonomy_choices() {
60 $widget = new Widget();
61 $instance = array();
62
63 // Fills $instance['post_types'] (taxonomy => post type + hierarchical) and
64 // $instance['include_tax'] (post type => default taxonomy).
65 $widget->initPostTypesAndTaxes( $instance );
66
67 $post_types = isset( $instance['post_types'] ) ? $instance['post_types'] : array();
68 $include_tax = isset( $instance['include_tax'] ) ? $instance['include_tax'] : array();
69
70 $grouped = array();
71
72 foreach ( get_taxonomies( array( 'public' => true ), 'objects' ) as $taxonomy ) {
73 if ( ! isset( $post_types[ $taxonomy->name ]['post_type'] ) ) {
74 continue;
75 }
76
77 $terms = get_terms(
78 array(
79 'taxonomy' => $taxonomy->name,
80 'hide_empty' => false, // Show not yet populated terms as well.
81 'fields' => 'id=>name',
82 )
83 );
84
85 if ( is_wp_error( $terms ) || empty( $terms ) ) {
86 continue; // The widget's form skips taxonomies without terms.
87 }
88
89 $post_type = $post_types[ $taxonomy->name ]['post_type'];
90
91 $term_list = array();
92 foreach ( $terms as $term_id => $term_name ) {
93 $term_list[] = array(
94 'id' => (int) $term_id,
95 'name' => $term_name,
96 );
97 }
98
99 if ( ! isset( $grouped[ $post_type ] ) ) {
100 $post_type_object = get_post_type_object( $post_type );
101
102 $grouped[ $post_type ] = array(
103 'postType' => $post_type,
104 'label' => $post_type_object ? $post_type_object->label : $post_type,
105 'defaultTaxonomy' => isset( $include_tax[ $post_type ] ) ? $include_tax[ $post_type ] : '',
106 'taxonomies' => array(),
107 );
108 }
109
110 $grouped[ $post_type ]['taxonomies'][] = array(
111 'name' => $taxonomy->name,
112 'label' => $taxonomy->labels->name,
113 'hierarchical' => (bool) $taxonomy->hierarchical,
114 'terms' => $term_list,
115 );
116 }
117
118 return array_values( $grouped );
119 }
120
121 /**
122 * Route callback.
123 *
124 * @return \WP_REST_Response
125 */
126 function rest_taxonomies() {
127 return rest_ensure_response( taxonomy_choices() );
128 }
129