Skip to content

Commit b65d5f5

Browse files
Merge pull request #9 from level-level/adds-paged-progress-and-restart-from-page
Adds paged progress and restart from page
2 parents e6bc228 + e1bdfed commit b65d5f5

File tree

2 files changed

+90
-78
lines changed

2 files changed

+90
-78
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ Run indexing of FacetWP via WP-CLI
33

44
`wp help facet index`
55

6-
`wp facet index [--post-type=post_type]`
6+
`wp facet index [--post-type=post_type] [--start-from-page=number]`

facetwp-wp-cli.php

Lines changed: 89 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -18,106 +18,118 @@
1818
namespace WP_Facet;
1919

2020
if ( !defined ('WP_CLI') )
21-
return;
21+
return;
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 {
2928

30-
/**
31-
* Indexes all posts
32-
*
33-
* ## OPTIONS
34-
* [--post-type=<name>]
35-
* : post type, 'any' if not defined
36-
*
37-
* ## EXAMPLES
38-
*
39-
* wp facet index
40-
* wp facet index --post-type=product
41-
*
42-
* @synopsis
43-
*/
44-
function index( $args, $assoc_args ) {
45-
46-
error_reporting(0);
47-
48-
if ( empty( $assoc_args['post-type'] ) )
49-
$post_type = 'any';
50-
else
51-
$post_type = $assoc_args['post-type'];
29+
/**
30+
* Indexes all posts
31+
*
32+
* ## OPTIONS
33+
* [--post-type=<name>]
34+
* : post type, 'any' if not defined
35+
*
36+
* [--start-at-page=<number>]
37+
* : pagenumber to start indexing, 1 if not defined
38+
*
39+
* ## EXAMPLES
40+
*
41+
* wp facet index
42+
* wp facet index --post-type=product
43+
* wp facet index --start-at-page=7
44+
*
45+
* @synopsis
46+
*/
47+
function index( $args, $assoc_args ) {
48+
if ( ! defined( 'WP_IMPORTING' ) ) {
49+
define('WP_IMPORTING', true);
50+
}
5251

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

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-
);
56+
wp_suspend_cache_addition( true );
6557

66-
$total = 0;
58+
$post_type = 'any';
59+
if ( ! empty( $assoc_args['post-type'] ) ) {
60+
$post_type = $assoc_args['post-type'];
61+
}
6762

68-
do {
63+
$page = 1;
64+
if ( ! empty( $assoc_args['start-at-page'] ) ) {
65+
$page = $assoc_args['start-at-page'];
66+
}
67+
$posts_per_page = 100;
68+
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+
6981
$args['paged'] = $page;
7082
$my_query = new WP_Query( $args );
71-
72-
if ($my_query->have_posts())
73-
{
74-
if ($page == 1)
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 );
87+
do {
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())
7592
{
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 );
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++;
79101
}
80102

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-
}
87103

88-
// Free up memory
89-
$this->stop_the_insanity();
104+
} while ( $my_query->have_posts() );
90105

91-
$page++;
106+
if ($total > 0) {
107+
$progress_bar->finish();
108+
WP_CLI::success( 'All posts indexed' );
109+
} else {
110+
WP_CLI::error( 'No posts of type "'.$post_type.'" found!' );
92111
}
93-
94-
95-
} while ( $my_query->have_posts() );
96-
97-
if ($total > 0)
98-
{
99-
$progress_bar->finish();
100-
WP_CLI::success( 'All posts indexed' );
101-
} else {
102-
WP_CLI::error( 'No posts of type "'.$post_type.'" found!' );
103112
}
104-
}
105113

106-
/*
114+
/**
107115
* Clear all of the caches for memory management
108116
*/
109-
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 ) )
113-
return;
114-
$wp_object_cache->group_ops = array();
115-
$wp_object_cache->stats = array();
116-
$wp_object_cache->memcache_debug = array();
117-
$wp_object_cache->cache = array();
118-
if ( is_callable( $wp_object_cache, '__remoteset' ) )
119-
$wp_object_cache->__remoteset(); // important
120-
}
117+
protected function stop_the_insanity() {
118+
global $wpdb, $wp_object_cache, $wp_actions;
119+
$wpdb->queries = array();
120+
$wp_actions = array();
121+
wp_cache_flush();
122+
if ( !is_object( $wp_object_cache ) ){
123+
return;
124+
}
125+
$wp_object_cache->group_ops = array();
126+
$wp_object_cache->stats = array();
127+
$wp_object_cache->memcache_debug = array();
128+
$wp_object_cache->cache = array();
129+
if ( is_callable( $wp_object_cache, '__remoteset' ) ){
130+
$wp_object_cache->__remoteset();
131+
}
132+
}
121133

122134
}
123135

0 commit comments

Comments
 (0)