Completed
Branch FET-8385-datetime-ticket-selec... (cac7e5)
by
unknown
67:23 queued 43:13
created

InvalidCheckoutAccess::checkoutAccessIsInvalid()   D

Complexity

Conditions 10
Paths 17

Size

Total Lines 37
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 29
nc 17
nop 1
dl 0
loc 37
rs 4.8196
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace EventEspresso\modules\invalid_checkout_access;
3
4
if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) {
5
	exit( 'No direct script access allowed' );
6
}
7
8
9
10
/**
11
 * Class InvalidCheckoutAccessForm
12
 * Controls and tracks invalid access to the registration checkout page
13
 *
14
 * @package Event Espresso
15
 * @author  Brent Christensen
16
 * @since   4.9.17
17
 */
18
class InvalidCheckoutAccess {
19
20
	/**
21
	 * key used for saving invalid checkout access data to the wp_options table
22
	 */
23
	const OPTION_KEY = 'ee_invalid_checkout_access';
24
25
26
27
	/**
28
	 * _block_bots
29
	 * checks that the incoming request has either of the following set:
30
	 *  a uts (unix timestamp) which indicates that the request was redirected from the Ticket Selector
31
	 *  a REG URL Link, which indicates that the request is a return visit to SPCO for a valid TXN
32
	 * so if you're not coming from the Ticket Selector nor returning for a valid IP...
33
	 * then where you coming from man?
34
	 *
35
	 * @param \EE_Checkout $checkout
36
	 * @return bool true if access to registration checkout appears to be invalid
37
	 */
38
	public function checkoutAccessIsInvalid( \EE_Checkout $checkout ) {
39
		if (
40
			\EE_Config::instance()->registration->track_invalid_checkout_access()
41
			&& ! ( $checkout->uts || $checkout->reg_url_link )
42
			&& ! ( defined( 'DOING_AJAX' ) && DOING_AJAX )
43
		) {
44
			/** @var \EE_Request $request */
45
			$request = \EE_Registry::instance()->create( 'EE_Request' );
46
			$ip_address = $request->ip_address();
47
			$ee_bot_checkout = get_option( InvalidCheckoutAccess::OPTION_KEY );
48
			if ( $ee_bot_checkout === false ) {
49
				$ee_bot_checkout = array();
50
				add_option( InvalidCheckoutAccess::OPTION_KEY, $ee_bot_checkout, '', false );
51
			}
52
			if ( ! isset( $ee_bot_checkout[ $ip_address ] ) ) {
53
				$ee_bot_checkout[ $ip_address ] = array();
54
			}
55
			$http_referer = ( isset( $_SERVER['HTTP_REFERER'] ) )
56
				? esc_attr( $_SERVER['HTTP_REFERER'] )
57
				: 0;
58
			if ( ! isset( $ee_bot_checkout[ $ip_address ][ $http_referer ] ) ) {
59
				$ee_bot_checkout[ $ip_address ][ $http_referer ] = 0;
60
			}
61
			$ee_bot_checkout[ $ip_address ][ $http_referer ]++;
62
			update_option( InvalidCheckoutAccess::OPTION_KEY, $ee_bot_checkout );
63
			$checkout->redirect = true;
64
			$checkout->redirect_url = get_post_type_archive_link('espresso_events');
65
            \EE_Error::add_error(
66
                esc_html__('Direct access to the registration checkout page is not allowed.', 'event_espresso'),
67
                __FILE__,
68
                __FUNCTION__,
69
                __LINE__
70
            );
71
			return true;
72
		}
73
		return false;
74
	}
75
76
77
	/**
78
	 * _invalid_checkout_access_form
79
	 *
80
	 * @return \EE_Form_Section_Proper
81
	 * @throws \EE_Error
82
	 */
83
	public function getForm() {
84
		return new \EE_Form_Section_Proper(
85
			array(
86
				'name'            => 'invalid_checkout_access',
87
				'html_id'         => 'invalid_checkout_access',
88
				'layout_strategy' => new \EE_Admin_Two_Column_Layout(),
89
				'subsections'     => array(
90
					'invalid_checkout_access_hdr'   => new \EE_Form_Section_HTML(
91
						\EEH_HTML::h2( esc_html__( 'Invalid Checkout Access', 'event_espresso' ) )
92
					),
93
					'ee_bot_checkout_data'          => new \EE_Text_Area_Input(
94
						array(
95
							'html_label_text' => esc_html__( 'Invalid Checkout Data', 'event_espresso' ),
96
							'default'         => var_export(
97
								get_option( InvalidCheckoutAccess::OPTION_KEY, array() ),
98
								true
99
							),
100
							'required'        => false,
101
							'html_help_text'  => esc_html__(
102
								'Event Espresso blocks any attempt to directly access the registration checkout page, that is NOT from a Ticket Selector or for a return visit for a valid transaction. These are not valid requests accessing your checkout page, so we track the IP addresses, what web page they just came from, and the number of times that they have attempted to access your registration page. This information may help you with protecting your site by other means, such as firewalls, etc, but please note that IP addresses are almost guaranteed to be spoofed by malicious agents.',
103
								'event_espresso'
104
							)
105
						)
106
					),
107
					'track_invalid_checkout_access' => new \EE_Yes_No_Input(
108
						array(
109
							'html_label_text'         => __( 'Track Invalid Checkout Access?', 'event_espresso' ),
110
							'html_help_text'          => esc_html__(
111
								'Controls whether or not invalid attempts to directly access the registration checkout page should be tracked. Setting this to "No" means that the above data will no longer be collected.',
112
								'event_espresso'
113
							),
114
							'default'                 => \EE_Config::instance()
115
															->registration
116
															->track_invalid_checkout_access(),
117
							'display_html_label_text' => false
118
						)
119
					),
120
					'delete_invalid_checkout_data'  => new \EE_Yes_No_Input(
121
						array(
122
							'html_label_text'         => __( 'Reset Invalid Checkout Data', 'event_espresso' ),
123
							'html_help_text'          => esc_html__(
124
								'Setting this to "Yes" will delete all existing invalid checkout access data.',
125
								'event_espresso'
126
							),
127
							'default'                 => false,
128
							'display_html_label_text' => false
129
						)
130
					),
131
				)
132
			)
133
		);
134
	}
135
136
137
138
	/**
139
	 * update_invalid_checkout_access_form
140
	 *
141
	 * @param \EE_Registration_Config $EE_Registration_Config
142
	 * @return \EE_Registration_Config
143
	 */
144
	public function processForm( \EE_Registration_Config $EE_Registration_Config ) {
145
		try {
146
			$invalid_checkout_access_form = $this->getForm();
147
			// if not displaying a form, then check for form submission
148
			if ( $invalid_checkout_access_form->was_submitted() ) {
149
				// capture form data
150
				$invalid_checkout_access_form->receive_form_submission();
151
				// validate form data
152
				if ( $invalid_checkout_access_form->is_valid() ) {
153
					// grab validated data from form
154
					$valid_data = $invalid_checkout_access_form->valid_data();
155
					// ensure form inputs we want are set
156
					if (
157
						isset(
158
							$valid_data['track_invalid_checkout_access'],
159
							$valid_data['delete_invalid_checkout_data']
160
						)
161
					) {
162
						$EE_Registration_Config->set_track_invalid_checkout_access(
163
							$valid_data['track_invalid_checkout_access']
164
						);
165
						// if deleting, then update option with empty array
166
						if ( filter_var( $valid_data['delete_invalid_checkout_data'], FILTER_VALIDATE_BOOLEAN ) ) {
167
							update_option( InvalidCheckoutAccess::OPTION_KEY, array() );
168
						}
169
					} else {
170
						\EE_Error::add_error(
171
							esc_html__(
172
								'Invalid or missing Invalid Checkout Access form data. Please refresh the form and try again.',
173
								'event_espresso'
174
							),
175
							__FILE__,
176
							__FUNCTION__,
177
							__LINE__
178
						);
179
					}
180
				} else {
181
					if ( $invalid_checkout_access_form->submission_error_message() !== '' ) {
182
						\EE_Error::add_error(
183
							$invalid_checkout_access_form->submission_error_message(),
184
							__FILE__,
185
							__FUNCTION__,
186
							__LINE__
187
						);
188
					}
189
				}
190
			}
191
		} catch ( \EE_Error $e ) {
192
			$e->get_error();
193
		}
194
		return $EE_Registration_Config;
195
	}
196
197
}
198
// End of file InvalidCheckoutAccess.php
199
// Location: EventEspresso\modules\invalid_checkout_access/InvalidCheckoutAccess.php