Skip to content

Commit

Permalink
Add "just-launched" command
Browse files Browse the repository at this point in the history
  • Loading branch information
edjeavons committed Sep 9, 2023
1 parent 228bc40 commit 04e499f
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
146 changes: 146 additions & 0 deletions src/Commands/JustLaunched.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php
/**
* Configure a newly launched website
*
* @package eighteen73/wpi-cli-tools
*/

namespace Eighteen73\WP_CLI\Commands;

use Eighteen73\WP_CLI\Helpers;
use WP_CLI;
use WP_CLI\ExitException;
use WP_CLI_Command;

/**
* Run common post-launch commands on a website to make sure it's prepared for the live domain.
*
* ## EXAMPLES
*
* # Configure a newly launched website
* $ wp @production eighteen73 just-launched
*
* @package eighteen73/wpi-cli-tools
*/
class JustLaunched extends WP_CLI_Command {


/**
* The new website domain
*
* @var string|null
*/
private ?string $new_domain = null;

/**
* The old website domain(s)
*
* @var array
*/
private array $old_domains = [];

/**
* Configure a newly launched website.
*
* This script should be used just after a website goes live, when its domain and file paths may have changes
* which affect certain functionality from working properly.
*
* The command can do a simple search-replace for old and new domains (see example below) but if you need more
* control please use `wp search-replace` separately.
*
* There is no harm in running this script multiple times or later after a website launch.
*
* ---
*
* ## EXAMPLES
*
* wp @production eighteen73 just-launched
*
* wp @production eighteen73 just-launched --old-domain=beta.example.com --new-domain=example.com
*
* @when before_wp_load
*
* @param array $args Arguments
* @param array $assoc_args Arguments
*/
public function __invoke( array $args, array $assoc_args ) {
Helpers::version_check();

$this->check_args( $assoc_args );

$this->change_domain();
$this->reset_data_and_caches();

WP_CLI::line();
WP_CLI::success( 'Complete' );
}

/**
* Run `wp search-replace` to update domain references
*
* @return void
*/
private function change_domain(): void {
if ( ! $this->old_domains || ! $this->new_domain ) {
return;
}

// Search and replace for domain changes
foreach ( $this->old_domains as $old_domain ) {
WP_CLI::line();
WP_CLI::line( "Replacing: {$old_domain}{$this->new_domain}" );
WP_CLI::line();
Helpers::wp_command(
'search-replace "//' . $old_domain . '" "//' . $this->new_domain . '"',
null,
false
);
}
}

/**
* Clear important caches (inc. transients)
*
* @return void
*/
private function reset_data_and_caches(): void {
WP_CLI::line();
WP_CLI::line( 'Clearing caches' );
WP_CLI::line();
Helpers::wp_command( 'transient delete --all', null, false );
Helpers::wp_command( 'cache flush', null, false );

$has_yoast = ! empty( Helpers::wp_command( 'plugin status wordpress-seo' ) );
if ( $has_yoast ) {
WP_CLI::line();
WP_CLI::line( 'Reindexing Yoast' );
WP_CLI::line();
Helpers::wp_command( 'yoast index --reindex', null, false );
}
}

/**
* Validation the user's options
*
* @param array $assoc_args Arguments
* @return void
*/
private function check_args( array $assoc_args ): void {
// Validate domains provided for search-replace. They must be straightforward domains, no protocols or paths.
$new_domain = $assoc_args['new-domain'] ?? null;
$old_domains = isset( $assoc_args['old-domain'] ) ? array_filter( array_unique( explode( ',', $assoc_args['old-domain'] ) ) ) : [];
if ( $old_domains ) {
foreach ( $old_domains as $old_domain ) {
if ( ! filter_var( $old_domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME ) ) {
WP_CLI::error( "{$old_domain} is not a valid domain. If you have a complex replacement please use `wp search-replace`" );
}
}
if ( ! filter_var( $new_domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME ) ) {
WP_CLI::error( "{$new_domain} is not a valid domain. If you have a complex replacement please use `wp search-replace`" );
}
$this->old_domains = $old_domains;
$this->new_domain = $new_domain;
}
}

}
1 change: 1 addition & 0 deletions wp-cli-tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
WP_CLI::add_command( 'eighteen73 create-site', 'Eighteen73\WP_CLI\Commands\CreateSite' );
WP_CLI::add_command( 'eighteen73 first-sync', 'Eighteen73\WP_CLI\Commands\FirstSync' );
WP_CLI::add_command( 'eighteen73 sync', 'Eighteen73\WP_CLI\Commands\Sync' );
WP_CLI::add_command( 'eighteen73 just-launched', 'Eighteen73\WP_CLI\Commands\JustLaunched' );

// WP_CLI::add_command('eighteen73 create-plugin', ToDo::class);
// WP_CLI::add_command('eighteen73 create-block', ToDo::class);
Expand Down

0 comments on commit 04e499f

Please sign in to comment.