/** * Cache posts in batches. * * ## OPTIONS * * <number> * : How many posts to cache Default none. Choose a number or use 'all'. * * [--batch=<number>] * : How many posts to cache in each batch. Default: 50 (-1 to not cache in batches) * * [--sleep=<number>] * : How many seconds of sleep after a batch. Default: 6 (set to 0 for no sleep) * * [--taxonomies=<taxonomies>] * : Comma separated list of taxonomies. Default: all (all registered taxonomies) * * [--post_types=<post-types>] * : post_types parameter. Comma separated list of post types. Default: post * * [--posts_per_page=<posts-per-page>] * : posts_per_page parameter. Default: 5 * * [--order=<order>] * : order parameter. Default DESC (DESC, ASC, or RAND) * * [--limit_posts=<limit-posts>] * : limit_posts parameter. Default: -1 (don't limit posts) * * [--limit_year=<limit-year>] * : limit_year parameter. Default: 0 (bool) * * [--limit_month=<limit-month>] * : limit_month parameter. Default: 0 (bool) * * [--orderby=<orderby>] * : orderby parameter. Default: post_date (post_date or post_modified) * * [--exclude_terms=<exclude-terms>] * : exclude_terms parameter. Comma separated list of term IDs. Default none * * [--include_terms=<include-terms>] * : include_terms parameter. Comma separated list of term IDs. Default none * * [--exclude_posts=<exclude-posts>] * : exclude_posts parameter. Comma separated list of post IDs. Default none * * [--post_thumbnail=<post-thumbnail>] * : post_thumbnail parameter. Default: 0 (bool) * * [--related=<related>] * : related parameter. Default: 1 (bool) * * ## EXAMPLES * * wp rpbt-cache cache 1000 --posts_per_page=9 */ public function cache($args, $assoc_args) { list($count) = $args; $plugin = km_rpbt_plugin(); $defaults = km_rpbt_get_default_args(); if (!$plugin) { WP_CLI::error('Error: Could not find plugin instance'); } $args = wp_parse_args($assoc_args, $defaults); if (!isset($args['taxonomies'])) { $args['taxonomies'] = $plugin->all_tax; } $post_types = explode(',', $args['post_types']); $args = km_rpbt_sanitize_args($args); $taxonomies = $args['taxonomies']; if ($post_types != $args['post_types']) { WP_CLI::error(sprintf("Error: invalid post type in post_types: %s.", implode(', ', $post_types))); } $post_type_count = km_rpbtc_get_post_types_count($args['post_types']); if (!$post_type_count) { WP_CLI::error(sprintf("Error: No posts found for post types: %s.", implode(', ', $args['post_types']))); } $count = 'all' === $count ? $post_type_count : absint($count); if (!$count) { WP_CLI::error("Error: please provide a valid number or 'all'"); } $count = $post_type_count >= $count ? $count : $post_type_count; $notify = \WP_CLI\Utils\make_progress_bar('Caching related posts', $count); $batch = 50; // default batch $sleep = isset($args['sleep']) ? absint($args['sleep']) : 2; if (isset($args['batch'])) { if (-1 === (int) $args['batch']) { $batch = $count; $sleep = 0; } else { $batch = absint($args['batch']); $batch = $batch ? $batch : 50; } } $batch = $batch > $count ? $count : $batch; //unset( $args['taxonomies'], $args['batch'], $args['sleep'] ); for ($i = 0; $i < $count; $i += $batch) { $_args = array('posts_per_page' => $batch, 'post_types' => $args['post_types'], 'offset' => $i, 'fields' => 'ids'); $post_ids = get_posts($_args); if (!empty($post_ids)) { // Cache related posts. km_rpbtc_cache_related_posts($args, $batch, $post_ids, $notify, $sleep); } } $notify->finish(); WP_CLI::success(sprintf("%s posts cached.", $count)); }
/** * Cache related posts in batches. * * Used by ajax action: rpbt_cache_posts. */ function km_rpbt_cache_posts() { check_ajax_referer('rpbt_cache_nonce_ajax', 'security'); $plugin = km_rpbt_plugin(); if (!$plugin) { wp_send_json_error(__('Plugin not activated', 'rpbt-cache')); } if (!(isset($_POST['data']) && $_POST['data'])) { wp_send_json_error(__('No form data', 'rpbt-cache')); } $post_data = $_POST['data']; $total = isset($post_data['total']) ? (int) $post_data['total'] : -1; $total = -1 === $total ? (int) $post_data['count'] : $total; $batch = isset($post_data['batch']) ? (int) $post_data['batch'] : 5; $offset = isset($post_data['offset']) ? (int) $post_data['offset'] : 0; $data['done'] = false; $data['form'] = $post_data; $taxonomies = isset($post_data['taxonomies']) ? $post_data['taxonomies'] : $plugin->all_tax; $form_data = array_merge(km_rpbt_get_default_args(), $_POST['data']); $args = array('posts_per_page' => $batch, 'post_type' => explode(',', $form_data['post_types']), 'fields' => 'ids'); // Add an offset to get a batch if it's not the first batch. if (0 !== $offset) { $args['offset'] = $offset; // Check if new batch exceeds the total posts to cache. if ($batch + $offset > $total) { $args['posts_per_page'] = $total - $offset; } } else { // Check if batch is smaller as total posts to cache. if ($batch >= $total) { $args['posts_per_page'] = $total; } } // Get the post ids to cache related posts for. $post_ids = get_posts($args); $data['cached'] = count($post_ids); // Check if cached posts has reached total posts to cache. if ($data['cached'] + $offset >= $total) { $data['done'] = true; } if (!empty($post_ids)) { // Cache related posts. km_rpbtc_cache_related_posts($form_data, $batch, $post_ids); } else { // No posts found for offset. $data['done'] = true; } wp_send_json_success($data); }