1 <?php
  2 
  3 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  4 
  5 /**
  6  * WC_HTTPS class.
  7  *
  8  * @class       WC_HTTPS
  9  * @version     2.1.0
 10  * @package     WooCommerce/Classes
 11  * @category    Class
 12  * @author      WooThemes
 13  */
 14 class WC_HTTPS {
 15 
 16     /**
 17      * Hook in our HTTPS functions if we're on the frontend. This will ensure any links output to a page (when viewing via HTTPS) are also served over HTTPS.
 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                 // HTTPS urls with SSL on
 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      * force_https_url function.
 40      *
 41      * @param mixed $content
 42      * @return string
 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      * Force a post link to be SSL if needed
 56      *
 57      * @param  string $post_link
 58      * @param  object $post
 59      * @return string
 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      * Template redirect - if we end up on a page ensure it has the correct http/https url
 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      * Template redirect - if we end up on a page ensure it has the correct http/https url
 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 
WooCommerce API documentation generated by ApiGen 2.8.0