Skip to content

Commit e9e42c4

Browse files
committed
Adds a 'start-from-page' parameter, and paged progress.
Also optimises cache clearing and defines WP_IMPORT.
1 parent e6bc228 commit e9e42c4

File tree

1 file changed

+60
-48
lines changed

1 file changed

+60
-48
lines changed

facetwp-wp-cli.php

Lines changed: 60 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
use WP_CLI;
2424
use WP_CLI_Command;
25-
use FacetWP_Indexer;
2625
use WP_Query;
2726

2827
class CLI extends WP_CLI_Command {
@@ -33,63 +32,73 @@ class CLI extends WP_CLI_Command {
3332
* ## OPTIONS
3433
* [--post-type=<name>]
3534
* : post type, 'any' if not defined
35+
*
36+
* [--start-at-page=<number>]
37+
* : pagenumber to start indexing, 1 if not defined
3638
*
3739
* ## EXAMPLES
3840
*
3941
* wp facet index
4042
* wp facet index --post-type=product
43+
* wp facet index --start-at-page=7
4144
*
4245
* @synopsis
4346
*/
4447
function index( $args, $assoc_args ) {
48+
if ( ! defined( 'WP_IMPORTING' ) ) {
49+
define('WP_IMPORTING', true);
50+
}
4551

46-
error_reporting(0);
52+
if( ! function_exists('FWP')){
53+
WP_CLI::error( 'FacetWP plugin is not activated.' );
54+
}
4755

48-
if ( empty( $assoc_args['post-type'] ) )
49-
$post_type = 'any';
50-
else
51-
$post_type = $assoc_args['post-type'];
56+
wp_suspend_cache_addition( true );
5257

53-
$posts_per_page = 100;
54-
$page = 1;
55-
56-
$args = array(
57-
'posts_per_page' => $posts_per_page,
58-
'paged' => $page,
59-
'post_type' => $post_type,
60-
'post_status' => 'publish',
61-
'fields' => 'ids',
62-
'orderby' => 'ID',
63-
'cache_results' => false,
64-
);
58+
$post_type = 'any';
59+
if ( ! empty( $assoc_args['post-type'] ) ) {
60+
$post_type = $assoc_args['post-type'];
61+
}
6562

66-
$total = 0;
63+
$page = 1;
64+
if ( ! empty( $assoc_args['start-at-page'] ) ) {
65+
$page = $assoc_args['start-at-page'];
66+
}
67+
$posts_per_page = 100;
6768

69+
$args = array(
70+
'posts_per_page' => $posts_per_page,
71+
'paged' => $page,
72+
'post_type' => $post_type,
73+
'post_status' => 'publish',
74+
'fields' => 'ids',
75+
'orderby' => 'ID',
76+
'cache_results' => false,
77+
);
78+
79+
$total = 0;
80+
81+
$args['paged'] = $page;
82+
$my_query = new WP_Query( $args );
83+
84+
$total = $my_query->found_posts;
85+
WP_CLI::line( 'Found '.$total.' posts of type "'.$post_type.'"' );
86+
$progress_bar = WP_CLI\Utils\make_progress_bar('Indexing', $total, 1000 );
6887
do {
69-
$args['paged'] = $page;
70-
$my_query = new WP_Query( $args );
71-
72-
if ($my_query->have_posts())
73-
{
74-
if ($page == 1)
75-
{
76-
$total = $my_query->found_posts;
77-
WP_CLI::line( 'Found '.$total.' posts of type "'.$post_type.'"' );
78-
$progress_bar = WP_CLI\Utils\make_progress_bar('Indexing', $total );
79-
}
80-
81-
foreach ( $my_query->posts as $key => $post_id )
82-
{
83-
//WP_CLI::line( $post_id );
84-
$progress_bar->tick();
85-
FWP()->indexer->index( $post_id );
86-
}
87-
88-
// Free up memory
89-
$this->stop_the_insanity();
90-
91-
$page++;
92-
}
88+
$args['paged'] = $page;
89+
$my_query = new WP_Query( $args );
90+
WP_CLI::line( 'Starting indexing of page ' . $page . '.' );
91+
if ($my_query->have_posts())
92+
{
93+
foreach ( $my_query->posts as $post_id ) {
94+
$progress_bar->tick();
95+
FWP()->indexer->index( $post_id );
96+
}
97+
98+
$this->stop_the_insanity();
99+
WP_CLI::line( 'Finished indexing page ' . $page . '.' );
100+
$page++;
101+
}
93102

94103

95104
} while ( $my_query->have_posts() );
@@ -103,20 +112,23 @@ function index( $args, $assoc_args ) {
103112
}
104113
}
105114

106-
/*
115+
/**
107116
* Clear all of the caches for memory management
108117
*/
109118
protected function stop_the_insanity() {
110-
global $wpdb, $wp_object_cache;
111-
$wpdb->queries = array(); // or define( 'WP_IMPORTING', true );
112-
if ( !is_object( $wp_object_cache ) )
119+
global $wpdb, $wp_object_cache, $wp_actions;
120+
$wpdb->queries = array();
121+
$wp_actions = array();
122+
wp_cache_flush();
123+
if ( !is_object( $wp_object_cache ) ){
113124
return;
125+
}
114126
$wp_object_cache->group_ops = array();
115127
$wp_object_cache->stats = array();
116128
$wp_object_cache->memcache_debug = array();
117129
$wp_object_cache->cache = array();
118130
if ( is_callable( $wp_object_cache, '__remoteset' ) )
119-
$wp_object_cache->__remoteset(); // important
131+
$wp_object_cache->__remoteset();
120132
}
121133

122134
}

0 commit comments

Comments
 (0)