/wp-content/plugins/woocommerce/includes/class-wc-https.php

https://gitlab.com/hunt9310/ras · PHP · 131 lines · 80 code · 15 blank · 36 comment · 30 complexity · dfd17c4416fa9e254e0ce4dffad13d69 MD5 · raw file

  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. /**
  6. * WC_HTTPS class.
  7. *
  8. * @class WC_HTTPS
  9. * @version 2.2.0
  10. * @package WooCommerce/Classes
  11. * @category Class
  12. * @author WooThemes
  13. */
  14. class WC_HTTPS {
  15. /**
  16. * 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.
  17. */
  18. public static function init() {
  19. if ( 'yes' === get_option( 'woocommerce_force_ssl_checkout' ) && ! is_admin() ) {
  20. // HTTPS urls with SSL on
  21. $filters = array(
  22. 'post_thumbnail_html',
  23. 'wp_get_attachment_image_attributes',
  24. 'wp_get_attachment_url',
  25. 'option_stylesheet_url',
  26. 'option_template_url',
  27. 'script_loader_src',
  28. 'style_loader_src',
  29. 'template_directory_uri',
  30. 'stylesheet_directory_uri',
  31. 'site_url'
  32. );
  33. foreach ( $filters as $filter ) {
  34. add_filter( $filter, array( __CLASS__, 'force_https_url' ), 999 );
  35. }
  36. add_filter( 'page_link', array( __CLASS__, 'force_https_page_link' ), 10, 2 );
  37. add_action( 'template_redirect', array( __CLASS__, 'force_https_template_redirect' ) );
  38. if ( 'yes' == get_option( 'woocommerce_unforce_ssl_checkout' ) ) {
  39. add_action( 'template_redirect', array( __CLASS__, 'unforce_https_template_redirect' ) );
  40. }
  41. }
  42. add_action( 'http_api_curl', array( __CLASS__, 'http_api_curl' ), 10, 3 );
  43. }
  44. /**
  45. * Force https for urls.
  46. *
  47. * @param mixed $content
  48. * @return string
  49. */
  50. public static function force_https_url( $content ) {
  51. if ( is_ssl() ) {
  52. if ( is_array( $content ) ) {
  53. $content = array_map( 'WC_HTTPS::force_https_url', $content );
  54. } else {
  55. $content = str_replace( 'http:', 'https:', $content );
  56. }
  57. }
  58. return $content;
  59. }
  60. /**
  61. * Force a post link to be SSL if needed.
  62. *
  63. * @return string
  64. */
  65. public static function force_https_page_link( $link, $page_id ) {
  66. if ( in_array( $page_id, array( get_option( 'woocommerce_checkout_page_id' ), get_option( 'woocommerce_myaccount_page_id' ) ) ) ) {
  67. $link = str_replace( 'http:', 'https:', $link );
  68. } elseif ( 'yes' === get_option( 'woocommerce_unforce_ssl_checkout' ) && ! wc_site_is_https() ) {
  69. $link = str_replace( 'https:', 'http:', $link );
  70. }
  71. return $link;
  72. }
  73. /**
  74. * Template redirect - if we end up on a page ensure it has the correct http/https url.
  75. */
  76. public static function force_https_template_redirect() {
  77. if ( ! is_ssl() && ( is_checkout() || is_account_page() || apply_filters( 'woocommerce_force_ssl_checkout', false ) ) ) {
  78. if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
  79. wp_safe_redirect( preg_replace( '|^http://|', 'https://', $_SERVER['REQUEST_URI'] ) );
  80. exit;
  81. } else {
  82. wp_safe_redirect( 'https://' . ( ! empty( $_SERVER['HTTP_X_FORWARDED_HOST'] ) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $_SERVER['HTTP_HOST'] ) . $_SERVER['REQUEST_URI'] );
  83. exit;
  84. }
  85. }
  86. }
  87. /**
  88. * Template redirect - if we end up on a page ensure it has the correct http/https url.
  89. */
  90. public static function unforce_https_template_redirect() {
  91. if ( function_exists( 'is_customize_preview' ) && is_customize_preview() ) {
  92. return;
  93. }
  94. if ( ! wc_site_is_https() && is_ssl() && $_SERVER['REQUEST_URI'] && ! is_checkout() && ! is_ajax() && ! is_account_page() && apply_filters( 'woocommerce_unforce_ssl_checkout', true ) ) {
  95. if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
  96. wp_safe_redirect( preg_replace( '|^https://|', 'http://', $_SERVER['REQUEST_URI'] ) );
  97. exit;
  98. } else {
  99. wp_safe_redirect( 'http://' . ( ! empty( $_SERVER['HTTP_X_FORWARDED_HOST'] ) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $_SERVER['HTTP_HOST'] ) . $_SERVER['REQUEST_URI'] );
  100. exit;
  101. }
  102. }
  103. }
  104. /**
  105. * Force posts to PayPal to use TLS v1.2. See:
  106. * https://core.trac.wordpress.org/ticket/36320
  107. * https://core.trac.wordpress.org/ticket/34924#comment:13
  108. * https://www.paypal-knowledge.com/infocenter/index?page=content&widgetview=true&id=FAQ1914&viewlocale=en_US
  109. */
  110. public static function http_api_curl( $handle, $r, $url ) {
  111. if ( strstr( $url, 'https://' ) && ( strstr( $url, '.paypal.com/nvp' ) || strstr( $url, '.paypal.com/cgi-bin/webscr' ) ) ) {
  112. curl_setopt( $handle, CURLOPT_SSLVERSION, 6 );
  113. }
  114. }
  115. }
  116. WC_HTTPS::init();