Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: All public methods in Command class are treated as a wpcli command #3320

Merged
merged 15 commits into from
Feb 27, 2023
153 changes: 35 additions & 118 deletions includes/classes/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ElasticPress\Utils as Utils;
use ElasticPress\Elasticsearch as Elasticsearch;
use ElasticPress\Indexables as Indexables;
use ElasticPress\Command\Utility as Utility;
felipeelia marked this conversation as resolved.
Show resolved Hide resolved

if ( ! defined( 'ABSPATH' ) ) {
// @codeCoverageIgnoreStart
Expand Down Expand Up @@ -73,7 +74,7 @@ class Command extends WP_CLI_Command {
* @since 3.5.2
*/
public function __construct() {
add_filter( 'pre_transient_ep_wpcli_sync_interrupted', [ $this, 'custom_get_transient' ], 10, 2 );
add_filter( 'pre_transient_ep_wpcli_sync_interrupted', [ Utility::class, 'custom_get_transient' ], 10, 2 );
}

/**
Expand Down Expand Up @@ -631,7 +632,7 @@ public function epio_set_autosuggest( $args, $assoc_args ) {
}

/**
* Helper method for creating the network alias for an indexable
* Utility method for creating the network alias for an indexable
*
* @param Indexable $indexable Instance of indexable.
* @since 0.9
Expand Down Expand Up @@ -663,11 +664,8 @@ private function create_network_alias_helper( Indexable $indexable ) {
* @since 3.3
*/
public function delete_transient_on_int( $signal_no ) {
if ( SIGINT === $signal_no ) {
$this->delete_transient();
WP_CLI::log( esc_html__( 'Indexing cleaned up.', 'elasticpress' ) );
WP_CLI::halt( 0 );
}
_doing_it_wrong( __METHOD__, esc_html__( 'This method is deprecated and will be removed from 5.0. Use \ElasticPress\Command\Utility::delete_transient_on_int instead.', 'elasticpress' ), '4.5.1' );
felipeelia marked this conversation as resolved.
Show resolved Hide resolved
Utility::delete_transient_on_int( $signal_no );
}

/**
Expand Down Expand Up @@ -734,8 +732,6 @@ public function delete_transient_on_int( $signal_no ) {
* @param array $assoc_args Associative CLI args.
*/
public function sync( $args, $assoc_args ) {
global $wp_actions;

$setup_option = isset( $assoc_args['setup'] ) ? $assoc_args['setup'] : false;

if ( true === $setup_option ) {
Expand All @@ -746,7 +742,7 @@ public function sync( $args, $assoc_args ) {
WP_CLI::warning( esc_html__( 'Function pcntl_signal not available. Make sure to run `wp elasticpress clear-sync` in case the process is killed.', 'elasticpress' ) );
} else {
declare( ticks = 1 );
pcntl_signal( SIGINT, [ $this, 'delete_transient_on_int' ] );
pcntl_signal( SIGINT, [ Utility::class, 'delete_transient_on_int' ] );
}

$this->maybe_change_host( $assoc_args );
Expand Down Expand Up @@ -775,11 +771,11 @@ public function sync( $args, $assoc_args ) {
*/
do_action( 'ep_wp_cli_pre_index', $args, $assoc_args );

$this->timer_start();
Utility::timer_start();

add_action( 'ep_sync_put_mapping', [ $this, 'stop_on_failed_mapping' ], 10, 3 );
add_action( 'ep_sync_put_mapping', [ $this, 'call_ep_cli_put_mapping' ], 10, 2 );
add_action( 'ep_index_batch_new_attempt', [ $this, 'should_interrupt_sync' ] );
add_action( 'ep_sync_put_mapping', [ Utility::class, 'stop_on_failed_mapping' ], 10, 3 );
add_action( 'ep_sync_put_mapping', [ Utility::class, 'call_ep_cli_put_mapping' ], 10, 2 );
add_action( 'ep_index_batch_new_attempt', [ Utility::class, 'should_interrupt_sync' ] );

$no_bulk = ! empty( $assoc_args['nobulk'] );

Expand Down Expand Up @@ -830,11 +826,11 @@ public function sync( $args, $assoc_args ) {

\ElasticPress\IndexHelper::factory()->full_index( $index_args );

remove_action( 'ep_sync_put_mapping', [ $this, 'stop_on_failed_mapping' ] );
remove_action( 'ep_sync_put_mapping', [ $this, 'call_ep_cli_put_mapping' ], 10, 2 );
remove_action( 'ep_index_batch_new_attempt', [ $this, 'should_interrupt_sync' ] );
remove_action( 'ep_sync_put_mapping', [ Utility::class, 'stop_on_failed_mapping' ] );
remove_action( 'ep_sync_put_mapping', [ Utility::class, 'ep_cli_put_mapping' ], 10, 2 );
remove_action( 'ep_index_batch_new_attempt', [ Utility::class, 'maybe_interrupt_sync' ] );
felipeelia marked this conversation as resolved.
Show resolved Hide resolved

$sync_time_in_ms = $this->timer_stop();
$sync_time_in_ms = Utility::timer_stop();

/**
* Fires after executing a CLI index
Expand All @@ -847,9 +843,9 @@ public function sync( $args, $assoc_args ) {
*/
do_action( 'ep_wp_cli_after_index', $args, $assoc_args );

WP_CLI::log( WP_CLI::colorize( '%Y' . esc_html__( 'Total time elapsed: ', 'elasticpress' ) . '%N' . $this->timer_format( $sync_time_in_ms ) ) );
WP_CLI::log( WP_CLI::colorize( '%Y' . esc_html__( 'Total time elapsed: ', 'elasticpress' ) . '%N' . Utility::timer_format( $sync_time_in_ms ) ) );

$this->delete_transient();
Utility::delete_transient();

WP_CLI::success( esc_html__( 'Done!', 'elasticpress' ) );
}
Expand Down Expand Up @@ -973,15 +969,8 @@ private function index_occurring() {
* @since 3.1
*/
private function delete_transient() {
\ElasticPress\IndexHelper::factory()->clear_index_meta();

if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
delete_site_transient( 'ep_cli_sync_progress' );
delete_site_transient( 'ep_wpcli_sync_interrupted' );
} else {
delete_transient( 'ep_cli_sync_progress' );
delete_transient( 'ep_wpcli_sync_interrupted' );
}
_doing_it_wrong( __METHOD__, esc_html__( 'This method is deprecated and will be removed from 5.0. Use \ElasticPress\Command\Utility::delete_transient() instead.', 'elasticpress' ), '4.5.1' );
Utility::delete_transient();
}

/**
Expand All @@ -1003,7 +992,7 @@ public function clear_sync() {
*/
do_action( 'ep_cli_before_clear_index' );

$this->delete_transient();
Utility::delete_transient();

/**
* Fires after the CLI `clear-sync` command is executed.
Expand Down Expand Up @@ -1157,15 +1146,11 @@ function ( $prefix ) use ( $assoc_args ) {
* @since 3.5.2
*/
public function should_interrupt_sync() {
$should_interrupt_sync = get_transient( 'ep_wpcli_sync_interrupted' );

if ( $should_interrupt_sync ) {
WP_CLI::line( esc_html__( 'Sync was interrupted', 'elasticpress' ) );
$this->delete_transient_on_int( 2 );
WP_CLI::halt( 0 );
}
_doing_it_wrong( __METHOD__, esc_html__( 'This method is deprecated and will be removed from 5.0. Use \ElasticPress\Command\Utility::should_interrupt_sync instead.', 'elasticpress' ), '4.5.1' );
Utility::should_interrupt_sync();
}


felipeelia marked this conversation as resolved.
Show resolved Hide resolved
/**
* Stop the Sync operation started from the dashboard.
*
Expand Down Expand Up @@ -1286,33 +1271,9 @@ public function get_search_algorithm_version( $args, $assoc_args ) {
* @return true|null
*/
public function custom_get_transient( $pre_transient, $transient ) {
global $wpdb;

if ( wp_using_ext_object_cache() ) {
/**
* When external object cache is used we need to make sure to force a remote fetch,
* so that the value from the local memory is discarded.
*/
$should_interrupt_sync = wp_cache_get( $transient, 'transient', true );
} else {
$options = $wpdb->options;

$should_interrupt_sync = $wpdb->get_var(
// phpcs:disable
$wpdb->prepare(
"
SELECT option_value
FROM $options
WHERE option_name = %s
LIMIT 1
",
"_transient_{$transient}"
)
// phpcs:enable
);
}
_doing_it_wrong( __METHOD__, esc_html__( 'This function is deprecated and will be removed from 5.0. Use \ElasticPress\Command\Utility::custom_get_transient instead.', 'elasticpress' ), '3.5.2' );

return $should_interrupt_sync ? (bool) $should_interrupt_sync : null;
return Utility::custom_get_transient( $pre_transient, $transient );
}

/**
Expand All @@ -1336,10 +1297,10 @@ protected function render_stats( $current_index, $body ) {
}

/**
* Function used to ouput messages coming from IndexHelper
* Function used to ouput messages coming from IndexUtility
*
* @param array $message Message data
* @param array $args Args sent and processed by IndexHelper
* @param array $args Args sent and processed by IndexUtility
* @param array $index_meta Current index state
* @param string $context Context of the message being outputted
*/
Expand All @@ -1359,7 +1320,7 @@ public function index_output( $message, $args, $index_meta, $context ) {
break;

case 'error':
$this->clear_sync();
Utility::delete_transient();
felipeelia marked this conversation as resolved.
Show resolved Hide resolved
WP_CLI::error( $message['message'] );
break;

Expand All @@ -1371,9 +1332,9 @@ public function index_output( $message, $args, $index_meta, $context ) {
if ( 'index_next_batch' === $context ) {
$counter++;
if ( ( $counter % 10 ) === 0 ) {
$time_elapsed_diff = $time_elapsed > 0 ? ' (+' . (string) ( $this->timer_stop() - $time_elapsed ) . ')' : '';
$time_elapsed = $this->timer_stop( 2 );
WP_CLI::log( WP_CLI::colorize( '%Y' . esc_html__( 'Time elapsed: ', 'elasticpress' ) . '%N' . $this->timer_format( $time_elapsed ) . $time_elapsed_diff ) );
$time_elapsed_diff = $time_elapsed > 0 ? ' (+' . (string) ( Utility::timer_stop() - $time_elapsed ) . ')' : '';
$time_elapsed = Utility::timer_stop( 2 );
WP_CLI::log( WP_CLI::colorize( '%Y' . esc_html__( 'Time elapsed: ', 'elasticpress' ) . '%N' . Utility::timer_format( $time_elapsed ) . $time_elapsed_diff ) );

$current_memory = round( memory_get_usage() / 1024 / 1024, 2 ) . 'mb';
$peak_memory = ' (Peak: ' . round( memory_get_peak_usage() / 1024 / 1024, 2 ) . 'mb)';
Expand All @@ -1382,6 +1343,7 @@ public function index_output( $message, $args, $index_meta, $context ) {
}
}


/**
* If put_mapping fails while indexing, stop the index process.
*
Expand All @@ -1390,11 +1352,8 @@ public function index_output( $message, $args, $index_meta, $context ) {
* @param bool $result Whether the request was successful or not
*/
public function stop_on_failed_mapping( $index_meta, $indexable, $result ) {
if ( ! $result ) {
$this->delete_transient();

WP_CLI::error( esc_html__( 'Mapping Failed.', 'elasticpress' ) );
}
_doing_it_wrong( __METHOD__, esc_html__( 'This method is deprecated and will be removed from 5.0. Use \ElasticPress\Command\Utility::stop_on_failed_mapping instead.', 'elasticpress' ), '4.5.1' );
Utility::stop_on_failed_mapping( $index_meta, $indexable, $result );
}

/**
Expand All @@ -1407,15 +1366,8 @@ public function stop_on_failed_mapping( $index_meta, $indexable, $result ) {
* @return void
*/
public function call_ep_cli_put_mapping( $index_meta, $indexable ) {
/**
* Fires after CLI put mapping
*
* @hook ep_cli_put_mapping
* @param {Indexable} $indexable Indexable involved in mapping
* @param {array} $args CLI command position args
* @param {array} $assoc_args CLI command associative args
*/
do_action( 'ep_cli_put_mapping', $indexable, $this->args, $this->assoc_args );
_doing_it_wrong( __METHOD__, esc_html__( 'This method is deprecated and will be removed from 5.0. Use \ElasticPress\Command\Utility::call_ep_cli_put_mapping instead.', 'elasticpress' ), '4.5.1' );
Utility::call_ep_cli_put_mapping( $index_meta, $indexable );
}

/**
Expand Down Expand Up @@ -1543,41 +1495,6 @@ public function settings_reset( $args, $assoc_args ) {
WP_CLI::line( esc_html__( 'Settings deleted.', 'elasticpress' ) );
}

/**
* Starts the timer.
*
* @since 4.2.0
* @return true
*/
protected function timer_start() {
$this->time_start = microtime( true );
return true;
}

/**
* Stops the timer.
*
* @since 4.2.0
* @param int $precision The number of digits from the right of the decimal to display. Default 3.
* @return float Time spent so far
*/
protected function timer_stop( $precision = 3 ) {
$diff = microtime( true ) - $this->time_start;
return (float) number_format( (float) $diff, $precision );
}

/**
* Given a timestamp in microseconds, returns it in the given format.
*
* @since 4.2.0
* @param float $microtime Unix timestamp in ms
* @param string $format Desired format
* @return string
*/
protected function timer_format( $microtime, $format = 'H:i:s.u' ) {
$microtime_date = \DateTime::createFromFormat( 'U.u', number_format( (float) $microtime, 3, '.', '' ) );
return $microtime_date->format( $format );
}

/**
* Print an HTTP response.
Expand Down
Loading