Plugin Directory

source: eelv-redirection/trunk/eelv_redirection.php

Last change on this file was 3415498, checked in by bastho, 4 months ago

Bump to v1.6.1

File size: 8.8 KB
Line 
1<?php
2/*
3Plugin Name: Simple Redirection
4Description: Simply redirect all pages to a specified URL
5Plugin URI: https://wordpress.org/plugins/eelv-redirection/
6Version: 1.6.1
7Author: N.O.U.S. Open Useful and Simple
8Author URI: https://apps.avecnous.eu/?mtm_campaign=wp-plugin&mtm_kwd=eelv-redirection&mtm_medium=wp-repo&mtm_source=author
9License: GPLv2
10Text Domain: eelv-redirection
11*/
12namespace EELV_REDIRECTION;
13
14\add_action('init', 'EELV_REDIRECTION\mk_redirect');
15\add_action('wp_head', 'EELV_REDIRECTION\mk_redirecthtml'); // We never know...
16\add_action('admin_menu', 'EELV_REDIRECTION\add_submenu');
17\add_action('admin_bar_menu', 'EELV_REDIRECTION\adminbar', 100);
18\add_filter('plugin_action_links_eelv-redirection/eelv_redirection.php', 'EELV_REDIRECTION\settings_link' );
19\load_plugin_textdomain( 'eelv-redirection', false, 'eelv-redirection/languages' );
20
21function add_submenu() {
22    //add_menu_page('Redirection','redirection', 'manage_options', 'eelv_option_redirect', 'eelv_option_redirect' );
23    \add_submenu_page('options-general.php', __('Redirection', 'eelv-redirection' ), __('Redirection', 'eelv-redirection' ), 'manage_options', 'eelv_redirection', 'EELV_REDIRECTION\settings_page');
24}
25
26function where_redirect(){
27    $dir=basename(realpath('.'));
28    if(defined( 'WP_CLI' ) && WP_CLI){
29        return false;
30    }
31    if(defined('REDIRECT_ALLOW_USER_AGENT') && isset($_SERVER['HTTP_USER_AGENT']) && strstr($_SERVER['HTTP_USER_AGENT'], REDIRECT_ALLOW_USER_AGENT)){
32        @header("X-Redirect-Debug: Allowed by user-agent pattern", true);
33        return false;
34    }
35    return ($dir!='wp-admin' && $dir!='network' && isset($_SERVER['SCRIPT_NAME']) && !preg_match('#^/wp-(.*).php#', $_SERVER['SCRIPT_NAME']));
36}
37
38function get_redirect_url(){
39    $redirect_url = get_option('eelv_url_redirect');
40    if(strstr($redirect_url, '%query_string%') && isset($_SERVER['QUERY_STRING'])){
41        $redirect_url =str_replace('%query_string%', sanitize_text_field($_SERVER['QUERY_STRING']), $redirect_url);
42    }
43    if(strstr($redirect_url, '%request_uri%') && isset($_SERVER['REQUEST_URI'])){
44        $redirect_url =str_replace('%request_uri%', sanitize_text_field($_SERVER['REQUEST_URI']), $redirect_url);
45    }
46    return $redirect_url;
47}
48
49function mk_redirect() {
50    if(where_redirect()){
51      if(get_option('eelv_url_redirect')!='') {
52        $eelv_when_redirect = get_option( 'eelv_when_redirect' ,'-1');
53        if($eelv_when_redirect==0 || ($eelv_when_redirect==1 && !is_user_logged_in())){
54            $eelv_code_redirect=get_option("eelv_code_redirect");
55            if($eelv_code_redirect=='301') header("HTTP/1.1 301 Moved Permanently");
56            header("location: ".get_redirect_url());
57            exit();
58        }
59     }
60  }
61}
62function mk_redirecthtml() {
63    if(where_redirect()){ // only on front
64    if(get_option('eelv_url_redirect')!='') {
65    $eelv_when_redirect = get_option( 'eelv_when_redirect','-1');
66    if($eelv_when_redirect==0 || ($eelv_when_redirect==1 && !is_user_logged_in())){ ?>
67    <!-- redirection -->
68 <?php if(get_option("eelv_code_redirect")=='301') : ?>
69    <meta http-equiv="refresh" content="0; url=<?php echo esc_url(get_redirect_url()); ?>"/>
70 <?php else: ?>
71    <link rel="canonical" href="<?php echo esc_url(get_redirect_url()); ?>" />
72<?php endif; ?>
73    <script language="JavaScript">
74     document.location.href="<?php echo esc_url(get_redirect_url()); ?>"
75    </script>
76    <?php    }
77    }
78  }
79}
80
81function settings_link( $links ) {
82    $settings_link = '<a href="options-general.php?page=eelv_redirection">' . __( 'Settings', 'eelv-redirection' ) . '</a>';
83    // place it before other links
84    array_unshift( $links, $settings_link );
85    return $links;
86}
87
88function settings_page() {
89    // See if the user has posted us some information
90    // If they did, this hidden field will be set to 'Y'
91    if( isset($_POST[ 'eelv_url_redirect' ])) {
92        if (wp_verify_nonce( sanitize_text_field($_REQUEST['_wpnonce']), 'eelv_redirection_settings')){ 
93            update_option( 'eelv_url_redirect', esc_url_raw(filter_input(INPUT_POST, 'eelv_url_redirect', FILTER_SANITIZE_URL)));
94            update_option( "eelv_code_redirect", sanitize_text_field($_POST[ 'eelv_code_redirect' ] ?? ''));
95            update_option( "eelv_when_redirect", sanitize_text_field($_POST[ 'eelv_when_redirect' ] ?? ''));
96            ?>
97            <div class="updated"><p><strong><?php esc_html_e('Option saved','eelv-redirection'); ?></strong></p></div>
98            <?php
99        }else{
100            ?>   
101            <div class="error"><p><strong><?php esc_html_e('Warning there has been a hacking attempt','eelv-redirection'); ?></strong></p></div>
102            <?php
103        }
104    }
105   
106    $eelv_url_redirect = get_option( 'eelv_url_redirect' );
107    $eelv_code_redirect = get_option( "eelv_code_redirect" );
108    $eelv_when_redirect = get_option( "eelv_when_redirect",'-1');
109    ?>
110<div class="wrap">
111    <h2>
112        <?php esc_html_e('Simple Redirection', 'eelv-redirection' ); ?>
113    </h2>
114
115    <form name="form1" method="post" action="#">
116   <?php wp_nonce_field( 'eelv_redirection_settings' ); ?>
117
118    <p>
119            <label for="eelv_when_redirect"><?php esc_html_e('When:','eelv-redirection')?></label>
120            <select name="eelv_when_redirect" id="eelv_when_redirect">
121                <option value="-1" <?php selected($eelv_when_redirect, -1, true); ?>><?php echo esc_attr('Never (deactivated)','eelv-redirection')?></option>
122                <option value="0" <?php selected($eelv_when_redirect, 0, true); ?>><?php echo esc_attr('Always','eelv-redirection')?></option>
123                <option value="1" <?php selected($eelv_when_redirect, 1, true); ?>><?php echo esc_attr('Only non-logged-in users','eelv-redirection')?></option>
124            </select>
125        </p>
126        <p>
127            <label id="eelv_code_redirect"><?php esc_html_e('How:','eelv-redirection')?></label>
128            <select name="eelv_code_redirect" id="eelv_code_redirect">
129                        <option value="302" <?php selected($eelv_code_redirect, 302, true); ?>><?php echo esc_attr('302 : Moved Temporarily','eelv-redirection')?></option>
130                <option value="301" <?php selected($eelv_code_redirect, 301, true); ?>><?php echo esc_attr('301 : Moved Permanently','eelv-redirection')?></option>
131            </select>
132        </p>
133        <p>
134            <label id='eelv_url_redirect'><?php esc_html_e('Where:','eelv-redirection')?></label>
135            <input type="text" name='eelv_url_redirect' id='eelv_url_redirect' value="<?php echo esc_attr($eelv_url_redirect) ?>" size="50" placeholder="https://">
136            <span class="text-description">
137                <?php 
138                // Translators: %s will be replaced by tokens.
139                echo wp_kses_post(sprintf(__('You can use these variables in the redirect URL: %s','eelv-redirection'), '<code>%query_string%</code>, <code>%request_uri%</code>')); ?>
140            </span>
141        </p>
142        <details>
143            <summary><?php esc_html_e('Advanced settings','eelv-redirection')?></summary>
144            <?php if(defined('REDIRECT_ALLOW_USER_AGENT')): ?>
145                <p><?php esc_html_e('Allowed users agent pattern','eelv-redirection'); ?></p>
146                <p><code><?php echo esc_html(REDIRECT_ALLOW_USER_AGENT); ?></code></p>
147            <?php else: ?>
148                <p><?php esc_html_e('You can allow some user-agents to bypass redirect by adding this to your wp-config.php','eelv-redirection'); ?></p>
149                <p><code>define('REDIRECT_ALLOW_USER_AGENT', 'HTTrack');</code></p>
150                <p><?php esc_html_e('Replace HTTrack by a pattern to find in user-agents.','eelv-redirection'); ?></p>
151            <?php endif; ?>
152        </details>
153
154    <?php \submit_button(); ?>
155    </form>
156</div>
157
158<?php
159
160}
161
162function adminbar($admin_bar) {
163    if (current_user_can('manage_options')) {
164        $redirect_url = get_option('eelv_url_redirect');
165        $redirect_condition = (int) get_option( 'eelv_when_redirect' ,'-1');
166        $redirect_code = (int) get_option( 'eelv_code_redirect' ,'-1');
167        if(!empty($redirect_url) && $redirect_condition >= 0){
168            $admin_bar->add_menu(
169                array(
170                    'id' => 'eelv-redirection',
171                    'title' => '<span class="ab-icon dashicons dashicons-randomize"></span>',
172                    'href' => add_query_arg(array('page'=>'eelv_redirection'), admin_url('options-general.php')),
173                    'meta' => array(
174                        // Translators: %s is the redirect code (301, 302, etc).
175                        'title' => sprintf(_x('%s redirection active', 'redirect code', 'eelv-redirection'), $redirect_code),
176                    ),
177                )
178            );
179        }
180    }
181}
Note: See TracBrowser for help on using the repository browser.