Skip to content

Instantly share code, notes, and snippets.

@FredericoC
Last active July 23, 2019 06:08
Show Gist options
  • Save FredericoC/177626bd1ca620741f6a742ac3a3901d to your computer and use it in GitHub Desktop.
Save FredericoC/177626bd1ca620741f6a742ac3a3901d to your computer and use it in GitHub Desktop.
Go Live CLI
<?php
if ( defined( 'WP_CLI' ) && WP_CLI ) {
// Custom "Go Live Pro CLI"
if ( defined( 'GO_LIVE_UPDATE_URLS_PRO_DIR' ) ) {
require_once 'go-live-pro-cli.php';
}
}
<?php
$plugin_path = dirname(GO_LIVE_UPDATE_URLS_PRO_DIR);
require_once $plugin_path . '/go-live-update-urls/src/Core.php';
require_once $plugin_path . '/go-live-update-urls/src/Admin_Page.php';
class Go_Live_Update_URLS_Pro_CLI extends WP_CLI_Command {
/**
* Update "Old url" in the database with "New Url"
*
* ## OPTIONS
*
* <oldUrl>
* : The old URL
*
* <newUrl>
* : The new URL
*
* <tables>
* : The list of tables we are going to update. `all` does all tables
*
* ## EXAMPLES
*
* wp go-live update https://example.test https://example.com all
*/
function update( $args, $extra ) {
$old_url = trim($args[0]);
$new_url = trim($args[1]);
if ($args[2] != 'all') {
$tables = explode(',', $args[2]);
} else {
$db = Go_Live_Update_Urls_Database::instance();
$tables = $db->get_all_table_names();
}
if ( Go_Live_Update_Urls_Database::instance()->update_the_database( $old_url, $new_url, $tables ) ) {
WP_CLI::success( 'Successfully replaced ' . $old_url . ' with ' . $new_url . ' on ' . join(',',$tables ) . ' tables' );
} else {
WP_CLI::error( 'Error updating the database.' );
}
}
}
WP_CLI::add_command( 'go-live', 'Go_Live_Update_URLS_Pro_CLI' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment