Plugin Directory


Ignore:
Timestamp:
11/22/2019 02:39:27 AM (6 years ago)
Author:
mra13
Message:

WP 5.3 warning fix for the add_submenu_page() function call. Thanks to @vfontj for pointing this out.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • https-redirection/trunk/https-redirection.php

    r1831198 r2198540  
    66  Description: The plugin HTTPS Redirection allows an automatic redirection to the "HTTPS" version/URL of the site.
    77  Author: Tips and Tricks HQ
    8   Version: 1.9
     8  Version: 1.9.1
    99  Author URI: https://www.tipsandtricks-hq.com/
    1010  License: GPLv2 or later
     
    1313 */
    1414
    15 if (!defined('ABSPATH')) {
     15if ( ! defined( 'ABSPATH' ) ) {
    1616    exit; //Exit if accessed directly
    1717}
     
    2222function httpsrdrctn_plugin_init() {
    2323    global $httpsrdrctn_options;
    24     if (empty($httpsrdrctn_options)) {
    25         $httpsrdrctn_options = get_option('httpsrdrctn_options');
     24    if ( empty( $httpsrdrctn_options ) ) {
     25    $httpsrdrctn_options = get_option( 'httpsrdrctn_options' );
    2626    }
    2727
    2828    //Do force resource embedded using HTTPS
    29     if (isset($httpsrdrctn_options['force_resources']) && $httpsrdrctn_options['force_resources'] == '1') {
    30         //Handle the appropriate content filters to force the static resources to use HTTPS URL
    31         if (is_admin()) {
    32             add_action("admin_init", "httpsrdrctn_start_buffer");
    33         } else {
    34             add_action("init", "httpsrdrctn_start_buffer");
    35             add_action("init", "httpsrdrctn_init_time_tasks");
    36         }
    37         add_action("shutdown", "httpsrdrctn_end_buffer");
     29    if ( isset( $httpsrdrctn_options[ 'force_resources' ] ) && $httpsrdrctn_options[ 'force_resources' ] == '1' ) {
     30    //Handle the appropriate content filters to force the static resources to use HTTPS URL
     31    if ( is_admin() ) {
     32        add_action( "admin_init", "httpsrdrctn_start_buffer" );
     33    } else {
     34        add_action( "init", "httpsrdrctn_start_buffer" );
     35        add_action( "init", "httpsrdrctn_init_time_tasks" );
     36    }
     37    add_action( "shutdown", "httpsrdrctn_end_buffer" );
    3838    }
    3939}
    4040
    4141function httpsrdrctn_start_buffer() {
    42     ob_start("httpsrdrctn_the_content");
     42    ob_start( "httpsrdrctn_the_content" );
    4343}
    4444
    4545function httpsrdrctn_end_buffer() {
    46     if (ob_get_length())
    47         ob_end_flush();
     46    if ( ob_get_length() )
     47    ob_end_flush();
    4848}
    4949
     
    5454function httpsrdrctn_load_language() {
    5555    /* Internationalization */
    56     load_plugin_textdomain('https_redirection', false, dirname(plugin_basename(__FILE__)) . '/languages/');
     56    load_plugin_textdomain( 'https_redirection', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
    5757}
    5858
     
    6060    global $httpsrdrctn_plugin_info;
    6161
    62     $httpsrdrctn_plugin_info = get_plugin_data(__FILE__, false);
     62    $httpsrdrctn_plugin_info = get_plugin_data( __FILE__, false );
    6363
    6464    /* Call register settings function */
    65     if (isset($_GET['page']) && "https-redirection" == $_GET['page']) {
    66         register_httpsrdrctn_settings();
     65    if ( isset( $_GET[ 'page' ] ) && "https-redirection" == $_GET[ 'page' ] ) {
     66    register_httpsrdrctn_settings();
    6767    }
    6868}
     
    7474
    7575    $httpsrdrctn_option_defaults = array(
    76         'https' => 0,
    77         'https_domain' => 1,
    78         'https_pages_array' => array(),
    79         'force_resources' => 0,
    80         'plugin_option_version' => $httpsrdrctn_plugin_info["Version"]
     76    'https'          => 0,
     77    'https_domain'      => 1,
     78    'https_pages_array' => array(),
     79    'force_resources'    => 0,
     80    'plugin_option_version'  => $httpsrdrctn_plugin_info[ "Version" ]
    8181    );
    8282
    8383    /* Install the option defaults */
    84     if (1 == $wpmu) {
    85         if (!get_site_option('httpsrdrctn_options'))
    86             add_site_option('httpsrdrctn_options', $httpsrdrctn_option_defaults, '', 'yes');
     84    if ( 1 == $wpmu ) {
     85    if ( ! get_site_option( 'httpsrdrctn_options' ) )
     86        add_site_option( 'httpsrdrctn_options', $httpsrdrctn_option_defaults, '', 'yes' );
    8787    } else {
    88         if (!get_option('httpsrdrctn_options'))
    89             add_option('httpsrdrctn_options', $httpsrdrctn_option_defaults, '', 'yes');
     88    if ( ! get_option( 'httpsrdrctn_options' ) )
     89        add_option( 'httpsrdrctn_options', $httpsrdrctn_option_defaults, '', 'yes' );
    9090    }
    9191
    9292    /* Get options from the database */
    93     if (1 == $wpmu)
    94         $httpsrdrctn_options = get_site_option('httpsrdrctn_options');
     93    if ( 1 == $wpmu )
     94    $httpsrdrctn_options     = get_site_option( 'httpsrdrctn_options' );
    9595    else
    96         $httpsrdrctn_options = get_option('httpsrdrctn_options');
     96    $httpsrdrctn_options     = get_option( 'httpsrdrctn_options' );
    9797
    9898    /* Array merge incase this version has added new options */
    99     if (!isset($httpsrdrctn_options['plugin_option_version']) || $httpsrdrctn_options['plugin_option_version'] != $httpsrdrctn_plugin_info["Version"]) {
    100         $httpsrdrctn_options = array_merge($httpsrdrctn_option_defaults, $httpsrdrctn_options);
    101         $httpsrdrctn_options['plugin_option_version'] = $httpsrdrctn_plugin_info["Version"];
    102         update_option('httpsrdrctn_options', $httpsrdrctn_options);
    103     }
    104 }
    105 
    106 function httpsrdrctn_plugin_action_links($links, $file) {
     99    if ( ! isset( $httpsrdrctn_options[ 'plugin_option_version' ] ) || $httpsrdrctn_options[ 'plugin_option_version' ] != $httpsrdrctn_plugin_info[ "Version" ] ) {
     100    $httpsrdrctn_options                 = array_merge( $httpsrdrctn_option_defaults, $httpsrdrctn_options );
     101    $httpsrdrctn_options[ 'plugin_option_version' ]  = $httpsrdrctn_plugin_info[ "Version" ];
     102    update_option( 'httpsrdrctn_options', $httpsrdrctn_options );
     103    }
     104}
     105
     106function httpsrdrctn_plugin_action_links( $links, $file ) {
    107107    /* Static so we don't call plugin_basename on every plugin row. */
    108108    static $this_plugin;
    109     if (!$this_plugin)
    110         $this_plugin = plugin_basename(__FILE__);
    111 
    112     if ($file == $this_plugin) {
    113         $settings_link = '<a href="admin.php?page=https-redirection">' . __('Settings', 'https_redirection') . '</a>';
    114         array_unshift($links, $settings_link);
     109    if ( ! $this_plugin )
     110    $this_plugin = plugin_basename( __FILE__ );
     111
     112    if ( $file == $this_plugin ) {
     113    $settings_link = '<a href="admin.php?page=https-redirection">' . __( 'Settings', 'https_redirection' ) . '</a>';
     114    array_unshift( $links, $settings_link );
    115115    }
    116116    return $links;
    117117}
    118118
    119 function httpsrdrctn_register_plugin_links($links, $file) {
    120     $base = plugin_basename(__FILE__);
    121     if ($file == $base) {
    122         $links[] = '<a href="admin.php?page=https-redirection">' . __('Settings', 'https_redirection') . '</a>';
     119function httpsrdrctn_register_plugin_links( $links, $file ) {
     120    $base = plugin_basename( __FILE__ );
     121    if ( $file == $base ) {
     122    $links[] = '<a href="admin.php?page=https-redirection">' . __( 'Settings', 'https_redirection' ) . '</a>';
    123123    }
    124124    return $links;
     
    129129 */
    130130
    131 function httpsrdrctn_filter_content($content) {
     131function httpsrdrctn_filter_content( $content ) {
    132132    //filter buffer
    133     $home_no_www = str_replace("://www.", "://", get_option('home'));
    134     $home_yes_www = str_replace("://", "://www.", $home_no_www);
    135     $http_urls = array(
    136         str_replace("https://", "http://", $home_yes_www),
    137         str_replace("https://", "http://", $home_no_www),
    138         "src='http://",
    139         'src="http://',
     133    $home_no_www     = str_replace( "://www.", "://", get_option( 'home' ) );
     134    $home_yes_www    = str_replace( "://", "://www.", $home_no_www );
     135    $http_urls  = array(
     136    str_replace( "https://", "http://", $home_yes_www ),
     137    str_replace( "https://", "http://", $home_no_www ),
     138    "src='http://",
     139    'src="http://',
    140140    );
    141     $ssl_array = str_replace("http://", "https://", $http_urls);
     141    $ssl_array   = str_replace( "http://", "https://", $http_urls );
    142142    //now replace these links
    143     $str = str_replace($http_urls, $ssl_array, $content);
     143    $str         = str_replace( $http_urls, $ssl_array, $content );
    144144
    145145    //replace all http links except hyperlinks
     
    147147
    148148    $pattern = array(
    149         '/url\([\'"]?\K(http:\/\/)(?=[^)]+)/i',
    150         '/<link .*?href=[\'"]\K(http:\/\/)(?=[^\'"]+)/i',
    151         '/<meta property="og:image" .*?content=[\'"]\K(http:\/\/)(?=[^\'"]+)/i',
    152         '/<form [^>]*?action=[\'"]\K(http:\/\/)(?=[^\'"]+)/i',
     149    '/url\([\'"]?\K(http:\/\/)(?=[^)]+)/i',
     150    '/<link .*?href=[\'"]\K(http:\/\/)(?=[^\'"]+)/i',
     151    '/<meta property="og:image" .*?content=[\'"]\K(http:\/\/)(?=[^\'"]+)/i',
     152    '/<form [^>]*?action=[\'"]\K(http:\/\/)(?=[^\'"]+)/i',
    153153    );
    154     $str = preg_replace($pattern, 'https://', $str);
     154    $str     = preg_replace( $pattern, 'https://', $str );
    155155    return $str;
    156156}
    157157
    158 function httpsrdrctn_the_content($content) {
     158function httpsrdrctn_the_content( $content ) {
    159159    global $httpsrdrctn_options;
    160     if (empty($httpsrdrctn_options)) {
    161         $httpsrdrctn_options = get_option('httpsrdrctn_options');
    162     }
    163 
    164     $current_page = sanitize_post($GLOBALS['wp_the_query']->get_queried_object());
     160    if ( empty( $httpsrdrctn_options ) ) {
     161    $httpsrdrctn_options = get_option( 'httpsrdrctn_options' );
     162    }
     163
     164    $current_page    = sanitize_post( $GLOBALS[ 'wp_the_query' ]->get_queried_object() );
    165165    // Get the page slug
    166     $slug = str_replace(home_url() . '/', '', get_permalink($current_page));
    167     $slug = rtrim($slug, "/"); //remove trailing slash if it's there
    168 
    169     if ($httpsrdrctn_options['force_resources'] == '1' && $httpsrdrctn_options['https'] == 1) {
    170         if ($httpsrdrctn_options['https_domain'] == 1) {
    171             $content = httpsrdrctn_filter_content($content);
    172         } else if (!empty($httpsrdrctn_options['https_pages_array'])) {
    173             $pages_str = '';
    174             $on_https_page = false;
    175             foreach ($httpsrdrctn_options['https_pages_array'] as $https_page) {
    176                 $pages_str .= preg_quote($https_page, '/') . '[\/|][\'"]|'; //let's add page to the preg expression string in case we'd need it later
    177                 if ($https_page == $slug) { //if we are on the page that is in the array, let's set the var to true
    178                     $on_https_page = true;
    179                 } else { //if not - let's replace all links to that page only to https
    180                     $http_domain = home_url();
    181                     $https_domain = str_replace('http://', 'https://', home_url());
    182                     $content = str_replace($http_domain . '/' . $https_page, $https_domain . '/' . $https_page, $content);
    183                 }
    184             }
    185             if ($on_https_page) { //we are on one of the https pages
    186                 $pages_str = substr($pages_str, 0, strlen($pages_str) - 1); //remove last '|'
    187                 $content = httpsrdrctn_filter_content($content); //let's change everything to https first
    188                 $http_domain = str_replace('https://', 'http://', home_url());
    189                 $https_domain = str_replace('http://', 'https://', home_url());
    190                 //now let's change all inner links to http, excluding those that user sets to be https in plugin settings
    191                 $content = preg_replace('/<a .*?href=[\'"]\K' . preg_quote($https_domain, '/') . '\/((?!' . $pages_str . ').)(?=[^\'"]+)/i', $http_domain . '/$1', $content);
    192                 $content = preg_replace('/' . preg_quote($https_domain, '/') . '([\'"])/i', $http_domain . '$1', $content);
    193             }
    194         }
     166    $slug        = str_replace( home_url() . '/', '', get_permalink( $current_page ) );
     167    $slug        = rtrim( $slug, "/" ); //remove trailing slash if it's there
     168
     169    if ( $httpsrdrctn_options[ 'force_resources' ] == '1' && $httpsrdrctn_options[ 'https' ] == 1 ) {
     170    if ( $httpsrdrctn_options[ 'https_domain' ] == 1 ) {
     171        $content = httpsrdrctn_filter_content( $content );
     172    } else if ( ! empty( $httpsrdrctn_options[ 'https_pages_array' ] ) ) {
     173        $pages_str  = '';
     174        $on_https_page  = false;
     175        foreach ( $httpsrdrctn_options[ 'https_pages_array' ] as $https_page ) {
     176        $pages_str .= preg_quote( $https_page, '/' ) . '[\/|][\'"]|'; //let's add page to the preg expression string in case we'd need it later
     177        if ( $https_page == $slug ) { //if we are on the page that is in the array, let's set the var to true
     178            $on_https_page = true;
     179        } else { //if not - let's replace all links to that page only to https
     180            $http_domain    = home_url();
     181            $https_domain    = str_replace( 'http://', 'https://', home_url() );
     182            $content     = str_replace( $http_domain . '/' . $https_page, $https_domain . '/' . $https_page, $content );
     183        }
     184        }
     185        if ( $on_https_page ) { //we are on one of the https pages
     186        $pages_str   = substr( $pages_str, 0, strlen( $pages_str ) - 1 ); //remove last '|'
     187        $content     = httpsrdrctn_filter_content( $content ); //let's change everything to https first
     188        $http_domain     = str_replace( 'https://', 'http://', home_url() );
     189        $https_domain    = str_replace( 'http://', 'https://', home_url() );
     190        //now let's change all inner links to http, excluding those that user sets to be https in plugin settings
     191        $content     = preg_replace( '/<a .*?href=[\'"]\K' . preg_quote( $https_domain, '/' ) . '\/((?!' . $pages_str . ').)(?=[^\'"]+)/i', $http_domain . '/$1', $content );
     192        $content     = preg_replace( '/' . preg_quote( $https_domain, '/' ) . '([\'"])/i', $http_domain . '$1', $content );
     193        }
     194    }
    195195    }
    196196    return $content;
    197197}
    198198
    199 if (!function_exists('httpsrdrctn_admin_head')) {
     199if ( ! function_exists( 'httpsrdrctn_admin_head' ) ) {
    200200
    201201    function httpsrdrctn_admin_head() {
    202         if (isset($_REQUEST['page']) && 'https-redirection' == $_REQUEST['page']) {
    203             wp_enqueue_style('httpsrdrctn_stylesheet', plugins_url('css/style.css', __FILE__));
    204             wp_enqueue_script('httpsrdrctn_script', plugins_url('js/script.js', __FILE__));
    205         }
     202    if ( isset( $_REQUEST[ 'page' ] ) && 'https-redirection' == $_REQUEST[ 'page' ] ) {
     203        wp_enqueue_style( 'httpsrdrctn_stylesheet', plugins_url( 'css/style.css', __FILE__ ) );
     204        wp_enqueue_script( 'httpsrdrctn_script', plugins_url( 'js/script.js', __FILE__ ) );
     205    }
    206206    }
    207207
     
    209209
    210210/* Function for delete delete options */
    211 if (!function_exists('httpsrdrctn_delete_options')) {
     211if ( ! function_exists( 'httpsrdrctn_delete_options' ) ) {
    212212
    213213    function httpsrdrctn_delete_options() {
    214         delete_option('httpsrdrctn_options');
    215         delete_site_option('httpsrdrctn_options');
     214    delete_option( 'httpsrdrctn_options' );
     215    delete_site_option( 'httpsrdrctn_options' );
    216216    }
    217217
     
    219219
    220220function add_httpsrdrctn_admin_menu() {
    221     add_submenu_page('options-general.php', 'HTTPS Redirection', 'HTTPS Redirection', 'manage_options', 'https-redirection', 'httpsrdrctn_settings_page', plugins_url("images/px.png", __FILE__), 1001);
    222 }
    223 
    224 add_action('admin_menu', 'add_httpsrdrctn_admin_menu');
    225 add_action('admin_init', 'httpsrdrctn_plugin_admin_init');
    226 add_action('admin_enqueue_scripts', 'httpsrdrctn_admin_head');
     221    add_submenu_page( 'options-general.php', 'HTTPS Redirection', 'HTTPS Redirection', 'manage_options', 'https-redirection', 'httpsrdrctn_settings_page' );
     222}
     223
     224add_action( 'admin_menu', 'add_httpsrdrctn_admin_menu' );
     225add_action( 'admin_init', 'httpsrdrctn_plugin_admin_init' );
     226add_action( 'admin_enqueue_scripts', 'httpsrdrctn_admin_head' );
    227227
    228228/* Adds "Settings" link to the plugin action page */
    229 add_filter('plugin_action_links', 'httpsrdrctn_plugin_action_links', 10, 2);
     229add_filter( 'plugin_action_links', 'httpsrdrctn_plugin_action_links', 10, 2 );
    230230/* Additional links on the plugin page */
    231 add_filter('plugin_row_meta', 'httpsrdrctn_register_plugin_links', 10, 2);
     231add_filter( 'plugin_row_meta', 'httpsrdrctn_register_plugin_links', 10, 2 );
    232232//add_filter('mod_rewrite_rules', 'httpsrdrctn_mod_rewrite_rules');//TODO 5
    233233
    234 register_uninstall_hook(__FILE__, 'httpsrdrctn_delete_options');
     234register_uninstall_hook( __FILE__, 'httpsrdrctn_delete_options' );
    235235
    236236httpsrdrctn_plugin_init();
Note: See TracChangeset for help on using the changeset viewer.