https-redirection
Last commit date
css
12 years ago
images
12 years ago
js
6 years ago
languages
12 years ago
https-redirection-settings.php
6 years ago
https-redirection.php
3 years ago
https-rules-helper.php
3 years ago
https-utillity-functions.php
6 years ago
readme.txt
1 year ago
screenshot-1.png
12 years ago
https-redirection.php
237 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | Plugin Name: Easy HTTPS (SSL) Redirection |
| 5 | Plugin URI: https://www.tipsandtricks-hq.com/wordpress-easy-https-redirection-plugin |
| 6 | Description: The plugin HTTPS Redirection allows an automatic redirection to the "HTTPS" version/URL of the site. |
| 7 | Author: Tips and Tricks HQ |
| 8 | Version: 1.9.2 |
| 9 | Author URI: https://www.tipsandtricks-hq.com/ |
| 10 | License: GPLv2 or later |
| 11 | Text Domain: https-redirection |
| 12 | Domain Path: /languages/ |
| 13 | */ |
| 14 | |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit; //Exit if accessed directly |
| 17 | } |
| 18 | |
| 19 | include_once('https-rules-helper.php'); |
| 20 | include_once('https-redirection-settings.php'); |
| 21 | |
| 22 | function httpsrdrctn_plugin_init() { |
| 23 | global $httpsrdrctn_options; |
| 24 | if ( empty( $httpsrdrctn_options ) ) { |
| 25 | $httpsrdrctn_options = get_option( 'httpsrdrctn_options' ); |
| 26 | } |
| 27 | |
| 28 | //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" ); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | function httpsrdrctn_start_buffer() { |
| 42 | ob_start( "httpsrdrctn_the_content" ); |
| 43 | } |
| 44 | |
| 45 | function httpsrdrctn_end_buffer() { |
| 46 | if ( ob_get_length() ) |
| 47 | ob_end_flush(); |
| 48 | } |
| 49 | |
| 50 | function httpsrdrctn_init_time_tasks() { |
| 51 | httpsrdrctn_load_language(); |
| 52 | } |
| 53 | |
| 54 | function httpsrdrctn_load_language() { |
| 55 | /* Internationalization */ |
| 56 | load_plugin_textdomain( 'https_redirection', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); |
| 57 | } |
| 58 | |
| 59 | function httpsrdrctn_plugin_admin_init() { |
| 60 | global $httpsrdrctn_plugin_info; |
| 61 | |
| 62 | $httpsrdrctn_plugin_info = get_plugin_data( __FILE__, false ); |
| 63 | |
| 64 | /* Call register settings function */ |
| 65 | if ( isset( $_GET[ 'page' ] ) && "https-redirection" == $_GET[ 'page' ] ) { |
| 66 | register_httpsrdrctn_settings(); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /* Register settings function */ |
| 71 | |
| 72 | function register_httpsrdrctn_settings() { |
| 73 | global $wpmu, $httpsrdrctn_options, $httpsrdrctn_plugin_info; |
| 74 | |
| 75 | $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" ] |
| 81 | ); |
| 82 | |
| 83 | /* 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' ); |
| 87 | } else { |
| 88 | if ( ! get_option( 'httpsrdrctn_options' ) ) |
| 89 | add_option( 'httpsrdrctn_options', $httpsrdrctn_option_defaults, '', 'yes' ); |
| 90 | } |
| 91 | |
| 92 | /* Get options from the database */ |
| 93 | if ( 1 == $wpmu ) |
| 94 | $httpsrdrctn_options = get_site_option( 'httpsrdrctn_options' ); |
| 95 | else |
| 96 | $httpsrdrctn_options = get_option( 'httpsrdrctn_options' ); |
| 97 | |
| 98 | /* 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 ) { |
| 107 | /* Static so we don't call plugin_basename on every plugin row. */ |
| 108 | static $this_plugin; |
| 109 | if ( ! $this_plugin ) |
| 110 | $this_plugin = plugin_basename( __FILE__ ); |
| 111 | |
| 112 | if ( $file == $this_plugin ) { |
| 113 | $settings_link = '<a href="options-general.php?page=https-redirection">' . __( 'Settings', 'https_redirection' ) . '</a>'; |
| 114 | array_unshift( $links, $settings_link ); |
| 115 | } |
| 116 | return $links; |
| 117 | } |
| 118 | |
| 119 | function httpsrdrctn_register_plugin_links( $links, $file ) { |
| 120 | $base = plugin_basename( __FILE__ ); |
| 121 | if ( $file == $base ) { |
| 122 | $links[] = '<a href="options-general.php?page=https-redirection">' . __( 'Settings', 'https_redirection' ) . '</a>'; |
| 123 | } |
| 124 | return $links; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Function that changes "http" embeds to "https" |
| 129 | */ |
| 130 | |
| 131 | function httpsrdrctn_filter_content( $content ) { |
| 132 | //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://', |
| 140 | ); |
| 141 | $ssl_array = str_replace( "http://", "https://", $http_urls ); |
| 142 | //now replace these links |
| 143 | $str = str_replace( $http_urls, $ssl_array, $content ); |
| 144 | |
| 145 | //replace all http links except hyperlinks |
| 146 | //all tags with src attr are already fixed by str_replace |
| 147 | |
| 148 | $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', |
| 153 | ); |
| 154 | $str = preg_replace( $pattern, 'https://', $str ); |
| 155 | return $str; |
| 156 | } |
| 157 | |
| 158 | function httpsrdrctn_the_content( $content ) { |
| 159 | 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() ); |
| 165 | // 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 | } |
| 195 | } |
| 196 | return $content; |
| 197 | } |
| 198 | |
| 199 | if ( ! function_exists( 'httpsrdrctn_admin_head' ) ) { |
| 200 | |
| 201 | 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 | } |
| 206 | } |
| 207 | |
| 208 | } |
| 209 | |
| 210 | /* Function for delete delete options */ |
| 211 | if ( ! function_exists( 'httpsrdrctn_delete_options' ) ) { |
| 212 | |
| 213 | function httpsrdrctn_delete_options() { |
| 214 | delete_option( 'httpsrdrctn_options' ); |
| 215 | delete_site_option( 'httpsrdrctn_options' ); |
| 216 | } |
| 217 | |
| 218 | } |
| 219 | |
| 220 | function add_httpsrdrctn_admin_menu() { |
| 221 | add_submenu_page( 'options-general.php', 'HTTPS Redirection', 'HTTPS Redirection', 'manage_options', 'https-redirection', 'httpsrdrctn_settings_page' ); |
| 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' ); |
| 227 | |
| 228 | /* Adds "Settings" link to the plugin action page */ |
| 229 | add_filter( 'plugin_action_links', 'httpsrdrctn_plugin_action_links', 10, 2 ); |
| 230 | /* Additional links on the plugin page */ |
| 231 | add_filter( 'plugin_row_meta', 'httpsrdrctn_register_plugin_links', 10, 2 ); |
| 232 | //add_filter('mod_rewrite_rules', 'httpsrdrctn_mod_rewrite_rules');//TODO 5 |
| 233 | |
| 234 | register_uninstall_hook( __FILE__, 'httpsrdrctn_delete_options' ); |
| 235 | |
| 236 | httpsrdrctn_plugin_init(); |
| 237 |