| 1 |
<?php |
| 2 |
/** |
| 3 |
* Sync (Dashboard Index) functionality |
| 4 |
* |
| 5 |
* @since 3.6.0 |
| 6 |
* @package elasticpress |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ElasticPress\Screen; |
| 10 |
|
| 11 |
use ElasticPress\IndexHelper; |
| 12 |
use ElasticPress\Screen; |
| 13 |
use ElasticPress\Utils; |
| 14 |
use ElasticPress\Stats; |
| 15 |
use ElasticPress\Elasticsearch; |
| 16 |
|
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit; // Exit if accessed directly. |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Class Sync |
| 23 |
* |
| 24 |
* @since 3.6.0 |
| 25 |
* @package ElasticPress |
| 26 |
*/ |
| 27 |
class Sync { |
| 28 |
/** |
| 29 |
* Initialize class |
| 30 |
*/ |
| 31 |
public function setup() { |
| 32 |
add_action( 'wp_ajax_ep_index', [ $this, 'action_wp_ajax_ep_index' ] ); |
| 33 |
add_action( 'wp_ajax_ep_index_status', [ $this, 'action_wp_ajax_ep_index_status' ] ); |
| 34 |
add_action( 'wp_ajax_ep_cancel_index', [ $this, 'action_wp_ajax_ep_cancel_index' ] ); |
| 35 |
|
| 36 |
add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Getting the status of ongoing index fired by WP CLI |
| 41 |
* |
| 42 |
* @since 3.6.0 |
| 43 |
*/ |
| 44 |
public function action_wp_ajax_ep_index_status() { |
| 45 |
if ( ! check_ajax_referer( 'ep_dashboard_nonce', 'nonce', false ) || ! EP_DASHBOARD_SYNC ) { |
| 46 |
wp_send_json_error( null, 403 ); |
| 47 |
exit; |
| 48 |
} |
| 49 |
|
| 50 |
$index_meta = Utils\get_indexing_status(); |
| 51 |
|
| 52 |
if ( isset( $index_meta['method'] ) && 'cli' === $index_meta['method'] ) { |
| 53 |
wp_send_json_success( |
| 54 |
[ |
| 55 |
'message' => sprintf( |
| 56 |
/* translators: 1. Number of objects indexed, 2. Total number of objects, 3. Last object ID */ |
| 57 |
esc_html__( 'Processed %1$d/%2$d. Last Object ID: %3$d', 'elasticpress' ), |
| 58 |
$index_meta['offset'], |
| 59 |
$index_meta['found_items'], |
| 60 |
$index_meta['current_sync_item']['last_processed_object_id'] |
| 61 |
), |
| 62 |
'index_meta' => $index_meta, |
| 63 |
] |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
wp_send_json_success( |
| 68 |
[ |
| 69 |
'is_finished' => true, |
| 70 |
'totals' => Utils\get_option( 'ep_last_index' ), |
| 71 |
] |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Perform index |
| 77 |
* |
| 78 |
* @since 3.6.0 |
| 79 |
*/ |
| 80 |
public function action_wp_ajax_ep_index() { |
| 81 |
if ( ! check_ajax_referer( 'ep_dashboard_nonce', 'nonce', false ) || ! EP_DASHBOARD_SYNC ) { |
| 82 |
wp_send_json_error( null, 403 ); |
| 83 |
exit; |
| 84 |
} |
| 85 |
|
| 86 |
$index_meta = Utils\get_indexing_status(); |
| 87 |
|
| 88 |
if ( isset( $index_meta['method'] ) && 'cli' === $index_meta['method'] ) { |
| 89 |
$this->action_wp_ajax_ep_index_status(); |
| 90 |
exit; |
| 91 |
} |
| 92 |
|
| 93 |
IndexHelper::factory()->full_index( |
| 94 |
[ |
| 95 |
'method' => 'dashboard', |
| 96 |
'put_mapping' => ! empty( $_REQUEST['put_mapping'] ), |
| 97 |
'output_method' => [ $this, 'index_output' ], |
| 98 |
'show_errors' => true, |
| 99 |
'network_wide' => 0, |
| 100 |
] |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Cancel index |
| 106 |
* |
| 107 |
* @since 3.6.0 |
| 108 |
*/ |
| 109 |
public function action_wp_ajax_ep_cancel_index() { |
| 110 |
if ( ! check_ajax_referer( 'ep_dashboard_nonce', 'nonce', false ) || ! EP_DASHBOARD_SYNC ) { |
| 111 |
wp_send_json_error( null, 403 ); |
| 112 |
exit; |
| 113 |
} |
| 114 |
|
| 115 |
$index_meta = Utils\get_indexing_status(); |
| 116 |
|
| 117 |
if ( isset( $index_meta['method'] ) && 'cli' === $index_meta['method'] ) { |
| 118 |
set_transient( 'ep_wpcli_sync_interrupted', true, MINUTE_IN_SECONDS ); |
| 119 |
wp_send_json_success(); |
| 120 |
exit; |
| 121 |
} |
| 122 |
|
| 123 |
Utils\delete_option( 'ep_index_meta' ); |
| 124 |
|
| 125 |
wp_send_json_success(); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Enqueue script. |
| 130 |
* |
| 131 |
* @since 3.6.0 |
| 132 |
* @return void |
| 133 |
*/ |
| 134 |
public function admin_enqueue_scripts() { |
| 135 |
if ( 'sync' !== Screen::factory()->get_current_screen() ) { |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
wp_enqueue_script( |
| 140 |
'ep_sync_scripts', |
| 141 |
EP_URL . 'dist/js/sync-script.js', |
| 142 |
Utils\get_asset_info( 'sync-script', 'dependencies' ), |
| 143 |
Utils\get_asset_info( 'sync-script', 'version' ), |
| 144 |
true |
| 145 |
); |
| 146 |
|
| 147 |
wp_set_script_translations( 'ep_sync_scripts', 'elasticpress' ); |
| 148 |
|
| 149 |
wp_enqueue_style( 'wp-components' ); |
| 150 |
wp_enqueue_style( 'wp-edit-post' ); |
| 151 |
|
| 152 |
wp_enqueue_style( |
| 153 |
'ep_sync_style', |
| 154 |
EP_URL . 'dist/css/sync-styles.css', |
| 155 |
Utils\get_asset_info( 'sync-styles', 'dependencies' ), |
| 156 |
Utils\get_asset_info( 'sync-styles', 'version' ) |
| 157 |
); |
| 158 |
|
| 159 |
$data = array( 'nonce' => wp_create_nonce( 'ep_dashboard_nonce' ) ); |
| 160 |
$index_meta = Utils\get_indexing_status(); |
| 161 |
$last_sync = Utils\get_option( 'ep_last_sync', false ); |
| 162 |
|
| 163 |
if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) { |
| 164 |
$install_complete_url = admin_url( 'network/admin.php?page=elasticpress&install_complete' ); |
| 165 |
} else { |
| 166 |
$install_complete_url = admin_url( 'admin.php?page=elasticpress&install_complete' ); |
| 167 |
} |
| 168 |
|
| 169 |
if ( isset( $_GET['do_sync'] ) && ( ! defined( 'EP_DASHBOARD_SYNC' ) || EP_DASHBOARD_SYNC ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 170 |
$data['auto_start_index'] = true; |
| 171 |
} |
| 172 |
|
| 173 |
if ( ! empty( $index_meta ) ) { |
| 174 |
$data['index_meta'] = $index_meta; |
| 175 |
} |
| 176 |
|
| 177 |
$ep_last_index = IndexHelper::factory()->get_last_index(); |
| 178 |
|
| 179 |
$indices_comparison = Elasticsearch::factory()->get_indices_comparison(); |
| 180 |
$sync_required = count( $indices_comparison['missing_indices'] ) > 0; |
| 181 |
|
| 182 |
if ( ! empty( $ep_last_index ) && ! $sync_required ) { |
| 183 |
$data['ep_last_sync_date'] = ! empty( $ep_last_index['end_date_time'] ) ? $ep_last_index['end_date_time'] : false; |
| 184 |
$data['ep_last_sync_failed'] = ! empty( $ep_last_index['failed'] ) || ! empty( $ep_last_index['errors'] ) ? true : false; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Filter indexable labels used in dashboard sync UI |
| 189 |
* |
| 190 |
* @since 3.0 |
| 191 |
* @hook ep_dashboard_indexable_labels |
| 192 |
* @param {array} $labels Current indexable lables |
| 193 |
* @return {array} New labels |
| 194 |
*/ |
| 195 |
$data['sync_indexable_labels'] = apply_filters( |
| 196 |
'ep_dashboard_indexable_labels', |
| 197 |
[ |
| 198 |
'post' => [ |
| 199 |
'singular' => esc_html__( 'Post', 'elasticpress' ), |
| 200 |
'plural' => esc_html__( 'Posts', 'elasticpress' ), |
| 201 |
], |
| 202 |
'term' => [ |
| 203 |
'singular' => esc_html__( 'Term', 'elasticpress' ), |
| 204 |
'plural' => esc_html__( 'Terms', 'elasticpress' ), |
| 205 |
], |
| 206 |
'user' => [ |
| 207 |
'singular' => esc_html__( 'User', 'elasticpress' ), |
| 208 |
'plural' => esc_html__( 'Users', 'elasticpress' ), |
| 209 |
], |
| 210 |
'comment' => [ |
| 211 |
'singular' => esc_html__( 'Comment', 'elasticpress' ), |
| 212 |
'plural' => esc_html__( 'Comments', 'elasticpress' ), |
| 213 |
], |
| 214 |
] |
| 215 |
); |
| 216 |
|
| 217 |
$data['ajax_url'] = admin_url( 'admin-ajax.php' ); |
| 218 |
$data['install_sync'] = empty( $last_sync ); |
| 219 |
$data['install_complete_url'] = esc_url( $install_complete_url ); |
| 220 |
$data['sync_complete'] = esc_html__( 'Sync complete', 'elasticpress' ); |
| 221 |
$data['sync_paused'] = esc_html__( 'Sync paused', 'elasticpress' ); |
| 222 |
$data['sync_syncing'] = esc_html__( 'Syncing', 'elasticpress' ); |
| 223 |
$data['sync_initial'] = esc_html__( 'Starting sync', 'elasticpress' ); |
| 224 |
$data['sync_wpcli'] = esc_html__( 'WP CLI sync is occurring.', 'elasticpress' ); |
| 225 |
$data['sync_error'] = esc_html__( 'An error occurred while syncing', 'elasticpress' ); |
| 226 |
$data['sync_interrupted'] = esc_html__( 'Sync interrupted.', 'elasticpress' ); |
| 227 |
$data['is_epio'] = Utils\is_epio(); |
| 228 |
|
| 229 |
wp_localize_script( 'ep_sync_scripts', 'epDash', $data ); |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Output information received from the index helper class. |
| 234 |
* |
| 235 |
* @param array $message Message to be outputted with its status and additional info, if needed. |
| 236 |
*/ |
| 237 |
public static function index_output( $message ) { |
| 238 |
switch ( $message['status'] ) { |
| 239 |
case 'success': |
| 240 |
wp_send_json_success( $message ); |
| 241 |
break; |
| 242 |
|
| 243 |
case 'error': |
| 244 |
wp_send_json_error( $message ); |
| 245 |
break; |
| 246 |
|
| 247 |
default: |
| 248 |
wp_send_json( [ 'data' => $message ] ); |
| 249 |
break; |
| 250 |
} |
| 251 |
exit; |
| 252 |
} |
| 253 |
} |
| 254 |
|