本文整理汇总了PHP中WP_CLI::log方法的典型用法代码示例。如果您正苦于以下问题:PHP WP_CLI::log方法的具体用法?PHP WP_CLI::log怎么用?PHP WP_CLI::log使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WP_CLI
的用法示例。
在下文中一共展示了WP_CLI::log方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: migrate
/**
* Migrate meta values to taxonomy terms. Delete meta after import
*
* ## OPTIONS
*
* <meta-key>
* : Meta key to convert
*
* <taxonomy-slug>
* : Taxonomy to move values into
*
* [--<field>=<value>]
* : One or more args to pass to WP_Query.
*
* ## EXAMPLES
*
* wp mtt migrate meta_key taxonomy_slug
* wp mtt migrate meta_key taxonomy_slug --posts_per_page=200 --paged=2
*
*/
function migrate($args, $assoc_args)
{
list($meta_key, $taxonomy) = $args;
if (!($taxonomy_object = get_taxonomy($taxonomy))) {
WP_CLI::error(sprintf("The taxonomy '%s' doesn't exist", $taxonomy));
}
$defaults = array('post_type' => $taxonomy_object->object_type, 'posts_per_page' => -1, 'post_status' => 'any');
$query_args = array_merge($defaults, $assoc_args);
$query = new WP_Query($query_args);
// summary
WP_CLI::log(sprintf("---\nPer page: %d \nPage: %d \nTotal pages: %d\n---", $query_args['posts_per_page'], isset($query_args['paged']) ? $query_args['paged'] : 1, $query->max_num_pages));
while ($query->have_posts()) {
$query->the_post();
$id = get_the_id();
// get meta
$metas = get_post_meta($id, $meta_key);
// create term
if (!$metas) {
WP_CLI::log(WP_CLI::colorize("%c[{$id}]%n No meta, skipped"));
} else {
if (!is_wp_error(wp_set_object_terms($id, $metas, $taxonomy, true))) {
WP_CLI::log(WP_CLI::colorize("%g[{$id}]%n Migrated: " . implode(', ', $metas)));
// clean meta
delete_post_meta($id, $meta_key);
} else {
WP_CLI::log(WP_CLI::colorize("%r[{$id}]%n Error: Could not set terms for post"));
}
}
}
}
示例2: add
/**
* Grant super-admin privileges to one or more users.
*
* <user>...
* : One or more user IDs, user emails, or user logins.
*/
public function add($args, $_)
{
$users = $this->fetcher->get_many($args);
$user_logins = wp_list_pluck($users, 'user_login');
$super_admins = self::get_admins();
$num_super_admins = count($super_admins);
foreach ($user_logins as $user_login) {
$user = get_user_by('login', $user_login);
if (!$user) {
WP_CLI::warning("Couldn't find {$user_login} user.");
continue;
}
if (in_array($user->user_login, $super_admins)) {
WP_CLI::warning("User {$user_login} already has super-admin capabilities.");
continue;
}
$super_admins[] = $user->user_login;
}
if ($num_super_admins === count($super_admins)) {
WP_CLI::log('No changes.');
} else {
if (update_site_option('site_admins', $super_admins)) {
WP_CLI::success('Granted super-admin capabilities.');
} else {
WP_CLI::error('Site options update failed!');
}
}
}
示例3: purge
/**
* Forces a full Varnish Purge of the entire site (provided
* regex is supported).
*
* ## EXAMPLES
*
* wp varnish purge
*
* wp varnish purge http://example.com/wp-content/themes/twentyeleventy/style.css
*
* wp vanrish purge "/wp-content/themes/twentysixty/style.css"
*
* wp varnish purge http://example.com/wp-content/themes/ --wildcard
*
* wp varnish purge "/wp-content/themes/" --wildcard
*
*/
function purge($args, $assoc_args)
{
$wp_version = get_bloginfo('version');
$cli_version = WP_CLI_VERSION;
// Set the URL/path
list($url) = $args;
// If wildcard is set, or the URL argument is empty
// then treat this as a full purge
if (isset($assoc_args['wildcard']) || empty($url)) {
$pregex = '/?vhp-regex';
$wild = ".*";
} else {
$pregex = $wild = '';
}
wp_create_nonce('vhp-flush-cli');
// Make sure the URL is a URL:
if (!empty($url)) {
// If the URL isn't a URL, make it a URL
if (empty(esc_url($url))) {
$url = $this->varnish_purge->the_home_url() . $url;
}
} else {
$url = $this->varnish_purge->the_home_url();
}
if (version_compare($wp_version, '4.6', '>=') && (version_compare($cli_version, '0.25.0', '<') || version_compare($cli_version, '0.25.0-alpha', 'eq'))) {
WP_CLI::log(sprintf('This plugin does not work on WP 4.6 and up, unless WP-CLI is version 0.25.0 or greater. You\'re using WP-CLI %s and WordPress %s.', $cli_version, $wp_version));
WP_CLI::log('To flush your cache, please run the following command:');
WP_CLI::log(sprintf('$ curl -X PURGE "%s"', $url . $wild));
WP_CLI::error('Varnish Cache must be purged manually.');
}
$this->varnish_purge->purgeUrl($url . $pregex);
WP_CLI::success('The Varnish cache was purged.');
}
示例4: __invoke
/**
* Import content from a WXR file.
*
* ## OPTIONS
*
* <file>...
* : Path to one or more valid WXR files for importing.
*
* --authors=<authors>
* : How the author mapping should be handled. Options are 'create', 'mapping.csv', or 'skip'. The first will create any non-existent users from the WXR file. The second will read author mapping associations from a CSV, or create a CSV for editing if the file path doesn't exist. The CSV requires two columns, and a header row like "old_user_login,new_user_login". The last option will skip any author mapping.
*
* [--skip=<data-type>]
* : Skip importing specific data. Supported options are: 'attachment' and 'image_resize' (skip time-consuming thumbnail generation).
*/
public function __invoke($args, $assoc_args)
{
$defaults = array('authors' => null, 'skip' => array());
$assoc_args = wp_parse_args($assoc_args, $defaults);
if (!is_array($assoc_args['skip'])) {
$assoc_args['skip'] = explode(',', $assoc_args['skip']);
}
$importer = $this->is_importer_available();
if (is_wp_error($importer)) {
WP_CLI::error($importer);
}
$this->add_wxr_filters();
WP_CLI::log('Starting the import process...');
foreach ($args as $file) {
if (!is_readable($file)) {
WP_CLI::warning("Can't read {$file} file.");
}
$ret = $this->import_wxr($file, $assoc_args);
if (is_wp_error($ret)) {
WP_CLI::warning($ret);
} else {
WP_CLI::line();
// WXR import ends with HTML, so make sure message is on next line
WP_CLI::success("Finished importing from {$file} file.");
}
}
}
示例5: blank_media_fix
/**
* Fixes issues with slow saving in wp-admin due to no audio/video media files
*
* Core Ticket: https://core.trac.wordpress.org/ticket/31071
*
* eg.: `wp vip-go-one-time-fixers blank-media-fix --allow-root --url=beta.thesun.co.uk`
*
* @subcommand blank-media-fix
*/
public function blank_media_fix($args, $assoc_args)
{
if (!function_exists('wpcom_vip_download_image')) {
WP_CLI::error('This script requires the wpcom_vip_download_image() function, https://vip.wordpress.com/functions/wpcom_vip_download_image/');
}
$audio_file_url = 'https://cldup.com/xmre07YagX.mp3';
// 1sec.mp3
$video_file_url = 'https://cldup.com/KHsK5yZkvv.avi';
// 1sec.avi
$args = array('post_type' => 'attachment', 'post_status' => 'inherit', 'meta_query' => array(array('key' => '_vip_blank_media_fix', 'value' => 'video')));
$video_query = new WP_Query($args);
if (!$video_query->post_count) {
WP_CLI::log('Video fix not found, applying...');
$video_file_id = $this->wpcom_vip_download_image($video_file_url, 0, 'VIP: Fix for slow post saving');
if (!is_wp_error($video_file_id)) {
$args = array('ID' => $video_file_id, 'post_date' => '2000-01-01', 'post_date_gmt' => '2000-01-01', 'post_modified' => '2000-01-01', 'post_modified_gmt' => '2000-01-01');
$updated_video_file_id = wp_update_post($args, true);
if (!is_wp_error($updated_video_file_id)) {
WP_CLI::success('Video fix applied');
$video_meta = update_post_meta($updated_video_file_id, '_vip_blank_media_fix', 'video');
if (false === $video_meta) {
WP_CLI::warning('Could not update video _vip_blank_media_fix meta');
}
} else {
// Video date was not updated
WP_CLI::error($updated_video_file_id->get_error_message());
}
} else {
// Sideload failed
WP_CLI::error($video_file_id->get_error_message());
}
} else {
WP_CLI::warning('Blank video fix already exists for this site');
}
$args = array('post_type' => 'attachment', 'post_status' => 'inherit', 'meta_query' => array(array('key' => '_vip_blank_media_fix', 'value' => 'audio')));
$audio_query = new WP_Query($args);
if (!$audio_query->post_count) {
WP_CLI::log('Audio fix not found, applying...');
$audio_file_id = $this->wpcom_vip_download_image($audio_file_url, 0, 'VIP: Fix for slow post saving');
if (!is_wp_error($audio_file_id)) {
$args = array('ID' => $audio_file_id, 'post_date' => '2000-01-01', 'post_date_gmt' => '2000-01-01', 'post_modified' => '2000-01-01', 'post_modified_gmt' => '2000-01-01');
$updated_audio_file_id = wp_update_post($args, true);
if (!is_wp_error($updated_audio_file_id)) {
WP_CLI::success('Audio fix applied');
$audio_meta = update_post_meta($updated_audio_file_id, '_vip_blank_media_fix', 'audio');
if (false === $audio_meta) {
WP_CLI::warning('Could not update audio _vip_blank_media_fix meta');
}
} else {
// Audio date was not updated
WP_CLI::error($updated_audio_file_id->get_error_message());
}
} else {
// Sideload failed
WP_CLI::error($video_file_id->get_error_message());
}
} else {
WP_CLI::warning('Blank video fix already exists for this site');
}
}
示例6: taxonomy
/**
*
* Assigns random terms to all posts in a taxonomy.
*
* By default all objects of the 'post' post type will be randomized, use the
* --post_type flag to target pages or a custom post type. Use the --include
* and --exclude flags to filter or ignore specific object IDs and the --before
* and --after flags to specify a date range. Also, optionally pass --terms as
* a list of terms you want to use for the randomization. If terms exist in the
* target taxonomy, those terms will be used. If not, a string of 6 words
* generated randomly will be used for the randomization.
*
* ## Options
*
* <taxonomy>
* : The taxonomy that should get randomized
*
* ## Exmples
*
* wp randomize category
*
* @synopsis <taxonomy> [--include=<bar>] [--exclude=<foo>] [--post_type=<foo>]
* [--before=<bar>] [--after=<date>] [--terms=<terms>]
*
**/
public function taxonomy($args, $assoc_args)
{
$taxonomy = $args[0];
$get_posts = $this->get_specified_posts($assoc_args);
$message = $get_posts['message'];
$posts = $get_posts['posts'];
$args = $get_posts['args'];
$preamble = "Will assign random {$taxonomy} terms";
print_r("{$preamble} {$message}.\n");
if (isset($assoc_args['terms'])) {
$terms = explode(',', $assoc_args['terms']);
\WP_CLI::log('Using terms ' . $assoc_args['terms']);
} else {
\WP_CLI::log('Gathering and processing random terms.');
$terms = $this->get_random_terms();
\WP_CLI::log('No term list given, using random terms.');
}
foreach ($posts as $p) {
$index = array_rand($terms);
$term = $terms[$index];
\WP_CLI::log("Assigning {$term} to taxonomy {$taxonomy} for {$p->post_type} {$p->ID}");
if (!term_exists($term, $taxonomy)) {
wp_insert_term($term, $taxonomy);
}
wp_set_object_terms($p->ID, $term, $taxonomy, $append = false);
}
}
示例7: setup
/**
* Setup Elasticsearch.
*
* ## OPTIONS
*
* [--host=<url>]
* : The name of the person to greet.
*
* [--port=<number>]
* : Accepted values: csv, json. Default: csv
*
* ## EXAMPLES
*
* wp elasticsearch setup --host=example.com --port=9200
*
* @subcommand setup
*/
function setup($args, $assoc_args)
{
$param = array();
$param['endpoint'] = preg_replace('/(^https:\\/\\/|^http:\\/\\/)/is', '', $assoc_args['host']);
$param['port'] = $assoc_args['port'];
$tries = 5;
$sleep = 3;
do {
$response = wp_remote_get(esc_url($assoc_args['host']) . ':' . $assoc_args['port']);
if (200 == wp_remote_retrieve_response_code($response)) {
// Looks good!
break;
} else {
WP_CLI::log("\nInvalid response from ES, sleeping {$sleep} seconds and trying again...\n");
sleep($sleep);
}
} while (--$tries);
if (200 != wp_remote_retrieve_response_code($response)) {
WP_CLI::error('Could not connect to Elasticsearch server.');
exit;
}
update_option('wpels_settings', $param);
try {
if (!\MegumiTeam\WooCommerceElasticsearch\Loader::get_instance()->data_sync()) {
WP_CLI::error('Elasticsearch built index failed.');
}
} catch (Exception $e) {
WP_CLI::error($e->getMessage());
exit;
}
WP_CLI::success("Elasticsearch built index completed.");
}
示例8: check_themes
function check_themes()
{
$themes = get_theme_updates();
if (!empty($themes)) {
WP_CLI::success('Theme updates refreshed.');
} else {
WP_CLI::log('Themes are up to date.');
}
}
示例9: __invoke
/**
* List plugins a user has favorited in the WordPress.org plugins directory.
*
* ## OPTIONS
*
* <user>
* : The username of the wordpress.org account whose favorite plugins you are listing.
*
* [--slug]
* : Only return plugin slugs. Can be combined with `wp plugin install` (see examples).
*
* [--verbose]
* : Display more information about the plugins.
*
* ## EXAMPLES
*
* wp plugin favorites matt
* wp plugin favorites matt --verbose
* wp plugin favorites matt --slug | xargs wp plugin install --activate
* wp plugin favorites matt --slug | grep -vwE "(hello-dolly|bbpress)" | xargs wp plugin install --activate
*
* @synopsis <user> [--slug] [--verbose]
*/
public function __invoke($args, $assoc_args)
{
// prepare variables
list($user) = $args;
extract($assoc_args = wp_parse_args($assoc_args, array('slug' => false, 'verbose' => false)));
// get access to plugins_api
require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
// query wordpress.org
$api = plugins_api('query_plugins', array('user' => $user, 'fields' => array('last_updated' => true, 'active_installs' => true)));
// only return slug?
if ($slug) {
foreach ($api->plugins as $plugin) {
WP_CLI::log($plugin->slug);
}
return;
}
// get table columns
$props = array('name', 'last_updated', 'rating', 'num_ratings', 'active_installs');
if ($verbose) {
$props = array_merge($props, array('author', 'version', 'requires', 'tested', 'short_description'));
}
// pull object properties into an array
$plugins = array();
foreach ($api->plugins as $plugin) {
$args = array();
foreach ($props as $prop) {
$args[$prop] = '';
if (isset($plugin->{$prop})) {
$args[$prop] = $plugin->{$prop};
// clean up some fields for output
switch ($prop) {
case 'rating':
$args[$prop] = (int) $args['rating'] / 100 * 5 . '/5';
break;
case 'author':
$args[$prop] = strip_tags($args['author']);
break;
case 'last_updated':
$args[$prop] = date('Y-m-d', strtotime($args['last_updated']));
break;
case 'active_installs':
$args[$prop] = number_format($args['active_installs']);
break;
}
}
}
$plugins[$plugin->slug] = $args;
}
if (!$plugins) {
WP_CLI::log('No favorite plugins found.');
return;
}
// output as list table
$formatter = new \WP_CLI\Formatter($assoc_args, $props, 'plugin');
$formatter->display_items($plugins);
}
示例10: list_
/**
* List all cron events that are due now.
*
* ## EXAMPLES
*
* wp cron event wppaas list
*
* @subcommand list
*/
public function list_($args, $assoc_args)
{
$events = static::get_cron_events_due_now();
if (!is_wp_error($events) && $events) {
$hooks = wp_list_pluck($events, 'hook');
WP_CLI::log(sprintf('There are %d cron event(s) due now: %s', count($hooks), implode(', ', $hooks)));
return;
}
WP_CLI::warning('No cron events are due.');
}
示例11: working
/**
* See Status of a working job
*
* @param $args
* @param $assoc_args
*/
public function working($args, $assoc_args)
{
$job_object = BackWPup_Job::get_working_data();
if (!is_object($job_object)) {
WP_CLI::error(__('No job running', 'backwpup'));
}
$formatter_args = array('format' => 'table', 'fields' => array('JobID', 'Name', 'Warnings', 'Errors', 'On Step', 'Done'), 'field' => NULL);
$formatter = new WP_CLI\Formatter($formatter_args);
$items = array();
$items[] = array('JobID' => $job_object->job['jobid'], 'Name' => $job_object->job['name'], 'Warnings' => $job_object->warnings, 'Errors' => $job_object->errors, 'On Step' => $job_object->steps_data[$job_object->step_working]['NAME'], 'Done' => $job_object->step_percent . ' / ' . $job_object->substep_percent, 'Last message' => str_replace('…', '...', strip_tags($job_object->lastmsg)));
$formatter->display_items($items);
WP_CLI::log('Last Message: ' . str_replace('…', '...', strip_tags($job_object->lastmsg)));
}
示例12: __invoke
/**
* Export content to a WXR file.
*
* ## OPTIONS
*
* [--dir=<dirname>]
* : Full path to directory where WXR export files should be stored. Defaults
* to current working directory.
*
* [--skip_comments]
* : Don't export comments.
*
* [--max_file_size=<MB>]
* : A single export file should have this many megabytes.
*
* ## FILTERS
*
* [--start_date=<date>]
* : Export only posts newer than this date, in format YYYY-MM-DD.
*
* [--end_date=<date>]
* : Export only posts older than this date, in format YYYY-MM-DD.
*
* [--post_type=<post-type>]
* : Export only posts with this post_type.
*
* [--post__in=<pid>]
* : Export all posts specified as a comma-separated list of IDs.
*
* [--author=<author>]
* : Export only posts by this author. Can be either user login or user ID.
*
* [--category=<name>]
* : Export only posts in this category.
*
* [--post_status=<status>]
* : Export only posts with this status.
*
* ## EXAMPLES
*
* wp export --dir=/tmp/ --user=admin --post_type=post --start_date=2011-01-01 --end_date=2011-12-31
*
* wp export --dir=/tmp/ --post__in=123,124,125
*/
public function __invoke($_, $assoc_args)
{
$defaults = array('dir' => NULL, 'start_date' => NULL, 'end_date' => NULL, 'post_type' => NULL, 'author' => NULL, 'category' => NULL, 'post_status' => NULL, 'post__in' => NULL, 'skip_comments' => NULL, 'max_file_size' => 15);
$this->validate_args(wp_parse_args($assoc_args, $defaults));
if (!function_exists('wp_export')) {
self::load_export_api();
}
WP_CLI::log('Starting export process...');
add_action('wp_export_new_file', function ($file_path) {
WP_CLI::log(sprintf("Writing to file %s", $file_path));
});
wp_export(array('filters' => $this->export_args, 'writer' => 'WP_Export_Split_Files_Writer', 'writer_args' => array('max_file_size' => $this->max_file_size * MB_IN_BYTES, 'destination_directory' => $this->wxr_path, 'filename_template' => self::get_filename_template())));
WP_CLI::success('All done with export.');
}
示例13: log
/**
* @param string $level
* @param string $message
* @param array $context
*
* @return void
*/
public function log($level, $message, array $context = array())
{
switch ($level) {
case LogLevel::WARNING:
\WP_CLI::warning($message);
break;
case LogLevel::ERROR:
case LogLevel::ALERT:
case LogLevel::EMERGENCY:
case LogLevel::CRITICAL:
\WP_CLI::error($message);
break;
default:
\WP_CLI::log($message);
}
}
示例14: __invoke
public function __invoke()
{
$sets = GP::$translation_set->all();
foreach ($sets as $set) {
/* translators: %d: Set ID */
WP_CLI::log(sprintf(__('Processing set #%d..', 'glotpress'), $set->id));
$translations = GP::$translation->find(array('translation_set_id' => $set->id, 'status' => 'current'), 'original_id ASC');
$prev_original_id = null;
foreach ($translations as $translation) {
if ($translation->original_id == $prev_original_id) {
WP_CLI::warning(sprintf(__('Duplicate with original_id #%1$d. Translation #%2$d', 'glotpress'), $prev_original_id, $translation->id));
$translation->delete();
}
$prev_original_id = $translation->original_id;
}
}
WP_CLI::success('Multiple currents are cleaned up.');
}
示例15: feedback
function feedback($string)
{
if (isset($this->upgrader->strings[$string])) {
$string = $this->upgrader->strings[$string];
}
if (strpos($string, '%') !== false) {
$args = func_get_args();
$args = array_splice($args, 1);
if (!empty($args)) {
$string = vsprintf($string, $args);
}
}
if (empty($string)) {
return;
}
$string = str_replace('…', '...', strip_tags($string));
\WP_CLI::log($string);
}