forked from Automattic/Co-Authors-Plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
co-authors-plus.php
executable file
·1996 lines (1686 loc) · 69.8 KB
/
co-authors-plus.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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Plugin Name: Co-Authors Plus
Plugin URI: http://wordpress.org/extend/plugins/co-authors-plus/
Description: Allows multiple authors to be assigned to a post. This plugin is an extended version of the Co-Authors plugin developed by Weston Ruter.
Version: 3.5.6
Author: Mohammad Jangda, Daniel Bachhuber, Automattic
Copyright: 2008-2015 Shared and distributed between Mohammad Jangda, Daniel Bachhuber, Weston Ruter
GNU General Public License, Free Software Foundation <http://creativecommons.org/licenses/GPL/2.0/>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-----------------
Glossary:
User - a WordPress user account
Guest author - a CAP-created co-author
Co-author - in the context of a single post, a guest author or user assigned to the post alongside others
Author - user with the role of author
*/
define( 'COAUTHORS_PLUS_VERSION', '3.5.6' );
require_once dirname( __FILE__ ) . '/vendor/autoload.php';
require_once dirname( __FILE__ ) . '/template-tags.php';
require_once dirname( __FILE__ ) . '/deprecated.php';
require_once dirname( __FILE__ ) . '/php/class-coauthors-template-filters.php';
require_once dirname( __FILE__ ) . '/php/class-coauthors-endpoint.php';
require_once dirname( __FILE__ ) . '/php/integrations/amp.php';
CoAuthors\Integrations\Yoast::init();
if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once dirname( __FILE__ ) . '/php/class-wp-cli.php';
}
class CoAuthors_Plus {
// Name for the taxonomy we're using to store relationships
// and the post type we're using to store guest authors
var $coauthor_taxonomy = 'author';
var $coreauthors_meta_box_name = 'authordiv';
var $coauthors_meta_box_name = 'coauthorsdiv';
var $force_guest_authors = false;
var $gravatar_size = 25;
var $_pages_whitelist = array( 'post.php', 'post-new.php', 'edit.php' );
var $supported_post_types = array();
var $ajax_search_fields = array( 'display_name', 'first_name', 'last_name', 'user_login', 'ID', 'user_email' );
var $having_terms = '';
var $to_be_filtered_caps = array();
/**
* __construct()
*/
function __construct() {
// Register our models
add_action( 'init', array( $this, 'action_init' ) );
add_action( 'init', array( $this, 'action_init_late' ), 100 );
// Load admin_init function
add_action( 'admin_init', array( $this, 'admin_init' ) );
// Modify SQL queries to include guest authors
add_filter( 'posts_where', array( $this, 'posts_where_filter' ), 10, 2 );
add_filter( 'posts_join', array( $this, 'posts_join_filter' ), 10, 2 );
add_filter( 'posts_groupby', array( $this, 'posts_groupby_filter' ), 10, 2 );
// Action to set co-authors when a post is saved
add_action( 'save_post', array( $this, 'coauthors_update_post' ), 10, 2 );
// Filter to set the post_author field when wp_insert_post is called
add_filter( 'wp_insert_post_data', array( $this, 'coauthors_set_post_author_field' ), 10, 2 );
// Action to reassign posts when a guest author is deleted
add_action( 'delete_user', array( $this, 'delete_user_action' ) );
add_filter( 'get_usernumposts', array( $this, 'filter_count_user_posts' ), 10, 2 );
// Action to set up co-author auto-suggest
add_action( 'wp_ajax_coauthors_ajax_suggest', array( $this, 'ajax_suggest' ) );
// Filter to allow co-authors to edit posts
add_filter( 'user_has_cap', array( $this, 'filter_user_has_cap' ), 10, 3 );
// Handle the custom co-author meta box
add_action( 'add_meta_boxes', array( $this, 'add_coauthors_box' ) );
add_action( 'add_meta_boxes', array( $this, 'remove_authors_box' ) );
// Removes the co-author dropdown from the post quick edit
add_action( 'admin_head', array( $this, 'remove_quick_edit_authors_box' ) );
// Restricts WordPress from blowing away term order on bulk edit
add_filter( 'wp_get_object_terms', array( $this, 'filter_wp_get_object_terms' ), 10, 4 );
// Make sure we've correctly set data on guest author pages
add_action( 'posts_selection', array( $this, 'fix_author_page' ) ); // use posts_selection since it's after WP_Query has built the request and before it's queried any posts
add_action( 'the_post', array( $this, 'fix_author_page' ) );
// Support for Edit Flow's calendar and story budget
add_filter( 'ef_calendar_item_information_fields', array( $this, 'filter_ef_calendar_item_information_fields' ), 10, 2 );
add_filter( 'ef_story_budget_term_column_value', array( $this, 'filter_ef_story_budget_term_column_value' ), 10, 3 );
// Support Jetpack Open Graph Tags
add_filter( 'jetpack_open_graph_tags', array( $this, 'filter_jetpack_open_graph_tags' ), 10, 2 );
// Filter to send comment moderation notification e-mail to multiple co-authors
add_filter( 'comment_moderation_recipients', 'cap_filter_comment_moderation_email_recipients', 10, 2 );
// Support infinite scroll for Guest Authors on author pages
add_filter( 'infinite_scroll_js_settings', array( $this, 'filter_infinite_scroll_js_settings' ), 10, 2 );
// Delete Co-Author Cache on Post Save & Post Delete
add_action( 'save_post', array( $this, 'clear_cache' ) );
add_action( 'delete_post', array( $this, 'clear_cache' ) );
add_action( 'set_object_terms', array( $this, 'clear_cache_on_terms_set' ), 10, 6 );
// Filter to correct author on author archive page
add_filter( 'get_the_archive_title', array( $this, 'filter_author_archive_title' ), 10, 1 );
// Filter to display author image if exists instead of avatar
add_filter( 'pre_get_avatar_data', array( $this, 'filter_pre_get_avatar_data_url' ), 10, 2 );
// Block editor assets for the sidebar plugin.
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_sidebar_plugin_assets' ) );
}
/**
* Register the taxonomy used to managing relationships,
* and the custom post type to store our author data
*/
public function action_init() {
// Allow Co-Authors Plus to be easily translated
load_plugin_textdomain( 'co-authors-plus', null, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
// Load the Guest Authors functionality if needed
if ( $this->is_guest_authors_enabled() ) {
require_once dirname( __FILE__ ) . '/php/class-coauthors-guest-authors.php';
$this->guest_authors = new CoAuthors_Guest_Authors();
if ( apply_filters( 'coauthors_guest_authors_force', false ) ) {
$this->force_guest_authors = true;
}
}
// Maybe automatically apply our template tags
if ( apply_filters( 'coauthors_auto_apply_template_tags', false ) ) {
global $coauthors_plus_template_filters;
$coauthors_plus_template_filters = new CoAuthors_Template_Filters();
}
}
/**
* Determine if block editor sidebar integration should be loaded.
*
* @param WP_Post|int|null $post Post ID or object, null to use global.
* @return bool
*/
public function is_block_editor( $post = null ) {
$screen = get_current_screen();
// Pre-5.0 compatibility
if ( method_exists( $screen, 'is_block_editor' ) ) {
return $screen->is_block_editor();
} else {
return false;
}
}
/**
* When filter is set to enable block editor integration, enqueue assets
* for posts and users where Co Authors is enabled
*/
public function enqueue_sidebar_plugin_assets() {
if ( $this->is_post_type_enabled() && $this->current_user_can_set_authors() ) {
$asset = require dirname( __FILE__ ) . '/build/index.asset.php';
wp_register_script(
'coauthors-sidebar-js',
plugins_url( 'build/index.js', __FILE__ ),
$asset['dependencies'],
$asset['version']
);
wp_register_style(
'coauthors-sidebar-css',
plugins_url( 'build/style-index.css', __FILE__ ),
'',
$asset['version']
);
wp_enqueue_script( 'coauthors-sidebar-js' );
wp_enqueue_style( 'coauthors-sidebar-css' );
}
}
/**
* Register the 'author' taxonomy and add post type support
*/
public function action_init_late() {
// Register new taxonomy so that we can store all of the relationships
$args = array(
'hierarchical' => false,
'labels' => array(
'name' => __( 'Authors' ),
'all_items' => __( 'All Authors' ),
),
'query_var' => false,
'rewrite' => false,
'public' => false,
'sort' => true,
'args' => array( 'orderby' => 'term_order' ),
'show_ui' => false,
);
// If we use the nasty SQL query, we need our custom callback. Otherwise, we still need to flush cache.
if ( apply_filters( 'coauthors_plus_should_query_post_author', true ) ) {
$args['update_count_callback'] = array( $this, '_update_users_posts_count' );
} else {
add_action( 'edited_term_taxonomy', array( $this, 'action_edited_term_taxonomy_flush_cache' ), 10, 2 );
}
$post_types_with_authors = array_values( get_post_types() );
foreach ( $post_types_with_authors as $key => $name ) {
if ( ! post_type_supports( $name, $this->coauthor_taxonomy ) || in_array( $name, array( 'revision', 'attachment' ) ) ) {
unset( $post_types_with_authors[ $key ] );
}
}
$this->supported_post_types = apply_filters( 'coauthors_supported_post_types', $post_types_with_authors );
register_taxonomy( $this->coauthor_taxonomy, $this->supported_post_types, $args );
}
/**
* Initialize the plugin for the admin
*/
public function admin_init() {
global $pagenow;
// Add the main JS script and CSS file
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
// Add necessary JS variables
add_action( 'admin_head', array( $this, 'js_vars' ) );
// Hooks to add additional co-authors to 'authors' column to edit page
add_filter( 'manage_posts_columns', array( $this, '_filter_manage_posts_columns' ) );
add_filter( 'manage_pages_columns', array( $this, '_filter_manage_posts_columns' ) );
add_action( 'manage_posts_custom_column', array( $this, '_filter_manage_posts_custom_column' ) );
add_action( 'manage_pages_custom_column', array( $this, '_filter_manage_posts_custom_column' ) );
// Add quick-edit co-author select field
add_action( 'quick_edit_custom_box', array( $this, '_action_quick_edit_custom_box' ), 10, 2 );
// Hooks to modify the published post number count on the Users WP List Table
add_filter( 'manage_users_columns', array( $this, '_filter_manage_users_columns' ) );
add_filter( 'manage_users_custom_column', array( $this, '_filter_manage_users_custom_column' ), 10, 3 );
// Apply some targeted filters
add_action( 'load-edit.php', array( $this, 'load_edit' ) );
}
/**
* Check whether the guest authors functionality is enabled or not
* Guest authors can be disabled entirely with:
* add_filter( 'coauthors_guest_authors_enabled', '__return_false' )
*
* @since 3.0
*
* @return bool
*/
public function is_guest_authors_enabled() {
return apply_filters( 'coauthors_guest_authors_enabled', true );
}
/**
* Get a guest author object by a specific type of key
*
* @param string $key Key to search by (slug,email)
* @param string $value Value to search for
* @return object|false $coauthor The co-author on success, false on failure
*/
public function get_coauthor_by( $key, $value, $force = false ) {
// If Guest Authors are enabled, prioritize those profiles
if ( $this->is_guest_authors_enabled() && isset( $this->guest_authors ) ) {
$guest_author = $this->guest_authors->get_guest_author_by( $key, $value, $force );
if ( is_object( $guest_author ) ) {
return $guest_author;
}
}
switch ( $key ) {
case 'id':
case 'login':
case 'user_login':
case 'email':
case 'user_nicename':
case 'user_email':
if ( 'user_login' == $key ) {
$key = 'login';
}
if ( 'user_email' == $key ) {
$key = 'email';
}
if ( 'user_nicename' == $key ) {
$key = 'slug';
}
$user = get_user_by( $key, $value );
if ( ! $user && ( 'login' == $key || 'slug' == $key ) ) {
// Re-try lookup without prefixed value if no results found.
$value = preg_replace( '#^cap\-#', '', $value );
$user = get_user_by( $key, $value );
}
if ( ! $user ) {
return false;
}
$user->type = 'wpuser';
// However, if guest authors are enabled and there's a guest author linked to this
// user account, we want to use that instead
if ( $this->is_guest_authors_enabled() && isset( $this->guest_authors ) ) {
$guest_author = $this->guest_authors->get_guest_author_by( 'linked_account', $user->user_login );
if ( is_object( $guest_author ) ) {
$user = $guest_author;
}
}
return $user;
break;
}
return false;
}
/**
* Whether or not Co-Authors Plus is enabled for this post type
* Must be called after init
*
* @since 3.0
*
* @param string $post_type The name of the post type we're considering
* @return bool Whether or not it's enabled
*/
public function is_post_type_enabled( $post_type = null ) {
if ( ! $post_type ) {
$post_type = get_post_type();
if ( is_admin() && ! $post_type ) {
$post_type = get_current_screen()->post_type;
}
}
return (bool) in_array( $post_type, $this->supported_post_types );
}
/**
* Removes the standard WordPress 'Author' box.
* We don't need it because the Co-Authors Plus one is way cooler.
*/
public function remove_authors_box() {
if ( $this->is_post_type_enabled() ) {
remove_meta_box( $this->coreauthors_meta_box_name, get_post_type(), 'normal' );
}
}
/**
* Adds a custom 'Authors' box
*/
public function add_coauthors_box() {
if ( $this->is_post_type_enabled() && $this->current_user_can_set_authors() ) {
if ( false === $this->is_block_editor() ) {
add_meta_box( $this->coauthors_meta_box_name, apply_filters( 'coauthors_meta_box_title', __( 'Authors', 'co-authors-plus' ) ), array( $this, 'coauthors_meta_box' ), get_post_type(), apply_filters( 'coauthors_meta_box_context', 'side' ), apply_filters( 'coauthors_meta_box_priority', 'high' ) );
}
}
}
/**
* Callback for adding the custom 'Authors' box
*/
public function coauthors_meta_box( $post ) {
global $post, $coauthors_plus, $current_screen;
$post_id = $post->ID;
$default_user = apply_filters( 'coauthors_default_author', wp_get_current_user() );
// @daniel, $post_id and $post->post_author are always set when a new post is created due to auto draft,
// and the else case below was always able to properly assign users based on wp_posts.post_author,
// but that's not possible with force_guest_authors = true.
if ( ! $post_id || 0 === $post_id || ( ! $post->post_author && ! $coauthors_plus->force_guest_authors ) || ( 'post' === $current_screen->base && 'add' === $current_screen->action ) ) {
$coauthors = array();
// If guest authors is enabled, try to find a guest author attached to this user ID
if ( $this->is_guest_authors_enabled() ) {
$coauthor = $coauthors_plus->guest_authors->get_guest_author_by( 'linked_account', $default_user->user_login );
if ( $coauthor ) {
$coauthors[] = $coauthor;
}
}
// If the above block was skipped, or if it failed to find a guest author, use the current
// logged in user, so long as force_guest_authors is false. If force_guest_authors = true, we are
// OK with having an empty authoring box.
if ( ! $coauthors_plus->force_guest_authors && empty( $coauthors ) ) {
if ( is_array( $default_user ) ) {
$coauthors = $default_user;
} else {
$coauthors[] = $default_user;
}
}
} else {
$coauthors = get_coauthors();
}
$count = 0;
if ( ! empty( $coauthors ) ) :
?>
<div id="coauthors-readonly" class="hide-if-js">
<ul>
<?php
foreach ( $coauthors as $coauthor ) :
$count++;
$avatar_url = get_avatar_url( $coauthor->ID );
?>
<li>
<?php echo get_avatar( $coauthor->ID, $this->gravatar_size ); ?>
<span id="<?php echo esc_attr( 'coauthor-readonly-' . $count ); ?>" class="coauthor-tag">
<input type="text" name="coauthorsinput[]" readonly="readonly" value="<?php echo esc_attr( $coauthor->display_name ); ?>" />
<input type="text" name="coauthors[]" value="<?php echo esc_attr( $coauthor->user_login ); ?>" />
<input type="text" name="coauthorsemails[]" value="<?php echo esc_attr( $coauthor->user_email ); ?>" />
<input type="text" name="coauthorsnicenames[]" value="<?php echo esc_attr( $coauthor->user_nicename ); ?>" />
<input type="hidden" name="coauthorsavatars[]" value="<?php echo esc_url( $avatar_url ); ?>" />
</span>
</li>
<?php
endforeach;
?>
</ul>
<div class="clear"></div>
<p><?php echo wp_kses( __( '<strong>Note:</strong> To edit post authors, please enable javascript or use a javascript-capable browser', 'co-authors-plus' ), array( 'strong' => array() ) ); ?></p>
</div>
<?php
endif;
?>
<div id="coauthors-edit" class="hide-if-no-js">
<p><?php echo wp_kses( __( 'Click on an author to change them. Drag to change their order. Click on <strong>Remove</strong> to remove them.', 'co-authors-plus' ), array( 'strong' => array() ) ); ?></p>
</div>
<?php wp_nonce_field( 'coauthors-edit', 'coauthors-nonce' ); ?>
<?php
}
/**
* Removes the default 'author' dropdown from quick edit
*/
function remove_quick_edit_authors_box() {
global $pagenow;
if ( 'edit.php' == $pagenow && $this->is_post_type_enabled() ) {
remove_post_type_support( get_post_type(), $this->coauthor_taxonomy );
}
}
/**
* Add co-authors to 'authors' column on edit pages
*
* @param array $post_columns
*/
function _filter_manage_posts_columns( $posts_columns ) {
$new_columns = array();
if ( ! $this->is_post_type_enabled() ) {
return $posts_columns;
}
foreach ( $posts_columns as $key => $value ) {
$new_columns[ $key ] = $value;
if ( 'title' === $key ) {
$new_columns['coauthors'] = __( 'Authors', 'co-authors-plus' );
}
if ( $this->coauthor_taxonomy === $key ) {
unset( $new_columns[ $key ] );
}
}
return $new_columns;
}
/**
* Insert co-authors into post rows on Edit Page
*
* @param string $column_name
*/
function _filter_manage_posts_custom_column( $column_name ) {
if ( 'coauthors' === $column_name ) {
global $post;
$authors = get_coauthors( $post->ID );
$count = 1;
foreach ( $authors as $author ) :
$args = array(
'author_name' => $author->user_nicename,
);
if ( 'post' != $post->post_type ) {
$args['post_type'] = $post->post_type;
}
$author_filter_url = add_query_arg( array_map( 'rawurlencode', $args ), admin_url( 'edit.php' ) );
?>
<a href="<?php echo esc_url( $author_filter_url ); ?>"
data-user_nicename="<?php echo esc_attr( $author->user_nicename ); ?>"
data-user_email="<?php echo esc_attr( $author->user_email ); ?>"
data-display_name="<?php echo esc_attr( $author->display_name ); ?>"
data-user_login="<?php echo esc_attr( $author->user_login ); ?>"
data-avatar="<?php echo esc_attr( get_avatar_url( $author->ID ) ); ?>"
><?php echo esc_html( $author->display_name ); ?></a><?php echo ( $count < count( $authors ) ) ? ',' : ''; ?>
<?php
$count++;
endforeach;
}
}
/**
* Unset the post count column because it's going to be inaccurate and provide our own
*/
function _filter_manage_users_columns( $columns ) {
$new_columns = array();
// Unset and add our column while retaining the order of the columns
foreach ( $columns as $column_name => $column_title ) {
if ( 'posts' == $column_name ) {
$new_columns['coauthors_post_count'] = __( 'Posts', 'co-authors-plus' );
} else {
$new_columns[ $column_name ] = $column_title;
}
}
return $new_columns;
}
/**
* Provide an accurate count when looking up the number of published posts for a user
*/
function _filter_manage_users_custom_column( $value, $column_name, $user_id ) {
if ( 'coauthors_post_count' != $column_name ) {
return $value;
}
// We filter count_user_posts() so it provides an accurate number
$numposts = count_user_posts( $user_id ); // phpcs:ignore
$user = get_user_by( 'id', $user_id );
if ( $numposts > 0 ) {
$value .= "<a href='edit.php?author_name=$user->user_nicename' title='" . esc_attr__( 'View posts by this author', 'co-authors-plus' ) . "' class='edit'>";
$value .= $numposts;
$value .= '</a>';
} else {
$value .= 0;
}
return $value;
}
/**
* Quick Edit co-authors box.
*/
function _action_quick_edit_custom_box( $column_name, $post_type ) {
if ( 'coauthors' != $column_name || ! $this->is_post_type_enabled( $post_type ) || ! $this->current_user_can_set_authors() ) {
return;
}
?>
<label class="inline-edit-group inline-edit-coauthors">
<span class="title"><?php esc_html_e( 'Authors', 'co-authors-plus' ); ?></span>
<div id="coauthors-edit" class="hide-if-no-js">
<p><?php echo wp_kses( __( 'Click on an author to change them. Drag to change their order. Click on <strong>Remove</strong> to remove them.', 'co-authors-plus' ), array( 'strong' => array() ) ); ?></p>
</div>
<?php wp_nonce_field( 'coauthors-edit', 'coauthors-nonce' ); ?>
</label>
<?php
}
/**
* When we update the terms at all, we should update the published post count for each user
*/
function _update_users_posts_count( $tt_ids, $taxonomy ) {
global $wpdb;
$tt_ids = implode( ', ', array_map( 'intval', $tt_ids ) );
$term_ids = $wpdb->get_results( "SELECT term_id FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)" ); // phpcs:ignore
foreach ( (array) $term_ids as $term_id_result ) {
$term = get_term_by( 'id', $term_id_result->term_id, $this->coauthor_taxonomy );
$this->update_author_term_post_count( $term );
}
$tt_ids = explode( ', ', $tt_ids );
clean_term_cache( $tt_ids, '', false );
}
/**
* If we're forcing Co-Authors Plus to just do taxonomy queries, we still
* need to flush our special cache after a taxonomy term has been updated
*
* @since 3.1
*/
public function action_edited_term_taxonomy_flush_cache( $tt_id, $taxonomy ) {
global $wpdb;
if ( $this->coauthor_taxonomy != $taxonomy ) {
return;
}
$term_id = $wpdb->get_results( $wpdb->prepare( "SELECT term_id FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d ", $tt_id ) );
$term = get_term_by( 'id', $term_id[0]->term_id, $taxonomy );
$coauthor = $this->get_coauthor_by( 'user_nicename', $term->slug );
if ( ! $coauthor ) {
return new WP_Error( 'missing-coauthor', __( 'No co-author exists for that term', 'co-authors-plus' ) );
}
wp_cache_delete( 'author-term-' . $coauthor->user_nicename, 'co-authors-plus' );
}
/**
* Update the post count associated with an author term
*
* @since 3.0
*
* @param object $term The co-author term
*/
public function update_author_term_post_count( $term ) {
global $wpdb;
$coauthor = $this->get_coauthor_by( 'user_nicename', $term->slug );
if ( ! $coauthor ) {
return new WP_Error( 'missing-coauthor', __( 'No co-author exists for that term', 'co-authors-plus' ) );
}
$query = "SELECT COUNT({$wpdb->posts}.ID) FROM {$wpdb->posts}";
$query .= " LEFT JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)";
$query .= " LEFT JOIN {$wpdb->term_taxonomy} ON ( {$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id )";
$having_terms_and_authors = $having_terms = $wpdb->prepare( "{$wpdb->term_taxonomy}.term_id = %d", $term->term_id );
if ( 'wpuser' == $coauthor->type ) {
$having_terms_and_authors .= $wpdb->prepare( " OR {$wpdb->posts}.post_author = %d", $coauthor->ID );
}
$post_types = apply_filters( 'coauthors_count_published_post_types', array( 'post' ) );
$post_types = array_map( 'sanitize_key', $post_types );
$post_types = "'" . implode( "','", $post_types ) . "'";
$query .= " WHERE ({$having_terms_and_authors}) AND {$wpdb->posts}.post_type IN ({$post_types}) AND {$wpdb->posts}.post_status = 'publish'";
$query .= $wpdb->prepare( " GROUP BY {$wpdb->posts}.ID HAVING MAX( IF ( {$wpdb->term_taxonomy}.taxonomy = '%s', IF ( {$having_terms},2,1 ),0 ) ) <> 1 ", $this->coauthor_taxonomy ); //phpcs:ignore
$count = $wpdb->query( $query ); // phpcs:ignore
$wpdb->update( $wpdb->term_taxonomy, array( 'count' => $count ), array( 'term_taxonomy_id' => $term->term_taxonomy_id ) );
wp_cache_delete( 'author-term-' . $coauthor->user_nicename, 'co-authors-plus' );
}
/**
* Modify the author query posts SQL to include posts co-authored
*/
function posts_join_filter( $join, $query ) {
global $wpdb;
if ( $query->is_author() ) {
$post_type = $query->query_vars['post_type'];
if ( 'any' === $post_type ) {
$post_type = get_post_types( array( 'exclude_from_search' => false ) );
}
if ( ! empty( $post_type ) && ! is_object_in_taxonomy( $post_type, $this->coauthor_taxonomy ) ) {
return $join;
}
if ( empty( $this->having_terms ) ) {
return $join;
}
// Check to see that JOIN hasn't already been added. Props michaelingp and nbaxley
$term_relationship_inner_join = " INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)";
$term_relationship_left_join = " LEFT JOIN {$wpdb->term_relationships} AS tr1 ON ({$wpdb->posts}.ID = tr1.object_id)";
$term_taxonomy_join = " INNER JOIN {$wpdb->term_taxonomy} ON ( tr1.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id )";
// 4.6+ uses a LEFT JOIN for tax queries so we need to check for both
if ( false === strpos( $join, trim( $term_relationship_inner_join ) )
&& false === strpos( $join, trim( $term_relationship_left_join ) ) ) {
$join .= $term_relationship_left_join;
}
if ( false === strpos( $join, trim( $term_taxonomy_join ) ) ) {
$join .= str_replace( 'INNER JOIN', 'LEFT JOIN', $term_taxonomy_join );
}
}
return $join;
}
/**
* Modify the author query posts SQL to include posts co-authored
*
* @param string $where
* @param WP_Query $query
*
* @return string
*/
function posts_where_filter( $where, $query ) {
global $wpdb;
if ( $query->is_author() ) {
$post_type = $query->query_vars['post_type'];
if ( 'any' === $post_type ) {
$post_type = get_post_types( array( 'exclude_from_search' => false ) );
}
if ( ! empty( $post_type ) && ! is_object_in_taxonomy( $post_type, $this->coauthor_taxonomy ) ) {
return $where;
}
if ( $query->get( 'author_name' ) ) {
$author_name = sanitize_title( $query->get( 'author_name' ) );
} else {
$author_data = get_userdata( $query->get( $this->coauthor_taxonomy ) );
if ( is_object( $author_data ) ) {
$author_name = $author_data->user_nicename;
} else {
return $where;
}
}
$terms = array();
$coauthor = $this->get_coauthor_by( 'user_nicename', $author_name );
if ( $author_term = $this->get_author_term( $coauthor ) ) {
$terms[] = $author_term;
}
// If this co-author has a linked account, we also need to get posts with those terms
if ( ! empty( $coauthor->linked_account ) ) {
$linked_account = get_user_by( 'login', $coauthor->linked_account );
if ( $guest_author_term = $this->get_author_term( $linked_account ) ) {
$terms[] = $guest_author_term;
}
}
// Whether or not to include the original 'post_author' value in the query
// Don't include it if we're forcing guest authors, or it's obvious our query is for a guest author's posts
if ( $this->force_guest_authors || stripos( $where, '.post_author = 0)' ) ) {
$maybe_both = false;
} else {
$maybe_both = apply_filters( 'coauthors_plus_should_query_post_author', true );
}
$maybe_both_query = $maybe_both ? '$1 OR' : '';
if ( ! empty( $terms ) ) {
$terms_implode = '';
$this->having_terms = '';
foreach ( $terms as $term ) {
$terms_implode .= '(' . $wpdb->term_taxonomy . '.taxonomy = \'' . $this->coauthor_taxonomy . '\' AND ' . $wpdb->term_taxonomy . '.term_id = \'' . $term->term_id . '\') OR ';
$this->having_terms .= ' ' . $wpdb->term_taxonomy . '.term_id = \'' . $term->term_id . '\' OR ';
}
$terms_implode = rtrim( $terms_implode, ' OR' );
// We need to check the query is the main query as a new query object would result in the wrong ID
$id = is_author() && $query->is_main_query() ? get_queried_object_id() : '\d+';
// If we have an ID but it's not a "real" ID that means that this isn't the first time the filter has fired and the object_id has already been replaced by a previous run of this filter. We therefore need to replace the 0
// This happens when wp_query::get_posts() is run multiple times.
// If previous condition resulted in this being a string there's no point wasting a db query looking for a user.
if ( $id !== '\d+' && false === get_user_by( 'id', $id ) ) {
$id = '\d+';
}
$maybe_both_query = $maybe_both ? '$0 OR' : '';
// add the taxonomy terms to the where query
$where = preg_replace( '/\(?\b(?:' . $wpdb->posts . '\.)?post_author\s*(?:=|IN)\s*\(?\d+\)?\)?/', ' (' . $maybe_both_query . ' ' . $terms_implode . ')', $where, 1 );
// if there is a duplicate post_author query parameter, remove the duplicate
$where = preg_replace( '/AND\s*\((?:' . $wpdb->posts . '\.)?post_author\s*\=\s*\d+\)/', ' ', $where, 1 );
// When WordPress generates query as 'post_author IN (id)', and there is a numeric $id, replace the often errant $id with the correct one - related to https://core.trac.wordpress.org/ticket/54268
if ( '\d+' !== $id ) {
$where = preg_replace( '/\b(?:' . $wpdb->posts . '\.)?post_author\s*IN\s*\(\d+\)/', ' (' . $wpdb->posts . '.post_author = ' . $id . ')', $where, 1 );
}
// the block targets the private posts clause (if it exists)
if (
is_user_logged_in() &&
is_author() &&
get_queried_object_id() != get_current_user_id()
) {
$current_coauthor = $this->get_coauthor_by( 'user_nicename', wp_get_current_user()->user_nicename );
$current_coauthor_term = $this->get_author_term( $current_coauthor );
if ( is_a( $current_coauthor_term, 'WP_Term' ) ) {
$current_user_query = $wpdb->term_taxonomy . '.taxonomy = \'' . $this->coauthor_taxonomy . '\' AND ' . $wpdb->term_taxonomy . '.term_id = \'' . $current_coauthor_term->term_id . '\'';
$this->having_terms .= ' ' . $wpdb->term_taxonomy . '.term_id = \'' . $current_coauthor_term->term_id . '\' OR ';
$where = preg_replace( '/(\b(?:' . $wpdb->posts . '\.)?post_author\s*=\s*(' . get_current_user_id() . ') )/', $current_user_query . ' ', $where, 1 ); // ' . $wpdb->postmeta . '.meta_id IS NOT NULL AND}
}
}
$this->having_terms = rtrim( $this->having_terms, ' OR' );
}
}
return $where;
}
/**
* Modify the author query posts SQL to include posts co-authored
*/
function posts_groupby_filter( $groupby, $query ) {
global $wpdb;
if ( $query->is_author() ) {
$post_type = $query->query_vars['post_type'];
if ( 'any' === $post_type ) {
$post_type = get_post_types( array( 'exclude_from_search' => false ) );
}
if ( ! empty( $post_type ) && ! is_object_in_taxonomy( $post_type, $this->coauthor_taxonomy ) ) {
return $groupby;
}
if ( $this->having_terms ) {
$having = 'MAX( IF ( ' . $wpdb->term_taxonomy . '.taxonomy = \'' . $this->coauthor_taxonomy . '\', IF ( ' . $this->having_terms . ',2,1 ),0 ) ) <> 1 ';
$groupby = $wpdb->posts . '.ID HAVING ' . $having;
}
}
return $groupby;
}
/**
* Filters post data before saving to db to set post_author
*/
function coauthors_set_post_author_field( $data, $postarr ) {
// Bail on autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $data;
}
// Bail on revisions
if ( ! $this->is_post_type_enabled( $data['post_type'] ) ) {
return $data;
}
// This action happens when a post is saved while editing a post
if ( isset( $_REQUEST['coauthors-nonce'] ) && isset( $_POST['coauthors'] ) && is_array( $_POST['coauthors'] ) ) { // phpcs:ignore
// rawurlencode() is for encoding coauthor name with special characters to compare names when getting coauthor.
$author = rawurlencode( sanitize_text_field( $_POST['coauthors'][0] ) ); // phpcs:ignore
if ( $author ) {
$author_data = $this->get_coauthor_by( 'user_nicename', $author );
// If it's a guest author and has a linked account, store that information in post_author
// because it'll be the valid user ID
if ( 'guest-author' == $author_data->type && ! empty( $author_data->linked_account ) ) {
$user = get_user_by( 'login', $author_data->linked_account );
if ( is_object( $user ) ) {
$data['post_author'] = $user->ID;
}
} elseif ( 'wpuser' === $author_data->type ) {
$data['post_author'] = $author_data->ID;
}
}
}
// If for some reason we don't have the co-authors fields set
if ( ! isset( $data['post_author'] ) ) {
$user = wp_get_current_user();
$data['post_author'] = $user->ID;
}
// Allow the 'post_author' to be forced to generic user if it doesn't match any users on the post
$data['post_author'] = apply_filters( 'coauthors_post_author_value', $data['post_author'], $postarr['ID'] );
return $data;
}
/**
* Update a post's co-authors on the 'save_post' hook
*
* @param $post_ID
*/
function coauthors_update_post( $post_id, $post ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! $this->is_post_type_enabled( $post->post_type ) ) {
return;
}
if ( $this->current_user_can_set_authors() ) {
// if current_user_can_set_authors and nonce valid
if ( isset( $_POST['coauthors-nonce'] ) && isset( $_POST['coauthors'] ) ) {
check_admin_referer( 'coauthors-edit', 'coauthors-nonce' );
$coauthors = (array) $_POST['coauthors'];
$coauthors = array_map( 'sanitize_title', $coauthors );
$this->add_coauthors( $post_id, $coauthors );
}
} else {
// If the user can't set authors and a co-author isn't currently set, we need to explicity set one
if ( ! $this->has_author_terms( $post_id ) ) {
$user = get_userdata( $post->post_author );
if ( $user ) {
$this->add_coauthors( $post_id, array( $user->user_nicename ) );
}
}
}
}
function has_author_terms( $post_id ) {
$terms = wp_get_object_terms( $post_id, $this->coauthor_taxonomy, array( 'fields' => 'ids' ) );
return ! empty( $terms ) && ! is_wp_error( $terms );
}
/**
* Add one or more co-authors as bylines for a post
*
* @param int
* @param array
* @param bool
* @param string
*/
public function add_coauthors( $post_id, $coauthors, $append = false, $query_type = 'user_nicename' ) {
global $current_user, $wpdb;
$post_id = (int) $post_id;
$insert = false;
// Best way to persist order
if ( $append ) {
$field = apply_filters( 'coauthors_post_list_pluck_field', 'user_login' );
$existing_coauthors = wp_list_pluck( get_coauthors( $post_id ), $field );
} else {
$existing_coauthors = array();
}
// A co-author is always required
// If no coauthor is provided AND no coauthors are currently set, assign to current user - retain old ones otherwise.
if ( empty( $coauthors ) ) {
if ( empty( $existing_coauthors ) ) {
$coauthors = array( $current_user->user_login );
} else {
$coauthors = $existing_coauthors;
}
}
// Set the co-authors
$coauthors = array_unique( array_merge( $existing_coauthors, $coauthors ) );
$coauthor_objects = array();
foreach ( $coauthors as &$author_name ) {
$field = apply_filters( 'coauthors_post_get_coauthor_by_field', $query_type, $author_name );
$author = $this->get_coauthor_by( $field, $author_name );
$coauthor_objects[] = $author;
$term = $this->update_author_term( $author );
if ( is_object( $term ) ) {
$author_name = $term->slug;
}
}
wp_set_post_terms( $post_id, $coauthors, $this->coauthor_taxonomy, false );
// If the original post_author is no longer assigned,
// update to the first WP_User $coauthor
$post_author_user = get_user_by( 'id', get_post( $post_id )->post_author );
if ( empty( $post_author_user )
|| ! in_array( $post_author_user->user_login, $coauthors ) ) {
foreach ( $coauthor_objects as $coauthor_object ) {
if ( 'wpuser' == $coauthor_object->type ) {
$new_author = $coauthor_object;
break;
}
}
// Uh oh, no WP_Users assigned to the post
if ( empty( $new_author ) ) {