1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) exit;
4
5 6 7 8 9 10 11 12 13
14 class WC_HTTPS {
15
16 17 18
19 public function __construct() {
20 if ( 'yes' == get_option( 'woocommerce_force_ssl_checkout' ) ) {
21 if ( ! is_admin() || ( defined( 'DOING_AJAX' ) && in_array( $_REQUEST['action'], array( 'woocommerce_get_refreshed_fragments', 'woocommerce_checkout', 'woocommerce_update_order_review', 'woocommerce_update_shipping_method', 'woocommerce_apply_coupon' ) ) ) ) {
22
23 $filters = array( 'post_thumbnail_html', 'wp_get_attachment_url', 'wp_get_attachment_image_attributes', 'wp_get_attachment_url', 'option_stylesheet_url', 'option_template_url', 'script_loader_src', 'style_loader_src', 'template_directory_uri', 'stylesheet_directory_uri', 'site_url' );
24
25 foreach ( $filters as $filter ) {
26 add_filter( $filter, 'WC_HTTPS::force_https_url' );
27 }
28
29 add_filter( 'page_link', array( $this, 'force_https_page_link' ), 10, 2 );
30 add_action( 'template_redirect', array( $this, 'force_https_template_redirect' ) );
31
32 if ( get_option('woocommerce_unforce_ssl_checkout') == 'yes' )
33 add_action( 'template_redirect', array( $this, 'unforce_https_template_redirect' ) );
34 }
35 }
36 }
37
38 39 40 41 42 43
44 public static function force_https_url( $content ) {
45 if ( is_ssl() ) {
46 if ( is_array( $content ) )
47 $content = array_map( 'WC_HTTPS::force_https_url', $content );
48 else
49 $content = str_replace( 'http:', 'https:', $content );
50 }
51 return $content;
52 }
53
54 55 56 57 58 59 60
61 public function force_https_page_link( $link, $page_id ) {
62 if ( in_array( $page_id, array( get_option( 'woocommerce_checkout_page_id' ), get_option( 'woocommerce_myaccount_page_id' ) ) ) ) {
63 $link = str_replace( 'http:', 'https:', $link );
64 } elseif ( get_option('woocommerce_unforce_ssl_checkout') == 'yes' ) {
65 $link = str_replace( 'https:', 'http:', $link );
66 }
67 return $link;
68 }
69
70 71 72
73 public function force_https_template_redirect() {
74 if ( ! is_ssl() && ( is_checkout() || is_account_page() || apply_filters( 'woocommerce_force_ssl_checkout', false ) ) ) {
75
76 if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
77 wp_safe_redirect( preg_replace( '|^http://|', 'https://', $_SERVER['REQUEST_URI'] ) );
78 exit;
79 } else {
80 wp_safe_redirect( 'https://' . ( ! empty( $_SERVER['HTTP_X_FORWARDED_HOST'] ) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $_SERVER['HTTP_HOST'] ) . $_SERVER['REQUEST_URI'] );
81 exit;
82 }
83 }
84 }
85
86 87 88
89 public function unforce_https_template_redirect() {
90 if ( is_ssl() && $_SERVER['REQUEST_URI'] && ! is_checkout() && ! is_ajax() && ! is_account_page() && apply_filters( 'woocommerce_unforce_ssl_checkout', true ) ) {
91
92 if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
93 wp_safe_redirect( preg_replace( '|^https://|', 'http://', $_SERVER['REQUEST_URI'] ) );
94 exit;
95 } else {
96 wp_safe_redirect( 'http://' . ( ! empty( $_SERVER['HTTP_X_FORWARDED_HOST'] ) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $_SERVER['HTTP_HOST'] ) . $_SERVER['REQUEST_URI'] );
97 exit;
98 }
99 }
100 }
101 }
102
103 new WC_HTTPS();
104