forked from timcrockford/302-instead
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.php
99 lines (80 loc) · 4.01 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/*
Plugin Name: 302 Instead + 301 for YOURLS URLs
Plugin URI: https://github.com/timcrockford/302-instead
Description: Send a 302 (temporary) redirects that do not redirect to other short URLs and a 301 for YOURLS URLs
Version: 1.2
Author: BrettR / Tim Crockford
Author URI: http://codearoundcorners.com/
*/
if( !defined( 'YOURLS_ABSPATH' ) ) die();
yourls_add_action('pre_redirect', 'temp_instead_function');
yourls_add_action( 'plugins_loaded', 'temp_instead_admin_page_add' );
// This function will check the URL and the HTTP status code of the passed
// in arguments. If the URL happens to be an existing short URL on the same
// YOURLS installation, it does nothing. Otherwise it will send a 302
// redirect. Useful when you want to change the short URLs that end users
// might be using but you can't change.
function temp_instead_function($args) {
$url = $args[0];
$code = $args[1];
$match = strpos($url, yourls_site_url(false));
$mode = intval(yourls_get_option( 'temp_instead_mode', 1 ));
// We check here if the url contains the YOURLS installation address,
// and if it doesn't we'll return a 302 redirect if it isn't getting
// one already.
if ( $code != 302 && ($mode == 1 || ($match === false && $mode == 3))) {
yourls_redirect($url, 302);
}
// We check here if the url contains the YOURLS installation address,
// and if it does we'll return a 301 redirect if it isn't getting
// one already.
if ( $code != 301 && ($mode == 2 || ($match !== false && $mode == 3))) {
yourls_redirect($url, 301);
}
}
// Register our plugin admin page
function temp_instead_admin_page_add() {
yourls_register_plugin_page( 'temp_instead', 'Redirect Rules', 'temp_instead_admin_page_do' );
}
// Display admin page
function temp_instead_admin_page_do() {
if( isset( $_POST['temp_instead_mode'] ) ) {
yourls_verify_nonce( 'temp_instead' );
temp_instead_admin_page_update();
}
$mode = intval(yourls_get_option( 'temp_instead_mode', 1 ));
$nonce = yourls_create_nonce( 'temp_instead' );
// If the option hasn't been added previously, we add the default value of everything using
// 302 redirects.
echo '<h2>302-Redirect Redirection Rules</h2>';
echo '<p>This plugin allows you to configure how the 302-redirect plugin operates.</p>';
echo '<form method="post">';
echo '<input type="hidden" name="nonce" value="' . $nonce . '" />';
echo '<label for="temp_instead_mode">Select Redirect Mode:</label>';
echo '<select id="temp_instead_mode" name="temp_instead_mode">';
$opt1 = ( $mode == 1 ? ' selected' : '');
$opt2 = ( $mode == 2 ? ' selected' : '');
$opt3 = ( $mode == 3 ? ' selected' : '');
echo '<option value=1' . $opt1 . '>Redirect all using 302 temporary redirect</option>';
echo '<option value=2' . $opt2 . '>Redirect all using 301 permanent redirect</option>';
echo '<option value=3' . $opt3 . '>Redirect full URLs using 302 and short URLs using 301</option>';
echo '<p><input type="submit" value="Update Redirect Mode" /></p>';
echo '</select>';
echo '</form>';
}
// Update option in database
function temp_instead_admin_page_update() {
$mode = $_POST['temp_instead_mode'];
if( $mode ) {
$mode = intval($mode);
if ( yourls_get_option( 'temp_instead_mode' ) !== false ) {
echo '<b>Redirect mode was updated successfully.</b>';
yourls_update_option( 'temp_instead_mode', $mode );
} else {
echo '<b>Redirect mode was stored successfully.</b>';
yourls_add_option( 'temp_instead_mode', $mode );
}
}
}
?>