PluginProbe
ElasticPress / 4.2.0
ElasticPress v4.2.0
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
elasticpress / includes / classes / Feature / Facets / Block.php

Block.php in ElasticPress 4.2.0, at includes/classes/Feature/Facets/Block.php

214 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Facets block
4 *
5 * @since 4.2.0
6 * @package elasticpress
7 */
8
9 namespace ElasticPress\Feature\Facets;
10
11 use ElasticPress\Features;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit; // Exit if accessed directly.
15 }
16
17 /**
18 * Facets block class
19 */
20 class Block {
21 /**
22 * Hook block funcionality.
23 */
24 public function setup() {
25 add_action( 'init', [ $this, 'register_block' ] );
26 add_action( 'rest_api_init', [ $this, 'setup_endpoints' ] );
27
28 $this->renderer = new Renderer();
29 }
30
31 /**
32 * Setup REST endpoints for the feature.
33 */
34 public function setup_endpoints() {
35 register_rest_route(
36 'elasticpress/v1',
37 'facets/taxonomies',
38 [
39 'methods' => 'GET',
40 'permission_callback' => [ $this, 'check_facets_taxonomies_rest_permission' ],
41 'callback' => [ $this, 'get_rest_facetable_taxonomies' ],
42 ]
43 );
44 register_rest_route(
45 'elasticpress/v1',
46 'facets/block-preview',
47 [
48 'methods' => 'GET',
49 'permission_callback' => [ $this, 'check_facets_taxonomies_rest_permission' ],
50 'callback' => [ $this, 'render_block_preview' ],
51 'args' => [
52 'facet' => [
53 'sanitize_callback' => 'sanitize_text_field',
54 ],
55 'orderby' => [
56 'sanitize_callback' => 'sanitize_text_field',
57 ],
58 'order' => [
59 'sanitize_callback' => 'sanitize_text_field',
60 ],
61 ],
62
63 ]
64 );
65 }
66
67 /**
68 * Check permissions of the /facets/taxonomies REST endpoint.
69 *
70 * @return WP_Error|true
71 */
72 public function check_facets_taxonomies_rest_permission() {
73 if ( ! is_user_logged_in() ) {
74 return new \WP_Error( 'ep_rest_forbidden', esc_html__( 'Sorry, you cannot view this resource.', 'elasticpress' ), array( 'status' => 401 ) );
75 }
76
77 return true;
78 }
79
80 /**
81 * Return an array of taxonomies, their name, plural label, and a sample of terms.
82 *
83 * @return array
84 */
85 public function get_rest_facetable_taxonomies() {
86 $taxonomies_raw = Features::factory()->get_registered_feature( 'facets' )->get_facetable_taxonomies();
87
88 $taxonomies = [];
89 foreach ( $taxonomies_raw as $slug => $taxonomy ) {
90 $terms_sample = get_terms(
91 [
92 'taxonomy' => $slug,
93 'number' => 20,
94 ]
95 );
96 if ( is_array( $terms_sample ) ) {
97 // This way we make sure it will be an array in the outputted JSON.
98 $terms_sample = array_values( $terms_sample );
99 } else {
100 $terms_sample = [];
101 }
102
103 $taxonomies[ $slug ] = [
104 'label' => $taxonomy->label,
105 'plural' => $taxonomy->labels->name,
106 'terms' => $terms_sample,
107 ];
108 }
109
110 return $taxonomies;
111 }
112
113 /**
114 * Register the block.
115 */
116 public function register_block() {
117 register_block_type_from_metadata(
118 EP_PATH . 'assets/js/blocks/facets',
119 [
120 'render_callback' => [ $this, 'render_block' ],
121 ]
122 );
123 }
124
125 /**
126 * Render the block.
127 *
128 * @param array $attributes Block attributes.
129 */
130 public function render_block( $attributes ) {
131 $attributes = $this->parse_attributes( $attributes );
132 ob_start();
133 ?>
134 <div class="wp-block-elasticpress-facet">
135 <?php $this->renderer->render( [], $attributes ); ?>
136 </div>
137 <?php
138 return ob_get_clean();
139 }
140
141 /**
142 * Outputs the block preview
143 *
144 * @param \WP_REST_Request $request REST request
145 * @return string
146 */
147 public function render_block_preview( $request ) {
148 global $wp_query;
149
150 add_filter( 'ep_is_facetable', '__return_true' );
151
152 $search = Features::factory()->get_registered_feature( 'search' );
153
154 $wp_query = new \WP_Query(
155 [
156 'post_type' => $search->get_searchable_post_types(),
157 'per_page' => 1,
158 ]
159 );
160
161 $attributes = $this->parse_attributes(
162 [
163 'facet' => $request->get_param( 'facet' ),
164 'orderby' => $request->get_param( 'orderby' ),
165 'order' => $request->get_param( 'order' ),
166 ]
167 );
168
169 ob_start();
170 $this->renderer->render( [], $attributes );
171 $block_content = ob_get_clean();
172
173 if ( empty( $block_content ) ) {
174 $taxonomy = get_taxonomy( $attributes['facet'] );
175 if ( ! $taxonomy ) {
176 return esc_html__( 'Invalid taxonomy selected.', 'elasticpress' );
177 }
178 return sprintf(
179 /* translators: Taxonomy name */
180 esc_html__( 'Term preview for %s not available', 'elasticpress' ),
181 esc_html( $taxonomy->labels->name )
182 );
183 }
184
185 $block_content = preg_replace( '/href="(.*?)"/', 'href="#"', $block_content );
186 return '<div class="wp-block-elasticpress-facet">' . $block_content . '</div>';
187 }
188
189 /**
190 * Utilitary method to set default attributes.
191 *
192 * @param array $attributes Attributes passed
193 * @return array
194 */
195 protected function parse_attributes( $attributes ) {
196 $attributes = wp_parse_args(
197 $attributes,
198 [
199 'facet' => '',
200 'orderby' => 'count',
201 'order' => 'desc',
202
203 ]
204 );
205 if ( empty( $attributes['facet'] ) ) {
206 $taxonomies = Features::factory()->get_registered_feature( 'facets' )->get_facetable_taxonomies();
207 if ( ! empty( $taxonomies ) ) {
208 $attributes['facet'] = key( $taxonomies );
209 }
210 }
211 return $attributes;
212 }
213 }
214