Plugin Directory

Changeset 3250822


Ignore:
Timestamp:
03/05/2025 06:15:02 AM (9 months ago)
Author:
rehmatworks
Message:

Minor improvements and compatibility check with latest WP version

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wp-ssl-redirect/trunk/wp-ssl-redirect.php

    r2816977 r3250822  
    11<?php
    2  
    32/**
    4 * Plugin Name: WP SSL Redirect
    5 * Description: A very tiny plugin to force SSL on WordPress websites (via 301 redirects for SEO purpose).
    6 * Version: 1.6
    7 * Author: Rehmat Alam
    8 * Author URI: https://supportivehands.net/
    9 * License: GPL2
    10 */
     3 * Plugin Name: WP SSL Redirect
     4 * Description: A very tiny plugin to force SSL on WordPress websites (via 301 redirects for SEO).
     5 * Version: 1.7
     6 * Author: Rehmat Alam
     7 * Author URI: https://supportivehands.net/
     8 * License: GPL2
     9 */
    1110
    12 defined( 'ABSPATH' ) or die(); // Prevents direct access to plugin dir
     11defined( 'ABSPATH' ) or exit; // Prevent direct access
    1312
    14 add_action('admin_menu', function() {
    15     add_options_page( 'WP SSL Redirect Settings', 'WP SSL Redirect', 'manage_options', 'wp-ssl-redirect', 'wp_ssl_redirect' );
     13// Add settings page
     14add_action( 'admin_menu', function() {
     15    add_options_page(
     16        'WP SSL Redirect Settings',
     17        'WP SSL Redirect',
     18        'manage_options',
     19        'wp-ssl-redirect',
     20        'wp_ssl_redirect_options_page'
     21    );
    1622});
    17  
     23
     24// Register setting with sanitization
    1825add_action( 'admin_init', function() {
    19     register_setting( 'wp-ssl-redirect-settings', 'wp_ssl_redirect_protocol' );
     26    register_setting(
     27        'wp-ssl-redirect-settings',
     28        'wp_ssl_redirect_protocol',
     29        [ 'sanitize_callback' => 'sanitize_text_field' ]
     30    );
    2031});
    21  
    22  
    23 function wp_ssl_redirect() {
    24   ?>
     32
     33// Admin page callback
     34function wp_ssl_redirect_options_page() {
     35    // Check if function is available
     36    if ( ! function_exists( 'is_plugin_active' ) ) {
     37        require_once ABSPATH . 'wp-admin/includes/plugin.php';
     38    }
     39    ?>
    2540    <div class="wrap">
    26       <h3>WP SSL Redirect Options</h3>
    27       <hr>
    28       <?php
    29         if(is_plugin_active('all-in-one-seo-pack/all_in_one_seo_pack.php')) {?>
    30           <div class="notice notice-error">
    31             <p>All In One SEO Pack is known to create issues with WP SSL Redirect so WP SSL Redirect's features will not work unless you will deactivate <strong>All In One SEO Pack</strong> first.</p>
    32         </div>
    33       <?php }?>
    34       <form action="options.php" method="post">
    35  
    36         <?php
    37           settings_fields( 'wp-ssl-redirect-settings' );
    38           do_settings_sections( 'wp-ssl-redirect-settings' );
    39         ?>
    40         <table>
    41  
    42             <tr>
    43                 <th>Preferred Domain</th>
    44                 <td>
    45  
    46                     <select name="wp_ssl_redirect_protocol">
    47                         <option value="auto-detect" <?php echo esc_attr( get_option('wp_ssl_redirect_protocol') ) == 'auto-detect' ? 'selected="selected"' : ''; ?>>Use-default (Uses site url)</option>
    48                         <option value="www" <?php echo esc_attr( get_option('wp_ssl_redirect_protocol') ) == 'www' ? 'selected="selected"' : ''; ?>>Force www version</option>
    49                         <option value="non-www" <?php echo esc_attr( get_option('wp_ssl_redirect_protocol') ) == 'non-www' ? 'selected="selected"' : ''; ?>>Force non-www version</option>
    50                     </select>
    51  
    52                 </td>
    53             </tr>
    54  
    55             <tr>
    56                 <td><?php submit_button(); ?></td>
    57             </tr>
    58  
    59         </table>
    60  
    61       </form>
     41        <h3>WP SSL Redirect Options</h3>
     42        <hr>
     43        <?php if ( is_plugin_active( 'all-in-one-seo-pack/all_in_one_seo_pack.php' ) ) : ?>
     44            <div class="notice notice-error">
     45                <p>
     46                    All In One SEO Pack conflicts with WP SSL Redirect. Deactivate
     47                    <strong>All In One SEO Pack</strong> for WP SSL Redirect to work properly.
     48                </p>
     49            </div>
     50        <?php endif; ?>
     51
     52        <form action="options.php" method="post">
     53            <?php
     54            settings_fields( 'wp-ssl-redirect-settings' );
     55            do_settings_sections( 'wp-ssl-redirect-settings' );
     56            ?>
     57            <table class="form-table">
     58                <tr>
     59                    <th scope="row">Preferred Domain</th>
     60                    <td>
     61                        <select name="wp_ssl_redirect_protocol">
     62                            <?php $current = get_option( 'wp_ssl_redirect_protocol', 'auto-detect' ); ?>
     63                            <option value="auto-detect" <?php selected( $current, 'auto-detect' ); ?>>
     64                                Use-default (Uses site URL)
     65                            </option>
     66                            <option value="www" <?php selected( $current, 'www' ); ?>>
     67                                Force www version
     68                            </option>
     69                            <option value="non-www" <?php selected( $current, 'non-www' ); ?>>
     70                                Force non-www version
     71                            </option>
     72                        </select>
     73                    </td>
     74                </tr>
     75                <tr>
     76                    <td colspan="2"><?php submit_button(); ?></td>
     77                </tr>
     78            </table>
     79        </form>
    6280    </div>
    63   <?php
     81    <?php
    6482}
    6583
    66 function rw_has_www($url)
    67 {
    68   return (bool) (strpos($url, '://www') !== false);
     84/**
     85 * Check if a URL contains 'www.' after the scheme.
     86 */
     87function rw_has_www( $url ) {
     88    return ( strpos( $url, '://www.' ) !== false );
    6989}
    7090
    71 function do_the_ssl_redirect() {
    72   include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
    73   if(!is_plugin_active('all-in-one-seo-pack/all_in_one_seo_pack.php')) {
    74     $scheme = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http");
    75     $ori_url = rtrim($scheme . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]", '/');
    76     $redirectUrl = $ori_url;
    77     if(get_option('wp_ssl_redirect_protocol') == 'www') {
    78       if(!rw_has_www($ori_url)) {
    79         $redirectUrl = str_replace('://', '://www.', $ori_url);
    80       }
    81     } else if(get_option('wp_ssl_redirect_protocol') == 'non-www') {
    82       if(rw_has_www($ori_url)) {
    83         $redirectUrl = str_replace('//www.', '//', $ori_url);
    84       }
     91/**
     92 * Handle the SSL/domain redirect if All In One SEO Pack is inactive.
     93 */
     94function rw_do_ssl_redirect() {
     95    // Ensure this function is available
     96    if ( ! function_exists( 'is_plugin_active' ) ) {
     97        require_once ABSPATH . 'wp-admin/includes/plugin.php';
    8598    }
    86     $redirectUrl = rtrim(str_replace('http://', 'https://', $redirectUrl), '/');
    87     if($ori_url !== $redirectUrl)
    88     {
    89       wp_redirect($redirectUrl, 301);
    90       exit;
     99
     100    if ( is_admin() ) {
     101        return; // Don't redirect in admin area
    91102    }
    92   }
     103
     104    // Skip if "All In One SEO Pack" is active
     105    if ( is_plugin_active( 'all-in-one-seo-pack/all_in_one_seo_pack.php' ) ) {
     106        return;
     107    }
     108
     109    // Build original URL
     110    $host = esc_url_raw( $_SERVER['HTTP_HOST'] ?? '' );
     111    $uri  = esc_url_raw( $_SERVER['REQUEST_URI'] ?? '' );
     112    $scheme = is_ssl() ? 'https' : 'http';
     113
     114    $original_url = rtrim( "{$scheme}://{$host}{$uri}", '/' );
     115    $redirect_url = $original_url;
     116
     117    // Adjust for forced www or non-www
     118    $setting = get_option( 'wp_ssl_redirect_protocol', 'auto-detect' );
     119    if ( $setting === 'www' && ! rw_has_www( $original_url ) ) {
     120        $redirect_url = str_replace( '://', '://www.', $redirect_url );
     121    } elseif ( $setting === 'non-www' && rw_has_www( $original_url ) ) {
     122        $redirect_url = str_replace( '//www.', '//', $redirect_url );
     123    }
     124
     125    // Force HTTPS
     126    $redirect_url = str_replace( 'http://', 'https://', $redirect_url );
     127
     128    // Perform the redirect if changed
     129    if ( $redirect_url !== $original_url ) {
     130        // Double-check that final URL is valid
     131        if ( wp_http_validate_url( $redirect_url ) ) {
     132            wp_redirect( $redirect_url, 301 );
     133            exit;
     134        }
     135    }
    93136}
    94 
    95 add_action('send_headers', 'do_the_ssl_redirect');
     137add_action( 'send_headers', 'rw_do_ssl_redirect' );
Note: See TracChangeset for help on using the changeset viewer.