PluginProbe
ElasticPress / 5.3.5
ElasticPress v5.3.5
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
← All changes | includes/classes/Feature/Facets/Block.php +25 -183 4.2.25.3.5 View file →
@@ -1,213 +1,55 @@
1 1 <?php
2 2 /**
3 - * Facets block
3 + * Abstract Facet Block class.
4 4 *
5 - * @since 4.2.0
5 + * @since 4.7.0
6 6 * @package elasticpress
7 7 */
8 8
9 9 namespace ElasticPress\Feature\Facets;
10 10
11 -use ElasticPress\Features;
12 -
13 -if ( ! defined( 'ABSPATH' ) ) {
14 - exit; // Exit if accessed directly.
15 -}
16 -
17 11 /**
18 - * Facets block class
12 + * Abstract Facet Block class.
19 13 */
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' ] );
14 +abstract class Block {
27 15
28 - $this->renderer = new Renderer();
29 - }
30 -
31 16 /**
32 - * Setup REST endpoints for the feature.
17 + * Setup hooks and filters for facet block.
33 18 */
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 - ],
19 + abstract public function setup();
62 20
63 - ]
64 - );
65 - }
66 -
67 21 /**
68 - * Check permissions of the /facets/taxonomies REST endpoint.
69 - *
70 - * @return WP_Error|true
22 + * Register facet block.
71 23 */
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 - }
24 + abstract public function register_block();
76 25
77 - return true;
78 - }
79 -
80 26 /**
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 27 * Render the block.
127 28 *
128 29 * @param array $attributes Block attributes.
30 + * @return string
129 31 */
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 - }
32 + abstract public function render_block( $attributes );
140 33
141 34 /**
142 - * Outputs the block preview
35 + * Check if facets should be enabled in the editor.
143 36 *
144 - * @param \WP_REST_Request $request REST request
145 - * @return string
37 + * @since 5.3.0
38 + * @return boolean
146 39 */
147 - public function render_block_preview( $request ) {
148 - global $wp_query;
40 + public function is_facet_enabled_in_editor(): bool {
41 + global $pagenow;
149 42
150 - add_filter( 'ep_is_facetable', '__return_true' );
43 + $in_editor = in_array( $pagenow, [ 'post-new.php', 'post.php' ], true );
151 44
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;
45 + /**
46 + * Filter if facet should be enabled in the editor. Default: false
47 + *
48 + * @hook ep_facet_enabled_in_editor
49 + * @since 5.1.0
50 + * @param {bool} $enabled
51 + * @return {bool} If enabled or not
52 + */
53 + return ! ( $in_editor && ! apply_filters( 'ep_facet_enabled_in_editor', false ) );
212 54 }
213 55 }