diff --git a/src/Commands/JustLaunched.php b/src/Commands/JustLaunched.php new file mode 100644 index 0000000..72dc525 --- /dev/null +++ b/src/Commands/JustLaunched.php @@ -0,0 +1,146 @@ +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; + } + } + +} diff --git a/wp-cli-tools.php b/wp-cli-tools.php index 6961e01..ae95144 100644 --- a/wp-cli-tools.php +++ b/wp-cli-tools.php @@ -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);