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/SearchOrdering/SearchOrdering.php +58 -137 4.3.05.3.5 View file →
@@ -7,12 +7,12 @@
7 7
8 8 namespace ElasticPress\Feature\SearchOrdering;
9 9
10 10 use ElasticPress\Feature;
11 -use ElasticPress\FeatureRequirementsStatus as FeatureRequirementsStatus;
11 +use ElasticPress\FeatureRequirementsStatus;
12 12 use ElasticPress\Features;
13 -use ElasticPress\Indexable\Post\Post;
14 13 use ElasticPress\Indexables;
14 +use ElasticPress\REST;
15 15 use ElasticPress\Utils;
16 16
17 17 if ( ! defined( 'ABSPATH' ) ) {
18 18 exit; // Exit if accessed directly.
@@ -35,8 +35,17 @@
35 35 */
36 36 const TAXONOMY_NAME = 'ep_custom_result';
37 37
38 38 /**
39 + * Capability required to manage.
40 + *
41 + * This will be removed in future versions of ElasticPress. Please use `Utils\get_capability()` instead.
42 + *
43 + * @deprecated 4.5.0
44 + */
45 + const CAPABILITY = 'elasticpress_manage';
46 +
47 + /**
39 48 * Initialize feature setting it's config
40 49 *
41 50 * @since 3.0
42 51 */
@@ -42,17 +51,29 @@
42 51 */
43 52 public function __construct() {
44 53 $this->slug = 'searchordering';
45 54
46 - $this->title = esc_html__( 'Custom Search Results', 'elasticpress' );
55 + $this->group = 'core-search';
47 56
48 - $this->summary = __( 'Insert specific posts into search results for specific search queries.', 'elasticpress' );
57 + $this->requires_install_reindex = false;
49 58
50 - $this->docs_url = __( 'https://elasticpress.zendesk.com/hc/en-us/articles/360050447492-Configuring-ElasticPress-via-the-Plugin-Dashboard#custom-search-results', 'elasticpress' );
59 + $this->requires_feature = 'search';
51 60
52 - $this->requires_install_reindex = false;
61 + parent::__construct();
62 + }
53 63
54 - parent::__construct();
64 + /**
65 + * Sets i18n strings.
66 + *
67 + * @return void
68 + * @since 5.2.0
69 + */
70 + public function set_i18n_strings(): void {
71 + $this->title = esc_html__( 'Custom Search Results', 'elasticpress' );
72 +
73 + $this->summary = '<p>' . __( 'Selected posts will be inserted into search results in the specified position.', 'elasticpress' ) . '</p>';
74 +
75 + $this->docs_url = __( 'https://www.elasticpress.io/resources/articles/configuring-elasticpress-via-the-plugin-dashboard/#custom-search-results', 'elasticpress' );
55 76 }
56 77
57 78 /**
58 79 * Setup Feature Functionality
@@ -63,9 +84,13 @@
63 84
64 85 /** Search Feature @var Feature\Search\Search $search */
65 86 $search = $features->get_registered_feature( 'search' );
66 87
67 - if ( ! $search->is_active() && $this->is_active() ) {
88 + if ( ! Utils\is_site_indexable() ) {
89 + return false;
90 + }
91 +
92 + if ( ( ! $search->is_active() && $this->is_active() ) ) {
68 93 $features->deactivate_feature( $this->slug );
69 94 return false;
70 95 }
71 96
@@ -149,9 +174,9 @@
149 174 8 => esc_html__( 'Custom result submitted.', 'elasticpress' ),
150 175 9 => sprintf(
151 176 // translators: Scheduled date.
152 177 esc_html__( 'Custom result scheduled for: %1$s.', 'elasticpress' ),
153 - // translators: Publish box date format, see http://php.net/date
178 + // translators: Publish box date format, see https://php.net/date
154 179 date_i18n( esc_html__( 'M j, Y @ G:i', 'elasticpress' ), strtotime( $post->post_date ) )
155 180 ),
156 181 10 => esc_html__( 'Custom result draft updated.', 'elasticpress' ),
157 182 );
@@ -165,32 +190,13 @@
165 190 * Requires the search feature to be activated
166 191 *
167 192 * @return FeatureRequirementsStatus
168 193 */
169 - public function requirements_status() {
170 - /** Features Class @var Features $features */
171 - $features = Features::factory();
172 -
173 - /** Search Feature @var Feature\Search\Search $search */
174 - $search = $features->get_registered_feature( 'search' );
175 -
176 - if ( ! $search->is_active() ) {
177 - return new FeatureRequirementsStatus( 2, esc_html__( 'This feature requires the "Post Search" feature to be enabled', 'elasticpress' ) );
178 - }
179 -
180 - return parent::requirements_status();
194 + public function requirements_status(): FeatureRequirementsStatus {
195 + return new FeatureRequirementsStatus( 0, null, $this );
181 196 }
182 197
183 198 /**
184 - * Output feature box long
185 - */
186 - public function output_feature_box_long() {
187 - ?>
188 - <p><?php esc_html_e( 'Selected posts will be inserted into search results in the specified position.', 'elasticpress' ); ?></p>
189 - <?php
190 - }
191 -
192 - /**
193 199 * Adds this taxonomy as one of the taxonomies to index
194 200 *
195 201 * @param array $taxonomies Current indexable taxonomies
196 202 *
@@ -209,9 +215,9 @@
209 215 add_submenu_page(
210 216 'elasticpress',
211 217 esc_html__( 'Custom Results', 'elasticpress' ),
212 218 esc_html__( 'Custom Results', 'elasticpress' ),
213 - 'manage_options',
219 + Utils\get_capability( 'search-ordering' ),
214 220 'edit.php?post_type=' . self::POST_TYPE_NAME
215 221 );
216 222 }
217 223
@@ -262,11 +268,8 @@
262 268 /**
263 269 * Registers the pointer post type for the injected results
264 270 */
265 271 public function register_post_type() {
266 - $is_network = defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK;
267 - $menu = $is_network ? null : false;
268 -
269 272 $labels = array(
270 273 'name' => esc_html_x( 'Custom Search Results', 'post type general name', 'elasticpress' ),
271 274 'singular_name' => esc_html_x( 'Custom Search Result', 'post type singular name', 'elasticpress' ),
272 275 'menu_name' => esc_html_x( 'Custom Search Results', 'admin menu', 'elasticpress' ),
@@ -288,18 +291,18 @@
288 291 'description' => esc_html__( 'Posts to inject into search results', 'elasticpress' ),
289 292 'public' => false,
290 293 'publicly_queryable' => false,
291 294 'show_ui' => true,
292 - 'show_in_menu' => $menu,
295 + 'show_in_menu' => false,
293 296 'query_var' => true,
294 297 'rewrite' => array( 'slug' => 'ep-pointer' ),
295 - 'capability_type' => 'post',
298 + 'capabilities' => Utils\get_post_map_capabilities( 'search-ordering' ),
296 299 'has_archive' => false,
297 300 'hierarchical' => false,
298 301 'menu_position' => 100,
299 302 'supports' => [ 'title' ],
300 303 'register_meta_box_cb' => [ $this, 'register_meta_box' ],
301 - 'menu_icon' => 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48c3ZnIHZlcnNpb249IjEuMSIgaWQ9IkxheWVyXzEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHg9IjBweCIgeT0iMHB4IiB2aWV3Qm94PSIwIDAgNzMgNzEuMyIgc3R5bGU9ImVuYWJsZS1iYWNrZ3JvdW5kOm5ldyAwIDAgNzMgNzEuMzsiIHhtbDpzcGFjZT0icHJlc2VydmUiPjxwYXRoIGQ9Ik0zNi41LDQuN0MxOS40LDQuNyw1LjYsMTguNiw1LjYsMzUuN2MwLDEwLDQuNywxOC45LDEyLjEsMjQuNWw0LjUtNC41YzAuMS0wLjEsMC4xLTAuMiwwLjItMC4zbDAuNy0wLjdsNi40LTYuNGMyLjEsMS4yLDQuNSwxLjksNy4xLDEuOWM4LDAsMTQuNS02LjUsMTQuNS0xNC41cy02LjUtMTQuNS0xNC41LTE0LjVTMjIsMjcuNiwyMiwzNS42YzAsMi44LDAuOCw1LjMsMi4xLDcuNWwtNi40LDYuNGMtMi45LTMuOS00LjYtOC43LTQuNi0xMy45YzAtMTIuOSwxMC41LTIzLjQsMjMuNC0yMy40czIzLjQsMTAuNSwyMy40LDIzLjRTNDkuNCw1OSwzNi41LDU5Yy0yLjEsMC00LjEtMC4zLTYtMC44bC0wLjYsMC42bC01LjIsNS40YzMuNiwxLjUsNy42LDIuMywxMS44LDIuM2MxNy4xLDAsMzAuOS0xMy45LDMwLjktMzAuOVM1My42LDQuNywzNi41LDQuN3oiLz48L3N2Zz4=',
304 + 'menu_icon' => 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48c3ZnIHZlcnNpb249IjEuMSIgaWQ9IkxheWVyXzEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHg9IjBweCIgeT0iMHB4IiB2aWV3Qm94PSIwIDAgNzMgNzEuMyIgc3R5bGU9ImVuYWJsZS1iYWNrZ3JvdW5kOm5ldyAwIDAgNzMgNzEuMzsiIHhtbDpzcGFjZT0icHJlc2VydmUiPjxwYXRoIGQ9Ik0zNi41LDQuN0MxOS40LDQuNyw1LjYsMTguNiw1LjYsMzUuN2MwLDEwLDQuNywxOC45LDEyLjEsMjQuNWw0LjUtNC41YzAuMS0wLjEsMC4xLTAuMiwwLjItMC4zbDAuNy0wLjdsNi40LTYuNGMyLjEsMS4yLDQuNSwxLjksNy4xLDEuOWM4LDAsMTQuNS02LjUsMTQuNS0xNC41cy02LjUtMTQuNS0xNC41LTE0LjVTMjIsMjcuNiwyMiwzNS42YzAsMi44LDAuOCw1LjMsMi4xLDcuNWwtNi40LDYuNGMtMi45LTMuOS00LjYtOC43LTQuNi0xMy45YzAtMTIuOSwxMC41LTIzLjQsMjMuNC0yMy40czIzLjQsMTAuNSwyMy40LDIzLjRTNDkuNCw1OSwzNi41LDU5Yy0yLjEsMC00LjEtMC4zLTYtMC44bC0wLjYsMC42bC01LjIsNS40YzMuNiwxLjUsNy42LDIuMywxMS44LDIuM2MxNy4xLDAsMzAuOS0xMy45LDMwLjktMzAuOVM1My42LDQuNywzNi41LDQuN3oiIGZpbGw9IiNhN2FhYWQiLz48L3N2Zz4=',
302 305 );
303 306
304 307 register_post_type( self::POST_TYPE_NAME, $args );
305 308
@@ -324,8 +327,9 @@
324 327 'show_ui' => false,
325 328 'show_admin_column' => false,
326 329 'query_var' => false,
327 330 'rewrite' => false,
331 + 'public' => false,
328 332 );
329 333
330 334 /** Features Class @var Features $features */
331 335 $features = Features::factory();
@@ -379,8 +383,9 @@
379 383 [
380 384 'post_type' => 'any',
381 385 'post__in' => $post_ids,
382 386 'count' => count( $post_ids ),
387 + 'orderby' => 'post__in',
383 388 ]
384 389 );
385 390
386 391 $final_posts = [];
@@ -408,17 +413,19 @@
408 413
409 414 if ( in_array( $pagenow, [ 'post-new.php', 'post.php' ], true ) && $screen instanceof \WP_Screen && self::POST_TYPE_NAME === $screen->post_type ) {
410 415 wp_enqueue_script(
411 416 'ep_ordering_scripts',
412 - EP_URL . 'dist/js/ordering-script.min.js',
417 + EP_URL . 'dist/js/ordering-script.js',
413 418 Utils\get_asset_info( 'ordering-script', 'dependencies' ),
414 419 Utils\get_asset_info( 'ordering-script', 'version' ),
415 420 true
416 421 );
417 422
423 + wp_set_script_translations( 'ep_ordering_scripts', 'elasticpress' );
424 +
418 425 wp_enqueue_style(
419 426 'ep_ordering_styles',
420 - EP_URL . 'dist/css/ordering-styles.min.css',
427 + EP_URL . 'dist/css/ordering-styles.css',
421 428 Utils\get_asset_info( 'ordering-styles', 'dependencies' ),
422 429 Utils\get_asset_info( 'ordering-styles', 'version' )
423 430 );
424 431
@@ -449,9 +456,9 @@
449 456 public function save_post( $post_id, $post ) {
450 457 /** Post Indexable @var Post $post_indexable */
451 458 $post_indexable = Indexables::factory()->get( 'post' );
452 459
453 - if ( ! isset( $_POST['search-ordering-nonce'] ) || ! wp_verify_nonce( $_POST['search-ordering-nonce'], 'save-search-ordering' ) ) {
460 + if ( ! isset( $_POST['search-ordering-nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['search-ordering-nonce'] ), 'save-search-ordering' ) ) {
454 461 return;
455 462 }
456 463
457 464 if ( ! current_user_can( 'edit_post', $post_id ) ) {
@@ -463,9 +470,10 @@
463 470 // Track the old IDs that aren't retained so we can delete the terms later
464 471 $previous_order_data = get_post_meta( $post_id, 'pointers', true );
465 472 $previous_post_ids = ! empty( $previous_order_data ) ? array_flip( wp_list_pluck( $previous_order_data, 'ID' ) ) : [];
466 473
467 - $ordered_posts = json_decode( wp_unslash( $_POST['ordered_posts'] ), true );
474 + $ordered_posts = isset( $_POST['ordered_posts'] ) ? wp_unslash( $_POST['ordered_posts'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
475 + $ordered_posts = json_decode( $ordered_posts, true );
468 476
469 477 $posts_per_page = (int) get_option( 'posts_per_page', 10 );
470 478
471 479 $old_search_term = get_post_meta( $post->ID, 'search_term', true );
@@ -484,8 +492,9 @@
484 492 if ( intval( $order_data['order'] ) <= $posts_per_page ) {
485 493 $final_order_data[] = [
486 494 'ID' => intval( $order_data['ID'] ),
487 495 'order' => intval( $order_data['order'] ),
496 + 'type' => ! empty( $order_data['type'] ) ? sanitize_text_field( $order_data['type'] ) : 'reordered',
488 497 ];
489 498 } else {
490 499 $previous_post_ids[ intval( $order_data['ID'] ) ] = true;
491 500 }
@@ -645,15 +654,14 @@
645 654
646 655 $to_inject = array();
647 656
648 657 foreach ( $posts as $key => &$post ) {
649 - if ( isset( $post->terms ) && isset( $post->terms[ self::TAXONOMY_NAME ] ) ) {
650 - foreach ( $post->terms[ self::TAXONOMY_NAME ] as $current_term ) {
658 + $terms = is_string( $post->terms ) ? json_decode( wp_specialchars_decode( $post->terms, ENT_QUOTES ), true ) : (array) $post->terms;
659 + if ( isset( $terms[ self::TAXONOMY_NAME ] ) ) {
660 + foreach ( $terms[ self::TAXONOMY_NAME ] as $current_term ) {
651 661 if ( strtolower( $current_term['name'] ) === $search_query ) {
652 - $to_inject[ $current_term['term_order'] ] = $post->ID;
653 -
662 + $to_inject[ $current_term['term_order'] ] = $post;
654 663 unset( $posts[ $key ] );
655 -
656 664 break;
657 665 }
658 666 }
659 667 }
@@ -666,9 +674,9 @@
666 674 ksort( $to_inject );
667 675
668 676 if ( ! empty( $to_inject ) ) {
669 677 foreach ( $to_inject as $position => $newpost ) {
670 - array_splice( $posts, $position - 1, 0, $newpost );
678 + array_splice( $posts, $position - 1, 0, array( $newpost ) );
671 679 }
672 680 }
673 681
674 682 // reindex just in case we got out of order keys
@@ -681,99 +689,13 @@
681 689 /**
682 690 * Registers the API endpoint for searching from the admin interface
683 691 */
684 692 public function rest_api_init() {
685 - register_rest_route(
686 - 'elasticpress/v1',
687 - 'pointer_search',
688 - [
689 - 'methods' => 'GET',
690 - 'callback' => [ $this, 'handle_pointer_search' ],
691 - 'permission_callback' => '__return_true',
692 - 'args' => [
693 - 's' => [
694 - 'validate_callback' => function ( $param ) {
695 - return ! empty( $param );
696 - },
697 - 'required' => true,
698 - ],
699 - ],
700 - ]
701 - );
702 -
703 - register_rest_route(
704 - 'elasticpress/v1',
705 - 'pointer_preview',
706 - [
707 - 'methods' => 'GET',
708 - 'callback' => [ $this, 'handle_pointer_preview' ],
709 - 'permission_callback' => '__return_true',
710 - 'args' => [
711 - 's' => [
712 - 'validate_callback' => function ( $param ) {
713 - return ! empty( $param );
714 - },
715 - 'required' => true,
716 - ],
717 - ],
718 - ]
719 - );
693 + $controller = new REST\SearchOrdering();
694 + $controller->register_routes();
720 695 }
721 696
722 697 /**
723 - * Handles the search for posts from the admin interface for the post type
724 - *
725 - * @param \WP_REST_Request $request Rest request
726 - *
727 - * @return array
728 - */
729 - public function handle_pointer_search( $request ) {
730 - $search = $request->get_param( 's' );
731 -
732 - /** Features Class @var Features $features */
733 - $features = Features::factory();
734 -
735 - /** Search Feature @var Feature\Search\Search $search */
736 - $search_feature = $features->get_registered_feature( 'search' );
737 -
738 - $post_types = $search_feature->get_searchable_post_types();
739 -
740 - $query = new \WP_Query(
741 - [
742 - 'post_type' => $post_types,
743 - 'post_status' => 'publish',
744 - 's' => $search,
745 - ]
746 - );
747 -
748 - return $query->posts;
749 - }
750 -
751 - /**
752 - * Handles the search preview on the pointer edit screen
753 - *
754 - * @param \WP_REST_Request $request Rest request
755 - *
756 - * @return array
757 - */
758 - public function handle_pointer_preview( $request ) {
759 - remove_filter( 'ep_searchable_post_types', [ $this, 'searchable_post_types' ] );
760 -
761 - $search = $request->get_param( 's' );
762 -
763 - $query = new \WP_Query(
764 - [
765 - 's' => $search,
766 - 'exclude_pointers' => true,
767 - ]
768 - );
769 -
770 - add_filter( 'ep_searchable_post_types', [ $this, 'searchable_post_types' ] );
771 -
772 - return $query->posts;
773 - }
774 -
775 - /**
776 698 * Removes taxonomy terms from the references posts when a pointer is deleted or trashed
777 699 *
778 700 * @param int $post_id Post ID that is being deleted
779 701 */
@@ -840,9 +762,9 @@
840 762 */
841 763 protected function assign_term_to_post( $post_id, $term_taxonomy_id, $order ) {
842 764 global $wpdb;
843 765
844 - $result = $wpdb->query(
766 + $result = $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
845 767 $wpdb->prepare(
846 768 "INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES ( %d, %d, %d ) ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)",
847 769 $post_id,
848 770 $term_taxonomy_id,
@@ -873,6 +795,5 @@
873 795 }
874 796
875 797 return $admin_title;
876 798 }
877 -
878 799 }