This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exa-bylines.php
419 lines (341 loc) · 11.4 KB
/
exa-bylines.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
<?php
/**
* Plugin Name: Exa Bylines
* Description: Allows multiple authors for an article.
* Version: 1.0
* Author: Jason Chan for The Badger Herald
* License: GPL2
*/
/**
*
* Add Exa Bylines meta box to post edit screen
* and setup saving actio upon different hooks.
*
*
*/
function exa_bylines_setup(){
/*add meta box */
add_meta_box(
'exa-bylines-meta-box',
'Multiple Bylines',
'exa_bylines_meta_box',
'post',
'side',
'high'
);
/* Save! */
add_action( 'save_post', 'exa_bylines_save_data');
add_action( 'pre_post_update', 'exa_bylines_save_data');
add_action( 'edit_post', 'exa_bylines_save_data');
add_action( 'publish_post', 'exa_bylines_save_data');
add_action( 'edit_page_form', 'exa_bylines_save_data');
/* Let old formats be processed! */
exa_bylines_old_posts();
}
add_action( 'load-post.php', 'exa_bylines_setup' );
add_action( 'load-post-new.php', 'exa_bylines_setup' );
function exa_bylines(){
return;
}
function exa_bylines_activated_plugin( ){
exa_bylines_old_posts();
return ;
}
add_action( 'activated_plugin', 'exa_bylines_activated_plugin' );
/**
*
* Add Herald Bylines meta box to post edit screen
*
*
*
*/
function exa_bylines_meta_box( $post){
//retrieve the list of users(authors)
$userMeta = exa_bylines_get_active_users( $post);
//stores list of users active
$userActive = array();
//create <ul>
echo "<ul id='exa_bylines_current_authors'>";
//if no users are returned, say so.
if( !$userMeta) :
?>
<li class="exa_byline_no_byline"></li>
<?php
//otherwise, generate list
else:
//create a random number for the remove button
//so the page doesn't jump.
$rand = rand(10000,99999);
//for every user, create a <li>
foreach ($userMeta as $user) :
?>
<li class='exa_byline_current_author' exa-byline-userID=<?php echo $user['id']; ?> >
<label><?php echo $user['display_name']; ?><?php echo $user['guest'] ? ' (guest)' : ''; ?></label>
<a href="#<?php echo $rand; ?>" class='exa_byline_current_author_remove'
name="exa_byline_current_author_remove_<?php echo $user['id']; ?> ">Remove</a>
</li>
<?php
//save id into $userActive
$userActive[] = $user['id'];
endforeach;
endif;
//close <ul>
echo "</ul>";
// create the HTML to add users
?>
<hr>
<p><b>Existing author</b><br>choose and press enter</p>
<input type="text" id="exa_byline_input" placeholder="Type name here..."/>
<hr>
<p><b>Can't find someone?</b><br>Just add the name below</p>
<input type="text" id="exa_byline_input_guest" placeholder="Guest name"/>
<input type="button" id="exa_byline_input_guest_button" class="button exa_byline_input_guest_button" value="Add"/>
<input type="hidden" id="exa_byline_active_user_list" name="exa_byline_active_user_list" value="<?php echo implode(",", $userActive); ?>" />
<?php
exa_bylines_old_posts();
}
/**
*
*
*
* @return (mixed) 2D array of userFullNames, or empty string if no users are found.
*/
function exa_bylines_get_active_users( $post){
//retrieve custom post metadata '_exa_byline'
$userIDs = get_post_meta($post->ID, '_exa_byline');
//return array(array('display_name' => 'Jason Chan', 'id' => 'Jason Chan', 'guest' => true), array('display_name' => 'Will Haynes', 'id' => '321', 'guest' => false));
//if no userIDs are retrieved(ie. single user posts, old posts, new posts)
if( !$userIDs)
return '';
//stores the full name and ID of users as 2-D array
$userFullNames = array();
//find the display name and id
foreach( $userIDs as $userID){
if( is_numeric($userID)){
$userFullNames[] = array('display_name' => get_user_meta( $userID, 'first_name', true).' '.get_user_meta( $userID, 'last_name', true),
'id' => $userID,
'guest' => false);
}else
$userFullNames[] = array('display_name' => $userID, 'id' => $userID, 'guest' => true);
}
return $userFullNames;
}
add_action('add_meta_boxes', 'exa_bylines', 10, 2);
/**
*
* finds the list of users (except subscribers) and localize the data
* for jquery-ui-autocomplete to use.
*
*
*
*/
function exa_bylines_autocomplete_data(){
//enqueue the js and css file for this plugin
wp_enqueue_script('exa_bylines_js', plugin_dir_url( __FILE__ ) . 'js/exa_bylines_js.js', array('jquery','jquery-ui-autocomplete','jquery-ui-sortable'));
wp_enqueue_style('exa_bylines_css', plugin_dir_url( __FILE__ ) . 'css/exa_bylines_css.css');
$users = exa_bylines_get_all_valid_users();
// localize the all-user information for js use.
wp_localize_script('exa_bylines_js','exa_bylines_all_users', $users);
}
add_action( 'load-post.php', 'exa_bylines_autocomplete_data' );
add_action( 'load-post-new.php', 'exa_bylines_autocomplete_data' );
function exa_bylines_get_all_valid_users( $js = true, $additionalFields = array() ){
global $wpdb;
$users = array();
$fields = array_merge(array('user_login','display_name', 'ID'), $additionalFields);
$badUsers = $wpdb->get_col(
"SELECT `ID` FROM $wpdb->users WHERE `display_name` LIKE \"% and %\" OR `display_name` LIKE \"% & %\" ORDER BY `ID` DESC"
);
//retrieve all users with exclusion
$allUsers = get_users(array('order'=>'ASC',
'orderby'=>'login',
'fields' => $fields,
'exclude'=> $badUsers
)
);
//retrieve full name and ID and store them in $users as an array. for js
foreach( $allUsers as $user){
if( $user->display_name && $user->display_name != ' ') //users without a display name shouldn't be used.
if( $js)
$users[] = array("label" => $user->display_name, "value" => $user->ID);
else{
$userMeta = array();
foreach( $fields as $field){
$userMeta[$field] = $user->$field;
}
$users[] = $userMeta;
}
}
return $users;
}
/*
*
*
*
*/
function exa_bylines_save_data( $post_id, $manualInput = false){
global $wpdb;
add_post_meta( $post_id, '_exa_bylines_all', null, true);
if( isset($_POST['exa_byline_active_user_list']) && $_POST['exa_byline_active_user_list']){
$activeList = $_POST['exa_byline_active_user_list'];
}else if( $manualInput == true){
$activeList = $manualInput;
}else{
delete_post_meta( $post_id, '_exa_bylines_all');
return;
}
update_post_meta( $post_id, '_exa_bylines_all', $activeList);
$authors = explode(',', $activeList);
delete_post_meta( $post_id, '_exa_byline');
foreach( $authors as $author){
add_post_meta( $post_id, '_exa_byline', $author, false);
}
// set primary author
foreach( $authors as $author){
if( is_numeric($author)){
$wpdb->update('wp_posts', array("post_author" => $author), array('ID' => $post_id));
return;
}
}
return ;
}
/**
* front-end use of exa_bylines. This function creates the byline in an article.
* while able to call this function directly, this function also filters the_author(),
* a native Wordpress template tag. However, for individual author_links, call directly.
*
* @param (string)name. is the default author's name
* @param (boolean)link, optional. makes each author a link to the author page.
* @param (array)linkArgs, opt. inserts <a> attributes to help style the author links. ignored if $link = false
*
* @return (string) the new author link. or the default.
*/
function exa_bylines_the_authors( $name, $link = false, $linkArgs = array()){
global $post;
if( $post)
$authors = get_post_meta( $post->ID, '_exa_bylines_all', true);
else
return;
$attrs = '';
foreach( $linkArgs as $attr => $values){
$attrs .= $attr . '="' . $values . '" ';
}
if( $authors){
$name = '';
$authors = explode(',', $authors);
foreach( $authors as $key => $author){
$user = get_userdata( $author);
if( $user && $user->display_name != ''){
if( $link)
$name .= '<a href="' . get_author_posts_url( $user->ID) . '" '. $attrs .'>' . $user->display_name . '</a>';
else
$name .= $user->display_name;
}
else{
if( $link)
$name .= "<a $attrs >" . $author . "</a>";
else
$name .= $author;
}
if( $key < count($authors) -2 )
$name .= ', ';
else if( $key < count($authors) -1)
$name .= ' and ';
}
}else{
if( !$name){
$user = get_userdata( $post->post_author);
if( $link)
$name = '<a href="' . $user->user_url . '" '. $attrs .'>' . $user->display_name . '</a>';
else
$name = $user->display_name;
}
}
return $name;
}
add_filter('the_author', 'exa_bylines_the_authors');
add_filter('get_the_author_display_name', 'exa_bylines_the_authors');
/**
*
* retrieve the list of exa_bylines users or, if not set, just the default user.
*
* @param none.
*
* @return (array) array of WP_User(s).
*/
function exa_bylines_get_the_authors(){
global $post;
$authorsList = array();
$authors = get_post_meta( $post->ID, '_exa_byline');
if( $authors){
foreach( $authors as $author){
$authorsList[] = get_userdata( $author) ? get_userdata( $author) : $author;
}
}else
$authorsList = array( get_userdata( $post->post_author));
return $authorsList;
}
// under construction
function exa_bylines_author_archive( $query){
if( is_author() && $query->is_main_query()){
}
}
function exa_bylines_old_posts(){
global $wpdb;
$allUsers = exa_bylines_get_all_valid_users( false, array('user_nicename')); //false: don't format for js
if ( $badUsers = $wpdb->get_results(
"SELECT * FROM $wpdb->users WHERE (`display_name` LIKE \"% and %\") OR (`display_name` LIKE \"% & %\") OR (`display_name` LIKE \"% & %\") ORDER BY `id` DESC", ARRAY_A
)){
foreach( $badUsers as $badUser){
$badUserID = $badUser['ID'];
$badUserDisplayName = $badUser['display_name'];
$badUserSeperateNames = array();
$goodUsers = array(); //Display name, id
//if have three+ authors( , , , ... and )
if( strpos($badUserDisplayName, ',') != -1){
$badUserDisplayName = str_replace(array(',and', ', and', ' & ', ' & '), ' and ', $badUserDisplayName);
$badUserSeperateNames = explode(',', $badUserDisplayName);
//and the and at the last
$size = sizeof($badUserSeperateNames);
$lastTwoNames = explode(' and ', $badUserSeperateNames[ $size - 1]);
if( sizeof($lastTwoNames) == 2){
$badUserSeperateNames[ $size - 1] = $lastTwoNames[0];
$badUserSeperateNames[ $size ] = $lastTwoNames[1];
}
}else{
$badUserDisplayName = str_replace(array(' & ',' & ', ', And '), ' and ', $badUserDisplayName);
$badUserSeperateNames = explode(' and ', $badUserDisplayName);
}
//attempts to match existing users.
$allUsersName = array_map(function($e){return $e['display_name'];}, $allUsers);
//active user list
$activeList = '';
//print_r( $allUsersName);
foreach( $badUserSeperateNames as $count => $badUserSeperateName){
$key = array_search( trim($badUserSeperateName), $allUsersName);
if( $key !== false){
$goodUsers[] = array( 'display_name' => $allUsers[$key]['display_name'], 'id' => $allUsers[$key]['ID']);
$activeList .= $allUsers[$key]['ID'];
}
else{
$goodUsers[] = array( 'display_name' => $badUserSeperateName, 'id' => $badUserSeperateName);
$activeList .= $badUserSeperateName;
}
if( $count != sizeof( $badUserSeperateNames)-1)
$activeList .= ',';
}
//get posts tied to this $badUser
$badUserPostIDs = get_posts( array(
'author' => $badUserID,
'posts_per_page' => -1,
'fields' => 'ids'
));
foreach( $badUserPostIDs as $badUserPostID){
if( get_post_meta( $badUserPostID, '_exa_bylines_all') == false)
exa_bylines_save_data( $badUserPostID, $activeList);
}
}
return true;
}
return false;
}