-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: ElasticPress v5.0.0 API changes.
Ref: wpmlbridge-288
- Loading branch information
1 parent
a00e02c
commit c702222
Showing
6 changed files
with
245 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ tests/wordpress | |
Thumbs.db | ||
/tests/compatibility/*.txt | ||
.DS_Store | ||
.vscode | ||
|
||
inc/sandbox.inc | ||
*.sh | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace WPML\ElasticPress\Sync; | ||
|
||
use ElasticPress\Indexables; | ||
use ElasticPress\IndexHelper; | ||
use ElasticPress\Utils; | ||
|
||
use WPML\ElasticPress\Manager\Indices; | ||
use WPML\ElasticPress\Manager\DashboardStatus; | ||
|
||
use WPML\ElasticPress\Sync\Dashboard; | ||
|
||
use WPML\ElasticPress\Traits\ManageIndexables; | ||
|
||
/** | ||
* Before ElasticPress 5.0.0 the Dashboard sync was run over AJAX. | ||
*/ | ||
class DashboardAjax extends Dashboard { | ||
|
||
public function addHooks() { | ||
if ( 0 === count( $this->activeLanguages ) ) { | ||
return; | ||
} | ||
|
||
add_action( 'wp_ajax_ep_index', [ $this, 'action_wp_ajax_ep_index' ], 9 ); | ||
add_action( 'wp_ajax_ep_cancel_index', [ $this, 'action_wp_ajax_ep_cancel_index' ], 9 ); | ||
} | ||
|
||
private function isDashboardSync() { | ||
if ( ! check_ajax_referer( 'ep_dashboard_nonce', 'nonce', false ) || ! EP_DASHBOARD_SYNC ) { | ||
wp_send_json_error( null, 403 ); | ||
exit; | ||
} | ||
|
||
$index_meta = Utils\get_indexing_status(); | ||
|
||
if ( isset( $index_meta['method'] ) && 'cli' === $index_meta['method'] ) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function action_wp_ajax_ep_index() { | ||
if ( false === $this->isDashboardSync() ) { | ||
return; | ||
} | ||
$this->setUpAndRun(); | ||
} | ||
|
||
public function action_wp_ajax_ep_cancel_index() { | ||
if ( false === $this->isDashboardSync() ) { | ||
return; | ||
} | ||
$this->tearDown(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace WPML\ElasticPress\Sync; | ||
|
||
use ElasticPress\Indexables; | ||
use ElasticPress\IndexHelper; | ||
use ElasticPress\Utils; | ||
|
||
use WPML\ElasticPress\Manager\Indices; | ||
use WPML\ElasticPress\Manager\DashboardStatus; | ||
|
||
use WPML\ElasticPress\Sync\Dashboard; | ||
|
||
use WPML\ElasticPress\Traits\ManageIndexables; | ||
|
||
/** | ||
* After ElasticPress 5.0.0 the Dashboard sync was run over the REST API. | ||
*/ | ||
class DashboardRest extends Dashboard { | ||
|
||
public function addHooks() { | ||
if ( 0 === count( $this->activeLanguages ) ) { | ||
return; | ||
} | ||
|
||
add_filter( 'rest_request_before_callbacks', [ $this, 'rest_request_before_callbacks' ], 10, 3 ) ; | ||
} | ||
|
||
private function isSyncMethod( $method, $request ) { | ||
if ( $method !== $request->get_method() ) { | ||
return false; | ||
} | ||
|
||
$route = $request->get_route(); | ||
if ( preg_match( "/^\/elasticpress\/v\d+\/sync$/", $route ) ) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @param \WP_REST_Response|\WP_HTTP_Response|\WP_Error|mixed $response | ||
* @param array $handler | ||
* @param \WP_REST_Request $request | ||
* | ||
* @return \WP_REST_Response|\WP_HTTP_Response|\WP_Error|mixed | ||
*/ | ||
public function rest_request_before_callbacks( $response, $handler, $request ) { | ||
$capability = Utils\get_capability(); | ||
|
||
if ( ! current_user_can( $capability ) ) { | ||
return $response; | ||
} | ||
|
||
if ( $this->isSyncMethod( 'POST', $request ) ) { | ||
$this->setFullIndexArgs( $request->get_params() ); | ||
$this->setUpAndRun(); | ||
} | ||
|
||
if ( $this->isSyncMethod( 'DELETE', $request ) ) { | ||
$this->tearDown(); | ||
} | ||
|
||
return $response; | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.