|
18 | 18 | namespace WP_Facet; |
19 | 19 |
|
20 | 20 | if ( !defined ('WP_CLI') ) |
21 | | - return; |
| 21 | + return; |
22 | 22 |
|
23 | 23 | use WP_CLI; |
24 | 24 | use WP_CLI_Command; |
25 | | -use FacetWP_Indexer; |
26 | 25 | use WP_Query; |
27 | 26 |
|
28 | 27 | class CLI extends WP_CLI_Command { |
29 | 28 |
|
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 | + } |
52 | 51 |
|
53 | | - $posts_per_page = 100; |
54 | | - $page = 1; |
| 52 | + if( ! function_exists('FWP')){ |
| 53 | + WP_CLI::error( 'FacetWP plugin is not activated.' ); |
| 54 | + } |
55 | 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 | | - ); |
| 56 | + wp_suspend_cache_addition( true ); |
65 | 57 |
|
66 | | - $total = 0; |
| 58 | + $post_type = 'any'; |
| 59 | + if ( ! empty( $assoc_args['post-type'] ) ) { |
| 60 | + $post_type = $assoc_args['post-type']; |
| 61 | + } |
67 | 62 |
|
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 | + |
69 | 81 | $args['paged'] = $page; |
70 | 82 | $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()) |
75 | 92 | { |
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++; |
79 | 101 | } |
80 | 102 |
|
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 | 103 |
|
88 | | - // Free up memory |
89 | | - $this->stop_the_insanity(); |
| 104 | + } while ( $my_query->have_posts() ); |
90 | 105 |
|
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!' ); |
92 | 111 | } |
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!' ); |
103 | 112 | } |
104 | | - } |
105 | 113 |
|
106 | | - /* |
| 114 | + /** |
107 | 115 | * Clear all of the caches for memory management |
108 | 116 | */ |
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 | + } |
121 | 133 |
|
122 | 134 | } |
123 | 135 |
|
|
0 commit comments