forked from Automattic/Co-Authors-Plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template-tags.php
795 lines (695 loc) · 25.9 KB
/
template-tags.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
<?php
function get_coauthors( $post_id = 0 ) {
global $post, $post_ID, $coauthors_plus, $wpdb;
$coauthors = array();
$post_id = (int) $post_id;
if ( ! $post_id && $post_ID ) {
$post_id = $post_ID;
}
if ( ! $post_id && $post ) {
$post_id = $post->ID;
}
if ( $post_id ) {
$coauthor_terms = cap_get_coauthor_terms_for_post( $post_id );
if ( is_array( $coauthor_terms ) && ! empty( $coauthor_terms ) ) {
foreach ( $coauthor_terms as $coauthor ) {
$coauthor_slug = preg_replace( '#^cap\-#', '', $coauthor->slug );
$post_author = $coauthors_plus->get_coauthor_by( 'user_nicename', $coauthor_slug );
// In case the user has been deleted while plugin was deactivated
if ( ! empty( $post_author ) ) {
$coauthors[] = $post_author;
}
}
} elseif ( ! $coauthors_plus->force_guest_authors ) {
if ( $post && $post_id == $post->ID ) {
$post_author = get_userdata( $post->post_author );
} else {
$post_author = get_userdata( $wpdb->get_var( $wpdb->prepare( "SELECT post_author FROM $wpdb->posts WHERE ID = %d", $post_id ) ) );
}
if ( ! empty( $post_author ) ) {
$coauthors[] = $post_author;
}
} // the empty else case is because if we force guest authors, we don't ever care what value wp_posts.post_author has.
}
// remove duplicate $coauthors objects from mapping user accounts to guest authors accounts
$coauthors = array_unique( $coauthors, SORT_REGULAR );
$coauthors = apply_filters( 'get_coauthors', $coauthors, $post_id );
return $coauthors;
}
/**
* Checks to see if the the specified user is author of the current global post or post (if specified)
*
* @param object|int $user
* @param int $post_id
*/
function is_coauthor_for_post( $user, $post_id = 0 ) {
global $post;
if ( ! $post_id && $post ) {
$post_id = $post->ID;
}
if ( ! $post_id ) {
return false;
}
if ( ! $user ) {
return false;
}
$coauthors = get_coauthors( $post_id );
if ( is_numeric( $user ) ) {
$user = get_userdata( $user );
$user = $user->user_login;
} elseif ( isset( $user->user_login ) ) {
$user = $user->user_login;
} else {
return false;
}
foreach ( $coauthors as $coauthor ) {
if ( $user == $coauthor->user_login || $user == $coauthor->linked_account ) {
return true;
}
}
return false;
}
class CoAuthorsIterator {
var $position = -1;
var $original_authordata;
var $current_author;
var $authordata_array;
var $count;
function __construct( $postID = 0 ) {
global $post, $authordata, $wpdb;
$postID = (int) $postID;
if ( ! $postID && $post ) {
$postID = (int) $post->ID;
}
if ( ! $postID ) {
trigger_error( esc_html( 'No post ID provided for CoAuthorsIterator constructor. Are you not in a loop or is $post not set?' ) ); // return null;
}
$this->original_authordata = $this->current_author = $authordata;
$this->authordata_array = get_coauthors( $postID );
$this->count = count( $this->authordata_array );
}
function iterate() {
global $authordata;
$this->position++;
// At the end of the loop
if ( $this->position > $this->count - 1 ) {
$authordata = $this->current_author = $this->original_authordata; // phpcs:ignore
$this->position = -1;
return false;
}
// At the beginning of the loop
if ( 0 === $this->position && ! empty( $authordata ) ) {
$this->original_authordata = $authordata;
}
$authordata = $this->current_author = $this->authordata_array[ $this->position ]; // phpcs:ignore
return true;
}
function get_position() {
if ( $this->position === -1 ) {
return false;
}
return $this->position;
}
function is_last() {
return $this->position === $this->count - 1;
}
function is_first() {
return $this->position === 0;
}
function count() {
return $this->count;
}
function get_all() {
return $this->authordata_array;
}
}
// Helper function for the following new template tags
function coauthors__echo( $tag, $type = 'tag', $separators = array(), $tag_args = null, $echo = true ) {
// Define the standard output separator. Constant support is for backwards compat.
// @see https://github.com/danielbachhuber/Co-Authors-Plus/issues/12
$default_before = ( defined( 'COAUTHORS_DEFAULT_BEFORE' ) ) ? COAUTHORS_DEFAULT_BEFORE : '';
$default_between = ( defined( 'COAUTHORS_DEFAULT_BETWEEN' ) ) ? COAUTHORS_DEFAULT_BETWEEN : ', ';
$default_between_last = ( defined( 'COAUTHORS_DEFAULT_BETWEEN_LAST' ) ) ? COAUTHORS_DEFAULT_BETWEEN_LAST : __( ' and ', 'co-authors-plus' );
$default_after = ( defined( 'COAUTHORS_DEFAULT_AFTER' ) ) ? COAUTHORS_DEFAULT_AFTER : '';
if ( ! isset( $separators['before'] ) || null === $separators['before'] ) {
$separators['before'] = apply_filters( 'coauthors_default_before', $default_before );
}
if ( ! isset( $separators['between'] ) || null === $separators['between'] ) {
$separators['between'] = apply_filters( 'coauthors_default_between', $default_between );
}
if ( ! isset( $separators['betweenLast'] ) || null === $separators['betweenLast'] ) {
$separators['betweenLast'] = apply_filters( 'coauthors_default_between_last', $default_between_last );
}
if ( ! isset( $separators['after'] ) || null === $separators['after'] ) {
$separators['after'] = apply_filters( 'coauthors_default_after', $default_after );
}
$output = '';
$i = new CoAuthorsIterator();
$output .= $separators['before'];
$i->iterate();
do {
$author_text = '';
if ( 'tag' === $type ) {
$author_text = $tag( $tag_args );
} elseif ( 'field' === $type && isset( $i->current_author->$tag ) ) {
$author_text = $i->current_author->$tag;
} elseif ( 'callback' === $type && is_callable( $tag ) ) {
$author_text = call_user_func( $tag, $i->current_author );
}
// Fallback to user_login if we get something empty
if ( empty( $author_text ) ) {
$author_text = $i->current_author->user_login;
}
// Append separators
if ( $i->count() - $i->position == 1 ) { // last author or only author
$output .= $author_text;
} elseif ( $i->count() - $i->position == 2 ) { // second to last
$output .= $author_text . $separators['betweenLast'];
} else {
$output .= $author_text . $separators['between'];
}
} while ( $i->iterate() );
$output .= $separators['after'];
if ( $echo ) {
echo $output; // phpcs:ignore
}
return $output;
}
/**
* Outputs the co-authors display names, without links to their posts.
* Co-Authors Plus equivalent of the_author() template tag.
*
* @param string $between Delimiter that should appear between the co-authors
* @param string $betweenLast Delimiter that should appear between the last two co-authors
* @param string $before What should appear before the presentation of co-authors
* @param string $after What should appear after the presentation of co-authors
* @param bool $echo Whether the co-authors should be echoed or returned. Defaults to true.
*/
function coauthors( $between = null, $betweenLast = null, $before = null, $after = null, $echo = true ) {
return coauthors__echo(
'display_name',
'field',
array(
'between' => $between,
'betweenLast' => $betweenLast,
'before' => $before,
'after' => $after,
),
null,
$echo
);
}
/**
* Outputs the co-authors display names, with links to their posts.
* Co-Authors Plus equivalent of the_author_posts_link() template tag.
*
* @param string $between Delimiter that should appear between the co-authors
* @param string $betweenLast Delimiter that should appear between the last two co-authors
* @param string $before What should appear before the presentation of co-authors
* @param string $after What should appear after the presentation of co-authors
* @param bool $echo Whether the co-authors should be echoed or returned. Defaults to true.
*/
function coauthors_posts_links( $between = null, $betweenLast = null, $before = null, $after = null, $echo = true ) {
global $coauthors_plus_template_filters;
$modify_filter = ! empty( $coauthors_plus_template_filters ) && $coauthors_plus_template_filters instanceof CoAuthors_Template_Filters;
if ( $modify_filter ) {
/**
* Removing "the_author" filter so that it won't get called in loop and append names for each author.
*
* Ref : https://github.com/Automattic/Co-Authors-Plus/issues/279
*/
remove_filter( 'the_author', array( $coauthors_plus_template_filters, 'filter_the_author' ) );
}
$coauthors_posts_links = coauthors__echo(
'coauthors_posts_links_single',
'callback',
array(
'between' => $between,
'betweenLast' => $betweenLast,
'before' => $before,
'after' => $after,
),
null,
$echo
);
if ( $modify_filter ) {
add_filter( 'the_author', array( $coauthors_plus_template_filters, 'filter_the_author' ) );
}
return $coauthors_posts_links;
}
/**
* Outputs a single co-author linked to their post archive.
*
* @param object $author
* @return string
*/
function coauthors_posts_links_single( $author ) {
// Return if the fields we are trying to use are not sent
if ( ! isset( $author->ID, $author->user_nicename, $author->display_name ) ) {
_doing_it_wrong(
'coauthors_posts_links_single',
'Invalid author object used',
'3.2'
);
return;
}
$args = array(
'before_html' => '',
'href' => get_author_posts_url( $author->ID, $author->user_nicename ),
'rel' => 'author',
/* translators: Author display name. */
'title' => sprintf( __( 'Posts by %s', 'co-authors-plus' ), apply_filters( 'the_author', $author->display_name ) ),
'class' => 'author url fn',
'text' => apply_filters( 'the_author', $author->display_name ),
'after_html' => '',
);
$args = apply_filters( 'coauthors_posts_link', $args, $author );
$single_link = sprintf(
'<a href="%1$s" title="%2$s" class="%3$s" rel="%4$s">%5$s</a>',
esc_url( $args['href'] ),
esc_attr( $args['title'] ),
esc_attr( $args['class'] ),
esc_attr( $args['rel'] ),
esc_html( $args['text'] )
);
return $args['before_html'] . $single_link . $args['after_html'];
}
/**
* Outputs the co-authors first names, without links to their posts.
*
* @param string $between Delimiter that should appear between the co-authors
* @param string $betweenLast Delimiter that should appear between the last two co-authors
* @param string $before What should appear before the presentation of co-authors
* @param string $after What should appear after the presentation of co-authors
* @param bool $echo Whether the co-authors should be echoed or returned. Defaults to true.
*/
function coauthors_firstnames( $between = null, $betweenLast = null, $before = null, $after = null, $echo = true ) {
return coauthors__echo(
'get_the_author_meta',
'tag',
array(
'between' => $between,
'betweenLast' => $betweenLast,
'before' => $before,
'after' => $after,
),
'first_name',
$echo
);
}
/**
* Outputs the co-authors last names, without links to their posts.
*
* @param string $between Delimiter that should appear between the co-authors
* @param string $betweenLast Delimiter that should appear between the last two co-authors
* @param string $before What should appear before the presentation of co-authors
* @param string $after What should appear after the presentation of co-authors
* @param bool $echo Whether the co-authors should be echoed or returned. Defaults to true.
*/
function coauthors_lastnames( $between = null, $betweenLast = null, $before = null, $after = null, $echo = true ) {
return coauthors__echo(
'get_the_author_meta',
'tag',
array(
'between' => $between,
'betweenLast' => $betweenLast,
'before' => $before,
'after' => $after,
),
'last_name',
$echo
);
}
/**
* Outputs the co-authors nicknames, without links to their posts.
*
* @param string $between Delimiter that should appear between the co-authors
* @param string $betweenLast Delimiter that should appear between the last two co-authors
* @param string $before What should appear before the presentation of co-authors
* @param string $after What should appear after the presentation of co-authors
* @param bool $echo Whether the co-authors should be echoed or returned. Defaults to true.
*/
function coauthors_nicknames( $between = null, $betweenLast = null, $before = null, $after = null, $echo = true ) {
return coauthors__echo(
'get_the_author_meta',
'tag',
array(
'between' => $between,
'betweenLast' => $betweenLast,
'before' => $before,
'after' => $after,
),
'nickname',
$echo
);
}
/**
* Outputs the co-authors display names, with links to their websites if they've provided them.
*
* @param string $between Delimiter that should appear between the co-authors
* @param string $betweenLast Delimiter that should appear between the last two co-authors
* @param string $before What should appear before the presentation of co-authors
* @param string $after What should appear after the presentation of co-authors
* @param bool $echo Whether the co-authors should be echoed or returned. Defaults to true.
*/
function coauthors_links( $between = null, $betweenLast = null, $before = null, $after = null, $echo = true ) {
global $coauthors_plus_template_filters;
$modify_filter = ! empty( $coauthors_plus_template_filters ) && $coauthors_plus_template_filters instanceof CoAuthors_Template_Filters;
if ( $modify_filter ) {
/**
* Removing "the_author" filter so that it won't get called in loop and append names for each author.
*
* Ref : https://github.com/Automattic/Co-Authors-Plus/issues/279
*/
remove_filter( 'the_author', array( $coauthors_plus_template_filters, 'filter_the_author' ) );
}
$coauthors_links = coauthors__echo(
'coauthors_links_single',
'callback',
array(
'between' => $between,
'betweenLast' => $betweenLast,
'before' => $before,
'after' => $after,
),
null,
$echo
);
if ( $modify_filter ) {
add_filter( 'the_author', array( $coauthors_plus_template_filters, 'filter_the_author' ) );
}
return $coauthors_links;
}
/**
* Outputs the co-authors email addresses
*
* @param string $between Delimiter that should appear between the email addresses
* @param string $betweenLast Delimiter that should appear between the last two email addresses
* @param string $before What should appear before the presentation of email addresses
* @param string $after What should appear after the presentation of email addresses
* @param bool $echo Whether the co-authors should be echoed or returned. Defaults to true.
*/
function coauthors_emails( $between = null, $betweenLast = null, $before = null, $after = null, $echo = true ) {
return coauthors__echo(
'get_the_author_meta',
'tag',
array(
'between' => $between,
'betweenLast' => $betweenLast,
'before' => $before,
'after' => $after,
),
'user_email',
$echo
);
}
/**
* Outputs a single co-author, linked to their website if they've provided one.
*
* @param object $author
* @return string
*/
function coauthors_links_single( $author ) {
if ( 'guest-author' === $author->type && get_the_author_meta( 'website' ) ) {
return sprintf(
'<a href="%s" title="%s" rel="author external">%s</a>',
esc_url( get_the_author_meta( 'website' ) ),
/* translators: Author display name. */
esc_attr( sprintf( __( 'Visit %s’s website', 'co-authors-plus' ), esc_html( get_the_author() ) ) ),
esc_html( get_the_author() )
);
} elseif ( get_the_author_meta( 'url' ) ) {
return sprintf(
'<a href="%s" title="%s" rel="author external">%s</a>',
esc_url( get_the_author_meta( 'url' ) ),
/* translators: Author display name. */
esc_attr( sprintf( __( 'Visit %s’s website', 'co-authors-plus' ), esc_html( get_the_author() ) ) ),
esc_html( get_the_author() )
);
} else {
return esc_html( get_the_author() );
}
}
/**
* Outputs the co-authors IDs
*
* @param string $between Delimiter that should appear between the co-authors
* @param string $betweenLast Delimiter that should appear between the last two co-authors
* @param string $before What should appear before the presentation of co-authors
* @param string $after What should appear after the presentation of co-authors
* @param bool $echo Whether the co-authors should be echoed or returned. Defaults to true.
*/
function coauthors_ids( $between = null, $betweenLast = null, $before = null, $after = null, $echo = true ) {
return coauthors__echo(
'ID',
'field',
array(
'between' => $between,
'betweenLast' => $betweenLast,
'before' => $before,
'after' => $after,
),
null,
$echo
);
}
/**
* Outputs the co-authors Meta Data
*
* @param string $field Required The user field to retrieve.[login, email, nicename, display_name, url, type]
* @param string $user_id Optional The user ID for meta
*
* @return array $meta Value of the user field
*/
function get_the_coauthor_meta( $field, $user_id = false ) {
global $coauthors_plus;
if ( ! $user_id ) {
$coauthors = get_coauthors();
} else {
$coauthor_data = $coauthors_plus->get_coauthor_by( 'id', $user_id );
$coauthors = array();
if ( ! empty( $coauthor_data ) ) {
$coauthors[] = $coauthor_data;
}
}
$meta = array();
if ( in_array( $field, array( 'login', 'pass', 'nicename', 'email', 'url', 'registered', 'activation_key', 'status' ) ) ) {
$field = 'user_' . $field;
}
foreach ( $coauthors as $coauthor ) {
$user_id = $coauthor->ID;
if ( isset( $coauthor->type ) && 'user_url' === $field ) {
if ( 'guest-author' === $coauthor->type ) {
$field = 'website';
}
} elseif ( 'website' === $field ) {
$field = 'user_url';
}
if ( isset( $coauthor->$field ) ) {
$meta[ $user_id ] = $coauthor->$field;
} else {
$meta[ $user_id ] = '';
}
}
return $meta;
}
function the_coauthor_meta( $field, $user_id = 0 ) {
// TODO: need before after options
$coauthor_meta = get_the_coauthor_meta( $field, $user_id );
foreach ( $coauthor_meta as $meta ) {
echo esc_html( $meta );
}
}
/**
* Returns an array of blog users and co-authors.
* @param array $args An argument array to customize the returned result.
* number (int) (20): The maximum number of (co-)authors to return.
* guest_authors_only (boolean) (false): If true, include only guest authors without WP users.
* authors_with_posts_only (boolean) (false): If true, don't query for authors with no posts.
* orderby (string) ('name'): A field to order the authors by {@see WP_Term_Query::__construct()}
*
* @return array A unique array of WP_User-like objects each containing data for a use or a co-author.
* The returned array may contain a mix of native WP users as well as guest authors as
* designated by $args. You can use the $user->type property to check for the user type.
*/
function coauthors_get_users( $args = array() ) {
global $coauthors_plus;
$defaults = array(
'number' => 20, // A sane limit to start to avoid breaking all the things
'guest_authors_only' => false,
'authors_with_posts_only' => false,
'orderby' => 'name',
);
$args = wp_parse_args( $args, $defaults );
$term_args = array(
'orderby' => $args['orderby'],
'number' => (int) $args['number'],
/*
* Historically, this was set to always be `0` ignoring `$args['hide_empty']` value
* To avoid any backwards incompatibility, inventing `authors_with_posts_only` that defaults to false
*/
'hide_empty' => (bool) $args['authors_with_posts_only'],
);
$author_terms = get_terms( $coauthors_plus->coauthor_taxonomy, $term_args );
$authors = array();
foreach ( $author_terms as $author_term ) {
// Something's wrong in the state of Denmark
if ( false === ( $coauthor = $coauthors_plus->get_coauthor_by( 'user_login', $author_term->name ) ) ) {
continue;
}
$authors[ $author_term->name ] = $coauthor;
// only show guest authors if the $args is set to true
if ( ! $args['guest_authors_only'] || $authors[ $author_term->name ]->type === 'guest-author' ) {
$authors[ $author_term->name ]->post_count = $author_term->count;
} else {
unset( $authors[ $author_term->name ] );
}
}
$authors = apply_filters( 'coauthors_wp_list_authors_array', $authors );
// remove duplicates from linked accounts
$linked_accounts = array_unique( array_column( $authors, 'linked_account' ) );
foreach ( $linked_accounts as $linked_account ) {
unset( $authors[ $linked_account ] );
}
return $authors;
}
/**
* List all the *co-authors* of the blog, with several options available.
* optioncount (boolean) (false): Show the count in parenthesis next to the author's name.
* show_fullname (boolean) (false): Show their full names.
* hide_empty (boolean) (true): Don't show authors without any posts.
* feed (string) (''): If isn't empty, show links to author's feeds.
* feed_image (string) (''): If isn't empty, use this image to link to feeds.
* echo (boolean) (true): Set to false to return the output, instead of echoing.
* authors_with_posts_only (boolean) (false): If true, don't query for authors with no posts.
*
* @param array $args The argument array.
* @return null|string The output, if echo is set to false.
*/
function coauthors_wp_list_authors( $args = array() ) {
$defaults = array(
'optioncount' => false,
'show_fullname' => false,
'hide_empty' => true,
'feed' => '',
'feed_image' => '',
'feed_type' => '',
'echo' => true,
'style' => 'list',
'html' => true,
'number' => 20, // A sane limit to start to avoid breaking all the things
'guest_authors_only' => false,
'authors_with_posts_only' => false,
'orderby' => 'name',
);
$args = wp_parse_args( $args, $defaults );
$return = '';
$authors = coauthors_get_users( $args );
foreach ( (array) $authors as $author ) {
$link = '';
if ( $args['show_fullname'] && ( $author->first_name && $author->last_name ) ) {
$name = "$author->first_name $author->last_name";
} else {
$name = $author->display_name;
}
if ( ! $args['html'] ) {
if ( 0 === $author->post_count ) {
if ( ! $args['hide_empty'] ) {
$return .= $name . ', ';
}
} else {
$return .= $name . ', ';
}
// No need to go further to process HTML.
continue;
}
if ( ! ( 0 === $author->post_count && $args['hide_empty'] ) && 'list' == $args['style'] ) {
$return .= '<li>';
}
if ( 0 === $author->post_count ) {
if ( ! $args['hide_empty'] ) {
$link = $name;
}
} else {
/* translators: Author display name. */
$link = '<a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '" title="' . esc_attr( sprintf( __( 'Posts by %s', 'co-authors-plus' ), $name ) ) . '">' . esc_html( $name ) . '</a>';
if ( ( ! empty( $args['feed_image'] ) ) || ( ! empty( $args['feed'] ) ) ) {
$link .= ' ';
if ( empty( $args['feed_image'] ) ) {
$link .= '(';
}
$link .= '<a href="' . esc_url( get_author_feed_link( $author->ID, $args['feed_type'] ) ) . '"';
$alt = '';
$title = '';
if ( ! empty( $args['feed'] ) ) {
$title = ' title="' . esc_attr( $args['feed'] ) . '"';
$alt = ' alt="' . esc_attr( $args['feed'] ) . '"';
$name = $args['feed'];
$link .= $title;
}
$link .= '>';
if ( ! empty( $args['feed_image'] ) ) {
$link .= '<img src="' . esc_url( $args['feed_image'] ) . "\" style=\"border: none;\"$alt$title" . ' />';
} else {
$link .= $name;
}
$link .= '</a>';
if ( empty( $args['feed_image'] ) ) {
$link .= ')';
}
}
if ( $args['optioncount'] ) {
$link .= ' (' . $author->post_count . ')';
}
}
if ( ! ( 0 === $author->post_count && $args['hide_empty'] ) && 'list' == $args['style'] ) {
$return .= $link . '</li>';
} elseif ( ! $args['hide_empty'] ) {
$return .= $link . ', ';
}
}
$return = trim( $return, ', ' );
if ( ! $args['echo'] ) {
return $return;
}
echo $return; // phpcs:ignore
}
/**
* Retrieve a Co-Author's Avatar.
*
* Since Guest Authors doesn't enforce unique email addresses, simply loading the avatar by email won't work when
* multiple Guest Authors share the same address.
*
* This is a replacement for using get_avatar(), which only operates on email addresses and cannot differentiate
* between Guest Authors (who may share an email) and regular user accounts
*
* @param object $coauthor The Co Author or Guest Author object.
* @param int $size The desired size.
* @param string $default Optional. URL for the default image or a default type. Accepts '404'
* (return a 404 instead of a default image), 'retro' (8bit), 'monsterid'
* (monster), 'wavatar' (cartoon face), 'indenticon' (the "quilt"),
* 'mystery', 'mm', or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF),
* or 'gravatar_default' (the Gravatar logo). Default is the value of the
* 'avatar_default' option, with a fallback of 'mystery'.
* @param string $alt Optional. Alternative text to use in <img> tag. Default false.
* @param array|string $class Optional. Array or string of additional classes to add to the <img> element. Default null.
* @return string The image tag for the avatar, or an empty string if none could be determined.
*/
function coauthors_get_avatar( $coauthor, $size = 32, $default = '', $alt = false, $class = null ) {
global $coauthors_plus;
if ( ! is_object( $coauthor ) ) {
return '';
}
if ( isset( $coauthor->type ) && 'guest-author' == $coauthor->type ) {
$guest_author_thumbnail = $coauthors_plus->guest_authors->get_guest_author_thumbnail( $coauthor, $size, $class );
if ( $guest_author_thumbnail ) {
return $guest_author_thumbnail;
}
}
// Make sure we're dealing with an object for which we can retrieve an email
if ( isset( $coauthor->user_email ) ) {
return get_avatar( $coauthor->user_email, $size, $default, $alt, array( 'class' => $class ) );
}
// Nothing matched, an invalid object was passed.
return '';
}