idx
int64
0
60.3k
question
stringlengths
101
6.21k
target
stringlengths
7
803
1,300
private function query_spec ( $ args , $ operator = 'AND' ) { $ operator = strtoupper ( $ operator ) ; $ count = count ( $ args ) ; $ filtered = array ( ) ; foreach ( $ this -> spec as $ key => $ to_match ) { $ matched = 0 ; foreach ( $ args as $ m_key => $ m_value ) { if ( array_key_exists ( $ m_key , $ to_match ) && $ m_value === $ to_match [ $ m_key ] ) { $ matched ++ ; } } if ( ( 'AND' === $ operator && $ matched === $ count ) || ( 'OR' === $ operator && $ matched > 0 ) || ( 'NOT' === $ operator && 0 === $ matched ) ) { $ filtered [ $ key ] = $ to_match ; } } return $ filtered ; }
Filters a list of associative arrays based on a set of key = > value arguments .
1,301
public function add_namespace ( $ root , $ base_dir , $ prefix = '' , $ suffix = '.php' , $ lowercase = false , $ underscores = false ) { $ this -> namespaces [ ] = array ( 'root' => $ this -> normalize_root ( ( string ) $ root ) , 'base_dir' => $ this -> add_trailing_slash ( ( string ) $ base_dir ) , 'prefix' => ( string ) $ prefix , 'suffix' => ( string ) $ suffix , 'lowercase' => ( bool ) $ lowercase , 'underscores' => ( bool ) $ underscores , ) ; return $ this ; }
Add a specific namespace structure with our custom autoloader .
1,302
public function autoload ( $ class ) { foreach ( $ this -> namespaces as $ namespace ) { if ( 0 !== strpos ( $ class , $ namespace [ 'root' ] ) ) { continue ; } $ filename = str_replace ( array ( $ namespace [ 'root' ] , '\\' ) , array ( '' , DIRECTORY_SEPARATOR ) , $ class ) ; $ filename = $ this -> remove_leading_backslash ( $ filename ) ; if ( $ namespace [ 'lowercase' ] ) { $ filename = strtolower ( $ filename ) ; } if ( $ namespace [ 'underscores' ] ) { $ filename = str_replace ( '_' , '-' , $ filename ) ; } $ filepath = $ namespace [ 'base_dir' ] . $ namespace [ 'prefix' ] . $ filename . $ namespace [ 'suffix' ] ; if ( is_readable ( $ filepath ) ) { require_once $ filepath ; } } }
The autoload function that gets registered with the SPL Autoloader system .
1,303
public function filter_pre_http_request ( $ response , $ args , $ url ) { if ( ! isset ( $ this -> whitelist [ $ url ] ) ) { return $ response ; } if ( 'GET' !== $ args [ 'method' ] || empty ( $ args [ 'filename' ] ) ) { return $ response ; } $ filename = $ this -> cache -> has ( $ this -> whitelist [ $ url ] [ 'key' ] , $ this -> whitelist [ $ url ] [ 'ttl' ] ) ; if ( $ filename ) { WP_CLI :: log ( sprintf ( 'Using cached file \'%s\'...' , $ filename ) ) ; if ( copy ( $ filename , $ args [ 'filename' ] ) ) { return array ( 'response' => array ( 'code' => 200 , 'message' => 'OK' , ) , 'filename' => $ args [ 'filename' ] , ) ; } WP_CLI :: error ( sprintf ( 'Error copying cached file %s to %s' , $ filename , $ url ) ) ; } return $ response ; }
short circuit wp http api with cached file
1,304
public function filter_http_response ( $ response , $ args , $ url ) { if ( ! isset ( $ this -> whitelist [ $ url ] ) ) { return $ response ; } if ( 'GET' !== $ args [ 'method' ] || empty ( $ args [ 'filename' ] ) ) { return $ response ; } if ( is_wp_error ( $ response ) || 200 !== wp_remote_retrieve_response_code ( $ response ) ) { return $ response ; } $ this -> cache -> import ( $ this -> whitelist [ $ url ] [ 'key' ] , $ response [ 'filename' ] ) ; return $ response ; }
cache wp http api downloads
1,305
public function whitelist_package ( $ url , $ group , $ slug , $ version , $ ttl = null ) { $ ext = pathinfo ( Utils \ parse_url ( $ url , PHP_URL_PATH ) , PATHINFO_EXTENSION ) ; $ key = "$group/$slug-$version.$ext" ; $ this -> whitelist_url ( $ url , $ key , $ ttl ) ; wp_update_plugins ( ) ; }
whitelist a package url
1,306
public function whitelist_url ( $ url , $ key = null , $ ttl = null ) { $ key = $ key ? : $ url ; $ this -> whitelist [ $ url ] = compact ( 'key' , 'ttl' ) ; }
whitelist a url
1,307
protected function get_custom_vendor_folder ( ) { $ maybe_composer_json = WP_CLI_ROOT . '/../../../composer.json' ; if ( ! is_readable ( $ maybe_composer_json ) ) { return false ; } $ composer = json_decode ( file_get_contents ( $ maybe_composer_json ) ) ; if ( ! empty ( $ composer -> config ) && ! empty ( $ composer -> config -> { 'vendor-dir' } ) ) { return $ composer -> config -> { 'vendor-dir' } ; } return false ; }
Get the name of the custom vendor folder as set in composer . json .
1,308
private function get_parameters ( $ spec = array ( ) ) { $ local_parameters = array_column ( $ spec , 'name' ) ; $ global_parameters = array_column ( WP_CLI \ SynopsisParser :: parse ( $ this -> get_global_params ( ) ) , 'name' ) ; return array_unique ( array_merge ( $ local_parameters , $ global_parameters ) ) ; }
Get an array of parameter names by merging the command - specific and the global parameters .
1,309
public function register_early_invoke ( $ when , $ command ) { $ this -> early_invoke [ $ when ] [ ] = array_slice ( Dispatcher \ get_path ( $ command ) , 1 ) ; }
Register a command for early invocation generally before WordPress loads .
1,310
private function do_early_invoke ( $ when ) { if ( ! isset ( $ this -> early_invoke [ $ when ] ) ) { return ; } $ real_when = '' ; $ r = $ this -> find_command_to_run ( $ this -> arguments ) ; if ( is_array ( $ r ) ) { list ( $ command , $ final_args , $ cmd_path ) = $ r ; foreach ( $ this -> early_invoke as $ _when => $ _path ) { foreach ( $ _path as $ cmd ) { if ( $ cmd === $ cmd_path ) { $ real_when = $ _when ; } } } } foreach ( $ this -> early_invoke [ $ when ] as $ path ) { if ( $ this -> cmd_starts_with ( $ path ) ) { if ( empty ( $ real_when ) || ( $ real_when && $ real_when === $ when ) ) { $ this -> run_command_and_exit ( ) ; } } } }
Perform the early invocation of a command .
1,311
public function get_global_config_path ( ) { if ( getenv ( 'WP_CLI_CONFIG_PATH' ) ) { $ config_path = getenv ( 'WP_CLI_CONFIG_PATH' ) ; $ this -> global_config_path_debug = 'Using global config from WP_CLI_CONFIG_PATH env var: ' . $ config_path ; } else { $ config_path = Utils \ get_home_dir ( ) . '/.wp-cli/config.yml' ; $ this -> global_config_path_debug = 'Using default global config: ' . $ config_path ; } if ( is_readable ( $ config_path ) ) { return $ config_path ; } $ this -> global_config_path_debug = 'No readable global config found' ; return false ; }
Get the path to the global configuration YAML file .
1,312
public function get_project_config_path ( ) { $ config_files = array ( 'wp-cli.local.yml' , 'wp-cli.yml' , ) ; $ project_config_path = Utils \ find_file_upward ( $ config_files , getcwd ( ) , function ( $ dir ) { static $ wp_load_count = 0 ; $ wp_load_path = $ dir . DIRECTORY_SEPARATOR . 'wp-load.php' ; if ( file_exists ( $ wp_load_path ) ) { ++ $ wp_load_count ; } return $ wp_load_count > 1 ; } ) ; $ this -> project_config_path_debug = 'No project config found' ; if ( ! empty ( $ project_config_path ) ) { $ this -> project_config_path_debug = 'Using project config: ' . $ project_config_path ; } return $ project_config_path ; }
Get the path to the project - specific configuration YAML file . wp - cli . local . yml takes priority over wp - cli . yml .
1,313
public function get_packages_dir_path ( ) { if ( getenv ( 'WP_CLI_PACKAGES_DIR' ) ) { $ packages_dir = Utils \ trailingslashit ( getenv ( 'WP_CLI_PACKAGES_DIR' ) ) ; } else { $ packages_dir = Utils \ get_home_dir ( ) . '/.wp-cli/packages/' ; } return $ packages_dir ; }
Get the path to the packages directory
1,314
private static function extract_subdir_path ( $ index_path ) { $ index_code = file_get_contents ( $ index_path ) ; if ( ! preg_match ( '|^\s*require\s*\(?\s*(.+?)/wp-blog-header\.php([\'"])|m' , $ index_code , $ matches ) ) { return false ; } $ wp_path_src = $ matches [ 1 ] . $ matches [ 2 ] ; $ wp_path_src = Utils \ replace_path_consts ( $ wp_path_src , $ index_path ) ; $ wp_path = eval ( "return $wp_path_src;" ) ; if ( ! Utils \ is_path_absolute ( $ wp_path ) ) { $ wp_path = dirname ( $ index_path ) . "/$wp_path" ; } return $ wp_path ; }
Attempts to find the path to the WP installation inside index . php
1,315
private function find_wp_root ( ) { if ( ! empty ( $ this -> config [ 'path' ] ) ) { $ path = $ this -> config [ 'path' ] ; if ( ! Utils \ is_path_absolute ( $ path ) ) { $ path = getcwd ( ) . '/' . $ path ; } return $ path ; } if ( $ this -> cmd_starts_with ( array ( 'core' , 'download' ) ) ) { return getcwd ( ) ; } $ dir = getcwd ( ) ; while ( is_readable ( $ dir ) ) { if ( file_exists ( "$dir/wp-load.php" ) ) { return $ dir ; } if ( file_exists ( "$dir/index.php" ) ) { $ path = self :: extract_subdir_path ( "$dir/index.php" ) ; if ( ! empty ( $ path ) ) { return $ path ; } } $ parent_dir = dirname ( $ dir ) ; if ( empty ( $ parent_dir ) || $ parent_dir === $ dir ) { break ; } $ dir = $ parent_dir ; } }
Find the directory that contains the WordPress files . Defaults to the current working dir .
1,316
private static function set_wp_root ( $ path ) { if ( ! defined ( 'ABSPATH' ) ) { define ( 'ABSPATH' , Utils \ normalize_path ( Utils \ trailingslashit ( $ path ) ) ) ; } elseif ( ! is_null ( $ path ) ) { WP_CLI :: error_multi_line ( array ( 'The --path parameter cannot be used when ABSPATH is already defined elsewhere' , 'ABSPATH is defined as: "' . ABSPATH . '"' , ) ) ; } WP_CLI :: debug ( 'ABSPATH defined: ' . ABSPATH , 'bootstrap' ) ; $ _SERVER [ 'DOCUMENT_ROOT' ] = realpath ( $ path ) ; }
Set WordPress root as a given path .
1,317
private static function guess_url ( $ assoc_args ) { if ( isset ( $ assoc_args [ 'blog' ] ) ) { $ assoc_args [ 'url' ] = $ assoc_args [ 'blog' ] ; } if ( isset ( $ assoc_args [ 'url' ] ) ) { $ url = $ assoc_args [ 'url' ] ; if ( true === $ url ) { WP_CLI :: warning ( 'The --url parameter expects a value.' ) ; } } if ( isset ( $ url ) ) { return $ url ; } return false ; }
Guess which URL context WP - CLI has been invoked under .
1,318
public function find_command_to_run ( $ args ) { $ command = \ WP_CLI :: get_root_command ( ) ; WP_CLI :: do_hook ( 'find_command_to_run_pre' ) ; $ cmd_path = array ( ) ; while ( ! empty ( $ args ) && $ command -> can_have_subcommands ( ) ) { $ cmd_path [ ] = $ args [ 0 ] ; $ full_name = implode ( ' ' , $ cmd_path ) ; $ subcommand = $ command -> find_subcommand ( $ args ) ; if ( ! $ subcommand ) { if ( count ( $ cmd_path ) > 1 ) { $ child = array_pop ( $ cmd_path ) ; $ parent_name = implode ( ' ' , $ cmd_path ) ; $ suggestion = $ this -> get_subcommand_suggestion ( $ child , $ command ) ; return sprintf ( "'%s' is not a registered subcommand of '%s'. See 'wp help %s' for available subcommands.%s" , $ child , $ parent_name , $ parent_name , ! empty ( $ suggestion ) ? PHP_EOL . "Did you mean '{$suggestion}'?" : '' ) ; } $ suggestion = $ this -> get_subcommand_suggestion ( $ full_name , $ command ) ; return sprintf ( "'%s' is not a registered wp command. See 'wp help' for available commands.%s" , $ full_name , ! empty ( $ suggestion ) ? PHP_EOL . "Did you mean '{$suggestion}'?" : '' ) ; } if ( $ this -> is_command_disabled ( $ subcommand ) ) { return sprintf ( "The '%s' command has been disabled from the config file." , $ full_name ) ; } $ command = $ subcommand ; } return array ( $ command , $ args , $ cmd_path ) ; }
Given positional arguments find the command to execute .
1,319
public function run_command ( $ args , $ assoc_args = array ( ) , $ options = array ( ) ) { WP_CLI :: do_hook ( 'before_run_command' ) ; if ( ! empty ( $ options [ 'back_compat_conversions' ] ) ) { list ( $ args , $ assoc_args ) = self :: back_compat_conversions ( $ args , $ assoc_args ) ; } $ r = $ this -> find_command_to_run ( $ args ) ; if ( is_string ( $ r ) ) { WP_CLI :: error ( $ r ) ; } list ( $ command , $ final_args , $ cmd_path ) = $ r ; $ name = implode ( ' ' , $ cmd_path ) ; $ extra_args = array ( ) ; if ( isset ( $ this -> extra_config [ $ name ] ) ) { $ extra_args = $ this -> extra_config [ $ name ] ; } WP_CLI :: debug ( 'Running command: ' . $ name , 'bootstrap' ) ; try { $ command -> invoke ( $ final_args , $ assoc_args , $ extra_args ) ; } catch ( WP_CLI \ Iterators \ Exception $ e ) { WP_CLI :: error ( $ e -> getMessage ( ) ) ; } }
Find the WP - CLI command to run given arguments and invoke it .
1,320
public function show_synopsis_if_composite_command ( ) { $ r = $ this -> find_command_to_run ( $ this -> arguments ) ; if ( is_array ( $ r ) ) { list ( $ command ) = $ r ; if ( $ command -> can_have_subcommands ( ) ) { $ command -> show_usage ( ) ; exit ; } } }
Show synopsis if the called command is a composite command
1,321
private function generate_ssh_command ( $ bits , $ wp_command ) { $ escaped_command = '' ; foreach ( array ( 'scheme' , 'user' , 'host' , 'port' , 'path' ) as $ bit ) { if ( ! isset ( $ bits [ $ bit ] ) ) { $ bits [ $ bit ] = null ; } WP_CLI :: debug ( 'SSH ' . $ bit . ': ' . $ bits [ $ bit ] , 'bootstrap' ) ; } $ is_tty = function_exists ( 'posix_isatty' ) && posix_isatty ( STDOUT ) ; if ( 'docker' === $ bits [ 'scheme' ] ) { $ command = 'docker exec %s%s%s sh -c %s' ; $ escaped_command = sprintf ( $ command , $ bits [ 'user' ] ? '--user ' . escapeshellarg ( $ bits [ 'user' ] ) . ' ' : '' , $ is_tty ? '-t ' : '' , escapeshellarg ( $ bits [ 'host' ] ) , escapeshellarg ( $ wp_command ) ) ; } if ( 'docker-compose' === $ bits [ 'scheme' ] ) { $ command = 'docker-compose exec %s%s%s sh -c %s' ; $ escaped_command = sprintf ( $ command , $ bits [ 'user' ] ? '--user ' . escapeshellarg ( $ bits [ 'user' ] ) . ' ' : '' , $ is_tty ? '' : '-T ' , escapeshellarg ( $ bits [ 'host' ] ) , escapeshellarg ( $ wp_command ) ) ; } if ( 'vagrant' === $ bits [ 'scheme' ] ) { $ command = 'vagrant ssh -c %s %s' ; $ escaped_command = sprintf ( $ command , escapeshellarg ( $ wp_command ) , escapeshellarg ( $ bits [ 'host' ] ) ) ; } if ( 'ssh' === $ bits [ 'scheme' ] || null === $ bits [ 'scheme' ] ) { $ command = 'ssh -q %s%s %s %s' ; if ( $ bits [ 'user' ] ) { $ bits [ 'host' ] = $ bits [ 'user' ] . '@' . $ bits [ 'host' ] ; } $ escaped_command = sprintf ( $ command , $ bits [ 'port' ] ? '-p ' . ( int ) $ bits [ 'port' ] . ' ' : '' , escapeshellarg ( $ bits [ 'host' ] ) , $ is_tty ? '-t' : '-T' , escapeshellarg ( $ wp_command ) ) ; } WP_CLI :: debug ( 'Running SSH command: ' . $ escaped_command , 'bootstrap' ) ; return $ escaped_command ; }
Generate a shell command from the parsed connection string .
1,322
public function is_command_disabled ( $ command ) { $ path = implode ( ' ' , array_slice ( \ WP_CLI \ Dispatcher \ get_path ( $ command ) , 1 ) ) ; return in_array ( $ path , $ this -> config [ 'disabled_commands' ] , true ) ; }
Check whether a given command is disabled by the config
1,323
public function get_wp_config_code ( ) { $ wp_config_path = Utils \ locate_wp_config ( ) ; $ wp_config_code = explode ( "\n" , file_get_contents ( $ wp_config_path ) ) ; $ found_wp_settings = false ; $ lines_to_run = array ( ) ; foreach ( $ wp_config_code as $ line ) { if ( preg_match ( '/^\s*require.+wp-settings\.php/' , $ line ) ) { $ found_wp_settings = true ; continue ; } $ lines_to_run [ ] = $ line ; } if ( ! $ found_wp_settings ) { WP_CLI :: error ( 'Strange wp-config.php file: wp-settings.php is not loaded directly.' ) ; } $ source = implode ( "\n" , $ lines_to_run ) ; $ source = Utils \ replace_path_consts ( $ source , $ wp_config_path ) ; return preg_replace ( '|^\s*\<\?php\s*|' , '' , $ source ) ; }
Returns wp - config . php code skipping the loading of wp - settings . php
1,324
public function load_wordpress ( ) { static $ wp_cli_is_loaded ; global $ site_id , $ wpdb , $ public , $ current_site , $ current_blog , $ path , $ shortcode_tags ; if ( ! empty ( $ wp_cli_is_loaded ) ) { return ; } $ wp_cli_is_loaded = true ; WP_CLI :: debug ( 'Begin WordPress load' , 'bootstrap' ) ; WP_CLI :: do_hook ( 'before_wp_load' ) ; $ this -> check_wp_version ( ) ; $ wp_config_path = Utils \ locate_wp_config ( ) ; if ( ! $ wp_config_path ) { WP_CLI :: error ( "'wp-config.php' not found.\n" . 'Either create one manually or use `wp config create`.' ) ; } WP_CLI :: debug ( 'wp-config.php path: ' . $ wp_config_path , 'bootstrap' ) ; WP_CLI :: do_hook ( 'before_wp_config_load' ) ; $ wp_cli_original_defined_vars = get_defined_vars ( ) ; eval ( $ this -> get_wp_config_code ( ) ) ; foreach ( get_defined_vars ( ) as $ key => $ var ) { if ( array_key_exists ( $ key , $ wp_cli_original_defined_vars ) || 'wp_cli_original_defined_vars' === $ key ) { continue ; } global $ { $ key } ; $ { $ key } = $ var ; } $ this -> maybe_update_url_from_domain_constant ( ) ; WP_CLI :: do_hook ( 'after_wp_config_load' ) ; $ this -> do_early_invoke ( 'after_wp_config_load' ) ; if ( $ this -> cmd_starts_with ( array ( 'core' , 'is-installed' ) ) && ! defined ( 'COOKIEHASH' ) ) { define ( 'COOKIEHASH' , md5 ( 'wp-cli' ) ) ; } require WP_CLI_ROOT . '/php/utils-wp.php' ; $ this -> setup_bootstrap_hooks ( ) ; if ( Utils \ wp_version_compare ( '4.6-alpha-37575' , '>=' ) ) { if ( $ this -> cmd_starts_with ( array ( 'help' ) ) ) { if ( ! defined ( 'WP_DEBUG' ) ) { define ( 'WP_DEBUG' , true ) ; } if ( ! defined ( 'WP_DEBUG_DISPLAY' ) ) { define ( 'WP_DEBUG_DISPLAY' , true ) ; } } require ABSPATH . 'wp-settings.php' ; } else { require WP_CLI_ROOT . '/php/wp-settings-cli.php' ; } ini_set ( 'memory_limit' , - 1 ) ; require ABSPATH . 'wp-admin/includes/admin.php' ; add_filter ( 'filesystem_method' , function ( ) { return 'direct' ; } , 99 ) ; if ( getenv ( 'BEHAT_RUN' ) ) { $ this -> enable_error_reporting ( ) ; } WP_CLI :: debug ( 'Loaded WordPress' , 'bootstrap' ) ; WP_CLI :: do_hook ( 'after_wp_load' ) ; }
Load WordPress if it hasn t already been loaded
1,325
private function setup_skip_plugins_filters ( ) { $ wp_cli_filter_active_plugins = function ( $ plugins ) { $ skipped_plugins = WP_CLI :: get_runner ( ) -> config [ 'skip-plugins' ] ; if ( true === $ skipped_plugins ) { return array ( ) ; } if ( ! is_array ( $ plugins ) ) { return $ plugins ; } foreach ( $ plugins as $ a => $ b ) { if ( false !== strpos ( current_filter ( ) , 'active_sitewide_plugins' ) && Utils \ is_plugin_skipped ( $ a ) ) { unset ( $ plugins [ $ a ] ) ; } elseif ( false !== strpos ( current_filter ( ) , 'active_plugins' ) && Utils \ is_plugin_skipped ( $ b ) ) { unset ( $ plugins [ $ a ] ) ; } } if ( false !== strpos ( current_filter ( ) , 'active_plugins' ) ) { $ plugins = array_values ( $ plugins ) ; } return $ plugins ; } ; $ hooks = array ( 'pre_site_option_active_sitewide_plugins' , 'site_option_active_sitewide_plugins' , 'pre_option_active_plugins' , 'option_active_plugins' , ) ; foreach ( $ hooks as $ hook ) { WP_CLI :: add_wp_hook ( $ hook , $ wp_cli_filter_active_plugins , 999 ) ; } WP_CLI :: add_wp_hook ( 'plugins_loaded' , function ( ) use ( $ hooks , $ wp_cli_filter_active_plugins ) { foreach ( $ hooks as $ hook ) { remove_filter ( $ hook , $ wp_cli_filter_active_plugins , 999 ) ; } } , 0 ) ; }
Set up the filters to skip the loaded plugins
1,326
public function action_setup_theme_wp_cli_skip_themes ( ) { $ wp_cli_filter_active_theme = function ( $ value ) { $ skipped_themes = WP_CLI :: get_runner ( ) -> config [ 'skip-themes' ] ; if ( true === $ skipped_themes ) { return '' ; } if ( ! is_array ( $ skipped_themes ) ) { $ skipped_themes = explode ( ',' , $ skipped_themes ) ; } $ checked_value = $ value ; if ( false !== stripos ( current_filter ( ) , 'option_template' ) ) { $ checked_value = get_option ( 'stylesheet' ) ; } if ( '' === $ checked_value || in_array ( $ checked_value , $ skipped_themes , true ) ) { return '' ; } return $ value ; } ; $ hooks = array ( 'pre_option_template' , 'option_template' , 'pre_option_stylesheet' , 'option_stylesheet' , ) ; foreach ( $ hooks as $ hook ) { add_filter ( $ hook , $ wp_cli_filter_active_theme , 999 ) ; } WP_CLI :: add_wp_hook ( 'after_setup_theme' , function ( ) use ( $ hooks , $ wp_cli_filter_active_theme ) { foreach ( $ hooks as $ hook ) { remove_filter ( $ hook , $ wp_cli_filter_active_theme , 999 ) ; } } , 0 ) ; }
Set up the filters to skip the loaded theme
1,327
private function auto_check_update ( ) { if ( ! Utils \ inside_phar ( ) ) { return ; } $ existing_phar = realpath ( $ _SERVER [ 'argv' ] [ 0 ] ) ; if ( ! is_writable ( $ existing_phar ) || ! is_writable ( dirname ( $ existing_phar ) ) ) { return ; } if ( ! function_exists ( 'posix_isatty' ) || ! posix_isatty ( STDOUT ) ) { return ; } if ( getenv ( 'WP_CLI_DISABLE_AUTO_CHECK_UPDATE' ) ) { return ; } $ days_between_checks = getenv ( 'WP_CLI_AUTO_CHECK_UPDATE_DAYS' ) ; if ( false === $ days_between_checks ) { $ days_between_checks = 1 ; } $ cache = WP_CLI :: get_cache ( ) ; $ cache_key = 'wp-cli-update-check' ; if ( ! $ cache -> has ( $ cache_key ) ) { $ cache -> write ( $ cache_key , time ( ) ) ; return ; } $ last_check = ( int ) $ cache -> read ( $ cache_key ) ; if ( ( time ( ) - ( 24 * 60 * 60 * $ days_between_checks ) ) < $ last_check ) { return ; } $ cache -> write ( $ cache_key , time ( ) ) ; ob_start ( ) ; WP_CLI :: run_command ( array ( 'cli' , 'check-update' ) , array ( 'format' => 'count' , ) ) ; $ count = ob_get_clean ( ) ; if ( ! $ count ) { return ; } WP_CLI :: run_command ( array ( 'cli' , 'update' ) ) ; exit ; }
Check whether there s a WP - CLI update available and suggest update if so .
1,328
private function enumerate_commands ( CompositeCommand $ command , array & $ list , $ parent = '' ) { foreach ( $ command -> get_subcommands ( ) as $ subcommand ) { $ command_string = empty ( $ parent ) ? $ subcommand -> get_name ( ) : "{$parent} {$subcommand->get_name()}" ; $ list [ ] = $ command_string ; $ this -> enumerate_commands ( $ subcommand , $ list , $ command_string ) ; } }
Recursive method to enumerate all known commands .
1,329
public static function extract ( $ tarball_or_zip , $ dest ) { if ( preg_match ( '/\.zip$/' , $ tarball_or_zip ) ) { return self :: extract_zip ( $ tarball_or_zip , $ dest ) ; } if ( preg_match ( '/\.tar\.gz$/' , $ tarball_or_zip ) ) { return self :: extract_tarball ( $ tarball_or_zip , $ dest ) ; } throw new \ Exception ( "Extraction only supported for '.zip' and '.tar.gz' file types." ) ; }
Extract the archive file to a specific destination .
1,330
private static function extract_zip ( $ zipfile , $ dest ) { if ( ! class_exists ( 'ZipArchive' ) ) { throw new \ Exception ( 'Extracting a zip file requires ZipArchive.' ) ; } $ zip = new ZipArchive ( ) ; $ res = $ zip -> open ( $ zipfile ) ; if ( true === $ res ) { $ tempdir = implode ( DIRECTORY_SEPARATOR , array ( dirname ( $ zipfile ) , Utils \ basename ( $ zipfile , '.zip' ) , $ zip -> getNameIndex ( 0 ) , ) ) ; $ zip -> extractTo ( dirname ( $ tempdir ) ) ; $ zip -> close ( ) ; self :: copy_overwrite_files ( $ tempdir , $ dest ) ; self :: rmdir ( dirname ( $ tempdir ) ) ; } else { throw new \ Exception ( sprintf ( "ZipArchive failed to unzip '%s': %s." , $ zipfile , self :: zip_error_msg ( $ res ) ) ) ; } }
Extract a ZIP file to a specific destination .
1,331
private static function extract_tarball ( $ tarball , $ dest ) { if ( class_exists ( 'PharData' ) ) { try { $ phar = new PharData ( $ tarball ) ; $ tempdir = implode ( DIRECTORY_SEPARATOR , array ( dirname ( $ tarball ) , Utils \ basename ( $ tarball , '.tar.gz' ) , $ phar -> getFilename ( ) , ) ) ; $ phar -> extractTo ( dirname ( $ tempdir ) , null , true ) ; self :: copy_overwrite_files ( $ tempdir , $ dest ) ; self :: rmdir ( dirname ( $ tempdir ) ) ; return ; } catch ( \ Exception $ e ) { WP_CLI :: warning ( "PharData failed, falling back to 'tar xz' (" . $ e -> getMessage ( ) . ')' ) ; } } $ cmd = Utils \ esc_cmd ( 'tar xz --strip-components=1 --directory=%s -f %s' , $ dest , $ tarball ) ; $ process_run = WP_CLI :: launch ( $ cmd , false , true ) ; if ( 0 !== $ process_run -> return_code ) { throw new \ Exception ( sprintf ( 'Failed to execute `%s`: %s.' , $ cmd , self :: tar_error_msg ( $ process_run ) ) ) ; } }
Extract a tarball to a specific destination .
1,332
public static function copy_overwrite_files ( $ source , $ dest ) { $ iterator = new RecursiveIteratorIterator ( new RecursiveDirectoryIterator ( $ source , RecursiveDirectoryIterator :: SKIP_DOTS ) , RecursiveIteratorIterator :: SELF_FIRST ) ; $ error = 0 ; if ( ! is_dir ( $ dest ) ) { mkdir ( $ dest , 0777 , true ) ; } foreach ( $ iterator as $ item ) { $ dest_path = $ dest . DIRECTORY_SEPARATOR . $ iterator -> getSubPathName ( ) ; if ( $ item -> isDir ( ) ) { if ( ! is_dir ( $ dest_path ) ) { mkdir ( $ dest_path ) ; } } else { if ( file_exists ( $ dest_path ) && is_writable ( $ dest_path ) ) { copy ( $ item , $ dest_path ) ; } elseif ( ! file_exists ( $ dest_path ) ) { copy ( $ item , $ dest_path ) ; } else { $ error = 1 ; WP_CLI :: warning ( "Unable to copy '" . $ iterator -> getSubPathName ( ) . "' to current directory." ) ; } } } if ( $ error ) { throw new \ Exception ( 'There was an error overwriting existing files.' ) ; } }
Copy files from source directory to destination directory . Source directory must exist .
1,333
public static function rmdir ( $ dir ) { $ files = new RecursiveIteratorIterator ( new RecursiveDirectoryIterator ( $ dir , RecursiveDirectoryIterator :: SKIP_DOTS ) , RecursiveIteratorIterator :: CHILD_FIRST ) ; foreach ( $ files as $ fileinfo ) { $ todo = $ fileinfo -> isDir ( ) ? 'rmdir' : 'unlink' ; $ todo ( $ fileinfo -> getRealPath ( ) ) ; } rmdir ( $ dir ) ; }
Delete all files and directories recursively from directory . Directory must exist .
1,334
public static function zip_error_msg ( $ error_code ) { static $ zip_err_msgs = array ( ZipArchive :: ER_OK => 'No error' , ZipArchive :: ER_MULTIDISK => 'Multi-disk zip archives not supported' , ZipArchive :: ER_RENAME => 'Renaming temporary file failed' , ZipArchive :: ER_CLOSE => 'Closing zip archive failed' , ZipArchive :: ER_SEEK => 'Seek error' , ZipArchive :: ER_READ => 'Read error' , ZipArchive :: ER_WRITE => 'Write error' , ZipArchive :: ER_CRC => 'CRC error' , ZipArchive :: ER_ZIPCLOSED => 'Containing zip archive was closed' , ZipArchive :: ER_NOENT => 'No such file' , ZipArchive :: ER_EXISTS => 'File already exists' , ZipArchive :: ER_OPEN => 'Can\'t open file' , ZipArchive :: ER_TMPOPEN => 'Failure to create temporary file' , ZipArchive :: ER_ZLIB => 'Zlib error' , ZipArchive :: ER_MEMORY => 'Malloc failure' , ZipArchive :: ER_CHANGED => 'Entry has been changed' , ZipArchive :: ER_COMPNOTSUPP => 'Compression method not supported' , ZipArchive :: ER_EOF => 'Premature EOF' , ZipArchive :: ER_INVAL => 'Invalid argument' , ZipArchive :: ER_NOZIP => 'Not a zip archive' , ZipArchive :: ER_INTERNAL => 'Internal error' , ZipArchive :: ER_INCONS => 'Zip archive inconsistent' , ZipArchive :: ER_REMOVE => 'Can\'t remove file' , ZipArchive :: ER_DELETED => 'Entry has been deleted' , ) ; if ( isset ( $ zip_err_msgs [ $ error_code ] ) ) { return sprintf ( '%s (%d)' , $ zip_err_msgs [ $ error_code ] , $ error_code ) ; } return $ error_code ; }
Return formatted ZipArchive error message from error code .
1,335
public static function tar_error_msg ( $ process_run ) { $ stderr = trim ( $ process_run -> stderr ) ; $ nl_pos = strpos ( $ stderr , "\n" ) ; if ( false !== $ nl_pos ) { $ stderr = trim ( substr ( $ stderr , 0 , $ nl_pos ) ) ; } if ( $ stderr ) { return sprintf ( '%s (%d)' , $ stderr , $ process_run -> return_code ) ; } return $ process_run -> return_code ; }
Return formatted error message from ProcessRun of tar command .
1,336
protected function _line ( $ message , $ label , $ color , $ handle = STDOUT ) { if ( class_exists ( 'cli\Colors' ) ) { $ label = \ cli \ Colors :: colorize ( "$color$label:%n" , $ this -> in_color ) ; } else { $ label = "$label:" ; } $ this -> write ( $ handle , "$label $message\n" ) ; }
Output one line of message to a resource .
1,337
private function declare_loggers ( ) { $ logger_dir = WP_CLI_ROOT . '/php/WP_CLI/Loggers' ; $ iterator = new \ DirectoryIterator ( $ logger_dir ) ; include_once "$logger_dir/Base.php" ; foreach ( $ iterator as $ filename ) { if ( '.php' !== substr ( $ filename , - 4 ) ) { continue ; } include_once "$logger_dir/$filename" ; } }
Load the class declarations for the loggers .
1,338
public function cache_clear ( ) { $ cache = WP_CLI :: get_cache ( ) ; if ( ! $ cache -> is_enabled ( ) ) { WP_CLI :: error ( 'Cache directory does not exist.' ) ; } $ cache -> clear ( ) ; WP_CLI :: success ( 'Cache cleared.' ) ; }
Clear the internal cache .
1,339
public function cache_prune ( ) { $ cache = WP_CLI :: get_cache ( ) ; if ( ! $ cache -> is_enabled ( ) ) { WP_CLI :: error ( 'Cache directory does not exist.' ) ; } $ cache -> prune ( ) ; WP_CLI :: success ( 'Cache pruned.' ) ; }
Prune the internal cache .
1,340
public function find_subcommand ( & $ args ) { $ command = array_shift ( $ args ) ; Utils \ load_command ( $ command ) ; if ( ! isset ( $ this -> subcommands [ $ command ] ) ) { return false ; } return $ this -> subcommands [ $ command ] ; }
Find a subcommand registered on the root command .
1,341
private static function create_subcommand ( $ parent , $ name , $ callable , $ reflection ) { $ doc_comment = self :: get_doc_comment ( $ reflection ) ; $ docparser = new \ WP_CLI \ DocParser ( $ doc_comment ) ; if ( is_array ( $ callable ) ) { if ( ! $ name ) { $ name = $ docparser -> get_tag ( 'subcommand' ) ; } if ( ! $ name ) { $ name = $ reflection -> name ; } } if ( ! $ doc_comment ) { \ WP_CLI :: debug ( null === $ doc_comment ? "Failed to get doc comment for {$name}." : "No doc comment for {$name}." , 'commandfactory' ) ; } $ when_invoked = function ( $ args , $ assoc_args ) use ( $ callable ) { if ( is_array ( $ callable ) ) { $ callable [ 0 ] = is_object ( $ callable [ 0 ] ) ? $ callable [ 0 ] : new $ callable [ 0 ] ( ) ; call_user_func ( array ( $ callable [ 0 ] , $ callable [ 1 ] ) , $ args , $ assoc_args ) ; } else { call_user_func ( $ callable , $ args , $ assoc_args ) ; } } ; return new Subcommand ( $ parent , $ name , $ docparser , $ when_invoked ) ; }
Create a new Subcommand instance .
1,342
private static function create_composite_command ( $ parent , $ name , $ callable ) { $ reflection = new ReflectionClass ( $ callable ) ; $ doc_comment = self :: get_doc_comment ( $ reflection ) ; if ( ! $ doc_comment ) { \ WP_CLI :: debug ( null === $ doc_comment ? "Failed to get doc comment for {$name}." : "No doc comment for {$name}." , 'commandfactory' ) ; } $ docparser = new \ WP_CLI \ DocParser ( $ doc_comment ) ; $ container = new CompositeCommand ( $ parent , $ name , $ docparser ) ; foreach ( $ reflection -> getMethods ( ) as $ method ) { if ( ! self :: is_good_method ( $ method ) ) { continue ; } $ class = is_object ( $ callable ) ? $ callable : $ reflection -> name ; $ subcommand = self :: create_subcommand ( $ container , false , array ( $ class , $ method -> name ) , $ method ) ; $ subcommand_name = $ subcommand -> get_name ( ) ; $ container -> add_subcommand ( $ subcommand_name , $ subcommand ) ; } return $ container ; }
Create a new Composite command instance .
1,343
private static function create_namespace ( $ parent , $ name , $ callable ) { $ reflection = new ReflectionClass ( $ callable ) ; $ doc_comment = self :: get_doc_comment ( $ reflection ) ; if ( ! $ doc_comment ) { \ WP_CLI :: debug ( null === $ doc_comment ? "Failed to get doc comment for {$name}." : "No doc comment for {$name}." , 'commandfactory' ) ; } $ docparser = new \ WP_CLI \ DocParser ( $ doc_comment ) ; return new CommandNamespace ( $ parent , $ name , $ docparser ) ; }
Create a new command namespace instance .
1,344
private static function get_doc_comment ( $ reflection ) { $ doc_comment = $ reflection -> getDocComment ( ) ; if ( false !== $ doc_comment || ! ( ini_get ( 'opcache.enable_cli' ) && ! ini_get ( 'opcache.save_comments' ) ) ) { if ( ! getenv ( 'WP_CLI_TEST_GET_DOC_COMMENT' ) ) { return $ doc_comment ; } } $ filename = $ reflection -> getFileName ( ) ; if ( isset ( self :: $ file_contents [ $ filename ] ) ) { $ contents = self :: $ file_contents [ $ filename ] ; } elseif ( is_readable ( $ filename ) ) { $ contents = file_get_contents ( $ filename ) ; if ( is_string ( $ contents ) && '' !== $ contents ) { $ contents = explode ( "\n" , $ contents ) ; self :: $ file_contents [ $ filename ] = $ contents ; } } if ( ! empty ( $ contents ) ) { return self :: extract_last_doc_comment ( implode ( "\n" , array_slice ( $ contents , 0 , $ reflection -> getStartLine ( ) ) ) ) ; } \ WP_CLI :: debug ( "Could not read contents for filename '{$filename}'." , 'commandfactory' ) ; return null ; }
Gets the document comment . Caters for PHP directive opcache . save comments being disabled .
1,345
public function display_item ( $ item , $ ascii_pre_colorized = false ) { if ( isset ( $ this -> args [ 'field' ] ) ) { $ item = ( object ) $ item ; $ key = $ this -> find_item_key ( $ item , $ this -> args [ 'field' ] ) ; $ value = $ item -> $ key ; if ( in_array ( $ this -> args [ 'format' ] , array ( 'table' , 'csv' ) , true ) && ( is_object ( $ value ) || is_array ( $ value ) ) ) { $ value = json_encode ( $ value ) ; } \ WP_CLI :: print_value ( $ value , array ( 'format' => $ this -> args [ 'format' ] , ) ) ; } else { $ this -> show_multiple_fields ( $ item , $ this -> args [ 'format' ] , $ ascii_pre_colorized ) ; } }
Display a single item according to the output arguments .
1,346
private function format ( $ items , $ ascii_pre_colorized = false ) { $ fields = $ this -> args [ 'fields' ] ; switch ( $ this -> args [ 'format' ] ) { case 'count' : if ( ! is_array ( $ items ) ) { $ items = iterator_to_array ( $ items ) ; } echo count ( $ items ) ; break ; case 'ids' : if ( ! is_array ( $ items ) ) { $ items = iterator_to_array ( $ items ) ; } echo implode ( ' ' , $ items ) ; break ; case 'table' : self :: show_table ( $ items , $ fields , $ ascii_pre_colorized ) ; break ; case 'csv' : \ WP_CLI \ Utils \ write_csv ( STDOUT , $ items , $ fields ) ; break ; case 'json' : case 'yaml' : $ out = array ( ) ; foreach ( $ items as $ item ) { $ out [ ] = \ WP_CLI \ Utils \ pick_fields ( $ item , $ fields ) ; } if ( 'json' === $ this -> args [ 'format' ] ) { if ( defined ( 'JSON_PARTIAL_OUTPUT_ON_ERROR' ) ) { echo json_encode ( $ out , JSON_PARTIAL_OUTPUT_ON_ERROR ) ; } else { echo json_encode ( $ out ) ; } } elseif ( 'yaml' === $ this -> args [ 'format' ] ) { echo Spyc :: YAMLDump ( $ out , 2 , 0 ) ; } break ; default : \ WP_CLI :: error ( 'Invalid format: ' . $ this -> args [ 'format' ] ) ; } }
Format items according to arguments .
1,347
private function show_single_field ( $ items , $ field ) { $ key = null ; $ values = array ( ) ; foreach ( $ items as $ item ) { $ item = ( object ) $ item ; if ( null === $ key ) { $ key = $ this -> find_item_key ( $ item , $ field ) ; } if ( 'json' === $ this -> args [ 'format' ] ) { $ values [ ] = $ item -> $ key ; } else { \ WP_CLI :: print_value ( $ item -> $ key , array ( 'format' => $ this -> args [ 'format' ] , ) ) ; } } if ( 'json' === $ this -> args [ 'format' ] ) { echo json_encode ( $ values ) ; } }
Show a single field from a list of items .
1,348
private function show_multiple_fields ( $ data , $ format , $ ascii_pre_colorized = false ) { $ true_fields = array ( ) ; foreach ( $ this -> args [ 'fields' ] as $ field ) { $ true_fields [ ] = $ this -> find_item_key ( $ data , $ field ) ; } foreach ( $ data as $ key => $ value ) { if ( ! in_array ( $ key , $ true_fields , true ) ) { if ( is_array ( $ data ) ) { unset ( $ data [ $ key ] ) ; } elseif ( is_object ( $ data ) ) { unset ( $ data -> $ key ) ; } } } switch ( $ format ) { case 'table' : case 'csv' : $ rows = $ this -> assoc_array_to_rows ( $ data ) ; $ fields = array ( 'Field' , 'Value' ) ; if ( 'table' === $ format ) { self :: show_table ( $ rows , $ fields , $ ascii_pre_colorized ) ; } elseif ( 'csv' === $ format ) { \ WP_CLI \ Utils \ write_csv ( STDOUT , $ rows , $ fields ) ; } break ; case 'yaml' : case 'json' : \ WP_CLI :: print_value ( $ data , array ( 'format' => $ format , ) ) ; break ; default : \ WP_CLI :: error ( 'Invalid format: ' . $ format ) ; break ; } }
Show multiple fields of an object .
1,349
private function assoc_array_to_rows ( $ fields ) { $ rows = array ( ) ; foreach ( $ fields as $ field => $ value ) { if ( ! is_string ( $ value ) ) { $ value = json_encode ( $ value ) ; } $ rows [ ] = ( object ) array ( 'Field' => $ field , 'Value' => $ value , ) ; } return $ rows ; }
Format an associative array as a table .
1,350
public function transform_item_values_to_json ( $ item ) { foreach ( $ this -> args [ 'fields' ] as $ field ) { $ true_field = $ this -> find_item_key ( $ item , $ field ) ; $ value = is_object ( $ item ) ? $ item -> $ true_field : $ item [ $ true_field ] ; if ( is_array ( $ value ) || is_object ( $ value ) ) { if ( is_object ( $ item ) ) { $ item -> $ true_field = json_encode ( $ value ) ; } elseif ( is_array ( $ item ) ) { $ item [ $ true_field ] = json_encode ( $ value ) ; } } } return $ item ; }
Transforms objects and arrays to JSON as necessary
1,351
private static function parse_reference_links ( $ longdesc ) { $ description = '' ; foreach ( explode ( "\n" , $ longdesc ) as $ line ) { if ( 0 === strpos ( $ line , '#' ) ) { break ; } $ description .= $ line . "\n" ; } if ( $ description ) { $ links = array ( ) ; $ pattern = '/\[.+?\]\((https?:\/\/.+?)\)/' ; $ newdesc = preg_replace_callback ( $ pattern , function ( $ matches ) use ( & $ links ) { static $ count = 0 ; $ count ++ ; $ links [ ] = $ matches [ 1 ] ; return str_replace ( '(' . $ matches [ 1 ] . ')' , '[' . $ count . ']' , $ matches [ 0 ] ) ; } , $ description ) ; $ footnote = '' ; $ link_count = count ( $ links ) ; for ( $ i = 0 ; $ i < $ link_count ; $ i ++ ) { $ n = $ i + 1 ; $ footnote .= '[' . $ n . '] ' . $ links [ $ i ] . "\n" ; } if ( $ footnote ) { $ newdesc = trim ( $ newdesc ) . "\n\n---\n" . $ footnote ; $ longdesc = str_replace ( trim ( $ description ) , trim ( $ newdesc ) , $ longdesc ) ; } } return $ longdesc ; }
Parse reference links from longdescription .
1,352
public function show_usage ( ) { $ methods = $ this -> get_subcommands ( ) ; $ i = 0 ; foreach ( $ methods as $ name => $ subcommand ) { $ prefix = ( 0 === $ i ) ? 'usage: ' : ' or: ' ; $ i ++ ; if ( \ WP_CLI :: get_runner ( ) -> is_command_disabled ( $ subcommand ) ) { continue ; } \ WP_CLI :: line ( $ subcommand -> get_usage ( $ prefix ) ) ; } $ cmd_name = implode ( ' ' , array_slice ( get_path ( $ this ) , 1 ) ) ; \ WP_CLI :: line ( ) ; \ WP_CLI :: line ( "See 'wp help $cmd_name <command>' for more information on a specific command." ) ; }
Show the usage for all subcommands contained by the composite command .
1,353
public function find_subcommand ( & $ args ) { $ name = array_shift ( $ args ) ; $ subcommands = $ this -> get_subcommands ( ) ; if ( ! isset ( $ subcommands [ $ name ] ) ) { $ aliases = self :: get_aliases ( $ subcommands ) ; if ( isset ( $ aliases [ $ name ] ) ) { $ name = $ aliases [ $ name ] ; } } if ( ! isset ( $ subcommands [ $ name ] ) ) { return false ; } return $ subcommands [ $ name ] ; }
Given supplied arguments find a contained subcommand
1,354
private static function get_aliases ( $ subcommands ) { $ aliases = array ( ) ; foreach ( $ subcommands as $ name => $ subcommand ) { $ alias = $ subcommand -> get_alias ( ) ; if ( $ alias ) { $ aliases [ $ alias ] = $ name ; } } return $ aliases ; }
Get any registered aliases for this composite command s subcommands .
1,355
public function info ( $ _ , $ assoc_args ) { $ php_bin = Utils \ get_php_binary ( ) ; $ system_os = PHP_MAJOR_VERSION < 7 ? php_uname ( ) : sprintf ( '%s %s %s %s' , php_uname ( 's' ) , php_uname ( 'r' ) , php_uname ( 'v' ) , php_uname ( 'm' ) ) ; $ shell = getenv ( 'SHELL' ) ; if ( ! $ shell && Utils \ is_windows ( ) ) { $ shell = getenv ( 'ComSpec' ) ; } $ runner = WP_CLI :: get_runner ( ) ; $ packages_dir = $ runner -> get_packages_dir_path ( ) ; if ( ! is_dir ( $ packages_dir ) ) { $ packages_dir = null ; } if ( \ WP_CLI \ Utils \ get_flag_value ( $ assoc_args , 'format' ) === 'json' ) { $ info = array ( 'php_binary_path' => $ php_bin , 'global_config_path' => $ runner -> global_config_path , 'project_config_path' => $ runner -> project_config_path , 'wp_cli_dir_path' => WP_CLI_ROOT , 'wp_cli_packages_dir_path' => $ packages_dir , 'wp_cli_version' => WP_CLI_VERSION , 'system_os' => $ system_os , 'shell' => $ shell , ) ; WP_CLI :: line ( json_encode ( $ info ) ) ; } else { WP_CLI :: line ( "OS:\t" . $ system_os ) ; WP_CLI :: line ( "Shell:\t" . $ shell ) ; WP_CLI :: line ( "PHP binary:\t" . $ php_bin ) ; WP_CLI :: line ( "PHP version:\t" . PHP_VERSION ) ; WP_CLI :: line ( "php.ini used:\t" . get_cfg_var ( 'cfg_file_path' ) ) ; WP_CLI :: line ( "WP-CLI root dir:\t" . WP_CLI_ROOT ) ; WP_CLI :: line ( "WP-CLI vendor dir:\t" . WP_CLI_VENDOR_DIR ) ; WP_CLI :: line ( "WP_CLI phar path:\t" . ( defined ( 'WP_CLI_PHAR_PATH' ) ? WP_CLI_PHAR_PATH : '' ) ) ; WP_CLI :: line ( "WP-CLI packages dir:\t" . $ packages_dir ) ; WP_CLI :: line ( "WP-CLI global config:\t" . $ runner -> global_config_path ) ; WP_CLI :: line ( "WP-CLI project config:\t" . $ runner -> project_config_path ) ; WP_CLI :: line ( "WP-CLI version:\t" . WP_CLI_VERSION ) ; } }
Print various details about the WP - CLI environment .
1,356
public function check_update ( $ _ , $ assoc_args ) { $ updates = $ this -> get_updates ( $ assoc_args ) ; if ( $ updates ) { $ formatter = new \ WP_CLI \ Formatter ( $ assoc_args , array ( 'version' , 'update_type' , 'package_url' ) ) ; $ formatter -> display_items ( $ updates ) ; } elseif ( empty ( $ assoc_args [ 'format' ] ) || 'table' === $ assoc_args [ 'format' ] ) { $ update_type = $ this -> get_update_type_str ( $ assoc_args ) ; WP_CLI :: success ( "WP-CLI is at the latest{$update_type}version." ) ; } }
Check to see if there is a newer version of WP - CLI available .
1,357
public function param_dump ( $ _ , $ assoc_args ) { $ spec = \ WP_CLI :: get_configurator ( ) -> get_spec ( ) ; if ( \ WP_CLI \ Utils \ get_flag_value ( $ assoc_args , 'with-values' ) ) { $ config = \ WP_CLI :: get_configurator ( ) -> to_array ( ) ; foreach ( $ spec as $ key => $ value ) { $ current = null ; if ( isset ( $ config [ 0 ] [ $ key ] ) ) { $ current = $ config [ 0 ] [ $ key ] ; } $ spec [ $ key ] [ 'current' ] = $ current ; } } if ( 'var_export' === \ WP_CLI \ Utils \ get_flag_value ( $ assoc_args , 'format' ) ) { var_export ( $ spec ) ; } else { echo json_encode ( $ spec ) ; } }
Dump the list of global parameters as JSON or in var_export format .
1,358
public function completions ( $ _ , $ assoc_args ) { $ line = substr ( $ assoc_args [ 'line' ] , 0 , $ assoc_args [ 'point' ] ) ; $ compl = new \ WP_CLI \ Completions ( $ line ) ; $ compl -> render ( ) ; }
Generate tab completion strings .
1,359
private function get_update_type_str ( $ assoc_args ) { $ update_type = ' ' ; foreach ( array ( 'major' , 'minor' , 'patch' ) as $ type ) { if ( true === \ WP_CLI \ Utils \ get_flag_value ( $ assoc_args , $ type ) ) { $ update_type = ' ' . $ type . ' ' ; break ; } } return $ update_type ; }
Get a string representing the type of update being checked for .
1,360
public function has_command ( $ _ , $ assoc_args ) { $ command = explode ( ' ' , implode ( ' ' , $ _ ) ) ; WP_CLI :: halt ( is_array ( WP_CLI :: get_runner ( ) -> find_command_to_run ( $ command ) ) ? 0 : 1 ) ; }
Detects if a command exists
1,361
public function get ( $ args , $ assoc_args ) { list ( $ alias ) = $ args ; $ aliases = WP_CLI :: get_runner ( ) -> aliases ; if ( empty ( $ aliases [ $ alias ] ) ) { WP_CLI :: error ( "No alias found with key '{$alias}'." ) ; } foreach ( $ aliases [ $ alias ] as $ key => $ value ) { WP_CLI :: log ( "{$key}: {$value}" ) ; } }
Gets the value for an alias .
1,362
public function delete ( $ args , $ assoc_args ) { list ( $ alias ) = $ args ; $ config = ( ! empty ( $ assoc_args [ 'config' ] ) ? $ assoc_args [ 'config' ] : '' ) ; list ( $ config_path , $ aliases ) = $ this -> get_aliases_data ( $ config , $ alias ) ; $ this -> validate_config_file ( $ config_path ) ; if ( empty ( $ aliases [ $ alias ] ) ) { WP_CLI :: error ( "No alias found with key '{$alias}'." ) ; } unset ( $ aliases [ $ alias ] ) ; $ this -> process_aliases ( $ aliases , $ alias , $ config_path , 'Deleted' ) ; }
Deletes an alias .
1,363
private function get_aliases_data ( $ config , $ alias ) { $ global_config_path = WP_CLI :: get_runner ( ) -> get_global_config_path ( ) ; $ global_aliases = Spyc :: YAMLLoad ( $ global_config_path ) ; $ project_config_path = WP_CLI :: get_runner ( ) -> get_project_config_path ( ) ; $ project_aliases = Spyc :: YAMLLoad ( $ project_config_path ) ; if ( 'global' === $ config ) { $ config_path = $ global_config_path ; $ aliases = $ global_aliases ; } elseif ( 'project' === $ config ) { $ config_path = $ project_config_path ; $ aliases = $ project_aliases ; } else { $ is_global_alias = array_key_exists ( $ alias , $ global_aliases ) ; $ is_project_alias = array_key_exists ( $ alias , $ project_aliases ) ; if ( $ is_global_alias && $ is_project_alias ) { WP_CLI :: error ( "Key '{$alias}' found in more than one path. Please pass --config param." ) ; } elseif ( $ is_global_alias ) { $ config_path = $ global_config_path ; $ aliases = $ global_aliases ; } else { $ config_path = $ project_config_path ; $ aliases = $ project_aliases ; } } return array ( $ config_path , $ aliases ) ; }
Get config path and aliases data based on config type .
1,364
private function build_aliases ( $ aliases , $ alias , $ assoc_args , $ is_grouping , $ grouping = '' , $ is_update = false ) { if ( $ is_grouping ) { $ valid_assoc_args = array ( 'config' , 'grouping' ) ; $ invalid_args = array_diff ( array_keys ( $ assoc_args ) , $ valid_assoc_args ) ; if ( ! empty ( $ invalid_args ) ) { $ args_info = implode ( ',' , $ invalid_args ) ; WP_CLI :: error ( "--grouping argument works alone. Found invalid arg(s) '$args_info'." ) ; } } if ( $ is_update ) { $ this -> validate_alias_type ( $ aliases , $ alias , $ assoc_args , $ grouping ) ; } if ( ! $ is_grouping ) { foreach ( $ assoc_args as $ key => $ value ) { if ( strpos ( $ key , 'set-' ) !== false ) { $ alias_key_info = explode ( '-' , $ key ) ; $ alias_key = empty ( $ alias_key_info [ 1 ] ) ? '' : $ alias_key_info [ 1 ] ; if ( ! empty ( $ alias_key ) && ! empty ( $ value ) ) { $ aliases [ $ alias ] [ $ alias_key ] = $ value ; } } } } else { if ( ! empty ( $ grouping ) ) { $ group_alias_list = explode ( ',' , $ grouping ) ; $ group_alias = array_map ( function ( $ current_alias ) { return '@' . ltrim ( $ current_alias , '@' ) ; } , $ group_alias_list ) ; $ aliases [ $ alias ] = $ group_alias ; } } return $ aliases ; }
Return aliases array .
1,365
private function validate_input ( $ assoc_args , $ grouping ) { $ arg_match = preg_grep ( '/^set-(\w+)/i' , array_keys ( $ assoc_args ) ) ; if ( empty ( $ grouping ) && empty ( $ arg_match ) ) { WP_CLI :: error ( 'No valid arguments passed.' ) ; } $ assoc_arg_values = array_filter ( array_intersect_key ( $ assoc_args , array_flip ( $ arg_match ) ) ) ; if ( empty ( $ grouping ) && empty ( $ assoc_arg_values ) ) { WP_CLI :: error ( 'No value passed to arguments.' ) ; } }
Validate input of passed arguments .
1,366
private function validate_alias_type ( $ aliases , $ alias , $ assoc_args , $ grouping ) { $ alias_data = $ aliases [ $ alias ] ; $ group_aliases_match = preg_grep ( '/^@(\w+)/i' , $ alias_data ) ; $ arg_match = preg_grep ( '/^set-(\w+)/i' , array_keys ( $ assoc_args ) ) ; if ( ! empty ( $ group_aliases_match ) && ! empty ( $ arg_match ) ) { WP_CLI :: error ( 'Trying to update group alias with invalid arguments.' ) ; } elseif ( empty ( $ group_aliases_match ) && ! empty ( $ grouping ) ) { WP_CLI :: error ( 'Trying to update simple alias with invalid --grouping argument.' ) ; } }
Validate alias type before update .
1,367
private function process_aliases ( $ aliases , $ alias , $ config_path , $ operation = '' ) { $ yaml_data = Spyc :: YAMLDump ( $ aliases ) ; if ( file_put_contents ( $ config_path , $ yaml_data ) ) { WP_CLI :: success ( "$operation '{$alias}' alias." ) ; } }
Save aliases data to config file .
1,368
private static function remove_decorations ( $ comment ) { $ comment = preg_replace ( '|^/\*\*[\r\n]+|' , '' , $ comment ) ; $ comment = preg_replace ( '|\n[\t ]*\*/$|' , '' , $ comment ) ; $ comment = preg_replace ( '|^[\t ]*\* ?|m' , '' , $ comment ) ; return $ comment ; }
Remove unused cruft from PHPdoc comment .
1,369
public function get_longdesc ( ) { $ shortdesc = $ this -> get_shortdesc ( ) ; if ( ! $ shortdesc ) { return '' ; } $ longdesc = substr ( $ this -> doc_comment , strlen ( $ shortdesc ) ) ; $ lines = array ( ) ; foreach ( explode ( "\n" , $ longdesc ) as $ line ) { if ( 0 === strpos ( $ line , '@' ) ) { break ; } $ lines [ ] = $ line ; } $ longdesc = trim ( implode ( $ lines , "\n" ) ) ; return $ longdesc ; }
Get the command s full description
1,370
private function get_arg_or_param_args ( $ regex ) { $ bits = explode ( "\n" , $ this -> doc_comment ) ; $ within_arg = false ; $ within_doc = false ; $ document = array ( ) ; foreach ( $ bits as $ bit ) { if ( preg_match ( $ regex , $ bit ) ) { $ within_arg = true ; } if ( $ within_arg && $ within_doc && '---' === $ bit ) { $ within_doc = false ; } if ( $ within_arg && ! $ within_doc && '---' === $ bit ) { $ within_doc = true ; } if ( $ within_doc ) { $ document [ ] = $ bit ; } if ( $ within_arg && '' === $ bit ) { $ within_arg = false ; break ; } } if ( $ document ) { return Spyc :: YAMLLoadString ( implode ( "\n" , $ document ) ) ; } return null ; }
Get the args for an arg or param
1,371
public function add_deferred_commands ( ) { $ deferred_additions = \ WP_CLI :: get_deferred_additions ( ) ; foreach ( $ deferred_additions as $ name => $ addition ) { \ WP_CLI :: debug ( sprintf ( 'Adding deferred command: %s => %s' , $ name , json_encode ( $ addition ) ) , 'bootstrap' ) ; \ WP_CLI :: add_command ( $ name , $ addition [ 'callable' ] , $ addition [ 'args' ] ) ; } }
Add deferred commands that are still waiting to be processed .
1,372
private static function get_project_milestones ( $ project , $ args = array ( ) ) { $ request_url = sprintf ( 'https://api.github.com/repos/%s/milestones' , $ project ) ; list ( $ body , $ headers ) = self :: make_github_api_request ( $ request_url , $ args ) ; return $ body ; }
Get the milestones for a given project
1,373
private static function parse_contributors_from_pull_requests ( $ pull_requests ) { $ contributors = array ( ) ; foreach ( $ pull_requests as $ pull_request ) { if ( ! empty ( $ pull_request -> user ) ) { $ contributors [ $ pull_request -> user -> html_url ] = $ pull_request -> user -> login ; } } return $ contributors ; }
Parse the contributors from pull request objects
1,374
private static function make_github_api_request ( $ url , $ args = array ( ) ) { $ headers = array ( 'Accept' => 'application/vnd.github.v3+json' , 'User-Agent' => 'WP-CLI' , ) ; $ token = getenv ( 'GITHUB_TOKEN' ) ; if ( false !== $ token ) { $ headers [ 'Authorization' ] = 'token ' . $ token ; } $ response = Utils \ http_request ( 'GET' , $ url , $ args , $ headers ) ; if ( 200 !== $ response -> status_code ) { WP_CLI :: error ( sprintf ( 'GitHub API returned: %s (HTTP code %d)' , $ response -> body , $ response -> status_code ) ) ; } return array ( json_decode ( $ response -> body ) , $ response -> headers ) ; }
Make a request to the GitHub API
1,375
private function adjust_offset_for_shrinking_result_set ( ) { if ( empty ( $ this -> count_query ) ) { return ; } $ row_count = $ this -> db -> get_var ( $ this -> count_query ) ; if ( $ row_count < $ this -> row_count ) { $ this -> offset -= $ this -> row_count - $ row_count ; } $ this -> row_count = $ row_count ; }
Reduces the offset when the query row count shrinks
1,376
public function get_aliases ( ) { $ runtime_alias = getenv ( 'WP_CLI_RUNTIME_ALIAS' ) ; if ( false !== $ runtime_alias ) { $ returned_aliases = array ( ) ; foreach ( json_decode ( $ runtime_alias , true ) as $ key => $ value ) { if ( preg_match ( '#' . self :: ALIAS_REGEX . '#' , $ key ) ) { $ returned_aliases [ $ key ] = array ( ) ; foreach ( self :: $ alias_spec as $ i ) { if ( isset ( $ value [ $ i ] ) ) { $ returned_aliases [ $ key ] [ $ i ] = $ value [ $ i ] ; } } } } return $ returned_aliases ; } return $ this -> aliases ; }
Get any aliases defined in config files .
1,377
public function parse_args ( $ arguments ) { list ( $ positional_args , $ mixed_args , $ global_assoc , $ local_assoc ) = self :: extract_assoc ( $ arguments ) ; list ( $ assoc_args , $ runtime_config ) = $ this -> unmix_assoc_args ( $ mixed_args , $ global_assoc , $ local_assoc ) ; return array ( $ positional_args , $ assoc_args , $ runtime_config ) ; }
Splits a list of arguments into positional associative and config .
1,378
public static function extract_assoc ( $ arguments ) { $ positional_args = array ( ) ; $ assoc_args = array ( ) ; $ global_assoc = array ( ) ; $ local_assoc = array ( ) ; foreach ( $ arguments as $ arg ) { $ positional_arg = null ; $ assoc_arg = null ; if ( preg_match ( '|^--no-([^=]+)$|' , $ arg , $ matches ) ) { $ assoc_arg = array ( $ matches [ 1 ] , false ) ; } elseif ( preg_match ( '|^--([^=]+)$|' , $ arg , $ matches ) ) { $ assoc_arg = array ( $ matches [ 1 ] , true ) ; } elseif ( preg_match ( '|^--([^=]+)=(.*)|s' , $ arg , $ matches ) ) { $ assoc_arg = array ( $ matches [ 1 ] , $ matches [ 2 ] ) ; } else { $ positional = $ arg ; } if ( ! is_null ( $ assoc_arg ) ) { $ assoc_args [ ] = $ assoc_arg ; if ( count ( $ positional_args ) ) { $ local_assoc [ ] = $ assoc_arg ; } else { $ global_assoc [ ] = $ assoc_arg ; } } elseif ( ! is_null ( $ positional ) ) { $ positional_args [ ] = $ positional ; } } return array ( $ positional_args , $ assoc_args , $ global_assoc , $ local_assoc ) ; }
Splits positional args from associative args .
1,379
private function unmix_assoc_args ( $ mixed_args , $ global_assoc = array ( ) , $ local_assoc = array ( ) ) { $ assoc_args = array ( ) ; $ runtime_config = array ( ) ; if ( getenv ( 'WP_CLI_STRICT_ARGS_MODE' ) ) { foreach ( $ global_assoc as $ tmp ) { list ( $ key , $ value ) = $ tmp ; if ( isset ( $ this -> spec [ $ key ] ) && false !== $ this -> spec [ $ key ] [ 'runtime' ] ) { $ this -> assoc_arg_to_runtime_config ( $ key , $ value , $ runtime_config ) ; } } foreach ( $ local_assoc as $ tmp ) { $ assoc_args [ $ tmp [ 0 ] ] = $ tmp [ 1 ] ; } } else { foreach ( $ mixed_args as $ tmp ) { list ( $ key , $ value ) = $ tmp ; if ( isset ( $ this -> spec [ $ key ] ) && false !== $ this -> spec [ $ key ] [ 'runtime' ] ) { $ this -> assoc_arg_to_runtime_config ( $ key , $ value , $ runtime_config ) ; } else { $ assoc_args [ $ key ] = $ value ; } } } return array ( $ assoc_args , $ runtime_config ) ; }
Separate runtime parameters from command - specific parameters .
1,380
public function merge_yml ( $ path , $ current_alias = null ) { $ yaml = self :: load_yml ( $ path ) ; if ( ! empty ( $ yaml [ '_' ] [ 'inherit' ] ) ) { $ this -> merge_yml ( $ yaml [ '_' ] [ 'inherit' ] , $ current_alias ) ; } $ yml_file_dir = $ path ? dirname ( $ path ) : false ; foreach ( $ yaml as $ key => $ value ) { if ( preg_match ( '#' . self :: ALIAS_REGEX . '#' , $ key ) ) { $ this -> aliases [ $ key ] = array ( ) ; $ is_alias = false ; foreach ( self :: $ alias_spec as $ i ) { if ( isset ( $ value [ $ i ] ) ) { if ( 'path' === $ i && ! isset ( $ value [ 'ssh' ] ) ) { self :: absolutize ( $ value [ $ i ] , $ yml_file_dir ) ; } $ this -> aliases [ $ key ] [ $ i ] = $ value [ $ i ] ; $ is_alias = true ; } } if ( ! $ is_alias && is_array ( $ value ) ) { $ alias_group = array ( ) ; foreach ( $ value as $ i => $ k ) { if ( preg_match ( '#' . self :: ALIAS_REGEX . '#' , $ k ) ) { $ alias_group [ ] = $ k ; } } $ this -> aliases [ $ key ] = $ alias_group ; } } elseif ( ! isset ( $ this -> spec [ $ key ] ) || false === $ this -> spec [ $ key ] [ 'file' ] ) { if ( isset ( $ this -> extra_config [ $ key ] ) && ! empty ( $ yaml [ '_' ] [ 'merge' ] ) && is_array ( $ this -> extra_config [ $ key ] ) && is_array ( $ value ) ) { $ this -> extra_config [ $ key ] = array_merge ( $ this -> extra_config [ $ key ] , $ value ) ; } else { $ this -> extra_config [ $ key ] = $ value ; } } elseif ( $ this -> spec [ $ key ] [ 'multiple' ] ) { self :: arrayify ( $ value ) ; $ this -> config [ $ key ] = array_merge ( $ this -> config [ $ key ] , $ value ) ; } else { if ( $ current_alias && in_array ( $ key , self :: $ alias_spec , true ) ) { continue ; } $ this -> config [ $ key ] = $ value ; } } }
Load a YAML file of parameters into scope .
1,381
public function merge_array ( $ config ) { foreach ( $ this -> spec as $ key => $ details ) { if ( false !== $ details [ 'runtime' ] && isset ( $ config [ $ key ] ) ) { $ value = $ config [ $ key ] ; if ( 'require' === $ key ) { $ value = \ WP_CLI \ Utils \ expand_globs ( $ value ) ; } if ( $ details [ 'multiple' ] ) { self :: arrayify ( $ value ) ; $ this -> config [ $ key ] = array_merge ( $ this -> config [ $ key ] , $ value ) ; } else { $ this -> config [ $ key ] = $ value ; } } } }
Merge an array of values into the configurator config .
1,382
private static function load_yml ( $ yml_file ) { if ( ! $ yml_file ) { return array ( ) ; } $ config = Spyc :: YAMLLoad ( $ yml_file ) ; $ yml_file_dir = dirname ( $ yml_file ) ; if ( isset ( $ config [ 'path' ] ) ) { self :: absolutize ( $ config [ 'path' ] , $ yml_file_dir ) ; } if ( isset ( $ config [ 'require' ] ) ) { self :: arrayify ( $ config [ 'require' ] ) ; $ config [ 'require' ] = \ WP_CLI \ Utils \ expand_globs ( $ config [ 'require' ] ) ; foreach ( $ config [ 'require' ] as & $ path ) { self :: absolutize ( $ path , $ yml_file_dir ) ; } } if ( isset ( $ config [ 'core config' ] ) ) { $ config [ 'config create' ] = $ config [ 'core config' ] ; unset ( $ config [ 'core config' ] ) ; } if ( isset ( $ config [ 'checksum core' ] ) ) { $ config [ 'core verify-checksums' ] = $ config [ 'checksum core' ] ; unset ( $ config [ 'checksum core' ] ) ; } if ( isset ( $ config [ 'checksum plugin' ] ) ) { $ config [ 'plugin verify-checksums' ] = $ config [ 'checksum plugin' ] ; unset ( $ config [ 'checksum plugin' ] ) ; } return $ config ; }
Load values from a YAML file .
1,383
private static function absolutize ( & $ path , $ base ) { if ( ! empty ( $ path ) && ! \ WP_CLI \ Utils \ is_path_absolute ( $ path ) ) { $ path = $ base . DIRECTORY_SEPARATOR . $ path ; } }
Make a path absolute .
1,384
public static function set_url ( $ url ) { self :: debug ( 'Set URL: ' . $ url , 'bootstrap' ) ; $ url_parts = Utils \ parse_url ( $ url ) ; self :: set_url_params ( $ url_parts ) ; }
Set the context in which WP - CLI should be run
1,385
public static function add_hook ( $ when , $ callback ) { if ( array_key_exists ( $ when , self :: $ hooks_passed ) ) { self :: debug ( sprintf ( 'Immediately invoking on passed hook "%s": %s' , $ when , Utils \ describe_callable ( $ callback ) ) , 'hooks' ) ; call_user_func_array ( $ callback , ( array ) self :: $ hooks_passed [ $ when ] ) ; } self :: $ hooks [ $ when ] [ ] = $ callback ; }
Schedule a callback to be executed at a certain point .
1,386
public static function do_hook ( $ when ) { $ args = func_num_args ( ) > 1 ? array_slice ( func_get_args ( ) , 1 ) : array ( ) ; self :: $ hooks_passed [ $ when ] = $ args ; if ( ! isset ( self :: $ hooks [ $ when ] ) ) { return ; } self :: debug ( sprintf ( 'Processing hook "%s" with %d callbacks' , $ when , count ( self :: $ hooks [ $ when ] ) ) , 'hooks' ) ; foreach ( self :: $ hooks [ $ when ] as $ callback ) { self :: debug ( sprintf ( 'On hook "%s": %s' , $ when , Utils \ describe_callable ( $ callback ) ) , 'hooks' ) ; call_user_func_array ( $ callback , $ args ) ; } }
Execute callbacks registered to a given hook .
1,387
public static function add_wp_hook ( $ tag , $ function_to_add , $ priority = 10 , $ accepted_args = 1 ) { global $ wp_filter , $ merged_filters ; if ( function_exists ( 'add_filter' ) ) { add_filter ( $ tag , $ function_to_add , $ priority , $ accepted_args ) ; } else { $ idx = self :: wp_hook_build_unique_id ( $ tag , $ function_to_add , $ priority ) ; $ wp_filter [ $ tag ] [ $ priority ] [ $ idx ] = array ( 'function' => $ function_to_add , 'accepted_args' => $ accepted_args , ) ; unset ( $ merged_filters [ $ tag ] ) ; } return true ; }
Add a callback to a WordPress action or filter .
1,388
private static function defer_command_addition ( $ name , $ parent , $ callable , $ args = array ( ) ) { $ args [ 'is_deferred' ] = true ; self :: $ deferred_additions [ $ name ] = array ( 'parent' => $ parent , 'callable' => $ callable , 'args' => $ args , ) ; self :: add_hook ( "after_add_command:$parent" , function ( ) use ( $ name ) { $ deferred_additions = WP_CLI :: get_deferred_additions ( ) ; if ( ! array_key_exists ( $ name , $ deferred_additions ) ) { return ; } $ callable = $ deferred_additions [ $ name ] [ 'callable' ] ; $ args = $ deferred_additions [ $ name ] [ 'args' ] ; WP_CLI :: remove_deferred_addition ( $ name ) ; WP_CLI :: add_command ( $ name , $ callable , $ args ) ; } ) ; }
Defer command addition for a sub - command if the parent command is not yet registered .
1,389
public static function remove_deferred_addition ( $ name ) { if ( ! array_key_exists ( $ name , self :: $ deferred_additions ) ) { self :: warning ( "Trying to remove a non-existent command addition '{$name}'." ) ; } unset ( self :: $ deferred_additions [ $ name ] ) ; }
Remove a command addition from the list of outstanding deferred additions .
1,390
public static function error_multi_line ( $ message_lines ) { if ( ! isset ( self :: get_runner ( ) -> assoc_args [ 'completions' ] ) && is_array ( $ message_lines ) ) { self :: $ logger -> error_multi_line ( array_map ( array ( __CLASS__ , 'error_to_string' ) , $ message_lines ) ) ; } }
Display a multi - line error message in a red box . Doesn t exit script .
1,391
public static function confirm ( $ question , $ assoc_args = array ( ) ) { if ( ! \ WP_CLI \ Utils \ get_flag_value ( $ assoc_args , 'yes' ) ) { fwrite ( STDOUT , $ question . ' [y/n] ' ) ; $ answer = strtolower ( trim ( fgets ( STDIN ) ) ) ; if ( 'y' !== $ answer ) { exit ; } } }
Ask for confirmation before running a destructive operation .
1,392
public static function get_value_from_arg_or_stdin ( $ args , $ index ) { if ( isset ( $ args [ $ index ] ) ) { $ raw_value = $ args [ $ index ] ; } else { $ raw_value = '' ; while ( false !== ( $ line = fgets ( STDIN ) ) ) { $ raw_value .= $ line ; } } return $ raw_value ; }
Read value from a positional argument or from STDIN .
1,393
public static function read_value ( $ raw_value , $ assoc_args = array ( ) ) { if ( \ WP_CLI \ Utils \ get_flag_value ( $ assoc_args , 'format' ) === 'json' ) { $ value = json_decode ( $ raw_value , true ) ; if ( null === $ value ) { self :: error ( sprintf ( 'Invalid JSON: %s' , $ raw_value ) ) ; } } else { $ value = $ raw_value ; } return $ value ; }
Read a value from various formats .
1,394
public static function print_value ( $ value , $ assoc_args = array ( ) ) { if ( \ WP_CLI \ Utils \ get_flag_value ( $ assoc_args , 'format' ) === 'json' ) { $ value = json_encode ( $ value ) ; } elseif ( \ WP_CLI \ Utils \ get_flag_value ( $ assoc_args , 'format' ) === 'yaml' ) { $ value = Spyc :: YAMLDump ( $ value , 2 , 0 ) ; } elseif ( is_array ( $ value ) || is_object ( $ value ) ) { $ value = var_export ( $ value , true ) ; } echo $ value . "\n" ; }
Display a value in various formats
1,395
public static function error_to_string ( $ errors ) { if ( is_string ( $ errors ) ) { return $ errors ; } $ render_data = function ( $ data ) { if ( is_array ( $ data ) || is_object ( $ data ) ) { return json_encode ( $ data ) ; } return '"' . $ data . '"' ; } ; if ( is_object ( $ errors ) && is_a ( $ errors , 'WP_Error' ) ) { foreach ( $ errors -> get_error_messages ( ) as $ message ) { if ( $ errors -> get_error_data ( ) ) { return $ message . ' ' . $ render_data ( $ errors -> get_error_data ( ) ) ; } return $ message ; } } }
Convert a wp_error into a string
1,396
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 ] ; continue ; } $ value = self :: get_runner ( ) -> config [ $ key ] ; if ( $ value ) { $ assoc_args [ $ key ] = $ value ; } } $ php_bin = escapeshellarg ( Utils \ get_php_binary ( ) ) ; $ script_path = $ GLOBALS [ 'argv' ] [ 0 ] ; if ( getenv ( 'WP_CLI_CONFIG_PATH' ) ) { $ config_path = getenv ( 'WP_CLI_CONFIG_PATH' ) ; } else { $ config_path = Utils \ get_home_dir ( ) . '/.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 ) ; }
Run a WP - CLI command in a new process reusing the current runtime arguments .
1,397
public static function get_config ( $ key = null ) { if ( null === $ key ) { return self :: get_runner ( ) -> config ; } if ( ! isset ( self :: get_runner ( ) -> config [ $ key ] ) ) { self :: warning ( "Unknown config option '$key'." ) ; return null ; } return self :: get_runner ( ) -> config [ $ key ] ; }
Get values of global configuration parameters .
1,398
public static function render ( & $ synopsis ) { if ( ! is_array ( $ synopsis ) ) { return '' ; } $ bits = [ 'positional' => '' , 'assoc' => '' , 'generic' => '' , 'flag' => '' , ] ; $ reordered_synopsis = [ 'positional' => [ ] , 'assoc' => [ ] , 'generic' => [ ] , 'flag' => [ ] , ] ; foreach ( $ bits as $ key => & $ value ) { foreach ( $ synopsis as $ arg ) { if ( empty ( $ arg [ 'type' ] ) || $ key !== $ arg [ 'type' ] ) { continue ; } if ( empty ( $ arg [ 'name' ] ) && 'generic' !== $ arg [ 'type' ] ) { continue ; } if ( 'positional' === $ key ) { $ rendered_arg = "<{$arg['name']}>" ; $ reordered_synopsis [ 'positional' ] [ ] = $ arg ; } elseif ( 'assoc' === $ key ) { $ arg_value = isset ( $ arg [ 'value' ] [ 'name' ] ) ? $ arg [ 'value' ] [ 'name' ] : $ arg [ 'name' ] ; $ rendered_arg = "--{$arg['name']}=<{$arg_value}>" ; $ reordered_synopsis [ 'assoc' ] [ ] = $ arg ; } elseif ( 'generic' === $ key ) { $ rendered_arg = '--<field>=<value>' ; $ reordered_synopsis [ 'generic' ] [ ] = $ arg ; } elseif ( 'flag' === $ key ) { $ rendered_arg = "--{$arg['name']}" ; $ reordered_synopsis [ 'flag' ] [ ] = $ arg ; } if ( ! empty ( $ arg [ 'repeating' ] ) ) { $ rendered_arg = "{$rendered_arg}..." ; } if ( ! empty ( $ arg [ 'optional' ] ) ) { $ rendered_arg = "[{$rendered_arg}]" ; } $ value .= "{$rendered_arg} " ; } } $ rendered = '' ; foreach ( $ bits as $ v ) { if ( ! empty ( $ v ) ) { $ rendered .= $ v ; } } $ synopsis = array_merge ( $ reordered_synopsis [ 'positional' ] , $ reordered_synopsis [ 'assoc' ] , $ reordered_synopsis [ 'generic' ] , $ reordered_synopsis [ 'flag' ] ) ; return rtrim ( $ rendered , ' ' ) ; }
Render the Synopsis into a format string .
1,399
private static function classify_token ( $ token ) { $ param = array ( ) ; list ( $ param [ 'optional' ] , $ token ) = self :: is_optional ( $ token ) ; list ( $ param [ 'repeating' ] , $ token ) = self :: is_repeating ( $ token ) ; $ p_name = '([a-z-_0-9]+)' ; $ p_value = '([a-zA-Z-_|,0-9]+)' ; if ( '--<field>=<value>' === $ token ) { $ param [ 'type' ] = 'generic' ; } elseif ( preg_match ( "/^<($p_value)>$/" , $ token , $ matches ) ) { $ param [ 'type' ] = 'positional' ; $ param [ 'name' ] = $ matches [ 1 ] ; } elseif ( preg_match ( "/^--(?:\\[no-\\])?$p_name/" , $ token , $ matches ) ) { $ param [ 'name' ] = $ matches [ 1 ] ; $ value = substr ( $ token , strlen ( $ matches [ 0 ] ) ) ; if ( false === $ value || '' === $ value ) { $ param [ 'type' ] = 'flag' ; } else { $ param [ 'type' ] = 'assoc' ; list ( $ param [ 'value' ] [ 'optional' ] , $ value ) = self :: is_optional ( $ value ) ; if ( preg_match ( "/^=<$p_value>$/" , $ value , $ matches ) ) { $ param [ 'value' ] [ 'name' ] = $ matches [ 1 ] ; } else { $ param = array ( 'type' => 'unknown' , ) ; } } } else { $ param [ 'type' ] = 'unknown' ; } return $ param ; }
Classify argument attributes based on its syntax .