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/Indexable/Post/SyncManager.php +275 -40 4.5.25.3.5 View file →
@@ -7,13 +7,12 @@
7 7 */
8 8
9 9 namespace ElasticPress\Indexable\Post;
10 10
11 -use ElasticPress\Elasticsearch as Elasticsearch;
12 -use ElasticPress\Indexables as Indexables;
13 -use ElasticPress\SyncManager as SyncManagerAbstract;
11 +use ElasticPress\Elasticsearch;
12 +use ElasticPress\Indexables;
13 +use ElasticPress\IndexHelper;
14 14 use ElasticPress\Utils;
15 -use ElasticPress\IndexHelper;
16 15
17 16 if ( ! defined( 'ABSPATH' ) ) {
18 17 // @codeCoverageIgnoreStart
19 18 exit; // Exit if accessed directly.
@@ -22,9 +21,9 @@
22 21
23 22 /**
24 23 * Sync manager class
25 24 */
26 -class SyncManager extends SyncManagerAbstract {
25 +class SyncManager extends \ElasticPress\SyncManager {
27 26
28 27 /**
29 28 * Indexable slug
30 29 *
@@ -53,11 +52,12 @@
53 52 if ( ! $this->can_index_site() ) {
54 53 return;
55 54 }
56 55
57 - add_action( 'wp_insert_post', array( $this, 'action_sync_on_update' ), 999, 3 );
58 - add_action( 'add_attachment', array( $this, 'action_sync_on_update' ), 999, 3 );
59 - add_action( 'edit_attachment', array( $this, 'action_sync_on_update' ), 999, 3 );
56 + add_action( 'wp_insert_post', array( $this, 'action_sync_on_update' ), 999 );
57 + add_action( 'add_attachment', array( $this, 'action_sync_on_update' ), 999 );
58 + add_action( 'edit_attachment', array( $this, 'action_sync_on_update' ), 999 );
59 + add_action( 'wp_media_attach_action', array( $this, 'action_sync_on_media_attach' ), 999, 2 );
60 60 add_action( 'delete_post', array( $this, 'action_delete_post' ) );
61 61 add_action( 'updated_post_meta', array( $this, 'action_queue_meta_sync' ), 10, 4 );
62 62 add_action( 'added_post_meta', array( $this, 'action_queue_meta_sync' ), 10, 4 );
63 63 // Called just because we need to know somehow if $delete_all is set before action_queue_meta_sync() runs.
@@ -74,12 +74,12 @@
74 74 add_action( 'set_object_terms', array( $this, 'action_set_object_terms' ), 10, 6 );
75 75 add_action( 'edited_term', array( $this, 'action_edited_term' ), 10, 3 );
76 76 add_action( 'deleted_term_relationships', array( $this, 'action_deleted_term_relationships' ), 10, 3 );
77 77
78 - // Clear field limit cache
79 - add_action( 'ep_update_index_settings', [ $this, 'clear_total_fields_limit_cache' ] );
80 - add_action( 'ep_sync_put_mapping', [ $this, 'clear_total_fields_limit_cache' ] );
81 - add_action( 'ep_saved_weighting_configuration', [ $this, 'clear_total_fields_limit_cache' ] );
78 + // Clear index settings cache
79 + add_action( 'ep_update_index_settings', [ $this, 'clear_index_settings_cache' ] );
80 + add_action( 'ep_after_put_mapping', [ $this, 'clear_index_settings_cache' ] );
81 + add_action( 'ep_saved_weighting_configuration', [ $this, 'clear_index_settings_cache' ] );
82 82
83 83 // Clear distinct meta field per post type cache
84 84 add_action( 'wp_insert_post', [ $this, 'clear_meta_keys_db_per_post_type_cache_by_post_id' ] );
85 85 add_action( 'delete_post', [ $this, 'clear_meta_keys_db_per_post_type_cache_by_post_id' ] );
@@ -86,8 +86,17 @@
86 86 add_action( 'updated_post_meta', [ $this, 'clear_meta_keys_db_per_post_type_cache_by_meta' ], 10, 2 );
87 87 add_action( 'added_post_meta', [ $this, 'clear_meta_keys_db_per_post_type_cache_by_meta' ], 10, 2 );
88 88 add_action( 'deleted_post_meta', [ $this, 'clear_meta_keys_db_per_post_type_cache_by_meta' ], 10, 2 );
89 89 add_action( 'delete_post_metadata', [ $this, 'clear_meta_keys_db_per_post_type_cache_by_meta' ], 10, 2 );
90 +
91 + // Prevents password protected posts from being indexed
92 + add_filter( 'ep_post_sync_kill', [ $this, 'kill_sync_for_password_protected' ], 10, 2 );
93 +
94 + // Display the status of the document in ES in the admin bar
95 + add_filter( 'ep_admin_bar_status_and_summary', [ $this, 'maybe_add_doc_status_to_admin_bar_status' ] );
96 +
97 + // Delete a post from the index if a password was added
98 + add_action( 'post_updated', [ $this, 'delete_post_with_new_password' ], 10, 3 );
90 99 }
91 100
92 101 /**
93 102 * Un-setup actions and filters (for multisite).
@@ -97,8 +106,9 @@
97 106 public function tear_down() {
98 107 remove_action( 'wp_insert_post', array( $this, 'action_sync_on_update' ), 999 );
99 108 remove_action( 'add_attachment', array( $this, 'action_sync_on_update' ), 999 );
100 109 remove_action( 'edit_attachment', array( $this, 'action_sync_on_update' ), 999 );
110 + remove_action( 'wp_media_attach_action', array( $this, 'action_sync_on_media_attach' ), 999 );
101 111 remove_action( 'delete_post', array( $this, 'action_delete_post' ) );
102 112 remove_action( 'updated_post_meta', array( $this, 'action_queue_meta_sync' ) );
103 113 remove_action( 'added_post_meta', array( $this, 'action_queue_meta_sync' ) );
104 114 remove_filter( 'delete_post_metadata', array( $this, 'maybe_delete_meta_for_all' ) );
@@ -105,8 +115,16 @@
105 115 remove_action( 'deleted_post_meta', array( $this, 'action_queue_meta_sync' ) );
106 116 remove_action( 'wp_initialize_site', array( $this, 'action_create_blog_index' ) );
107 117 remove_filter( 'ep_sync_insert_permissions_bypass', array( $this, 'filter_bypass_permission_checks_for_machines' ) );
108 118 remove_filter( 'ep_sync_delete_permissions_bypass', array( $this, 'filter_bypass_permission_checks_for_machines' ) );
119 + remove_filter( 'ep_post_sync_kill', [ $this, 'kill_sync_for_password_protected' ] );
120 +
121 + // Clear index settings cache
122 + remove_action( 'ep_update_index_settings', [ $this, 'clear_index_settings_cache' ] );
123 + remove_action( 'ep_after_put_mapping', [ $this, 'clear_index_settings_cache' ] );
124 + remove_action( 'ep_saved_weighting_configuration', [ $this, 'clear_index_settings_cache' ] );
125 +
126 + remove_filter( 'ep_admin_bar_status_and_summary', [ $this, 'maybe_add_doc_status_to_admin_bar_status' ] );
109 127 }
110 128
111 129 /**
112 130 * Whether to delete all meta from other posts that is associated with the deleted post.
@@ -127,9 +145,9 @@
127 145 /**
128 146 * Filter to allow cron and WP CLI processes to index/delete documents
129 147 *
130 148 * @param boolean $bypass The current filtered value
131 - * @return boolean Boolean indicating if permission checking should be bypased or not
149 + * @return boolean Boolean indicating if permission checking should be bypassed or not
132 150 * @since 3.6.0
133 151 */
134 152 public function filter_bypass_permission_checks_for_machines( $bypass ) {
135 153 // Allow index/delete during cron
@@ -213,9 +231,9 @@
213 231
214 232 if ( $query->have_posts() && $query->elasticsearch_success ) {
215 233 $posts_to_be_synced = array_filter(
216 234 $query->posts,
217 - function( $object_id ) {
235 + function ( $object_id ) {
218 236 return ! apply_filters( 'ep_post_sync_kill', false, $object_id, $object_id );
219 237 }
220 238 );
221 239 if ( ! empty( $posts_to_be_synced ) ) {
@@ -292,11 +310,9 @@
292 310 /**
293 311 * Make sure to remove this post from the sync queue in case an shutdown happens
294 312 * before a redirect when a redirect has already been triggered.
295 313 */
296 - if ( isset( $this->sync_queue[ $post_id ] ) ) {
297 - unset( $this->sync_queue[ $post_id ] );
298 - }
314 + $this->remove_from_queue( $post_id );
299 315 }
300 316
301 317 /**
302 318 * Sync ES index with what happened to the post being saved
@@ -370,8 +386,9 @@
370 386 * @param {int} $object_id ID of post
371 387 * @return {boolean} New value
372 388 */
373 389 if ( apply_filters( 'ep_post_sync_kill', false, $post_id, $post_id ) ) {
390 + $this->remove_from_queue( $post_id );
374 391 return;
375 392 }
376 393
377 394 $this->add_to_queue( $post_id );
@@ -412,8 +429,9 @@
412 429 Utils\get_sync_url()
413 430 ),
414 431 'type' => 'warning',
415 432 'dismiss' => true,
433 + 'scope' => 'site',
416 434 ];
417 435 break;
418 436 }
419 437 }
@@ -427,8 +445,9 @@
427 445 Utils\get_sync_url()
428 446 ),
429 447 'type' => 'warning',
430 448 'dismiss' => true,
449 + 'scope' => 'site',
431 450 ];
432 451
433 452 return $notices;
434 453 }
@@ -461,8 +480,9 @@
461 480 Utils\get_sync_url()
462 481 ),
463 482 'type' => 'warning',
464 483 'dismiss' => true,
484 + 'scope' => 'site',
465 485 ];
466 486
467 487 return $notices;
468 488 }
@@ -578,9 +598,11 @@
578 598 }
579 599 }
580 600
581 601 // Find ID of all attached posts (query lifted from wp_delete_term())
582 - $object_ids = (array) $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id = %d", $tt_id ) );
602 + $object_ids = (array) $wpdb->get_col( // phpcs:disable WordPress.DB.DirectDatabaseQuery
603 + $wpdb->prepare( "SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id = %d", $tt_id )
604 + );
583 605
584 606 // If the current term is not attached, check if the child terms are attached to the post
585 607 if ( empty( $object_ids ) ) {
586 608 $child_terms = get_term_children( $term_id, $taxonomy );
@@ -585,9 +607,14 @@
585 607 if ( empty( $object_ids ) ) {
586 608 $child_terms = get_term_children( $term_id, $taxonomy );
587 609 if ( ! empty( $child_terms ) ) {
588 610 $in_id = join( ',', array_fill( 0, count( $child_terms ), '%d' ) );
589 - $object_ids = (array) $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id IN ( {$in_id} )", $child_terms ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
611 + $object_ids = (array) $wpdb->get_col( // phpcs:disable WordPress.DB.DirectDatabaseQuery
612 + $wpdb->prepare(
613 + "SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id IN ( {$in_id} )", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
614 + $child_terms
615 + )
616 + );
590 617 }
591 618 }
592 619 if ( ! count( $object_ids ) ) {
593 620 return;
@@ -726,21 +753,14 @@
726 753 restore_current_blog();
727 754 }
728 755
729 756 /**
730 - * Clear the cache of the total fields limit
757 + * DEPRECATED. Clear the cache of the total fields limit
731 758 *
732 759 * @since 4.4.0
733 760 */
734 761 public function clear_total_fields_limit_cache() {
735 - $indexable = Indexables::factory()->get( $this->indexable_slug );
736 - $cache_key = 'ep_total_fields_limit_' . $indexable->get_index_name();
737 -
738 - if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
739 - delete_site_transient( $cache_key );
740 - } else {
741 - delete_transient( $cache_key );
742 - }
762 + _deprecated_function( __METHOD__, '4.7.0', '\ElasticPress\Indexable\Post\SyncManager::clear_index_settings_cache()' );
743 763 }
744 764
745 765 /**
746 766 * Clear the cache of the total fields limit
@@ -780,15 +800,15 @@
780 800 delete_transient( 'ep_meta_field_keys_' . $post_type );
781 801 }
782 802
783 803 /**
784 - * Check if post attributes (post status, taxonomy, and type) match what is needed to reindex or not.
804 + * Given a post ID, check if it should be indexed or not.
785 805 *
786 - * @param int $post_id The post ID.
787 - * @param string $taxonomy The taxonomy slug.
806 + * @since 5.2.0
807 + * @param int $post_id Post ID.
788 808 * @return boolean
789 809 */
790 - protected function should_reindex_post( $post_id, $taxonomy ) {
810 + public function is_post_indexable( $post_id ) {
791 811 /**
792 812 * Filter to kill post sync
793 813 *
794 814 * @hook ep_post_sync_kill
@@ -813,8 +833,32 @@
813 833 if ( ! in_array( $post->post_status, $indexable_post_statuses, true ) ) {
814 834 return false;
815 835 }
816 836
837 + // Check post type
838 + $indexable_post_types = $indexable->get_indexable_post_types();
839 + if ( ! in_array( $post->post_type, $indexable_post_types, true ) ) {
840 + return false;
841 + }
842 +
843 + return true;
844 + }
845 +
846 + /**
847 + * Check if post attributes (post status, taxonomy, and type) match what is needed to reindex or not.
848 + *
849 + * @param int $post_id The post ID.
850 + * @param string $taxonomy The taxonomy slug.
851 + * @return boolean
852 + */
853 + protected function should_reindex_post( $post_id, $taxonomy ) {
854 + if ( ! $this->is_post_indexable( $post_id ) ) {
855 + return false;
856 + }
857 +
858 + $indexable = Indexables::factory()->get( $this->indexable_slug );
859 + $post = get_post( $post_id );
860 +
817 861 // Only re-index if the taxonomy is indexed for this post
818 862 $indexable_taxonomies = $indexable->get_indexable_post_taxonomies( $post );
819 863 $indexable_taxonomy_names = wp_list_pluck( $indexable_taxonomies, 'name' );
820 864 if ( ! in_array( $taxonomy, $indexable_taxonomy_names, true ) ) {
@@ -820,16 +864,10 @@
820 864 if ( ! in_array( $taxonomy, $indexable_taxonomy_names, true ) ) {
821 865 return false;
822 866 }
823 867
824 - // Check post type
825 - $indexable_post_types = $indexable->get_indexable_post_types();
826 - if ( ! in_array( $post->post_type, $indexable_post_types, true ) ) {
827 - return false;
828 - }
829 -
830 868 // If we have more items to update than the number set as Content Items per Index Cycle, skip it to avoid a timeout.
831 - $single_ids_queued = array_unique( array_keys( $this->sync_queue ) );
869 + $single_ids_queued = array_unique( array_keys( $this->get_sync_queue() ) );
832 870 $has_too_many_queued = count( $single_ids_queued ) > IndexHelper::factory()->get_index_default_per_page();
833 871
834 872 return ! $has_too_many_queued;
835 873 }
@@ -843,9 +881,9 @@
843 881 * @since 4.4.0
844 882 * @param \WP_Taxonomy $tax The taxonomy object
845 883 * @return boolean
846 884 */
847 - protected function is_tax_max_count_bigger_than_items_per_cycle( \WP_Taxonomy $tax ) : bool {
885 + protected function is_tax_max_count_bigger_than_items_per_cycle( \WP_Taxonomy $tax ): bool {
848 886 $transient_name = "ep_term_max_count_{$tax->name}";
849 887 $cached_max_count = get_transient( $transient_name );
850 888
851 889 if ( is_integer( $cached_max_count ) ) {
@@ -875,6 +913,203 @@
875 913 $is_max_count_bigger ? DAY_IN_SECONDS : HOUR_IN_SECONDS
876 914 );
877 915
878 916 return $is_max_count_bigger;
917 + }
918 +
919 + /**
920 + * Prevent a password protected post from being indexed.
921 + *
922 + * @since 4.6.0
923 + * @param bool $skip Whether should skip or not before checking for a password
924 + * @param int $object_id The Post ID
925 + * @return bool New value of $skip
926 + */
927 + public function kill_sync_for_password_protected( $skip, $object_id ) {
928 + /**
929 + * Short-circuits the process of checking if a post should be indexed or not depending on its password.
930 + *
931 + * Returning a non-null value will effectively short-circuit the function.
932 + *
933 + * @since 4.6.0
934 + * @hook ep_pre_kill_sync_for_password_protected
935 + * @param {null} $new_skip Whether should skip or not before checking for a password
936 + * @param {bool} $current_skip Current value
937 + * @param {int} $object_id The Post ID
938 + * @return {null|bool} New value of $skip or `null` to keep default behavior.
939 + */
940 + $skip_filter = apply_filters( 'ep_pre_kill_sync_for_password_protected', null, $skip, $object_id );
941 + if ( ! is_null( $skip_filter ) ) {
942 + return $skip_filter;
943 + }
944 +
945 + if ( $skip ) {
946 + return $skip;
947 + }
948 +
949 + $post = get_post( $object_id );
950 +
951 + return ! empty( $post->post_password );
952 + }
953 +
954 + /**
955 + * Sync ES index when attached or detached action is called.
956 + *
957 + * @since 4.7.0
958 + * @param string $action Attach/detach action
959 + * @param int $attachment_id The attachment ID
960 + */
961 + public function action_sync_on_media_attach( $action, $attachment_id ) {
962 + $indexable = Indexables::factory()->get( $this->indexable_slug );
963 + $indexable_post_types = $indexable->get_indexable_post_types();
964 +
965 + if ( ! in_array( 'attachment', $indexable_post_types, true ) ) {
966 + return;
967 + }
968 + $this->action_sync_on_update( $attachment_id );
969 + }
970 +
971 + /**
972 + * Add the document status to the admin bar.
973 + *
974 + * @since 5.2.0
975 + * @deprecated 5.3.0
976 + * @param \WP_Admin_Bar $admin_bar WP Admin Bar instance
977 + * @return void
978 + */
979 + public function add_admin_bar_status( \WP_Admin_Bar $admin_bar ) {
980 + _deprecated_function( __METHOD__, 'ElasticPress 5.3.0' );
981 + }
982 +
983 + /**
984 + * Add the document status to the admin bar status and summary.
985 + *
986 + * @since 5.3.0
987 + * @param array $status_and_summary The status and summary.
988 + * @return array The status and summary.
989 + */
990 + public function maybe_add_doc_status_to_admin_bar_status( $status_and_summary ): array {
991 + global $pagenow;
992 +
993 + if ( ! is_admin() || 'post.php' !== $pagenow ) {
994 + return $status_and_summary;
995 + }
996 +
997 + $post_id = get_the_ID();
998 + if ( ! $this->is_post_indexable( $post_id ) ) {
999 + return $status_and_summary;
1000 + }
1001 +
1002 + $document_status = $this->get_doc_status( $post_id );
1003 + if ( empty( $document_status['status'] ) ) {
1004 + return $status_and_summary;
1005 + }
1006 +
1007 + if ( 'success' === $status_and_summary['status'] || in_array( $document_status['status'], [ 'error', 'warning' ], true ) ) {
1008 + $status_and_summary['status'] = $document_status['status'];
1009 + }
1010 +
1011 + $status_and_summary['summary'] = array_merge(
1012 + [ 'doc_status' => "{$document_status['message']}: {$document_status['explanation']}" ],
1013 + $status_and_summary['summary'],
1014 + );
1015 +
1016 + return $status_and_summary;
1017 + }
1018 +
1019 + /**
1020 + * Get the document status for a post.
1021 + *
1022 + * @since 5.2.0
1023 + * @param int $post_id Post ID
1024 + * @return array
1025 + */
1026 + protected function get_doc_status( int $post_id ): array {
1027 + $status = [
1028 + 'status' => 'success',
1029 + 'message' => esc_html__( 'Content in sync', 'elasticpress' ),
1030 + 'explanation' => esc_html__( 'WordPress and Elasticsearch content match.', 'elasticpress' ),
1031 + ];
1032 +
1033 + $indexable = Indexables::factory()->get( $this->indexable_slug );
1034 + $es_doc = $indexable->get( $post_id );
1035 + if ( ! $es_doc ) {
1036 + $status = [
1037 + 'status' => 'error',
1038 + 'message' => esc_html__( 'Sync required', 'elasticpress' ),
1039 + 'explanation' => esc_html__( 'Content not found in Elasticsearch.', 'elasticpress' ),
1040 + ];
1041 + } else {
1042 + $post = get_post( $post_id );
1043 + if ( $post->post_modified_gmt !== $es_doc['post_modified_gmt'] ) {
1044 + $status = [
1045 + 'status' => 'warning',
1046 + 'message' => esc_html__( 'Out of sync', 'elasticpress' ),
1047 + 'explanation' => esc_html__( 'WordPress and Elasticsearch content are out of sync.', 'elasticpress' ),
1048 + ];
1049 + }
1050 + }
1051 +
1052 + /**
1053 + * Filter the document status array.
1054 + *
1055 + * @since 5.2.0
1056 + * @hook ep_doc_status
1057 + * @param array $status The status array containing status, message and explanation
1058 + * @param int $post_id The post ID being checked
1059 + * @param array $es_doc The Elasticsearch document
1060 + */
1061 + return (array) apply_filters( 'ep_doc_status', $status, $post_id, $es_doc );
1062 + }
1063 +
1064 + /**
1065 + * Format the document status for the admin bar.
1066 + *
1067 + * @since 5.2.0
1068 + * @deprecated 5.3.0
1069 + * @param array $document_status Document status
1070 + * @return string
1071 + */
1072 + protected function format_doc_status( array $document_status ): string {
1073 + $status_indicator = '<span class="ep-status-indicator ep-status-indicator--' . ( $document_status['status'] ?? '' ) . '"></span>';
1074 +
1075 + $message = sprintf(
1076 + // translators: 1: EP prefix 2: Document status message
1077 + _x( '[%1$s] %2$s', 'Doc status message', 'elasticpress' ),
1078 + 'EP',
1079 + $document_status['message']
1080 + );
1081 +
1082 + /**
1083 + * Filter the formatted document status.
1084 + *
1085 + * @since 5.2.0
1086 + * @deprecated 5.3.0
1087 + * @hook ep_formatted_doc_status
1088 + * @param string $formatted_status The formatted status
1089 + * @param array $document_status The document status
1090 + * @param string $status_indicator The status indicator
1091 + * @param string $message The message
1092 + */
1093 + return (string) apply_filters_deprecated(
1094 + 'ep_formatted_doc_status',
1095 + [ $status_indicator . $message, $document_status, $status_indicator, $message ],
1096 + 'ElasticPress 5.3.0',
1097 + 'ep_admin_bar_status_and_summary'
1098 + );
1099 + }
1100 +
1101 + /**
1102 + * If a password is added to an existent post, delete it from the index.
1103 + *
1104 + * @since 5.2.0
1105 + * @param int $post_id The post ID
1106 + * @param \WP_Post $post_after The post object after the update
1107 + * @param \WP_Post $post_before The post object before the update
1108 + * @return void
1109 + */
1110 + public function delete_post_with_new_password( $post_id, $post_after, $post_before ) {
1111 + if ( ! $post_before->post_password && $post_after->post_password ) {
1112 + Indexables::factory()->get( $this->indexable_slug )->delete( $post_id, false );
1113 + }
879 1114 }
880 1115 }