forked from publishpress/PublishPress-Authors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.php
1847 lines (1609 loc) · 60.5 KB
/
Plugin.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
/**
* @package MultipleAuthors
* @author PublishPress <[email protected]>
* @copyright Copyright (C) 2018 PublishPress. All rights reserved.
* @license GPLv2 or later
* @since 1.0.0
*/
namespace MultipleAuthors;
use MA_Multiple_Authors;
use MultipleAuthors\Classes\Legacy\Util;
use MultipleAuthors\Classes\Objects\Author;
use MultipleAuthors\Classes\Post_Editor;
use MultipleAuthors\Classes\Query;
use MultipleAuthors\Classes\Utils;
use MultipleAuthors\Traits\Author_box;
use WP_Query;
defined('ABSPATH') or die('No direct script access allowed.');
class Plugin
{
use Author_box;
// Name for the taxonomy we're suing to store relationships
// and the post type we're using to store co-authors
public $coauthor_taxonomy = 'author';
public $coreauthors_meta_box_name = 'authordiv';
public $coauthors_meta_box_name = 'coauthorsdiv';
public $gravatar_size = 25;
public $ajax_search_fields = [
'display_name',
'first_name',
'last_name',
'user_login',
'user_nicename',
'ID',
'user_email',
];
public $having_terms = '';
/**
* __construct()
*/
public function __construct()
{
// Register our models
add_action('init', [$this, 'action_init']);
add_action('init', [$this, 'action_init_late'], 15);
// Installation hooks
add_action(
'multiple_authors_install',
['MultipleAuthors\\Classes\\Installer', 'runInstallTasks']
);
add_action(
'multiple_authors_upgrade',
['MultipleAuthors\\Classes\\Installer', 'runUpgradeTasks']
);
if (
is_admin()
&& (!defined('DOING_AJAX') || !DOING_AJAX)
&& (!defined('DOING_CRON') || !DOING_CRON)
&& (!defined('PUBLISHPRESS_AUTHORS_BYPASS_INSTALLER') || !PUBLISHPRESS_AUTHORS_BYPASS_INSTALLER)
) {
add_action('admin_init', [$this, 'manage_installation'], 2000);
}
add_filter('get_usernumposts', [$this, 'filter_count_user_posts'], 10, 2);
add_filter('get_authornumposts', [$this, 'filter_count_author_posts'], 10, 2);
// Filter to allow coauthors to edit posts
add_filter('user_has_cap', [$this, 'allow_coauthors_edit_post'], 10, 4);
// Restricts WordPress from blowing away term order on bulk edit
add_filter('wp_get_object_terms', [$this, 'filter_wp_get_object_terms'], 10, 4);
// Support Jetpack Open Graph Tags
add_filter('jetpack_open_graph_tags', [$this, 'filter_jetpack_open_graph_tags'], 10, 2);
// Filter to send comment moderation notification e-mail to multiple authors
add_filter('comment_moderation_recipients', 'cap_filter_comment_moderation_email_recipients', 10, 2);
// Delete CoAuthor Cache on Post Save & Post Delete
add_action('save_post', [$this, 'clear_cache']);
add_action('delete_post', [$this, 'clear_cache']);
add_action('set_object_terms', [$this, 'clear_cache_on_terms_set'], 10, 6);
// Widget support
add_action('widgets_init', [$this, 'action_widget_init']);
// Author box to the content
add_filter('the_content', [$this, 'filter_the_content']);
/**
* @deprecated Since 3.13.2. Use publishpress_authors_box instead.
*/
if (PUBLISHPRESS_AUTHORS_LOAD_LEGACY_SHORTCODES) {
add_shortcode('author_box', [$this, 'shortcodeAuthorsBox']);
}
add_shortcode('publishpress_authors_box', [$this, 'shortcodeAuthorsBox']);
add_shortcode('publishpress_authors_data', [$this, 'shortcodeAuthorsData']);
add_shortcode('publishpress_authors_list', [$this, 'shortcodeAuthorsList']);
// Action to display the author box
add_action('pp_multiple_authors_show_author_box', [$this, 'action_echo_author_box'], 10, 5);
/*
* @todo: Improve hooks to only add them if post type is selected or if it is an admin page.
*/
// Fix the author page.
// Use posts_selection since it's after WP_Query has built the request and before it's queried any posts
add_filter('posts_selection', [$this, 'fix_query_for_author_page']);
add_filter('the_author', [$this, 'filter_the_author']);
add_action(
'init',
[
'MultipleAuthors\\Classes\\Content_Model',
'action_init_late_register_taxonomy_for_object_type',
],
16
);
add_filter(
'term_link',
['MultipleAuthors\\Classes\\Content_Model', 'filter_term_link'],
10,
3
);
add_filter(
'author_link',
['MultipleAuthors\\Classes\\Content_Model', 'filter_author_link'],
10,
2
);
add_filter(
'the_author_display_name',
['MultipleAuthors\\Classes\\Content_Model', 'filter_author_display_name'],
10,
2
);
add_filter(
'update_term_metadata',
['MultipleAuthors\\Classes\\Content_Model', 'filter_update_term_metadata'],
10,
4
);
add_action(
'parse_request',
['MultipleAuthors\\Classes\\Content_Model', 'action_parse_request']
);
add_action(
'user_register',
['MultipleAuthors\\Classes\\Author_Editor', 'action_user_register'],
20
);
// Hide the core Author field for the selected post types.
add_action('init', [Post_Editor::class, 'remove_core_author_field'], 9999);
// Admin customizations.
if (is_admin()) {
add_action('admin_init', [$this, 'admin_init']);
add_action(
'admin_init',
['MultipleAuthors\\Classes\\Post_Editor', 'action_admin_init']
);
add_action(
'admin_init',
['MultipleAuthors\\Classes\\Term_Editor', 'action_admin_init']
);
add_filter(
'manage_edit-author_columns',
[
'MultipleAuthors\\Classes\\Author_Editor',
'filter_manage_edit_author_columns',
]
);
add_filter(
'list_table_primary_column',
[
'MultipleAuthors\\Classes\\Author_Editor',
'filter_list_table_primary_column',
]
);
add_filter(
'manage_author_custom_column',
[
'MultipleAuthors\\Classes\\Author_Editor',
'filter_manage_author_custom_column',
],
10,
3
);
add_filter(
'user_row_actions',
['MultipleAuthors\\Classes\\Author_Editor', 'filter_user_row_actions'],
10,
2
);
add_filter(
'author_row_actions',
['MultipleAuthors\\Classes\\Author_Editor', 'filter_author_row_actions'],
10,
2
);
add_action(
'author_term_edit_form_top',
['MultipleAuthors\\Classes\\Author_Editor', 'action_author_edit_form_fields_tab']
);
add_action(
'author_edit_form_fields',
['MultipleAuthors\\Classes\\Author_Editor', 'action_author_edit_form_fields']
);
add_action(
'author_term_new_form_tag',
['MultipleAuthors\\Classes\\Author_Editor', 'action_new_form_tag'],
10
);
add_filter(
'wp_insert_term_data',
['MultipleAuthors\\Classes\\Author_Editor', 'filter_insert_term_data'],
10,
3
);
add_filter(
'created_author',
['MultipleAuthors\\Classes\\Author_Editor', 'action_created_author'],
10
);
add_action(
'edited_author',
['MultipleAuthors\\Classes\\Author_Editor', 'action_edited_author']
);
add_filter(
'bulk_actions-edit-author',
['MultipleAuthors\\Classes\\Author_Editor', 'filter_author_bulk_actions']
);
add_filter(
'handle_bulk_actions-edit-author',
['MultipleAuthors\\Classes\\Author_Editor', 'handle_author_bulk_actions'],
10,
3
);
add_action(
'admin_notices',
['MultipleAuthors\\Classes\\Author_Editor', 'admin_notices']
);
add_filter(
'pre_insert_term',
['MultipleAuthors\\Classes\\Author_Editor', 'filter_pre_insert_term'],
10,
2
);
add_action(
'wp_ajax_mapped_author_validation',
['MultipleAuthors\\Classes\\Admin_Ajax', 'handle_mapped_author_validation']
);
add_action(
'wp_ajax_handle_author_slug_generation',
['MultipleAuthors\\Classes\\Admin_Ajax', 'handle_author_slug_generation']
);
add_filter('admin_footer_text', [$this, 'update_footer_admin']);
}
// Query modifications for the author page
add_action(
'pre_get_posts',
['MultipleAuthors\\Classes\\Query', 'fix_query_pre_get_posts']
);
add_filter(
'posts_where',
['MultipleAuthors\\Classes\\Query', 'filter_author_posts_where'],
10,
2
);
add_filter(
'posts_join',
['MultipleAuthors\\Classes\\Query', 'filter_posts_join'],
10,
2
);
add_filter(
'posts_groupby',
['MultipleAuthors\\Classes\\Query', 'filter_posts_groupby'],
10,
2
);
add_filter(
'pre_handle_404',
[$this, 'fix_404_for_authors'],
10,
2
);
add_action(
'wp_head',
['MultipleAuthors\\Classes\\Query', 'fix_query_pre_get_posts'],
1
);
// Query modifications for the admin posts lists
add_filter(
'posts_where',
['MultipleAuthors\\Classes\\Query', 'filter_admin_posts_list_where'],
10,
2
);
add_filter(
'posts_join',
['MultipleAuthors\\Classes\\Query', 'filter_posts_list_join'],
10,
2
);
add_filter(
'posts_groupby',
['MultipleAuthors\\Classes\\Query', 'filter_posts_list_groupby'],
10,
2
);
// Author search
add_action(
'wp_ajax_authors_search',
['MultipleAuthors\\Classes\\Admin_Ajax', 'handle_authors_search']
);
add_action(
'wp_ajax_authors_users_search',
['MultipleAuthors\\Classes\\Admin_Ajax', 'handle_users_search']
);
add_action(
'wp_ajax_authors_filter_authors_search',
['MultipleAuthors\\Classes\\Admin_Ajax', 'handle_filter_authors_search']
);
add_action(
'wp_ajax_author_create_from_user',
['MultipleAuthors\\Classes\\Admin_Ajax', 'handle_author_create_from_user']
);
add_action(
'wp_ajax_author_get_user_data',
['MultipleAuthors\\Classes\\Admin_Ajax', 'handle_author_get_user_data']
);
// Post integration
add_action(
'add_meta_boxes',
['MultipleAuthors\\Classes\\Post_Editor', 'action_add_meta_boxes_late'],
100
);
add_action(
'save_post',
['MultipleAuthors\\Classes\\Post_Editor', 'action_save_post_authors_metabox'],
10,
2
);
add_action(
'save_post',
[
'MultipleAuthors\\Classes\\Post_Editor',
'action_save_post_set_initial_author',
],
10,
3
);
add_action(
'restrict_manage_posts',
['MultipleAuthors\\Classes\\Post_Editor', 'post_author_filter_field']
);
// Notification Workflow support
add_filter(
'ppma_get_author_data',
['MultipleAuthors\\Classes\\Content_Model', 'filter_ma_get_author_data'],
10,
3
);
// Theme template tag filters.
add_filter(
'get_the_archive_title',
[
'MultipleAuthors\\Classes\\Integrations\\Theme',
'filter_get_the_archive_title',
]
);
add_filter(
'get_the_archive_description',
[
'MultipleAuthors\\Classes\\Integrations\\Theme',
'filter_get_the_archive_description',
]
);
add_filter('cme_multiple_authors_capabilities', [$this, 'filterCMECapabilities'], 20);
$this->addTestShortcode();
}
private function addTestShortcode()
{
add_shortcode('publishpress_authors_test', [$this, 'shortcodeTest']);
}
public function shortcodeTest()
{
echo '<b>PublishPress Authors:</b> shortcode rendered successfully!';
}
/**
* Manages the installation detecting if this is the first time this module runs or is an upgrade.
* If no version is stored in the options, we treat as a new installation. Otherwise, we check the
* last version. If different, it is an upgrade or downgrade.
*/
public function manage_installation()
{
$option_name = 'PP_AUTHORS_VERSION';
$previous_version = get_option($option_name);
$current_version = PP_AUTHORS_VERSION;
if (!apply_filters('publishpress_authors_skip_installation', false, $previous_version, $current_version)) {
if (empty($previous_version)) {
/**
* Action called when the module is installed.
*
* @param string $current_version
*/
do_action('multiple_authors_install', $current_version);
} elseif (version_compare($previous_version, $current_version, '>')) {
/**
* Action called when the module is downgraded.
*
* @param string $previous_version
*/
do_action('multiple_authors_downgrade', $previous_version);
} elseif (version_compare($previous_version, $current_version, '<')) {
/**
* Action called when the module is upgraded.
*
* @param string $previous_version
*/
do_action('multiple_authors_upgrade', $previous_version);
}
}
if ($current_version !== $previous_version) {
update_option($option_name, $current_version, true);
}
}
/**
* Register the taxonomy used for managing relationships,
* and the custom post type to store the author data.
*/
public function action_init()
{
// Allow PublishPress Authors to be easily translated
load_plugin_textdomain(
'publishpress-authors',
null,
plugin_basename(PP_AUTHORS_BASE_PATH) . '/languages/'
);
add_filter('taxonomy_labels_author', [$this, 'filter_author_taxonomy_labels']);
}
public function filter_author_taxonomy_labels($labels)
{
global $pagenow;
if (
is_admin()
&& $pagenow === 'term.php'
&& isset($_GET['taxonomy']) && $_GET['taxonomy'] === 'author'
&& isset($_GET['tag_ID'])
) {
$author = Author::get_by_term_id((int)$_GET['tag_ID']);
if (is_object($author) && !is_wp_error($author) && (int)$author->user_id === get_current_user_id()) {
$labels->edit_item = __('Edit My Author Profile', 'publishpress-authors');
}
}
$labels->name_field_description = esc_html__(
'This is the name that will show on the site.',
'publishpress-authors'
);
$labels->slug_field_description = esc_html__(
'This forms part of the URL for the author’s profile page. If you choose a Mapped User, this URL is taken from the user’s account and can not be changed.',
'publishpress-authors'
);
return $labels;
}
/**
* Register the 'author' taxonomy and add post type support
*/
public function action_init_late()
{
// Register new taxonomy so that we can store all the relationships
$args = [
'labels' => [
'name' => _x(
'Authors',
'taxonomy general name',
'publishpress-authors'
),
'singular_name' => _x(
'Author',
'taxonomy singular name',
'publishpress-authors'
),
'search_items' => __('Search Authors', 'publishpress-authors'),
'popular_items' => __('Popular Authors', 'publishpress-authors'),
'all_items' => __('All Authors', 'publishpress-authors'),
'parent_item' => __('Parent Author', 'publishpress-authors'),
'parent_item_colon' => __('Parent Author:', 'publishpress-authors'),
'edit_item' => __('Edit Author', 'publishpress-authors'),
'view_item' => __('View Author', 'publishpress-authors'),
'update_item' => __('Update Author', 'publishpress-authors'),
'add_new_item' => __('New Author', 'publishpress-authors'),
'new_item_name' => __('New Author', 'publishpress-authors'),
'separate_items_with_commas' => __(
'Separate authors with commas',
'publishpress-authors'
),
'add_or_remove_items' => __('Add or remove authors', 'publishpress-authors'),
'choose_from_most_used' => __(
'Choose from the most used Authors',
'publishpress-authors'
),
'not_found' => __('No authors found.', 'publishpress-authors'),
'menu_name' => __('Author', 'publishpress-authors'),
'back_to_items' => __('Back to Authors', 'publishpress-authors'),
],
'public' => false,
'hierarchical' => false,
'sort' => true,
'args' => [
'orderby' => 'term_order',
],
'capabilities' => [
'manage_terms' => 'ppma_manage_authors',
'edit_terms' => 'ppma_manage_authors',
'delete_terms' => 'ppma_manage_authors',
'assign_terms' => 'ppma_edit_post_authors',
],
'show_ui' => true,
'show_in_menu' => true,
'show_in_quick_edit' => false,
'meta_box_cb' => false,
'query_var' => 'ppma_author',
'rewrite' => 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)) {
add_action('edited_term_taxonomy', [$this, 'action_edited_term_taxonomy_flush_cache'], 10, 2);
}
$supported_post_types = Utils::get_post_types_that_support_authors();
register_taxonomy($this->coauthor_taxonomy, $supported_post_types, $args);
}
/**
* Initialize the plugin for the admin
*/
public function admin_init()
{
// Add the main JS script and CSS file
add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']);
// Add quick-edit author select field
add_action('quick_edit_custom_box', [$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', [$this, '_filter_manage_users_columns']);
add_action('manage_users_custom_column', [$this, 'addUsersPostsCountColumn'], 10, 3);
add_action('manage_users_sortable_columns', [$this, 'makeUsersPostsColumnSortable'], 10, 3);
add_action('pre_user_query', [$this, 'addUsersPostsColumnToQuery']);
// Apply some targeted filters
add_action('load-edit.php', [$this, 'load_edit']);
add_filter('pp_authors_show_footer', [$this, 'filterDisplayFooter'], 10);
}
/**
* Display the PublishPress footer on the custom post pages
*/
public function update_footer_admin($footer)
{
if ($this->shouldDisplayFooter()) {
$legacyPlugin = Factory::getLegacyPlugin();
$html = '<div class="pressshack-admin-wrapper">';
$html .= $this->print_default_footer(
$legacyPlugin->modules->multiple_authors,
false
);
// We do not close the div by purpose. The footer contains it.
// Add the wordpress footer
$html .= $footer;
if (!defined('PUBLISHPRESS_AUTHORS_FOOTER_DISPLAYED')) {
define('PUBLISHPRESS_AUTHORS_FOOTER_DISPLAYED', true);
}
return $html;
}
return $footer;
}
private function shouldDisplayFooter()
{
/**
* @param bool $shouldDisplay
*
* @return bool
*/
return apply_filters('pp_authors_show_footer', false);
}
/**
* Echo or returns the default footer
*
* @param object $current_module
* @param bool $echo
*
* @return string
*/
public function print_default_footer($current_module, $echo = true)
{
$html = '';
/**
* @param bool $showFooter
* @param string $currentModule
*/
$showFooter = apply_filters('pp_authors_show_footer', true, $current_module);
if ($showFooter) {
$container = Factory::get_container();
$twig = $container['twig'];
$html = $twig->render(
'footer-base.twig',
[
'current_module' => $current_module,
'plugin_name' => __('PublishPress Authors', 'publishpress-authors'),
'plugin_slug' => 'publishpress-authors',
'plugin_url' => PP_AUTHORS_URL,
'rating_message' => __(
'If you like %s please leave us a %s rating. Thank you!',
'publishpress-authors'
),
]
);
}
if (!$echo) {
return $html;
}
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $html;
return '';
}
public function filterDisplayFooter($shouldDisplay = true)
{
global $current_screen;
if (defined('PUBLISHPRESS_AUTHORS_FOOTER_DISPLAYED')) {
return false;
}
if ($current_screen->base === 'edit-tags' && $current_screen->taxonomy === 'author') {
return true;
}
if ($current_screen->base === 'term' && $current_screen->taxonomy === 'author') {
return true;
}
if ($current_screen->base === 'authors_page_ppma-modules-settings') {
return true;
}
return $shouldDisplay;
}
public function action_widget_init()
{
register_widget('MultipleAuthors\\Widget');
register_widget('MultipleAuthors\\Authors_Widget');
}
/**
* Unset the post count column because it's going to be inaccurate and provide our own
* @param $columns
*
* @return mixed
*/
public function _filter_manage_users_columns($columns)
{
if (isset($columns['posts'])) {
unset($columns['posts']);
}
$columns['posts_count'] = sprintf(
'%s <i class="dashicons dashicons-info-outline" title="%s"></i>',
__('Posts', 'publishpress-authors'),
sprintf(
__('Published posts of the following post types: %s', 'publishpress-authors'),
implode(', ', Utils::getAuthorTaxonomyPostTypes())
)
);
return $columns;
}
public function addUsersPostsCountColumn($value, $column_name, $user_id)
{
if ($column_name !== 'posts_count') {
return $value;
}
$author = Author::get_by_user_id($user_id);
if (!is_object($author) || is_wp_error($author)) {
return 0;
}
$numPosts = $author->getTerm()->count;
$value = sprintf(
'<a href="%s" class="edit"><span aria-hidden="true">%s</span><span class="screen-reader-text">%s</span></a>',
"edit.php?author={$user_id}",
$numPosts,
sprintf(
/* translators: %s: Number of posts. */
_n('%s post by this author', '%s posts by this author', $numPosts),
number_format_i18n($numPosts)
)
);
return $value;
}
public function makeUsersPostsColumnSortable($columns)
{
$columns['posts_count'] = 'posts_count';
return $columns;
}
/**
* @param WP_User_Query $query
*/
public function addUsersPostsColumnToQuery($query)
{
if (!is_admin()) {
return;
}
$orderBy = $query->get('orderby');
if ('posts_count' === $orderBy) {
global $wpdb;
$query->query_fields .= ', tt.count as posts_count';
$query->query_from .= " LEFT JOIN $wpdb->termmeta as tm ON ($wpdb->users.ID = tm.meta_value AND tm.meta_key = \"user_id\")"; // phpcs:ignore WordPressVIPMinimum.Variables.RestrictedVariables.user_meta__wpdb__users
$query->query_from .= " LEFT JOIN $wpdb->term_taxonomy as tt ON (tm.`term_id` = tt.term_id AND tt.taxonomy = \"author\")";
$query->query_orderby = 'ORDER BY posts_count ' . $query->get('order');
}
}
/**
* Quick Edit co-authors box.
*/
public function _action_quick_edit_custom_box($column_name, $post_type)
{
if (
'authors' !== $column_name || !Utils::is_post_type_enabled(
$post_type
) || !Utils::current_user_can_set_authors()
) {
return;
}
?>
<label class="inline-edit-group inline-edit-coauthors">
<span class="title"><?php esc_html_e('Authors', 'publishpress-authors') ?></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.',
'publishpress-authors'
),
['strong' => []]
); ?></p>
</div>
<?php wp_nonce_field('coauthors-edit', 'coauthors-nonce'); ?>
</label>
<?php
}
/**
* If we're forcing PublishPress Authors 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;
}
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
$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', 'publishpress-authors')
);
}
wp_cache_delete('author-term-' . $coauthor->user_nicename, 'publishpress-authors');
}
/**
* Get a co-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)
{
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';
}
// Ensure we aren't doing the lookup by the prefixed value
if ('login' === $key || 'slug' === $key) {
$value = preg_replace('#^cap\-#', '', $value);
}
$user = get_user_by($key, $value);
if (!$user) {
return false;
}
$user->type = 'wpuser';
return $user;
break;
}
return false;
}
/**
* Add one or more co-authors as bylines for a post
*
* @param int
* @param array
* @param bool
*
* @deprecated Since 3.14.7
*/
public function add_coauthors($post_id, $coauthors, $append = false)
{
global $current_user, $wpdb;
$post_id = (int)$post_id;
// Best way to persist order
if ($append) {
$existing_coauthors = wp_list_pluck(get_post_authors($post_id), 'user_login');
} else {
$existing_coauthors = [];
}
// A co-author is always required
if (empty($coauthors)) {
$coauthors = [$current_user->user_login];
}
// Set the coauthors
$coauthors = array_unique(array_merge($existing_coauthors, $coauthors));
$coauthor_objects = [];
foreach ($coauthors as &$author_name) {
$author = $this->get_coauthor_by('user_nicename', $author_name);
$coauthor_objects[] = $author;
$term = $this->update_author_term($author);
$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)) {
return false;
}
$wpdb->update($wpdb->posts, ['post_author' => $new_author->ID], ['ID' => $post_id]);
clean_post_cache($post_id);
}
return true;
}
/**
* Update the author term for a given co-author
*
* @param object $coauthor The co-author object
*
* @return object|false $success Term object if successful, false if not
* @since 3.0
*
*/
public function update_author_term($coauthor)
{
if (!is_object($coauthor)) {
return false;
}
// Update the taxonomy term to include details about the user for searching
$search_values = [];
foreach ($this->ajax_search_fields as $search_field) {
$search_values[] = $coauthor->$search_field;
}
$term_description = implode(' ', $search_values);
if ($term = $this->get_author_term($coauthor)) {
if ($term->description != $term_description) {
wp_update_term(
$term->term_id,
$this->coauthor_taxonomy,
['description' => $term_description]
);
}
} else {