Changeset 2198540 for https-redirection/trunk/https-redirection.php
- Timestamp:
- 11/22/2019 02:39:27 AM (6 years ago)
- File:
-
- 1 edited
-
https-redirection/trunk/https-redirection.php (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
https-redirection/trunk/https-redirection.php
r1831198 r2198540 6 6 Description: The plugin HTTPS Redirection allows an automatic redirection to the "HTTPS" version/URL of the site. 7 7 Author: Tips and Tricks HQ 8 Version: 1.9 8 Version: 1.9.1 9 9 Author URI: https://www.tipsandtricks-hq.com/ 10 10 License: GPLv2 or later … … 13 13 */ 14 14 15 if ( !defined('ABSPATH')) {15 if ( ! defined( 'ABSPATH' ) ) { 16 16 exit; //Exit if accessed directly 17 17 } … … 22 22 function httpsrdrctn_plugin_init() { 23 23 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' ); 26 26 } 27 27 28 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 URL31 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" ); 38 38 } 39 39 } 40 40 41 41 function httpsrdrctn_start_buffer() { 42 ob_start( "httpsrdrctn_the_content");42 ob_start( "httpsrdrctn_the_content" ); 43 43 } 44 44 45 45 function httpsrdrctn_end_buffer() { 46 if ( ob_get_length())47 ob_end_flush();46 if ( ob_get_length() ) 47 ob_end_flush(); 48 48 } 49 49 … … 54 54 function httpsrdrctn_load_language() { 55 55 /* 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/' ); 57 57 } 58 58 … … 60 60 global $httpsrdrctn_plugin_info; 61 61 62 $httpsrdrctn_plugin_info = get_plugin_data( __FILE__, false);62 $httpsrdrctn_plugin_info = get_plugin_data( __FILE__, false ); 63 63 64 64 /* 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(); 67 67 } 68 68 } … … 74 74 75 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"]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 81 ); 82 82 83 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');84 if ( 1 == $wpmu ) { 85 if ( ! get_site_option( 'httpsrdrctn_options' ) ) 86 add_site_option( 'httpsrdrctn_options', $httpsrdrctn_option_defaults, '', 'yes' ); 87 87 } 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' ); 90 90 } 91 91 92 92 /* 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' ); 95 95 else 96 $httpsrdrctn_options = get_option('httpsrdrctn_options');96 $httpsrdrctn_options = get_option( 'httpsrdrctn_options' ); 97 97 98 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) {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 107 /* Static so we don't call plugin_basename on every plugin row. */ 108 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="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 ); 115 115 } 116 116 return $links; 117 117 } 118 118 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>';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>'; 123 123 } 124 124 return $links; … … 129 129 */ 130 130 131 function httpsrdrctn_filter_content( $content) {131 function httpsrdrctn_filter_content( $content ) { 132 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://',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 140 ); 141 $ssl_array = str_replace("http://", "https://", $http_urls);141 $ssl_array = str_replace( "http://", "https://", $http_urls ); 142 142 //now replace these links 143 $str = str_replace($http_urls, $ssl_array, $content);143 $str = str_replace( $http_urls, $ssl_array, $content ); 144 144 145 145 //replace all http links except hyperlinks … … 147 147 148 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',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 153 ); 154 $str = preg_replace($pattern, 'https://', $str);154 $str = preg_replace( $pattern, 'https://', $str ); 155 155 return $str; 156 156 } 157 157 158 function httpsrdrctn_the_content( $content) {158 function httpsrdrctn_the_content( $content ) { 159 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());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 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 there168 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 later177 if ($https_page == $slug) { //if we are on the page that is in the array, let's set the var to true178 $on_https_page = true;179 } else { //if not - let's replace all links to that page only to https180 $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 pages186 $pages_str = substr($pages_str, 0, strlen($pages_str) - 1); //remove last '|'187 $content = httpsrdrctn_filter_content($content); //let's change everything to https first188 $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 settings191 $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 } 195 195 } 196 196 return $content; 197 197 } 198 198 199 if ( !function_exists('httpsrdrctn_admin_head')) {199 if ( ! function_exists( 'httpsrdrctn_admin_head' ) ) { 200 200 201 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 }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 206 } 207 207 … … 209 209 210 210 /* Function for delete delete options */ 211 if ( !function_exists('httpsrdrctn_delete_options')) {211 if ( ! function_exists( 'httpsrdrctn_delete_options' ) ) { 212 212 213 213 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' ); 216 216 } 217 217 … … 219 219 220 220 function 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 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 227 228 228 /* Adds "Settings" link to the plugin action page */ 229 add_filter( 'plugin_action_links', 'httpsrdrctn_plugin_action_links', 10, 2);229 add_filter( 'plugin_action_links', 'httpsrdrctn_plugin_action_links', 10, 2 ); 230 230 /* Additional links on the plugin page */ 231 add_filter( 'plugin_row_meta', 'httpsrdrctn_register_plugin_links', 10, 2);231 add_filter( 'plugin_row_meta', 'httpsrdrctn_register_plugin_links', 10, 2 ); 232 232 //add_filter('mod_rewrite_rules', 'httpsrdrctn_mod_rewrite_rules');//TODO 5 233 233 234 register_uninstall_hook( __FILE__, 'httpsrdrctn_delete_options');234 register_uninstall_hook( __FILE__, 'httpsrdrctn_delete_options' ); 235 235 236 236 httpsrdrctn_plugin_init();
Note: See TracChangeset
for help on using the changeset viewer.