Completed
Branch master (c6d524)
by
unknown
08:28 queued 06:39
created
core/helpers/EEH_URL.helper.php 2 patches
Indentation   +252 added lines, -252 removed lines patch added patch discarded remove patch
@@ -14,280 +14,280 @@
 block discarded – undo
14 14
 class EEH_URL
15 15
 {
16 16
 
17
-    /**
18
-     * _add_query_arg
19
-     * adds nonce to array of arguments then calls WP add_query_arg function
20
-     *
21
-     * @access public
22
-     * @param array  $args
23
-     * @param string $url
24
-     * @param bool   $exclude_nonce If true then the nonce will be excluded from the generated url.
25
-     * @return string
26
-     */
27
-    public static function add_query_args_and_nonce($args = [], $url = '', $exclude_nonce = false)
28
-    {
29
-        // check that an action exists and add nonce
30
-        if (! $exclude_nonce) {
31
-            if (isset($args['action']) && ! empty($args['action'])) {
32
-                $args = array_merge(
33
-                    $args,
34
-                    [
35
-                        $args['action'] . '_nonce' => wp_create_nonce($args['action'] . '_nonce'),
36
-                    ]
37
-                );
38
-            } else {
39
-                $args = array_merge(
40
-                    $args,
41
-                    [
42
-                        'action'        => 'default',
43
-                        'default_nonce' => wp_create_nonce('default_nonce'),
44
-                    ]
45
-                );
46
-            }
47
-        }
17
+	/**
18
+	 * _add_query_arg
19
+	 * adds nonce to array of arguments then calls WP add_query_arg function
20
+	 *
21
+	 * @access public
22
+	 * @param array  $args
23
+	 * @param string $url
24
+	 * @param bool   $exclude_nonce If true then the nonce will be excluded from the generated url.
25
+	 * @return string
26
+	 */
27
+	public static function add_query_args_and_nonce($args = [], $url = '', $exclude_nonce = false)
28
+	{
29
+		// check that an action exists and add nonce
30
+		if (! $exclude_nonce) {
31
+			if (isset($args['action']) && ! empty($args['action'])) {
32
+				$args = array_merge(
33
+					$args,
34
+					[
35
+						$args['action'] . '_nonce' => wp_create_nonce($args['action'] . '_nonce'),
36
+					]
37
+				);
38
+			} else {
39
+				$args = array_merge(
40
+					$args,
41
+					[
42
+						'action'        => 'default',
43
+						'default_nonce' => wp_create_nonce('default_nonce'),
44
+					]
45
+				);
46
+			}
47
+		}
48 48
 
49
-        $action  = EEH_URL::getRequest()->getRequestParam('action');
50
-        // finally, let's always add a return address (if present) :)
51
-        if ($action !== '') {
52
-            $args['return'] = $action;
53
-        }
49
+		$action  = EEH_URL::getRequest()->getRequestParam('action');
50
+		// finally, let's always add a return address (if present) :)
51
+		if ($action !== '') {
52
+			$args['return'] = $action;
53
+		}
54 54
 
55
-        return add_query_arg($args, $url);
56
-    }
55
+		return add_query_arg($args, $url);
56
+	}
57 57
 
58 58
 
59
-    /**
60
-     * Returns whether not the remote file exists.
61
-     * Checking via GET because HEAD requests are blocked on some server configurations.
62
-     *
63
-     * @param string $url
64
-     * @param array  $args the arguments that should be passed through to the wp_remote_request call.
65
-     * @return boolean
66
-     */
67
-    public static function remote_file_exists($url, $args = [])
68
-    {
69
-        $results = wp_remote_request(
70
-            $url,
71
-            array_merge(
72
-                [
73
-                    'method'      => 'GET',
74
-                    'redirection' => 1,
75
-                ],
76
-                $args
77
-            )
78
-        );
79
-        return ! $results instanceof WP_Error
80
-               && isset($results['response']['code'])
81
-               && $results['response']['code'] == '200';
82
-    }
59
+	/**
60
+	 * Returns whether not the remote file exists.
61
+	 * Checking via GET because HEAD requests are blocked on some server configurations.
62
+	 *
63
+	 * @param string $url
64
+	 * @param array  $args the arguments that should be passed through to the wp_remote_request call.
65
+	 * @return boolean
66
+	 */
67
+	public static function remote_file_exists($url, $args = [])
68
+	{
69
+		$results = wp_remote_request(
70
+			$url,
71
+			array_merge(
72
+				[
73
+					'method'      => 'GET',
74
+					'redirection' => 1,
75
+				],
76
+				$args
77
+			)
78
+		);
79
+		return ! $results instanceof WP_Error
80
+			   && isset($results['response']['code'])
81
+			   && $results['response']['code'] == '200';
82
+	}
83 83
 
84 84
 
85
-    /**
86
-     * refactor_url
87
-     * primarily used for removing the query string from a URL
88
-     *
89
-     * @param string $url
90
-     * @param bool   $remove_query  - TRUE (default) will strip off any URL params, ie: ?this=1&that=2
91
-     * @param bool   $base_url_only - TRUE will only return the scheme and host with no other parameters
92
-     * @return string
93
-     */
94
-    public static function refactor_url($url = '', $remove_query = true, $base_url_only = false)
95
-    {
96
-        // break apart incoming URL
97
-        $url_bits = parse_url($url);
98
-        // HTTP or HTTPS ?
99
-        $scheme = isset($url_bits['scheme']) ? $url_bits['scheme'] . '://' : 'https://';
100
-        // domain
101
-        $host = isset($url_bits['host']) ? $url_bits['host'] : '';
102
-        // if only the base URL is requested, then return that now
103
-        if ($base_url_only) {
104
-            return $scheme . $host;
105
-        }
106
-        $port = isset($url_bits['port']) ? ':' . $url_bits['port'] : '';
107
-        $user = isset($url_bits['user']) ? $url_bits['user'] : '';
108
-        $pass = isset($url_bits['pass']) ? ':' . $url_bits['pass'] : '';
109
-        $pass = ($user || $pass) ? $pass . '@' : '';
110
-        $path = isset($url_bits['path']) ? $url_bits['path'] : '';
111
-        // if the query string is not required, then return what we have so far
112
-        if ($remove_query) {
113
-            return $scheme . $user . $pass . $host . $port . $path;
114
-        }
115
-        $query    = isset($url_bits['query']) ? '?' . $url_bits['query'] : '';
116
-        $fragment = isset($url_bits['fragment']) ? '#' . $url_bits['fragment'] : '';
117
-        return $scheme . $user . $pass . $host . $port . $path . $query . $fragment;
118
-    }
85
+	/**
86
+	 * refactor_url
87
+	 * primarily used for removing the query string from a URL
88
+	 *
89
+	 * @param string $url
90
+	 * @param bool   $remove_query  - TRUE (default) will strip off any URL params, ie: ?this=1&that=2
91
+	 * @param bool   $base_url_only - TRUE will only return the scheme and host with no other parameters
92
+	 * @return string
93
+	 */
94
+	public static function refactor_url($url = '', $remove_query = true, $base_url_only = false)
95
+	{
96
+		// break apart incoming URL
97
+		$url_bits = parse_url($url);
98
+		// HTTP or HTTPS ?
99
+		$scheme = isset($url_bits['scheme']) ? $url_bits['scheme'] . '://' : 'https://';
100
+		// domain
101
+		$host = isset($url_bits['host']) ? $url_bits['host'] : '';
102
+		// if only the base URL is requested, then return that now
103
+		if ($base_url_only) {
104
+			return $scheme . $host;
105
+		}
106
+		$port = isset($url_bits['port']) ? ':' . $url_bits['port'] : '';
107
+		$user = isset($url_bits['user']) ? $url_bits['user'] : '';
108
+		$pass = isset($url_bits['pass']) ? ':' . $url_bits['pass'] : '';
109
+		$pass = ($user || $pass) ? $pass . '@' : '';
110
+		$path = isset($url_bits['path']) ? $url_bits['path'] : '';
111
+		// if the query string is not required, then return what we have so far
112
+		if ($remove_query) {
113
+			return $scheme . $user . $pass . $host . $port . $path;
114
+		}
115
+		$query    = isset($url_bits['query']) ? '?' . $url_bits['query'] : '';
116
+		$fragment = isset($url_bits['fragment']) ? '#' . $url_bits['fragment'] : '';
117
+		return $scheme . $user . $pass . $host . $port . $path . $query . $fragment;
118
+	}
119 119
 
120 120
 
121
-    /**
122
-     * get_query_string
123
-     * returns just the query string from a URL, formatted by default into an array of key value pairs
124
-     *
125
-     * @param string $url
126
-     * @param bool   $as_array TRUE (default) will return query params as an array of key value pairs, FALSE will
127
-     *                         simply return the query string
128
-     * @return string|array
129
-     */
130
-    public static function get_query_string($url = '', $as_array = true)
131
-    {
132
-        // decode, then break apart incoming URL
133
-        $url_bits = parse_url(html_entity_decode($url));
134
-        // grab query string from URL
135
-        $query = isset($url_bits['query']) ? $url_bits['query'] : '';
136
-        // if we don't want the query string formatted into an array of key => value pairs, then just return it as is
137
-        if (! $as_array) {
138
-            return $query;
139
-        }
140
-        // if no query string exists then just return an empty array now
141
-        if (empty($query)) {
142
-            return [];
143
-        }
144
-        // empty array to hold results
145
-        $query_params = [];
146
-        // now break apart the query string into separate params
147
-        $query = explode('&', $query);
148
-        // loop thru our query params
149
-        foreach ($query as $query_args) {
150
-            // break apart the key value pairs
151
-            $query_args = explode('=', $query_args);
152
-            // and add to our results array
153
-            $query_params[ $query_args[0] ] = $query_args[1];
154
-        }
155
-        return $query_params;
156
-    }
121
+	/**
122
+	 * get_query_string
123
+	 * returns just the query string from a URL, formatted by default into an array of key value pairs
124
+	 *
125
+	 * @param string $url
126
+	 * @param bool   $as_array TRUE (default) will return query params as an array of key value pairs, FALSE will
127
+	 *                         simply return the query string
128
+	 * @return string|array
129
+	 */
130
+	public static function get_query_string($url = '', $as_array = true)
131
+	{
132
+		// decode, then break apart incoming URL
133
+		$url_bits = parse_url(html_entity_decode($url));
134
+		// grab query string from URL
135
+		$query = isset($url_bits['query']) ? $url_bits['query'] : '';
136
+		// if we don't want the query string formatted into an array of key => value pairs, then just return it as is
137
+		if (! $as_array) {
138
+			return $query;
139
+		}
140
+		// if no query string exists then just return an empty array now
141
+		if (empty($query)) {
142
+			return [];
143
+		}
144
+		// empty array to hold results
145
+		$query_params = [];
146
+		// now break apart the query string into separate params
147
+		$query = explode('&', $query);
148
+		// loop thru our query params
149
+		foreach ($query as $query_args) {
150
+			// break apart the key value pairs
151
+			$query_args = explode('=', $query_args);
152
+			// and add to our results array
153
+			$query_params[ $query_args[0] ] = $query_args[1];
154
+		}
155
+		return $query_params;
156
+	}
157 157
 
158 158
 
159
-    /**
160
-     * prevent_prefetching
161
-     *
162
-     * @return void
163
-     */
164
-    public static function prevent_prefetching()
165
-    {
166
-        // prevent browsers from prefetching of the rel='next' link, because it may contain content that interferes
167
-        // with the registration process
168
-        remove_action('wp_head', 'adjacent_posts_rel_link_wp_head');
169
-    }
159
+	/**
160
+	 * prevent_prefetching
161
+	 *
162
+	 * @return void
163
+	 */
164
+	public static function prevent_prefetching()
165
+	{
166
+		// prevent browsers from prefetching of the rel='next' link, because it may contain content that interferes
167
+		// with the registration process
168
+		remove_action('wp_head', 'adjacent_posts_rel_link_wp_head');
169
+	}
170 170
 
171 171
 
172
-    /**
173
-     * This generates a unique site-specific string.
174
-     * An example usage for this string would be to save as a unique identifier for a record in the db for usage in
175
-     * urls.
176
-     *
177
-     * @param string $prefix Use this to prefix the string with something.
178
-     * @return string
179
-     */
180
-    public static function generate_unique_token($prefix = '')
181
-    {
182
-        $token = md5(uniqid() . mt_rand());
183
-        return $prefix ? $prefix . '_' . $token : $token;
184
-    }
172
+	/**
173
+	 * This generates a unique site-specific string.
174
+	 * An example usage for this string would be to save as a unique identifier for a record in the db for usage in
175
+	 * urls.
176
+	 *
177
+	 * @param string $prefix Use this to prefix the string with something.
178
+	 * @return string
179
+	 */
180
+	public static function generate_unique_token($prefix = '')
181
+	{
182
+		$token = md5(uniqid() . mt_rand());
183
+		return $prefix ? $prefix . '_' . $token : $token;
184
+	}
185 185
 
186 186
 
187
-    /**
188
-     * filter_input_server_url
189
-     * uses filter_input() to sanitize one of the INPUT_SERVER URL values
190
-     * but adds a backup in case filter_input() returns nothing, which can erringly happen on some servers
191
-     *
192
-     * @param string $server_variable
193
-     * @return string
194
-     */
195
-    public static function filter_input_server_url($server_variable = 'REQUEST_URI')
196
-    {
197
-        $URL              = '';
198
-        $server_variables = [
199
-            'REQUEST_URI' => 1,
200
-            'HTTP_HOST'   => 1,
201
-            'PHP_SELF'    => 1,
202
-        ];
203
-        $server_variable  = strtoupper($server_variable);
204
-        // whitelist INPUT_SERVER var
205
-        if (isset($server_variables[ $server_variable ])) {
206
-            $URL = filter_input(INPUT_SERVER, $server_variable, FILTER_SANITIZE_URL, FILTER_NULL_ON_FAILURE);
207
-            if (empty($URL) || $URL !== $_SERVER[ $server_variable ]) {
208
-                // fallback sanitization if the above fails or URL has changed after filtering
209
-                $URL = wp_sanitize_redirect($_SERVER[ $server_variable ]);
210
-            }
211
-        }
212
-        return $URL;
213
-    }
187
+	/**
188
+	 * filter_input_server_url
189
+	 * uses filter_input() to sanitize one of the INPUT_SERVER URL values
190
+	 * but adds a backup in case filter_input() returns nothing, which can erringly happen on some servers
191
+	 *
192
+	 * @param string $server_variable
193
+	 * @return string
194
+	 */
195
+	public static function filter_input_server_url($server_variable = 'REQUEST_URI')
196
+	{
197
+		$URL              = '';
198
+		$server_variables = [
199
+			'REQUEST_URI' => 1,
200
+			'HTTP_HOST'   => 1,
201
+			'PHP_SELF'    => 1,
202
+		];
203
+		$server_variable  = strtoupper($server_variable);
204
+		// whitelist INPUT_SERVER var
205
+		if (isset($server_variables[ $server_variable ])) {
206
+			$URL = filter_input(INPUT_SERVER, $server_variable, FILTER_SANITIZE_URL, FILTER_NULL_ON_FAILURE);
207
+			if (empty($URL) || $URL !== $_SERVER[ $server_variable ]) {
208
+				// fallback sanitization if the above fails or URL has changed after filtering
209
+				$URL = wp_sanitize_redirect($_SERVER[ $server_variable ]);
210
+			}
211
+		}
212
+		return $URL;
213
+	}
214 214
 
215 215
 
216
-    /**
217
-     * Gets the current page's full URL.
218
-     *
219
-     * @return string
220
-     */
221
-    public static function current_url()
222
-    {
223
-        $url = '';
224
-        if (
225
-            EEH_URL::getRequest()->serverParamIsSet('HTTP_HOST')
226
-            && EEH_URL::getRequest()->serverParamIsSet('REQUEST_URI')
227
-        ) {
228
-            $url = is_ssl() ? 'https://' : 'http://';
229
-            $url .= EEH_URL::filter_input_server_url('HTTP_HOST');
230
-            $url .= EEH_URL::filter_input_server_url();
231
-        }
232
-        return $url;
233
-    }
216
+	/**
217
+	 * Gets the current page's full URL.
218
+	 *
219
+	 * @return string
220
+	 */
221
+	public static function current_url()
222
+	{
223
+		$url = '';
224
+		if (
225
+			EEH_URL::getRequest()->serverParamIsSet('HTTP_HOST')
226
+			&& EEH_URL::getRequest()->serverParamIsSet('REQUEST_URI')
227
+		) {
228
+			$url = is_ssl() ? 'https://' : 'http://';
229
+			$url .= EEH_URL::filter_input_server_url('HTTP_HOST');
230
+			$url .= EEH_URL::filter_input_server_url();
231
+		}
232
+		return $url;
233
+	}
234 234
 
235 235
 
236
-    /**
237
-     * Identical in functionality to EEH_current_url except it removes any provided query_parameters from it.
238
-     *
239
-     * @param array $query_parameters An array of query_parameters to remove from the current url.
240
-     * @return string
241
-     * @since 4.9.46.rc.029
242
-     */
243
-    public static function current_url_without_query_paramaters(array $query_parameters)
244
-    {
245
-        return remove_query_arg($query_parameters, EEH_URL::current_url());
246
-    }
236
+	/**
237
+	 * Identical in functionality to EEH_current_url except it removes any provided query_parameters from it.
238
+	 *
239
+	 * @param array $query_parameters An array of query_parameters to remove from the current url.
240
+	 * @return string
241
+	 * @since 4.9.46.rc.029
242
+	 */
243
+	public static function current_url_without_query_paramaters(array $query_parameters)
244
+	{
245
+		return remove_query_arg($query_parameters, EEH_URL::current_url());
246
+	}
247 247
 
248 248
 
249
-    /**
250
-     * @param string $location
251
-     * @param int    $status
252
-     * @param string $exit_notice
253
-     */
254
-    public static function safeRedirectAndExit($location, $status = 302, $exit_notice = '')
255
-    {
256
-        EE_Error::get_notices(false, true);
257
-        wp_safe_redirect($location, $status);
258
-        exit($exit_notice);
259
-    }
249
+	/**
250
+	 * @param string $location
251
+	 * @param int    $status
252
+	 * @param string $exit_notice
253
+	 */
254
+	public static function safeRedirectAndExit($location, $status = 302, $exit_notice = '')
255
+	{
256
+		EE_Error::get_notices(false, true);
257
+		wp_safe_redirect($location, $status);
258
+		exit($exit_notice);
259
+	}
260 260
 
261 261
 
262
-    /**
263
-     * Slugifies text for usage in a URL.
264
-     *
265
-     * Currently, this isn't just calling `sanitize_title()` on it, because that percent-encodes unicode characters,
266
-     * and WordPress chokes on them when used as CPT and custom taxonomy slugs.
267
-     *
268
-     * @param string $text
269
-     * @param string $fallback
270
-     * @return string which can be used in a URL
271
-     * @since 4.9.66.p
272
-     */
273
-    public static function slugify($text, $fallback)
274
-    {
275
-        // url decode after sanitizing title to restore unicode characters,
276
-        // see https://github.com/eventespresso/event-espresso-core/issues/575
277
-        return urldecode(sanitize_title($text, $fallback));
278
-    }
262
+	/**
263
+	 * Slugifies text for usage in a URL.
264
+	 *
265
+	 * Currently, this isn't just calling `sanitize_title()` on it, because that percent-encodes unicode characters,
266
+	 * and WordPress chokes on them when used as CPT and custom taxonomy slugs.
267
+	 *
268
+	 * @param string $text
269
+	 * @param string $fallback
270
+	 * @return string which can be used in a URL
271
+	 * @since 4.9.66.p
272
+	 */
273
+	public static function slugify($text, $fallback)
274
+	{
275
+		// url decode after sanitizing title to restore unicode characters,
276
+		// see https://github.com/eventespresso/event-espresso-core/issues/575
277
+		return urldecode(sanitize_title($text, $fallback));
278
+	}
279 279
 
280 280
 
281
-    /**
282
-     * @return RequestInterface
283
-     * @since   4.10.14.p
284
-     */
285
-    protected static function getRequest()
286
-    {
287
-        static $request;
288
-        if (! $request instanceof RequestInterface) {
289
-            $request = LoaderFactory::getLoader()->getShared(RequestInterface::class);
290
-        }
291
-        return $request;
292
-    }
281
+	/**
282
+	 * @return RequestInterface
283
+	 * @since   4.10.14.p
284
+	 */
285
+	protected static function getRequest()
286
+	{
287
+		static $request;
288
+		if (! $request instanceof RequestInterface) {
289
+			$request = LoaderFactory::getLoader()->getShared(RequestInterface::class);
290
+		}
291
+		return $request;
292
+	}
293 293
 }
Please login to merge, or discard this patch.
Spacing   +21 added lines, -21 removed lines patch added patch discarded remove patch
@@ -27,12 +27,12 @@  discard block
 block discarded – undo
27 27
     public static function add_query_args_and_nonce($args = [], $url = '', $exclude_nonce = false)
28 28
     {
29 29
         // check that an action exists and add nonce
30
-        if (! $exclude_nonce) {
30
+        if ( ! $exclude_nonce) {
31 31
             if (isset($args['action']) && ! empty($args['action'])) {
32 32
                 $args = array_merge(
33 33
                     $args,
34 34
                     [
35
-                        $args['action'] . '_nonce' => wp_create_nonce($args['action'] . '_nonce'),
35
+                        $args['action'].'_nonce' => wp_create_nonce($args['action'].'_nonce'),
36 36
                     ]
37 37
                 );
38 38
             } else {
@@ -46,7 +46,7 @@  discard block
 block discarded – undo
46 46
             }
47 47
         }
48 48
 
49
-        $action  = EEH_URL::getRequest()->getRequestParam('action');
49
+        $action = EEH_URL::getRequest()->getRequestParam('action');
50 50
         // finally, let's always add a return address (if present) :)
51 51
         if ($action !== '') {
52 52
             $args['return'] = $action;
@@ -96,25 +96,25 @@  discard block
 block discarded – undo
96 96
         // break apart incoming URL
97 97
         $url_bits = parse_url($url);
98 98
         // HTTP or HTTPS ?
99
-        $scheme = isset($url_bits['scheme']) ? $url_bits['scheme'] . '://' : 'https://';
99
+        $scheme = isset($url_bits['scheme']) ? $url_bits['scheme'].'://' : 'https://';
100 100
         // domain
101 101
         $host = isset($url_bits['host']) ? $url_bits['host'] : '';
102 102
         // if only the base URL is requested, then return that now
103 103
         if ($base_url_only) {
104
-            return $scheme . $host;
104
+            return $scheme.$host;
105 105
         }
106
-        $port = isset($url_bits['port']) ? ':' . $url_bits['port'] : '';
106
+        $port = isset($url_bits['port']) ? ':'.$url_bits['port'] : '';
107 107
         $user = isset($url_bits['user']) ? $url_bits['user'] : '';
108
-        $pass = isset($url_bits['pass']) ? ':' . $url_bits['pass'] : '';
109
-        $pass = ($user || $pass) ? $pass . '@' : '';
108
+        $pass = isset($url_bits['pass']) ? ':'.$url_bits['pass'] : '';
109
+        $pass = ($user || $pass) ? $pass.'@' : '';
110 110
         $path = isset($url_bits['path']) ? $url_bits['path'] : '';
111 111
         // if the query string is not required, then return what we have so far
112 112
         if ($remove_query) {
113
-            return $scheme . $user . $pass . $host . $port . $path;
113
+            return $scheme.$user.$pass.$host.$port.$path;
114 114
         }
115
-        $query    = isset($url_bits['query']) ? '?' . $url_bits['query'] : '';
116
-        $fragment = isset($url_bits['fragment']) ? '#' . $url_bits['fragment'] : '';
117
-        return $scheme . $user . $pass . $host . $port . $path . $query . $fragment;
115
+        $query    = isset($url_bits['query']) ? '?'.$url_bits['query'] : '';
116
+        $fragment = isset($url_bits['fragment']) ? '#'.$url_bits['fragment'] : '';
117
+        return $scheme.$user.$pass.$host.$port.$path.$query.$fragment;
118 118
     }
119 119
 
120 120
 
@@ -134,7 +134,7 @@  discard block
 block discarded – undo
134 134
         // grab query string from URL
135 135
         $query = isset($url_bits['query']) ? $url_bits['query'] : '';
136 136
         // if we don't want the query string formatted into an array of key => value pairs, then just return it as is
137
-        if (! $as_array) {
137
+        if ( ! $as_array) {
138 138
             return $query;
139 139
         }
140 140
         // if no query string exists then just return an empty array now
@@ -150,7 +150,7 @@  discard block
 block discarded – undo
150 150
             // break apart the key value pairs
151 151
             $query_args = explode('=', $query_args);
152 152
             // and add to our results array
153
-            $query_params[ $query_args[0] ] = $query_args[1];
153
+            $query_params[$query_args[0]] = $query_args[1];
154 154
         }
155 155
         return $query_params;
156 156
     }
@@ -179,8 +179,8 @@  discard block
 block discarded – undo
179 179
      */
180 180
     public static function generate_unique_token($prefix = '')
181 181
     {
182
-        $token = md5(uniqid() . mt_rand());
183
-        return $prefix ? $prefix . '_' . $token : $token;
182
+        $token = md5(uniqid().mt_rand());
183
+        return $prefix ? $prefix.'_'.$token : $token;
184 184
     }
185 185
 
186 186
 
@@ -200,13 +200,13 @@  discard block
 block discarded – undo
200 200
             'HTTP_HOST'   => 1,
201 201
             'PHP_SELF'    => 1,
202 202
         ];
203
-        $server_variable  = strtoupper($server_variable);
203
+        $server_variable = strtoupper($server_variable);
204 204
         // whitelist INPUT_SERVER var
205
-        if (isset($server_variables[ $server_variable ])) {
205
+        if (isset($server_variables[$server_variable])) {
206 206
             $URL = filter_input(INPUT_SERVER, $server_variable, FILTER_SANITIZE_URL, FILTER_NULL_ON_FAILURE);
207
-            if (empty($URL) || $URL !== $_SERVER[ $server_variable ]) {
207
+            if (empty($URL) || $URL !== $_SERVER[$server_variable]) {
208 208
                 // fallback sanitization if the above fails or URL has changed after filtering
209
-                $URL = wp_sanitize_redirect($_SERVER[ $server_variable ]);
209
+                $URL = wp_sanitize_redirect($_SERVER[$server_variable]);
210 210
             }
211 211
         }
212 212
         return $URL;
@@ -285,7 +285,7 @@  discard block
 block discarded – undo
285 285
     protected static function getRequest()
286 286
     {
287 287
         static $request;
288
-        if (! $request instanceof RequestInterface) {
288
+        if ( ! $request instanceof RequestInterface) {
289 289
             $request = LoaderFactory::getLoader()->getShared(RequestInterface::class);
290 290
         }
291 291
         return $request;
Please login to merge, or discard this patch.
core/libraries/form_sections/form_handlers/SequentialStepFormManager.php 1 patch
Indentation   +579 added lines, -579 removed lines patch added patch discarded remove patch
@@ -30,583 +30,583 @@
 block discarded – undo
30 30
 abstract class SequentialStepFormManager
31 31
 {
32 32
 
33
-    /**
34
-     * a simplified URL with no form related parameters
35
-     * that will be used to build the form's redirect URLs
36
-     *
37
-     * @var string $base_url
38
-     */
39
-    private $base_url = '';
40
-
41
-    /**
42
-     * the key used for the URL param that denotes the current form step
43
-     * defaults to 'ee-form-step'
44
-     *
45
-     * @var string $form_step_url_key
46
-     */
47
-    private $form_step_url_key = '';
48
-
49
-    /**
50
-     * @var string $default_form_step
51
-     */
52
-    private $default_form_step = '';
53
-
54
-    /**
55
-     * @var string $form_action
56
-     */
57
-    private $form_action;
58
-
59
-    /**
60
-     * value of one of the string constant above
61
-     *
62
-     * @var string $form_config
63
-     */
64
-    private $form_config;
65
-
66
-    /**
67
-     * @var string $progress_step_style
68
-     */
69
-    private $progress_step_style = '';
70
-
71
-    /**
72
-     * @var RequestInterface $request
73
-     */
74
-    private $request;
75
-
76
-    /**
77
-     * @var Collection $form_steps
78
-     */
79
-    protected $form_steps;
80
-
81
-    /**
82
-     * @var ProgressStepManager $progress_step_manager
83
-     */
84
-    protected $progress_step_manager;
85
-
86
-
87
-    /**
88
-     * @return Collection|null
89
-     */
90
-    abstract protected function getFormStepsCollection();
91
-
92
-    // phpcs:disable PEAR.Functions.ValidDefaultValue.NotAtEnd
93
-    /**
94
-     * StepsManager constructor
95
-     *
96
-     * @param string                           $base_url
97
-     * @param string                           $default_form_step
98
-     * @param string                           $form_action
99
-     * @param string                           $form_config
100
-     * @param EE_Request|RequestInterface|null $request
101
-     * @param string                           $progress_step_style
102
-     * @throws InvalidDataTypeException
103
-     * @throws InvalidArgumentException
104
-     */
105
-    public function __construct(
106
-        $base_url,
107
-        $default_form_step,
108
-        $form_action = '',
109
-        $form_config = FormHandler::ADD_FORM_TAGS_AND_SUBMIT,
110
-        $progress_step_style = 'number_bubbles',
111
-        $request = null
112
-    ) {
113
-        $this->setBaseUrl($base_url);
114
-        $this->setDefaultFormStep($default_form_step);
115
-        $this->setFormAction($form_action);
116
-        $this->setFormConfig($form_config);
117
-        $this->setProgressStepStyle($progress_step_style);
118
-        $this->request = $request instanceof RequestInterface
119
-            ? $request
120
-            : LoaderFactory::getLoader()->getShared('EventEspresso\core\services\request\RequestInterface');
121
-    }
122
-
123
-
124
-    /**
125
-     * @return string
126
-     * @throws InvalidFormHandlerException
127
-     */
128
-    public function baseUrl()
129
-    {
130
-        if (strpos($this->base_url, $this->getCurrentStep()->slug()) === false) {
131
-            add_query_arg(
132
-                array($this->form_step_url_key => $this->getCurrentStep()->slug()),
133
-                $this->base_url
134
-            );
135
-        }
136
-        return $this->base_url;
137
-    }
138
-
139
-
140
-    /**
141
-     * @param string $base_url
142
-     * @throws InvalidDataTypeException
143
-     * @throws InvalidArgumentException
144
-     */
145
-    protected function setBaseUrl($base_url)
146
-    {
147
-        if (! is_string($base_url)) {
148
-            throw new InvalidDataTypeException('$base_url', $base_url, 'string');
149
-        }
150
-        if (empty($base_url)) {
151
-            throw new InvalidArgumentException(
152
-                esc_html__('The base URL can not be an empty string.', 'event_espresso')
153
-            );
154
-        }
155
-        $this->base_url = $base_url;
156
-    }
157
-
158
-
159
-    /**
160
-     * @return string
161
-     * @throws InvalidDataTypeException
162
-     */
163
-    public function formStepUrlKey()
164
-    {
165
-        if (empty($this->form_step_url_key)) {
166
-            $this->setFormStepUrlKey();
167
-        }
168
-        return $this->form_step_url_key;
169
-    }
170
-
171
-
172
-    /**
173
-     * @param string $form_step_url_key
174
-     * @throws InvalidDataTypeException
175
-     */
176
-    public function setFormStepUrlKey($form_step_url_key = 'ee-form-step')
177
-    {
178
-        if (! is_string($form_step_url_key)) {
179
-            throw new InvalidDataTypeException('$form_step_key', $form_step_url_key, 'string');
180
-        }
181
-        $this->form_step_url_key = ! empty($form_step_url_key) ? $form_step_url_key : 'ee-form-step';
182
-    }
183
-
184
-
185
-    /**
186
-     * @return string
187
-     */
188
-    public function defaultFormStep()
189
-    {
190
-        return $this->default_form_step;
191
-    }
192
-
193
-
194
-    /**
195
-     * @param $default_form_step
196
-     * @throws InvalidDataTypeException
197
-     */
198
-    protected function setDefaultFormStep($default_form_step)
199
-    {
200
-        if (! is_string($default_form_step)) {
201
-            throw new InvalidDataTypeException('$default_form_step', $default_form_step, 'string');
202
-        }
203
-        $this->default_form_step = $default_form_step;
204
-    }
205
-
206
-
207
-    /**
208
-     * @return void
209
-     * @throws InvalidIdentifierException
210
-     * @throws InvalidDataTypeException
211
-     */
212
-    protected function setCurrentStepFromRequest()
213
-    {
214
-        $current_step_slug = $this->request()->getRequestParam($this->formStepUrlKey(), $this->defaultFormStep());
215
-        if (! $this->form_steps->setCurrent($current_step_slug)) {
216
-            throw new InvalidIdentifierException(
217
-                $current_step_slug,
218
-                $this->defaultFormStep(),
219
-                sprintf(
220
-                    esc_html__('The "%1$s" form step could not be set.', 'event_espresso'),
221
-                    $current_step_slug
222
-                )
223
-            );
224
-        }
225
-    }
226
-
227
-
228
-    /**
229
-     * @return SequentialStepFormInterface|object
230
-     * @throws InvalidFormHandlerException
231
-     */
232
-    public function getCurrentStep()
233
-    {
234
-        if (! $this->form_steps->current() instanceof SequentialStepForm) {
235
-            throw new InvalidFormHandlerException($this->form_steps->current());
236
-        }
237
-        return $this->form_steps->current();
238
-    }
239
-
240
-
241
-    /**
242
-     * @return string
243
-     * @throws InvalidFormHandlerException
244
-     */
245
-    public function formAction()
246
-    {
247
-        if (! is_string($this->form_action) || empty($this->form_action)) {
248
-            $this->form_action = $this->baseUrl();
249
-        }
250
-        return $this->form_action;
251
-    }
252
-
253
-
254
-    /**
255
-     * @param string $form_action
256
-     * @throws InvalidDataTypeException
257
-     */
258
-    public function setFormAction($form_action)
259
-    {
260
-        if (! is_string($form_action)) {
261
-            throw new InvalidDataTypeException('$form_action', $form_action, 'string');
262
-        }
263
-        $this->form_action = $form_action;
264
-    }
265
-
266
-
267
-    /**
268
-     * @param array $form_action_args
269
-     * @throws InvalidDataTypeException
270
-     * @throws InvalidFormHandlerException
271
-     */
272
-    public function addFormActionArgs($form_action_args = array())
273
-    {
274
-        if (! is_array($form_action_args)) {
275
-            throw new InvalidDataTypeException('$form_action_args', $form_action_args, 'array');
276
-        }
277
-        $form_action_args = ! empty($form_action_args)
278
-            ? $form_action_args
279
-            : array($this->formStepUrlKey() => $this->form_steps->current()->slug());
280
-        $this->getCurrentStep()->setFormAction(
281
-            add_query_arg($form_action_args, $this->formAction())
282
-        );
283
-        $this->form_action = $this->getCurrentStep()->formAction();
284
-    }
285
-
286
-
287
-    /**
288
-     * @return string
289
-     */
290
-    public function formConfig()
291
-    {
292
-        return $this->form_config;
293
-    }
294
-
295
-
296
-    /**
297
-     * @param string $form_config
298
-     */
299
-    public function setFormConfig($form_config)
300
-    {
301
-        $this->form_config = $form_config;
302
-    }
303
-
304
-
305
-    /**
306
-     * @return string
307
-     */
308
-    public function progressStepStyle()
309
-    {
310
-        return $this->progress_step_style;
311
-    }
312
-
313
-
314
-    /**
315
-     * @param string $progress_step_style
316
-     */
317
-    public function setProgressStepStyle($progress_step_style)
318
-    {
319
-        $this->progress_step_style = $progress_step_style;
320
-    }
321
-
322
-
323
-    /**
324
-     * @return RequestInterface
325
-     */
326
-    public function request()
327
-    {
328
-        return $this->request;
329
-    }
330
-
331
-
332
-    /**
333
-     * @return Collection|null
334
-     * @throws InvalidInterfaceException
335
-     */
336
-    protected function getProgressStepsCollection()
337
-    {
338
-        static $collection = null;
339
-        if (! $collection instanceof ProgressStepCollection) {
340
-            $collection = new ProgressStepCollection();
341
-        }
342
-        return $collection;
343
-    }
344
-
345
-
346
-    /**
347
-     * @param Collection $progress_steps_collection
348
-     * @return ProgressStepManager
349
-     * @throws InvalidInterfaceException
350
-     * @throws InvalidClassException
351
-     * @throws InvalidDataTypeException
352
-     * @throws InvalidEntityException
353
-     * @throws InvalidFormHandlerException
354
-     */
355
-    protected function generateProgressSteps(Collection $progress_steps_collection)
356
-    {
357
-        $current_step = $this->getCurrentStep();
358
-        /** @var SequentialStepForm $form_step */
359
-        foreach ($this->form_steps as $form_step) {
360
-            // is this step active ?
361
-            if (! $form_step->initialize()) {
362
-                continue;
363
-            }
364
-            $progress_steps_collection->add(
365
-                new ProgressStep(
366
-                    $form_step->order(),
367
-                    $form_step->slug(),
368
-                    $form_step->slug(),
369
-                    $form_step->formName()
370
-                ),
371
-                $form_step->slug()
372
-            );
373
-        }
374
-        // set collection pointer back to current step
375
-        $this->form_steps->setCurrentUsingObject($current_step);
376
-        return new ProgressStepManager(
377
-            $this->progressStepStyle(),
378
-            $this->defaultFormStep(),
379
-            $this->formStepUrlKey(),
380
-            $progress_steps_collection
381
-        );
382
-    }
383
-
384
-
385
-    /**
386
-     * @throws InvalidClassException
387
-     * @throws InvalidDataTypeException
388
-     * @throws InvalidEntityException
389
-     * @throws InvalidIdentifierException
390
-     * @throws InvalidInterfaceException
391
-     * @throws InvalidArgumentException
392
-     * @throws InvalidFormHandlerException
393
-     */
394
-    public function buildForm()
395
-    {
396
-        $this->buildCurrentStepFormForDisplay();
397
-    }
398
-
399
-
400
-    /**
401
-     * @param array $form_data
402
-     * @throws InvalidArgumentException
403
-     * @throws InvalidClassException
404
-     * @throws InvalidDataTypeException
405
-     * @throws InvalidEntityException
406
-     * @throws InvalidFormHandlerException
407
-     * @throws InvalidIdentifierException
408
-     * @throws InvalidInterfaceException
409
-     */
410
-    public function processForm($form_data = array())
411
-    {
412
-        $this->buildCurrentStepFormForProcessing();
413
-        $this->processCurrentStepForm($form_data);
414
-    }
415
-
416
-
417
-    /**
418
-     * @throws InvalidClassException
419
-     * @throws InvalidDataTypeException
420
-     * @throws InvalidEntityException
421
-     * @throws InvalidInterfaceException
422
-     * @throws InvalidIdentifierException
423
-     * @throws InvalidArgumentException
424
-     * @throws InvalidFormHandlerException
425
-     */
426
-    public function buildCurrentStepFormForDisplay()
427
-    {
428
-        $form_step = $this->buildCurrentStepForm();
429
-        // no displayable content ? then skip straight to processing
430
-        if (! $form_step->displayable()) {
431
-            $this->addFormActionArgs();
432
-            $form_step->setFormAction($this->formAction());
433
-            wp_safe_redirect($form_step->formAction());
434
-        }
435
-    }
436
-
437
-
438
-    /**
439
-     * @throws InvalidClassException
440
-     * @throws InvalidDataTypeException
441
-     * @throws InvalidEntityException
442
-     * @throws InvalidInterfaceException
443
-     * @throws InvalidIdentifierException
444
-     * @throws InvalidArgumentException
445
-     * @throws InvalidFormHandlerException
446
-     */
447
-    public function buildCurrentStepFormForProcessing()
448
-    {
449
-        $this->buildCurrentStepForm(false);
450
-    }
451
-
452
-
453
-    /**
454
-     * @param bool $for_display
455
-     * @return SequentialStepFormInterface
456
-     * @throws InvalidArgumentException
457
-     * @throws InvalidClassException
458
-     * @throws InvalidDataTypeException
459
-     * @throws InvalidEntityException
460
-     * @throws InvalidFormHandlerException
461
-     * @throws InvalidIdentifierException
462
-     * @throws InvalidInterfaceException
463
-     */
464
-    private function buildCurrentStepForm($for_display = true)
465
-    {
466
-        $this->form_steps = $this->getFormStepsCollection();
467
-        $this->setCurrentStepFromRequest();
468
-        $form_step = $this->getCurrentStep();
469
-        if ($form_step->submitBtnText() === esc_html__('Submit', 'event_espresso')) {
470
-            $form_step->setSubmitBtnText(esc_html__('Next Step', 'event_espresso'));
471
-        }
472
-        if ($for_display && $form_step->displayable()) {
473
-            $this->progress_step_manager = $this->generateProgressSteps(
474
-                $this->getProgressStepsCollection()
475
-            );
476
-            $this->progress_step_manager->setCurrentStep(
477
-                $form_step->slug()
478
-            );
479
-            // mark all previous progress steps as completed
480
-            $this->progress_step_manager->setPreviousStepsCompleted();
481
-            $this->progress_step_manager->enqueueStylesAndScripts();
482
-            $this->addFormActionArgs();
483
-            $form_step->setFormAction($this->formAction());
484
-        } else {
485
-            $form_step->setRedirectUrl($this->baseUrl());
486
-            $form_step->addRedirectArgs(
487
-                array($this->formStepUrlKey() => $this->form_steps->current()->slug())
488
-            );
489
-        }
490
-        $form_step->generate();
491
-        if ($for_display) {
492
-            $form_step->enqueueStylesAndScripts();
493
-        }
494
-        return $form_step;
495
-    }
496
-
497
-
498
-    /**
499
-     * @param bool $return_as_string
500
-     * @return string
501
-     * @throws InvalidFormHandlerException
502
-     */
503
-    public function displayProgressSteps($return_as_string = true)
504
-    {
505
-        $form_step = $this->getCurrentStep();
506
-        if (! $form_step->displayable()) {
507
-            return '';
508
-        }
509
-        $progress_steps = apply_filters(
510
-            'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_SequentialStepFormManager__displayProgressSteps__before_steps',
511
-            ''
512
-        );
513
-        $progress_steps .= $this->progress_step_manager->displaySteps();
514
-        $progress_steps .= apply_filters(
515
-            'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_SequentialStepFormManager__displayProgressSteps__after_steps',
516
-            ''
517
-        );
518
-        if ($return_as_string) {
519
-            return $progress_steps;
520
-        }
521
-        echo $progress_steps; // already escaped
522
-        return '';
523
-    }
524
-
525
-
526
-    /**
527
-     * @param bool $return_as_string
528
-     * @return string
529
-     * @throws InvalidFormHandlerException
530
-     */
531
-    public function displayCurrentStepForm($return_as_string = true)
532
-    {
533
-        if ($return_as_string) {
534
-            return $this->getCurrentStep()->display();
535
-        }
536
-        echo $this->getCurrentStep()->display(); // already escaped
537
-        return '';
538
-    }
539
-
540
-
541
-    /**
542
-     * @param array $form_data
543
-     * @return void
544
-     * @throws InvalidArgumentException
545
-     * @throws InvalidDataTypeException
546
-     * @throws InvalidFormHandlerException
547
-     */
548
-    public function processCurrentStepForm($form_data = array())
549
-    {
550
-        // grab instance of current step because after calling next() below,
551
-        // any calls to getCurrentStep() will return the "next" step because we advanced
552
-        $current_step = $this->getCurrentStep();
553
-        try {
554
-            // form processing should either throw exceptions or return true
555
-            $current_step->process($form_data);
556
-        } catch (Exception $e) {
557
-            // something went wrong, convert the Exception to an EE_Error
558
-            EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__);
559
-            // prevent redirect to next step or other if exception was thrown
560
-            if (
561
-                $current_step->redirectTo() === SequentialStepForm::REDIRECT_TO_NEXT_STEP
562
-                || $current_step->redirectTo() === SequentialStepForm::REDIRECT_TO_OTHER
563
-            ) {
564
-                $current_step->setRedirectTo(SequentialStepForm::REDIRECT_TO_CURRENT_STEP);
565
-            }
566
-        }
567
-        // save notices to a transient so that when we redirect back
568
-        // to the display portion for this step
569
-        // those notices can be displayed
570
-        EE_Error::get_notices(false, true);
571
-        $this->redirectForm($current_step);
572
-    }
573
-
574
-
575
-    /**
576
-     * handles where to go to next
577
-     *
578
-     * @param SequentialStepFormInterface $current_step
579
-     * @throws InvalidArgumentException
580
-     * @throws InvalidDataTypeException
581
-     * @throws InvalidFormHandlerException
582
-     */
583
-    public function redirectForm(SequentialStepFormInterface $current_step)
584
-    {
585
-        $redirect_step = $current_step;
586
-        switch ($current_step->redirectTo()) {
587
-            case SequentialStepForm::REDIRECT_TO_OTHER:
588
-                // going somewhere else, so just check out now
589
-                wp_safe_redirect($redirect_step->redirectUrl());
590
-                exit();
591
-            case SequentialStepForm::REDIRECT_TO_PREV_STEP:
592
-                $redirect_step = $this->form_steps->previous();
593
-                break;
594
-            case SequentialStepForm::REDIRECT_TO_NEXT_STEP:
595
-                $this->form_steps->next();
596
-                if ($this->form_steps->valid()) {
597
-                    $redirect_step = $this->form_steps->current();
598
-                }
599
-                break;
600
-            case SequentialStepForm::REDIRECT_TO_CURRENT_STEP:
601
-            default:
602
-                // $redirect_step is already set
603
-        }
604
-        $current_step->setRedirectUrl($this->baseUrl());
605
-        $current_step->addRedirectArgs(
606
-            // use the slug for whatever step we are redirecting too
607
-            array($this->formStepUrlKey() => $redirect_step->slug())
608
-        );
609
-        wp_safe_redirect($current_step->redirectUrl());
610
-        exit();
611
-    }
33
+	/**
34
+	 * a simplified URL with no form related parameters
35
+	 * that will be used to build the form's redirect URLs
36
+	 *
37
+	 * @var string $base_url
38
+	 */
39
+	private $base_url = '';
40
+
41
+	/**
42
+	 * the key used for the URL param that denotes the current form step
43
+	 * defaults to 'ee-form-step'
44
+	 *
45
+	 * @var string $form_step_url_key
46
+	 */
47
+	private $form_step_url_key = '';
48
+
49
+	/**
50
+	 * @var string $default_form_step
51
+	 */
52
+	private $default_form_step = '';
53
+
54
+	/**
55
+	 * @var string $form_action
56
+	 */
57
+	private $form_action;
58
+
59
+	/**
60
+	 * value of one of the string constant above
61
+	 *
62
+	 * @var string $form_config
63
+	 */
64
+	private $form_config;
65
+
66
+	/**
67
+	 * @var string $progress_step_style
68
+	 */
69
+	private $progress_step_style = '';
70
+
71
+	/**
72
+	 * @var RequestInterface $request
73
+	 */
74
+	private $request;
75
+
76
+	/**
77
+	 * @var Collection $form_steps
78
+	 */
79
+	protected $form_steps;
80
+
81
+	/**
82
+	 * @var ProgressStepManager $progress_step_manager
83
+	 */
84
+	protected $progress_step_manager;
85
+
86
+
87
+	/**
88
+	 * @return Collection|null
89
+	 */
90
+	abstract protected function getFormStepsCollection();
91
+
92
+	// phpcs:disable PEAR.Functions.ValidDefaultValue.NotAtEnd
93
+	/**
94
+	 * StepsManager constructor
95
+	 *
96
+	 * @param string                           $base_url
97
+	 * @param string                           $default_form_step
98
+	 * @param string                           $form_action
99
+	 * @param string                           $form_config
100
+	 * @param EE_Request|RequestInterface|null $request
101
+	 * @param string                           $progress_step_style
102
+	 * @throws InvalidDataTypeException
103
+	 * @throws InvalidArgumentException
104
+	 */
105
+	public function __construct(
106
+		$base_url,
107
+		$default_form_step,
108
+		$form_action = '',
109
+		$form_config = FormHandler::ADD_FORM_TAGS_AND_SUBMIT,
110
+		$progress_step_style = 'number_bubbles',
111
+		$request = null
112
+	) {
113
+		$this->setBaseUrl($base_url);
114
+		$this->setDefaultFormStep($default_form_step);
115
+		$this->setFormAction($form_action);
116
+		$this->setFormConfig($form_config);
117
+		$this->setProgressStepStyle($progress_step_style);
118
+		$this->request = $request instanceof RequestInterface
119
+			? $request
120
+			: LoaderFactory::getLoader()->getShared('EventEspresso\core\services\request\RequestInterface');
121
+	}
122
+
123
+
124
+	/**
125
+	 * @return string
126
+	 * @throws InvalidFormHandlerException
127
+	 */
128
+	public function baseUrl()
129
+	{
130
+		if (strpos($this->base_url, $this->getCurrentStep()->slug()) === false) {
131
+			add_query_arg(
132
+				array($this->form_step_url_key => $this->getCurrentStep()->slug()),
133
+				$this->base_url
134
+			);
135
+		}
136
+		return $this->base_url;
137
+	}
138
+
139
+
140
+	/**
141
+	 * @param string $base_url
142
+	 * @throws InvalidDataTypeException
143
+	 * @throws InvalidArgumentException
144
+	 */
145
+	protected function setBaseUrl($base_url)
146
+	{
147
+		if (! is_string($base_url)) {
148
+			throw new InvalidDataTypeException('$base_url', $base_url, 'string');
149
+		}
150
+		if (empty($base_url)) {
151
+			throw new InvalidArgumentException(
152
+				esc_html__('The base URL can not be an empty string.', 'event_espresso')
153
+			);
154
+		}
155
+		$this->base_url = $base_url;
156
+	}
157
+
158
+
159
+	/**
160
+	 * @return string
161
+	 * @throws InvalidDataTypeException
162
+	 */
163
+	public function formStepUrlKey()
164
+	{
165
+		if (empty($this->form_step_url_key)) {
166
+			$this->setFormStepUrlKey();
167
+		}
168
+		return $this->form_step_url_key;
169
+	}
170
+
171
+
172
+	/**
173
+	 * @param string $form_step_url_key
174
+	 * @throws InvalidDataTypeException
175
+	 */
176
+	public function setFormStepUrlKey($form_step_url_key = 'ee-form-step')
177
+	{
178
+		if (! is_string($form_step_url_key)) {
179
+			throw new InvalidDataTypeException('$form_step_key', $form_step_url_key, 'string');
180
+		}
181
+		$this->form_step_url_key = ! empty($form_step_url_key) ? $form_step_url_key : 'ee-form-step';
182
+	}
183
+
184
+
185
+	/**
186
+	 * @return string
187
+	 */
188
+	public function defaultFormStep()
189
+	{
190
+		return $this->default_form_step;
191
+	}
192
+
193
+
194
+	/**
195
+	 * @param $default_form_step
196
+	 * @throws InvalidDataTypeException
197
+	 */
198
+	protected function setDefaultFormStep($default_form_step)
199
+	{
200
+		if (! is_string($default_form_step)) {
201
+			throw new InvalidDataTypeException('$default_form_step', $default_form_step, 'string');
202
+		}
203
+		$this->default_form_step = $default_form_step;
204
+	}
205
+
206
+
207
+	/**
208
+	 * @return void
209
+	 * @throws InvalidIdentifierException
210
+	 * @throws InvalidDataTypeException
211
+	 */
212
+	protected function setCurrentStepFromRequest()
213
+	{
214
+		$current_step_slug = $this->request()->getRequestParam($this->formStepUrlKey(), $this->defaultFormStep());
215
+		if (! $this->form_steps->setCurrent($current_step_slug)) {
216
+			throw new InvalidIdentifierException(
217
+				$current_step_slug,
218
+				$this->defaultFormStep(),
219
+				sprintf(
220
+					esc_html__('The "%1$s" form step could not be set.', 'event_espresso'),
221
+					$current_step_slug
222
+				)
223
+			);
224
+		}
225
+	}
226
+
227
+
228
+	/**
229
+	 * @return SequentialStepFormInterface|object
230
+	 * @throws InvalidFormHandlerException
231
+	 */
232
+	public function getCurrentStep()
233
+	{
234
+		if (! $this->form_steps->current() instanceof SequentialStepForm) {
235
+			throw new InvalidFormHandlerException($this->form_steps->current());
236
+		}
237
+		return $this->form_steps->current();
238
+	}
239
+
240
+
241
+	/**
242
+	 * @return string
243
+	 * @throws InvalidFormHandlerException
244
+	 */
245
+	public function formAction()
246
+	{
247
+		if (! is_string($this->form_action) || empty($this->form_action)) {
248
+			$this->form_action = $this->baseUrl();
249
+		}
250
+		return $this->form_action;
251
+	}
252
+
253
+
254
+	/**
255
+	 * @param string $form_action
256
+	 * @throws InvalidDataTypeException
257
+	 */
258
+	public function setFormAction($form_action)
259
+	{
260
+		if (! is_string($form_action)) {
261
+			throw new InvalidDataTypeException('$form_action', $form_action, 'string');
262
+		}
263
+		$this->form_action = $form_action;
264
+	}
265
+
266
+
267
+	/**
268
+	 * @param array $form_action_args
269
+	 * @throws InvalidDataTypeException
270
+	 * @throws InvalidFormHandlerException
271
+	 */
272
+	public function addFormActionArgs($form_action_args = array())
273
+	{
274
+		if (! is_array($form_action_args)) {
275
+			throw new InvalidDataTypeException('$form_action_args', $form_action_args, 'array');
276
+		}
277
+		$form_action_args = ! empty($form_action_args)
278
+			? $form_action_args
279
+			: array($this->formStepUrlKey() => $this->form_steps->current()->slug());
280
+		$this->getCurrentStep()->setFormAction(
281
+			add_query_arg($form_action_args, $this->formAction())
282
+		);
283
+		$this->form_action = $this->getCurrentStep()->formAction();
284
+	}
285
+
286
+
287
+	/**
288
+	 * @return string
289
+	 */
290
+	public function formConfig()
291
+	{
292
+		return $this->form_config;
293
+	}
294
+
295
+
296
+	/**
297
+	 * @param string $form_config
298
+	 */
299
+	public function setFormConfig($form_config)
300
+	{
301
+		$this->form_config = $form_config;
302
+	}
303
+
304
+
305
+	/**
306
+	 * @return string
307
+	 */
308
+	public function progressStepStyle()
309
+	{
310
+		return $this->progress_step_style;
311
+	}
312
+
313
+
314
+	/**
315
+	 * @param string $progress_step_style
316
+	 */
317
+	public function setProgressStepStyle($progress_step_style)
318
+	{
319
+		$this->progress_step_style = $progress_step_style;
320
+	}
321
+
322
+
323
+	/**
324
+	 * @return RequestInterface
325
+	 */
326
+	public function request()
327
+	{
328
+		return $this->request;
329
+	}
330
+
331
+
332
+	/**
333
+	 * @return Collection|null
334
+	 * @throws InvalidInterfaceException
335
+	 */
336
+	protected function getProgressStepsCollection()
337
+	{
338
+		static $collection = null;
339
+		if (! $collection instanceof ProgressStepCollection) {
340
+			$collection = new ProgressStepCollection();
341
+		}
342
+		return $collection;
343
+	}
344
+
345
+
346
+	/**
347
+	 * @param Collection $progress_steps_collection
348
+	 * @return ProgressStepManager
349
+	 * @throws InvalidInterfaceException
350
+	 * @throws InvalidClassException
351
+	 * @throws InvalidDataTypeException
352
+	 * @throws InvalidEntityException
353
+	 * @throws InvalidFormHandlerException
354
+	 */
355
+	protected function generateProgressSteps(Collection $progress_steps_collection)
356
+	{
357
+		$current_step = $this->getCurrentStep();
358
+		/** @var SequentialStepForm $form_step */
359
+		foreach ($this->form_steps as $form_step) {
360
+			// is this step active ?
361
+			if (! $form_step->initialize()) {
362
+				continue;
363
+			}
364
+			$progress_steps_collection->add(
365
+				new ProgressStep(
366
+					$form_step->order(),
367
+					$form_step->slug(),
368
+					$form_step->slug(),
369
+					$form_step->formName()
370
+				),
371
+				$form_step->slug()
372
+			);
373
+		}
374
+		// set collection pointer back to current step
375
+		$this->form_steps->setCurrentUsingObject($current_step);
376
+		return new ProgressStepManager(
377
+			$this->progressStepStyle(),
378
+			$this->defaultFormStep(),
379
+			$this->formStepUrlKey(),
380
+			$progress_steps_collection
381
+		);
382
+	}
383
+
384
+
385
+	/**
386
+	 * @throws InvalidClassException
387
+	 * @throws InvalidDataTypeException
388
+	 * @throws InvalidEntityException
389
+	 * @throws InvalidIdentifierException
390
+	 * @throws InvalidInterfaceException
391
+	 * @throws InvalidArgumentException
392
+	 * @throws InvalidFormHandlerException
393
+	 */
394
+	public function buildForm()
395
+	{
396
+		$this->buildCurrentStepFormForDisplay();
397
+	}
398
+
399
+
400
+	/**
401
+	 * @param array $form_data
402
+	 * @throws InvalidArgumentException
403
+	 * @throws InvalidClassException
404
+	 * @throws InvalidDataTypeException
405
+	 * @throws InvalidEntityException
406
+	 * @throws InvalidFormHandlerException
407
+	 * @throws InvalidIdentifierException
408
+	 * @throws InvalidInterfaceException
409
+	 */
410
+	public function processForm($form_data = array())
411
+	{
412
+		$this->buildCurrentStepFormForProcessing();
413
+		$this->processCurrentStepForm($form_data);
414
+	}
415
+
416
+
417
+	/**
418
+	 * @throws InvalidClassException
419
+	 * @throws InvalidDataTypeException
420
+	 * @throws InvalidEntityException
421
+	 * @throws InvalidInterfaceException
422
+	 * @throws InvalidIdentifierException
423
+	 * @throws InvalidArgumentException
424
+	 * @throws InvalidFormHandlerException
425
+	 */
426
+	public function buildCurrentStepFormForDisplay()
427
+	{
428
+		$form_step = $this->buildCurrentStepForm();
429
+		// no displayable content ? then skip straight to processing
430
+		if (! $form_step->displayable()) {
431
+			$this->addFormActionArgs();
432
+			$form_step->setFormAction($this->formAction());
433
+			wp_safe_redirect($form_step->formAction());
434
+		}
435
+	}
436
+
437
+
438
+	/**
439
+	 * @throws InvalidClassException
440
+	 * @throws InvalidDataTypeException
441
+	 * @throws InvalidEntityException
442
+	 * @throws InvalidInterfaceException
443
+	 * @throws InvalidIdentifierException
444
+	 * @throws InvalidArgumentException
445
+	 * @throws InvalidFormHandlerException
446
+	 */
447
+	public function buildCurrentStepFormForProcessing()
448
+	{
449
+		$this->buildCurrentStepForm(false);
450
+	}
451
+
452
+
453
+	/**
454
+	 * @param bool $for_display
455
+	 * @return SequentialStepFormInterface
456
+	 * @throws InvalidArgumentException
457
+	 * @throws InvalidClassException
458
+	 * @throws InvalidDataTypeException
459
+	 * @throws InvalidEntityException
460
+	 * @throws InvalidFormHandlerException
461
+	 * @throws InvalidIdentifierException
462
+	 * @throws InvalidInterfaceException
463
+	 */
464
+	private function buildCurrentStepForm($for_display = true)
465
+	{
466
+		$this->form_steps = $this->getFormStepsCollection();
467
+		$this->setCurrentStepFromRequest();
468
+		$form_step = $this->getCurrentStep();
469
+		if ($form_step->submitBtnText() === esc_html__('Submit', 'event_espresso')) {
470
+			$form_step->setSubmitBtnText(esc_html__('Next Step', 'event_espresso'));
471
+		}
472
+		if ($for_display && $form_step->displayable()) {
473
+			$this->progress_step_manager = $this->generateProgressSteps(
474
+				$this->getProgressStepsCollection()
475
+			);
476
+			$this->progress_step_manager->setCurrentStep(
477
+				$form_step->slug()
478
+			);
479
+			// mark all previous progress steps as completed
480
+			$this->progress_step_manager->setPreviousStepsCompleted();
481
+			$this->progress_step_manager->enqueueStylesAndScripts();
482
+			$this->addFormActionArgs();
483
+			$form_step->setFormAction($this->formAction());
484
+		} else {
485
+			$form_step->setRedirectUrl($this->baseUrl());
486
+			$form_step->addRedirectArgs(
487
+				array($this->formStepUrlKey() => $this->form_steps->current()->slug())
488
+			);
489
+		}
490
+		$form_step->generate();
491
+		if ($for_display) {
492
+			$form_step->enqueueStylesAndScripts();
493
+		}
494
+		return $form_step;
495
+	}
496
+
497
+
498
+	/**
499
+	 * @param bool $return_as_string
500
+	 * @return string
501
+	 * @throws InvalidFormHandlerException
502
+	 */
503
+	public function displayProgressSteps($return_as_string = true)
504
+	{
505
+		$form_step = $this->getCurrentStep();
506
+		if (! $form_step->displayable()) {
507
+			return '';
508
+		}
509
+		$progress_steps = apply_filters(
510
+			'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_SequentialStepFormManager__displayProgressSteps__before_steps',
511
+			''
512
+		);
513
+		$progress_steps .= $this->progress_step_manager->displaySteps();
514
+		$progress_steps .= apply_filters(
515
+			'FHEE__EventEspresso_core_libraries_form_sections_form_handlers_SequentialStepFormManager__displayProgressSteps__after_steps',
516
+			''
517
+		);
518
+		if ($return_as_string) {
519
+			return $progress_steps;
520
+		}
521
+		echo $progress_steps; // already escaped
522
+		return '';
523
+	}
524
+
525
+
526
+	/**
527
+	 * @param bool $return_as_string
528
+	 * @return string
529
+	 * @throws InvalidFormHandlerException
530
+	 */
531
+	public function displayCurrentStepForm($return_as_string = true)
532
+	{
533
+		if ($return_as_string) {
534
+			return $this->getCurrentStep()->display();
535
+		}
536
+		echo $this->getCurrentStep()->display(); // already escaped
537
+		return '';
538
+	}
539
+
540
+
541
+	/**
542
+	 * @param array $form_data
543
+	 * @return void
544
+	 * @throws InvalidArgumentException
545
+	 * @throws InvalidDataTypeException
546
+	 * @throws InvalidFormHandlerException
547
+	 */
548
+	public function processCurrentStepForm($form_data = array())
549
+	{
550
+		// grab instance of current step because after calling next() below,
551
+		// any calls to getCurrentStep() will return the "next" step because we advanced
552
+		$current_step = $this->getCurrentStep();
553
+		try {
554
+			// form processing should either throw exceptions or return true
555
+			$current_step->process($form_data);
556
+		} catch (Exception $e) {
557
+			// something went wrong, convert the Exception to an EE_Error
558
+			EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__);
559
+			// prevent redirect to next step or other if exception was thrown
560
+			if (
561
+				$current_step->redirectTo() === SequentialStepForm::REDIRECT_TO_NEXT_STEP
562
+				|| $current_step->redirectTo() === SequentialStepForm::REDIRECT_TO_OTHER
563
+			) {
564
+				$current_step->setRedirectTo(SequentialStepForm::REDIRECT_TO_CURRENT_STEP);
565
+			}
566
+		}
567
+		// save notices to a transient so that when we redirect back
568
+		// to the display portion for this step
569
+		// those notices can be displayed
570
+		EE_Error::get_notices(false, true);
571
+		$this->redirectForm($current_step);
572
+	}
573
+
574
+
575
+	/**
576
+	 * handles where to go to next
577
+	 *
578
+	 * @param SequentialStepFormInterface $current_step
579
+	 * @throws InvalidArgumentException
580
+	 * @throws InvalidDataTypeException
581
+	 * @throws InvalidFormHandlerException
582
+	 */
583
+	public function redirectForm(SequentialStepFormInterface $current_step)
584
+	{
585
+		$redirect_step = $current_step;
586
+		switch ($current_step->redirectTo()) {
587
+			case SequentialStepForm::REDIRECT_TO_OTHER:
588
+				// going somewhere else, so just check out now
589
+				wp_safe_redirect($redirect_step->redirectUrl());
590
+				exit();
591
+			case SequentialStepForm::REDIRECT_TO_PREV_STEP:
592
+				$redirect_step = $this->form_steps->previous();
593
+				break;
594
+			case SequentialStepForm::REDIRECT_TO_NEXT_STEP:
595
+				$this->form_steps->next();
596
+				if ($this->form_steps->valid()) {
597
+					$redirect_step = $this->form_steps->current();
598
+				}
599
+				break;
600
+			case SequentialStepForm::REDIRECT_TO_CURRENT_STEP:
601
+			default:
602
+				// $redirect_step is already set
603
+		}
604
+		$current_step->setRedirectUrl($this->baseUrl());
605
+		$current_step->addRedirectArgs(
606
+			// use the slug for whatever step we are redirecting too
607
+			array($this->formStepUrlKey() => $redirect_step->slug())
608
+		);
609
+		wp_safe_redirect($current_step->redirectUrl());
610
+		exit();
611
+	}
612 612
 }
Please login to merge, or discard this patch.
espresso.php 1 patch
Indentation   +80 added lines, -80 removed lines patch added patch discarded remove patch
@@ -38,103 +38,103 @@
 block discarded – undo
38 38
  * @since           4.0
39 39
  */
40 40
 if (function_exists('espresso_version')) {
41
-    if (! function_exists('espresso_duplicate_plugin_error')) {
42
-        /**
43
-         *    espresso_duplicate_plugin_error
44
-         *    displays if more than one version of EE is activated at the same time
45
-         */
46
-        function espresso_duplicate_plugin_error()
47
-        {
48
-            ?>
41
+	if (! function_exists('espresso_duplicate_plugin_error')) {
42
+		/**
43
+		 *    espresso_duplicate_plugin_error
44
+		 *    displays if more than one version of EE is activated at the same time
45
+		 */
46
+		function espresso_duplicate_plugin_error()
47
+		{
48
+			?>
49 49
             <div class="error">
50 50
                 <p>
51 51
                     <?php
52
-                    echo esc_html__(
53
-                        'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.',
54
-                        'event_espresso'
55
-                    ); ?>
52
+					echo esc_html__(
53
+						'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.',
54
+						'event_espresso'
55
+					); ?>
56 56
                 </p>
57 57
             </div>
58 58
             <?php
59
-            espresso_deactivate_plugin(plugin_basename(__FILE__));
60
-        }
61
-    }
62
-    add_action('admin_notices', 'espresso_duplicate_plugin_error', 1);
59
+			espresso_deactivate_plugin(plugin_basename(__FILE__));
60
+		}
61
+	}
62
+	add_action('admin_notices', 'espresso_duplicate_plugin_error', 1);
63 63
 } else {
64
-    define('EE_MIN_PHP_VER_REQUIRED', '5.6.2');
65
-    if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) {
66
-        /**
67
-         * espresso_minimum_php_version_error
68
-         *
69
-         * @return void
70
-         */
71
-        function espresso_minimum_php_version_error()
72
-        {
73
-            ?>
64
+	define('EE_MIN_PHP_VER_REQUIRED', '5.6.2');
65
+	if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) {
66
+		/**
67
+		 * espresso_minimum_php_version_error
68
+		 *
69
+		 * @return void
70
+		 */
71
+		function espresso_minimum_php_version_error()
72
+		{
73
+			?>
74 74
             <div class="error">
75 75
                 <p>
76 76
                     <?php
77
-                    printf(
78
-                        esc_html__(
79
-                            'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.',
80
-                            'event_espresso'
81
-                        ),
82
-                        EE_MIN_PHP_VER_REQUIRED,
83
-                        PHP_VERSION,
84
-                        '<br/>',
85
-                        '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>'
86
-                    );
87
-                    ?>
77
+					printf(
78
+						esc_html__(
79
+							'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.',
80
+							'event_espresso'
81
+						),
82
+						EE_MIN_PHP_VER_REQUIRED,
83
+						PHP_VERSION,
84
+						'<br/>',
85
+						'<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>'
86
+					);
87
+					?>
88 88
                 </p>
89 89
             </div>
90 90
             <?php
91
-            espresso_deactivate_plugin(plugin_basename(__FILE__));
92
-        }
91
+			espresso_deactivate_plugin(plugin_basename(__FILE__));
92
+		}
93 93
 
94
-        add_action('admin_notices', 'espresso_minimum_php_version_error', 1);
95
-    } else {
96
-        define('EVENT_ESPRESSO_MAIN_FILE', __FILE__);
97
-        /**
98
-         * espresso_version
99
-         * Returns the plugin version
100
-         *
101
-         * @return string
102
-         */
103
-        function espresso_version()
104
-        {
105
-            return apply_filters('FHEE__espresso__espresso_version', '4.10.15.rc.015');
106
-        }
94
+		add_action('admin_notices', 'espresso_minimum_php_version_error', 1);
95
+	} else {
96
+		define('EVENT_ESPRESSO_MAIN_FILE', __FILE__);
97
+		/**
98
+		 * espresso_version
99
+		 * Returns the plugin version
100
+		 *
101
+		 * @return string
102
+		 */
103
+		function espresso_version()
104
+		{
105
+			return apply_filters('FHEE__espresso__espresso_version', '4.10.15.rc.015');
106
+		}
107 107
 
108
-        /**
109
-         * espresso_plugin_activation
110
-         * adds a wp-option to indicate that EE has been activated via the WP admin plugins page
111
-         */
112
-        function espresso_plugin_activation()
113
-        {
114
-            update_option('ee_espresso_activation', true);
115
-        }
108
+		/**
109
+		 * espresso_plugin_activation
110
+		 * adds a wp-option to indicate that EE has been activated via the WP admin plugins page
111
+		 */
112
+		function espresso_plugin_activation()
113
+		{
114
+			update_option('ee_espresso_activation', true);
115
+		}
116 116
 
117
-        register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation');
117
+		register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation');
118 118
 
119
-        require_once __DIR__ . '/core/bootstrap_espresso.php';
120
-        bootstrap_espresso();
121
-    }
119
+		require_once __DIR__ . '/core/bootstrap_espresso.php';
120
+		bootstrap_espresso();
121
+	}
122 122
 }
123 123
 if (! function_exists('espresso_deactivate_plugin')) {
124
-    /**
125
-     *    deactivate_plugin
126
-     * usage:  espresso_deactivate_plugin( plugin_basename( __FILE__ ));
127
-     *
128
-     * @access public
129
-     * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file
130
-     * @return    void
131
-     */
132
-    function espresso_deactivate_plugin($plugin_basename = '')
133
-    {
134
-        if (! function_exists('deactivate_plugins')) {
135
-            require_once ABSPATH . 'wp-admin/includes/plugin.php';
136
-        }
137
-        unset($_GET['activate'], $_REQUEST['activate']);
138
-        deactivate_plugins($plugin_basename);
139
-    }
124
+	/**
125
+	 *    deactivate_plugin
126
+	 * usage:  espresso_deactivate_plugin( plugin_basename( __FILE__ ));
127
+	 *
128
+	 * @access public
129
+	 * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file
130
+	 * @return    void
131
+	 */
132
+	function espresso_deactivate_plugin($plugin_basename = '')
133
+	{
134
+		if (! function_exists('deactivate_plugins')) {
135
+			require_once ABSPATH . 'wp-admin/includes/plugin.php';
136
+		}
137
+		unset($_GET['activate'], $_REQUEST['activate']);
138
+		deactivate_plugins($plugin_basename);
139
+	}
140 140
 }
Please login to merge, or discard this patch.