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 +53 -142 4.3.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.
@@ -36,10 +36,14 @@
36 36 const TAXONOMY_NAME = 'ep_custom_result';
37 37
38 38 /**
39 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
40 44 */
41 - const CAPABILITY = 'manage_options';
45 + const CAPABILITY = 'elasticpress_manage';
42 46
43 47 /**
44 48 * Initialize feature setting it's config
45 49 *
@@ -47,17 +51,29 @@
47 51 */
48 52 public function __construct() {
49 53 $this->slug = 'searchordering';
50 54
51 - $this->title = esc_html__( 'Custom Search Results', 'elasticpress' );
55 + $this->group = 'core-search';
52 56
53 - $this->summary = __( 'Insert specific posts into search results for specific search queries.', 'elasticpress' );
57 + $this->requires_install_reindex = false;
54 58
55 - $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';
56 60
57 - $this->requires_install_reindex = false;
61 + parent::__construct();
62 + }
58 63
59 - 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' );
60 76 }
61 77
62 78 /**
63 79 * Setup Feature Functionality
@@ -68,9 +84,13 @@
68 84
69 85 /** Search Feature @var Feature\Search\Search $search */
70 86 $search = $features->get_registered_feature( 'search' );
71 87
72 - 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() ) ) {
73 93 $features->deactivate_feature( $this->slug );
74 94 return false;
75 95 }
76 96
@@ -154,9 +174,9 @@
154 174 8 => esc_html__( 'Custom result submitted.', 'elasticpress' ),
155 175 9 => sprintf(
156 176 // translators: Scheduled date.
157 177 esc_html__( 'Custom result scheduled for: %1$s.', 'elasticpress' ),
158 - // translators: Publish box date format, see http://php.net/date
178 + // translators: Publish box date format, see https://php.net/date
159 179 date_i18n( esc_html__( 'M j, Y @ G:i', 'elasticpress' ), strtotime( $post->post_date ) )
160 180 ),
161 181 10 => esc_html__( 'Custom result draft updated.', 'elasticpress' ),
162 182 );
@@ -170,32 +190,13 @@
170 190 * Requires the search feature to be activated
171 191 *
172 192 * @return FeatureRequirementsStatus
173 193 */
174 - public function requirements_status() {
175 - /** Features Class @var Features $features */
176 - $features = Features::factory();
177 -
178 - /** Search Feature @var Feature\Search\Search $search */
179 - $search = $features->get_registered_feature( 'search' );
180 -
181 - if ( ! $search->is_active() ) {
182 - return new FeatureRequirementsStatus( 2, esc_html__( 'This feature requires the "Post Search" feature to be enabled', 'elasticpress' ) );
183 - }
184 -
185 - return parent::requirements_status();
194 + public function requirements_status(): FeatureRequirementsStatus {
195 + return new FeatureRequirementsStatus( 0, null, $this );
186 196 }
187 197
188 198 /**
189 - * Output feature box long
190 - */
191 - public function output_feature_box_long() {
192 - ?>
193 - <p><?php esc_html_e( 'Selected posts will be inserted into search results in the specified position.', 'elasticpress' ); ?></p>
194 - <?php
195 - }
196 -
197 - /**
198 199 * Adds this taxonomy as one of the taxonomies to index
199 200 *
200 201 * @param array $taxonomies Current indexable taxonomies
201 202 *
@@ -214,9 +215,9 @@
214 215 add_submenu_page(
215 216 'elasticpress',
216 217 esc_html__( 'Custom Results', 'elasticpress' ),
217 218 esc_html__( 'Custom Results', 'elasticpress' ),
218 - self::CAPABILITY,
219 + Utils\get_capability( 'search-ordering' ),
219 220 'edit.php?post_type=' . self::POST_TYPE_NAME
220 221 );
221 222 }
222 223
@@ -267,11 +268,8 @@
267 268 /**
268 269 * Registers the pointer post type for the injected results
269 270 */
270 271 public function register_post_type() {
271 - $is_network = defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK;
272 - $menu = $is_network ? null : false;
273 -
274 272 $labels = array(
275 273 'name' => esc_html_x( 'Custom Search Results', 'post type general name', 'elasticpress' ),
276 274 'singular_name' => esc_html_x( 'Custom Search Result', 'post type singular name', 'elasticpress' ),
277 275 'menu_name' => esc_html_x( 'Custom Search Results', 'admin menu', 'elasticpress' ),
@@ -293,18 +291,18 @@
293 291 'description' => esc_html__( 'Posts to inject into search results', 'elasticpress' ),
294 292 'public' => false,
295 293 'publicly_queryable' => false,
296 294 'show_ui' => true,
297 - 'show_in_menu' => $menu,
295 + 'show_in_menu' => false,
298 296 'query_var' => true,
299 297 'rewrite' => array( 'slug' => 'ep-pointer' ),
300 - 'capability_type' => 'post',
298 + 'capabilities' => Utils\get_post_map_capabilities( 'search-ordering' ),
301 299 'has_archive' => false,
302 300 'hierarchical' => false,
303 301 'menu_position' => 100,
304 302 'supports' => [ 'title' ],
305 303 'register_meta_box_cb' => [ $this, 'register_meta_box' ],
306 - 'menu_icon' => 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48c3ZnIHZlcnNpb249IjEuMSIgaWQ9IkxheWVyXzEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHg9IjBweCIgeT0iMHB4IiB2aWV3Qm94PSIwIDAgNzMgNzEuMyIgc3R5bGU9ImVuYWJsZS1iYWNrZ3JvdW5kOm5ldyAwIDAgNzMgNzEuMzsiIHhtbDpzcGFjZT0icHJlc2VydmUiPjxwYXRoIGQ9Ik0zNi41LDQuN0MxOS40LDQuNyw1LjYsMTguNiw1LjYsMzUuN2MwLDEwLDQuNywxOC45LDEyLjEsMjQuNWw0LjUtNC41YzAuMS0wLjEsMC4xLTAuMiwwLjItMC4zbDAuNy0wLjdsNi40LTYuNGMyLjEsMS4yLDQuNSwxLjksNy4xLDEuOWM4LDAsMTQuNS02LjUsMTQuNS0xNC41cy02LjUtMTQuNS0xNC41LTE0LjVTMjIsMjcuNiwyMiwzNS42YzAsMi44LDAuOCw1LjMsMi4xLDcuNWwtNi40LDYuNGMtMi45LTMuOS00LjYtOC43LTQuNi0xMy45YzAtMTIuOSwxMC41LTIzLjQsMjMuNC0yMy40czIzLjQsMTAuNSwyMy40LDIzLjRTNDkuNCw1OSwzNi41LDU5Yy0yLjEsMC00LjEtMC4zLTYtMC44bC0wLjYsMC42bC01LjIsNS40YzMuNiwxLjUsNy42LDIuMywxMS44LDIuM2MxNy4xLDAsMzAuOS0xMy45LDMwLjktMzAuOVM1My42LDQuNywzNi41LDQuN3oiLz48L3N2Zz4=',
304 + 'menu_icon' => 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48c3ZnIHZlcnNpb249IjEuMSIgaWQ9IkxheWVyXzEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHg9IjBweCIgeT0iMHB4IiB2aWV3Qm94PSIwIDAgNzMgNzEuMyIgc3R5bGU9ImVuYWJsZS1iYWNrZ3JvdW5kOm5ldyAwIDAgNzMgNzEuMzsiIHhtbDpzcGFjZT0icHJlc2VydmUiPjxwYXRoIGQ9Ik0zNi41LDQuN0MxOS40LDQuNyw1LjYsMTguNiw1LjYsMzUuN2MwLDEwLDQuNywxOC45LDEyLjEsMjQuNWw0LjUtNC41YzAuMS0wLjEsMC4xLTAuMiwwLjItMC4zbDAuNy0wLjdsNi40LTYuNGMyLjEsMS4yLDQuNSwxLjksNy4xLDEuOWM4LDAsMTQuNS02LjUsMTQuNS0xNC41cy02LjUtMTQuNS0xNC41LTE0LjVTMjIsMjcuNiwyMiwzNS42YzAsMi44LDAuOCw1LjMsMi4xLDcuNWwtNi40LDYuNGMtMi45LTMuOS00LjYtOC43LTQuNi0xMy45YzAtMTIuOSwxMC41LTIzLjQsMjMuNC0yMy40czIzLjQsMTAuNSwyMy40LDIzLjRTNDkuNCw1OSwzNi41LDU5Yy0yLjEsMC00LjEtMC4zLTYtMC44bC0wLjYsMC42bC01LjIsNS40YzMuNiwxLjUsNy42LDIuMywxMS44LDIuM2MxNy4xLDAsMzAuOS0xMy45LDMwLjktMzAuOVM1My42LDQuNywzNi41LDQuN3oiIGZpbGw9IiNhN2FhYWQiLz48L3N2Zz4=',
307 305 );
308 306
309 307 register_post_type( self::POST_TYPE_NAME, $args );
310 308
@@ -329,8 +327,9 @@
329 327 'show_ui' => false,
330 328 'show_admin_column' => false,
331 329 'query_var' => false,
332 330 'rewrite' => false,
331 + 'public' => false,
333 332 );
334 333
335 334 /** Features Class @var Features $features */
336 335 $features = Features::factory();
@@ -414,17 +413,19 @@
414 413
415 414 if ( in_array( $pagenow, [ 'post-new.php', 'post.php' ], true ) && $screen instanceof \WP_Screen && self::POST_TYPE_NAME === $screen->post_type ) {
416 415 wp_enqueue_script(
417 416 'ep_ordering_scripts',
418 - EP_URL . 'dist/js/ordering-script.min.js',
417 + EP_URL . 'dist/js/ordering-script.js',
419 418 Utils\get_asset_info( 'ordering-script', 'dependencies' ),
420 419 Utils\get_asset_info( 'ordering-script', 'version' ),
421 420 true
422 421 );
423 422
423 + wp_set_script_translations( 'ep_ordering_scripts', 'elasticpress' );
424 +
424 425 wp_enqueue_style(
425 426 'ep_ordering_styles',
426 - EP_URL . 'dist/css/ordering-styles.min.css',
427 + EP_URL . 'dist/css/ordering-styles.css',
427 428 Utils\get_asset_info( 'ordering-styles', 'dependencies' ),
428 429 Utils\get_asset_info( 'ordering-styles', 'version' )
429 430 );
430 431
@@ -455,9 +456,9 @@
455 456 public function save_post( $post_id, $post ) {
456 457 /** Post Indexable @var Post $post_indexable */
457 458 $post_indexable = Indexables::factory()->get( 'post' );
458 459
459 - 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' ) ) {
460 461 return;
461 462 }
462 463
463 464 if ( ! current_user_can( 'edit_post', $post_id ) ) {
@@ -469,9 +470,10 @@
469 470 // Track the old IDs that aren't retained so we can delete the terms later
470 471 $previous_order_data = get_post_meta( $post_id, 'pointers', true );
471 472 $previous_post_ids = ! empty( $previous_order_data ) ? array_flip( wp_list_pluck( $previous_order_data, 'ID' ) ) : [];
472 473
473 - $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 );
474 476
475 477 $posts_per_page = (int) get_option( 'posts_per_page', 10 );
476 478
477 479 $old_search_term = get_post_meta( $post->ID, 'search_term', true );
@@ -490,8 +492,9 @@
490 492 if ( intval( $order_data['order'] ) <= $posts_per_page ) {
491 493 $final_order_data[] = [
492 494 'ID' => intval( $order_data['ID'] ),
493 495 'order' => intval( $order_data['order'] ),
496 + 'type' => ! empty( $order_data['type'] ) ? sanitize_text_field( $order_data['type'] ) : 'reordered',
494 497 ];
495 498 } else {
496 499 $previous_post_ids[ intval( $order_data['ID'] ) ] = true;
497 500 }
@@ -651,15 +654,14 @@
651 654
652 655 $to_inject = array();
653 656
654 657 foreach ( $posts as $key => &$post ) {
655 - if ( isset( $post->terms ) && isset( $post->terms[ self::TAXONOMY_NAME ] ) ) {
656 - 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 ) {
657 661 if ( strtolower( $current_term['name'] ) === $search_query ) {
658 - $to_inject[ $current_term['term_order'] ] = $post->ID;
659 -
662 + $to_inject[ $current_term['term_order'] ] = $post;
660 663 unset( $posts[ $key ] );
661 -
662 664 break;
663 665 }
664 666 }
665 667 }
@@ -672,9 +674,9 @@
672 674 ksort( $to_inject );
673 675
674 676 if ( ! empty( $to_inject ) ) {
675 677 foreach ( $to_inject as $position => $newpost ) {
676 - array_splice( $posts, $position - 1, 0, $newpost );
678 + array_splice( $posts, $position - 1, 0, array( $newpost ) );
677 679 }
678 680 }
679 681
680 682 // reindex just in case we got out of order keys
@@ -687,103 +689,13 @@
687 689 /**
688 690 * Registers the API endpoint for searching from the admin interface
689 691 */
690 692 public function rest_api_init() {
691 - register_rest_route(
692 - 'elasticpress/v1',
693 - 'pointer_search',
694 - [
695 - 'methods' => 'GET',
696 - 'callback' => [ $this, 'handle_pointer_search' ],
697 - 'permission_callback' => function() {
698 - return current_user_can( self::CAPABILITY );
699 - },
700 - 'args' => [
701 - 's' => [
702 - 'validate_callback' => function ( $param ) {
703 - return ! empty( $param );
704 - },
705 - 'required' => true,
706 - ],
707 - ],
708 - ]
709 - );
710 -
711 - register_rest_route(
712 - 'elasticpress/v1',
713 - 'pointer_preview',
714 - [
715 - 'methods' => 'GET',
716 - 'callback' => [ $this, 'handle_pointer_preview' ],
717 - 'permission_callback' => function() {
718 - return current_user_can( self::CAPABILITY );
719 - },
720 - 'args' => [
721 - 's' => [
722 - 'validate_callback' => function ( $param ) {
723 - return ! empty( $param );
724 - },
725 - 'required' => true,
726 - ],
727 - ],
728 - ]
729 - );
693 + $controller = new REST\SearchOrdering();
694 + $controller->register_routes();
730 695 }
731 696
732 697 /**
733 - * Handles the search for posts from the admin interface for the post type
734 - *
735 - * @param \WP_REST_Request $request Rest request
736 - *
737 - * @return array
738 - */
739 - public function handle_pointer_search( $request ) {
740 - $search = $request->get_param( 's' );
741 -
742 - /** Features Class @var Features $features */
743 - $features = Features::factory();
744 -
745 - /** Search Feature @var Feature\Search\Search $search */
746 - $search_feature = $features->get_registered_feature( 'search' );
747 -
748 - $post_types = $search_feature->get_searchable_post_types();
749 -
750 - $query = new \WP_Query(
751 - [
752 - 'post_type' => $post_types,
753 - 'post_status' => 'publish',
754 - 's' => $search,
755 - ]
756 - );
757 -
758 - return $query->posts;
759 - }
760 -
761 - /**
762 - * Handles the search preview on the pointer edit screen
763 - *
764 - * @param \WP_REST_Request $request Rest request
765 - *
766 - * @return array
767 - */
768 - public function handle_pointer_preview( $request ) {
769 - remove_filter( 'ep_searchable_post_types', [ $this, 'searchable_post_types' ] );
770 -
771 - $search = $request->get_param( 's' );
772 -
773 - $query = new \WP_Query(
774 - [
775 - 's' => $search,
776 - 'exclude_pointers' => true,
777 - ]
778 - );
779 -
780 - add_filter( 'ep_searchable_post_types', [ $this, 'searchable_post_types' ] );
781 -
782 - return $query->posts;
783 - }
784 -
785 - /**
786 698 * Removes taxonomy terms from the references posts when a pointer is deleted or trashed
787 699 *
788 700 * @param int $post_id Post ID that is being deleted
789 701 */
@@ -850,9 +762,9 @@
850 762 */
851 763 protected function assign_term_to_post( $post_id, $term_taxonomy_id, $order ) {
852 764 global $wpdb;
853 765
854 - $result = $wpdb->query(
766 + $result = $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
855 767 $wpdb->prepare(
856 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)",
857 769 $post_id,
858 770 $term_taxonomy_id,
@@ -883,6 +795,5 @@
883 795 }
884 796
885 797 return $admin_title;
886 798 }
887 -
888 799 }