Example #1
0
 /**
  * Reset sidebar.
  *
  * Removes all widgets from the sidebar and places them in Inactive Widgets.
  *
  * ## OPTIONS
  *
  * [<sidebar-id>...]
  * : One or more sidebars to reset.
  *
  * [--all]
  * : If set, all sidebars will be reset.
  *
  * ## EXAMPLES
  *
  *     # Reset a sidebar
  *     $ wp widget reset sidebar-1
  *     Success: Sidebar 'sidebar-1' reset.
  *
  *     # Reset multiple sidebars
  *     $ wp widget reset sidebar-1 sidebar-2
  *     Success: Sidebar 'sidebar-1' reset.
  *     Success: Sidebar 'sidebar-2' reset.
  *
  *     # Reset all sidebars
  *     $ wp widget reset --all
  *     Success: Sidebar 'sidebar-1' reset.
  *     Success: Sidebar 'sidebar-2' reset.
  *     Success: Sidebar 'sidebar-3' reset.
  */
 public function reset($args, $assoc_args)
 {
     global $wp_registered_sidebars;
     $all = \WP_CLI\Utils\get_flag_value($assoc_args, 'all', false);
     // Bail if no arguments and no all flag.
     if (!$all && empty($args)) {
         WP_CLI::error('Please specify one or more sidebars, or use --all.');
     }
     // Fetch all sidebars if all flag is set.
     if ($all) {
         $args = array_keys($wp_registered_sidebars);
     }
     // Sidebar ID wp_inactive_widgets is reserved by WP core for inactive widgets.
     if (isset($args['wp_inactive_widgets'])) {
         unset($args['wp_inactive_widgets']);
     }
     // Check if no registered sidebar.
     if (empty($args)) {
         WP_CLI::error('No sidebar registered.');
     }
     $count = $errors = 0;
     foreach ($args as $sidebar_id) {
         if (!array_key_exists($sidebar_id, $wp_registered_sidebars)) {
             WP_CLI::warning(sprintf('Invalid sidebar: %s', $sidebar_id));
             $errors++;
             continue;
         }
         $widgets = $this->get_sidebar_widgets($sidebar_id);
         if (empty($widgets)) {
             WP_CLI::warning(sprintf("Sidebar '%s' is already empty.", $sidebar_id));
         } else {
             foreach ($widgets as $widget) {
                 $widget_id = $widget->id;
                 list($name, $option_index, $new_sidebar_id, $sidebar_index) = $this->get_widget_data($widget_id);
                 $this->move_sidebar_widget($widget_id, $new_sidebar_id, 'wp_inactive_widgets', $sidebar_index, 0);
             }
             WP_CLI::log(sprintf("Sidebar '%s' reset.", $sidebar_id));
             $count++;
         }
     }
     Utils\report_batch_operation_results('sidebar', 'reset', count($args), $count, $errors);
 }
Example #2
0
 /**
  * Create attachments from local files or URLs.
  *
  * ## OPTIONS
  *
  * <file>...
  * : Path to file or files to be imported. Supports the glob(3) capabilities of the current shell.
  *     If file is recognized as a URL (for example, with a scheme of http or ftp), the file will be
  *     downloaded to a temp file before being sideloaded.
  *
  * [--post_id=<post_id>]
  * : ID of the post to attach the imported files to.
  *
  * [--title=<title>]
  * : Attachment title (post title field).
  *
  * [--caption=<caption>]
  * : Caption for attachent (post excerpt field).
  *
  * [--alt=<alt_text>]
  * : Alt text for image (saved as post meta).
  *
  * [--desc=<description>]
  * : "Description" field (post content) of attachment post.
  *
  * [--featured_image]
  * : If set, set the imported image as the Featured Image of the post its attached to.
  *
  * [--porcelain]
  * : Output just the new attachment ID.
  *
  * ## EXAMPLES
  *
  *     # Import all jpgs in the current user's "Pictures" directory, not attached to any post.
  *     $ wp media import ~/Pictures/**\/*.jpg
  *     Imported file '/home/person/Pictures/beautiful-youg-girl-in-ivy.jpg' as attachment ID 1751.
  *     Imported file '/home/person/Pictures/fashion-girl.jpg' as attachment ID 1752.
  *     Success: Imported 2 of 2 images.
  *
  *     # Import a local image and set it to be the post thumbnail for a post.
  *     $ wp media import ~/Downloads/image.png --post_id=123 --title="A downloaded picture" --featured_image
  *     Imported file '/home/person/Downloads/image.png' as attachment ID 1753 and attached to post 123 as featured image.
  *     Success: Imported 1 of 1 images.
  *
  *     # Import a local image, but set it as the featured image for all posts.
  *     # 1. Import the image and get its attachment ID.
  *     # 2. Assign the attachment ID as the featured image for all posts.
  *     $ ATTACHMENT_ID="$(wp media import ~/Downloads/image.png --porcelain)"
  *     $ wp post list --post_type=post --format=ids | xargs -d ' ' -I % wp post meta add % _thumbnail_id $ATTACHMENT_ID
  *     Success: Added custom field.
  *     Success: Added custom field.
  *
  *     # Import an image from the web.
  *     $ wp media import http://s.wordpress.org/style/images/wp-header-logo.png --title='The WordPress logo' --alt="Semantic personal publishing"
  *     Imported file 'http://s.wordpress.org/style/images/wp-header-logo.png' as attachment ID 1755.
  *     Success: Imported 1 of 1 images.
  */
 function import($args, $assoc_args = array())
 {
     $assoc_args = wp_parse_args($assoc_args, array('title' => '', 'caption' => '', 'alt' => '', 'desc' => ''));
     if (isset($assoc_args['post_id'])) {
         if (!get_post($assoc_args['post_id'])) {
             WP_CLI::warning("Invalid --post_id");
             $assoc_args['post_id'] = false;
         }
     } else {
         $assoc_args['post_id'] = false;
     }
     $successes = $errors = 0;
     foreach ($args as $file) {
         $is_file_remote = parse_url($file, PHP_URL_HOST);
         $orig_filename = $file;
         if (empty($is_file_remote)) {
             if (!file_exists($file)) {
                 WP_CLI::warning("Unable to import file '{$file}'. Reason: File doesn't exist.");
                 $errors++;
                 break;
             }
             $tempfile = $this->make_copy($file);
         } else {
             $tempfile = download_url($file);
         }
         $file_array = array('tmp_name' => $tempfile, 'name' => basename($file));
         $post_array = array('post_title' => $assoc_args['title'], 'post_excerpt' => $assoc_args['caption'], 'post_content' => $assoc_args['desc']);
         $post_array = wp_slash($post_array);
         // use image exif/iptc data for title and caption defaults if possible
         if (empty($post_array['post_title']) || empty($post_array['post_excerpt'])) {
             // @codingStandardsIgnoreStart
             $image_meta = @wp_read_image_metadata($tempfile);
             // @codingStandardsIgnoreEnd
             if (!empty($image_meta)) {
                 if (empty($post_array['post_title']) && trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
                     $post_array['post_title'] = $image_meta['title'];
                 }
                 if (empty($post_array['post_excerpt']) && trim($image_meta['caption'])) {
                     $post_array['post_excerpt'] = $image_meta['caption'];
                 }
             }
         }
         if (empty($post_array['post_title'])) {
             $post_array['post_title'] = preg_replace('/\\.[^.]+$/', '', basename($file));
         }
         // Deletes the temporary file.
         $success = media_handle_sideload($file_array, $assoc_args['post_id'], $assoc_args['title'], $post_array);
         if (is_wp_error($success)) {
             WP_CLI::warning(sprintf("Unable to import file '%s'. Reason: %s", $orig_filename, implode(', ', $success->get_error_messages())));
             $errors++;
             continue;
         }
         // Set alt text
         if ($assoc_args['alt']) {
             update_post_meta($success, '_wp_attachment_image_alt', wp_slash($assoc_args['alt']));
         }
         // Set as featured image, if --post_id and --featured_image are set
         if ($assoc_args['post_id'] && \WP_CLI\Utils\get_flag_value($assoc_args, 'featured_image')) {
             update_post_meta($assoc_args['post_id'], '_thumbnail_id', $success);
         }
         $attachment_success_text = '';
         if ($assoc_args['post_id']) {
             $attachment_success_text = " and attached to post {$assoc_args['post_id']}";
             if (\WP_CLI\Utils\get_flag_value($assoc_args, 'featured_image')) {
                 $attachment_success_text .= ' as featured image';
             }
         }
         if (\WP_CLI\Utils\get_flag_value($assoc_args, 'porcelain')) {
             WP_CLI::line($success);
         } else {
             WP_CLI::log(sprintf("Imported file '%s' as attachment ID %d%s.", $orig_filename, $success, $attachment_success_text));
         }
         $successes++;
     }
     if (!Utils\get_flag_value($assoc_args, 'porcelain')) {
         Utils\report_batch_operation_results('image', 'import', count($args), $successes, $errors);
     }
 }
Example #3
0
 /**
  * Delete one or more items from a menu.
  *
  * ## OPTIONS
  *
  * <db-id>...
  * : Database ID for the menu item(s).
  *
  * ## EXAMPLES
  *
  *     $ wp menu item delete 45
  *     Success: 1 menu item deleted.
  *
  * @subcommand delete
  */
 public function delete($args, $_)
 {
     global $wpdb;
     $count = $errors = 0;
     foreach ($args as $arg) {
         $parent_menu_id = (int) get_post_meta($arg, '_menu_item_menu_item_parent', true);
         $ret = wp_delete_post($arg, true);
         if (!$ret) {
             WP_CLI::warning("Couldn't delete menu item {$arg}.");
             $errors++;
         } else {
             if ($parent_menu_id) {
                 $children = $wpdb->get_results($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key='_menu_item_menu_item_parent' AND meta_value=%s", (int) $arg));
                 if ($children) {
                     $children_query = $wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_value = %d WHERE meta_key = '_menu_item_menu_item_parent' AND meta_value=%s", $parent_menu_id, (int) $arg);
                     $wpdb->query($children_query);
                     foreach ($children as $child) {
                         clean_post_cache($child);
                     }
                 }
             }
         }
         if (false != $ret) {
             $count++;
         }
     }
     Utils\report_batch_operation_results('menu item', 'delete', count($args), $count, $errors);
 }
Example #4
0
 /**
  * Delete an existing term.
  *
  * Errors if the term doesn't exist, or there was a problem in deleting it.
  *
  * ## OPTIONS
  *
  * <taxonomy>
  * : Taxonomy of the term to delete.
  *
  * <term-id>...
  * : One or more IDs of terms to delete.
  *
  * ## EXAMPLES
  *
  *     # Delete post category
  *     $ wp term delete category 15
  *     Deleted category 15.
  *     Success: Deleted 1 of 1 terms.
  *
  *     # Delete all post tags
  *     $ wp term list post_tag --field=term_id | xargs wp term delete post_tag
  *     Deleted post_tag 159.
  *     Deleted post_tag 160.
  *     Deleted post_tag 161.
  *     Success: Deleted 3 of 3 terms.
  */
 public function delete($args)
 {
     $taxonomy = array_shift($args);
     $successes = $errors = 0;
     foreach ($args as $term_id) {
         $ret = wp_delete_term($term_id, $taxonomy);
         if (is_wp_error($ret)) {
             WP_CLI::warning($ret);
             $errors++;
         } else {
             if ($ret) {
                 WP_CLI::log(sprintf("Deleted %s %d.", $taxonomy, $term_id));
                 $successes++;
             } else {
                 WP_CLI::warning(sprintf("%s %d doesn't exist.", $taxonomy, $term_id));
             }
         }
     }
     Utils\report_batch_operation_results('term', 'delete', count($args), $successes, $errors);
 }
Example #5
0
 /**
  * Delete plugin files without deactivating or uninstalling.
  *
  * ## OPTIONS
  *
  * <plugin>...
  * : One or more plugins to delete.
  *
  * ## EXAMPLES
  *
  *     # Delete plugin
  *     $ wp plugin delete hello
  *     Deleted 'hello' plugin.
  *     Success: Deleted 1 of 1 plugins.
  *
  *     # Delete inactive plugins
  *     $ wp plugin delete $(wp plugin list --status=inactive --field=name)
  *     Deleted 'tinymce-templates' plugin.
  *     Success: Deleted 1 of 1 plugins.
  */
 public function delete($args, $assoc_args = array())
 {
     $successes = $errors = 0;
     foreach ($this->fetcher->get_many($args) as $plugin) {
         if ($this->_delete($plugin)) {
             WP_CLI::log("Deleted '{$plugin->name}' plugin.");
             $successes++;
         } else {
             $errors++;
         }
     }
     if (!$this->chained_command) {
         Utils\report_batch_operation_results('plugin', 'delete', count($args), $successes, $errors);
     }
 }
Example #6
0
 /**
  * Delete one or more menus.
  *
  * ## OPTIONS
  *
  * <menu>...
  * : The name, slug, or term ID for the menu(s).
  *
  * ## EXAMPLES
  *
  *     $ wp menu delete "My Menu"
  *     Success: 1 menu deleted.
  */
 public function delete($args, $_)
 {
     $count = $errors = 0;
     foreach ($args as $arg) {
         $ret = wp_delete_nav_menu($arg);
         if (!$ret || is_wp_error($ret)) {
             WP_CLI::warning("Couldn't delete menu '{$arg}'.");
             $errors++;
         } else {
             WP_CLI::log("Deleted menu '{$arg}'.");
             $count++;
         }
     }
     Utils\report_batch_operation_results('menu', 'delete', count($args), $count, $errors);
 }
Example #7
0
 /**
  * Delete a theme.
  *
  * Removes the theme from the filesystem.
  *
  * ## OPTIONS
  *
  * <theme>...
  * : One or more themes to delete.
  *
  * ## EXAMPLES
  *
  *     $ wp theme delete twentytwelve
  *     Deleted 'twentytwelve' theme.
  *     Success: Deleted 1 of 1 themes.
  *
  * @alias uninstall
  */
 public function delete($args)
 {
     $successes = $errors = 0;
     foreach ($this->fetcher->get_many($args) as $theme) {
         $theme_slug = $theme->get_stylesheet();
         if ($this->is_active_theme($theme)) {
             WP_CLI::warning("Can't delete the currently active theme: {$theme_slug}");
             $errors++;
             continue;
         }
         $r = delete_theme($theme_slug);
         if (is_wp_error($r)) {
             WP_CLI::warning($r);
             $errors++;
         } else {
             WP_CLI::log("Deleted '{$theme_slug}' theme.");
             $successes++;
         }
     }
     if (!$this->chained_command) {
         Utils\report_batch_operation_results('theme', 'delete', count($args), $successes, $errors);
     }
 }