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

https://gitlab.com/haque.mdmanzurul/barongbarong · PHP · 103 lines · 58 code · 13 blank · 32 comment · 26 complexity · 4f568d6426ad2857b4af1a0df410e695 MD5 · raw file

  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  3. /**
  4. * WC_HTTPS class.
  5. *
  6. * @class WC_HTTPS
  7. * @version 2.1.0
  8. * @package WooCommerce/Classes
  9. * @category Class
  10. * @author WooThemes
  11. */
  12. class WC_HTTPS {
  13. /**
  14. * 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.
  15. */
  16. public function __construct() {
  17. if ( 'yes' == get_option( 'woocommerce_force_ssl_checkout' ) ) {
  18. 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' ) ) ) ) {
  19. // HTTPS urls with SSL on
  20. $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' );
  21. foreach ( $filters as $filter ) {
  22. add_filter( $filter, 'WC_HTTPS::force_https_url' );
  23. }
  24. add_filter( 'page_link', array( $this, 'force_https_page_link' ), 10, 2 );
  25. add_action( 'template_redirect', array( $this, 'force_https_template_redirect' ) );
  26. if ( get_option('woocommerce_unforce_ssl_checkout') == 'yes' )
  27. add_action( 'template_redirect', array( $this, 'unforce_https_template_redirect' ) );
  28. }
  29. }
  30. }
  31. /**
  32. * force_https_url function.
  33. *
  34. * @param mixed $content
  35. * @return string
  36. */
  37. public static function force_https_url( $content ) {
  38. if ( is_ssl() ) {
  39. if ( is_array( $content ) )
  40. $content = array_map( 'WC_HTTPS::force_https_url', $content );
  41. else
  42. $content = str_replace( 'http:', 'https:', $content );
  43. }
  44. return $content;
  45. }
  46. /**
  47. * Force a post link to be SSL if needed
  48. *
  49. * @param string $post_link
  50. * @param object $post
  51. * @return string
  52. */
  53. public function force_https_page_link( $link, $page_id ) {
  54. if ( in_array( $page_id, array( get_option( 'woocommerce_checkout_page_id' ), get_option( 'woocommerce_myaccount_page_id' ) ) ) ) {
  55. $link = str_replace( 'http:', 'https:', $link );
  56. } elseif ( get_option('woocommerce_unforce_ssl_checkout') == 'yes' ) {
  57. $link = str_replace( 'https:', 'http:', $link );
  58. }
  59. return $link;
  60. }
  61. /**
  62. * Template redirect - if we end up on a page ensure it has the correct http/https url
  63. */
  64. public function force_https_template_redirect() {
  65. if ( ! is_ssl() && ( is_checkout() || is_account_page() || apply_filters( 'woocommerce_force_ssl_checkout', false ) ) ) {
  66. if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
  67. wp_safe_redirect( preg_replace( '|^http://|', 'https://', $_SERVER['REQUEST_URI'] ) );
  68. exit;
  69. } else {
  70. wp_safe_redirect( 'https://' . ( ! empty( $_SERVER['HTTP_X_FORWARDED_HOST'] ) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $_SERVER['HTTP_HOST'] ) . $_SERVER['REQUEST_URI'] );
  71. exit;
  72. }
  73. }
  74. }
  75. /**
  76. * Template redirect - if we end up on a page ensure it has the correct http/https url
  77. */
  78. public function unforce_https_template_redirect() {
  79. if ( is_ssl() && $_SERVER['REQUEST_URI'] && ! is_checkout() && ! is_ajax() && ! is_account_page() && apply_filters( 'woocommerce_unforce_ssl_checkout', true ) ) {
  80. if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
  81. wp_safe_redirect( preg_replace( '|^https://|', 'http://', $_SERVER['REQUEST_URI'] ) );
  82. exit;
  83. } else {
  84. wp_safe_redirect( 'http://' . ( ! empty( $_SERVER['HTTP_X_FORWARDED_HOST'] ) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $_SERVER['HTTP_HOST'] ) . $_SERVER['REQUEST_URI'] );
  85. exit;
  86. }
  87. }
  88. }
  89. }
  90. new WC_HTTPS();