Skip to content

Commit

Permalink
Merge pull request #9 from level-level/adds-paged-progress-and-restar…
Browse files Browse the repository at this point in the history
…t-from-page

Adds paged progress and restart from page
  • Loading branch information
NielsdeBlaauw authored Apr 7, 2020
2 parents e6bc228 + e1bdfed commit b65d5f5
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 78 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Run indexing of FacetWP via WP-CLI

`wp help facet index`

`wp facet index [--post-type=post_type]`
`wp facet index [--post-type=post_type] [--start-from-page=number]`
166 changes: 89 additions & 77 deletions facetwp-wp-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,106 +18,118 @@
namespace WP_Facet;

if ( !defined ('WP_CLI') )
return;
return;

use WP_CLI;
use WP_CLI_Command;
use FacetWP_Indexer;
use WP_Query;

class CLI extends WP_CLI_Command {

/**
* Indexes all posts
*
* ## OPTIONS
* [--post-type=<name>]
* : post type, 'any' if not defined
*
* ## EXAMPLES
*
* wp facet index
* wp facet index --post-type=product
*
* @synopsis
*/
function index( $args, $assoc_args ) {

error_reporting(0);

if ( empty( $assoc_args['post-type'] ) )
$post_type = 'any';
else
$post_type = $assoc_args['post-type'];
/**
* Indexes all posts
*
* ## OPTIONS
* [--post-type=<name>]
* : post type, 'any' if not defined
*
* [--start-at-page=<number>]
* : pagenumber to start indexing, 1 if not defined
*
* ## EXAMPLES
*
* wp facet index
* wp facet index --post-type=product
* wp facet index --start-at-page=7
*
* @synopsis
*/
function index( $args, $assoc_args ) {
if ( ! defined( 'WP_IMPORTING' ) ) {
define('WP_IMPORTING', true);
}

$posts_per_page = 100;
$page = 1;
if( ! function_exists('FWP')){
WP_CLI::error( 'FacetWP plugin is not activated.' );
}

$args = array(
'posts_per_page' => $posts_per_page,
'paged' => $page,
'post_type' => $post_type,
'post_status' => 'publish',
'fields' => 'ids',
'orderby' => 'ID',
'cache_results' => false,
);
wp_suspend_cache_addition( true );

$total = 0;
$post_type = 'any';
if ( ! empty( $assoc_args['post-type'] ) ) {
$post_type = $assoc_args['post-type'];
}

do {
$page = 1;
if ( ! empty( $assoc_args['start-at-page'] ) ) {
$page = $assoc_args['start-at-page'];
}
$posts_per_page = 100;

$args = array(
'posts_per_page' => $posts_per_page,
'paged' => $page,
'post_type' => $post_type,
'post_status' => 'publish',
'fields' => 'ids',
'orderby' => 'ID',
'cache_results' => false,
);

$total = 0;

$args['paged'] = $page;
$my_query = new WP_Query( $args );

if ($my_query->have_posts())
{
if ($page == 1)

$total = $my_query->found_posts;
WP_CLI::line( 'Found '.$total.' posts of type "'.$post_type.'"' );
$progress_bar = WP_CLI\Utils\make_progress_bar('Indexing', $total, 1000 );
do {
$args['paged'] = $page;
$my_query = new WP_Query( $args );
WP_CLI::line( 'Starting indexing of page ' . $page . '.' );
if ($my_query->have_posts())
{
$total = $my_query->found_posts;
WP_CLI::line( 'Found '.$total.' posts of type "'.$post_type.'"' );
$progress_bar = WP_CLI\Utils\make_progress_bar('Indexing', $total );
foreach ( $my_query->posts as $post_id ) {
$progress_bar->tick();
FWP()->indexer->index( $post_id );
}

$this->stop_the_insanity();
WP_CLI::line( 'Finished indexing page ' . $page . '.' );
$page++;
}

foreach ( $my_query->posts as $key => $post_id )
{
//WP_CLI::line( $post_id );
$progress_bar->tick();
FWP()->indexer->index( $post_id );
}

// Free up memory
$this->stop_the_insanity();
} while ( $my_query->have_posts() );

$page++;
if ($total > 0) {
$progress_bar->finish();
WP_CLI::success( 'All posts indexed' );
} else {
WP_CLI::error( 'No posts of type "'.$post_type.'" found!' );
}


} while ( $my_query->have_posts() );

if ($total > 0)
{
$progress_bar->finish();
WP_CLI::success( 'All posts indexed' );
} else {
WP_CLI::error( 'No posts of type "'.$post_type.'" found!' );
}
}

/*
/**
* Clear all of the caches for memory management
*/
protected function stop_the_insanity() {
global $wpdb, $wp_object_cache;
$wpdb->queries = array(); // or define( 'WP_IMPORTING', true );
if ( !is_object( $wp_object_cache ) )
return;
$wp_object_cache->group_ops = array();
$wp_object_cache->stats = array();
$wp_object_cache->memcache_debug = array();
$wp_object_cache->cache = array();
if ( is_callable( $wp_object_cache, '__remoteset' ) )
$wp_object_cache->__remoteset(); // important
}
protected function stop_the_insanity() {
global $wpdb, $wp_object_cache, $wp_actions;
$wpdb->queries = array();
$wp_actions = array();
wp_cache_flush();
if ( !is_object( $wp_object_cache ) ){
return;
}
$wp_object_cache->group_ops = array();
$wp_object_cache->stats = array();
$wp_object_cache->memcache_debug = array();
$wp_object_cache->cache = array();
if ( is_callable( $wp_object_cache, '__remoteset' ) ){
$wp_object_cache->__remoteset();
}
}

}

Expand Down

0 comments on commit b65d5f5

Please sign in to comment.