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: support both VIP and normal WP CLI #45

Merged
merged 3 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions includes/classes/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@
exit; // Exit if accessed directly.
}

if ( class_exists( 'WPCOM_VIP_CLI_Command' ) ) {
class Base_CLI_Command extends \WPCOM_VIP_CLI_Command {}
} else {
class Base_CLI_Command extends \WP_CLI_Command {} // phpcs:ignore
}

/**
* Class: Sophi CLI Command.
*/
class Command extends \WPCOM_VIP_CLI_Command {
class Command extends Base_CLI_Command {

/**
* Sync all existing content to Sophi Collector.
Expand All @@ -36,11 +42,16 @@ class Command extends \WPCOM_VIP_CLI_Command {
* [--include=<number>]
* : Post IDs to process. Comma separated for passing multiple item.
*
* [--dry-run=<boolean>]
* : Whether to run command in the dry run mode. Default to true.
*
* @param array $args Arguments.
* @param array $assoc_args Options.
*/
public function sync( $args, $assoc_args ) {
$this->start_bulk_operation();
if ( class_exists( 'WPCOM_VIP_CLI_Command' ) ) {
$this->start_bulk_operation();
}

$per_page = 50;
$limit = 0;
Expand Down Expand Up @@ -168,8 +179,10 @@ function( $post_type ) use ( $post_types ) {
WP_CLI::line( 'Preparing for the next batch...' );
sleep( 3 );

// Free up memory.
$this->stop_the_insanity();
if ( class_exists( 'WPCOM_VIP_CLI_Command' ) ) {
// Free up memory.
$this->stop_the_insanity();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would be worth adding our own version of this method, so if someone isn't on VIP, they still get the performance benefits this provides? I'm not worried about the start_bulk_operation or end_bulk_operation methods, as those don't do much in our case but having this method could help some with performance when processing large batches of content

}

$paged++;

Expand All @@ -184,5 +197,9 @@ function( $post_type ) use ( $post_types ) {
} else {
WP_CLI::success( sprintf( '%d posts will be synced to Sophi Collector.', $count ) );
}

if ( class_exists( 'WPCOM_VIP_CLI_Command' ) ) {
$this->end_bulk_operation();
}
}
}
2 changes: 1 addition & 1 deletion sophi-for-wordpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function() {
SophiWP\Blocks\setup();
( new SophiWP\SiteAutomation\Services() )->register();

if ( defined( 'WP_CLI' ) && WP_CLI && class_exists( 'WPCOM_VIP_CLI_Command' ) ) {
if ( defined( 'WP_CLI' ) && WP_CLI ) {
try {
\WP_CLI::add_command( 'sophi', 'SophiWP\Command' );
} catch ( \Exception $e ) {
Expand Down