PluginProbe ʕ •ᴥ•ʔ
AlphaListing / 4.3.4
AlphaListing v4.3.4
trunk 4.3.4 4.3.5 4.3.6 4.3.7 4.4.0
alphalisting / wp-api / api.php
alphalisting / wp-api Last commit date
api.php 1 year ago
api.php
167 lines
1 <?php
2 /**
3 * AlphaListing REST API Extensions
4 *
5 * @package alphalisting
6 */
7
8 namespace eslin87\AlphaListing;
9
10 /**
11 * Shared REST API handler
12 *
13 * @since 1.0.0
14 * @param WP_REST_Request $request The REST API Request.
15 * @param array $args Extra parameters set in the entrypoint functions.
16 */
17 function alphalisting_real_api_handler( WP_REST_Request $request, array $args ) {
18 $output = alphalisting_shortcode_handler( $args );
19
20 if ( $request->get_param( 'include-styles' ) ) {
21 wp_enqueue_style( 'alphalisting' );
22 global $wp_styles;
23 foreach ( $wp_styles->default_dirs as $key => $dir ) {
24 if ( '/wp-includes/css/' === $dir ) {
25 unset( $wp_styles->default_dirs[ $key ] );
26 }
27 }
28 $wp_styles->do_concat = true;
29 $wp_styles->do_items();
30 $output = $wp_styles->print_html . $wp_styles->print_code . $output;
31 }
32
33 return array(
34 'rendered' => $output,
35 );
36 }
37
38 /**
39 * REST API entrypoint for posts requests
40 *
41 * @since 2.0.0
42 * @param WP_REST_Request $request The REST API Request.
43 */
44 function alphalisting_posts_api_handler( WP_REST_Request $request ) {
45 $args = alphalisting_api_handler_defaults( $request );
46
47 $args['display'] = 'posts';
48 $args['post_type'] = $request->get_param( 'post_type' );
49 $args['taxonomy'] = $request->get_param( 'taxonomy' );
50 $args['terms'] = $request->get_param( 'terms' );
51
52 return alphalisting_real_api_handler( $request, $args );
53 }
54
55 /**
56 * REST API entrypoint for terms requests
57 *
58 * @since 2.0.0
59 * @param WP_REST_Request $request The REST API Request.
60 */
61 function alphalisting_terms_api_handler( WP_REST_Request $request ) {
62 $args = alphalisting_api_handler_defaults( $request );
63
64 $args['display'] = 'terms';
65 $args['taxonomy'] = $request->get_param( 'taxonomy' );
66
67 return alphalisting_real_api_handler( $request, $args );
68 }
69
70 /**
71 * Returns the default settings for the plugin's REST API Handler
72 *
73 * @since 2.0.0
74 * @param WP_REST_Request $request The REST API Request.
75 */
76 function alphalisting_api_handler_defaults( WP_REST_Request $request ) {
77 $args = array();
78
79 $args['alphabet'] = $request->get_param( 'alphabet' );
80 $args['grouping'] = $request->get_param( 'grouping' );
81 $args['group-numbers'] = $request->get_param( 'group-numbers' );
82
83 if ( $args['group-numbers'] ) {
84 $args['numbers'] = true;
85 } else {
86 $args['numbers'] = $request->get_param( 'include-numbers' );
87 }
88
89 return $args;
90 }
91
92 add_action( 'rest_api_init', __NAMESPACE__ . '\\alphalisting_register_rest_api' );
93 /**
94 * Register the REST API extensions for the plugin
95 *
96 * @since 2.0.0
97 */
98 function alphalisting_register_rest_api() {
99 $default_args = array(
100 'alphabet' => array(
101 'description' => __( 'Override default alphabet', 'alphalisting' ),
102 'type' => 'string',
103 'default' => '',
104 'sanitize_callback' => 'sanitize_text_field',
105 ),
106 'grouping' => array(
107 'description' => __( 'Size of buckets to group the alphabet', 'alphalisting' ),
108 'type' => 'integer',
109 'default' => 1,
110 'minimum' => 1,
111 ),
112 'group-numbers' => array(
113 'description' => __( 'Include numbers as separate group. Implies {numbers:true}', 'alphalisting' ),
114 'type' => 'boolean',
115 'default' => false,
116 ),
117 'taxonomy' => array(
118 'description' => __( 'Taxonomy', 'alphalisting' ),
119 'type' => 'string',
120 'default' => '',
121 'sanitize_callback' => 'sanitize_text_field',
122 ),
123 'include-styles' => array(
124 'description' => __( 'Include the stylesheet tags in the output', 'alphalisting' ),
125 'type' => 'boolean',
126 'default' => false,
127 ),
128 );
129
130 register_rest_route(
131 'alphalisting/v1',
132 '/posts/(?P<post_type>[a-z0-9-]+)',
133 array(
134 'methods' => 'GET',
135 'callback' => 'alphalisting_posts_api_handler',
136 'permission_callback' => '__return_true',
137 'args' => array(
138 'post_type' => array(
139 'description' => __( 'Post type', 'alphalisting' ),
140 'type' => 'string',
141 'default' => 'page',
142 'sanitize_callback' => 'sanitize_text_field',
143 ),
144 'terms' => array(
145 'description' => __( 'Terms to filter by', 'alphalisting' ),
146 'type' => 'string',
147 'default' => '',
148 'sanitize_callback' => 'sanitize_text_field',
149 ),
150 $default_args,
151 ),
152 )
153 );
154 register_rest_route(
155 'alphalisting/v1',
156 '/terms/(?P<taxonomy>[a-z0-9-]+)',
157 array(
158 'methods' => 'GET',
159 'callback' => 'alphalisting_terms_api_handler',
160 'permission_callback' => '__return_true',
161 'args' => array(
162 $default_args,
163 ),
164 )
165 );
166 }
167