Plugin Directory


Ignore:
Timestamp:
01/27/2017 12:00:59 PM (9 years ago)
Author:
mra13
Message:

committing v1.6

File:
1 edited

Legend:

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

    r1455885 r1583453  
    11<?php
     2
    23/*
    34  Plugin Name: Easy HTTPS (SSL) Redirection
    4   Plugin URI:
     5  Plugin URI: https://www.tipsandtricks-hq.com/development-center
    56  Description: The plugin HTTPS Redirection allows an automatic redirection to the "HTTPS" version/URL of the site.
    67  Author: Tips and Tricks HQ
    7   Version: 1.5
     8  Version: 1.6
    89  Author URI: https://www.tipsandtricks-hq.com/
    910  License: GPLv2 or later
    1011 */
    1112
    12 if (!defined('ABSPATH'))exit; //Exit if accessed directly
     13if (!defined('ABSPATH')) {
     14    exit; //Exit if accessed directly
     15}
    1316
    1417include_once('https-rules-helper.php');
    1518include_once('https-redirection-settings.php');
    1619
    17 function add_httpsrdrctn_admin_menu() {
    18     add_submenu_page('options-general.php', 'HTTPS Redirection', 'HTTPS Redirection', 'manage_options', 'https-redirection', 'httpsrdrctn_settings_page', plugins_url("images/px.png", __FILE__), 1001);
    19 }
    20 
    2120function httpsrdrctn_plugin_init() {
    2221    global $httpsrdrctn_options;
    23     /* Internationalization, first(!) */
    24     load_plugin_textdomain('https_redirection', false, dirname(plugin_basename(__FILE__)) . '/languages/');
    2522    if (empty($httpsrdrctn_options)) {
    2623        $httpsrdrctn_options = get_option('httpsrdrctn_options');
     
    3027    if (isset($httpsrdrctn_options['force_resources']) && $httpsrdrctn_options['force_resources'] == '1') {
    3128        //Handle the appropriate content filters to force the static resources to use HTTPS URL
    32         //TODO 1
    33         add_filter( 'the_content', 'httpsrdrctn_the_content' );
    34         add_filter( 'get_the_content', 'httpsrdrctn_the_content' );
    35         add_filter( 'the_excerpt', 'httpsrdrctn_the_content' );
    36         add_filter( 'get_the_excerpt', 'httpsrdrctn_the_content' );
    37     }
    38 
     29        if (is_admin()) {
     30            add_action("admin_init", "httpsrdrctn_start_buffer");
     31        } else {
     32            add_action("init", "httpsrdrctn_start_buffer");
     33            add_action("init", "httpsrdrctn_init_time_tasks");
     34        }
     35        add_action("shutdown", "httpsrdrctn_end_buffer");
     36    }
     37}
     38
     39function httpsrdrctn_start_buffer() {
     40    ob_start("httpsrdrctn_the_content");
     41}
     42
     43function httpsrdrctn_end_buffer() {
     44    if (ob_get_length())
     45        ob_end_flush();
     46}
     47
     48function httpsrdrctn_init_time_tasks() {
     49    httpsrdrctn_load_language();
     50}
     51
     52function httpsrdrctn_load_language() {
     53    /* Internationalization */
     54    load_plugin_textdomain('https_redirection', false, dirname(plugin_basename(__FILE__)) . '/languages/');
    3955}
    4056
     
    4561
    4662    /* Call register settings function */
    47     if (isset($_GET['page']) && "https-redirection" == $_GET['page']){
     63    if (isset($_GET['page']) && "https-redirection" == $_GET['page']) {
    4864        register_httpsrdrctn_settings();
    4965    }
    5066}
    5167
    52 /* register settings function */
     68/* Register settings function */
    5369function register_httpsrdrctn_settings() {
    5470    global $wpmu, $httpsrdrctn_options, $httpsrdrctn_plugin_info;
     
    98114}
    99115
    100 
    101 if (!function_exists('httpsrdrctn_register_plugin_links')) {
    102 
    103     function httpsrdrctn_register_plugin_links($links, $file) {
    104         $base = plugin_basename(__FILE__);
    105         if ($file == $base) {
    106             $links[] = '<a href="admin.php?page=https-redirection">' . __('Settings', 'https_redirection') . '</a>';
    107         }
    108         return $links;
    109     }
    110 
    111 }
    112 
    113 /*
     116function httpsrdrctn_register_plugin_links($links, $file) {
     117    $base = plugin_basename(__FILE__);
     118    if ($file == $base) {
     119        $links[] = '<a href="admin.php?page=https-redirection">' . __('Settings', 'https_redirection') . '</a>';
     120    }
     121    return $links;
     122}
     123
     124/*
    114125 * Function that changes "http" embeds to "https"
    115126 * TODO - Need to make it better so it only does it for static resources like JS, CSS and Images
    116127 */
     128function httpsrdrctn_filter_content($content) {
     129    //filter buffer
     130    $home_no_www = str_replace("://www.", "://", get_option('home'));
     131    $home_yes_www = str_replace("://", "://www.", $home_no_www);
     132
     133    $http_urls = array(
     134        str_replace("https://", "http://", $home_yes_www),
     135        str_replace("https://", "http://", $home_no_www),
     136        "src='http://",
     137        'src="http://',
     138    );
     139    $ssl_array = str_replace("http://", "https://", $http_urls);
     140    //now replace these links
     141    $str = str_replace($http_urls, $ssl_array, $content);
     142
     143    //replace all http links except hyperlinks
     144    //all tags with src attr are already fixed by str_replace
     145    $pattern = array(
     146        '/url\([\'"]?\K(http:\/\/)(?=[^)]+)/i',
     147        '/<link .*?href=[\'"]\K(http:\/\/)(?=[^\'"]+)/i',
     148        '/<meta property="og:image" .*?content=[\'"]\K(http:\/\/)(?=[^\'"]+)/i',
     149        '/<form [^>]*?action=[\'"]\K(http:\/\/)(?=[^\'"]+)/i',
     150    );
     151    $str = preg_replace($pattern, 'https://', $str);
     152    return $str;
     153}
     154
    117155function httpsrdrctn_the_content($content) {
    118156    global $httpsrdrctn_options;
     
    120158        $httpsrdrctn_options = get_option('httpsrdrctn_options');
    121159    }
     160
     161    $current_page = sanitize_post($GLOBALS['wp_the_query']->get_queried_object());
     162    // Get the page slug
     163    $slug =str_replace(home_url().'/','',get_permalink($current_page));
     164
    122165    if ($httpsrdrctn_options['force_resources'] == '1' && $httpsrdrctn_options['https'] == 1) {
    123166        if ($httpsrdrctn_options['https_domain'] == 1) {
    124             if (strpos(home_url(), 'https') !== false) {
    125                 $http_domain = str_replace('https', 'http', home_url());
    126                 $https_domain = home_url();
    127             } else {
    128                 $http_domain = home_url();
    129                 $https_domain = str_replace('http', 'https', home_url());
    130             }
    131             $content = str_replace($http_domain, $https_domain, $content);
     167            $content = httpsrdrctn_filter_content($content);
    132168        } else if (!empty($httpsrdrctn_options['https_pages_array'])) {
    133169            foreach ($httpsrdrctn_options['https_pages_array'] as $https_page) {
    134                 if (strpos(home_url(), 'https') !== false) {
    135                     $http_domain = str_replace('https', 'http', home_url());
    136                     $https_domain = home_url();
    137                 } else {
     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);
     172                } else { //if not - let's replace all links to that page only to https
    138173                    $http_domain = home_url();
    139                     $https_domain = str_replace('http', 'https', home_url());
     174                    $https_domain = str_replace('http://', 'https://', home_url());
     175                    $content = str_replace($http_domain . '/' . $https_page, $https_domain . '/' . $https_page, $content);
    140176                }
    141                 $content = str_replace($http_domain . '/' . $https_page, $https_domain . '/' . $https_page, $content);
    142177            }
    143178        }
     
    167202}
    168203
     204function add_httpsrdrctn_admin_menu() {
     205    add_submenu_page('options-general.php', 'HTTPS Redirection', 'HTTPS Redirection', 'manage_options', 'https-redirection', 'httpsrdrctn_settings_page', plugins_url("images/px.png", __FILE__), 1001);
     206}
     207
    169208add_action('admin_menu', 'add_httpsrdrctn_admin_menu');
    170 add_action('init', 'httpsrdrctn_plugin_init');
    171209add_action('admin_init', 'httpsrdrctn_plugin_admin_init');
    172210add_action('admin_enqueue_scripts', 'httpsrdrctn_admin_head');
     
    179217
    180218register_uninstall_hook(__FILE__, 'httpsrdrctn_delete_options');
     219
     220httpsrdrctn_plugin_init();
Note: See TracChangeset for help on using the changeset viewer.