-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCoAuthorsPlus.php
More file actions
354 lines (300 loc) · 9.55 KB
/
Copy pathCoAuthorsPlus.php
File metadata and controls
354 lines (300 loc) · 9.55 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php
/**
* Co-Author Plus integration Feature
*
* @since 1.1.0
* @package ElasticPressLabs
*/
namespace ElasticPressLabs\Feature;
use ElasticPress\Feature;
use ElasticPress\Features;
use ElasticPress\FeatureRequirementsStatus;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Co-Authors Plus feature
*
* @since 1.1.0
*/
class CoAuthorsPlus extends Feature {
/**
* Order of the feature in ElasticPress's Dashboard.
*
* @var integer
*/
public $order = 10;
/**
* Whether the Protected Content feature is active or not
*
* @since 2.1.1
* @var bool
*/
protected $is_protected_content_feature_active = false;
/**
* Initialize feature setting it's config
*
* @since 1.1.0
*/
public function __construct() {
$this->slug = 'co_authors_plus';
$this->group = 'third-party-plugins';
if ( ! defined( 'EP_VERSION' ) || version_compare( EP_VERSION, '5.2.0', '<' ) ) {
$this->set_i18n_strings();
}
$this->requires_install_reindex = true;
$this->requires_feature = 'search';
parent::__construct();
}
/**
* Sets i18n strings.
*
* @return void
* @since 2.4.0
*/
public function set_i18n_strings(): void {
$this->title = esc_html__( 'Co-Authors Plus', 'elasticpress-labs' );
}
/**
* Setup all feature filters
*
* @since 1.1.0
*/
public function setup() {
$settings = $this->get_settings();
if ( empty( $settings['active'] ) ) {
return;
}
add_filter( 'ep_sync_taxonomies', array( $this, 'include_author_term' ) );
if ( is_admin() && $this->is_protected_content_feature_active ) {
add_filter( 'ep_post_formatted_args', [ $this, 'include_author_in_es_query' ], 10, 3 );
}
/**
* Filter to skip coauthor plus query integration for frontend searches.
*
* @since 2.5.0
* @hook ep_coauthors_plus_skip_frontend_integration
* @param {bool} $skip Whether to skip coauthor plus query integration. Default false.
* @return {bool} Whether to skip coauthor plus query integration
*/
if ( apply_filters( 'ep_coauthors_plus_skip_frontend_integration', false ) ) {
add_filter( 'ep_weighting_configuration', [ $this, 'remove_author_weighting' ] );
return;
}
add_filter( 'ep_weighting_fields_for_post_type', [ $this, 'add_author_attributes_to_weighting' ], 10, 2 );
add_filter( 'ep_weighting_default_post_type_weights', [ $this, 'add_author_default_weight' ], 10, 2 );
}
/**
* Prepare Elasticsearch query to search for posts by author/coauthor
*
* @since 1.1.0
* @param {array} $formatted_args Formatted Elasticsearch query
* @param {array} $args Query variables
* @param WP_Query $wp_query Query part
* @return {array} New query
*/
public function include_author_in_es_query( $formatted_args, $args, $wp_query ) {
global $coauthors_plus;
if ( ( defined( 'WP_CLI' ) && WP_CLI ) || ! $wp_query->is_main_query() ) {
return $formatted_args;
}
$author_name = $wp_query->get( 'author_name' );
if ( ! $author_name ) {
return $formatted_args;
}
$coauthor = $coauthors_plus->get_coauthor_by( 'login', $author_name );
if ( ! $coauthor ) {
return $formatted_args;
}
$author_term = $coauthors_plus->get_author_term( $coauthor );
if ( $author_term ) {
$formatted_args['post_filter']['bool']['must'] = $this->filter_out_author_name_and_id_from_es_filter( $formatted_args );
$formatted_args['post_filter']['bool']['must'][] = $this->add_es_filter_for_author_coauthor( $author_term );
}
return $formatted_args;
}
/**
* Filter out the post_author.display_name and post_author.id
*
* @since 1.1.0
* @param array $formatted_args Formatted Elasticsearch query
* @return array|mixed
*/
protected function filter_out_author_name_and_id_from_es_filter( $formatted_args ) {
if ( ! isset( $formatted_args['post_filter']['bool']['must'] ) || ! is_array( $formatted_args['post_filter']['bool']['must'] ) ) {
return $formatted_args;
}
return array_values(
array_filter(
$formatted_args['post_filter']['bool']['must'],
function ( $item ) {
return ! ( isset( $item['term']['post_author.display_name'] ) || isset( $item['term']['post_author.id'] ) );
}
)
);
}
/**
* Add Elasticsearch filter for author/coauthor
*
* @since 1.1.0
* @param object $author_term The author term on success
* @return array
*/
protected function add_es_filter_for_author_coauthor( $author_term ) {
return [
'bool' => [
'should' => [
[
'terms' => [ 'post_author.login' => [ $author_term->name ] ],
],
[
'bool' => [
'must' => [
[
'terms' => [
'terms.author.slug' => [ $author_term->slug ],
],
],
[
'terms' => [
'terms.author.term_id' => [ $author_term->term_id ],
],
],
],
],
],
],
],
];
}
/**
* Include author taxonomy when indexing posts.
*
* @since 1.1.0
* @param array $taxonomies Selected taxonomies.
* @return WP_Taxonomy|false
*/
public function include_author_term( $taxonomies ) {
$taxonomies[] = get_taxonomy( 'author' );
return $taxonomies;
}
/**
* Output feature box summary
*
* @since 1.1.0
*/
public function output_feature_box_summary() {
?>
<p><?php esc_html_e( 'Add support for the Co-Authors Plus plugin in the Admin Post List screen by Author name.', 'elasticpress-labs' ); ?></p>
<?php
}
/**
* Output feature box long
*
* @since 1.1.0
*/
public function output_feature_box_long() {
if ( ! defined( 'EP_VERSION' ) || version_compare( EP_VERSION, '5.0.0', '<' ) ) {
?>
<p><?php echo wp_kses_post( __( 'If using the Co-Authors Plus plugin and the Protected Content feature, enable this feature to visit the Admin Post List screen by Author name <code>wp-admin/edit.php?author_name=<name></code> and see correct results.', 'elasticpress-labs' ) ); ?></p>
<?php
return;
}
_doing_it_wrong(
__METHOD__,
esc_html__( 'Settings are now generated via the set_settings_schema() method.' ),
'ElasticPress Labs 2.2.0'
);
}
/**
* Set the `settings_schema` attribute
*
* @since 2.2.0
*/
public function set_settings_schema() {
$this->settings_schema = [
[
'key' => 'instructions',
'label' => '<p>' . __( 'When enabled, this feature integrates ElasticPress with Co-Authors Plus to enhance author-related queries on the frontend. If "Protected Content" is activated, visit the Admin Post List screen by Author name <code>wp-admin/edit.php?author_name=<name></code> and see correct results.', 'elasticpress-labs' ) . '</p>',
'type' => 'markup',
],
];
}
/**
* Determine feature reqs status
*
* @since 1.1.0
* @return FeatureRequirementsStatus
*/
public function requirements_status() {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
$status = new FeatureRequirementsStatus( 0 );
$protected_content_feature = Features::factory()->get_registered_feature( 'protected_content' );
$this->is_protected_content_feature_active = $protected_content_feature && $protected_content_feature->is_active();
if ( ! \is_plugin_active( 'co-authors-plus/co-authors-plus.php' ) || ! class_exists( '\CoAuthors_Plus' ) ) {
$status->code = 2;
$status->message = esc_html__( 'You need to have Co-Authors Plus installed and activated.', 'elasticpress-labs' );
} elseif ( ! $this->is_protected_content_feature_active ) {
$status->code = 1;
$status->message = esc_html__( 'You need to activate the Protected Content Feature to this feature work properly.', 'elasticpress-labs' );
}
return $status;
}
/**
* Add Co-Authors attributes to the Weighting Dashboard.
*
* @since 2.5.0
* @param array $fields The array of weighting fields.
* @param string $post_type Current post type.
* @return array Modified array of weighting fields.
*/
public function add_author_attributes_to_weighting( $fields, $post_type ) {
global $coauthors_plus;
if ( ! in_array( $post_type, $coauthors_plus->supported_post_types, true ) ) {
return $fields;
}
$fields['attributes']['children'][ 'terms.' . $coauthors_plus->coauthor_taxonomy . '.name' ] = [
'key' => 'terms.' . $coauthors_plus->coauthor_taxonomy . '.name',
'label' => $coauthors_plus->guest_authors->labels['singular'],
];
return $fields;
}
/**
* Add default weight for Co-Authors field.
*
* @since 2.5.0
* @param array $defaults The default weight configuration.
* @param string $post_type Current post type.
* @return array Modified weight configuration.
*/
public function add_author_default_weight( $defaults, $post_type ) {
global $coauthors_plus;
if ( ! in_array( $post_type, $coauthors_plus->supported_post_types, true ) ) {
return $defaults;
}
$defaults[ 'terms.' . $coauthors_plus->coauthor_taxonomy . '.name' ] = [
'enabled' => true,
'weight' => 1,
];
return $defaults;
}
/**
* Remove author weighting from the weighting configuration.
*
* This is necessary because the value of the author field is stored in the options,
* and we need to ensure it is excluded from the weighting configuration.
*
* @since 2.5.0
* @param array $weighting_configuration The weighting configuration.
* @return array Modified weighting configuration.
*/
public function remove_author_weighting( $weighting_configuration ) {
global $coauthors_plus;
$supported_post_types = $coauthors_plus->supported_post_types;
$author_taxonomy_key = 'terms.' . $coauthors_plus->coauthor_taxonomy . '.name';
foreach ( $supported_post_types as $post_type ) {
unset( $weighting_configuration[ $post_type ][ $author_taxonomy_key ] );
}
return $weighting_configuration;
}
}