| 1 |
<?php |
| 2 |
|
| 3 |
include_once dirname( __FILE__ ).'/modules/wordpress.php'; |
| 4 |
|
| 5 |
class Redirection { |
| 6 |
private static $instance = null; |
| 7 |
private $module; |
| 8 |
|
| 9 |
static function init() { |
| 10 |
if ( is_null( self::$instance ) ) { |
| 11 |
self::$instance = new Redirection(); |
| 12 |
} |
| 13 |
|
| 14 |
return self::$instance; |
| 15 |
} |
| 16 |
|
| 17 |
public function __construct() { |
| 18 |
$this->module = Red_Module::get( WordPress_Module::MODULE_ID ); |
| 19 |
$this->module->start(); |
| 20 |
|
| 21 |
add_action( Red_Flusher::DELETE_HOOK, array( $this, 'clean_redirection_logs' ) ); |
| 22 |
add_action( 'redirection_url_target', array( $this, 'replace_special_tags' ) ); |
| 23 |
} |
| 24 |
|
| 25 |
public function clean_redirection_logs() { |
| 26 |
$flusher = new Red_Flusher(); |
| 27 |
$flusher->flush(); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* From the distant Redirection past. Undecided whether to keep |
| 32 |
*/ |
| 33 |
public function replace_special_tags( $url ) { |
| 34 |
if ( is_numeric( $url ) ) { |
| 35 |
$url = get_permalink( $url ); |
| 36 |
} else { |
| 37 |
$user = wp_get_current_user(); |
| 38 |
|
| 39 |
if ( ! empty( $user ) ) { |
| 40 |
$url = str_replace( '%userid%', $user->ID, $url ); |
| 41 |
$url = str_replace( '%userlogin%', isset( $user->user_login ) ? $user->user_login : '', $url ); |
| 42 |
$url = str_replace( '%userurl%', isset( $user->user_url ) ? $user->user_url : '', $url ); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
return $url; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
add_action( 'plugins_loaded', array( 'Redirection', 'init' ) ); |
| 51 |
|