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 +41 -136 4.5.15.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.
@@ -51,17 +51,29 @@
51 51 */
52 52 public function __construct() {
53 53 $this->slug = 'searchordering';
54 54
55 - $this->title = esc_html__( 'Custom Search Results', 'elasticpress' );
55 + $this->group = 'core-search';
56 56
57 - $this->summary = __( 'Insert specific posts into search results for specific search queries.', 'elasticpress' );
57 + $this->requires_install_reindex = false;
58 58
59 - $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';
60 60
61 - $this->requires_install_reindex = false;
61 + parent::__construct();
62 + }
62 63
63 - 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' );
64 76 }
65 77
66 78 /**
67 79 * Setup Feature Functionality
@@ -72,9 +84,13 @@
72 84
73 85 /** Search Feature @var Feature\Search\Search $search */
74 86 $search = $features->get_registered_feature( 'search' );
75 87
76 - 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() ) ) {
77 93 $features->deactivate_feature( $this->slug );
78 94 return false;
79 95 }
80 96
@@ -174,32 +190,13 @@
174 190 * Requires the search feature to be activated
175 191 *
176 192 * @return FeatureRequirementsStatus
177 193 */
178 - public function requirements_status() {
179 - /** Features Class @var Features $features */
180 - $features = Features::factory();
181 -
182 - /** Search Feature @var Feature\Search\Search $search */
183 - $search = $features->get_registered_feature( 'search' );
184 -
185 - if ( ! $search->is_active() ) {
186 - return new FeatureRequirementsStatus( 2, esc_html__( 'This feature requires the "Post Search" feature to be enabled', 'elasticpress' ) );
187 - }
188 -
189 - return parent::requirements_status();
194 + public function requirements_status(): FeatureRequirementsStatus {
195 + return new FeatureRequirementsStatus( 0, null, $this );
190 196 }
191 197
192 198 /**
193 - * Output feature box long
194 - */
195 - public function output_feature_box_long() {
196 - ?>
197 - <p><?php esc_html_e( 'Selected posts will be inserted into search results in the specified position.', 'elasticpress' ); ?></p>
198 - <?php
199 - }
200 -
201 - /**
202 199 * Adds this taxonomy as one of the taxonomies to index
203 200 *
204 201 * @param array $taxonomies Current indexable taxonomies
205 202 *
@@ -218,9 +215,9 @@
218 215 add_submenu_page(
219 216 'elasticpress',
220 217 esc_html__( 'Custom Results', 'elasticpress' ),
221 218 esc_html__( 'Custom Results', 'elasticpress' ),
222 - Utils\get_capability(),
219 + Utils\get_capability( 'search-ordering' ),
223 220 'edit.php?post_type=' . self::POST_TYPE_NAME
224 221 );
225 222 }
226 223
@@ -271,11 +268,8 @@
271 268 /**
272 269 * Registers the pointer post type for the injected results
273 270 */
274 271 public function register_post_type() {
275 - $is_network = defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK;
276 - $menu = $is_network ? null : false;
277 -
278 272 $labels = array(
279 273 'name' => esc_html_x( 'Custom Search Results', 'post type general name', 'elasticpress' ),
280 274 'singular_name' => esc_html_x( 'Custom Search Result', 'post type singular name', 'elasticpress' ),
281 275 'menu_name' => esc_html_x( 'Custom Search Results', 'admin menu', 'elasticpress' ),
@@ -297,18 +291,18 @@
297 291 'description' => esc_html__( 'Posts to inject into search results', 'elasticpress' ),
298 292 'public' => false,
299 293 'publicly_queryable' => false,
300 294 'show_ui' => true,
301 - 'show_in_menu' => $menu,
295 + 'show_in_menu' => false,
302 296 'query_var' => true,
303 297 'rewrite' => array( 'slug' => 'ep-pointer' ),
304 - 'capabilities' => Utils\get_post_map_capabilities(),
298 + 'capabilities' => Utils\get_post_map_capabilities( 'search-ordering' ),
305 299 'has_archive' => false,
306 300 'hierarchical' => false,
307 301 'menu_position' => 100,
308 302 'supports' => [ 'title' ],
309 303 'register_meta_box_cb' => [ $this, 'register_meta_box' ],
310 - 'menu_icon' => 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48c3ZnIHZlcnNpb249IjEuMSIgaWQ9IkxheWVyXzEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHg9IjBweCIgeT0iMHB4IiB2aWV3Qm94PSIwIDAgNzMgNzEuMyIgc3R5bGU9ImVuYWJsZS1iYWNrZ3JvdW5kOm5ldyAwIDAgNzMgNzEuMzsiIHhtbDpzcGFjZT0icHJlc2VydmUiPjxwYXRoIGQ9Ik0zNi41LDQuN0MxOS40LDQuNyw1LjYsMTguNiw1LjYsMzUuN2MwLDEwLDQuNywxOC45LDEyLjEsMjQuNWw0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UtMTQuNS0xNC41LTE0LjVTMjIsMjcuNiwyMiwzNS42YzAsMi44LDAuOCw1LjMsMi4xLDcuNWwtNi40LDYuNGMtMi45LTMuOS00LjYtOC43LTQuNi0xMy45YzAtMTIuOSwxMC41LTIzLjQsMjMuNC0yMy40czIzLjQsMTAuNSwyMy40LDIzLjRTNDkuNCw1OSwzNi41LDU5Yy0yLjEsMC00LjEtMC4zLTYtMC44bC0wLjYsMC42bC01LjIsNS40YzMuNiwxLjUsNy42LDIuMywxMS44LDIuM2MxNy4xLDAsMzAuOS0xMy45LDMwLjktMzAuOVM1My42LDQuNywzNi41LDQuN3oiIGZpbGw9IiNhN2FhYWQiLz48L3N2Zz4=',
311 305 );
312 306
313 307 register_post_type( self::POST_TYPE_NAME, $args );
314 308
@@ -333,8 +327,9 @@
333 327 'show_ui' => false,
334 328 'show_admin_column' => false,
335 329 'query_var' => false,
336 330 'rewrite' => false,
331 + 'public' => false,
337 332 );
338 333
339 334 /** Features Class @var Features $features */
340 335 $features = Features::factory();
@@ -461,9 +456,9 @@
461 456 public function save_post( $post_id, $post ) {
462 457 /** Post Indexable @var Post $post_indexable */
463 458 $post_indexable = Indexables::factory()->get( 'post' );
464 459
465 - 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' ) ) {
466 461 return;
467 462 }
468 463
469 464 if ( ! current_user_can( 'edit_post', $post_id ) ) {
@@ -475,9 +470,10 @@
475 470 // Track the old IDs that aren't retained so we can delete the terms later
476 471 $previous_order_data = get_post_meta( $post_id, 'pointers', true );
477 472 $previous_post_ids = ! empty( $previous_order_data ) ? array_flip( wp_list_pluck( $previous_order_data, 'ID' ) ) : [];
478 473
479 - $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 );
480 476
481 477 $posts_per_page = (int) get_option( 'posts_per_page', 10 );
482 478
483 479 $old_search_term = get_post_meta( $post->ID, 'search_term', true );
@@ -496,8 +492,9 @@
496 492 if ( intval( $order_data['order'] ) <= $posts_per_page ) {
497 493 $final_order_data[] = [
498 494 'ID' => intval( $order_data['ID'] ),
499 495 'order' => intval( $order_data['order'] ),
496 + 'type' => ! empty( $order_data['type'] ) ? sanitize_text_field( $order_data['type'] ) : 'reordered',
500 497 ];
501 498 } else {
502 499 $previous_post_ids[ intval( $order_data['ID'] ) ] = true;
503 500 }
@@ -657,15 +654,14 @@
657 654
658 655 $to_inject = array();
659 656
660 657 foreach ( $posts as $key => &$post ) {
661 - if ( isset( $post->terms ) && isset( $post->terms[ self::TAXONOMY_NAME ] ) ) {
662 - 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 ) {
663 661 if ( strtolower( $current_term['name'] ) === $search_query ) {
664 662 $to_inject[ $current_term['term_order'] ] = $post;
665 -
666 663 unset( $posts[ $key ] );
667 -
668 664 break;
669 665 }
670 666 }
671 667 }
@@ -693,103 +689,13 @@
693 689 /**
694 690 * Registers the API endpoint for searching from the admin interface
695 691 */
696 692 public function rest_api_init() {
697 - register_rest_route(
698 - 'elasticpress/v1',
699 - 'pointer_search',
700 - [
701 - 'methods' => 'GET',
702 - 'callback' => [ $this, 'handle_pointer_search' ],
703 - 'permission_callback' => function() {
704 - return current_user_can( Utils\get_capability() );
705 - },
706 - 'args' => [
707 - 's' => [
708 - 'validate_callback' => function ( $param ) {
709 - return ! empty( $param );
710 - },
711 - 'required' => true,
712 - ],
713 - ],
714 - ]
715 - );
716 -
717 - register_rest_route(
718 - 'elasticpress/v1',
719 - 'pointer_preview',
720 - [
721 - 'methods' => 'GET',
722 - 'callback' => [ $this, 'handle_pointer_preview' ],
723 - 'permission_callback' => function() {
724 - return current_user_can( Utils\get_capability() );
725 - },
726 - 'args' => [
727 - 's' => [
728 - 'validate_callback' => function ( $param ) {
729 - return ! empty( $param );
730 - },
731 - 'required' => true,
732 - ],
733 - ],
734 - ]
735 - );
693 + $controller = new REST\SearchOrdering();
694 + $controller->register_routes();
736 695 }
737 696
738 697 /**
739 - * Handles the search for posts from the admin interface for the post type
740 - *
741 - * @param \WP_REST_Request $request Rest request
742 - *
743 - * @return array
744 - */
745 - public function handle_pointer_search( $request ) {
746 - $search = $request->get_param( 's' );
747 -
748 - /** Features Class @var Features $features */
749 - $features = Features::factory();
750 -
751 - /** Search Feature @var Feature\Search\Search $search */
752 - $search_feature = $features->get_registered_feature( 'search' );
753 -
754 - $post_types = $search_feature->get_searchable_post_types();
755 -
756 - $query = new \WP_Query(
757 - [
758 - 'post_type' => $post_types,
759 - 'post_status' => 'publish',
760 - 's' => $search,
761 - ]
762 - );
763 -
764 - return $query->posts;
765 - }
766 -
767 - /**
768 - * Handles the search preview on the pointer edit screen
769 - *
770 - * @param \WP_REST_Request $request Rest request
771 - *
772 - * @return array
773 - */
774 - public function handle_pointer_preview( $request ) {
775 - remove_filter( 'ep_searchable_post_types', [ $this, 'searchable_post_types' ] );
776 -
777 - $search = $request->get_param( 's' );
778 -
779 - $query = new \WP_Query(
780 - [
781 - 's' => $search,
782 - 'exclude_pointers' => true,
783 - ]
784 - );
785 -
786 - add_filter( 'ep_searchable_post_types', [ $this, 'searchable_post_types' ] );
787 -
788 - return $query->posts;
789 - }
790 -
791 - /**
792 698 * Removes taxonomy terms from the references posts when a pointer is deleted or trashed
793 699 *
794 700 * @param int $post_id Post ID that is being deleted
795 701 */
@@ -856,9 +762,9 @@
856 762 */
857 763 protected function assign_term_to_post( $post_id, $term_taxonomy_id, $order ) {
858 764 global $wpdb;
859 765
860 - $result = $wpdb->query(
766 + $result = $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
861 767 $wpdb->prepare(
862 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)",
863 769 $post_id,
864 770 $term_taxonomy_id,
@@ -889,6 +795,5 @@
889 795 }
890 796
891 797 return $admin_title;
892 798 }
893 -
894 799 }