Use WP_CLI::runcommand() instead, which is easier to use and works better.
Note: While this command does persist a limited set of runtime arguments,
it *does not* persist environment variables. Practically speaking, WP-CLI
packages won't be loaded when using WP_CLI::launch_self() because the
launched process doesn't have access to the current process $HOME.
public static launch_self ( string $command, array $args = [], array $assoc_args = [], boolean $exit_on_error = true, boolean $return_detailed = false, array $runtime_args = [] ) : integer | ProcessRun | ||
$command | string | WP-CLI command to call. |
$args | array | Positional arguments to include when calling the command. |
$assoc_args | array | Associative arguments to include when calling the command. |
$exit_on_error | boolean | Whether to exit if the command returns an elevated return code. |
$return_detailed | boolean | Whether to return an exit status (default) or detailed execution results. |
$runtime_args | array | Override one or more global args (path,url,user,allow-root) |
return | integer | ProcessRun | The command exit status, or a ProcessRun instance |
/** * Update the permalink structure. * * ## DESCRIPTION * * Updates the post permalink structure. * * To regenerate a .htaccess file with WP-CLI, you'll need to add the mod_rewrite module * to your wp-cli.yml or config.yml. For example: * * `apache_modules: * - mod_rewrite` * * ## OPTIONS * * <permastruct> * : The new permalink structure to apply. * * [--category-base=<base>] * : Set the base for category permalinks, i.e. '/category/'. * * [--tag-base=<base>] * : Set the base for tag permalinks, i.e. '/tag/'. * * [--hard] * : Perform a hard flush - update `.htaccess` rules as well as rewrite rules in database. * * ## EXAMPLES * * wp rewrite structure '/%year%/%monthnum%/%postname%' */ public function structure($args, $assoc_args) { global $wp_rewrite; // copypasta from /wp-admin/options-permalink.php $prefix = $blog_prefix = ''; if (is_multisite() && !is_subdomain_install() && is_main_site()) { $blog_prefix = '/blog'; } $permalink_structure = $args[0] == 'default' ? '' : $args[0]; if (!empty($permalink_structure)) { $permalink_structure = preg_replace('#/+#', '/', '/' . str_replace('#', '', $permalink_structure)); if ($prefix && $blog_prefix) { $permalink_structure = $prefix . preg_replace('#^/?index\\.php#', '', $permalink_structure); } else { $permalink_structure = $blog_prefix . $permalink_structure; } } $wp_rewrite->set_permalink_structure($permalink_structure); // Update category or tag bases if (isset($assoc_args['category-base'])) { $category_base = $assoc_args['category-base']; if (!empty($category_base)) { $category_base = $blog_prefix . preg_replace('#/+#', '/', '/' . str_replace('#', '', $category_base)); } $wp_rewrite->set_category_base($category_base); } if (isset($assoc_args['tag-base'])) { $tag_base = $assoc_args['tag-base']; if (!empty($tag_base)) { $tag_base = $blog_prefix . preg_replace('#/+#', '/', '/' . str_replace('#', '', $tag_base)); } $wp_rewrite->set_tag_base($tag_base); } // make sure we detect mod_rewrite if configured in apache_modules in config self::apache_modules(); // Launch a new process to flush rewrites because core expects flush // to happen after rewrites are set $new_assoc_args = array(); if (\WP_CLI\Utils\get_flag_value($assoc_args, 'hard')) { $new_assoc_args['hard'] = true; if (!in_array('mod_rewrite', (array) WP_CLI::get_config('apache_modules'))) { WP_CLI::warning("Regenerating a .htaccess file requires special configuration. See usage docs."); } } $process_run = WP_CLI::launch_self('rewrite flush', array(), $new_assoc_args, true, true, array('apache_modules', WP_CLI::get_config('apache_modules'))); if (!empty($process_run->stderr)) { // Strip "Warning: " WP_CLI::warning(substr($process_run->stderr, 9)); } WP_CLI::success("Rewrite structure set."); }
/** * Update the permalink structure. * * ## OPTIONS * * <permastruct> * : The new permalink structure to apply. * * [--category-base=<base>] * : Set the base for category permalinks, i.e. '/category/'. * * [--tag-base=<base>] * : Set the base for tag permalinks, i.e. '/tag/'. * * [--hard] * : Perform a hard flush - update `.htaccess` rules as well as rewrite rules in database. * * ## EXAMPLES * * wp rewrite structure '/%year%/%monthnum%/%postname%' */ public function structure($args, $assoc_args) { global $wp_rewrite; // copypasta from /wp-admin/options-permalink.php $home_path = get_home_path(); $iis7_permalinks = iis7_supports_permalinks(); $prefix = $blog_prefix = ''; if (!got_mod_rewrite() && !$iis7_permalinks) { $prefix = '/index.php'; } if (is_multisite() && !is_subdomain_install() && is_main_site()) { $blog_prefix = '/blog'; } $permalink_structure = $args[0] == 'default' ? '' : $args[0]; if (!empty($permalink_structure)) { $permalink_structure = preg_replace('#/+#', '/', '/' . str_replace('#', '', $permalink_structure)); if ($prefix && $blog_prefix) { $permalink_structure = $prefix . preg_replace('#^/?index\\.php#', '', $permalink_structure); } else { $permalink_structure = $blog_prefix . $permalink_structure; } } $wp_rewrite->set_permalink_structure($permalink_structure); // Update category or tag bases if (isset($assoc_args['category-base'])) { $category_base = $assoc_args['category-base']; if (!empty($category_base)) { $category_base = $blog_prefix . preg_replace('#/+#', '/', '/' . str_replace('#', '', $category_base)); } $wp_rewrite->set_category_base($category_base); } if (isset($assoc_args['tag-base'])) { $tag_base = $assoc_args['tag-base']; if (!empty($tag_base)) { $tag_base = $blog_prefix . preg_replace('#/+#', '/', '/' . str_replace('#', '', $tag_base)); } $wp_rewrite->set_tag_base($tag_base); } // make sure we detect mod_rewrite if configured in apache_modules in config self::apache_modules(); // Launch a new process to flush rewrites because core expects flush // to happen after rewrites are set $new_assoc_args = array(); if (isset($assoc_args['hard'])) { $new_assoc_args['hard'] = true; } \WP_CLI::launch_self('rewrite flush', array(), $new_assoc_args); WP_CLI::success("Rewrite structure set."); }
/** * Update the WordPress database. * * [--network] * : Update databases for all sites on a network * * [--dry-run] * : Compare database versions without performing the update. * * @subcommand update-db */ function update_db($_, $assoc_args) { global $wpdb, $wp_db_version, $wp_current_db_version; $network = Utils\get_flag_value($assoc_args, 'network'); if ($network && !is_multisite()) { WP_CLI::error('This is not a multisite install.'); } $dry_run = Utils\get_flag_value($assoc_args, 'dry-run'); if ($dry_run) { WP_CLI::log('Performing a dry run, with no database modification.'); } if ($network) { $iterator_args = array('table' => $wpdb->blogs, 'where' => array('spam' => 0, 'deleted' => 0, 'archived' => 0)); $it = new \WP_CLI\Iterators\Table($iterator_args); $success = $total = 0; foreach ($it as $blog) { $total++; $url = $blog->domain . $blog->path; $process = WP_CLI::launch_self('core update-db', array(), array(), false, true, array('url' => $url, 'dry-run' => $dry_run)); if (0 == $process->return_code) { // See if we can parse the stdout if (preg_match('#Success: (.+)#', $process->stdout, $matches)) { $message = "{$matches[1]} on {$url}"; } else { $message = "Database upgraded successfully on {$url}"; } WP_CLI::log($message); $success++; } else { WP_CLI::warning("Database failed to upgrade on {$url}"); } } if (!$dry_run && $total && $success == $total) { update_site_option('wpmu_upgrade_site', $wp_db_version); } WP_CLI::success(sprintf('WordPress database upgraded on %d/%d sites', $success, $total)); } else { require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $wp_current_db_version = __get_option('db_version'); if ($wp_db_version != $wp_current_db_version) { if (!$dry_run) { wp_upgrade(); } WP_CLI::success("WordPress database upgraded successfully from db version {$wp_current_db_version} to {$wp_db_version}"); } else { WP_CLI::success("WordPress database already at latest db version {$wp_db_version}"); } } }
/** * @param string $command * @param array $args * @param array $assoc_args * @param bool|true $exit_on_error * @param bool|false $return_detailed * @param array $runtime_args * * @return int|\ProcessRun */ public function launch_self($command, $args = array(), $assoc_args = array(), $exit_on_error = true, $return_detailed = false, $runtime_args = array()) { return \WP_CLI::launch_self($command, $args, $assoc_args, $exit_on_error, $return_detailed, $runtime_args); }
/** * Reset the WP Easy Mode plugin and WordPress to default values * * ## OPTIONS * * [--yes] * : Answer yes to the confirmation message. * * ## EXAMPLES * * wp easy-mode reset [--yes] */ public function reset($args, $assoc_args) { global $wpdb; if (!defined('WP_DEBUG') || !WP_DEBUG) { WP_CLI::error('WP_DEBUG must be enabled to reset WP Easy Mode.'); } /** * Confirm */ if (!isset($assoc_args['yes'])) { WP_CLI::confirm('Are you sure you want to reset the plugin? This cannot be undone.'); } /** * Plugins */ WP_CLI::line('Deleting plugin: ninja-forms ...'); WP_CLI::launch_self('plugin deactivate ninja-forms', [], [], false); WP_CLI::launch_self('plugin delete ninja-forms', [], [], false); WP_CLI::line('Deleting plugin: woocommerce ...'); WP_CLI::launch_self('plugin deactivate woocommerce', [], [], false); WP_CLI::launch_self('plugin delete woocommerce', [], [], false); WP_CLI::line('Dropping custom database tables ...'); $mysql = $wpdb->get_results("SELECT GROUP_CONCAT( table_name ) AS query FROM INFORMATION_SCHEMA.TABLES\n\t\t\t\tWHERE ( table_name LIKE '{$wpdb->prefix}nf_%' )\n\t\t\t\t\tOR ( table_name LIKE '{$wpdb->prefix}ninja_forms_%' )\n\t\t\t\t\tOR ( table_name LIKE '{$wpdb->prefix}woocommerce_%' );"); if (isset($mysql[0]->query)) { $tables = implode(',', array_unique(explode(',', $mysql[0]->query))); $wpdb->query("DROP TABLE IF EXISTS {$tables};"); } $wpdb->query("DELETE FROM {$wpdb->options} WHERE ( option_name LIKE 'nf_%' ) OR ( option_name LIKE '%ninja_forms%' ) OR ( option_name LIKE '%woocommerce%' );"); /** * Themes */ WP_CLI::line(sprintf('Activating default theme: %s ...', WP_DEFAULT_THEME)); WP_CLI::launch_self('theme install ' . WP_DEFAULT_THEME . ' --activate', [], [], false); WP_CLI::line('Deleting non-default themes ...'); $inactive = shell_exec('wp theme list --status=inactive --field=name --format=csv'); $inactive = array_filter(explode("\n", $inactive)); $default_themes = array_filter($inactive, function ($theme) { return 'twenty' === substr($theme, 0, 6); }); $inactive = implode("\n", array_diff($inactive, $default_themes)); WP_CLI::launch_self("theme delete {$inactive}", [], [], false); /** * Users */ WP_CLI::line('Removing all users except main admin ...'); $wpdb->query("DELETE FROM {$wpdb->users} WHERE ID > 1"); /** * Settings */ WP_CLI::line('Restoring default settings ...'); $wpdb->query("DELETE FROM {$wpdb->options}\n\t\t\tWHERE ( option_name LIKE 'wpem_%' )\n\t\t\tOR ( option_name LIKE '%_transient_%' )\n\t\t\tOR ( option_name LIKE 'theme_mods_%' );"); update_option('WPLANG', ''); update_option('blogname', 'My Site'); update_option('blogdescription', 'Just another WordPress site'); $wpdb->query("DELETE FROM {$wpdb->usermeta}\n\t\t\tWHERE ( meta_key = 'sk_ignore_notice' )\n\t\t\tOR ( meta_key = 'dismissed_wp_pointers'\n\t\t\tAND meta_value\n\t\t\tLIKE '%wpem_%' );"); WP_CLI::line('Deleting all sidebar widgets ...'); update_option('sidebars_widgets', array('wp_inactive_widgets' => array())); /** * Site content */ WP_CLI::line('Resetting site content ...'); $wpdb->query("TRUNCATE TABLE {$wpdb->posts}"); $wpdb->query("TRUNCATE TABLE {$wpdb->postmeta}"); $wpdb->query("TRUNCATE TABLE {$wpdb->terms}"); $wpdb->query("TRUNCATE TABLE {$wpdb->term_taxonomy}"); $wpdb->query("TRUNCATE TABLE {$wpdb->term_relationships}"); $wpdb->query("TRUNCATE TABLE {$wpdb->termmeta}"); /** * Success */ WP_CLI::success('DONE!'); }
/** * Helper to generate posts before logging into WordPress */ protected function generatePosts() { DEBUG::debug('Generating 5 posts ...'); WP_CLI::launch_self('post generate', [], ['count' => 5], false); }
/** * Update the WordPress database. * * [--network] * : Update databases for all sites on a network * * @subcommand update-db */ function update_db($_, $assoc_args) { global $wpdb, $wp_db_version; $network = Utils\get_flag_value($assoc_args, 'network'); if ($network && !is_multisite()) { WP_CLI::error('This is not a multisite install.'); } if ($network) { $iterator_args = array('table' => $wpdb->blogs, 'where' => array('spam' => 0, 'deleted' => 0, 'archived' => 0)); $it = new \WP_CLI\Iterators\Table($iterator_args); $success = $total = 0; foreach ($it as $blog) { $total++; $url = $blog->domain . $blog->path; $process = WP_CLI::launch_self('core update-db', array(), array(), false, true, array('url' => $url)); if (0 == $process->return_code) { WP_CLI::log("Database upgraded successfully on {$url}"); $success++; } else { WP_CLI::warning("Database failed to upgrade on {$url}"); } } if ($total && $success == $total) { update_site_option('wpmu_upgrade_site', $wp_db_version); } WP_CLI::success(sprintf('WordPress database upgraded on %d/%d sites.', $success, $total)); } else { require_once ABSPATH . 'wp-admin/includes/upgrade.php'; wp_upgrade(); WP_CLI::success('WordPress database upgraded successfully.'); } }
<?php use Codeception\Util\Debug; $plugin_name = 'wp-easy-mode/wp-easy-mode.php'; /** * Activate our plugin if it's not already active. * * Note: Using a function_exists check too because it could * be being loaded by the System Plugin behind the scenes. */ if (!function_exists('wp_easy_mode') && !is_plugin_active($plugin_name)) { activate_plugin($plugin_name); } if (!class_exists('WPEM_CLI')) { WP_CLI::error("WP_DEBUG must be set to TRUE in wp-config.php to run tests!"); } WP_CLI::launch_self('selenium start', [], [], false); add_filter('wpem_self_destruct', '__return_false'); add_filter('wpem_deactivate', '__return_false'); add_filter('wpem_deactivate_plugins_on_quit', function ($plugins) { return array_diff($plugins, [$plugin_name, 'wp-codeception/wp-codeception.php']); });