Co-Authors Plus - Version 2.5

Version Description

Download this release

Release Info

Developer batmoo
Plugin Icon wp plugin Co-Authors Plus
Version 2.5
Comparing to
See all releases

Code changes from version 2.1.1 to 2.5

admin.css DELETED
@@ -1,50 +0,0 @@
1
- #coauthors-table {
2
-
3
- }
4
- div.dbx-content #coauthors-table {
5
- width:100%;
6
- }
7
- #coauthors-table td {
8
- padding-left:0;
9
- padding-right:0;
10
- vertical-align: middle;
11
- background: #F9F9F9;
12
- }
13
- #coauthors-table td.select select {
14
- margin-top:0;
15
- }
16
- div.dbx-content #coauthors-table td.select select {
17
- width:100%;
18
- }
19
-
20
- span.author-tag {
21
- display:block;
22
- padding: 5px 3px;
23
- color: #21759B;
24
- border-bottom: 1px solid #21759B;
25
- cursor: text;
26
- }
27
- span.author-tag:hover {
28
- cursor: text;
29
- background: #EAF2FA ;
30
- color:#D54E21;
31
- }
32
- input.coauthor-suggest {
33
- margin:5px 0;
34
- color:#888;
35
- }
36
- input.coauthor-suggest:focus {
37
- color: #333;
38
- }
39
-
40
- span.delete-coauthor {
41
- padding: 3px;
42
- text-decoration: underline;
43
- color:#FF0000;
44
- font-size:11px;
45
- }
46
- span.delete-coauthor:hover {
47
- cursor: pointer;
48
- background: #FF0000;
49
- color: #fff;
50
- }
admin.js DELETED
@@ -1,484 +0,0 @@
1
- jQuery(document).ready(function () {
2
-
3
- /*
4
- * Get an author's name from their ID
5
- * Iterates through the coauthors-select input box until finds the entry with the associated ID
6
- * @param int User ID
7
- *
8
- */
9
- /*
10
- function coauthors_get_author_name( authorID ) {
11
- var select = jQuery('#coauthors-select');
12
-
13
- if(authorID){
14
- //Find the provided author ID
15
- var name = "";
16
- select.find("option").each(function(i){
17
- if(this.value == authorID) {
18
- name = this.innerHTML;
19
- return;
20
- }
21
-
22
- });
23
- return name;
24
- }
25
- return false;
26
-
27
- }
28
- */
29
-
30
- /*
31
- * Selects author (specified by authorID) within the appropriate select box (specified by selectID)
32
- * @param int
33
- * @param string ID of the select box
34
- */
35
- function coauthors_select_author( author, selectID ) {
36
- if(!selectID) selectID = '#post_author_override';
37
-
38
- var select = jQuery(selectID);
39
-
40
- if(author.id){
41
- //Find the provided author ID
42
- select.find("option").each(function(i){
43
- if(this.value == author.id) {
44
- jQuery(this).attr('selected','true');
45
- // Fix to retain order of selected coauthors
46
- //jQuery(this).appendTo(select);
47
- return false;
48
- }
49
- });
50
- }
51
-
52
- }
53
-
54
- /*
55
- * Unselects author
56
- * @param string Name of the Author to remove
57
- */
58
- function coauthors_remove_author( authorName ) {
59
- var select = jQuery('#coauthors-select');
60
- if(authorName){
61
- //Find the provided author ID
62
- select.find("option").each(function(i){
63
- if(this.innerHTML == authorName) {
64
- jQuery(this).removeAttr('selected');
65
- return true;
66
- }
67
- });
68
- }
69
- }
70
-
71
- /*
72
- * Click handler for the delete button
73
- * @param event
74
- */
75
- var coauthors_delete_onclick = function(event){
76
-
77
- if(confirm(i18n.coauthors.confirm_delete)) {
78
- var tr = jQuery(this).parent().parent();
79
- var name = tr.find('span.author-tag').text();
80
- coauthors_remove_author(name);
81
- tr.remove();
82
-
83
- return true;
84
- }
85
- return false;
86
- };
87
-
88
- var coauthors_edit_onclick = function(event) {
89
- var tag = jQuery(event.currentTarget);
90
-
91
- var co = tag.prev();
92
-
93
- tag.hide();
94
- co.show()
95
- .focus()
96
- ;
97
-
98
- co.previousAuthor = tag.text();
99
- }
100
-
101
- /*
102
- * Save coauthor
103
- * @param int Author ID
104
- * @param string Author Name
105
- * @param object The autosuggest input box
106
- */
107
- function coauthors_save_coauthor(author, co) {
108
-
109
- coauthors_remove_author(co.next().text());
110
-
111
- // get sibling <span>, update, and hide
112
- co.next()
113
- .html(author.name)
114
- .show()
115
- .next()
116
- .val(author.login)
117
- ;
118
-
119
- // select new author
120
- if(co.attr('name')=='coauthors-main') {
121
- coauthors_select_author( author );
122
- }
123
-
124
- }
125
-
126
-
127
- /*
128
- * Add coauthor
129
- * @param int Author ID
130
- * @param string Author Name
131
- * @param object The autosuggest input box
132
- * @param boolean Initial set up or not?
133
- */
134
- // function coauthors_add_coauthor(authorID, authorName, co, init, count){
135
- function coauthors_add_coauthor(author, co, init, count){
136
-
137
- // Check if editing
138
- if(co && co.next().attr('class')=='author-tag') {
139
- coauthors_save_coauthor(author, co);
140
-
141
- } else {
142
- // Not editing, so we create a new author entry
143
- if(count == 0) {
144
- var coName = (count == 0) ? 'coauthors-main' : '';
145
- // Add new author to <select>
146
- coauthors_select_author( author );
147
- var options = {};
148
- } else {
149
- var options = { addDelete: true, addEdit: false };
150
- }
151
- // Create autosuggest box and text tag
152
- if(!co) var co = coauthors_create_autosuggest(author, coName)
153
- var tag = coauthors_create_author_tag(author);
154
- var input = coauthors_create_author_hidden_input(author);
155
-
156
- coauthors_add_to_table(co, tag, input, options);
157
-
158
- if(!init) {
159
- // Create new author-suggest and append it to a new row
160
- var newCO = coauthors_create_autosuggest('', false);
161
- coauthors_add_to_table(newCO);
162
- }
163
- }
164
-
165
- co.bind('blur', coauthors_stop_editing);
166
-
167
- // Set the value for the auto-suggest box to the Author's name and hide it
168
- co.val(unescape(author.name))
169
- .hide()
170
- .unbind('focus')
171
- ;
172
-
173
- return true;
174
- }
175
-
176
-
177
- /*
178
- * Add the autosuggest box and text tag to the Co-Authors table
179
- * @param object Autosuggest input box
180
- * @param object Text tag
181
- * @param
182
- */
183
- function coauthors_add_to_table( co, tag, input, options ) {
184
- if(co) {
185
- var td = jQuery('<td></td>')
186
- .addClass('suggest')
187
- .append(co)
188
- .append(tag)
189
- .append(input)
190
- ;
191
- var tr = jQuery('<tr></tr>');
192
- tr.append(td);
193
-
194
- //Add buttons to row
195
- if(tag) coauthors_insert_author_edit_cells(tr, options);
196
-
197
- jQuery('#coauthors-table').append(tr);
198
- }
199
- }
200
-
201
- /*
202
- * Adds a delete and edit button next to an author
203
- * @param object The row to which the new author should be added
204
- */
205
- function coauthors_insert_author_edit_cells(tr, options){
206
-
207
- var td = jQuery('<td></td>')
208
- .addClass('coauthors-author-options')
209
- ;
210
-
211
- /*
212
- if(options.addEdit) {
213
- var editBtn = jQuery('<span></span>')
214
- .addClass('edit-coauthor')
215
- .text(i18n.coauthors.edit_label)
216
- .bind('click', coauthors_edit_onclick)
217
- ;
218
- td.append(editBtn);
219
- }
220
- */
221
- if(options.addDelete) {
222
- var deleteBtn = jQuery('<span></span>')
223
- .addClass('delete-coauthor')
224
- .text(i18n.coauthors.delete_label)
225
- .bind('click', coauthors_delete_onclick)
226
- ;
227
- td.append(deleteBtn);
228
- }
229
-
230
- jQuery(tr).append(td);
231
- return tr;
232
-
233
- }
234
-
235
- /*
236
- * Creates autosuggest input box
237
- * @param string [optional] Name of the author
238
- * @param string [optional] Name to be applied to the input box
239
- */
240
- function coauthors_create_autosuggest(authorName, inputName) {
241
-
242
- if(!inputName) inputName = 'coauthorsinput[]';
243
-
244
- var co = jQuery('<input/>')
245
- .attr({
246
- 'class': 'coauthor-suggest',
247
- 'name': inputName
248
- })
249
- .appendTo(div)
250
-
251
- .suggest(coauthor_ajax_suggest_link,{
252
- onSelect:
253
- function() {
254
-
255
- var vals = this.value.split("|");
256
-
257
- var author = {}
258
- author.id = jQuery.trim(vals[0]);
259
- author.login = jQuery.trim(vals[1]);
260
- author.name = jQuery.trim(vals[2]);
261
-
262
- if(author.id=="New") {
263
- //alert('Eventually, this will allow you to add a new author right from here. But it\'s not ready yet. *sigh*');
264
- coauthors_new_author_display(name);
265
- } else {
266
- //coauthors_add_coauthor(login, name, co);
267
- coauthors_add_coauthor(author, co);
268
- }
269
- }
270
- })
271
- .keydown(function(e) {
272
- if(e.keyCode == 13) {return false;}
273
-
274
- })
275
- ;
276
-
277
- if(authorName)
278
- co.attr('value', unescape(authorName));
279
- else
280
- co.attr('value', i18n.coauthors.search_box_text)
281
- .focus(function(){co.val('')})
282
- .blur(function(){co.val(i18n.coauthors.search_box_text)})
283
- ;
284
-
285
- return co;
286
-
287
- }
288
-
289
- /*
290
- * Blur handler for autosuggest input box
291
- * @param event
292
- */
293
- function coauthors_stop_editing(event) {
294
-
295
- var co = jQuery(event.target);
296
- var tag = jQuery(co.next());
297
-
298
- co.attr('value',tag.text());
299
-
300
- co.hide();
301
- tag.show();
302
-
303
- // editing = false;
304
- }
305
-
306
- /*
307
- * Creates the text tag for an author
308
- * @param string Name of the author
309
- */
310
- function coauthors_create_author_tag (author) {
311
-
312
- var tag = jQuery('<span></span>')
313
- .html(unescape(author.name))
314
- .attr('title', i18n.coauthors.input_box_title)
315
- .addClass('author-tag')
316
- // Add Click event to edit
317
- .click(coauthors_edit_onclick);
318
- return tag;
319
- }
320
-
321
- /*
322
- * Creates the text tag for an author
323
- * @param string Name of the author
324
- */
325
- function coauthors_create_author_hidden_input (author) {
326
- var input = jQuery('<input />')
327
- .attr({
328
- 'type': 'hidden',
329
- 'id': 'coauthors_hidden_input',
330
- 'name': 'coauthors[]',
331
- 'value': unescape(author.login)
332
- })
333
- ;
334
-
335
- return input;
336
- }
337
-
338
-
339
- /*
340
- * Display form for creating new author
341
- * @param string Name of the author
342
- */
343
- function coauthors_new_author_display (name) {
344
-
345
- tb_show('Add New User', '?inlineId=awesome&modal=true');
346
-
347
- }
348
-
349
- /*
350
- * Creates display for adding new author
351
- * @param string Name of the author
352
- */
353
- function coauthors_new_author_create_display ( ) {
354
-
355
- var author_window = jQuery('<div></div>')
356
- .appendTo(jQuery('body'))
357
- .attr('id','new-author-window')
358
- .addClass('wrap')
359
- .append(
360
- jQuery('<div></div>')
361
- .addClass('icon32')
362
- .attr('id','icon-users')
363
- )
364
- .append(
365
- jQuery('<h2></h2>')
366
- .text('Add new author')
367
- .attr('id', 'add-new-user')
368
-
369
- )
370
- .append(
371
- jQuery('<div/>')
372
- .attr('id', 'createauthor-ajax-response')
373
- )
374
- ;
375
-
376
- var author_form = jQuery('<form />')
377
- .appendTo(author_window)
378
- .attr({
379
- id: 'createauthor',
380
- name: 'createauthor',
381
- method: 'post',
382
- action: ''
383
- })
384
- ;
385
-
386
-
387
-
388
- var create_text_field = function( name, id, label) {
389
-
390
- var field = jQuery('<input />')
391
- .attr({
392
- type:'text',
393
- name: name,
394
- id: id,
395
- })
396
- var label = jQuery('<label></label>')
397
- .attr('for',name)
398
- .text(label)
399
-
400
- //return {field, label};
401
-
402
- };
403
-
404
- create_field('user_login', 'user_login', 'User Name');
405
- create_field('first_name', 'first_name', 'First Name');
406
-
407
- //last_name
408
- //email
409
- //pass1
410
- //email password checkbox
411
- //role
412
- }
413
-
414
-
415
-
416
- if(jQuery('#post_author_override')){
417
-
418
- // Check if user has permissions to change post authors; if not, remove controls and end
419
- if(!coauthors_can_edit_others_posts){
420
- jQuery('#authordiv, #pageauthordiv').remove();
421
- return;
422
- }
423
-
424
- // Changes the meta_box title from "Post Author" to "Post Author(s)"
425
- var h3 = jQuery('#authordiv :header, #pageauthordiv :header').html(
426
- /page[^\/]+#x2F;.test(window.location.href) ?
427
- i18n.coauthors.page_metabox_title
428
- :
429
- i18n.coauthors.post_metabox_title
430
- );
431
-
432
- // Add the controls to add co-authors
433
- var div = jQuery('#authordiv div, #pageauthordiv div').filter(function(){
434
- if(jQuery(this).is('.inside') || jQuery(this).is('.dbx-content'))
435
- return true;
436
- return false;
437
- })[0];
438
-
439
- if(div){
440
-
441
- // Create the co-authors table
442
- var table = jQuery('<table></table>')
443
- .attr('id', 'coauthors-table')
444
- ;
445
- var coauthors_table = jQuery('<tbody></tbody>')
446
- .appendTo(table)
447
- ;
448
- var tr = jQuery('<tr></tr>');
449
- var td = jQuery('<td></td>')
450
- .addClass('select')
451
- ;
452
-
453
- var select = jQuery('#post_author_override')[0];
454
-
455
- td.append(select);
456
- tr.append(td);
457
- coauthors_table.append(tr);
458
- jQuery(div).append(table);
459
-
460
- // Hide original dropdown box
461
- jQuery('#post_author_override').hide();
462
-
463
- // Show help text
464
- var help = jQuery('<p></p>').html(i18n.coauthors.help_text);
465
- jQuery('#authordiv .inside').append(help);
466
- jQuery('#pageauthordiv .inside').append(help);
467
-
468
- }
469
-
470
- // Select authors already added to the post
471
- var addedAlready = [];
472
- //jQuery('#the-list tr').each(function(){
473
- var count = 0;
474
- jQuery.each(post_coauthors, function() {
475
- coauthors_add_coauthor(this, undefined, true, count );
476
- count++;
477
- });
478
-
479
- // Create new author-suggest and append it to a new row
480
- var newCO = coauthors_create_autosuggest('', false);
481
- coauthors_add_to_table(newCO);
482
- }
483
-
484
- });
co-authors-plus.php ADDED
@@ -0,0 +1,897 @@
1
+ <?php
2
+ /*
3
+ Plugin Name: Co-Authors Plus
4
+ Plugin URI: http://wordpress.org/extend/plugins/co-authors-plus/
5
+ 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.
6
+ Version: 2.5
7
+ Author: Mohammad Jangda
8
+ Author URI: http://digitalize.ca
9
+ Copyright: Some parts (C) 2009-2011, Mohammad Jangda; Other parts (C) 2008, Weston Ruter
10
+
11
+ GNU General Public License, Free Software Foundation <http://creativecommons.org/licenses/GPL/2.0/>
12
+ This program is free software; you can redistribute it and/or modify
13
+ it under the terms of the GNU General Public License as published by
14
+ the Free Software Foundation; either version 2 of the License, or
15
+ (at your option) any later version.
16
+
17
+ This program is distributed in the hope that it will be useful,
18
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
+ GNU General Public License for more details.
21
+
22
+ You should have received a copy of the GNU General Public License
23
+ along with this program; if not, write to the Free Software
24
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
+
26
+ */
27
+
28
+ if( ! defined( 'COAUTHORS_PLUS_DEBUG' ) )
29
+ define( 'COAUTHORS_PLUS_DEBUG', false );
30
+
31
+ if( ! defined( 'COAUTHORS_DEFAULT_BEFORE' ) )
32
+ define( 'COAUTHORS_DEFAULT_BEFORE', '' );
33
+
34
+ if( ! defined( 'COAUTHORS_DEFAULT_BETWEEN' ) )
35
+ define( 'COAUTHORS_DEFAULT_BETWEEN', ', ' );
36
+
37
+ if( ! defined( 'COAUTHORS_DEFAULT_BETWEEN_LAST' ) )
38
+ define( 'COAUTHORS_DEFAULT_BETWEEN_LAST', __( ', and ', 'co-authors-plus' ) );
39
+
40
+ if( ! defined( 'COAUTHORS_DEFAULT_AFTER' ) )
41
+ define( 'COAUTHORS_DEFAULT_AFTER', '' );
42
+
43
+ define( 'COAUTHORS_PLUS_PATH', dirname( __FILE__ ) );
44
+ define( 'COAUTHORS_PLUS_URL', plugin_dir_url( __FILE__ ) );
45
+
46
+ define( 'COAUTHORS_PLUS_VERSION', '2.5' );
47
+
48
+ class coauthors_plus {
49
+
50
+ // Name for the taxonomy we're using to store coauthors
51
+ var $coauthor_taxonomy = 'author';
52
+ // Unique identified added as a prefix to all options
53
+ var $options_group = 'coauthors_plus_';
54
+ // Initially stores default option values, but when load_options is run, it is populated with the options stored in the WP db
55
+ var $options = array(
56
+ 'allow_subscribers_as_authors' => 0,
57
+ );
58
+
59
+ var $coreauthors_meta_box_name = 'authordiv';
60
+ var $coauthors_meta_box_name = 'coauthorsdiv';
61
+
62
+ var $gravatar_size = 25;
63
+
64
+ var $_pages_whitelist = array( 'post.php', 'post-new.php' );
65
+
66
+ /**
67
+ * __construct()
68
+ */
69
+ function __construct() {
70
+ global $pagenow;
71
+
72
+ // Load admin_init function
73
+ add_action( 'admin_init', array( $this,'admin_init' ) );
74
+
75
+ // Load plugin options
76
+ $this->load_options();
77
+
78
+ // Register new taxonomy so that we can store all our authors
79
+ register_taxonomy( $this->coauthor_taxonomy, 'post', array('hierarchical' => false, 'update_count_callback' => '_update_post_term_count', 'label' => false, 'query_var' => false, 'rewrite' => false, 'sort' => true, 'show_ui' => false ) );
80
+
81
+ // Modify SQL queries to include coauthors
82
+ add_filter( 'posts_where', array( $this, 'posts_where_filter' ) );
83
+ add_filter( 'posts_join', array( $this, 'posts_join_filter' ) );
84
+ add_filter( 'posts_groupby', array( $this, 'posts_groupby_filter' ) );
85
+
86
+ // Action to set users when a post is saved
87
+ add_action( 'save_post', array( $this, 'coauthors_update_post' ), 10, 2 );
88
+ // Filter to set the post_author field when wp_insert_post is called
89
+ add_filter( 'wp_insert_post_data', array( $this, 'coauthors_set_post_author_field' ) );
90
+
91
+ // Action to reassign posts when a user is deleted
92
+ add_action( 'delete_user', array( $this, 'delete_user_action' ) );
93
+
94
+ add_filter( 'get_usernumposts', array( $this, 'filter_count_user_posts' ), 10, 2 );
95
+
96
+ // Action to set up author auto-suggest
97
+ add_action( 'wp_ajax_coauthors_ajax_suggest', array( $this, 'ajax_suggest' ) );
98
+
99
+ // Filter to allow coauthors to edit posts
100
+ add_filter( 'user_has_cap', array( $this, 'add_coauthor_cap' ), 10, 3 );
101
+
102
+ add_filter( 'comment_notification_headers', array( $this, 'notify_coauthors' ), 10, 3 );
103
+
104
+ // Add the necessary pages for the plugin
105
+ add_action( 'admin_menu', array( $this, 'add_menu_items' ) );
106
+
107
+ // Handle the custom author meta box
108
+ add_action( 'add_meta_boxes', array( $this, 'add_coauthors_box' ) );
109
+ add_action( 'add_meta_boxes', array( $this, 'remove_authors_box' ) );
110
+
111
+ // Removes the author dropdown from the post quick edit
112
+ add_action( 'load-edit.php', array( $this, 'remove_quick_edit_authors_box' ) );
113
+
114
+ // Fix for author info not properly displaying on author pages
115
+ add_action( 'the_post', array( $this, 'fix_author_page' ) );
116
+ }
117
+
118
+ function coauthors_plus() {
119
+ $this->__construct();
120
+ }
121
+
122
+ /**
123
+ * Initialize the plugin for the admin
124
+ */
125
+ function admin_init() {
126
+ global $pagenow;
127
+
128
+ // Register all plugin settings so that we can change them and such
129
+ // Not really being used
130
+ /*
131
+ foreach( $this->options as $option => $value ) {
132
+ register_setting( $this->options_group, $this->get_plugin_option_fullname( $option ) );
133
+ }
134
+ */
135
+
136
+ // Hook into load to initialize custom columns
137
+ if( $this->is_valid_page() ) {
138
+ add_action( 'load-' . $pagenow, array( $this, 'admin_load_page' ) );
139
+ }
140
+
141
+ // Hooks to add additional coauthors to author column to Edit page
142
+ add_filter( 'manage_posts_columns', array( $this, '_filter_manage_posts_columns' ) );
143
+ add_filter( 'manage_pages_columns', array( $this, '_filter_manage_posts_columns' ) );
144
+ add_action( 'manage_posts_custom_column', array( $this, '_filter_manage_posts_custom_column' ) );
145
+ add_action( 'manage_pages_custom_column', array( $this, '_filter_manage_posts_custom_column' ) );
146
+ }
147
+
148
+ function admin_load_page() {
149
+
150
+ // Add the main JS script and CSS file
151
+ add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
152
+
153
+ // Add necessary JS variables
154
+ add_action( 'admin_print_scripts', array( $this, 'js_vars' ) );
155
+
156
+ }
157
+
158
+ /**
159
+ * Checks to see if the post_type supports authors
160
+ */
161
+ function authors_supported( $post_type ) {
162
+
163
+ if( ! function_exists( 'post_type_supports' ) && in_array( $post_type, array( 'post', 'page' ) ) )
164
+ return true;
165
+
166
+ // Hacky way to prevent issues on the Edit page
167
+ if( isset( $this->_edit_page_post_type_supported ) && $this->_edit_page_post_type_supported )
168
+ return true;
169
+
170
+ if( post_type_supports( $post_type, 'author' ) )
171
+ return true;
172
+
173
+ return false;
174
+ }
175
+
176
+ /**
177
+ * Gets the current global post type if one is set
178
+ */
179
+ function get_current_post_type() {
180
+ global $post, $typenow, $current_screen;
181
+
182
+ // "Cache" it!
183
+ if( isset( $this->_current_post_type ) )
184
+ return $this->_current_post_type;
185
+
186
+ if( $post && $post->post_type )
187
+ $post_type = $post->post_type;
188
+ elseif( $typenow )
189
+ $post_type = $typenow;
190
+ elseif( $current_screen && isset( $current_screen->post_type ) )
191
+ $post_type = $current_screen->post_type;
192
+ elseif( isset( $_REQUEST['post_type'] ) )
193
+ $post_type = sanitize_key( $_REQUEST['post_type'] );
194
+ else
195
+ $post_type = '';
196
+
197
+ if( $post_type )
198
+ $this->_current_post_type = $post_type;
199
+
200
+ return $post_type;
201
+ }
202
+
203
+ /**
204
+ * Removes the standard WordPress Author box.
205
+ * We don't need it because the Co-Authors one is way cooler.
206
+ */
207
+ function remove_authors_box() {
208
+
209
+ $post_type = $this->get_current_post_type();
210
+
211
+ if( $this->authors_supported( $post_type ) )
212
+ remove_meta_box( $this->coreauthors_meta_box_name, $post_type, 'normal' );
213
+ }
214
+
215
+ /**
216
+ * Adds a custom Authors box
217
+ */
218
+ function add_coauthors_box() {
219
+
220
+ $post_type = $this->get_current_post_type();
221
+
222
+ if( $this->authors_supported( $post_type ) && $this->current_user_can_set_authors() )
223
+ add_meta_box($this->coauthors_meta_box_name, __('Post Authors', 'co-authors-plus'), array( &$this, 'coauthors_meta_box' ), $post_type, 'normal', 'high');
224
+ }
225
+
226
+ /**
227
+ * Callback for adding the custom author box
228
+ */
229
+ function coauthors_meta_box( $post ) {
230
+ global $post;
231
+
232
+ $post_id = $post->ID;
233
+
234
+ if( !$post_id || $post_id == 0 || !$post->post_author )
235
+ $coauthors = array( wp_get_current_user() );
236
+ else
237
+ $coauthors = get_coauthors();
238
+
239
+ $count = 0;
240
+ if( !empty( $coauthors ) ) :
241
+ ?>
242
+ <div id="coauthors-readonly" class="hide-if-js1">
243
+ <ul>
244
+ <?php
245
+ foreach( $coauthors as $coauthor ) :
246
+ $count++;
247
+ ?>
248
+ <li>
249
+ <?php echo get_avatar( $coauthor->user_email, $this->gravatar_size ); ?>
250
+ <span id="coauthor-readonly-<?php echo $count; ?>" class="coauthor-tag">
251
+ <input type="text" name="coauthorsinput[]" readonly="readonly" value="<?php echo esc_attr( $coauthor->display_name ); ?>" />
252
+ <input type="text" name="coauthors[]" value="<?php echo esc_attr( $coauthor->user_login ); ?>" />
253
+ <input type="text" name="coauthorsemails[]" value="<?php echo esc_attr( $coauthor->user_email ); ?>" />
254
+ </span>
255
+ </li>
256
+ <?php
257
+ endforeach;
258
+ ?>
259
+ </ul>
260
+ <div class="clear"></div>
261
+ <p><?php _e( '<strong>Note:</strong> To edit post authors, please enable javascript or use a javascript-capable browser', 'co-authors-plus' ); ?></p>
262
+ </div>
263
+ <?php
264
+ endif;
265
+ ?>
266
+
267
+ <div id="coauthors-edit" class="hide-if-no-js">
268
+ <p><?php _e( 'Click on an author to change them. Click on <strong>Delete</strong> to remove them.', 'co-authors-plus' ); ?></p>
269
+ </div>
270
+
271
+ <?php wp_nonce_field( 'coauthors-edit', 'coauthors-nonce' ); ?>
272
+
273
+ <?php
274
+ }
275
+
276
+ /**
277
+ * Removes the author dropdown from the post quick edit
278
+ * It's a bit hacky, but the only way I can figure out :(
279
+ */
280
+ function remove_quick_edit_authors_box() {
281
+ $post_type = $this->get_current_post_type();
282
+ if( post_type_supports( $post_type, 'author' )) {
283
+ $this->_edit_page_post_type_supported = true;
284
+ remove_post_type_support( $post_type, 'author' );
285
+ }
286
+ }
287
+
288
+ /**
289
+ * Adds menu items for the plugin
290
+ */
291
+ function add_menu_items ( ) {
292
+ // Add sub-menu page for Custom statuses
293
+ //add_options_page(__( 'Co-Authors Plus', 'co-authors-plus' ), __( 'Co-Authors Plus', 'co-authors-plus' ), 'manage_options', 'co-authors-plus', array( $this, 'settings_page' ) );
294
+ }
295
+
296
+ /**
297
+ * Add coauthors to author column on edit pages
298
+ * @param array $post_columns
299
+ */
300
+ function _filter_manage_posts_columns($posts_columns) {
301
+ $new_columns = array();
302
+
303
+ foreach ($posts_columns as $key => $value) {
304
+ $new_columns[$key] = $value;
305
+ if( $key == 'title' )
306
+ $new_columns['coauthors'] = __( 'Authors', 'co-authors-plus' );
307
+
308
+ if ( $key == 'author' )
309
+ unset($new_columns[$key]);
310
+ }
311
+ return $new_columns;
312
+ } // END: _filter_manage_posts_columns
313
+
314
+ /**
315
+ * Insert coauthors into post rows on Edit Page
316
+ * @param string $column_name
317
+ **/
318
+ function _filter_manage_posts_custom_column($column_name) {
319
+ if ($column_name == 'coauthors') {
320
+ global $post;
321
+ $authors = get_coauthors( $post->ID );
322
+
323
+ $count = 1;
324
+ foreach( $authors as $author ) :
325
+ ?>
326
+ <a href="edit.php?author=<?php echo $author->ID; ?>"><?php echo $author->display_name ?></a><?php echo ( $count < count( $authors ) ) ? ',' : ''; ?>
327
+ <?php
328
+ $count++;
329
+ endforeach;
330
+ }
331
+ }
332
+
333
+ /**
334
+ * Modify the author query posts SQL to include posts co-authored
335
+ */
336
+ function posts_join_filter( $join ){
337
+ global $wpdb, $wp_query;
338
+
339
+ if( is_author() ){
340
+ // Check to see that JOIN hasn't already been added. Props michaelingp
341
+ $join_string = " INNER JOIN {$wpdb->term_relationships} ON ( {$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id) INNER JOIN {$wpdb->term_taxonomy} ON ( {$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id )";
342
+
343
+ if( strpos( $join, $join_string ) === false ) {
344
+ $join .= $join_string;
345
+ }
346
+ }
347
+
348
+ return $join;
349
+ }
350
+
351
+ /**
352
+ * Modify
353
+ */
354
+ function posts_where_filter( $where ){
355
+ global $wpdb, $wp_query;
356
+
357
+ if( is_author() ) {
358
+ $author = get_userdata( $wp_query->query_vars['author'] );
359
+ $term = get_term_by( 'name', $author->user_login, $this->coauthor_taxonomy );
360
+
361
+ if( $author ) {
362
+ $where = preg_replace( '/(\b(?:' . $wpdb->posts . '\.)?post_author\s*=\s*(\d+))/', '($1 OR (' . $wpdb->term_taxonomy . '.taxonomy = \''. $this->coauthor_taxonomy.'\' AND '. $wpdb->term_taxonomy .'.term_id = \''. $term->term_id .'\'))', $where, 1 ); #' . $wpdb->postmeta . '.meta_id IS NOT NULL AND
363
+
364
+ }
365
+ }
366
+ return $where;
367
+ }
368
+
369
+ /**
370
+ *
371
+ */
372
+ function posts_groupby_filter( $groupby ) {
373
+ global $wpdb;
374
+
375
+ if( is_author() ) {
376
+ $groupby = $wpdb->posts .'.ID';
377
+ }
378
+ return $groupby;
379
+ }
380
+
381
+ /**
382
+ * Filters post data before saving to db to set post_author
383
+ */
384
+ function coauthors_set_post_author_field( $data ) {
385
+ if ( !defined( 'DOING_AUTOSAVE' ) || !DOING_AUTOSAVE ) {
386
+ if( isset( $_REQUEST['coauthors-nonce'] ) && is_array( $_POST['coauthors'] ) ) {
387
+ $author = $_POST['coauthors'][0];
388
+ if( $author ) {
389
+ $author_data = get_user_by( 'login', $author );
390
+ $data['post_author'] = $author_data->ID;
391
+ }
392
+ } else {
393
+ // If for some reason we don't have the coauthors fields set
394
+ if( ! isset( $data['post_author'] ) ) {
395
+ $user = wp_get_current_user();
396
+ $data['post_author'] = $user->ID;
397
+ }
398
+ }
399
+ }
400
+ return $data;
401
+ }
402
+
403
+ /**
404
+ * Update a post's co-authors
405
+ * @param $post_ID
406
+ * @return
407
+ */
408
+ function coauthors_update_post( $post_id, $post ) {
409
+ $post_type = $post->post_type;
410
+
411
+ if ( !defined( 'DOING_AUTOSAVE' ) || !DOING_AUTOSAVE ) {
412
+
413
+ if( isset( $_REQUEST['coauthors-nonce'] ) ) {
414
+ check_admin_referer( 'coauthors-edit', 'coauthors-nonce' );
415
+
416
+ if( $this->current_user_can_set_authors() ){
417
+ $coauthors = array_map( 'sanitize_key', $_POST['coauthors'] );
418
+ return $this->add_coauthors( $post_id, $coauthors );
419
+ }
420
+ }
421
+ }
422
+ }
423
+
424
+ /**
425
+ * Add a user as coauthor for a post
426
+ */
427
+ function add_coauthors( $post_id, $coauthors, $append = false ) {
428
+ global $current_user;
429
+
430
+ $post_id = (int) $post_id;
431
+ $insert = false;
432
+
433
+ // if an array isn't returned, create one and populate with default author
434
+ if ( !is_array( $coauthors ) || 0 == count( $coauthors ) || empty( $coauthors ) ) {
435
+ $coauthors = array( $current_user->user_login );
436
+ }
437
+
438
+ // Add each co-author to the post meta
439
+ foreach( array_unique( $coauthors ) as $author ){
440
+
441
+ // Name and slug of term are the username;
442
+ $name = $author;
443
+
444
+ // Add user as a term if they don't exist
445
+ if( !term_exists( $name, $this->coauthor_taxonomy ) ) {
446
+ $args = array( 'slug' => sanitize_title( $name ) );
447
+ $insert = wp_insert_term( $name, $this->coauthor_taxonomy, $args );
448
+ }
449
+ }
450
+
451
+ // Add authors as post terms
452
+ if( !is_wp_error( $insert ) ) {
453
+ $set = wp_set_post_terms( $post_id, $coauthors, $this->coauthor_taxonomy, $append );
454
+ }
455
+ }
456
+
457
+ /**
458
+ * Action taken when user is deleted.
459
+ * - User term is removed from all associated posts
460
+ * - Option to specify alternate user in place for each post
461
+ * @param delete_id
462
+ */
463
+ function delete_user_action($delete_id){
464
+ global $wpdb;
465
+
466
+ $reassign_id = absint( $_POST['reassign_user'] );
467
+
468
+ // If reassign posts, do that -- use coauthors_update_post
469
+ if($reassign_id) {
470
+ // Get posts belonging to deleted author
471
+ $reassign_user = get_profile_by_id( 'user_login', $reassign_id );
472
+ // Set to new author
473
+ if( $reassign_user ) {
474
+ $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $delete_id ) );
475
+
476
+ if ( $post_ids ) {
477
+ foreach ( $post_ids as $post_id ) {
478
+ $this->add_coauthors( $post_id, array( $reassign_user ), true );
479
+ }
480
+ }
481
+ }
482
+ }
483
+
484
+ $delete_user = get_profile_by_id( 'user_login', $delete_id );
485
+ if( $delete_user ) {
486
+ // Delete term
487
+ wp_delete_term( $delete_user, $this->coauthor_taxonomy );
488
+ }
489
+ }
490
+
491
+ function filter_count_user_posts( $count, $user_id ) {
492
+ $user = get_userdata( $user_id );
493
+
494
+ $term = get_term_by( 'slug', $user->user_login, $this->coauthor_taxonomy );
495
+
496
+ if( !$term || is_wp_error( ) ) {
497
+ $count = 0;
498
+ } else {
499
+ $count = $term->count;
500
+ }
501
+
502
+ return $count;
503
+ }
504
+
505
+ /**
506
+ * Checks to see if the current user can set authors or not
507
+ */
508
+ function current_user_can_set_authors( ) {
509
+ global $post, $typenow;
510
+
511
+ // TODO: Enable Authors to set Co-Authors
512
+
513
+ $post_type = $this->get_current_post_type();
514
+ // TODO: need to fix this; shouldn't just say no if don't have post_type
515
+ if( ! $post_type ) return false;
516
+
517
+ $post_type_object = get_post_type_object( $post_type );
518
+ $can_set_authors = current_user_can( $post_type_object->cap->edit_others_posts );
519
+
520
+ return apply_filters( 'coauthors_plus_edit_authors', $can_set_authors );
521
+ }
522
+
523
+ /**
524
+ * Fix for author info not properly displaying on author pages
525
+ *
526
+ * On an author archive, if the first story has coauthors and
527
+ * the first author is NOT the same as the author for the archive,
528
+ * the query_var is changed.
529
+ *
530
+ */
531
+ function fix_author_page( &$post ) {
532
+
533
+ if( is_author() ) {
534
+ global $wp_query, $authordata;
535
+
536
+ // Get the id of the author whose page we're on
537
+ $author_id = $wp_query->get( 'author' );
538
+
539
+ // Check that the the author matches the first author of the first post
540
+ if( $author_id != $authordata->ID ) {
541
+ // The IDs don't match, so we need to force the $authordata to the one we want
542
+ $authordata = get_userdata( $author_id );
543
+ }
544
+ }
545
+ }
546
+
547
+ /**
548
+ * Main function that handles search-as-you-type
549
+ */
550
+ function ajax_suggest() {
551
+ global $wpdb;
552
+
553
+ global $user_level;
554
+
555
+ if( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'coauthors-search' ) )
556
+ die();
557
+
558
+ if( empty( $_REQUEST['q'] ) )
559
+ die();
560
+
561
+ if( ! $this->current_user_can_set_authors() )
562
+ die();
563
+
564
+ $search = esc_html( strtolower( $_REQUEST['q'] ) );
565
+
566
+ $authors = $this->search_authors( $search );
567
+
568
+ foreach( $authors as $author ) {
569
+ echo $author->ID ." | ". $author->user_login ." | ". $author->display_name ." | ". $author->user_email ."\n";
570
+ }
571
+
572
+ if( COAUTHORS_PLUS_DEBUG ) {
573
+ echo 'queries:' . get_num_queries() ."\n";
574
+ echo 'timer: ' . timer_stop(1) . "sec\n";
575
+ }
576
+
577
+ die();
578
+
579
+ }
580
+
581
+ function search_authors( $search = '' ) {
582
+ $authors = array();
583
+ $author_fields = array( 'ID', 'display_name', 'user_login', 'user_email' );
584
+
585
+ if ( function_exists( 'get_users' ) ) {
586
+ $search = sprintf( '*%s*', $search ); // Enable wildcard searching
587
+ $authors = get_users( array( 'search' => $search, 'who' => 'authors', 'fields' => $author_fields ) );
588
+ } else {
589
+ // Pre 3.1 support
590
+ // This might not be the most eloquent way of doing things, but it's better than a nasty SQL query
591
+ $matching_authors = get_editable_authors( get_current_user_id() );
592
+
593
+ foreach( $matching_authors as $matching_author ) {
594
+ foreach( $author_fields as $author_field ) {
595
+ if( isset( $matching_author->$author_field ) && strpos( strtolower( $matching_author->$author_field ), $search ) !== false ) {
596
+ $authors[] = $matching_author;
597
+ break;
598
+ }
599
+ }
600
+ }
601
+ }
602
+
603
+ return (array) $authors;
604
+ }
605
+
606
+ /**
607
+ * Functions to add scripts and css
608
+ */
609
+ function enqueue_scripts($hook_suffix) {
610
+ global $pagenow, $post;
611
+
612
+ $post_type = $this->get_current_post_type();
613
+
614
+ // TODO: Check if user can set authors? $this->current_user_can_set_authors()
615
+ if( $this->is_valid_page() && $this->authors_supported( $post_type ) ) {
616
+
617
+ wp_enqueue_style( 'co-authors-plus-css', COAUTHORS_PLUS_URL . 'css/co-authors-plus.css', false, COAUTHORS_PLUS_VERSION, 'all' );
618
+ wp_enqueue_script( 'co-authors-plus-js', COAUTHORS_PLUS_URL . 'js/co-authors-plus.js', array('jquery', 'suggest'), COAUTHORS_PLUS_VERSION, true);
619
+
620
+ $js_strings = array(
621
+ 'edit_label' => __( 'Edit', 'co-authors-plus' ),
622
+ 'delete_label' => __( 'Delete', 'co-authors-plus' ),
623
+ 'confirm_delete' => __( 'Are you sure you want to delete this author?', 'co-authors-plus' ),
624
+ 'input_box_title' => __( 'Click to change this author', 'co-authors-plus' ),
625
+ 'search_box_text' => __( 'Search for an author', 'co-authors-plus' ),
626
+ 'help_text' => __( 'Click on an author to change them. Click on <strong>Delete</strong> to remove them.', 'co-authors-plus' ),
627
+ );
628
+ wp_localize_script( 'co-authors-plus-js', 'coAuthorsPlusStrings', $js_strings );
629
+
630
+ }
631
+ }
632
+
633
+ /**
634
+ * Adds necessary javascript variables to admin pages
635
+ */
636
+ function js_vars() {
637
+
638
+ $post_type = $this->get_current_post_type();
639
+
640
+ if( $this->is_valid_page() && $this->authors_supported( $post_type ) && $this->current_user_can_set_authors() ) {
641
+ ?>
642
+ <script type="text/javascript">
643
+ // AJAX link used for the autosuggest
644
+ var coAuthorsPlus_ajax_suggest_link = '<?php echo add_query_arg(
645
+ array(
646
+ 'action' => 'coauthors_ajax_suggest',
647
+ 'post_type' => $post_type,
648
+ ),
649
+ wp_nonce_url( 'admin-ajax.php', 'coauthors-search' )
650
+ ); ?>';
651
+ </script>
652
+ <?php
653
+ }
654
+ } // END: js_vars()
655
+
656
+ /**
657
+ * Helper to only add javascript to necessary pages. Avoids bloat in admin.
658
+ */
659
+ function is_valid_page() {
660
+ global $pagenow;
661
+
662
+ return in_array( $pagenow, $this->_pages_whitelist );
663
+ }
664
+
665
+ function get_post_id() {
666
+ global $post;
667
+ $post_id = 0;
668
+
669
+ if ( is_object( $post ) ) {
670
+ $post_id = $post->ID;
671
+ }
672
+
673
+ if( ! $post_id ) {
674
+ if ( isset( $_GET['post'] ) )
675
+ $post_id = (int) $_GET['post'];
676
+ elseif ( isset( $_POST['post_ID'] ) )
677
+ $post_id = (int) $_POST['post_ID'];
678
+ }
679
+
680
+ return $post_id;
681
+ }
682
+
683
+ /**
684
+ * Allows coauthors to edit the post they're coauthors of
685
+ * Pieces of code borrowed from http://pastebin.ca/1909968
686
+ *
687
+ */
688
+ function add_coauthor_cap( $allcaps, $caps, $args ) {
689
+
690
+ // Load the post data:
691
+ $user_id = isset( $args[1] ) ? $args[1] : 0;
692
+ $post_id = isset( $args[2] ) ? $args[2] : 0;
693
+
694
+ if( ! $post_id )
695
+ $post_id = $this->get_post_id();
696
+
697
+ if( ! $post_id )
698
+ return $allcaps;
699
+
700
+ $post = get_post( $post_id );
701
+
702
+ if( ! $post )
703
+ return $allcaps;
704
+
705
+ $post_type_object = get_post_type_object( $post->post_type );
706
+
707
+ // Bail out if we're not asking about a post
708
+ if ( ! in_array( $args[0], array( $post_type_object->cap->edit_post, $post_type_object->cap->edit_others_posts ) ) )
709
+ return $allcaps;
710
+
711
+ // Bail out for users who can already edit others posts
712
+ if ( isset( $allcaps[$post_type_object->cap->edit_others_posts] ) && $allcaps[$post_type_object->cap->edit_others_posts] )
713
+ return $allcaps;
714
+
715
+ // Bail out for users who can't publish posts if the post is already published
716
+ if ( 'publish' == $post->post_status && ( ! isset( $allcaps[$post_type_object->cap->publish_posts] ) || ! $allcaps[$post_type_object->cap->publish_posts] ) )
717
+ return $allcaps;
718
+
719
+ // Finally, double check that the user is a coauthor of the post
720
+ if( is_coauthor_for_post( $user_id, $post_id ) ) {
721
+ foreach($caps as $cap) {
722
+ $allcaps[$cap] = true;
723
+ }
724
+ }
725
+
726
+ return $allcaps;
727
+ }
728
+
729
+ /**
730
+ * Emails all coauthors when comment added instead of the main author
731
+ *
732
+ */
733
+ function notify_coauthors( $message_headers, $comment_id ) {
734
+ // TODO: this is broken!
735
+ $comment = get_comment($comment_id);
736
+ $post = get_post($comment->comment_post_ID);
737
+ $coauthors = get_coauthors($comment->comment_post_ID);
738
+
739
+ $message_headers .= 'cc: ';
740
+ $count = 0;
741
+ foreach($coauthors as $author) {
742
+ $count++;
743
+ if($author->ID != $post->post_author){
744
+ $message_headers .= $author->user_email;
745
+ if($count < count($coauthors)) $message_headers .= ',';
746
+ }
747
+ }
748
+ $message_headers .= "\n";
749
+ return $message_headers;
750
+ }
751
+
752
+ /**
753
+ * Loads options for the plugin.
754
+ * If option doesn't exist in database, it is added
755
+ *
756
+ * Note: default values are stored in the $this->options array
757
+ * Note: a prefix unique to the plugin is appended to all options. Prefix is stored in $this->options_group
758
+ */
759
+ function load_options ( ) {
760
+
761
+ $new_options = array();
762
+
763
+ foreach( $this->options as $option => $value ) {
764
+ $name = $this->get_plugin_option_fullname( $option );
765
+ $return = get_option( $name );
766
+ if( $return === false ) {
767
+ add_option( $name, $value );
768
+ $new_array[$option] = $value;
769
+ } else {
770
+ $new_array[$option] = $return;
771
+ }
772
+ }
773
+ $this->options = $new_array;
774
+
775
+ } // END: load_options
776
+
777
+
778
+ /**
779
+ * Returns option for the plugin specified by $name, e.g. custom_stati_enabled
780
+ *
781
+ * Note: The plugin option prefix does not need to be included in $name
782
+ *
783
+ * @param string name of the option
784
+ * @return option|null if not found
785
+ *
786
+ */
787
+ function get_plugin_option ( $name ) {
788
+ if( is_array( $this->options ) && $option = $this->options[$name] )
789
+ return $option;
790
+ else
791
+ return null;
792
+ } // END: get_option
793
+
794
+ // Utility function: appends the option prefix and returns the full name of the option as it is stored in the wp_options db
795
+ function get_plugin_option_fullname ( $name ) {
796
+ return $this->options_group . $name;
797
+ }
798
+
799
+ /**
800
+ * Adds Settings page for Edit Flow
801
+ */
802
+ function settings_page( ) {
803
+ global $wp_roles;
804
+
805
+ ?>
806
+ <div class="wrap">
807
+ <div class="icon32" id="icon-options-general"><br/></div>
808
+ <h2><?php _e('Co-Authors Plus', 'co-authors-plus') ?></h2>
809
+
810
+ <form method="post" action="options.php">
811
+ <?php settings_fields($this->options_group); ?>
812
+
813
+ <table class="form-table">
814
+ <tr valign="top">
815
+ <th scope="row"><strong><?php _e('Roles', 'co-authors-plus') ?></strong></th>
816
+ <td>
817
+ <p>
818
+ <label for="allow_subscribers_as_authors">
819
+ <?php /*<input type="checkbox" name="<?php echo $this->get_plugin_option_fullname('allow_subscribers_as_authors') ?>" value="1" <?php echo ($this->get_plugin_option('allow_subscribers_as_authors')) ? 'checked="checked"' : ''; ?> id="allow_subscribers_as_authors" /> <?php _e('Allow subscribers as authors', 'co-authors-plus') ?>*/ ?>
820
+ <input type="checkbox" disabled="disabled" name="<?php echo $this->get_plugin_option_fullname('allow_subscribers_as_authors') ?>" value="1" id="allow_subscribers_as_authors" /> <?php _e('Allow subscribers as authors', 'co-authors-plus') ?>
821
+ </label> <br />
822
+ <span class="description"><?php _e('Enabling this option will allow you to add users with the subscriber role as authors for posts.', 'co-authors-plus') ?></span>
823
+ <br />
824
+ <span class="description"><strong>Note:</strong> This option has been removed as of v2.5</span>
825
+ </p>
826
+ </td>
827
+ </tr>
828
+
829
+ </table>
830
+
831
+ <p class="submit">
832
+ <input type="submit" class="button-primary" value="<?php _e('Save Changes', 'co-authors-plus') ?>" />
833
+ </p>
834
+ </form>
835
+ </div>
836
+ <?php
837
+ }
838
+
839
+ function debug($msg, $object) {
840
+ if( COAUTHORS_PLUS_DEBUG ) {
841
+ echo '<hr />';
842
+ echo sprintf('<p>%s</p>', $msg);
843
+ echo '<pre>';
844
+ var_dump($object);
845
+ echo '</pre>';
846
+ }
847
+ }
848
+ }
849
+
850
+ /** Helper Functions **/
851
+
852
+ /**
853
+ * Replacement for the default WordPress get_profile function, since that doesn't allow for search by user_id
854
+ * Returns a the specified column value for the specified user
855
+ */
856
+ // TODO: Remove this function
857
+ if( ! function_exists( 'get_profile_by_id' ) ) {
858
+ function get_profile_by_id( $field, $user_id ) {
859
+ global $wpdb;
860
+
861
+ if( $field && $user_id )
862
+ return $wpdb->get_var( $wpdb->prepare( "SELECT $field FROM $wpdb->users WHERE ID = %d", $user_id ) );
863
+
864
+ return false;
865
+ }
866
+ }
867
+
868
+ function coauthors_plus_init() {
869
+
870
+ $plugin_dir = dirname( plugin_basename( __FILE__ ) ) . '/languages/';
871
+ load_plugin_textdomain( 'co-authors-plus', null, $plugin_dir );
872
+
873
+ // Check if we're running 3.0
874
+ if( function_exists( 'post_type_exists' ) ) {
875
+ // Create new instance of the coauthors_plus object
876
+ global $coauthors_plus;
877
+ $coauthors_plus = new coauthors_plus();
878
+
879
+ // Add template tags
880
+ require_once('template-tags.php');
881
+
882
+ } else {
883
+ // TODO: show error
884
+
885
+ }
886
+ }
887
+
888
+ /**
889
+ * Function to trigger actions when plugin is activated
890
+ */
891
+ function coauthors_plus_activate_plugin() {}
892
+
893
+ /** Let's get the plugin rolling **/
894
+ add_action( 'init', 'coauthors_plus_init' );
895
+
896
+ // Hook to perform action when plugin activated
897
+ register_activation_hook( __FILE__, 'coauthors_plus_activate_plugin' );
co-authors.php DELETED
@@ -1,648 +0,0 @@
1
- <?php
2
- /*
3
- Plugin Name: Co-Authors Plus
4
- Plugin URI: http://wordpress.org/extend/plugins/co-authors-plus/
5
- Description: Allows multiple authors to be assigned to a post. Co-authored posts appear on a co-author's posts page and feed. New template tags allow listing of co-authors. Editors may assign co-authors to a post via the 'Post Author' box. <em>This plugin is an extended version of the Co-Authors plugin originally developed at [Shepherd Interactive](http://www.shepherd-interactive.com/ "Shepherd Interactive specializes in web design and development in Portland, Oregon") (2007). Their plugin was inspired by 'Multiple Authors' plugin by Mark Jaquith (2005).</em>
6
- Version: 2.1.1
7
- Author: Mohammad Jangda
8
- Author URI: http://digitalize.ca
9
- Copyright: Some parts (C) 2009, Mohammad Jangda; Other parts (C) 2008, Weston Ruter, Shepherd Interactive
10
-
11
- GNU General Public License, Free Software Foundation <http://creativecommons.org/licenses/GPL/2.0/>
12
- This program is free software; you can redistribute it and/or modify
13
- it under the terms of the GNU General Public License as published by
14
- the Free Software Foundation; either version 2 of the License, or
15
- (at your option) any later version.
16
-
17
- This program is distributed in the hope that it will be useful,
18
- but WITHOUT ANY WARRANTY; without even the implied warranty of
19
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
- GNU General Public License for more details.
21
-
22
- You should have received a copy of the GNU General Public License
23
- along with this program; if not, write to the Free Software
24
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
-
26
- */
27
-
28
- $plugin_dir = basename(dirname(__FILE__));
29
- load_plugin_textdomain( 'co-authors-plus','wp-content/plugins/'.$plugin_dir, $plugin_dir);
30
-
31
-
32
- define('COAUTHORS_FILE_PATH', '');
33
- define('COAUTHORS_DEFAULT_BEFORE', '');
34
- define('COAUTHORS_DEFAULT_BETWEEN', ', ');
35
- define('COAUTHORS_DEFAULT_BETWEEN_LAST', __(' and ', 'co-authors-plus'));
36
- define('COAUTHORS_DEFAULT_AFTER', '');
37
- define('COAUTHORS_PLUS_VERSION', '2.1');
38
-
39
- require_once('template-tags.php');
40
-
41
- class coauthors_plus {
42
-
43
- // Name for the taxonomy we're using to store coauthors
44
- var $coauthor_taxonomy = 'author';
45
- // Unique identified added as a prefix to all options
46
- var $options_group = 'coauthors_plus_';
47
- // Initially stores default option values, but when load_options is run, it is populated with the options stored in the WP db
48
- var $options = array(
49
- 'allow_subscribers_as_authors' => 0,
50
- );
51
-
52
- function __construct() {
53
- global $pagenow;
54
-
55
- // Load plugin options
56
- $this->load_options();
57
-
58
- // Register new taxonomy so that we can store all our authors
59
- if(!is_taxonomy($this->coauthor_taxonomy)) register_taxonomy( $this->coauthor_taxonomy, 'post', array('hierarchical' => false, 'update_count_callback' => '_update_post_term_count', 'label' => false, 'query_var' => false, 'rewrite' => false, 'sort' => true ) );
60
-
61
- // Modify SQL queries to include coauthors
62
- add_filter('posts_where', array(&$this, 'posts_where_filter'));
63
- add_filter('posts_join', array(&$this, 'posts_join_filter'));
64
- add_filter('posts_groupby', array(&$this, 'posts_groupby_filter'));
65
-
66
- // Hooks to add additional coauthors to author column to Edit Posts page
67
- if($pagenow == 'edit.php') {
68
- add_filter('manage_posts_columns', array(&$this, '_filter_manage_posts_columns'));
69
- add_action('manage_posts_custom_column', array(&$this, '_filter_manage_posts_custom_column'));
70
- }
71
-
72
- // Action to set users when a post is saved
73
- //add_action('edit_post', array(&$this, 'coauthors_update_post'));
74
- add_action('save_post', array(&$this, 'coauthors_update_post'));
75
-
76
- // Action to reassign posts when a user is deleted
77
- add_action('delete_user', array(&$this, 'delete_user_action'));
78
-
79
- // Action to set up author auto-suggest
80
- add_action('wp_ajax_coauthors_ajax_suggest', array(&$this, 'ajax_suggest') );
81
-
82
- // Filter to allow coauthors to edit posts
83
- add_filter('user_has_cap', array(&$this, 'add_coauthor_cap'), 10, 3 );
84
-
85
- add_filter('comment_notification_headers', array(&$this, 'notify_coauthors'), 10, 3);
86
-
87
- // Add the main JS script and CSS file
88
- if(get_bloginfo('version') >= 2.8) {
89
- // Using WordPress 2.8, are we?
90
- add_action('admin_enqueue_scripts', array(&$this, 'enqueue_scripts'));
91
- } else {
92
- // Pfft, you're old school
93
- add_action('admin_print_scripts', array(&$this, 'enqueue_scripts_legacy'));
94
- }
95
- // Add necessary JS variables
96
- add_action('admin_print_scripts', array(&$this, 'js_vars') );
97
-
98
- }
99
-
100
- function init() {
101
- //Add the necessary pages for the plugin
102
- add_action('admin_menu', array(&$this, 'add_menu_items'));
103
- }
104
-
105
- /**
106
- * Initialize the plugin for the admin
107
- */
108
- function admin_init() {
109
- // Register all plugin settings so that we can change them and such
110
- foreach($this->options as $option => $value) {
111
- register_setting($this->options_group, $this->get_plugin_option_fullname($option));
112
- }
113
- }
114
- /**
115
- * Function to trigger actions when plugin is activated
116
- */
117
- function activate_plugin() {}
118
-
119
- /**
120
- * Adds menu items for the plugin
121
- */
122
- function add_menu_items ( ) {
123
- // Add sub-menu page for Custom statuses
124
- add_options_page(__('Co-Authors Plus', 'co-authors-plus'), __('Co-Authors Plus', 'co-authors-plus'), 8, __FILE__, array(&$this, 'settings_page'));
125
- }
126
-
127
- /**
128
- * Add coauthors to author column on edit pages
129
- *
130
- * @param array $post_columns
131
- **/
132
- function _filter_manage_posts_columns($posts_columns) {
133
- $new_columns = array();
134
- foreach ($posts_columns as $key => $value) {
135
- $new_columns[$key] = $value;
136
- if ($key == 'author') {
137
- unset($new_columns[$key]);
138
- $new_columns['coauthors'] = __('Authors', 'co-authors-plus');
139
- }
140
- }
141
- return $new_columns;
142
- } // END: _filter_manage_posts_columns
143
-
144
- /**
145
- * Insert coauthors into post rows on Edit Page
146
- *
147
- * @param string $column_name
148
- **/
149
- function _filter_manage_posts_custom_column($column_name) {
150
- if ($column_name == 'coauthors') {
151
- global $post;
152
- $authors = get_coauthors($post->ID);
153
-
154
- $count = 1;
155
- foreach($authors as $author) :
156
- ?>
157
- <a href="edit.php?author=<?php echo $author->ID; ?>"><?php echo $author->display_name ?></a><?php echo ($count < count($authors)) ? ',' : ''; ?>
158
- <?php
159
- $count++;
160
- endforeach;
161
- }
162
- }
163
-
164
- /* Modify the author query posts SQL to include posts co-authored
165
- *
166
- */
167
- function posts_join_filter($join){
168
- global $wpdb,$wp_query;
169
-
170
- if(is_author()){
171
- $join .= " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";
172
- }
173
- return $join;
174
- }
175
- /* Modify
176
- *
177
- */
178
- function posts_where_filter($where){
179
- global $wpdb, $wp_query;
180
-
181
- if(is_author()) {
182
- $author = get_userdata($wp_query->query_vars['author']);//get_profile( 'user_login', $wp_query->query_vars['author']);
183
- $term = get_term_by('name', $author->user_login, $this->coauthor_taxonomy);
184
-
185
- if($author) {
186
- $where = preg_replace('/(\b(?:' . $wpdb->posts . '\.)?post_author\s*=\s*(\d+))/', '($1 OR (' . $wpdb->term_taxonomy . '.taxonomy = \''. $this->coauthor_taxonomy.'\' AND '. $wpdb->term_taxonomy .'.term_id = \''. $term->term_id .'\'))', $where, 1); #' . $wpdb->postmeta . '.meta_id IS NOT NULL AND
187
-
188
- }
189
- }
190
- return $where;
191
- }
192
- /*
193
- *
194
- */
195
- function posts_groupby_filter($groupby){
196
- global $wpdb;
197
-
198
- if(is_author()) {
199
- $groupby = $wpdb->posts .'.ID';
200
- }
201
- return $groupby;
202
- }
203
-
204
-
205
- /* Update a post's co-authors
206
- * @param $postID
207
- * @return
208
- */
209
- function coauthors_update_post($post_ID){
210
- global $current_user;
211
-
212
- get_currentuserinfo();
213
-
214
- if($current_user->has_cap('edit_others_posts')){
215
- $coauthors = $_POST['coauthors'];
216
- return $this->add_coauthors($post_ID, $coauthors);
217
- }
218
- }
219
-
220
- /* Action taken when user is deleted.
221
- * - User term is removed from all associated posts
222
- * - Option to specify alternate user in place for each post
223
- * @param delete_id
224
- */
225
- function delete_user_action($delete_id){
226
- global $wpdb;
227
-
228
- $reassign_id = absint($_POST['reassign_user']);
229
-
230
- // If reassign posts, do that -- use coauthors_update_post
231
- if($reassign_id) {
232
- // Get posts belonging to deleted author
233
- $reassign_user = get_profile_by_id('user_login', $reassign_id);
234
- // Set to new author
235
- if($reassign_user) {
236
- $post_ids = $wpdb->get_col( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_author = %d", $delete_id) );
237
-
238
- if ($post_ids) {
239
- foreach ($post_ids as $post_id) {
240
- $this->add_coauthors($post_id, array($reassign_user), true);
241
- }
242
- }
243
- }
244
- }
245
-
246
- $delete_user = get_profile_by_id('user_login', $delete_id);
247
- if($delete_user) {
248
- // Delete term
249
- wp_delete_term($delete_user, $this->coauthor_taxonomy);
250
- }
251
- }
252
-
253
- /* Add a user as coauthor for a post
254
- *
255
- */
256
- function add_coauthors( $post_ID, $coauthors, $append = false ) {
257
- global $current_user;
258
-
259
- $post_ID = (int) $post_ID;
260
-
261
- // if an array isn't returned, create one and populate with default author
262
- if (!is_array($coauthors) || 0 == count($coauthors) || empty($coauthors)) {
263
- // @TOOD: create option for default author
264
- $coauthors = array(get_option('default_author'));
265
- }
266
-
267
- // Add each co-author to the post meta
268
- foreach(array_unique($coauthors) as $author){
269
-
270
- // Name and slug of term are the username;
271
- $name = $author;
272
-
273
- // Add user as a term if they don't exist
274
- if(!is_term($name, $this->coauthor_taxonomy) ) {
275
- $args = array('slug' => sanitize_title($name) );
276
- $insert = wp_insert_term( $name, $this->coauthor_taxonomy, $args );
277
- }
278
- }
279
-
280
- // Add authors as post terms
281
- if(!is_wp_error($insert)) {
282
- $set = wp_set_post_terms( $post_ID, $coauthors, $this->coauthor_taxonomy, $append );
283
- } else {
284
- // @TODO: error
285
- }
286
-
287
- }
288
-
289
- /* Main function that handles search-as-you-type
290
- *
291
- */
292
- function ajax_suggest() {
293
- global $wpdb, $current_user;
294
-
295
- // Make sure that user is logged in; we don't want to enable direct access
296
- get_currentuserinfo();
297
- global $user_level;
298
-
299
- if($current_user->has_cap('edit_others_posts')) {
300
-
301
- // Set the minimum level of users to return
302
- if(!$this->get_plugin_option('allow_subscribers_as_authors')) {
303
- $user_level_where = "WHERE meta_key = '".$wpdb->prefix."user_level' AND meta_value >= 1";
304
- }
305
-
306
- // @TODO validate return
307
- $q = '%'.strtolower($_REQUEST["q"]).'%';
308
- if (!$q) return;
309
-
310
- $authors_query = $wpdb->prepare("SELECT DISTINCT u.ID, u.user_login, u.display_name, u.user_email FROM $wpdb->users AS u"
311
- ." INNER JOIN $wpdb->usermeta AS um ON u.ID = um.user_id"
312
- ." WHERE ID = ANY (SELECT user_id FROM $wpdb->usermeta $user_level_where)"
313
- ." AND (um.meta_key = 'first_name' OR um.meta_key = 'last_name' OR um.meta_key = 'nickname')"
314
- ." AND (u.user_login LIKE %s"
315
- ." OR u.user_nicename LIKE %s"
316
- ." OR u.display_name LIKE %s"
317
- ." OR u.user_email LIKE %s"
318
- ." OR um.meta_value LIKE %s)",$q,$q,$q,$q,$q);
319
-
320
- //echo $authors_query;
321
- $authors = $wpdb->get_results($authors_query, ARRAY_A);
322
-
323
- if(is_array($authors)) {
324
- foreach ($authors as $author) {
325
- echo $author['ID'] ." | ". $author['user_login']." | ". $author['display_name'] ." | ".$author['user_email'] ."\n";
326
- }
327
- }
328
- }
329
- die();
330
-
331
- }
332
-
333
- /* Functions to add scripts and css
334
- * enqueue_scripts is for 2.8+; enqueue_scripts_legacy for > 2.8
335
- */
336
- function enqueue_scripts($hook_suffix) {
337
- global $pagenow;
338
-
339
- if($this->is_valid_page()) {
340
- wp_enqueue_style('co-authors-plus', plugins_url('co-authors-plus/admin.css'), false, '', 'all');
341
- wp_enqueue_script('co-authors-plus', plugins_url('co-authors-plus/admin.js'), array('jquery', 'suggest', 'sack'), '', true);
342
- }
343
-
344
-
345
- //wp_dropdown_users(array('name' => 'ef_author', 'include' => $current_user->ID ));
346
-
347
- }
348
- function enqueue_scripts_legacy($hook_suffix) {
349
- global $pagenow;
350
-
351
- if($this->is_valid_page()) {
352
- //wp_enqueue_style('co-authors-plus', plugins_url('co-authors-plus/admin.css'), false, '', 'all');
353
- wp_enqueue_script('co-authors-plus', plugins_url('co-authors-plus/admin.js'), array('jquery', 'suggest', 'sack'), '');
354
- ?>
355
- <link type="text/css" rel="stylesheet" href="<?php echo plugins_url('co-authors-plus/admin.css') ?>" media="all" />
356
- <?php
357
- }
358
- }
359
-
360
- /* Adds necessary javascript variables to admin pages
361
- *
362
- */
363
- function js_vars() {
364
- global $current_user, $post_ID;
365
-
366
- get_currentuserinfo();
367
-
368
- if($this->is_valid_page()) {
369
- //wp_print_scripts( array( 'sack' ));
370
- $coauthors = get_coauthors( $post_ID );
371
- ?>
372
- <script type="text/javascript">
373
-
374
- // AJAX link used for the autosuggest
375
- var coauthor_ajax_suggest_link = "<?php echo 'admin-ajax.php?action=coauthors_ajax_suggest' ?>";
376
-
377
- if(!i18n || i18n == 'undefined') var i18n = {};
378
- i18n.coauthors = {};
379
-
380
- var coauthors_can_edit_others_posts = "<?php echo ($current_user->has_cap('edit_others_posts') ? 'true' : 'false')?>";
381
-
382
- i18n.coauthors.post_metabox_title = "<?php _e('Post Author(s)', 'co-authors-plus')?>";
383
- i18n.coauthors.page_metabox_title = "<?php _e('Page Author(s)', 'co-authors-plus')?>";
384
- i18n.coauthors.edit_label = "<?php _e('Edit', 'co-authors-plus')?>";
385
- i18n.coauthors.delete_label = "<?php _e('Delete', 'co-authors-plus')?>";
386
- i18n.coauthors.confirm_delete = "<?php _e('Are you sure you want to delete this author?', 'co-authors-plus')?>";
387
- i18n.coauthors.input_box_title = "<?php _e('Click to change this author', 'co-authors-plus')?>";
388
- i18n.coauthors.search_box_text = "<?php _e('Search for an author', 'co-authors-plus')?>";
389
- i18n.coauthors.help_text = "<?php _e('Click on an author to change them. Click on <strong>Delete</strong> to remove them.', 'co-authors-plus')?>";
390
-
391
- <?php if(is_array($coauthors) && !(empty($coauthors))) : ?>
392
- var post_coauthors = [
393
- <?php
394
- foreach($coauthors as $author) {
395
- echo "{";
396
- echo "'login': escape('". $author->user_login ."'),";
397
- echo "'name': escape('". $author->display_name ."'),";
398
- echo "'id': '". $author->ID ."'";
399
- echo "},";
400
- }
401
- ?>
402
- ];
403
- <?php else : ?>
404
- var post_coauthors = [
405
- <?php
406
- echo "{";
407
- echo "'login': '". $current_user->user_login ."',";
408
- echo "'name': '". $current_user->display_name ."',";
409
- echo "'id': '". $current_user->ID ."'";
410
- echo "},";
411
- ?>
412
- ];
413
- <?php endif; ?>
414
- </script>
415
- <?php
416
- }
417
- } // END: js_vars()
418
-
419
- /* Helper to only add javascript to necessary pages
420
- * Avoid bloat on admin
421
- */
422
- private function is_valid_page() {
423
- global $pagenow;
424
-
425
- $pages = array('edit.php', 'post.php', 'post-new.php', 'page.php', 'page-new.php');
426
-
427
- if(in_array($pagenow, $pages)) return true;
428
-
429
- return false;
430
- }
431
-
432
- /* Allows coauthors to edit the post they're coauthors of
433
- *
434
- */
435
- function add_coauthor_cap( $allcaps, $caps, $args ) {
436
-
437
- if(in_array('edit_post', $args) || in_array('edit_others_posts', $args)) {
438
- // @TODO: Fix this disgusting hardcodedness. Ew.
439
- $user_id = $args[1];
440
- $post_id = $args[2];
441
- if(is_coauthor_for_post($user_id, $post_id)) {
442
- // @TODO check to see if can edit publish posts if post is published
443
- // @TODO check to see if can edit posts at all
444
- foreach($caps as $cap) {
445
- $allcaps[$cap] = 1;
446
- }
447
- }
448
- }
449
- return $allcaps;
450
- }
451
-
452
- /* Emails all coauthors when comment added instead of the main author
453
- *
454
- */
455
- function notify_coauthors( $message_headers, $comment_id ) {
456
- //echo '<p>Pre:';
457
- //print_r($message_headers);
458
- $comment = get_comment($comment_id);
459
- $post = get_post($comment->comment_post_ID);
460
- $coauthors = get_coauthors($comment->comment_post_ID);
461
-
462
- $message_headers .= 'cc: ';
463
- $count = 0;
464
- foreach($coauthors as $author) {
465
- $count++;
466
- if($author->ID != $post->post_author){
467
- $message_headers .= $author->user_email;
468
- if($count < count($coauthors)) $message_headers .= ',';
469
- }
470
- }
471
- $message_headers .= "\n";
472
- return $message_headers;
473
- //echo '<p>Post:';
474
- //print_r($message_headers);
475
-
476
- }
477
-
478
- /**
479
- * Loads options for the plugin.
480
- * If option doesn't exist in database, it is added
481
- *
482
- * Note: default values are stored in the $this->options array
483
- * Note: a prefix unique to the plugin is appended to all options. Prefix is stored in $this->options_group
484
- */
485
- protected function load_options ( ) {
486
-
487
- $new_options = array();
488
-
489
- foreach($this->options as $option => $value) {
490
- $name = $this->get_plugin_option_fullname($option);
491
- $return = get_option($name);
492
- if($return === false) {
493
- add_option($name, $value);
494
- $new_array[$option] = $value;
495
- } else {
496
- $new_array[$option] = $return;
497
- }
498
- }
499
- $this->options = $new_array;
500
-
501
- } // END: load_options
502
-
503
-
504
- /**
505
- * Returns option for the plugin specified by $name, e.g. custom_stati_enabled
506
- *
507
- * Note: The plugin option prefix does not need to be included in $name
508
- *
509
- * @param string name of the option
510
- * @return option|null if not found
511
- *
512
- */
513
- function get_plugin_option ( $name ) {
514
- if(is_array($this->options) && $option = $this->options[$name])
515
- return $option;
516
- else
517
- return null;
518
- } // END: get_option
519
-
520
- // Utility function: appends the option prefix and returns the full name of the option as it is stored in the wp_options db
521
- protected function get_plugin_option_fullname ( $name ) {
522
- return $this->options_group . $name;
523
- }
524
-
525
- /* Adds Settings page for Edit Flow
526
- *
527
- */
528
- function settings_page( ) {
529
- global $wp_roles;
530
-
531
- ?>
532
- <div class="wrap">
533
- <div class="icon32" id="icon-options-general"><br/></div>
534
- <h2><?php _e('Co-Authors Plus', 'co-authors-plus') ?></h2>
535
-
536
- <form method="post" action="options.php">
537
- <?php settings_fields($this->options_group); ?>
538
-
539
- <table class="form-table">
540
- <tr valign="top">
541
- <th scope="row"><strong><?php _e('Roles', 'co-authors-plus') ?></strong></th>
542
- <td>
543
- <p>
544
- <label for="allow_subscribers_as_authors">
545
- <input type="checkbox" name="<?php echo $this->get_plugin_option_fullname('allow_subscribers_as_authors') ?>" value="1" <?php echo ($this->get_plugin_option('allow_subscribers_as_authors')) ? 'checked="checked"' : ''; ?> id="allow_subscribers_as_authors" /> <?php _e('Allow subscribers as authors', 'co-authors-plus') ?>
546
- </label> <br />
547
- <span class="description"><?php _e('Enabling this option will allow you to add users with the subscriber role as authors for posts.', 'co-authors-plus') ?></span>
548
- </p>
549
- </td>
550
- </tr>
551
-
552
- </table>
553
-
554
- <p class="submit">
555
- <input type="submit" class="button-primary" value="<?php _e('Save Changes', 'co-authors-plus') ?>" />
556
- </p>
557
- </form>
558
- </div>
559
- <?php
560
- }
561
-
562
-
563
- /* Function updates coauthors from old meta-based storage to taxonomy-based
564
- *
565
- */
566
- function update() {
567
- // Get all posts with meta_key _coauthor
568
- $all_posts = get_posts(array('numberposts' => '-1'));
569
-
570
- //echo '<p>returned posts ('.count($all_posts).'):</p>';
571
- //print_r($posts);
572
- //echo '<hr />';
573
-
574
- if(is_array($all_posts)) {
575
- foreach($all_posts as $single_post) {
576
-
577
- // reset execution time limit
578
- set_time_limit( 60 );
579
-
580
- // create new array
581
- $coauthors = array();
582
- // get author id -- try to use get_profile
583
- $coauthors[] = get_profile_by_id('user_login', (int)$single_post->post_author);
584
- // get coauthors id
585
- $legacy_coauthors = get_post_meta($single_post->ID, '_coauthor');
586
- //print_r($legacy_coauthors);
587
- //echo '<hr />';
588
- if(is_array($legacy_coauthors)) {
589
- //echo '<p>Has Legacy coauthors';
590
- foreach($legacy_coauthors as $legacy_coauthor) {
591
- $legacy_coauthor_login = get_profile_by_id('user_login', (int)$legacy_coauthor);
592
- if($legacy_coauthor_login) $coauthors[] = $legacy_coauthor_login;
593
- }
594
- } else {
595
- //echo '<p>No Legacy coauthors';
596
- }
597
- //echo '<p>Post '.$single_post->ID;
598
- //print_r($coauthors);
599
- //echo '<hr />';
600
- $this->add_coauthors($single_post->ID, $coauthors);
601
-
602
- }
603
- }
604
- }
605
-
606
- /*
607
- * @TODO
608
- * - Add new author
609
- * - Add search-as-you-type to QuikcEdit
610
- * - get_coauthor_meta function
611
- */
612
-
613
- }
614
-
615
- /** Helper Functions **/
616
-
617
- /* Replacement for the default WordPress get_profile function, since that doesn't allow for search by user_id
618
- * Returns a the specified column value for the specified user
619
- */
620
-
621
- if(!function_exists('get_profile_by_id')) {
622
- function get_profile_by_id($field, $user_id) {
623
- global $wpdb;
624
- if($field && $user_id) return $wpdb->get_var( $wpdb->prepare("SELECT $field FROM $wpdb->users WHERE ID = %d", $user_id) );
625
- return false;
626
- }
627
- }
628
-
629
- /** Let's get the plugin rolling **/
630
-
631
- // Create new instance of the edit_flow object
632
- global $coauthors_plus;
633
- $coauthors_plus = new coauthors_plus();
634
- //$coauthors_plus->update();
635
-
636
- // Core hooks to initialize the plugin
637
- add_action('init', array(&$coauthors_plus,'init'));
638
- add_action('admin_init', array(&$coauthors_plus,'admin_init'));
639
-
640
- // Hook to perform action when plugin activated
641
- register_activation_hook( __FILE__, array(&$edit_flow, 'activate_plugin'));
642
-
643
- // Upgrade to new taxonomy system
644
- if(floatval(get_option('coauthors_plus_version')) < 2.0) $coauthors_plus->update();
645
-
646
- update_option('coauthors_plus_version', COAUTHORS_PLUS_VERSION);
647
-
648
- ?>