forked from publishpress/PublishPress-Authors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWP_Cli.php
210 lines (184 loc) · 7.26 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
* PublishPress Authors commands for the WP-CLI framework
*
* @package MultipleAuthors
* @author PublishPress <[email protected]>
* @copyright Copyright (C) 2018 PublishPress. All rights reserved.
* @license GPLv2 or later
* @since 1.0.0
* @see https://github.com/wp-cli/wp-cli
*/
namespace MultipleAuthors;
use MultipleAuthors\Classes\Installer;
use MultipleAuthors\Classes\Objects\Author;
use MultipleAuthors\Classes\Utils;
use WP_CLI_Command;
use WP_Query;
class WP_Cli extends WP_CLI_Command
{
public function _logCallback($message, $messageType = 'log')
{
if ('log' === $messageType) {
\WP_CLI::line($message);
} elseif ('error' === $messageType) {
\WP_CLI::error($message);
} elseif ('success' === $messageType) {
\WP_CLI::success($message);
}
}
/**
* List all of the posts without assigned co-authors terms
*
* @since 3.0
*
* @subcommand list-posts-without-terms
* @synopsis [--post_type=<ptype>] [--posts_per_page=<num>] [--paged=<page>] [--order=<order>] [--orederby=<orderby>]
*/
public function list_posts_without_terms($args, $assocArgs)
{
$defaults = [
'post_type' => 'post',
'order' => 'ASC',
'orderby' => 'ID',
'posts_per_page' => 300,
'paged' => 1,
];
$parsedArgs = wp_parse_args($assocArgs, $defaults);
$posts = Installer::getPostsWithoutAuthorTerms($parsedArgs);
if (count($posts) === 0) {
\WP_CLI::success(__('No posts without author terms were found', 'publishpress-authors'));
return;
}
foreach ($posts as $singlePost) {
$postData = [
$singlePost->ID,
addslashes($singlePost->post_title),
get_permalink($singlePost->ID),
$singlePost->post_date,
];
\WP_CLI::line('"' . implode('","', $postData) . '"');
}
\WP_CLI::success(
sprintf(
__('%d posts were found without author terms', 'publishpress-authors'),
count($posts)
)
);
}
/**
* Create author terms for all posts that don't have them
*
* @subcommand create-terms-for-posts
* @synopsis [--post_type=<ptype>] [--posts_per_page=<num>] [--paged=<page>]
*/
public function create_terms_for_posts($args, $assocArgs)
{
Installer::createAuthorTermsForPostsWithLegacyCoreAuthors($assocArgs, [$this, '_logCallback']);
\WP_CLI::success('Finished');
}
/**
* Clear all of the caches for memory management
*/
private function clearAllCaches()
{
global $wpdb, $wp_object_cache;
$wpdb->queries = []; // or define( 'WP_IMPORTING', true );
if (!is_object($wp_object_cache)) {
return;
}
$wp_object_cache->group_ops = [];
$wp_object_cache->stats = [];
$wp_object_cache->memcache_debug = [];
$wp_object_cache->cache = [];
if (is_callable($wp_object_cache, '__remoteset')) {
$wp_object_cache->__remoteset(); // important
}
}
/**
* Subcommand to assign coauthors to a post based on a given meta key
*
* @since 3.0
*
* @subcommand assign-author-by-meta-key
* @synopsis [--meta_key=<key>] [--post_type=<ptype>] [--append_author=<bool>] [--posts_per_page=<num>] [--paged=<page>] [--post_status=<string>]
*/
public function assign_author_by_meta_key($args, $assocArgs)
{
$defaults = [
'meta_key' => '_original_import_author', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'post_type' => 'post',
'order' => 'ASC',
'orderby' => 'ID',
'posts_per_page' => 100,
'paged' => 1,
'append_author' => false,
];
$this->args = wp_parse_args($assocArgs, $defaults);
// For global use and not a part of WP_Query
$appendAuthor = $this->args['append_author'];
unset($this->args['append_author']);
$postsTotal = 0;
$postsAlreadyAssociated = 0;
$postsMissingCoauthor = 0;
$postsAssociated = 0;
$missionCoauthors = [];
$posts = new WP_Query($this->args);
while ($posts->post_count) {
foreach ($posts->posts as $singlePost) {
$postsTotal++;
// See if the value in the post meta field is the same as any of the existing coauthors
$originalAuthor = get_post_meta($singlePost->ID, $this->args['meta_key'], true);
$existingCoauthors = get_post_authors($singlePost->ID);
$isAlreadyAssociated = false;
foreach ($existingCoauthors as $existingCoauthor) {
if ($originalAuthor == $existingCoauthor->user_nicename) {
$isAlreadyAssociated = true;
}
}
if ($isAlreadyAssociated && $appendAuthor) {
$postsAlreadyAssociated++;
\WP_CLI::line(
$postsTotal . ': Post #' . $singlePost->ID . ' already has "' . $originalAuthor . '" associated as a coauthor'
);
continue;
}
$coauthor = Author::get_by_term_slug(sanitize_title($originalAuthor));
if (false === $coauthor || is_wp_error($coauthor)) {
$postsMissingCoauthor++;
$missionCoauthors[] = $originalAuthor;
\WP_CLI::line(
$postsTotal . ': Post #' . $singlePost->ID . ' does not have "' . $originalAuthor . '" associated as a coauthor but there is not a coauthor profile'
);
continue;
}
if ($appendAuthor) {
$coauthors = $existingCoauthors;
$coauthors[] = $coauthor;
} else {
$coauthors = [$coauthor];
}
Utils::set_post_authors($singlePost->ID, $coauthors);
\WP_CLI::line(
$postsTotal . ': Post #' . $singlePost->ID . ' has been assigned "' . $originalAuthor . '" as the author'
);
$postsAssociated++;
clean_post_cache($singlePost->ID);
}
$this->args['paged']++;
$this->clearAllCaches();
$posts = new WP_Query($this->args);
}
\WP_CLI::line('All done! Here are your results:');
if ($postsAlreadyAssociated) {
\WP_CLI::line("- {$postsAlreadyAssociated} posts already had the coauthor assigned");
}
if ($postsMissingCoauthor) {
\WP_CLI::line("- {$postsMissingCoauthor} posts reference coauthors that don't exist. These are:");
\WP_CLI::line(' ' . implode(', ', array_unique($missionCoauthors)));
}
if ($postsAssociated) {
\WP_CLI::line("- {$postsAssociated} posts now have the proper coauthor");
}
}
}