public function generate_completions() { $completions = $posts = array(); if (count($this->elements)) { $progress = \WP_CLI\Utils\make_progress_bar("Generating {$this->name} completions:", count($this->elements)); foreach ($this->elements as $post) { if (!isset($posts[$post->post_type])) { $posts[$post->post_type] = array(); } if (!in_array($post->post_title, $posts[$post->post_type])) { $posts[$post->post_type][] = $post->post_title; if ($completion = $this->generate_completion($post)) { $completions[] = $completion; $this->update_messages($post->ID); ++$this->count; } } $progress->tick(); } $progress->finish(); } return $completions; }
/** * Import the PHPDoc $data into WordPress posts and taxonomies * * @param array $data * @param bool $skip_sleep If true, the sleep() calls are skipped. * @param bool $import_ignored If true, functions marked `@ignore` will be imported * Disabled, not remove to prevent PHP Warning */ protected function _do_import(array $data, $skip_sleep = false, $import_ignored = false) { if (!wp_get_current_user()->exists()) { WP_CLI::error('Please specify a valid user: --user=<id|login>'); } /** * Delete posts before import * Warning!!! This filter takes a long time to complete, * * Default: false */ if (apply_filters('sublime_delete_import_before_create', false)) { global $wpdb; $posts_id = $wpdb->get_col("SELECT DISTINCT ID FROM {$wpdb->posts} WHERE post_type LIKE '%wp-parser-%'"); if ($posts_id) { $total_count = count($posts_id); $progress = \WP_CLI\Utils\make_progress_bar(sprintf('Deleting %s posts.', $total_count), $total_count); $deleted_with_error = array(); foreach ($posts_id as $count => $post_id) { if (!wp_delete_post((int) $post_id, true)) { $deleted_with_error[] = $post_id; } $progress->tick(); if (!$skip_sleep && 0 == $count % 10) { // TODO figure our why are we still doing this sleep(3); } } $progress->finish(); if ($deleted_with_error) { WP_CLI::error(sprintf('Not deleting %s of the %s. This import not continue, please try again.', count($deleted_with_error), $total_count)); } } } // Run the importer Importer::run($data, $skip_sleep); }
/** * @subcommand fix-external-attachments * @synopsis [--dry-run] [--verbose] [--post-id=<post-id>] */ public function fix_external_attachments($args, $args_assoc) { $args_assoc = wp_parse_args($args_assoc, array('dry-run' => false, 'verbose' => false)); $image_replacer = new Image_Replacer(); WP_CLI::line('Looking for posts...'); if (!empty($args_assoc['post-id'])) { $posts = explode(',', $args_assoc['post-id']); } else { $posts = get_posts(array('post_status' => 'any', 'fields' => 'ids', 'showposts' => -1)); } WP_CLI::line(sprintf('Found %d posts with.', $total = count($posts))); $progress = \WP_CLI\Utils\make_progress_bar('Searching for broken images', $total); $todo = array(); foreach ($posts as $post_id) { $post = get_post($post_id); $progress->tick(); $images = $image_replacer->get_external_attachments_from_string($post->post_content); if ($args_assoc['verbose']) { WP_CLI::line(sprintf('Found %d images in post.', count($images))); } $t = array('post' => $post, 'image' => array()); foreach ($images as $image) { if ($image['post'] = get_post($post_id)) { $t['image'][] = $image; if ($args_assoc['verbose']) { WP_CLI::line(sprintf('Image %s is missing.', $image['url'])); } } } $todo[] = $t; } $progress->finish(); $progress = \WP_CLI\Utils\make_progress_bar(sprintf('Fixing %d broken images', count($todo)), count($todo)); $posts_to_update = array(); foreach ($todo as $t) { $progress->tick(); foreach ($t['image'] as $image) { $new_url = wp_get_attachment_image_src($image['id'], $image['size'])[0]; if ($args_assoc['verbose']) { WP_CLI::line(sprintf('Missing image URL %s, replacing with local image %s', $image['url'], $new_url)); } $t['post']->post_content = str_replace($image['url'], $new_url, $t['post']->post_content); } if (!$args_assoc['dry-run']) { wp_update_post($t['post']); } } $progress->finish(); WP_CLI::success('Done.'); }