| @@ -89,8 +89,14 @@ | ||
| 89 | 89 | add_action( 'delete_post_metadata', [ $this, 'clear_meta_keys_db_per_post_type_cache_by_meta' ], 10, 2 ); |
| 90 | 90 | |
| 91 | 91 | // Prevents password protected posts from being indexed |
| 92 | 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 ); | |
| 93 | 99 | } |
| 94 | 100 | |
| 95 | 101 | /** |
| 96 | 102 | * Un-setup actions and filters (for multisite). |
| @@ -115,8 +121,10 @@ | ||
| 115 | 121 | // Clear index settings cache |
| 116 | 122 | remove_action( 'ep_update_index_settings', [ $this, 'clear_index_settings_cache' ] ); |
| 117 | 123 | remove_action( 'ep_after_put_mapping', [ $this, 'clear_index_settings_cache' ] ); |
| 118 | 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' ] ); | |
| 119 | 127 | } |
| 120 | 128 | |
| 121 | 129 | /** |
| 122 | 130 | * Whether to delete all meta from other posts that is associated with the deleted post. |
| @@ -223,9 +231,9 @@ | ||
| 223 | 231 | |
| 224 | 232 | if ( $query->have_posts() && $query->elasticsearch_success ) { |
| 225 | 233 | $posts_to_be_synced = array_filter( |
| 226 | 234 | $query->posts, |
| 227 | - function( $object_id ) { | |
| 235 | + function ( $object_id ) { | |
| 228 | 236 | return ! apply_filters( 'ep_post_sync_kill', false, $object_id, $object_id ); |
| 229 | 237 | } |
| 230 | 238 | ); |
| 231 | 239 | if ( ! empty( $posts_to_be_synced ) ) { |
| @@ -378,8 +386,9 @@ | ||
| 378 | 386 | * @param {int} $object_id ID of post |
| 379 | 387 | * @return {boolean} New value |
| 380 | 388 | */ |
| 381 | 389 | if ( apply_filters( 'ep_post_sync_kill', false, $post_id, $post_id ) ) { |
| 390 | + $this->remove_from_queue( $post_id ); | |
| 382 | 391 | return; |
| 383 | 392 | } |
| 384 | 393 | |
| 385 | 394 | $this->add_to_queue( $post_id ); |
| @@ -420,8 +429,9 @@ | ||
| 420 | 429 | Utils\get_sync_url() |
| 421 | 430 | ), |
| 422 | 431 | 'type' => 'warning', |
| 423 | 432 | 'dismiss' => true, |
| 433 | + 'scope' => 'site', | |
| 424 | 434 | ]; |
| 425 | 435 | break; |
| 426 | 436 | } |
| 427 | 437 | } |
| @@ -435,8 +445,9 @@ | ||
| 435 | 445 | Utils\get_sync_url() |
| 436 | 446 | ), |
| 437 | 447 | 'type' => 'warning', |
| 438 | 448 | 'dismiss' => true, |
| 449 | + 'scope' => 'site', | |
| 439 | 450 | ]; |
| 440 | 451 | |
| 441 | 452 | return $notices; |
| 442 | 453 | } |
| @@ -469,8 +480,9 @@ | ||
| 469 | 480 | Utils\get_sync_url() |
| 470 | 481 | ), |
| 471 | 482 | 'type' => 'warning', |
| 472 | 483 | 'dismiss' => true, |
| 484 | + 'scope' => 'site', | |
| 473 | 485 | ]; |
| 474 | 486 | |
| 475 | 487 | return $notices; |
| 476 | 488 | } |
| @@ -788,15 +800,15 @@ | ||
| 788 | 800 | delete_transient( 'ep_meta_field_keys_' . $post_type ); |
| 789 | 801 | } |
| 790 | 802 | |
| 791 | 803 | /** |
| 792 | - * 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. | |
| 793 | 805 | * |
| 794 | - * @param int $post_id The post ID. | |
| 795 | - * @param string $taxonomy The taxonomy slug. | |
| 806 | + * @since 5.2.0 | |
| 807 | + * @param int $post_id Post ID. | |
| 796 | 808 | * @return boolean |
| 797 | 809 | */ |
| 798 | - protected function should_reindex_post( $post_id, $taxonomy ) { | |
| 810 | + public function is_post_indexable( $post_id ) { | |
| 799 | 811 | /** |
| 800 | 812 | * Filter to kill post sync |
| 801 | 813 | * |
| 802 | 814 | * @hook ep_post_sync_kill |
| @@ -821,8 +833,32 @@ | ||
| 821 | 833 | if ( ! in_array( $post->post_status, $indexable_post_statuses, true ) ) { |
| 822 | 834 | return false; |
| 823 | 835 | } |
| 824 | 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 | + | |
| 825 | 861 | // Only re-index if the taxonomy is indexed for this post |
| 826 | 862 | $indexable_taxonomies = $indexable->get_indexable_post_taxonomies( $post ); |
| 827 | 863 | $indexable_taxonomy_names = wp_list_pluck( $indexable_taxonomies, 'name' ); |
| 828 | 864 | if ( ! in_array( $taxonomy, $indexable_taxonomy_names, true ) ) { |
| @@ -828,14 +864,8 @@ | ||
| 828 | 864 | if ( ! in_array( $taxonomy, $indexable_taxonomy_names, true ) ) { |
| 829 | 865 | return false; |
| 830 | 866 | } |
| 831 | 867 | |
| 832 | - // Check post type | |
| 833 | - $indexable_post_types = $indexable->get_indexable_post_types(); | |
| 834 | - if ( ! in_array( $post->post_type, $indexable_post_types, true ) ) { | |
| 835 | - return false; | |
| 836 | - } | |
| 837 | - | |
| 838 | 868 | // If we have more items to update than the number set as Content Items per Index Cycle, skip it to avoid a timeout. |
| 839 | 869 | $single_ids_queued = array_unique( array_keys( $this->get_sync_queue() ) ); |
| 840 | 870 | $has_too_many_queued = count( $single_ids_queued ) > IndexHelper::factory()->get_index_default_per_page(); |
| 841 | 871 | |
| @@ -851,9 +881,9 @@ | ||
| 851 | 881 | * @since 4.4.0 |
| 852 | 882 | * @param \WP_Taxonomy $tax The taxonomy object |
| 853 | 883 | * @return boolean |
| 854 | 884 | */ |
| 855 | - 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 { | |
| 856 | 886 | $transient_name = "ep_term_max_count_{$tax->name}"; |
| 857 | 887 | $cached_max_count = get_transient( $transient_name ); |
| 858 | 888 | |
| 859 | 889 | if ( is_integer( $cached_max_count ) ) { |
| @@ -935,6 +965,151 @@ | ||
| 935 | 965 | if ( ! in_array( 'attachment', $indexable_post_types, true ) ) { |
| 936 | 966 | return; |
| 937 | 967 | } |
| 938 | 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 | + } | |
| 939 | 1114 | } |
| 940 | 1115 | } |