Plugin Directory


Ignore:
Timestamp:
01/28/2017 12:18:45 PM (9 years ago)
Author:
mra13
Message:

updating the force static files to use HTTPS algorithm.

File:
1 edited

Legend:

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

    r1583453 r1584125  
    6767
    6868/* Register settings function */
     69
    6970function register_httpsrdrctn_settings() {
    7071    global $wpmu, $httpsrdrctn_options, $httpsrdrctn_plugin_info;
     
    126127 * TODO - Need to make it better so it only does it for static resources like JS, CSS and Images
    127128 */
     129
    128130function httpsrdrctn_filter_content($content) {
    129131    //filter buffer
    130132    $home_no_www = str_replace("://www.", "://", get_option('home'));
    131133    $home_yes_www = str_replace("://", "://www.", $home_no_www);
    132 
    133134    $http_urls = array(
    134135        str_replace("https://", "http://", $home_yes_www),
     
    143144    //replace all http links except hyperlinks
    144145    //all tags with src attr are already fixed by str_replace
     146
    145147    $pattern = array(
    146148        '/url\([\'"]?\K(http:\/\/)(?=[^)]+)/i',
     
    161163    $current_page = sanitize_post($GLOBALS['wp_the_query']->get_queried_object());
    162164    // Get the page slug
    163     $slug =str_replace(home_url().'/','',get_permalink($current_page));
     165    $slug = str_replace(home_url() . '/', '', get_permalink($current_page));
     166    $slug = rtrim($slug, "/"); //remove trailing slash if it's there
     167
     168    $content.=$slug;
    164169
    165170    if ($httpsrdrctn_options['force_resources'] == '1' && $httpsrdrctn_options['https'] == 1) {
     
    167172            $content = httpsrdrctn_filter_content($content);
    168173        } else if (!empty($httpsrdrctn_options['https_pages_array'])) {
     174            $pages_str = '';
     175            $on_https_page = false;
    169176            foreach ($httpsrdrctn_options['https_pages_array'] as $https_page) {
    170                 if ($https_page == $slug) { //if we are on the page that is in the array, let's replace all contents to https
    171                     $content = httpsrdrctn_filter_content($content);
     177                $pages_str.=preg_quote($https_page, '/') . '[\/|][\'"]|'; //let's add page to the preg expression string in case we'd need it later
     178                if ($https_page == $slug) { //if we are on the page that is in the array, let's set the var to true
     179                    $on_https_page = true;
    172180                } else { //if not - let's replace all links to that page only to https
    173181                    $http_domain = home_url();
     
    176184                }
    177185            }
     186            if ($on_https_page) { //we are on one of the https pages
     187                $pages_str = substr($pages_str, 0, strlen($pages_str) - 1); //remove last '|'
     188                $content = httpsrdrctn_filter_content($content); //let's change everything to https first
     189                $http_domain = str_replace('https://', 'http://', home_url());
     190                $https_domain = str_replace('http://', 'https://', home_url());
     191                //now let's change all inner links to http, excluding those that user sets to be https in plugin settings
     192                $content = preg_replace('/<a .*?href=[\'"]\K' . preg_quote($https_domain, '/') . '\/((?!' . $pages_str . ').)(?=[^\'"]+)/i', $http_domain . '/$1', $content);
     193                $content = preg_replace('/' . preg_quote($https_domain, '/') . '([\'"])/i', $http_domain . '$1', $content);
     194            }
    178195        }
    179196    }
Note: See TracChangeset for help on using the changeset viewer.