| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Transform URL shortcodes |
| 5 |
*/ |
| 6 |
class Red_Url_Transform { |
| 7 |
/** |
| 8 |
* Replace special tags in the target URL. |
| 9 |
* |
| 10 |
* From the distant Redirection past. Undecided whether to keep |
| 11 |
* |
| 12 |
* @param String $url Target URL. |
| 13 |
* @return String |
| 14 |
*/ |
| 15 |
public function transform( $url ) { |
| 16 |
// Deprecated number post ID |
| 17 |
if ( is_numeric( $url ) ) { |
| 18 |
$permalink = get_permalink( intval( $url, 10 ) ); |
| 19 |
|
| 20 |
if ( $permalink ) { |
| 21 |
return $permalink; |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
global $shortcode_tags; |
| 26 |
|
| 27 |
$shortcode_copy = array_merge( [], $shortcode_tags ); |
| 28 |
|
| 29 |
remove_all_shortcodes(); |
| 30 |
|
| 31 |
$shortcodes = [ |
| 32 |
'userid', |
| 33 |
'userlogin', |
| 34 |
'unixtime', // Also replaces $dec$ |
| 35 |
|
| 36 |
// These require content |
| 37 |
'md5', |
| 38 |
'upper', |
| 39 |
'lower', |
| 40 |
'dashes', |
| 41 |
'underscores', |
| 42 |
]; |
| 43 |
|
| 44 |
foreach ( $shortcodes as $code ) { |
| 45 |
add_shortcode( $code, [ $this, 'do_shortcode' ] ); |
| 46 |
} |
| 47 |
|
| 48 |
// Support deprecated tags |
| 49 |
$url = $this->transform_deprecated( $url ); |
| 50 |
$url = do_shortcode( $url ); |
| 51 |
|
| 52 |
// Restore shortcodes |
| 53 |
// phpcs:ignore |
| 54 |
$shortcode_tags = array_merge( [], $shortcode_copy ); |
| 55 |
|
| 56 |
return $url; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Peform a shortcode |
| 61 |
* |
| 62 |
* @param array $attrs Shortcode attributes. |
| 63 |
* @param string $content Shortcode content. |
| 64 |
* @param string $tag Shortcode tag. |
| 65 |
* @return string |
| 66 |
*/ |
| 67 |
public function do_shortcode( $attrs, $content, $tag ) { |
| 68 |
$user = wp_get_current_user(); |
| 69 |
|
| 70 |
switch ( $tag ) { |
| 71 |
case 'userid': |
| 72 |
return (string) ( $user->ID || 0 ); |
| 73 |
|
| 74 |
case 'userlogin': |
| 75 |
return $user->ID ? $user->user_login : ''; |
| 76 |
|
| 77 |
case 'unixtime': |
| 78 |
return (string) time(); |
| 79 |
|
| 80 |
case 'md5': |
| 81 |
return md5( $content ); |
| 82 |
|
| 83 |
case 'upper': |
| 84 |
return strtoupper( $content ); |
| 85 |
|
| 86 |
case 'lower': |
| 87 |
return strtolower( $content ); |
| 88 |
|
| 89 |
case 'dashes': |
| 90 |
return str_replace( [ '_', ' ' ], '-', $content ); |
| 91 |
|
| 92 |
case 'underscores': |
| 93 |
return str_replace( [ '-', ' ' ], '_', $content ); |
| 94 |
} |
| 95 |
|
| 96 |
return apply_filters( 'redirection_url_transform', '', $tag, $attrs, $content ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Convert deprecated inline tags to shortcodes. |
| 101 |
* |
| 102 |
* @param string $url URL. |
| 103 |
* @return string |
| 104 |
*/ |
| 105 |
private function transform_deprecated( $url ) { |
| 106 |
$url = str_replace( '%userid%', '[userid]', $url ); |
| 107 |
$url = str_replace( '%userlogin%', '[userlogin]', $url ); |
| 108 |
$url = str_replace( '%userurl%', '[userurl]', $url ); |
| 109 |
|
| 110 |
return $url; |
| 111 |
} |
| 112 |
} |
| 113 |
|