forked from tomslominski/yourls-redirect-index
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.php
83 lines (70 loc) · 2.94 KB
/
plugin.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
<?php
/*
Plugin Name: Redirect Index
Plugin URI: https://github.com/tomslominski/yourls-redirect-index
Description: Redirect the base directory of YOURLS to a user selected link
Version: 1.0
Author: Tom Slominski
Author URI: http://tomslominski.net/
*/
// No direct calls
if( !defined( 'YOURLS_ABSPATH' ) ) die();
// Register plugin admin page and load translations
yourls_add_action( 'plugins_loaded', 'redirindex_init' );
function redirindex_init() {
yourls_register_plugin_page( 'redirindex', yourls__( 'Redirect Index', 'redirindex' ), 'redirindex_admin' );
yourls_load_custom_textdomain( 'redirindex', dirname( __FILE__ ) . '/translations' );
}
// The function that will draw the admin page
function redirindex_admin() {
if( isset( $_POST['action'] ) && $_POST['action'] == 'redirindex' ) {
redirindex_admin_save(); // If form data has been sent
} else {
redirindex_admin_display(); // If no form data has been sent
}
}
// If no form data has been sent
function redirindex_admin_display( $message = false, $message_type = false ) {
$nonce = yourls_create_nonce('redirindex');
// Bring up the current URL from the database
$current_url = yourls_get_option( 'redirindex_url' );
// Build strings
if( $message ) {
$message = "<p class=\"message $message_type\">$message</p>";
} else {
$message = '';
}
// Echo the page content
?>
<h2><?php yourls_e( 'Redirect Index', 'redirindex' ); ?></h2>
<?php echo $message; ?>
<p><?php yourls_e( 'Enter a URL to which the index page will redirect:', 'redirindex' ); ?></p>
<form method="post">
<input type="hidden" name="action" value="redirindex" />
<input type="hidden" name="nonce" value="<?php echo $nonce; ?>" />
<p><input type="url" name="redir_url" value="<?php echo $current_url; ?>" class="text" /></p>
<p><input type="submit" value="<?php echo yourls_e( 'Save', 'redirindex' ); ?>" class="button primary" /></p>
</form>
<p><?php
if( !file_exists( YOURLS_ABSPATH . '/index.php' ) ) {
yourls_e( 'Have you copied the index.php file into your YOURLS base directory?', 'redirindex' );
}
?></p>
<?php
}
// If form data has been sent
function redirindex_admin_save() {
yourls_verify_nonce( 'redirindex' );
$new_url = yourls_sanitize_url( $_POST['redir_url'] );
if( $new_url == yourls_get_option( 'redirindex_url' ) ) {
$message = sprintf( yourls__( 'The URL is the same as what you tried to change it to, so it has been kept. <a href="%1$s">Check out your redirect!</a>', 'redirindex' ), YOURLS_SITE );
redirindex_admin_display( $message, 'error' );
} elseif( yourls_update_option( 'redirindex_url', $new_url ) ) {
$message = sprintf( yourls__( 'URL saved successfully. <a href="%1$s">Check out your new redirect!</a>', 'redirindex' ), YOURLS_SITE );
redirindex_admin_display( $message, 'success' );
} else {
$message = yourls__( 'Something went wrong and the URL could not be saved. Please try again.', 'redirindex' );
redirindex_admin_display( $message, 'error' );
}
}
?>