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
159 changes: 37 additions & 122 deletions includes/classes/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@

namespace ElasticPress;

use \WP_CLI_Command as WP_CLI_Command;
use \WP_CLI as WP_CLI;
use ElasticPress\Features as Features;
use ElasticPress\Utils as Utils;
use ElasticPress\Elasticsearch as Elasticsearch;
use ElasticPress\Indexables as Indexables;
use \WP_CLI_Command;
use \WP_CLI;
use ElasticPress\Features;
use ElasticPress\Utils;
use ElasticPress\Elasticsearch;
use ElasticPress\Indexables;
use ElasticPress\Command\Utility;

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 @@ -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 );
}
_deprecated_function( __METHOD__, '4.5.0', '\ElasticPress\Command\Utility::delete_transient_on_int' );
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' );
}
_deprecated_function( __METHOD__, '4.5.0', '\ElasticPress\Command\Utility::delete_transient()' );
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 );
}
_deprecated_function( __METHOD__, '4.5.0', '\ElasticPress\Command\Utility::should_interrupt_sync' );
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,8 @@ 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
);
}

return $should_interrupt_sync ? (bool) $should_interrupt_sync : null;
_deprecated_function( __METHOD__, '4.5.0', '\ElasticPress\Command\Utility::custom_get_transient' );
return Utility::custom_get_transient( $pre_transient, $transient );
}

/**
Expand Down Expand Up @@ -1359,7 +1319,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 +1331,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 @@ -1390,11 +1350,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' ) );
}
_deprecated_function( __METHOD__, '4.5.0', '\ElasticPress\Command\Utility::stop_on_failed_mapping' );
Utility::stop_on_failed_mapping( $index_meta, $indexable, $result );
}

/**
Expand All @@ -1407,15 +1364,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 );
_deprecated_function( __METHOD__, '4.5.0', '\ElasticPress\Command\Utility::call_ep_cli_put_mapping' );
Utility::call_ep_cli_put_mapping( $index_meta, $indexable );
}

/**
Expand Down Expand Up @@ -1543,41 +1493,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