Skip to content

Commit

Permalink
Merge branch '5.0.0' into feature/sync-page-ui
Browse files Browse the repository at this point in the history
  • Loading branch information
JakePT committed Sep 26, 2023
2 parents abbbaea + 552a4f2 commit 808e0e5
Show file tree
Hide file tree
Showing 25 changed files with 301 additions and 136 deletions.
36 changes: 31 additions & 5 deletions elasticpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,42 @@
exit; // Exit if accessed directly.
}

// Require Composer autoloader if it exists.
if ( file_exists( __DIR__ . '/vendor-prefixed/autoload.php' ) ) {
require_once __DIR__ . '/vendor-prefixed/autoload.php';
}

define( 'EP_URL', plugin_dir_url( __FILE__ ) );
define( 'EP_PATH', plugin_dir_path( __FILE__ ) );
define( 'EP_FILE', plugin_basename( __FILE__ ) );
define( 'EP_VERSION', '4.7.1' );

define( 'EP_PHP_VERSION_MIN', '7.0' );

if ( ! version_compare( phpversion(), EP_PHP_VERSION_MIN, '>=' ) ) {
add_action(
'admin_notices',
function() {
?>
<div class="notice notice-error">
<p>
<?php
echo wp_kses_post(
sprintf(
/* translators: %s: Minimum required PHP version */
__( 'ElasticPress requires PHP version %s or later. Please upgrade PHP or disable the plugin.', 'elasticpress' ),
EP_PHP_VERSION_MIN
)
);
?>
</p>
</div>
<?php
}
);
return;
}

// Require Composer autoloader if it exists.
if ( file_exists( __DIR__ . '/vendor-prefixed/autoload.php' ) ) {
require_once __DIR__ . '/vendor-prefixed/autoload.php';
}

/**
* PSR-4-ish autoloading
*
Expand Down
2 changes: 1 addition & 1 deletion includes/classes/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ public function get_ongoing_sync_status( $args, $assoc_args ) {
*/
public function get_last_sync( $args, $assoc_args ) {
$pretty = \WP_CLI\Utils\get_flag_value( $assoc_args, 'pretty' );
$last_sync = \ElasticPress\IndexHelper::factory()->get_last_index();
$last_sync = \ElasticPress\IndexHelper::factory()->get_last_sync();

$this->pretty_json_encode( $last_sync, $pretty );
}
Expand Down
18 changes: 12 additions & 6 deletions includes/classes/Feature/Autosuggest/Autosuggest.php
Original file line number Diff line number Diff line change
Expand Up @@ -593,13 +593,19 @@ public function apply_autosuggest_weighting( $config = [] ) {
public function intercept_search_request( $response, $query = [] ) {
$this->autosuggest_query = $query['args']['body'];

$message = wp_json_encode(
[
esc_html__( 'This is a fake request to build the ElasticPress Autosuggest query. It is not really sent.', 'elasticpress' ),
]
);

return [
'response' => [ 'code' => 200 ],
'body' => wp_json_encode(
[
esc_html__( 'This is a fake request to build the ElasticPress Autosuggest query. It is not really sent.', 'elasticpress' ),
]
),
'is_ep_fake_request' => true,
'body' => $message,
'response' => [
'code' => 200,
'message' => $message,
],
];
}

Expand Down
12 changes: 6 additions & 6 deletions includes/classes/Feature/Search/Synonyms.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,8 @@ public function add_search_synonyms( $mapping, $index ) {
}

// Ensure we have analyzers and that it is an array.
if ( ! isset( $mapping['settings']['analysis']['analyzer']['default']['filter'] )
|| ! is_array( $mapping['settings']['analysis']['analyzer']['default']['filter'] )
if ( ! isset( $mapping['settings']['analysis']['analyzer']['default_search']['filter'] )
|| ! is_array( $mapping['settings']['analysis']['analyzer']['default_search']['filter'] )
) {
return $mapping;
}
Expand All @@ -389,10 +389,10 @@ public function add_search_synonyms( $mapping, $index ) {
$mapping['settings']['analysis']['filter'][ $filter_name ] = $this->get_synonym_filter();

// Tell the analyzer to use our newly created filter.
$mapping['settings']['analysis']['analyzer']['default']['filter'] = array_values(
$mapping['settings']['analysis']['analyzer']['default_search']['filter'] = array_values(
array_merge(
[ $filter_name ],
$mapping['settings']['analysis']['analyzer']['default']['filter']
$mapping['settings']['analysis']['analyzer']['default_search']['filter']
)
);

Expand Down Expand Up @@ -464,7 +464,7 @@ public function update_synonyms() {
function( $success, $index ) {
$filter = $this->get_synonym_filter();
$mapping = Elasticsearch::factory()->get_mapping( $index );
$filters = $mapping[ $index ]['settings']['index']['analysis']['analyzer']['default']['filter'];
$filters = $mapping[ $index ]['settings']['index']['analysis']['analyzer']['default_search']['filter'];

/*
* Due to limitations in Elasticsearch, we can't remove the filter and analyzer
Expand All @@ -479,7 +479,7 @@ function( $success, $index ) {
$setting['index']['analysis']['filter']['ep_synonyms_filter'] = $filter;

// Add the analyzer.
$setting['index']['analysis']['analyzer']['default']['filter'] = array_values(
$setting['index']['analysis']['analyzer']['default_search']['filter'] = array_values(
array_unique(
array_merge(
[ $this->get_synonym_filter_name() ],
Expand Down
87 changes: 77 additions & 10 deletions includes/classes/IndexHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ protected function build_index_meta() {
'start_date_time' => $start_date_time ? $start_date_time->format( DATE_ATOM ) : false,
'starting_indices' => $starting_indices,
'messages_queue' => [],
'trigger' => 'manual',
'totals' => [
'total' => 0,
'synced' => 0,
Expand Down Expand Up @@ -870,24 +871,64 @@ protected function index_cleanup() {
* @since 4.2.0
*/
protected function update_last_index() {
$is_full_sync = $this->index_meta['put_mapping'];
$method = $this->index_meta['method'];
$start_time = $this->index_meta['start_time'];
$totals = $this->index_meta['totals'];
$method = $this->index_meta['method'];
$is_full_sync = $this->index_meta['put_mapping'];
$trigger = $this->index_meta['trigger'];

$this->index_meta = null;

$end_date_time = date_create( 'now', wp_timezone() );
$start_time_sec = (int) $start_time;

// Time related info
$totals['end_date_time'] = $end_date_time ? $end_date_time->format( DATE_ATOM ) : false;
$totals['start_date_time'] = $start_time ? wp_date( DATE_ATOM, $start_time_sec ) : false;
$totals['end_time_gmt'] = time();
$totals['total_time'] = microtime( true ) - $start_time;
$totals['method'] = $method;
$totals['is_full_sync'] = $is_full_sync;

// Additional info
$totals['is_full_sync'] = $is_full_sync;
$totals['method'] = $method;
$totals['trigger'] = $trigger;

Utils\update_option( 'ep_last_cli_index', $totals, false );
Utils\update_option( 'ep_last_index', $totals, false );

$this->add_last_sync( $totals );
}

/**
* Add a sync to the list of all past syncs
*
* @since 5.0.0
* @param array $last_sync_info The latest sync info to be added to the log
* @return void
*/
protected function add_last_sync( array $last_sync_info ) {
// Remove error messages from previous syncs - we only store msgs for the newest one.
$last_syncs = array_map(
function( $sync ) {
unset( $sync['errors'] );
return $sync;
},
$this->get_sync_history()
);

/**
* Filter the number of past syncs to keep info
*
* @since 5.0.0
* @hook ep_syncs_to_keep_info
* @param {int} $number Number of past syncs to keep info
* @return {int} New number
*/
$syncs_to_keep = (int) apply_filters( 'ep_syncs_to_keep_info', 5 );

$last_syncs = array_slice( $last_syncs, 0, $syncs_to_keep - 1 );
array_unshift( $last_syncs, $last_sync_info );

Utils\update_option( 'ep_sync_history', $last_syncs, false );
}

/**
Expand Down Expand Up @@ -980,7 +1021,7 @@ protected function output( $message_text, $type = 'info', $context = '' ) {
Utils\update_option( 'ep_index_meta', $this->index_meta );
} else {
Utils\delete_option( 'ep_index_meta' );
$totals = $this->get_last_index();
$totals = $this->get_last_sync();
}

$message = [
Expand Down Expand Up @@ -1084,13 +1125,27 @@ public function is_full_reindexing( $indexable_slug, $blog_id = null ) {
}

/**
* Get the last index/sync meta information.
* Get the previous syncs meta information.
*
* @since 4.2.0
* @since 5.0.0
* @return array
*/
public function get_last_index() {
return Utils\get_option( 'ep_last_index', [] );
public function get_sync_history() : array {
return Utils\get_option( 'ep_sync_history', [] );
}

/**
* Get the last sync meta information.
*
* @since 5.0.0
* @return array
*/
public function get_last_sync() : array {
$syncs = $this->get_sync_history();
if ( empty( $syncs ) ) {
return [];
}
return array_shift( $syncs );
}

/**
Expand Down Expand Up @@ -1427,4 +1482,16 @@ public static function factory() {

return $instance;
}

/**
* DEPRECATED. Get the last index/sync meta information.
*
* @since 4.2.0
* @deprecated 5.0.0
* @return array
*/
public function get_last_index() {
_deprecated_function( __METHOD__, '5.0.0', '\ElasticPress\IndexHelper::get_last_sync' );
return $this->get_last_sync();
}
}
5 changes: 2 additions & 3 deletions includes/classes/Indexable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

use ElasticPress\Elasticsearch as Elasticsearch;
use ElasticPress\SyncManager as SyncManager;
use ElasticPress\QueryIntegration as QueryIntegration;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
Expand Down Expand Up @@ -49,7 +48,7 @@ abstract class Indexable {
* Instance of QueryIntegration. This should handle integrating with a default
* WP query.
*
* @var QueryIntegration
* @var object
* @since 3.0
*/
public $query_integration;
Expand Down Expand Up @@ -1149,7 +1148,7 @@ abstract public function prepare_document( $object_id );
* process across indexables.
*
* @param array $args Array to query DB against.
* @return boolean
* @return array
*/
abstract public function query_db( $args );

Expand Down
2 changes: 1 addition & 1 deletion includes/classes/Indexable/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -1956,7 +1956,7 @@ protected function parse_post__not_in( $args ) {
'bool' => [
'must_not' => [
'terms' => [
'post_id' => (array) $args['post__not_in'],
'post_id' => array_values( (array) $args['post__not_in'] ),
],
],
],
Expand Down
2 changes: 1 addition & 1 deletion includes/classes/Indexables.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function get_all( $global = null, $slug_only = false, $status = 'active'
/**
* Return singleton instance of class
*
* @return object
* @return self
*/
public static function factory() {
static $instance = false;
Expand Down
2 changes: 1 addition & 1 deletion includes/classes/REST/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function get_sync_status( \WP_REST_Request $request ) {
wp_send_json_success(
[
'is_finished' => true,
'totals' => Utils\get_option( 'ep_last_index' ),
'totals' => IndexHelper::factory()->get_last_sync(),
]
);
}
Expand Down
12 changes: 6 additions & 6 deletions includes/classes/Screen/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ public function admin_enqueue_scripts() {

$data = [];

$ep_last_index = IndexHelper::factory()->get_last_index();
$indexables = Indexables::factory()->get_all();
$post_types = Indexables::factory()->get( 'post' )->get_indexable_post_types();
$post_types = array_values( $post_types );
$ep_last_sync = IndexHelper::factory()->get_last_sync();
$indexables = Indexables::factory()->get_all();
$post_types = Indexables::factory()->get( 'post' )->get_indexable_post_types();
$post_types = array_values( $post_types );

$data['api_url'] = rest_url( 'elasticpress/v1/sync' );
$data['auto_start_index'] = isset( $_GET['do_sync'] ) && ( ! defined( 'EP_DASHBOARD_SYNC' ) || EP_DASHBOARD_SYNC ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$data['ep_last_sync_date'] = ! empty( $ep_last_index['end_date_time'] ) ? $ep_last_index['end_date_time'] : false;
$data['ep_last_sync_failed'] = ! empty( $ep_last_index['failed'] ) || ! empty( $ep_last_index['errors'] ) ? true : false;
$data['ep_last_sync_date'] = ! empty( $ep_last_sync['end_date_time'] ) ? $ep_last_sync['end_date_time'] : false;
$data['ep_last_sync_failed'] = ! empty( $ep_last_sync['failed'] ) || ! empty( $ep_last_sync['errors'] ) ? true : false;
$data['index_meta'] = Utils\get_indexing_status();
$data['indexables'] = array_map( fn( $indexable) => [ $indexable->slug, $indexable->labels['plural'] ], $indexables );
$data['is_epio'] = Utils\is_epio();
Expand Down
2 changes: 1 addition & 1 deletion includes/classes/StatusReport/LastSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function get_title() : string {
public function get_groups() : array {
$fields = [];

$sync_info = \ElasticPress\IndexHelper::factory()->get_last_index();
$sync_info = \ElasticPress\IndexHelper::factory()->get_last_sync();

if ( empty( $sync_info ) ) {
return [];
Expand Down
11 changes: 6 additions & 5 deletions includes/classes/SyncManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ public function __construct( $indexable_slug ) {
/**
* Add an object to the sync queue.
*
* @param id $object_id object ID to sync
* @since 3.1.2
* @since 3.1.2
*
* @param int $object_id Object ID to sync.
* @return boolean
*/
public function add_to_queue( $object_id ) {
Expand All @@ -95,8 +96,9 @@ public function add_to_queue( $object_id ) {
/**
* Remove an object from the sync queue.
*
* @param id $object_id object ID to remove from the queue
* @since 3.5
* @since 3.5
*
* @param int $object_id Object ID to remove from the queue.
* @return boolean
*/
public function remove_from_queue( $object_id ) {
Expand Down Expand Up @@ -147,7 +149,6 @@ public function index_sync_queue_on_redirect( $location ) {
return $location;
}


/**
* Sync objects in queue.
*
Expand Down
Loading

0 comments on commit 808e0e5

Please sign in to comment.