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 +37 -133 4.7.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();
@@ -497,8 +492,9 @@
497 492 if ( intval( $order_data['order'] ) <= $posts_per_page ) {
498 493 $final_order_data[] = [
499 494 'ID' => intval( $order_data['ID'] ),
500 495 'order' => intval( $order_data['order'] ),
496 + 'type' => ! empty( $order_data['type'] ) ? sanitize_text_field( $order_data['type'] ) : 'reordered',
501 497 ];
502 498 } else {
503 499 $previous_post_ids[ intval( $order_data['ID'] ) ] = true;
504 500 }
@@ -658,15 +654,14 @@
658 654
659 655 $to_inject = array();
660 656
661 657 foreach ( $posts as $key => &$post ) {
662 - if ( isset( $post->terms ) && isset( $post->terms[ self::TAXONOMY_NAME ] ) ) {
663 - 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 ) {
664 661 if ( strtolower( $current_term['name'] ) === $search_query ) {
665 662 $to_inject[ $current_term['term_order'] ] = $post;
666 -
667 663 unset( $posts[ $key ] );
668 -
669 664 break;
670 665 }
671 666 }
672 667 }
@@ -694,103 +689,13 @@
694 689 /**
695 690 * Registers the API endpoint for searching from the admin interface
696 691 */
697 692 public function rest_api_init() {
698 - register_rest_route(
699 - 'elasticpress/v1',
700 - 'pointer_search',
701 - [
702 - 'methods' => 'GET',
703 - 'callback' => [ $this, 'handle_pointer_search' ],
704 - 'permission_callback' => function() {
705 - return current_user_can( Utils\get_capability() );
706 - },
707 - 'args' => [
708 - 's' => [
709 - 'validate_callback' => function ( $param ) {
710 - return ! empty( $param );
711 - },
712 - 'required' => true,
713 - ],
714 - ],
715 - ]
716 - );
717 -
718 - register_rest_route(
719 - 'elasticpress/v1',
720 - 'pointer_preview',
721 - [
722 - 'methods' => 'GET',
723 - 'callback' => [ $this, 'handle_pointer_preview' ],
724 - 'permission_callback' => function() {
725 - return current_user_can( Utils\get_capability() );
726 - },
727 - 'args' => [
728 - 's' => [
729 - 'validate_callback' => function ( $param ) {
730 - return ! empty( $param );
731 - },
732 - 'required' => true,
733 - ],
734 - ],
735 - ]
736 - );
693 + $controller = new REST\SearchOrdering();
694 + $controller->register_routes();
737 695 }
738 696
739 697 /**
740 - * Handles the search for posts from the admin interface for the post type
741 - *
742 - * @param \WP_REST_Request $request Rest request
743 - *
744 - * @return array
745 - */
746 - public function handle_pointer_search( $request ) {
747 - $search = $request->get_param( 's' );
748 -
749 - /** Features Class @var Features $features */
750 - $features = Features::factory();
751 -
752 - /** Search Feature @var Feature\Search\Search $search */
753 - $search_feature = $features->get_registered_feature( 'search' );
754 -
755 - $post_types = $search_feature->get_searchable_post_types();
756 -
757 - $query = new \WP_Query(
758 - [
759 - 'post_type' => $post_types,
760 - 'post_status' => 'publish',
761 - 's' => $search,
762 - ]
763 - );
764 -
765 - return $query->posts;
766 - }
767 -
768 - /**
769 - * Handles the search preview on the pointer edit screen
770 - *
771 - * @param \WP_REST_Request $request Rest request
772 - *
773 - * @return array
774 - */
775 - public function handle_pointer_preview( $request ) {
776 - remove_filter( 'ep_searchable_post_types', [ $this, 'searchable_post_types' ] );
777 -
778 - $search = $request->get_param( 's' );
779 -
780 - $query = new \WP_Query(
781 - [
782 - 's' => $search,
783 - 'exclude_pointers' => true,
784 - ]
785 - );
786 -
787 - add_filter( 'ep_searchable_post_types', [ $this, 'searchable_post_types' ] );
788 -
789 - return $query->posts;
790 - }
791 -
792 - /**
793 698 * Removes taxonomy terms from the references posts when a pointer is deleted or trashed
794 699 *
795 700 * @param int $post_id Post ID that is being deleted
796 701 */
@@ -890,6 +795,5 @@
890 795 }
891 796
892 797 return $admin_title;
893 798 }
894 -
895 799 }