class-permalinks.php
15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
<?php
namespace um\core;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'um\core\Permalinks' ) ) {
/**
* Class Permalinks
* @package um\core
*/
class Permalinks {
/**
* @var
*/
var $core;
/**
* @var
*/
var $current_url;
/**
* Permalinks constructor.
*/
public function __construct() {
add_action( 'init', array( &$this, 'set_current_url' ), 0 );
add_action( 'init', array( &$this, 'check_for_querystrings' ), 1 );
add_action( 'init', array( &$this, 'activate_account_via_email_link' ), 1 );
}
/**
* Set current URL variable
*/
public function set_current_url() {
$this->current_url = $this->get_current_url();
}
/**
* Get query as array
*
* @return array
*/
public function get_query_array() {
$parts = parse_url( $this->get_current_url() );
if ( isset( $parts['query'] ) ) {
parse_str( $parts['query'], $query );
return $query;
}
return array();
}
/**
* Get current URL anywhere
*
* @param bool $no_query_params
*
* @return string
*/
public function get_current_url( $no_query_params = false ) {
//use WP native function for fill $_SERVER variables by correct values
wp_fix_server_vars();
//check if WP-CLI there isn't set HTTP_HOST, use localhost instead
if ( defined( 'WP_CLI' ) && WP_CLI ) {
$host = isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : 'localhost';
} else {
if ( isset( $_SERVER['HTTP_HOST'] ) ) {
$host = $_SERVER['HTTP_HOST'];
} else {
$host = 'localhost';
}
}
$page_url = ( is_ssl() ? 'https://' : 'http://' ) . $host . $_SERVER['REQUEST_URI'];
if ( false !== $no_query_params ) {
$page_url = strtok( $page_url, '?' );
}
/**
* Filters current page URL.
*
* @param {string} $page_url Current page URL.
* @param {bool} $no_query_params Ignore $_GET attributes in URL. false !== ignore.
*
* @return {string} Current page URL.
*
* @since 1.3.x
* @hook um_get_current_page_url
*
* @example <caption>Add your custom $_GET attribute to all links.</caption>
* function my_um_get_current_page_url( $page_url, $no_query_params ) {
* $page_url = add_query_arg( '{attr_value}', '{attr_key}', $page_url ); // replace to your custom value and key.
* return $page_url;
* }
* add_filter( 'um_get_current_page_url', 'my_um_get_current_page_url', 10, 2 );
*/
return apply_filters( 'um_get_current_page_url', $page_url, $no_query_params );
}
/**
* Activates an account via email
*/
public function activate_account_via_email_link() {
if ( isset( $_REQUEST['act'] ) && 'activate_via_email' === sanitize_key( $_REQUEST['act'] ) && isset( $_REQUEST['hash'] ) && is_string( $_REQUEST['hash'] ) && strlen( $_REQUEST['hash'] ) == 40 &&
isset( $_REQUEST['user_id'] ) && is_numeric( $_REQUEST['user_id'] ) ) { // valid token
$user_id = absint( $_REQUEST['user_id'] );
delete_option( "um_cache_userdata_{$user_id}" );
$account_secret_hash = get_user_meta( $user_id, 'account_secret_hash', true );
if ( empty( $account_secret_hash ) || strtolower( sanitize_text_field( $_REQUEST['hash'] ) ) !== strtolower( $account_secret_hash ) ) {
wp_die( __( 'This activation link is expired or have already been used.', 'ultimate-member' ) );
}
$account_secret_hash_expiry = get_user_meta( $user_id, 'account_secret_hash_expiry', true );
if ( ! empty( $account_secret_hash_expiry ) && time() > $account_secret_hash_expiry ) {
wp_die( __( 'This activation link is expired.', 'ultimate-member' ) );
}
um_fetch_user( $user_id );
UM()->user()->approve();
um_reset_user();
$user_role = UM()->roles()->get_priority_user_role( $user_id );
$user_role_data = UM()->roles()->role_data( $user_role );
// log in automatically
$login = ! empty( $user_role_data['login_email_activate'] ); // Role setting "Login user after validating the activation link?"
if ( ! is_user_logged_in() && $login ) {
$user = get_userdata( $user_id );
// update wp user
wp_set_current_user( $user_id, $user->user_login );
wp_set_auth_cookie( $user_id );
ob_start();
do_action( 'wp_login', $user->user_login, $user );
ob_end_clean();
}
/**
* UM hook
*
* @type action
* @title um_after_email_confirmation
* @description Action on user activation
* @input_vars
* [{"var":"$user_id","type":"int","desc":"User ID"}]
* @change_log
* ["Since: 2.0"]
* @usage add_action( 'um_after_email_confirmation', 'function_name', 10, 1 );
* @example
* <?php
* add_action( 'um_after_email_confirmation', 'my_after_email_confirmation', 10, 1 );
* function my_after_email_confirmation( $user_id ) {
* // your code here
* }
* ?>
*/
do_action( 'um_after_email_confirmation', $user_id );
$redirect = empty( $user_role_data['url_email_activate'] ) ? um_get_core_page( 'login', 'account_active' ) : trim( $user_role_data['url_email_activate'] ); // Role setting "URL redirect after e-mail activation"
$redirect = apply_filters( 'um_after_email_confirmation_redirect', $redirect, $user_id, $login );
exit( wp_redirect( $redirect ) );
}
}
/**
* Makes an activate link for any user
*
* @return bool|string
*/
public function activate_url() {
if ( ! um_user( 'account_secret_hash' ) ) {
return false;
}
/**
* UM hook
*
* @type filter
* @title um_activate_url
* @description Change activate user URL
* @input_vars
* [{"var":"$url","type":"string","desc":"Activate URL"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_activate_url', 'function_name', 10, 1 ); ?>
* @example
* <?php
* add_filter( 'um_activate_url', 'my_activate_url', 10, 1 );
* function my_activate_url( $url ) {
* // your code here
* return $url;
* }
* ?>
*/
$url = apply_filters( 'um_activate_url', home_url() );
$url = add_query_arg( 'act', 'activate_via_email', $url );
$url = add_query_arg( 'hash', um_user( 'account_secret_hash' ), $url );
$url = add_query_arg( 'user_id', um_user( 'ID' ), $url );
return $url;
}
/**
* Checks for UM query strings
*/
public function check_for_querystrings() {
if ( isset( $_REQUEST['message'] ) ) {
UM()->shortcodes()->message_mode = true;
}
}
/**
* Add a query param to url
*
* @param $key
* @param $value
*
* @return string
*/
public function add_query( $key, $value ) {
$this->current_url = add_query_arg( $key, $value, $this->get_current_url() );
return $this->current_url;
}
/**
* Remove a query param from url
*
* @param $key
* @param $value
*
* @return string
*/
public function remove_query( $key, $value ) {
$this->current_url = remove_query_arg( $key, $this->current_url );
return $this->current_url;
}
/**
* @param $slug
*
* @return int|null|string
*/
public function slug_exists_user_id( $slug ) {
global $wpdb;
$permalink_base = UM()->options()->get( 'permalink_base' );
$user_id = $wpdb->get_var(
"SELECT user_id
FROM {$wpdb->usermeta}
WHERE meta_key = 'um_user_profile_url_slug_{$permalink_base}' AND
meta_value = '{$slug}'
ORDER BY umeta_id ASC
LIMIT 1"
);
if ( ! empty( $user_id ) ) {
return $user_id;
}
return false;
}
/**
* Get Profile Permalink
*
* @param string $slug
*
* @return string
*/
public function profile_permalink( $slug ) {
/**
* Filters user profile URL externally with own logic.
*
* @since 2.6.3
* @hook um_external_profile_url
*
* @param {bool|string} $profile_url Profile URL.
* @param {string} $slug User profile slug.
*
* @return {string} Profile URL.
*
* @example <caption>Change profile URL to your custom link and ignore native profile permalink handlers.</caption>
* function my_um_external_profile_url( $profile_url, $slug ) {
* $profile_url = '{some your custom URL}'; // replace to your custom link.
* return $profile_url;
* }
* add_filter( 'um_external_profile_url', 'my_um_external_profile_url', 10, 2 );
*/
$external_profile_url = apply_filters( 'um_external_profile_url', false, $slug );
if ( false !== $external_profile_url ) {
return ! empty( $external_profile_url ) ? $external_profile_url : '';
}
$page_id = UM()->config()->permalinks['user'];
$profile_url = get_permalink( $page_id );
/**
* Filters the base URL of the UM profile page.
*
* @since 1.3.x
* @deprecated 2.6.3 Use <a href="https://developer.wordpress.org/reference/hooks/post_link/" target="_blank" title="'post_link' hook article on developer.wordpress.org">'post_link'</a> instead.
* @hook um_localize_permalink_filter
* @todo fully remove since 2.7.0
*
* @param {string} $profile_url Profile URL.
* @param {int} $page_id Profile Page ID.
*
* @return {string} Profile URL.
*
* @example <caption>Change profile base URL to your custom link.</caption>
* function my_localize_permalink( $profile_url, $page_id ) {
* $profile_url = '{some your custom URL}'; // replace to your custom link.
* return $profile_url;
* }
* add_filter( 'um_localize_permalink_filter', 'my_localize_permalink', 10, 2 );
*/
$profile_url = apply_filters( 'um_localize_permalink_filter', $profile_url, $page_id );
if ( UM()->is_permalinks ) {
$profile_url = trailingslashit( untrailingslashit( $profile_url ) );
$profile_url .= trailingslashit( strtolower( $slug ) );
} else {
$profile_url = add_query_arg( 'um_user', strtolower( $slug ), $profile_url );
}
/**
* Filters user profile URL.
*
* @since 2.6.3
* @hook um_profile_permalink
*
* @param {string} $profile_url Profile URL.
* @param {int} $page_id Profile Page ID.
* @param {string} $slug User profile slug.
*
* @return {string} Profile URL.
*
* @example <caption>Change profile URL to your custom link.</caption>
* function my_um_profile_permalink( $profile_url, $page_id, $slug ) {
* $profile_url = '{some your custom URL}'; // replace to your custom link.
* return $profile_url;
* }
* add_filter( 'um_profile_permalink', 'my_um_profile_permalink', 10, 3 );
*/
$profile_url = apply_filters( 'um_profile_permalink', $profile_url, $page_id, $slug );
return ! empty( $profile_url ) ? $profile_url : '';
}
/**
* Generate profile slug
*
* @param string $full_name
* @param string $first_name
* @param string $last_name
* @return string
*/
public function profile_slug( $full_name, $first_name, $last_name ) {
$permalink_base = UM()->options()->get( 'permalink_base' );
$user_in_url = '';
$full_name = str_replace( "'", "", $full_name );
$full_name = str_replace( "&", "", $full_name );
$full_name = str_replace( "/", "", $full_name );
switch ( $permalink_base ) {
case 'name': // dotted
$full_name_slug = $full_name;
$difficulties = 0;
if ( strpos( $full_name, '.' ) > -1 ) {
$full_name = str_replace( ".", "_", $full_name );
$difficulties++;
}
$full_name = strtolower( str_replace( " ", ".", $full_name ) );
if ( strpos( $full_name, '_.' ) > -1 ) {
$full_name = str_replace( '_.', '_', $full_name );
$difficulties++;
}
$full_name_slug = str_replace( '-', '.', $full_name_slug );
$full_name_slug = str_replace( ' ', '.', $full_name_slug );
$full_name_slug = str_replace( '..', '.', $full_name_slug );
if ( strpos( $full_name, '.' ) > -1 ) {
$full_name = str_replace( '.', ' ', $full_name );
$difficulties++;
}
$user_in_url = rawurlencode( $full_name_slug );
break;
case 'name_dash': // dashed
$difficulties = 0;
$full_name_slug = strtolower( $full_name );
// if last name has dashed replace with underscore
if ( strpos( $last_name, '-' ) > -1 && strpos( $full_name, '-' ) > -1 ) {
$difficulties++;
$full_name = str_replace( '-', '_', $full_name );
}
// if first name has dashed replace with underscore
if ( strpos( $first_name, '-' ) > -1 && strpos( $full_name, '-' ) > -1 ) {
$difficulties++;
$full_name = str_replace( '-', '_', $full_name );
}
// if name has space, replace with dash
$full_name_slug = str_replace( ' ', '-', $full_name_slug );
// if name has period
if ( strpos( $last_name, '.' ) > -1 && strpos( $full_name, '.' ) > -1 ) {
$difficulties++;
}
$full_name_slug = str_replace( '.', '-', $full_name_slug );
$full_name_slug = str_replace( '--', '-', $full_name_slug );
$user_in_url = rawurlencode( $full_name_slug );
break;
case 'name_plus': // plus
$difficulties = 0;
$full_name_slug = strtolower( $full_name );
// if last name has dashed replace with underscore
if ( strpos( $last_name, '+' ) > -1 && strpos( $full_name, '+' ) > -1 ) {
$difficulties++;
$full_name = str_replace( '-', '_', $full_name );
}
// if first name has dashed replace with underscore
if ( strpos( $first_name, '+' ) > -1 && strpos( $full_name, '+' ) > -1 ) {
$difficulties++;
$full_name = str_replace( '-', '_', $full_name );
}
if ( strpos( $last_name, '-' ) > -1 || strpos( $first_name, '-' ) > -1 || strpos( $full_name, '-' ) > -1 ) {
$difficulties++;
}
// if name has space, replace with dash
$full_name_slug = str_replace( ' ', '+', $full_name_slug );
$full_name_slug = str_replace( '-', '+', $full_name_slug );
// if name has period
if ( strpos( $last_name, '.' ) > -1 && strpos( $full_name, '.' ) > -1 ) {
$difficulties++;
}
$full_name_slug = str_replace( '.', '+', $full_name_slug );
$full_name_slug = str_replace( '++', '+', $full_name_slug );
$user_in_url = $full_name_slug;
break;
}
return $user_in_url;
}
/**
* Get action url for admin use
*
* @param $action
* @param $subaction
*
* @deprecated 2.6.9
*
* @return mixed|string|void
*/
public function admin_act_url( $action, $subaction ) {
_deprecated_function( __METHOD__, '2.6.9' );
$url = add_query_arg(
array(
'um_adm_action' => $action,
'sub' => $subaction,
'user_id' => um_user( 'ID' ),
'_wpnonce' => wp_create_nonce( $action ),
)
);
return $url;
}
/**
* SEO canonical href bugfix
*
* @deprecated since version 2.1.7
*
* @todo remove since 2.7.0
* @see function um_profile_remove_wpseo()
*/
public function um_rel_canonical_() {
_deprecated_function( __METHOD__, '2.1.7', 'um_profile_remove_wpseo()' );
global $wp_the_query;
if ( ! is_singular() )
return;
/**
* UM hook
*
* @type filter
* @title um_allow_canonical__filter
* @description Allow canonical
* @input_vars
* [{"var":"$allow_canonical","type":"bool","desc":"Allow?"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_allow_canonical__filter', 'function_name', 10, 1 ); ?>
* @example
* <?php
* add_filter( 'um_allow_canonical__filter', 'my_allow_canonical', 10, 1 );
* function my_allow_canonical( $allow_canonical ) {
* // your code here
* return $allow_canonical;
* }
* ?>
*/
$enable_canonical = apply_filters( "um_allow_canonical__filter", true );
if( ! $enable_canonical )
return;
if ( !$id = $wp_the_query->get_queried_object_id() )
return;
if ( UM()->config()->permalinks['user'] == $id ) {
$link = esc_url( $this->get_current_url() );
echo "<link rel='canonical' href='$link' />\n";
return;
}
$link = get_permalink( $id );
if ( $page = get_query_var( 'cpage' ) ) {
$link = get_comments_pagenum_link( $page );
echo "<link rel='canonical' href='$link' />\n";
}
}
}
}