forked from thebrandonallen/edit-author-slug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit-author-slug.php
513 lines (432 loc) · 14.4 KB
/
edit-author-slug.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
<?php
/**
* Edit Author Slug Plugin
*
* Customize a user's author links.
*
* @package Edit_Author_Slug
* @subpackage Main
*
* @author Brandon Allen
*/
/**
* Plugin Name: Edit Author Slug
* Plugin URI: https://github.com/thebrandonallen/edit-author-slug/
* Description: Allows an Admin (or capable user) to edit the author slug of a user, and change the Author Base. <em>i.e. - (WordPress default structure) http://example.com/author/username/ (Plugin allows) http://example.com/ninja/master-ninja/</em>
* Version: 1.1.2
* Tested With: 3.8.10, 3.9.9, 4.0.8, 4.1.8, 4.2.5, 4.3.1
* Author: Brandon Allen
* Author URI: https://github.com/thebrandonallen/
* License: GPLv2 or later
* Text Domain: edit-author-slug
* Domain Path: /languages
*/
/*
Copyright 2015 Brandon Allen (email : plugins ([at]) brandonallen ([dot]) me)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) { exit; }
/**
* Main Edit Author Slug class.
*/
if ( ! class_exists( 'BA_Edit_Author_Slug' ) ) :
/**
* Final BA_Edit_Author_Slug class.
*
* @since 0.1.0
*
* @final
*
* @property string $version
* @property int $db_version
* @property int $current_db_version
* @property string $file
* @property string $plugin_dir
* @property string $plugin_url
* @property string $plugin_basename
* @property string $domain
* @property string $author_base
* @property int $do_auto_update
* @property string $default_user_nicename
* @property int $do_role_based
* @property array $role_slugs
*/
final class BA_Edit_Author_Slug {
/** Magic *************************************************************/
/**
* Edit Author Slug uses many variables, several of which can be filtered
* to customize the way it operates. Most of these variables are stored
* in a private array that gets updated with the help of PHP magic methods.
*
* This is a precautionary measure, to avoid potential errors produced by
* unanticipated direct manipulation of Edit Author Slug's run-time data.
*
* @since 1.0.0
*
* @see BA_Edit_Author_Slug::setup_globals()
* @var object
*/
private $data;
/** Singleton *********************************************************/
/**
* Main BA_Edit_Author_Slug Instance
*
* Insures that only one instance of BA_Edit_Author_Slug exists in memory
* at any one time. Also prevents needing to define globals all over the
* place.
*
* @since 1.0.0
*
* @staticvar object $instance
*
* @uses BA_Edit_Author_Slug::setup_globals() Setup the globals needed.
* @uses BA_Edit_Author_Slug::includes() Include the required files.
* @uses BA_Edit_Author_Slug::setup_actions() Setup the hooks and actions.
*
* @see ba_eas()
*
* @return BA_Edit_Author_Slug|null The one true BA_Edit_Author_Slug.
*/
public static function instance() {
// Store the instance locally to avoid private static replication.
static $instance = null;
// Only run these methods if they haven't been ran previously.
if ( null === $instance ) {
$instance = new BA_Edit_Author_Slug;
$instance->setup_globals();
$instance->includes();
$instance->setup_actions();
}
// Always return the instance.
return $instance;
}
/** Magic Methods *****************************************************/
/**
* A dummy constructor to prevent BA_Edit_Author_Slug from being loaded
* more than once.
*
* @since 0.7.0
*
* @see BA_Edit_Author_Slug::instance()
* @see ba_eas();
*/
private function __construct() { /* Do nothing here */ }
/**
* A dummy magic method to prevent BA_Edit_Author_Slug from being cloned.
*
* @since 1.0.0
*
* @return void
*/
public function __clone() { _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'edit-author-slug' ), '1.0' ); }
/**
* A dummy magic method to prevent BA_Edit_Author_Slug from being unserialized.
*
* @since 1.0.0
*
* @return void
*/
public function __wakeup() { _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'edit-author-slug' ), '1.0' ); }
/**
* Magic method for checking the existence of a certain custom field.
*
* @since 1.0.0
*
* @return bool True if the field is set.
*/
public function __isset( $key ) { return isset( $this->data->$key ); }
/**
* Magic method for getting BA_Edit_Author_Slug variables.
*
* @since 1.0.0
*
* @return mixed The field value if it exists. Null otherwise.
*/
public function __get( $key ) { return isset( $this->data->$key ) ? $this->data->$key : null; }
/**
* Magic method for setting BA_Edit_Author_Slug variables.
*
* @since 1.0.0
*
* @return void
*/
public function __set( $key, $value ) { $this->data->$key = $value; }
/**
* Magic method for unsetting BA_Edit_Author_Slug variables.
*
* @since 1.0.0
*
* @return void
*/
public function __unset( $key ) { if ( isset( $this->data->$key ) ) { unset( $this->data->$key ); } }
/**
* Magic method to prevent notices and errors from invalid method calls.
*
* @since 1.0.0
*
* @return void
*/
public function __call( $name = '', $args = array() ) { unset( $name, $args ); return null; }
/** Private Methods ***************************************************/
/**
* Edit Author Slug global variables.
*
* @since 0.7.0
*
* @uses plugin_dir_path() To generate Edit Author Slug plugin path.
* @uses plugin_dir_url() To generate Edit Author Slug plugin url.
* @uses plugin_basename() To get the plugin basename.
* @uses get_option() To get the Edit Author Slug options.
* @uses absint() To cast db variables as absolute integers.
*
* @return void
*/
private function setup_globals() {
/** Magic *********************************************************/
$this->data = new stdClass();
/** Versions ******************************************************/
$this->version = '1.1.2';
$this->db_version = 133;
$this->current_db_version = 0;
/** Paths *********************************************************/
$this->file = __FILE__;
$this->plugin_dir = plugin_dir_path( $this->file );
$this->plugin_url = plugin_dir_url( $this->file );
$this->plugin_basename = plugin_basename( $this->file );
/** Miscellaneous *************************************************/
$this->domain = 'edit-author-slug';
/** Options *******************************************************/
// Setup author base.
$this->author_base = 'author';
// Options.
if ( $base = get_option( '_ba_eas_author_base' ) ) {
// Sanitize the db value.
$base = sanitize_title( $base );
// Author base.
if ( ! empty( $base ) ) {
$this->author_base = $base;
}
// Current DB version.
$this->current_db_version = absint( get_option( '_ba_eas_db_version' ) );
// Pre-0.9 Back compat.
} elseif ( $options = get_option( 'ba_edit_author_slug' ) ) {
// Sanitize the db value.
if ( ! empty( $options['author_base'] ) ) {
$base = sanitize_title( $options['author_base'] );
}
// Author base.
if ( ! empty( $base ) ) {
$this->author_base = $base;
}
// Current DB version.
if ( ! empty( $options['db_version'] ) ) {
$this->current_db_version = absint( $options['db_version'] );
}
}
// Load auto-update option.
$this->do_auto_update = (bool) absint( get_option( '_ba_eas_do_auto_update', 0 ) );
// Load the default nicename structure for auto-update.
$default_user_nicename = get_option( '_ba_eas_default_user_nicename' );
$default_user_nicename = sanitize_key( $default_user_nicename );
if ( empty( $default_user_nicename ) ) {
$default_user_nicename = 'username';
}
$this->default_user_nicename = $default_user_nicename;
// Load role-based author slug option.
$this->do_role_based = (bool) absint( get_option( '_ba_eas_do_role_based', 0 ) );
// Load role-based author slug option.
$this->role_slugs = array();
}
/**
* Include necessary files.
*
* @since 0.7.0
*
* @uses is_admin() To load admin specific functions.
*
* @return void
*/
private function includes() {
// Load the core functions.
require_once( $this->plugin_dir . 'includes/functions.php' );
require_once( $this->plugin_dir . 'includes/hooks.php' );
// Maybe load the admin functions.
if ( is_admin() ) {
require_once( $this->plugin_dir . 'includes/admin.php' );
}
}
/**
* Display Author slug edit field on User/Profile edit page.
*
* @since 0.7.0
*
* @uses register_activation_hook() To register the activation hook.
* @uses register_deactivation_hook() To register the deactivation hook.
* @uses add_action() To call various hooks.
*
* @return void
*/
private function setup_actions() {
// Register Edit Author Slug activation/deactivation sequences.
register_activation_hook( $this->file, 'ba_eas_activation' );
register_deactivation_hook( $this->file, 'ba_eas_deactivation' );
// Author Base Actions.
add_action( 'after_setup_theme', array( $this, 'set_role_slugs' ) );
add_action( 'init', array( $this, 'author_base_rewrite' ), 4 );
add_action( 'init', array( $this, 'add_rewrite_tags' ), 20 );
// Localize.
add_action( 'init', array( $this, 'load_textdomain' ), 0 );
}
/** Public Methods ****************************************************/
/**
* Load the translation file for current language. Checks the Edit Author
* Slug languages folder first, then inside the default WP language
* plugins folder.
*
* Note that custom translation files inside the Edit Author Slug plugin
* folder will be removed on edit-author-slug updates. If you're creating
* custom translation files, please use the global language folder
* (ie - wp-content/languages/plugins).
*
* @since 0.9.6
*
* @uses load_plugin_textdomain() To load the textdomain inside the
* 'plugin/languages' folder.
*
* @return void
*/
public function load_textdomain() {
// Look in wp-content/plugins/edit-author-slug/languages first.
// Fallback to wp-content/languages/plugins.
load_plugin_textdomain( $this->domain, false, dirname( $this->plugin_basename ) . '/languages/' );
}
/**
* Rewrite Author Base according to user's setting.
*
* Rewrites Author Base to user's setting from the
* Author Base field on Options > Permalinks.
*
* @since 0.4.0
*
* @uses ba_eas_do_role_based_author_base() To check if we're doing
* role-based author bases.
*
* @return void
*/
public function author_base_rewrite() {
// Are we doing a role-based author base?
if ( ba_eas_do_role_based_author_base() ) {
$GLOBALS['wp_rewrite']->author_base = '%ba_eas_author_role%';
// Has the author base changed from the default?
} elseif ( ! empty( $this->author_base ) && 'author' !== $this->author_base ) {
$GLOBALS['wp_rewrite']->author_base = $this->author_base;
}
}
/**
* Set the role_slugs global
*
* @since 1.0.0
*
* @uses ba_eas_get_default_role_slugs() To get an array of default role slugs.
* @uses get_option() To get the custom role slugs array.
*
* @return void
*/
public function set_role_slugs() {
// Merge system roles with any customizations we may have.
$this->role_slugs = array_replace_recursive( ba_eas_get_default_role_slugs(), get_option( '_ba_eas_role_slugs', array() ) );
}
/** Custom Rewrite Rules **********************************************/
/**
* Add the Edit Author Slug rewrite tags
*
* @since 1.0.0
*
* @uses ba_eas_do_role_based_author_base() To check if we're doing
* role-based author bases.
* @uses wp_list_pluck() To get only the role slugs.
* @uses add_rewrite_tag() To add the rewrite tags.
*
* @return void
*/
public function add_rewrite_tags() {
// Should we be here?
if ( ! ba_eas_do_role_based_author_base() ) {
return;
}
// Get the role slugs to add the rewrite tag.
$role_slugs = wp_list_pluck( $this->role_slugs, 'slug' );
$role_slugs = array_filter( array_values( $role_slugs ) );
// Add the author base as a fallback.
$role_slugs[] = ba_eas()->author_base;
// Add the role-based rewrite tag, and the expected role slugs.
add_rewrite_tag( '%ba_eas_author_role%', '(' . implode( '|', array_unique( $role_slugs ) ) . ')' );
}
}
/**
* The main function responsible for returning the one true BA_Edit_Author_Slug
* Instance to functions everywhere.
*
* Use this function like you would a global variable, except without needing
* to declare the global.
*
* Example: <?php $ba_eas = ba_eas(); ?>
*
* @return BA_Edit_Author_Slug The one true BA_Edit_Author_Slug Instance.
*/
function ba_eas() {
return BA_Edit_Author_Slug::instance();
}
// Places everyone! The show is starting!
ba_eas();
endif; //end class BA_Edit_Author_Slug.
/**
* Runs on Edit Author Slug activation.
*
* @since 0.7.0
*
* @uses do_action() To call the `ba_eas_activation` hook.
*
* @return void
*/
function ba_eas_activation() {
/**
* Fires on Edit Author Slug activation.
*
* @since 0.7.0
*/
do_action( 'ba_eas_activation' );
// Pre-emptive courtesy flush in case of existing author base.
ba_eas_flush_rewrite_rules();
}
/**
* Runs on Edit Author Slug deactivation.
*
* @since 0.7.0
*
* @uses do_action() To call the `ba_eas_deactivation` hook.
*
* @return void
*/
function ba_eas_deactivation() {
/**
* Fires on Edit Author Slug deactivation.
*
* @since 0.7.0
*/
do_action( 'ba_eas_deactivation' );
// Courtesy flush.
delete_option( 'rewrite_rules' );
}