| 1 |
<?php |
| 2 |
/** |
| 3 |
* Manage syncing of content between WP and Elasticsearch for Comments |
| 4 |
* |
| 5 |
* @since 3.6.0 |
| 6 |
* @package elasticpress |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ElasticPress\Indexable\Comment; |
| 10 |
|
| 11 |
use ElasticPress\Elasticsearch; |
| 12 |
use ElasticPress\Indexables; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; // Exit if accessed directly. |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Sync manager class |
| 20 |
*/ |
| 21 |
class SyncManager extends \ElasticPress\SyncManager { |
| 22 |
/** |
| 23 |
* Indexable slug |
| 24 |
* |
| 25 |
* @since 4.7.0 |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
public $indexable_slug = 'comment'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Setup actions and filters |
| 32 |
* |
| 33 |
* @since 3.6.0 |
| 34 |
*/ |
| 35 |
public function setup() { |
| 36 |
if ( ! Elasticsearch::factory()->get_elasticsearch_version() ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
if ( ! $this->can_index_site() ) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
add_action( 'wp_insert_comment', [ $this, 'action_sync_on_insert' ] ); |
| 45 |
add_action( 'edit_comment', [ $this, 'action_sync_on_update' ] ); |
| 46 |
add_action( 'transition_comment_status', [ $this, 'action_sync_on_transition_comment_status' ], 10, 3 ); |
| 47 |
|
| 48 |
add_action( 'trashed_comment', [ $this, 'action_sync_on_delete' ] ); |
| 49 |
add_action( 'deleted_comment', [ $this, 'action_sync_on_delete' ] ); |
| 50 |
|
| 51 |
add_action( 'added_comment_meta', [ $this, 'action_queue_meta_sync' ], 10, 2 ); |
| 52 |
add_action( 'deleted_comment_meta', [ $this, 'action_queue_meta_sync' ], 10, 2 ); |
| 53 |
add_action( 'updated_comment_meta', [ $this, 'action_queue_meta_sync' ], 10, 2 ); |
| 54 |
|
| 55 |
// Clear index settings cache |
| 56 |
add_action( 'ep_update_index_settings', [ $this, 'clear_index_settings_cache' ] ); |
| 57 |
add_action( 'ep_after_put_mapping', [ $this, 'clear_index_settings_cache' ] ); |
| 58 |
add_action( 'ep_saved_weighting_configuration', [ $this, 'clear_index_settings_cache' ] ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Un-setup actions and filters (for multisite). |
| 63 |
* |
| 64 |
* @since 4.0 |
| 65 |
*/ |
| 66 |
public function tear_down() { |
| 67 |
remove_action( 'wp_insert_comment', [ $this, 'action_sync_on_insert' ] ); |
| 68 |
remove_action( 'edit_comment', [ $this, 'action_sync_on_update' ] ); |
| 69 |
remove_action( 'transition_comment_status', [ $this, 'action_sync_on_transition_comment_status' ] ); |
| 70 |
remove_action( 'trashed_comment', [ $this, 'action_sync_on_delete' ] ); |
| 71 |
remove_action( 'deleted_comment', [ $this, 'action_sync_on_delete' ] ); |
| 72 |
remove_action( 'added_comment_meta', [ $this, 'action_queue_meta_sync' ] ); |
| 73 |
remove_action( 'deleted_comment_meta', [ $this, 'action_queue_meta_sync' ] ); |
| 74 |
remove_action( 'updated_comment_meta', [ $this, 'action_queue_meta_sync' ] ); |
| 75 |
|
| 76 |
// Clear index settings cache |
| 77 |
remove_action( 'ep_update_index_settings', [ $this, 'clear_index_settings_cache' ] ); |
| 78 |
remove_action( 'ep_after_put_mapping', [ $this, 'clear_index_settings_cache' ] ); |
| 79 |
remove_action( 'ep_saved_weighting_configuration', [ $this, 'clear_index_settings_cache' ] ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Sync ES index when new comments are saved |
| 84 |
* |
| 85 |
* @param int $comment_id Comment ID. |
| 86 |
* @since 3.6.3 |
| 87 |
*/ |
| 88 |
public function action_sync_on_insert( $comment_id ) { |
| 89 |
if ( $this->kill_sync() ) { |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
$this->maybe_index_comment( $comment_id ); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Sync ES index with changes to the comment being saved |
| 98 |
* |
| 99 |
* @param int $comment_id Comment ID. |
| 100 |
* @since 3.6.0 |
| 101 |
*/ |
| 102 |
public function action_sync_on_update( $comment_id ) { |
| 103 |
if ( $this->kill_sync() ) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
if ( ! current_user_can( 'edit_comment', $comment_id ) ) { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
$this->maybe_index_comment( $comment_id ); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Sync ES index with changes to the comment status |
| 116 |
* |
| 117 |
* @param int|string $new_status The new comment status. |
| 118 |
* @param int|string $old_status The old comment status. |
| 119 |
* @param WP_Comment $comment Comment object. |
| 120 |
* @since 3.6.3 |
| 121 |
*/ |
| 122 |
public function action_sync_on_transition_comment_status( $new_status, $old_status, $comment ) { |
| 123 |
if ( $this->kill_sync() ) { |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
if ( current_user_can( 'edit_comment', $comment->comment_ID ) || current_user_can( 'moderate_comments', $comment->comment_ID ) ) { |
| 128 |
$this->maybe_index_comment( $comment->comment_ID ); |
| 129 |
} else { |
| 130 |
return; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Delete comment from ES when deleted or trashed in WP |
| 136 |
* |
| 137 |
* @param int $comment_id Comment ID. |
| 138 |
* @since 3.6.0 |
| 139 |
*/ |
| 140 |
public function action_sync_on_delete( $comment_id ) { |
| 141 |
if ( $this->kill_sync() ) { |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
if ( ! current_user_can( 'moderate_comments', $comment_id ) ) { |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
Indexables::factory()->get( 'comment' )->delete( $comment_id, false ); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* When comment meta is updated/added/deleted, queue the comment for reindex |
| 154 |
* |
| 155 |
* @param int $meta_id Meta ID. |
| 156 |
* @param int $comment_id Comment ID. |
| 157 |
* @since 3.6.0 |
| 158 |
*/ |
| 159 |
public function action_queue_meta_sync( $meta_id, $comment_id ) { |
| 160 |
if ( $this->kill_sync() ) { |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
if ( ! current_user_can( 'edit_comment_meta', $comment_id ) ) { |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
$this->maybe_index_comment( $comment_id ); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Maybe index a comment |
| 173 |
* |
| 174 |
* Check if comment could be indexed. |
| 175 |
* |
| 176 |
* @param int $comment_id Comment ID. |
| 177 |
* @since 3.6.0 |
| 178 |
*/ |
| 179 |
protected function maybe_index_comment( $comment_id ) { |
| 180 |
$comment = get_comment( $comment_id ); |
| 181 |
|
| 182 |
if ( ! empty( $comment ) ) { |
| 183 |
|
| 184 |
$comment_status = $comment->comment_approved; |
| 185 |
$post_status = get_post_status( $comment->comment_post_ID ); |
| 186 |
|
| 187 |
$indexable_comment_statuses = Indexables::factory()->get( 'comment' )->get_indexable_comment_status(); |
| 188 |
$indexable_post_statuses = Indexables::factory()->get( 'post' )->get_indexable_post_status(); |
| 189 |
|
| 190 |
$has_allowed_comment_status = [ 'all' ] === $indexable_comment_statuses ? true : in_array( $comment_status, $indexable_comment_statuses, true ); |
| 191 |
$has_allowed_post_status = in_array( $post_status, $indexable_post_statuses, true ); |
| 192 |
|
| 193 |
if ( ! $has_allowed_comment_status || ! $has_allowed_post_status ) { |
| 194 |
$this->action_sync_on_delete( $comment_id ); |
| 195 |
} else { |
| 196 |
$comment_type = $comment->comment_type; |
| 197 |
$post_type = get_post_type( $comment->comment_post_ID ); |
| 198 |
|
| 199 |
$indexable_comment_types = Indexables::factory()->get( 'comment' )->get_indexable_comment_types(); |
| 200 |
$indexable_post_types = Indexables::factory()->get( 'post' )->get_indexable_post_types(); |
| 201 |
|
| 202 |
if ( in_array( $comment_type, $indexable_comment_types, true ) && in_array( $post_type, $indexable_post_types, true ) ) { |
| 203 |
/** |
| 204 |
* Fire before comment is queued for syncing |
| 205 |
* |
| 206 |
* @hook ep_sync_comment_on_transition |
| 207 |
* @since 3.6.0 |
| 208 |
* @param {int} $comment_id Comment ID |
| 209 |
*/ |
| 210 |
do_action( 'ep_sync_comment_on_transition', $comment_id ); |
| 211 |
|
| 212 |
/** |
| 213 |
* Filter to kill comment sync |
| 214 |
* |
| 215 |
* @hook ep_comment_sync_kill |
| 216 |
* @since 3.6.0 |
| 217 |
* @param {bool} $skip True means kill sync for comment |
| 218 |
* @param {int} $comment_id Comment ID |
| 219 |
* @return {boolean} New value |
| 220 |
*/ |
| 221 |
if ( apply_filters( 'ep_comment_sync_kill', false, $comment_id ) ) { |
| 222 |
return; |
| 223 |
} |
| 224 |
|
| 225 |
$this->add_to_queue( $comment_id ); |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|