forked from Automattic/Co-Authors-Plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template-tags.php
388 lines (326 loc) · 11.7 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
<?php
function get_coauthors( $post_id = 0, $args = array() ) {
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;
$defaults = array('orderby'=>'term_order', 'order'=>'ASC');
$args = wp_parse_args( $args, $defaults );
if ( $post_id ) {
$coauthor_terms = wp_get_post_terms( $post_id, $coauthors_plus->coauthor_taxonomy, $args );
if ( is_array( $coauthor_terms ) && !empty( $coauthor_terms ) ) {
foreach( $coauthor_terms as $coauthor ) {
$post_author = get_user_by( 'login', $coauthor->name );
// In case the user has been deleted while plugin was deactivated
if ( !empty( $post_author ) )
$coauthors[] = $post_author;
}
} else {
if ( $post ) {
$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;
}
}
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;
$coauthors = get_coauthors( $post_id );
if ( is_numeric( $user ) ) {
$user = get_userdata( $user );
$user = $user->user_login;
}
foreach( $coauthors as $coauthor ) {
if ( $user == $coauthor->user_login )
return true;
}
return false;
}
class CoAuthorsIterator {
var $position = -1;
var $original_authordata;
var $current_author;
var $authordata_array;
var $count;
function CoAuthorsIterator( $postID = 0 ){
global $post, $authordata, $wpdb;
$postID = (int)$postID;
if( !$postID && $post )
$postID = (int)$post->ID;
if( !$postID )
trigger_error(__('No post ID provided for CoAuthorsIterator constructor. Are you not in a loop or is $post not set?', 'co-authors-plus')); //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;
$this->position = -1;
return false;
}
//At the beginning of the loop
if( $this->position == 0 && !empty( $authordata ) )
$this->original_authordata = $authordata;
$authordata = $this->current_author = $this->authordata_array[$this->position];
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'] ) || $separators['before'] === NULL )
$separators['before'] = apply_filters( 'coauthors_default_before', $default_before );
if( ! isset( $separators['between'] ) || $separators['between'] === NULL )
$separators['between'] = apply_filters( 'coauthors_default_between', $default_between );
if( ! isset( $separators['betweenLast'] ) || $separators['betweenLast'] === NULL )
$separators['betweenLast'] = apply_filters( 'coauthors_default_between_last', $default_between_last );
if( ! isset( $separators['after'] ) || $separators['after'] === NULL )
$separators['after'] = apply_filters( 'coauthors_default_after', $default_after );
$output = '';
$i = new CoAuthorsIterator();
$output .= $separators['before'];
$i->iterate();
do {
$author_text = '';
if( $type == 'tag' )
$author_text = $tag( $tag_args );
elseif( $type == 'field' && isset( $i->current_author->$tag ) )
$author_text = $i->current_author->$tag;
elseif( $type == 'callback' && 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->is_first() && $i->count() > 2 )
$output .= $separators['between'];
if ( $i->is_last() && $i->count() > 1 ) {
$output = rtrim( $output, ' ' );
$output .= ' ' . $separators['betweenLast'];
}
$output .= $author_text;
} while( $i->iterate() );
$output .= $separators['after'];
if( $echo )
echo $output;
return $output;
}
//Provide co-author equivalents to the existing author template tags
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 );
}
function coauthors_posts_links( $between = null, $betweenLast = null, $before = null, $after = null, $echo = true ){
return coauthors__echo('coauthors_posts_links_single', 'callback', array(
'between' => $between,
'betweenLast' => $betweenLast,
'before' => $before,
'after' => $after
), null, $echo );
}
function coauthors_posts_links_single( $author ) {
return sprintf(
'<a href="%1$s" title="%2$s">%3$s</a>',
get_author_posts_url( $author->ID, $author->user_nicename ),
esc_attr( sprintf( __( 'Posts by %s', 'co-authors-plus' ), get_the_author() ) ),
get_the_author()
);
}
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 );
}
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 );
}
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 );
}
function coauthors_links($between = null, $betweenLast = null, $before = null, $after = null, $echo = true ) {
return coauthors__echo('coauthors_links_single', 'callback', array(
'between' => $between,
'betweenLast' => $betweenLast,
'before' => $before,
'after' => $after
), null, $echo );
}
function coauthors_links_single( $author ) {
if ( get_the_author_meta('url') ) {
return sprintf( '<a href="%s" title="%s" rel="external">%s</a>',
get_the_author_meta('url'),
esc_attr( sprintf(__("Visit %s’s website"), get_the_author()) ),
get_the_author()
);
} else {
return get_the_author();
}
}
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 );
}
function get_the_coauthor_meta( $field ) {
global $wp_query, $post;
$coauthors = get_coauthors();
$meta = array();
foreach( $coauthors as $coauthor ) {
$user_id = $coauthor->ID;
$meta[$user_id] = get_the_author_meta( $field, $user_id );
}
return $meta;
}
function the_coauthor_meta( $field, $user_id = 0 ) {
// TODO: need before after options
echo get_the_coauthor_meta($field, $user_id);
}
/**
* 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.
* exclude_admin (boolean) (true): Exclude the 'admin' user that is installed by default.
* 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.
* @param array $args The argument array.
* @return null|string The output, if echo is set to false.
*
* NOTE: This is not perfect and probably won't work that well.
*
*/
function coauthors_wp_list_authors($args = '') {
global $wpdb, $coauthors_plus;
$defaults = array(
'optioncount' => false, 'exclude_admin' => true,
'show_fullname' => false, 'hide_empty' => true,
'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
'style' => 'list', 'html' => true
);
$r = wp_parse_args( $args, $defaults );
extract($r, EXTR_SKIP);
$return = '';
$authors = $coauthors_plus->search_authors();
$author_terms = get_terms( 'author' );
foreach ( (array) $author_terms as $author_term ) {
$author_count[$author_term->slug] = $author_term->count;
}
foreach ( (array) $authors as $author ) {
$link = '';
$author = get_userdata( $author->ID );
$posts = (isset($author_count[$author->user_login])) ? $author_count[$author->user_login] : 0;
$name = $author->display_name;
if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') )
$name = "$author->first_name $author->last_name";
if( !$html ) {
if ( $posts == 0 ) {
if ( ! $hide_empty )
$return .= $name . ', ';
} else
$return .= $name . ', ';
// No need to go further to process HTML.
continue;
}
if ( !($posts == 0 && $hide_empty) && 'list' == $style )
$return .= '<li>';
if ( $posts == 0 ) {
if ( ! $hide_empty )
$link = $name;
} else {
$link = '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . esc_attr( sprintf(__("Posts by %s", 'co-authors-plus'), $author->display_name) ) . '">' . $name . '</a>';
if ( (! empty($feed_image)) || (! empty($feed)) ) {
$link .= ' ';
if (empty($feed_image))
$link .= '(';
$link .= '<a href="' . get_author_feed_link($author->ID) . '"';
if ( !empty($feed) ) {
$title = ' title="' . esc_attr($feed) . '"';
$alt = ' alt="' . esc_attr($feed) . '"';
$name = $feed;
$link .= $title;
}
$link .= '>';
if ( !empty($feed_image) )
$link .= "<img src=\"" . esc_url($feed_image) . "\" style=\"border: none;\"$alt$title" . ' />';
else
$link .= $name;
$link .= '</a>';
if ( empty($feed_image) )
$link .= ')';
}
if ( $optioncount )
$link .= ' ('. $posts . ')';
}
if ( !($posts == 0 && $hide_empty) && 'list' == $style )
$return .= $link . '</li>';
else if ( ! $hide_empty )
$return .= $link . ', ';
}
$return = trim($return, ', ');
if ( ! $echo )
return $return;
echo $return;
}