utilities
1 year ago
ehssl-config.php
1 year ago
ehssl-cronjob.php
1 year ago
ehssl-custom-post-types.php
1 year ago
ehssl-debug-logger.php
1 year ago
ehssl-email-handler.php
1 year ago
ehssl-init-time-tasks.php
1 year ago
ehssl-rules-helper.php
1 year ago
ehssl-ssl-certificate.php
1 year ago
index.php
1 year ago
ehssl-init-time-tasks.php
119 lines
| 1 | <?php |
| 2 | |
| 3 | class EHSSL_Init_Time_Tasks { |
| 4 | |
| 5 | // Constructor |
| 6 | public function __construct() { |
| 7 | // Add the init action hook |
| 8 | add_action('init', array($this, 'early_priority_init_handler'), 0);// Early priority init. |
| 9 | add_action('init', array($this, 'run_init_time_tasks'));//Normal priority init. |
| 10 | } |
| 11 | |
| 12 | // Method to run at 'init' action hook. |
| 13 | public function run_init_time_tasks() { |
| 14 | // Register custom post types. |
| 15 | EHSSL_Custom_Post_Types::get_instance()->register_custom_post_types(); |
| 16 | } |
| 17 | |
| 18 | public function early_priority_init_handler() { |
| 19 | //This is the early priority init handler (needed for some features of the plugin). |
| 20 | global $httpsrdrctn_options; |
| 21 | if (empty($httpsrdrctn_options)) { |
| 22 | $httpsrdrctn_options = get_option('httpsrdrctn_options'); |
| 23 | } |
| 24 | |
| 25 | //Mixed content fix feature. Do force resource embedded using HTTPS. |
| 26 | if (isset($httpsrdrctn_options['force_resources']) && $httpsrdrctn_options['force_resources'] == '1') { |
| 27 | // Handle the appropriate content filters to force the static resources to use HTTPS URL. |
| 28 | if (is_admin()) { |
| 29 | add_action("admin_init", array($this, "ehssl_start_buffer")); |
| 30 | } else { |
| 31 | add_action("init", array($this, "ehssl_start_buffer")); |
| 32 | } |
| 33 | add_action("shutdown", array($this, "ehssl_end_buffer")); |
| 34 | } |
| 35 | |
| 36 | } |
| 37 | |
| 38 | public function ehssl_start_buffer(){ |
| 39 | ob_start(array($this, "ehssl_the_content")); |
| 40 | } |
| 41 | |
| 42 | public function ehssl_end_buffer() { |
| 43 | if (ob_get_length()) { |
| 44 | ob_end_flush(); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | public function ehssl_the_content($content) { |
| 49 | global $httpsrdrctn_options; |
| 50 | if (empty($httpsrdrctn_options)) { |
| 51 | $httpsrdrctn_options = get_option('httpsrdrctn_options'); |
| 52 | } |
| 53 | |
| 54 | $current_page = sanitize_post($GLOBALS['wp_the_query']->get_queried_object()); |
| 55 | // Get the page slug |
| 56 | $slug = str_replace(home_url() . '/', '', get_permalink($current_page)); |
| 57 | $slug = rtrim($slug, "/"); //remove trailing slash if it's there |
| 58 | |
| 59 | if ($httpsrdrctn_options['force_resources'] == '1' && $httpsrdrctn_options['https'] == 1) { |
| 60 | if ($httpsrdrctn_options['https_domain'] == 1) { |
| 61 | $content = $this->ehssl_filter_content($content); |
| 62 | } else if (!empty($httpsrdrctn_options['https_pages_array'])) { |
| 63 | $pages_str = ''; |
| 64 | $on_https_page = false; |
| 65 | foreach ($httpsrdrctn_options['https_pages_array'] as $https_page) { |
| 66 | $pages_str .= preg_quote($https_page, '/') . '[\/|][\'"]|'; //let's add page to the preg expression string in case we'd need it later |
| 67 | if ($https_page == $slug) { //if we are on the page that is in the array, let's set the var to true |
| 68 | $on_https_page = true; |
| 69 | } else { //if not - let's replace all links to that page only to https |
| 70 | $http_domain = home_url(); |
| 71 | $https_domain = str_replace('http://', 'https://', home_url()); |
| 72 | $content = str_replace($http_domain . '/' . $https_page, $https_domain . '/' . $https_page, $content); |
| 73 | } |
| 74 | } |
| 75 | if ($on_https_page) { //we are on one of the https pages |
| 76 | $pages_str = substr($pages_str, 0, strlen($pages_str) - 1); //remove last '|' |
| 77 | $content = $this->ehssl_filter_content($content); //let's change everything to https first |
| 78 | $http_domain = str_replace('https://', 'http://', home_url()); |
| 79 | $https_domain = str_replace('http://', 'https://', home_url()); |
| 80 | //now let's change all inner links to http, excluding those that user sets to be https in plugin settings |
| 81 | $content = preg_replace('/<a .*?href=[\'"]\K' . preg_quote($https_domain, '/') . '\/((?!' . $pages_str . ').)(?=[^\'"]+)/i', $http_domain . '/$1', $content); |
| 82 | $content = preg_replace('/' . preg_quote($https_domain, '/') . '([\'"])/i', $http_domain . '$1', $content); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | return $content; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Function that changes "http" embeds to "https" |
| 91 | */ |
| 92 | public function ehssl_filter_content($content) { |
| 93 | //filter buffer |
| 94 | $home_no_www = str_replace("://www.", "://", get_option('home')); |
| 95 | $home_yes_www = str_replace("://", "://www.", $home_no_www); |
| 96 | $http_urls = array( |
| 97 | str_replace("https://", "http://", $home_yes_www), |
| 98 | str_replace("https://", "http://", $home_no_www), |
| 99 | "src='http://", |
| 100 | 'src="http://', |
| 101 | ); |
| 102 | $ssl_array = str_replace("http://", "https://", $http_urls); |
| 103 | //now replace these links |
| 104 | $str = str_replace($http_urls, $ssl_array, $content); |
| 105 | |
| 106 | //replace all http links except hyperlinks |
| 107 | //all tags with src attr are already fixed by str_replace |
| 108 | |
| 109 | $pattern = array( |
| 110 | '/url\([\'"]?\K(http:\/\/)(?=[^)]+)/i', |
| 111 | '/<link .*?href=[\'"]\K(http:\/\/)(?=[^\'"]+)/i', |
| 112 | '/<meta property="og:image" .*?content=[\'"]\K(http:\/\/)(?=[^\'"]+)/i', |
| 113 | '/<form [^>]*?action=[\'"]\K(http:\/\/)(?=[^\'"]+)/i', |
| 114 | ); |
| 115 | $str = preg_replace($pattern, 'https://', $str); |
| 116 | return $str; |
| 117 | } |
| 118 | |
| 119 | } |