2013-09-03 12:03:05 +00:00
|
|
|
<?php
|
2014-09-20 18:48:04 +00:00
|
|
|
|
2014-05-28 13:52:50 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit; // Exit if accessed directly
|
|
|
|
}
|
2013-09-03 12:03:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* WC_HTTPS class.
|
|
|
|
*
|
2014-08-11 13:03:27 +00:00
|
|
|
* @class WC_HTTPS
|
|
|
|
* @version 2.2.0
|
|
|
|
* @package WooCommerce/Classes
|
|
|
|
* @category Class
|
|
|
|
* @author WooThemes
|
2013-09-03 12:03:05 +00:00
|
|
|
*/
|
|
|
|
class WC_HTTPS {
|
|
|
|
|
|
|
|
/**
|
2014-01-08 12:08:37 +00:00
|
|
|
* 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.
|
2013-09-03 12:03:05 +00:00
|
|
|
*/
|
2014-05-28 13:52:50 +00:00
|
|
|
public static function init() {
|
2016-01-08 11:27:13 +00:00
|
|
|
if ( 'yes' === get_option( 'woocommerce_force_ssl_checkout' ) && ! is_admin() ) {
|
2015-08-11 10:18:50 +00:00
|
|
|
// HTTPS urls with SSL on
|
|
|
|
$filters = array(
|
|
|
|
'post_thumbnail_html',
|
|
|
|
'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',
|
2016-08-27 02:08:49 +00:00
|
|
|
'site_url',
|
2015-08-11 10:18:50 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( $filters as $filter ) {
|
|
|
|
add_filter( $filter, array( __CLASS__, 'force_https_url' ), 999 );
|
|
|
|
}
|
2014-08-11 13:03:27 +00:00
|
|
|
|
2015-08-11 10:18:50 +00:00
|
|
|
add_filter( 'page_link', array( __CLASS__, 'force_https_page_link' ), 10, 2 );
|
|
|
|
add_action( 'template_redirect', array( __CLASS__, 'force_https_template_redirect' ) );
|
2013-09-03 12:03:05 +00:00
|
|
|
|
2015-08-11 10:18:50 +00:00
|
|
|
if ( 'yes' == get_option( 'woocommerce_unforce_ssl_checkout' ) ) {
|
|
|
|
add_action( 'template_redirect', array( __CLASS__, 'unforce_https_template_redirect' ) );
|
2014-01-08 14:53:38 +00:00
|
|
|
}
|
2013-09-03 12:03:05 +00:00
|
|
|
}
|
2016-03-29 13:28:15 +00:00
|
|
|
add_action( 'http_api_curl', array( __CLASS__, 'http_api_curl' ), 10, 3 );
|
2013-09-03 12:03:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-06 15:24:47 +00:00
|
|
|
* Force https for urls.
|
2013-09-03 12:03:05 +00:00
|
|
|
*
|
|
|
|
* @param mixed $content
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function force_https_url( $content ) {
|
|
|
|
if ( is_ssl() ) {
|
2014-08-11 13:03:27 +00:00
|
|
|
if ( is_array( $content ) ) {
|
2013-09-03 12:03:05 +00:00
|
|
|
$content = array_map( 'WC_HTTPS::force_https_url', $content );
|
2014-08-11 13:03:27 +00:00
|
|
|
} else {
|
2013-09-03 12:03:05 +00:00
|
|
|
$content = str_replace( 'http:', 'https:', $content );
|
2014-08-11 13:03:27 +00:00
|
|
|
}
|
2013-09-03 12:03:05 +00:00
|
|
|
}
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Force a post link to be SSL if needed.
|
2013-09-03 12:03:05 +00:00
|
|
|
*
|
2017-05-15 11:50:52 +00:00
|
|
|
* @param string $link
|
|
|
|
* @param int $page_id
|
|
|
|
*
|
2013-09-03 12:03:05 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2014-05-28 13:52:50 +00:00
|
|
|
public static function force_https_page_link( $link, $page_id ) {
|
2013-09-03 12:03:05 +00:00
|
|
|
if ( in_array( $page_id, array( get_option( 'woocommerce_checkout_page_id' ), get_option( 'woocommerce_myaccount_page_id' ) ) ) ) {
|
|
|
|
$link = str_replace( 'http:', 'https:', $link );
|
2016-01-08 11:27:13 +00:00
|
|
|
} elseif ( 'yes' === get_option( 'woocommerce_unforce_ssl_checkout' ) && ! wc_site_is_https() ) {
|
2013-09-03 12:03:05 +00:00
|
|
|
$link = str_replace( 'https:', 'http:', $link );
|
|
|
|
}
|
|
|
|
return $link;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Template redirect - if we end up on a page ensure it has the correct http/https url.
|
2013-09-03 12:03:05 +00:00
|
|
|
*/
|
2014-05-28 13:52:50 +00:00
|
|
|
public static function force_https_template_redirect() {
|
2013-09-03 12:03:05 +00:00
|
|
|
if ( ! is_ssl() && ( is_checkout() || is_account_page() || apply_filters( 'woocommerce_force_ssl_checkout', false ) ) ) {
|
|
|
|
|
|
|
|
if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
|
|
|
|
wp_safe_redirect( preg_replace( '|^http://|', 'https://', $_SERVER['REQUEST_URI'] ) );
|
|
|
|
exit;
|
|
|
|
} else {
|
2013-12-20 19:03:55 +00:00
|
|
|
wp_safe_redirect( 'https://' . ( ! empty( $_SERVER['HTTP_X_FORWARDED_HOST'] ) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $_SERVER['HTTP_HOST'] ) . $_SERVER['REQUEST_URI'] );
|
2013-09-03 12:03:05 +00:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Template redirect - if we end up on a page ensure it has the correct http/https url.
|
2013-09-03 12:03:05 +00:00
|
|
|
*/
|
2014-05-28 13:52:50 +00:00
|
|
|
public static function unforce_https_template_redirect() {
|
2014-12-12 13:15:38 +00:00
|
|
|
if ( function_exists( 'is_customize_preview' ) && is_customize_preview() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-08 11:27:13 +00:00
|
|
|
if ( ! wc_site_is_https() && is_ssl() && $_SERVER['REQUEST_URI'] && ! is_checkout() && ! is_ajax() && ! is_account_page() && apply_filters( 'woocommerce_unforce_ssl_checkout', true ) ) {
|
2013-09-03 12:03:05 +00:00
|
|
|
|
|
|
|
if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
|
|
|
|
wp_safe_redirect( preg_replace( '|^https://|', 'http://', $_SERVER['REQUEST_URI'] ) );
|
|
|
|
exit;
|
|
|
|
} else {
|
2013-12-20 19:03:55 +00:00
|
|
|
wp_safe_redirect( 'http://' . ( ! empty( $_SERVER['HTTP_X_FORWARDED_HOST'] ) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $_SERVER['HTTP_HOST'] ) . $_SERVER['REQUEST_URI'] );
|
2013-09-03 12:03:05 +00:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-29 13:28:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Force posts to PayPal to use TLS v1.2. See:
|
2017-05-15 11:50:52 +00:00
|
|
|
* https://core.trac.wordpress.org/ticket/36320
|
|
|
|
* https://core.trac.wordpress.org/ticket/34924#comment:13
|
|
|
|
* https://www.paypal-knowledge.com/infocenter/index?page=content&widgetview=true&id=FAQ1914&viewlocale=en_US
|
|
|
|
*
|
|
|
|
* @param string $handle
|
|
|
|
* @param mixed $r
|
|
|
|
* @param string $url
|
2016-03-29 13:28:15 +00:00
|
|
|
*/
|
|
|
|
public static function http_api_curl( $handle, $r, $url ) {
|
2016-03-31 09:12:28 +00:00
|
|
|
if ( strstr( $url, 'https://' ) && ( strstr( $url, '.paypal.com/nvp' ) || strstr( $url, '.paypal.com/cgi-bin/webscr' ) ) ) {
|
2016-04-04 17:37:45 +00:00
|
|
|
curl_setopt( $handle, CURLOPT_SSLVERSION, 6 );
|
2016-03-29 13:28:15 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-03 12:03:05 +00:00
|
|
|
}
|
|
|
|
|
2014-05-28 13:52:50 +00:00
|
|
|
WC_HTTPS::init();
|