/** * Get a list of posts. * * @subcommand list * @synopsis [--<field>=<value>] [--ids] */ public function _list($_, $assoc_args) { $query_args = array('posts_per_page' => -1); foreach ($assoc_args as $key => $value) { if (true === $value) { continue; } $query_args[$key] = $value; } if (isset($assoc_args['ids'])) { $query_args['fields'] = 'ids'; } $query = new WP_Query($query_args); if (isset($assoc_args['ids'])) { WP_CLI::out(implode(' ', $query->posts)); } else { $fields = array('ID', 'post_title', 'post_name', 'post_date'); $table = new \cli\Table(); $table->setHeaders($fields); foreach ($query->posts as $post) { $line = array(); foreach ($fields as $field) { $line[] = $post->{$field}; } $table->addRow($line); } $table->display(); } }
/** * List users. * * @subcommand list * @synopsis [--role=<role>] [--ids] */ public function _list($args, $assoc_args) { global $blog_id; $params = array('blog_id' => $blog_id, 'fields' => isset($assoc_args['ids']) ? 'ids' : 'all_with_meta'); if (array_key_exists('role', $assoc_args)) { $params['role'] = $assoc_args['role']; } $users = get_users($params); if (isset($assoc_args['ids'])) { WP_CLI::out(implode(' ', $users)); return; } $fields = array('ID', 'user_login', 'display_name', 'user_email', 'user_registered'); $table = new \cli\Table(); $table->setHeaders(array_merge($fields, array('roles'))); foreach ($users as $user) { $line = array(); foreach ($fields as $field) { $line[] = $user->{$field}; } $line[] = implode(',', $user->roles); $table->addRow($line); } $table->display(); WP_CLI::line('Total: ' . count($users) . ' users'); }
private function single_command_help($name, $command) { WP_CLI::out(' wp ' . $name); $methods = WP_CLI_Command::get_subcommands($command); if (!empty($methods)) { WP_CLI::out(' [' . implode('|', $methods) . ']'); } WP_CLI::line(' ...'); }
private static function confirm($question, $assoc_args) { if (!isset($assoc_args['yes'])) { WP_CLI::out($question . " [y/n] "); $answer = trim(fgets(STDIN)); if ('y' != $answer) { exit; } } }
/** * Removes all tables from the database. */ function reset($args, $assoc_args) { if (!isset($assoc_args['yes'])) { WP_CLI::out("Are you sure you want to reset the database? [y/n] "); $answer = trim(fgets(STDIN)); if ('y' != $answer) { return; } } WP_CLI::launch(self::create_cmd('mysql --host=%s --user=%s --password=%s --execute=%s', DB_HOST, DB_USER, DB_PASSWORD, 'DROP DATABASE IF EXISTS ' . DB_NAME)); WP_CLI::launch(self::create_cmd('mysql --host=%s --user=%s --password=%s --execute=%s', DB_HOST, DB_USER, DB_PASSWORD, 'CREATE DATABASE ' . DB_NAME)); WP_CLI::success("Database reset."); }
/** * Print string * * @param string $string */ protected static function o($string) { \WP_CLI::out($string); }
/** * Pull sharedcount for all posts * * ## OPTIONS * * [--parallelize=<n_of_m>] * : Process only every N of M attachments, formatted as N/M. Useful for doing large batches quickly, optimizing the CPU. Wrap in a shell script to iterate 0 < N < M and reap the benefits. * */ function fetch_counts($args, $assoc_args = array()) { // Clean the output buffer. Not sure why this exists. ob_end_clean(); // Either every single post... $every_n = 0; $of_m = 1; // Or a set of them to parallelize pages if (!empty($assoc_args['parallelize']) && preg_match('#^(\\d+)/(\\d+)$#', $assoc_args['parallelize'], $matches)) { $every_n = (int) $matches[1]; $of_m = (int) $matches[2]; } $query_args = array('meta_query' => array('relation' => 'OR', array('key' => 'sharedcount_updated', 'type' => 'DATETIME', 'value' => gmdate('Y-m-d H:i:s', time() - 3600), 'compare' => '<'), array('key' => 'sharedcount_updated', 'compare' => 'NOT EXISTS')), 'posts_per_page' => 50, 'paged' => $every_n); do { $query = new WP_Query(apply_filters('sharedcount_update_query', $query_args)); foreach ($query->posts as $post) { $current = (int) get_post_meta($post->ID, 'sharedcount_count', true); WP_CLI::out("Getting sharecount for {$post->ID} `{$post->post_title}` (current: {$current}) ... "); $counts = SharedCount::get_counts(get_permalink($post)); if (!empty($counts)) { $count = (int) $counts['total']; WP_CLI::out($count . '... '); if ($current < $count) { WP_CLI::out(' Updating'); update_post_meta($post->ID, 'sharedcount_count', $count); } } WP_CLI::line(); update_post_meta($post->ID, 'sharedcount_updated', current_time('mysql', true)); } $query_args['paged'] += $of_m; } while ($query->max_num_pages > $query_args['paged']); }
/** * Prompt for some input from the user */ private function prompt($message) { WP_CLI::out(trim($message) . " "); return trim(fgets(STDIN)); }