/** * 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); }
/** * Cache posts in batches. * Sleep between batches. * * @param array $data Arguments to cache related posts for. * @param integer $batch Batch number. * @param array $post_ids Array with post ids to cache related posts for. * @return void */ function km_rpbtc_cache_related_posts($data, $batch = 50, $post_ids = array(), $notify = false, $sleep = 0) { $plugin = km_rpbt_plugin(); if (!$plugin || empty($post_ids)) { return; } $sanitized_data = $plugin->cache->sanitize_cache_args($data); $i = 0; foreach ($post_ids as $post_id) { $widget = $shortcode = false; $sanitized_data['post_id'] = $post_id; $shortcode_data = km_rpbtc_apply_filters('shortcode', $data, $post_id); $shortcode_data_s = $plugin->cache->sanitize_cache_args($shortcode_data); $widget_data = km_rpbtc_apply_filters('widget', $data, $post_id); $widget_data_s = $plugin->cache->sanitize_cache_args($widget_data); if ($shortcode_data_s != $sanitized_data) { // Shortcode args was filtered $shortcode = true; $plugin->cache->get_related_posts($shortcode_data); $i = km_rpbtc_sleep(++$i, $batch); } if ($widget_data_s != $sanitized_data) { // Widget args was filtered $widget = true; if ($shortcode) { // Don't cache widget if it has the same $args as the shortcode if ($widget_data_s != $shortcode_data_s) { $plugin->cache->get_related_posts($widget_data); $i = km_rpbtc_sleep(++$i, $batch); } } else { $plugin->cache->get_related_posts($widget_data); $i = km_rpbtc_sleep(++$i, $batch); } } if (!($widget && $shortcode)) { // Widget AND shortcode not filtered. $plugin->cache->get_related_posts($sanitized_data); $i = km_rpbtc_sleep(++$i, $batch); } // wp-cli command if ($notify) { $notify->tick(); if ($sleep && $batch === $i) { sleep($sleep); } } } }
/** * Admin page output. */ function km_rpbt_cache_admin() { echo '<div class="wrap rpbt_cache">'; echo '<h1>' . __('Related Posts by Taxonomy Cache', 'rpbt-cache') . '</h1>'; $plugin = false; if (function_exists('km_rpbt_plugin')) { $plugin = km_rpbt_plugin(); } if (!$plugin instanceof Related_Posts_By_Taxonomy_Defaults) { $error = __('Related Posts by Taxonomy plugin is not installed or activated!', 'rpbt-cache'); echo '<div class="error"><p>' . $error . '</p></div></div>'; return; } $cache_exists = $plugin->cache instanceof Related_Posts_By_Taxonomy_Cache; // Check if cache is enabled. if (!$cache_exists || !class_exists('Related_Posts_By_Taxonomy_Cache')) { $error = __('The cache for the Related Posts by Taxonomy plugin is not enabled!', 'rpbt-cache'); echo '<div class="error"><p>' . $error . '</p></div></div>'; return; } $cache = $plugin->cache; $taxonomies = implode(', ', array_keys($plugin->taxonomies)); $post_types = implode(', ', array_keys($plugin->post_types)); // Description for the settings page form fields. $desc = array('order' => __('DESC, ASC, or RAND. Default: DESC', 'rpbt-cache'), 'taxonomies' => sprintf(__("'%s' or comma separated list of taxonomies", 'rpbt-cache'), $plugin->all_tax) . '<br/>' . sprintf(__("Available taxonomies: %s", 'rpbt-cache'), $taxonomies) . '<br/>' . sprintf(__("Default: %s (all existing taxonomies)", 'rpbt-cache'), $plugin->all_tax), 'batch' => __('How many posts to cache in batches. Default: 50', 'rpbt-cache'), 'total' => __('Amount of posts to cache. Default: -1 (cache all posts)', 'rpbt-cache'), 'post_types' => __("Comma separated list of post types", 'rpbt-cache') . '<br/>' . sprintf(__("Available post types: %s", 'rpbt-cache'), $post_types) . '<br/>' . __("Default: post", 'rpbt-cache'), 'limit_posts' => __('Default: -1 (don\'t limit posts)', 'rpbt-cache'), 'limit_year' => __('Default: empty or 0 (don\'t limit by years)', 'rpbt-cache'), 'limit_month' => __('Default: empty or 0 (don\'t limit by months)', 'rpbt-cache'), 'orderby' => __('post_date or post_modified. Default: post_date', 'rpbt-cache'), 'post_thumbnail' => __('boolean: 1 or 0. Default: 0', 'rpbt-cache'), 'related' => __('boolean: 1 or 0. Default: 1', 'rpbt-cache'), 'exclude_terms' => __('Comma separated list of term ids. Default: empty.', 'rpbt-cache'), 'include_terms' => __('Comma separated list of term ids. Default: empty.', 'rpbt-cache'), 'exclude_posts' => __('Comma separated list of post ids. Default: empty.', 'rpbt-cache'), 'posts_per_page' => __('Default 5.', 'rpbt-cache')); $fields = array('total' => -1, 'batch' => 50, 'taxonomies' => $taxonomies); $defaults = km_rpbt_get_default_args(); // Not used by the widget or shortcode unset($defaults['fields']); $fields = array_merge($fields, $defaults); $get_option = false; if ('POST' === $_SERVER['REQUEST_METHOD']) { if (isset($_POST['cache_posts'])) { check_admin_referer('rpbt_cache_nonce_ajax'); echo '<div class="notice"><p>' . __('Please enable Javascript to cache posts', 'rpbt-cache') . '</p></div>'; return; } $_POST = stripslashes_deep($_POST); $post_args = wp_parse_args($_POST, $fields); $fields = array_intersect_key($post_args, $fields); $get_option = isset($_POST['flush_cache']) ? true : false; } else { $get_option = true; } // Get the settings from the database. if ($get_option) { $option = get_option('rpbt_related_posts_cache_args'); if (!empty($option) && is_array($option)) { unset($option['post_id'], $option['count'], $option['fields']); $fields = array_merge($fields, $option); } } foreach (array('related', 'post_thumbnail') as $field) { $fields[$field] = $fields[$field] ? 1 : 0; } // Start output echo '<p>' . __('Flush the cache or cache related posts in batches.', 'rpbt-cache') . '</p>'; settings_errors(); // Add notice if cache was flushed. if (isset($_POST['flush_cache'])) { check_admin_referer('rpbt_cache_nonce_flush'); $cache->flush_cache(); echo '<div class="updated"><p>' . __('Flushed the Cache', 'rpbt-cache') . '</p></div>'; } echo '<div id="rpbt_cache_forms" aria-hidden="false">'; echo '<h3>' . __('Flush Cache', 'rpbt-cache') . '</h3>'; echo '<p>' . __('Flush the cache manually', 'rpbt-cache') . '</p>'; echo '<form method="post" action="" style="border-bottom: 1px solid #dedede">'; wp_nonce_field('rpbt_cache_nonce_flush'); echo '<input type="hidden" name="flush_cache" value="1" />'; submit_button(__('Flush Cache!', 'rpbt-cache')); echo '</form>'; $version = km_rpbt_cache_get_plugin_version(); if (RELATED_POSTS_BY_TAXONOMY_CACHE_VERSION !== $version) { $error = __('Please update this plugin to the same version as the Related Posts By Taxonomy plugin to cache posts in batches.', 'rpbt-cache'); echo '<div class="plugin-error"><p>' . $error . '</p></div></div></div>'; return; } echo '<h3>' . __('Cache Parameters', 'rpbt-cache') . '</h3>'; echo '<form method="post" action="" id="cache_form">'; wp_nonce_field('rpbt_cache_nonce_ajax'); echo "<table class='form-table'>"; // Form field output foreach ($fields as $key => $value) { $value = esc_attr($value); echo "<tr valign='top'><th scope='row'>{$key}</th>"; echo "<td><input class='regular-text' type='text' name='{$key}' value='{$value}'>"; if (isset($desc[$key])) { echo '<p class="description">' . $desc[$key] . '</p>'; } echo "</td></tr>"; if ('batch' === $key) { echo '</table>'; echo '<h3>' . __('Widget and Shortcode Settings', 'rpbt-cache') . '</h3>'; echo "<table class='form-table'>"; } } // Not used by the widget or shortcode echo '<input type="hidden" name="fields" value="" />'; echo "</table>\n"; echo '<p class="submit">'; echo '<input id="cache_posts" class="button button-primary" type="submit" '; echo 'aria-expanded="false" aria-controls="rpbt_cache_progress_bar_container" '; echo 'value="' . __('Cache Related Posts!', 'rpbt-cache') . '" name="cache_posts"></p>'; echo '</form>'; echo '</div>'; echo '</div>'; }