/
wp-cli.php
159 lines (131 loc) · 4.12 KB
/
wp-cli.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/**
* Cache related posts by taxonomy.
*/
class Related_Posts_By_Taxonomy_CLI extends WP_CLI_Command {
/**
* Flush cache.
*
* ## EXAMPLES
*
* wp rpbt-cache flush
*/
public function flush() {
km_rpbt_flush_cache();
WP_CLI::success( "Cache Flushed." );
}
/**
* 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_query_vars();
if ( !$plugin ) {
WP_CLI::error( 'Error: Could not find plugin instance' );
}
$args = wp_parse_args( $assoc_args, $defaults );
if ( !isset( $args['taxonomies'] ) ) {
$args['taxonomies'] = '';
}
$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 ) );
}
}
WP_CLI::add_command( 'rpbt-cache', 'Related_Posts_By_Taxonomy_CLI' );