Completed
Push — master ( 19295b...ab5790 )
by Mike
08:48
created

WC_HTTPS   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 114
Duplicated Lines 12.28 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 35
c 1
b 0
f 0
lcom 0
cbo 0
dl 14
loc 114
rs 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 29 5
A force_https_url() 0 10 3
A force_https_page_link() 0 8 4
B force_https_template_redirect() 7 12 7
B unforce_https_template_redirect() 7 16 12
A http_api_curl() 0 5 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 16 and the first side effect is on line 4.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit; // Exit if accessed directly
5
}
6
7
/**
8
 * WC_HTTPS class.
9
 *
10
 * @class    WC_HTTPS
11
 * @version  2.2.0
12
 * @package  WooCommerce/Classes
13
 * @category Class
14
 * @author   WooThemes
15
 */
16
class WC_HTTPS {
17
18
	/**
19
	 * 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.
20
	 */
21
	public static function init() {
22
		if ( 'yes' === get_option( 'woocommerce_force_ssl_checkout' ) && ! is_admin() ) {
23
			// HTTPS urls with SSL on
24
			$filters = array(
25
				'post_thumbnail_html',
26
				'wp_get_attachment_image_attributes',
27
				'wp_get_attachment_url',
28
				'option_stylesheet_url',
29
				'option_template_url',
30
				'script_loader_src',
31
				'style_loader_src',
32
				'template_directory_uri',
33
				'stylesheet_directory_uri',
34
				'site_url'
35
			);
36
37
			foreach ( $filters as $filter ) {
38
				add_filter( $filter, array( __CLASS__, 'force_https_url' ), 999 );
39
			}
40
41
			add_filter( 'page_link', array( __CLASS__, 'force_https_page_link' ), 10, 2 );
42
			add_action( 'template_redirect', array( __CLASS__, 'force_https_template_redirect' ) );
43
44
			if ( 'yes' == get_option( 'woocommerce_unforce_ssl_checkout' ) ) {
45
				add_action( 'template_redirect', array( __CLASS__, 'unforce_https_template_redirect' ) );
46
			}
47
		}
48
		add_action( 'http_api_curl', array( __CLASS__, 'http_api_curl' ), 10, 3 );
49
	}
50
51
	/**
52
	 * Force https for urls.
53
	 *
54
	 * @param mixed $content
55
	 * @return string
56
	 */
57
	public static function force_https_url( $content ) {
58
		if ( is_ssl() ) {
59
			if ( is_array( $content ) ) {
60
				$content = array_map( 'WC_HTTPS::force_https_url', $content );
61
			} else {
62
				$content = str_replace( 'http:', 'https:', $content );
63
			}
64
		}
65
		return $content;
66
	}
67
68
	/**
69
	 * Force a post link to be SSL if needed.
70
	 *
71
	 * @return string
72
	 */
73
	public static function force_https_page_link( $link, $page_id ) {
74
		if ( in_array( $page_id, array( get_option( 'woocommerce_checkout_page_id' ), get_option( 'woocommerce_myaccount_page_id' ) ) ) ) {
75
			$link = str_replace( 'http:', 'https:', $link );
76
		} elseif ( 'yes' === get_option( 'woocommerce_unforce_ssl_checkout' ) && ! wc_site_is_https() ) {
77
			$link = str_replace( 'https:', 'http:', $link );
78
		}
79
		return $link;
80
	}
81
82
	/**
83
	 * Template redirect - if we end up on a page ensure it has the correct http/https url.
84
	 */
85
	public static function force_https_template_redirect() {
86
		if ( ! is_ssl() && ( is_checkout() || is_account_page() || apply_filters( 'woocommerce_force_ssl_checkout', false ) ) ) {
87
88 View Code Duplication
			if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
				wp_safe_redirect( preg_replace( '|^http://|', 'https://', $_SERVER['REQUEST_URI'] ) );
90
				exit;
91
			} else {
92
				wp_safe_redirect( 'https://' . ( ! empty( $_SERVER['HTTP_X_FORWARDED_HOST'] ) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $_SERVER['HTTP_HOST'] ) . $_SERVER['REQUEST_URI'] );
93
				exit;
94
			}
95
		}
96
	}
97
98
	/**
99
	 * Template redirect - if we end up on a page ensure it has the correct http/https url.
100
	 */
101
	public static function unforce_https_template_redirect() {
102
		if ( function_exists( 'is_customize_preview' ) && is_customize_preview() ) {
103
			return;
104
		}
105
106
		if ( ! wc_site_is_https() && is_ssl() && $_SERVER['REQUEST_URI'] && ! is_checkout() && ! is_ajax() && ! is_account_page() && apply_filters( 'woocommerce_unforce_ssl_checkout', true ) ) {
107
108 View Code Duplication
			if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
				wp_safe_redirect( preg_replace( '|^https://|', 'http://', $_SERVER['REQUEST_URI'] ) );
110
				exit;
111
			} else {
112
				wp_safe_redirect( 'http://' . ( ! empty( $_SERVER['HTTP_X_FORWARDED_HOST'] ) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $_SERVER['HTTP_HOST'] ) . $_SERVER['REQUEST_URI'] );
113
				exit;
114
			}
115
		}
116
	}
117
118
	/**
119
	 * Force posts to PayPal to use TLS v1.2. See:
120
	 * 		https://core.trac.wordpress.org/ticket/36320
121
	 * 		https://core.trac.wordpress.org/ticket/34924#comment:13
122
	 * 		https://www.paypal-knowledge.com/infocenter/index?page=content&widgetview=true&id=FAQ1914&viewlocale=en_US
123
	 */
124
	public static function http_api_curl( $handle, $r, $url ) {
125
		if ( strstr( $url, 'https://' ) && strstr( $url, '.paypal.com' ) && defined( 'CURL_SSLVERSION_TLSv1' ) ) {
126
			curl_setopt( $handle, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1 );
127
		}
128
	}
129
}
130
131
WC_HTTPS::init();
132