| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handle rewriting asset URLs for SSL enforced booking |
| 4 |
* |
| 5 |
* @author Benito Lopez <hello@lopezb.com> |
| 6 |
* @category Class |
| 7 |
* @package Hotelier/Classes |
| 8 |
* @version 1.0.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly |
| 13 |
} |
| 14 |
|
| 15 |
if ( ! class_exists( 'HTL_HTTPS' ) ) : |
| 16 |
|
| 17 |
/** |
| 18 |
* HTL_HTTPS Class |
| 19 |
*/ |
| 20 |
class HTL_HTTPS { |
| 21 |
|
| 22 |
/** |
| 23 |
* This will ensure any links output to a page (when viewing via HTTPS) are also served over HTTPS. |
| 24 |
*/ |
| 25 |
public static function init() { |
| 26 |
if ( htl_get_option( 'enforce_ssl_booking' ) && ! is_admin() ) { |
| 27 |
|
| 28 |
// HTTPS urls with SSL on |
| 29 |
$filters = array( |
| 30 |
'post_thumbnail_html', |
| 31 |
'wp_get_attachment_image_attributes', |
| 32 |
'wp_get_attachment_url', |
| 33 |
'option_stylesheet_url', |
| 34 |
'option_template_url', |
| 35 |
'script_loader_src', |
| 36 |
'style_loader_src', |
| 37 |
'template_directory_uri', |
| 38 |
'stylesheet_directory_uri', |
| 39 |
'site_url' |
| 40 |
); |
| 41 |
|
| 42 |
foreach ( $filters as $filter ) { |
| 43 |
add_filter( $filter, array( __CLASS__, 'force_https_url' ), 999 ); |
| 44 |
} |
| 45 |
|
| 46 |
add_filter( 'page_link', array( __CLASS__, 'force_https_page_link' ), 10, 2 ); |
| 47 |
add_action( 'template_redirect', array( __CLASS__, 'force_https_template_redirect' ) ); |
| 48 |
|
| 49 |
if ( htl_get_option( 'unforce_ssl_booking' ) ) { |
| 50 |
add_action( 'template_redirect', array( __CLASS__, 'unforce_https_template_redirect' ) ); |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* force_https_url function. |
| 57 |
* |
| 58 |
* @param mixed $content |
| 59 |
* @return string |
| 60 |
*/ |
| 61 |
public static function force_https_url( $content ) { |
| 62 |
if ( is_ssl() ) { |
| 63 |
if ( is_array( $content ) ) { |
| 64 |
$content = array_map( 'HTL_HTTPS::force_https_url', $content ); |
| 65 |
} else { |
| 66 |
$content = str_replace( 'http:', 'https:', $content ); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
return $content; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Force a post link to be SSL if needed. |
| 75 |
* |
| 76 |
* @return string |
| 77 |
*/ |
| 78 |
public static function force_https_page_link( $link, $page_id ) { |
| 79 |
if ( $page_id == get_option( 'hotelier_booking_page_id' ) ) { |
| 80 |
$link = str_replace( 'http:', 'https:', $link ); |
| 81 |
} elseif ( htl_get_option( 'unforce_ssl_booking' ) ) { |
| 82 |
$link = str_replace( 'https:', 'http:', $link ); |
| 83 |
} |
| 84 |
|
| 85 |
return $link; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Template redirect - if we end up on a page ensure it has the correct http/https url. |
| 90 |
*/ |
| 91 |
public static function force_https_template_redirect() { |
| 92 |
if ( ! is_ssl() && ( is_booking() || apply_filters( 'hotelier_enforce_ssl_booking', false ) ) ) { |
| 93 |
|
| 94 |
if ( 0 === strpos( $_SERVER[ 'REQUEST_URI' ], 'http' ) ) { |
| 95 |
wp_safe_redirect( preg_replace( '|^http://|', 'https://', $_SERVER[ 'REQUEST_URI' ] ) ); |
| 96 |
exit; |
| 97 |
} else { |
| 98 |
wp_safe_redirect( 'https://' . ( ! empty( $_SERVER[ 'HTTP_X_FORWARDED_HOST' ] ) ? $_SERVER[ 'HTTP_X_FORWARDED_HOST' ] : $_SERVER[ 'HTTP_HOST' ] ) . $_SERVER[ 'REQUEST_URI' ] ); |
| 99 |
exit; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Template redirect - if we end up on a page ensure it has the correct http/https url. |
| 106 |
*/ |
| 107 |
public static function unforce_https_template_redirect() { |
| 108 |
if ( function_exists( 'is_customize_preview' ) && is_customize_preview() ) { |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
if ( is_ssl() && $_SERVER[ 'REQUEST_URI' ] && ! is_booking() && ! is_ajax() && apply_filters( 'hotelier_unforce_ssl_booking', true ) ) { |
| 113 |
|
| 114 |
if ( 0 === strpos( $_SERVER[ 'REQUEST_URI' ], 'http' ) ) { |
| 115 |
wp_safe_redirect( preg_replace( '|^https://|', 'http://', $_SERVER[ 'REQUEST_URI' ] ) ); |
| 116 |
exit; |
| 117 |
} else { |
| 118 |
wp_safe_redirect( 'http://' . ( ! empty( $_SERVER[ 'HTTP_X_FORWARDED_HOST' ] ) ? $_SERVER[ 'HTTP_X_FORWARDED_HOST' ] : $_SERVER['HTTP_HOST'] ) . $_SERVER[ 'REQUEST_URI' ] ); |
| 119 |
exit; |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
endif; |
| 127 |
|
| 128 |
HTL_HTTPS::init(); |
| 129 |
|