WP_CLI::launch_self PHP Method

launch_self() public static method

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
    public static function launch_self($command, $args = array(), $assoc_args = array(), $exit_on_error = true, $return_detailed = false, $runtime_args = array())
    {
        $reused_runtime_args = array('path', 'url', 'user', 'allow-root');
        foreach ($reused_runtime_args as $key) {
            if (isset($runtime_args[$key])) {
                $assoc_args[$key] = $runtime_args[$key];
            } else {
                if ($value = self::get_runner()->config[$key]) {
                    $assoc_args[$key] = $value;
                }
            }
        }
        $php_bin = self::get_php_binary();
        $script_path = $GLOBALS['argv'][0];
        if (getenv('WP_CLI_CONFIG_PATH')) {
            $config_path = getenv('WP_CLI_CONFIG_PATH');
        } else {
            $config_path = getenv('HOME') . '/.wp-cli/config.yml';
        }
        $config_path = escapeshellarg($config_path);
        $args = implode(' ', array_map('escapeshellarg', $args));
        $assoc_args = \WP_CLI\Utils\assoc_args_to_str($assoc_args);
        $full_command = "WP_CLI_CONFIG_PATH={$config_path} {$php_bin} {$script_path} {$command} {$args} {$assoc_args}";
        return self::launch($full_command, $exit_on_error, $return_detailed);
    }

Usage Example

Esempio n. 1
0
 /**
  * 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.");
 }
All Usage Examples Of WP_CLI::launch_self