Completed
Branch ease-off-request-interface (6a17b6)
by
unknown
17:32 queued 15:53
created
core/admin/EE_Admin_Page_Init.core.php 2 patches
Indentation   +446 added lines, -446 removed lines patch added patch discarded remove patch
@@ -17,451 +17,451 @@
 block discarded – undo
17 17
 abstract class EE_Admin_Page_Init extends EE_Base
18 18
 {
19 19
 
20
-    // identity properties (set in _set_defaults and _set_init_properties)
21
-    public $label;
20
+	// identity properties (set in _set_defaults and _set_init_properties)
21
+	public $label;
22 22
 
23
-    /**
24
-     * Menu map has a capability.  However, this allows admin pages to have separate capability requirements for menus
25
-     * and accessing pages.  If capability is NOT set, then it defaults to the menu_map capability.
26
-     *
27
-     * @var string
28
-     */
29
-    public $capability;
30
-
31
-
32
-    /**
33
-     * This holds the menu map object for this admin page.
34
-     *
35
-     * @var EE_Admin_Page_Menu_Map
36
-     */
37
-    protected $_menu_map;
38
-
39
-    /**
40
-     * deprecated
41
-     */
42
-    public $menu_label;
43
-
44
-    public $menu_slug;
45
-
46
-
47
-    // set in _set_defaults
48
-    protected $_folder_name;
49
-
50
-    protected $_folder_path;
51
-
52
-    protected $_file_name;
53
-
54
-    public $hook_file;
55
-
56
-    protected $_wp_page_slug;
57
-
58
-    protected $_routing;
59
-
60
-
61
-    // will hold page object.
62
-    protected $_loaded_page_object;
63
-
64
-
65
-    // for caf
66
-    protected $_files_hooked;
67
-
68
-    protected $_hook_paths;
69
-
70
-    // load_page?
71
-    private $_load_page;
72
-
73
-    /**
74
-     * @var LoaderInterface
75
-     */
76
-    protected $loader;
77
-
78
-    /**
79
-     * @var RequestInterface
80
-     */
81
-    protected $request;
82
-
83
-
84
-    /**
85
-     * @Constructor
86
-     * @return void
87
-     */
88
-    public function __construct(RequestInterface $request = null)
89
-    {
90
-        $this->loader = LoaderFactory::getLoader();
91
-        $this->request = $request instanceof RequestInterface
92
-            ? $request
93
-            : $this->loader->getShared(RequestInterface::class);
94
-        // set global defaults
95
-        $this->_set_defaults();
96
-        // set properties that are always available with objects.
97
-        $this->_set_init_properties();
98
-        // global styles/scripts across all wp admin pages
99
-        add_action('admin_enqueue_scripts', [$this, 'load_wp_global_scripts_styles'], 5);
100
-        // load initial stuff.
101
-        $this->_set_file_and_folder_name();
102
-        $this->_set_menu_map();
103
-        if (! $this->verifyMenuMapSet()) {
104
-            return;
105
-        }
106
-        // set default capability
107
-        $this->_set_capability();
108
-    }
109
-
110
-
111
-    /**
112
-     * _set_init_properties
113
-     * Child classes use to set the following properties:
114
-     * $label
115
-     *
116
-     * @abstract
117
-     * @return void
118
-     */
119
-    abstract protected function _set_init_properties();
120
-
121
-
122
-    /**
123
-     * _set_menu_map is a function that child classes use to set the menu_map property (which should be an instance of
124
-     * EE_Admin_Page_Menu_Map.  Their menu can either be EE_Admin_Page_Main_Menu or EE_Admin_Page_Sub_Menu.
125
-     *
126
-     * @since 4.4.0
127
-     * @ return void.
128
-     */
129
-    protected function _set_menu_map()
130
-    {
131
-        return [];
132
-    }
133
-
134
-
135
-    /**
136
-     * @return bool
137
-     * @since   4.10.14.p
138
-     */
139
-    private function verifyMenuMapSet()
140
-    {
141
-        if (empty($this->_menu_map) || is_array($this->_menu_map)) {
142
-            EE_Error::doing_it_wrong(
143
-                get_class($this) . '::$_menu_map',
144
-                sprintf(
145
-                    esc_html__(
146
-                        'The EE4 addon with the class %s is setting up the _menu_map property incorrectly for this version of EE core.  Please see Admin_Page_Init class examples in core for the new way of setting this property up.',
147
-                        'event_espresso'
148
-                    ),
149
-                    get_class($this)
150
-                ),
151
-                '4.4.0'
152
-            );
153
-            return true;
154
-        }
155
-        return false;
156
-    }
157
-
158
-
159
-    /**
160
-     * returns the menu map for this admin page
161
-     *
162
-     * @return EE_Admin_Page_Menu_Map
163
-     * @since 4.4.0
164
-     */
165
-    public function get_menu_map()
166
-    {
167
-        return $this->_menu_map;
168
-    }
169
-
170
-
171
-    /**
172
-     * This loads scripts and styles for the EE_Admin system
173
-     * that must be available on ALL WP admin pages (i.e. EE_menu items)
174
-     *
175
-     * @return void
176
-     */
177
-    public function load_wp_global_scripts_styles()
178
-    {
179
-        wp_register_style(
180
-            'espresso_menu',
181
-            EE_ADMIN_URL . 'assets/admin-menu-styles.css',
182
-            ['dashicons'],
183
-            EVENT_ESPRESSO_VERSION
184
-        );
185
-        wp_enqueue_style('espresso_menu');
186
-    }
187
-
188
-
189
-    /**
190
-     * this sets default properties (might be overridden in _set_init_properties);
191
-     *
192
-     * @return  void
193
-     */
194
-    private function _set_defaults()
195
-    {
196
-        $this->_file_name    = $this->_folder_name = $this->_wp_page_slug = $this->capability = null;
197
-        $this->_routing      = true;
198
-        $this->_load_page    = false;
199
-        $this->_files_hooked = $this->_hook_paths = [];
200
-        // menu_map
201
-        $this->_menu_map = $this->get_menu_map();
202
-    }
203
-
204
-
205
-    protected function _set_capability()
206
-    {
207
-        $capability       = empty($this->capability) ? $this->_menu_map->capability : $this->capability;
208
-        $this->capability = apply_filters('FHEE_' . $this->_menu_map->menu_slug . '_capability', $capability);
209
-    }
210
-
211
-
212
-    /**
213
-     * initialize_admin_page
214
-     * This method is what executes the loading of the specific page class for the given dir_name as called by the
215
-     * EE_Admin_Init class.
216
-     *
217
-     * @return void
218
-     * @uses    _initialize_admin_page()
219
-     */
220
-    public function initialize_admin_page()
221
-    {
222
-        // let's check user access first
223
-        $this->_check_user_access();
224
-        if (! is_object($this->_loaded_page_object)) {
225
-            return;
226
-        }
227
-        $this->_loaded_page_object->route_admin_request();
228
-    }
229
-
230
-
231
-    /**
232
-     * @param string $wp_page_slug
233
-     * @throws EE_Error
234
-     */
235
-    public function set_page_dependencies($wp_page_slug)
236
-    {
237
-        if (! $this->_load_page) {
238
-            return;
239
-        }
240
-        if (! is_object($this->_loaded_page_object)) {
241
-            $msg[] = esc_html__(
242
-                'We can\'t load the page because we\'re missing a valid page object that tells us what to load',
243
-                'event_espresso'
244
-            );
245
-            $msg[] = $msg[0] . "\r\n"
246
-                     . sprintf(
247
-                         esc_html__(
248
-                             'The custom slug you have set for this page is %s. This means we\'re looking for the class %s_Admin_Page (found in %s_Admin_Page.core.php) within your %s directory',
249
-                             'event_espresso'
250
-                         ),
251
-                         $this->_file_name,
252
-                         $this->_file_name,
253
-                         $this->_folder_path . $this->_file_name,
254
-                         $this->_menu_map->menu_slug
255
-                     );
256
-            throw new EE_Error(implode('||', $msg));
257
-        }
258
-        $this->_loaded_page_object->set_wp_page_slug($wp_page_slug);
259
-        $page_hook = 'load-' . $wp_page_slug;
260
-        // hook into page load hook so all page specific stuff gets loaded.
261
-        if (! empty($wp_page_slug)) {
262
-            add_action($page_hook, [$this->_loaded_page_object, 'load_page_dependencies']);
263
-        }
264
-    }
265
-
266
-
267
-    /**
268
-     * This executes the initial page loads for EE_Admin pages to take care of any ajax or other code needing to run
269
-     * before the load-page... hook. Note, the page loads are happening around the wp_init hook.
270
-     *
271
-     * @return void
272
-     */
273
-    public function do_initial_loads()
274
-    {
275
-        // no loading or initializing if menu map is setup incorrectly.
276
-        if (empty($this->_menu_map) || is_array($this->_menu_map)) {
277
-            return;
278
-        }
279
-        $this->_initialize_admin_page();
280
-    }
281
-
282
-
283
-    /**
284
-     * all we're doing here is setting the $_file_name property for later use.
285
-     *
286
-     * @return void
287
-     */
288
-    private function _set_file_and_folder_name()
289
-    {
290
-        $bt = debug_backtrace();
291
-        // for more reliable determination of folder name
292
-        // we're using this to get the actual folder name of the CALLING class (i.e. the child class that extends this).  Why?  Because $this->menu_slug may be different than the folder name (to avoid conflicts with other plugins)
293
-        $class = get_class($this);
294
-        foreach ($bt as $index => $values) {
295
-            if (isset($values['class']) && $values['class'] == $class) {
296
-                $file_index         = $index - 1;
297
-                $this->_folder_name = basename(dirname($bt[ $file_index ]['file']));
298
-                if (! empty($this->_folder_name)) {
299
-                    break;
300
-                }
301
-            }
302
-        }
303
-        $this->_folder_path = EE_ADMIN_PAGES . $this->_folder_name . '/';
304
-        $this->_file_name   = preg_replace('/^ee/', 'EE', $this->_folder_name);
305
-        $this->_file_name   = ucwords(str_replace('_', ' ', $this->_file_name));
306
-        $this->_file_name   = str_replace(' ', '_', $this->_file_name);
307
-    }
308
-
309
-
310
-    /**
311
-     * This automatically checks if we have a hook class in the loaded child directory.  If we DO then we will register
312
-     * it with the appropriate pages.  That way all we have to do is make sure the file is named correctly and
313
-     * "dropped" in. Example: if we wanted to set this up for Messages hooking into Events then we would do:
314
-     * events_Messages_Hooks.class.php
315
-     *
316
-     * @param bool $extend This indicates whether we're checking the extend directory for any register_hooks
317
-     *                     files/classes
318
-     * @return array
319
-     */
320
-    public function register_hooks($extend = false)
321
-    {
322
-
323
-        // get a list of files in the directory that have the "Hook" in their name an
324
-        // if this is an extended check (i.e. caf is active) then we will scan the caffeinated/extend directory first and any hook files that are found will be have their reference added to the $_files_hook array property.  Then, we make sure that when we loop through the core decaf directories to find hook files that we skip over any hooks files that have already been set by caf.
325
-        if ($extend) {
326
-            $hook_files_glob_path = apply_filters(
327
-                'FHEE__EE_Admin_Page_Init__register_hooks__hook_files_glob_path__extend',
328
-                EE_CORE_CAF_ADMIN_EXTEND
329
-                . $this->_folder_name
330
-                . '/*'
331
-                . $this->_file_name
332
-                . '_Hooks_Extend.class.php'
333
-            );
334
-            $this->_hook_paths    = $this->_register_hook_files($hook_files_glob_path, $extend);
335
-        }
336
-        // loop through decaf folders
337
-        $hook_files_glob_path = apply_filters(
338
-            'FHEE__EE_Admin_Page_Init__register_hooks__hook_files_glob_path',
339
-            $this->_folder_path . '*' . $this->_file_name . '_Hooks.class.php'
340
-        );
341
-        $this->_hook_paths    = array_merge(
342
-            $this->_register_hook_files($hook_files_glob_path),
343
-            $this->_hook_paths
344
-        );  // making sure any extended hook paths are later in the array than the core hook paths!
345
-        return $this->_hook_paths;
346
-    }
347
-
348
-
349
-    protected function _register_hook_files($hook_files_glob_path, $extend = false)
350
-    {
351
-        $hook_paths = glob($hook_files_glob_path);
352
-        if (empty($hook_paths)) {
353
-            return [];
354
-        }
355
-        foreach ($hook_paths as $file) {
356
-            // lets get the linked admin.
357
-            $hook_file = $extend
358
-                ? str_replace(EE_CORE_CAF_ADMIN_EXTEND . $this->_folder_name . '/', '', $file)
359
-                : str_replace($this->_folder_path, '', $file);
360
-            $replace         = $extend
361
-                ? '_' . $this->_file_name . '_Hooks_Extend.class.php'
362
-                : '_' . $this->_file_name . '_Hooks.class.php';
363
-            $rel_admin       = str_replace($replace, '', $hook_file);
364
-            $rel_admin       = strtolower($rel_admin);
365
-            // make sure we haven't already got a hook setup for this page path
366
-            if (in_array($rel_admin, $this->_files_hooked)) {
367
-                continue;
368
-            }
369
-            $this->hook_file = $hook_file;
370
-            $rel_admin_hook = 'FHEE_do_other_page_hooks_' . $rel_admin;
371
-            add_filter($rel_admin_hook, [$this, 'load_admin_hook']);
372
-            $this->_files_hooked[] = $rel_admin;
373
-        }
374
-        return $hook_paths;
375
-    }
376
-
377
-
378
-    public function load_admin_hook($registered_pages)
379
-    {
380
-        return array_merge((array) $this->hook_file, $registered_pages);
381
-    }
382
-
383
-
384
-    /**
385
-     * _initialize_admin_page
386
-     *
387
-     * @see  initialize_admin_page() for info
388
-     */
389
-    protected function _initialize_admin_page()
390
-    {
391
-        // JUST CHECK WE'RE ON RIGHT PAGE.
392
-        $page = $this->request->getRequestParam('page');
393
-        $page = $this->request->getRequestParam('current_page', $page);
394
-        $menu_slug = $this->_menu_map->menu_slug;
395
-
396
-
397
-        if ($this->_routing && ($page === '' || $page !== $menu_slug)) {
398
-            // not on the right page so let's get out.
399
-            return;
400
-        }
401
-        $this->_load_page = true;
402
-
403
-        // we don't need to do a page_request check here because it's only called via WP menu system.
404
-        $admin_page  = $this->_file_name . '_Admin_Page';
405
-        $hook_suffix = "{$menu_slug}_{$admin_page}";
406
-        $admin_page  = apply_filters(
407
-            "FHEE__EE_Admin_Page_Init___initialize_admin_page__admin_page__{$hook_suffix}",
408
-            $admin_page
409
-        );
410
-        if (empty($admin_page)) {
411
-            return;
412
-        }
413
-        // define requested admin page class name then load the file and instantiate
414
-        $path_to_file = str_replace(['\\', '/'], '/', $this->_folder_path . $admin_page . '.core.php');
415
-        // so if the file would be in EE_ADMIN/attendees/Attendee_Admin_Page.core.php, the filter would be:
416
-        // FHEE__EE_Admin_Page_Init___initialize_admin_page__path_to_file__attendees_Attendee_Admin_Page
417
-        $path_to_file = apply_filters(
418
-            "FHEE__EE_Admin_Page_Init___initialize_admin_page__path_to_file__{$hook_suffix}",
419
-            $path_to_file
420
-        );
421
-        if (! is_readable($path_to_file)) {
422
-            return;
423
-        }
424
-        // This is a place where EE plugins can hook in to make sure their own files are required in the appropriate place
425
-        do_action('AHEE__EE_Admin_Page___initialize_admin_page__before_initialization');
426
-        do_action("AHEE__EE_Admin_Page___initialize_admin_page__before_initialization_{$menu_slug}");
427
-        require_once($path_to_file);
428
-        $this->_loaded_page_object = $this->loader->getShared($admin_page, [$this->_routing]);
429
-
430
-        do_action('AHEE__EE_Admin_Page___initialize_admin_page__after_initialization');
431
-        do_action("AHEE__EE_Admin_Page___initialize_admin_page__after_initialization_{$menu_slug}");
432
-    }
433
-
434
-
435
-    public function get_admin_page_name()
436
-    {
437
-        return $this->_file_name . '_Admin_Page';
438
-    }
439
-
440
-
441
-    /**
442
-     * @return mixed
443
-     */
444
-    public function loaded_page_object()
445
-    {
446
-        return $this->_loaded_page_object;
447
-    }
448
-
449
-    /**
450
-     * _check_user_access
451
-     * verifies user access for this admin page.  If no user access is available then let's gracefully exit with a
452
-     * WordPress die message.
453
-     *
454
-     * @return void  wp_die if fail
455
-     */
456
-    private function _check_user_access()
457
-    {
458
-        if (
459
-            ! EE_Registry::instance()->CAP->current_user_can(
460
-                $this->_menu_map->capability,
461
-                $this->_menu_map->menu_slug
462
-            )
463
-        ) {
464
-            wp_die(esc_html__('You don\'t have access to this page.', 'event_espresso'), '', ['back_link' => true]);
465
-        }
466
-    }
23
+	/**
24
+	 * Menu map has a capability.  However, this allows admin pages to have separate capability requirements for menus
25
+	 * and accessing pages.  If capability is NOT set, then it defaults to the menu_map capability.
26
+	 *
27
+	 * @var string
28
+	 */
29
+	public $capability;
30
+
31
+
32
+	/**
33
+	 * This holds the menu map object for this admin page.
34
+	 *
35
+	 * @var EE_Admin_Page_Menu_Map
36
+	 */
37
+	protected $_menu_map;
38
+
39
+	/**
40
+	 * deprecated
41
+	 */
42
+	public $menu_label;
43
+
44
+	public $menu_slug;
45
+
46
+
47
+	// set in _set_defaults
48
+	protected $_folder_name;
49
+
50
+	protected $_folder_path;
51
+
52
+	protected $_file_name;
53
+
54
+	public $hook_file;
55
+
56
+	protected $_wp_page_slug;
57
+
58
+	protected $_routing;
59
+
60
+
61
+	// will hold page object.
62
+	protected $_loaded_page_object;
63
+
64
+
65
+	// for caf
66
+	protected $_files_hooked;
67
+
68
+	protected $_hook_paths;
69
+
70
+	// load_page?
71
+	private $_load_page;
72
+
73
+	/**
74
+	 * @var LoaderInterface
75
+	 */
76
+	protected $loader;
77
+
78
+	/**
79
+	 * @var RequestInterface
80
+	 */
81
+	protected $request;
82
+
83
+
84
+	/**
85
+	 * @Constructor
86
+	 * @return void
87
+	 */
88
+	public function __construct(RequestInterface $request = null)
89
+	{
90
+		$this->loader = LoaderFactory::getLoader();
91
+		$this->request = $request instanceof RequestInterface
92
+			? $request
93
+			: $this->loader->getShared(RequestInterface::class);
94
+		// set global defaults
95
+		$this->_set_defaults();
96
+		// set properties that are always available with objects.
97
+		$this->_set_init_properties();
98
+		// global styles/scripts across all wp admin pages
99
+		add_action('admin_enqueue_scripts', [$this, 'load_wp_global_scripts_styles'], 5);
100
+		// load initial stuff.
101
+		$this->_set_file_and_folder_name();
102
+		$this->_set_menu_map();
103
+		if (! $this->verifyMenuMapSet()) {
104
+			return;
105
+		}
106
+		// set default capability
107
+		$this->_set_capability();
108
+	}
109
+
110
+
111
+	/**
112
+	 * _set_init_properties
113
+	 * Child classes use to set the following properties:
114
+	 * $label
115
+	 *
116
+	 * @abstract
117
+	 * @return void
118
+	 */
119
+	abstract protected function _set_init_properties();
120
+
121
+
122
+	/**
123
+	 * _set_menu_map is a function that child classes use to set the menu_map property (which should be an instance of
124
+	 * EE_Admin_Page_Menu_Map.  Their menu can either be EE_Admin_Page_Main_Menu or EE_Admin_Page_Sub_Menu.
125
+	 *
126
+	 * @since 4.4.0
127
+	 * @ return void.
128
+	 */
129
+	protected function _set_menu_map()
130
+	{
131
+		return [];
132
+	}
133
+
134
+
135
+	/**
136
+	 * @return bool
137
+	 * @since   4.10.14.p
138
+	 */
139
+	private function verifyMenuMapSet()
140
+	{
141
+		if (empty($this->_menu_map) || is_array($this->_menu_map)) {
142
+			EE_Error::doing_it_wrong(
143
+				get_class($this) . '::$_menu_map',
144
+				sprintf(
145
+					esc_html__(
146
+						'The EE4 addon with the class %s is setting up the _menu_map property incorrectly for this version of EE core.  Please see Admin_Page_Init class examples in core for the new way of setting this property up.',
147
+						'event_espresso'
148
+					),
149
+					get_class($this)
150
+				),
151
+				'4.4.0'
152
+			);
153
+			return true;
154
+		}
155
+		return false;
156
+	}
157
+
158
+
159
+	/**
160
+	 * returns the menu map for this admin page
161
+	 *
162
+	 * @return EE_Admin_Page_Menu_Map
163
+	 * @since 4.4.0
164
+	 */
165
+	public function get_menu_map()
166
+	{
167
+		return $this->_menu_map;
168
+	}
169
+
170
+
171
+	/**
172
+	 * This loads scripts and styles for the EE_Admin system
173
+	 * that must be available on ALL WP admin pages (i.e. EE_menu items)
174
+	 *
175
+	 * @return void
176
+	 */
177
+	public function load_wp_global_scripts_styles()
178
+	{
179
+		wp_register_style(
180
+			'espresso_menu',
181
+			EE_ADMIN_URL . 'assets/admin-menu-styles.css',
182
+			['dashicons'],
183
+			EVENT_ESPRESSO_VERSION
184
+		);
185
+		wp_enqueue_style('espresso_menu');
186
+	}
187
+
188
+
189
+	/**
190
+	 * this sets default properties (might be overridden in _set_init_properties);
191
+	 *
192
+	 * @return  void
193
+	 */
194
+	private function _set_defaults()
195
+	{
196
+		$this->_file_name    = $this->_folder_name = $this->_wp_page_slug = $this->capability = null;
197
+		$this->_routing      = true;
198
+		$this->_load_page    = false;
199
+		$this->_files_hooked = $this->_hook_paths = [];
200
+		// menu_map
201
+		$this->_menu_map = $this->get_menu_map();
202
+	}
203
+
204
+
205
+	protected function _set_capability()
206
+	{
207
+		$capability       = empty($this->capability) ? $this->_menu_map->capability : $this->capability;
208
+		$this->capability = apply_filters('FHEE_' . $this->_menu_map->menu_slug . '_capability', $capability);
209
+	}
210
+
211
+
212
+	/**
213
+	 * initialize_admin_page
214
+	 * This method is what executes the loading of the specific page class for the given dir_name as called by the
215
+	 * EE_Admin_Init class.
216
+	 *
217
+	 * @return void
218
+	 * @uses    _initialize_admin_page()
219
+	 */
220
+	public function initialize_admin_page()
221
+	{
222
+		// let's check user access first
223
+		$this->_check_user_access();
224
+		if (! is_object($this->_loaded_page_object)) {
225
+			return;
226
+		}
227
+		$this->_loaded_page_object->route_admin_request();
228
+	}
229
+
230
+
231
+	/**
232
+	 * @param string $wp_page_slug
233
+	 * @throws EE_Error
234
+	 */
235
+	public function set_page_dependencies($wp_page_slug)
236
+	{
237
+		if (! $this->_load_page) {
238
+			return;
239
+		}
240
+		if (! is_object($this->_loaded_page_object)) {
241
+			$msg[] = esc_html__(
242
+				'We can\'t load the page because we\'re missing a valid page object that tells us what to load',
243
+				'event_espresso'
244
+			);
245
+			$msg[] = $msg[0] . "\r\n"
246
+					 . sprintf(
247
+						 esc_html__(
248
+							 'The custom slug you have set for this page is %s. This means we\'re looking for the class %s_Admin_Page (found in %s_Admin_Page.core.php) within your %s directory',
249
+							 'event_espresso'
250
+						 ),
251
+						 $this->_file_name,
252
+						 $this->_file_name,
253
+						 $this->_folder_path . $this->_file_name,
254
+						 $this->_menu_map->menu_slug
255
+					 );
256
+			throw new EE_Error(implode('||', $msg));
257
+		}
258
+		$this->_loaded_page_object->set_wp_page_slug($wp_page_slug);
259
+		$page_hook = 'load-' . $wp_page_slug;
260
+		// hook into page load hook so all page specific stuff gets loaded.
261
+		if (! empty($wp_page_slug)) {
262
+			add_action($page_hook, [$this->_loaded_page_object, 'load_page_dependencies']);
263
+		}
264
+	}
265
+
266
+
267
+	/**
268
+	 * This executes the initial page loads for EE_Admin pages to take care of any ajax or other code needing to run
269
+	 * before the load-page... hook. Note, the page loads are happening around the wp_init hook.
270
+	 *
271
+	 * @return void
272
+	 */
273
+	public function do_initial_loads()
274
+	{
275
+		// no loading or initializing if menu map is setup incorrectly.
276
+		if (empty($this->_menu_map) || is_array($this->_menu_map)) {
277
+			return;
278
+		}
279
+		$this->_initialize_admin_page();
280
+	}
281
+
282
+
283
+	/**
284
+	 * all we're doing here is setting the $_file_name property for later use.
285
+	 *
286
+	 * @return void
287
+	 */
288
+	private function _set_file_and_folder_name()
289
+	{
290
+		$bt = debug_backtrace();
291
+		// for more reliable determination of folder name
292
+		// we're using this to get the actual folder name of the CALLING class (i.e. the child class that extends this).  Why?  Because $this->menu_slug may be different than the folder name (to avoid conflicts with other plugins)
293
+		$class = get_class($this);
294
+		foreach ($bt as $index => $values) {
295
+			if (isset($values['class']) && $values['class'] == $class) {
296
+				$file_index         = $index - 1;
297
+				$this->_folder_name = basename(dirname($bt[ $file_index ]['file']));
298
+				if (! empty($this->_folder_name)) {
299
+					break;
300
+				}
301
+			}
302
+		}
303
+		$this->_folder_path = EE_ADMIN_PAGES . $this->_folder_name . '/';
304
+		$this->_file_name   = preg_replace('/^ee/', 'EE', $this->_folder_name);
305
+		$this->_file_name   = ucwords(str_replace('_', ' ', $this->_file_name));
306
+		$this->_file_name   = str_replace(' ', '_', $this->_file_name);
307
+	}
308
+
309
+
310
+	/**
311
+	 * This automatically checks if we have a hook class in the loaded child directory.  If we DO then we will register
312
+	 * it with the appropriate pages.  That way all we have to do is make sure the file is named correctly and
313
+	 * "dropped" in. Example: if we wanted to set this up for Messages hooking into Events then we would do:
314
+	 * events_Messages_Hooks.class.php
315
+	 *
316
+	 * @param bool $extend This indicates whether we're checking the extend directory for any register_hooks
317
+	 *                     files/classes
318
+	 * @return array
319
+	 */
320
+	public function register_hooks($extend = false)
321
+	{
322
+
323
+		// get a list of files in the directory that have the "Hook" in their name an
324
+		// if this is an extended check (i.e. caf is active) then we will scan the caffeinated/extend directory first and any hook files that are found will be have their reference added to the $_files_hook array property.  Then, we make sure that when we loop through the core decaf directories to find hook files that we skip over any hooks files that have already been set by caf.
325
+		if ($extend) {
326
+			$hook_files_glob_path = apply_filters(
327
+				'FHEE__EE_Admin_Page_Init__register_hooks__hook_files_glob_path__extend',
328
+				EE_CORE_CAF_ADMIN_EXTEND
329
+				. $this->_folder_name
330
+				. '/*'
331
+				. $this->_file_name
332
+				. '_Hooks_Extend.class.php'
333
+			);
334
+			$this->_hook_paths    = $this->_register_hook_files($hook_files_glob_path, $extend);
335
+		}
336
+		// loop through decaf folders
337
+		$hook_files_glob_path = apply_filters(
338
+			'FHEE__EE_Admin_Page_Init__register_hooks__hook_files_glob_path',
339
+			$this->_folder_path . '*' . $this->_file_name . '_Hooks.class.php'
340
+		);
341
+		$this->_hook_paths    = array_merge(
342
+			$this->_register_hook_files($hook_files_glob_path),
343
+			$this->_hook_paths
344
+		);  // making sure any extended hook paths are later in the array than the core hook paths!
345
+		return $this->_hook_paths;
346
+	}
347
+
348
+
349
+	protected function _register_hook_files($hook_files_glob_path, $extend = false)
350
+	{
351
+		$hook_paths = glob($hook_files_glob_path);
352
+		if (empty($hook_paths)) {
353
+			return [];
354
+		}
355
+		foreach ($hook_paths as $file) {
356
+			// lets get the linked admin.
357
+			$hook_file = $extend
358
+				? str_replace(EE_CORE_CAF_ADMIN_EXTEND . $this->_folder_name . '/', '', $file)
359
+				: str_replace($this->_folder_path, '', $file);
360
+			$replace         = $extend
361
+				? '_' . $this->_file_name . '_Hooks_Extend.class.php'
362
+				: '_' . $this->_file_name . '_Hooks.class.php';
363
+			$rel_admin       = str_replace($replace, '', $hook_file);
364
+			$rel_admin       = strtolower($rel_admin);
365
+			// make sure we haven't already got a hook setup for this page path
366
+			if (in_array($rel_admin, $this->_files_hooked)) {
367
+				continue;
368
+			}
369
+			$this->hook_file = $hook_file;
370
+			$rel_admin_hook = 'FHEE_do_other_page_hooks_' . $rel_admin;
371
+			add_filter($rel_admin_hook, [$this, 'load_admin_hook']);
372
+			$this->_files_hooked[] = $rel_admin;
373
+		}
374
+		return $hook_paths;
375
+	}
376
+
377
+
378
+	public function load_admin_hook($registered_pages)
379
+	{
380
+		return array_merge((array) $this->hook_file, $registered_pages);
381
+	}
382
+
383
+
384
+	/**
385
+	 * _initialize_admin_page
386
+	 *
387
+	 * @see  initialize_admin_page() for info
388
+	 */
389
+	protected function _initialize_admin_page()
390
+	{
391
+		// JUST CHECK WE'RE ON RIGHT PAGE.
392
+		$page = $this->request->getRequestParam('page');
393
+		$page = $this->request->getRequestParam('current_page', $page);
394
+		$menu_slug = $this->_menu_map->menu_slug;
395
+
396
+
397
+		if ($this->_routing && ($page === '' || $page !== $menu_slug)) {
398
+			// not on the right page so let's get out.
399
+			return;
400
+		}
401
+		$this->_load_page = true;
402
+
403
+		// we don't need to do a page_request check here because it's only called via WP menu system.
404
+		$admin_page  = $this->_file_name . '_Admin_Page';
405
+		$hook_suffix = "{$menu_slug}_{$admin_page}";
406
+		$admin_page  = apply_filters(
407
+			"FHEE__EE_Admin_Page_Init___initialize_admin_page__admin_page__{$hook_suffix}",
408
+			$admin_page
409
+		);
410
+		if (empty($admin_page)) {
411
+			return;
412
+		}
413
+		// define requested admin page class name then load the file and instantiate
414
+		$path_to_file = str_replace(['\\', '/'], '/', $this->_folder_path . $admin_page . '.core.php');
415
+		// so if the file would be in EE_ADMIN/attendees/Attendee_Admin_Page.core.php, the filter would be:
416
+		// FHEE__EE_Admin_Page_Init___initialize_admin_page__path_to_file__attendees_Attendee_Admin_Page
417
+		$path_to_file = apply_filters(
418
+			"FHEE__EE_Admin_Page_Init___initialize_admin_page__path_to_file__{$hook_suffix}",
419
+			$path_to_file
420
+		);
421
+		if (! is_readable($path_to_file)) {
422
+			return;
423
+		}
424
+		// This is a place where EE plugins can hook in to make sure their own files are required in the appropriate place
425
+		do_action('AHEE__EE_Admin_Page___initialize_admin_page__before_initialization');
426
+		do_action("AHEE__EE_Admin_Page___initialize_admin_page__before_initialization_{$menu_slug}");
427
+		require_once($path_to_file);
428
+		$this->_loaded_page_object = $this->loader->getShared($admin_page, [$this->_routing]);
429
+
430
+		do_action('AHEE__EE_Admin_Page___initialize_admin_page__after_initialization');
431
+		do_action("AHEE__EE_Admin_Page___initialize_admin_page__after_initialization_{$menu_slug}");
432
+	}
433
+
434
+
435
+	public function get_admin_page_name()
436
+	{
437
+		return $this->_file_name . '_Admin_Page';
438
+	}
439
+
440
+
441
+	/**
442
+	 * @return mixed
443
+	 */
444
+	public function loaded_page_object()
445
+	{
446
+		return $this->_loaded_page_object;
447
+	}
448
+
449
+	/**
450
+	 * _check_user_access
451
+	 * verifies user access for this admin page.  If no user access is available then let's gracefully exit with a
452
+	 * WordPress die message.
453
+	 *
454
+	 * @return void  wp_die if fail
455
+	 */
456
+	private function _check_user_access()
457
+	{
458
+		if (
459
+			! EE_Registry::instance()->CAP->current_user_can(
460
+				$this->_menu_map->capability,
461
+				$this->_menu_map->menu_slug
462
+			)
463
+		) {
464
+			wp_die(esc_html__('You don\'t have access to this page.', 'event_espresso'), '', ['back_link' => true]);
465
+		}
466
+	}
467 467
 }
Please login to merge, or discard this patch.
Spacing   +26 added lines, -26 removed lines patch added patch discarded remove patch
@@ -100,7 +100,7 @@  discard block
 block discarded – undo
100 100
         // load initial stuff.
101 101
         $this->_set_file_and_folder_name();
102 102
         $this->_set_menu_map();
103
-        if (! $this->verifyMenuMapSet()) {
103
+        if ( ! $this->verifyMenuMapSet()) {
104 104
             return;
105 105
         }
106 106
         // set default capability
@@ -140,7 +140,7 @@  discard block
 block discarded – undo
140 140
     {
141 141
         if (empty($this->_menu_map) || is_array($this->_menu_map)) {
142 142
             EE_Error::doing_it_wrong(
143
-                get_class($this) . '::$_menu_map',
143
+                get_class($this).'::$_menu_map',
144 144
                 sprintf(
145 145
                     esc_html__(
146 146
                         'The EE4 addon with the class %s is setting up the _menu_map property incorrectly for this version of EE core.  Please see Admin_Page_Init class examples in core for the new way of setting this property up.',
@@ -178,7 +178,7 @@  discard block
 block discarded – undo
178 178
     {
179 179
         wp_register_style(
180 180
             'espresso_menu',
181
-            EE_ADMIN_URL . 'assets/admin-menu-styles.css',
181
+            EE_ADMIN_URL.'assets/admin-menu-styles.css',
182 182
             ['dashicons'],
183 183
             EVENT_ESPRESSO_VERSION
184 184
         );
@@ -205,7 +205,7 @@  discard block
 block discarded – undo
205 205
     protected function _set_capability()
206 206
     {
207 207
         $capability       = empty($this->capability) ? $this->_menu_map->capability : $this->capability;
208
-        $this->capability = apply_filters('FHEE_' . $this->_menu_map->menu_slug . '_capability', $capability);
208
+        $this->capability = apply_filters('FHEE_'.$this->_menu_map->menu_slug.'_capability', $capability);
209 209
     }
210 210
 
211 211
 
@@ -221,7 +221,7 @@  discard block
 block discarded – undo
221 221
     {
222 222
         // let's check user access first
223 223
         $this->_check_user_access();
224
-        if (! is_object($this->_loaded_page_object)) {
224
+        if ( ! is_object($this->_loaded_page_object)) {
225 225
             return;
226 226
         }
227 227
         $this->_loaded_page_object->route_admin_request();
@@ -234,15 +234,15 @@  discard block
 block discarded – undo
234 234
      */
235 235
     public function set_page_dependencies($wp_page_slug)
236 236
     {
237
-        if (! $this->_load_page) {
237
+        if ( ! $this->_load_page) {
238 238
             return;
239 239
         }
240
-        if (! is_object($this->_loaded_page_object)) {
240
+        if ( ! is_object($this->_loaded_page_object)) {
241 241
             $msg[] = esc_html__(
242 242
                 'We can\'t load the page because we\'re missing a valid page object that tells us what to load',
243 243
                 'event_espresso'
244 244
             );
245
-            $msg[] = $msg[0] . "\r\n"
245
+            $msg[] = $msg[0]."\r\n"
246 246
                      . sprintf(
247 247
                          esc_html__(
248 248
                              'The custom slug you have set for this page is %s. This means we\'re looking for the class %s_Admin_Page (found in %s_Admin_Page.core.php) within your %s directory',
@@ -250,15 +250,15 @@  discard block
 block discarded – undo
250 250
                          ),
251 251
                          $this->_file_name,
252 252
                          $this->_file_name,
253
-                         $this->_folder_path . $this->_file_name,
253
+                         $this->_folder_path.$this->_file_name,
254 254
                          $this->_menu_map->menu_slug
255 255
                      );
256 256
             throw new EE_Error(implode('||', $msg));
257 257
         }
258 258
         $this->_loaded_page_object->set_wp_page_slug($wp_page_slug);
259
-        $page_hook = 'load-' . $wp_page_slug;
259
+        $page_hook = 'load-'.$wp_page_slug;
260 260
         // hook into page load hook so all page specific stuff gets loaded.
261
-        if (! empty($wp_page_slug)) {
261
+        if ( ! empty($wp_page_slug)) {
262 262
             add_action($page_hook, [$this->_loaded_page_object, 'load_page_dependencies']);
263 263
         }
264 264
     }
@@ -294,13 +294,13 @@  discard block
 block discarded – undo
294 294
         foreach ($bt as $index => $values) {
295 295
             if (isset($values['class']) && $values['class'] == $class) {
296 296
                 $file_index         = $index - 1;
297
-                $this->_folder_name = basename(dirname($bt[ $file_index ]['file']));
298
-                if (! empty($this->_folder_name)) {
297
+                $this->_folder_name = basename(dirname($bt[$file_index]['file']));
298
+                if ( ! empty($this->_folder_name)) {
299 299
                     break;
300 300
                 }
301 301
             }
302 302
         }
303
-        $this->_folder_path = EE_ADMIN_PAGES . $this->_folder_name . '/';
303
+        $this->_folder_path = EE_ADMIN_PAGES.$this->_folder_name.'/';
304 304
         $this->_file_name   = preg_replace('/^ee/', 'EE', $this->_folder_name);
305 305
         $this->_file_name   = ucwords(str_replace('_', ' ', $this->_file_name));
306 306
         $this->_file_name   = str_replace(' ', '_', $this->_file_name);
@@ -331,17 +331,17 @@  discard block
 block discarded – undo
331 331
                 . $this->_file_name
332 332
                 . '_Hooks_Extend.class.php'
333 333
             );
334
-            $this->_hook_paths    = $this->_register_hook_files($hook_files_glob_path, $extend);
334
+            $this->_hook_paths = $this->_register_hook_files($hook_files_glob_path, $extend);
335 335
         }
336 336
         // loop through decaf folders
337 337
         $hook_files_glob_path = apply_filters(
338 338
             'FHEE__EE_Admin_Page_Init__register_hooks__hook_files_glob_path',
339
-            $this->_folder_path . '*' . $this->_file_name . '_Hooks.class.php'
339
+            $this->_folder_path.'*'.$this->_file_name.'_Hooks.class.php'
340 340
         );
341
-        $this->_hook_paths    = array_merge(
341
+        $this->_hook_paths = array_merge(
342 342
             $this->_register_hook_files($hook_files_glob_path),
343 343
             $this->_hook_paths
344
-        );  // making sure any extended hook paths are later in the array than the core hook paths!
344
+        ); // making sure any extended hook paths are later in the array than the core hook paths!
345 345
         return $this->_hook_paths;
346 346
     }
347 347
 
@@ -355,11 +355,11 @@  discard block
 block discarded – undo
355 355
         foreach ($hook_paths as $file) {
356 356
             // lets get the linked admin.
357 357
             $hook_file = $extend
358
-                ? str_replace(EE_CORE_CAF_ADMIN_EXTEND . $this->_folder_name . '/', '', $file)
358
+                ? str_replace(EE_CORE_CAF_ADMIN_EXTEND.$this->_folder_name.'/', '', $file)
359 359
                 : str_replace($this->_folder_path, '', $file);
360 360
             $replace         = $extend
361
-                ? '_' . $this->_file_name . '_Hooks_Extend.class.php'
362
-                : '_' . $this->_file_name . '_Hooks.class.php';
361
+                ? '_'.$this->_file_name.'_Hooks_Extend.class.php'
362
+                : '_'.$this->_file_name.'_Hooks.class.php';
363 363
             $rel_admin       = str_replace($replace, '', $hook_file);
364 364
             $rel_admin       = strtolower($rel_admin);
365 365
             // make sure we haven't already got a hook setup for this page path
@@ -367,7 +367,7 @@  discard block
 block discarded – undo
367 367
                 continue;
368 368
             }
369 369
             $this->hook_file = $hook_file;
370
-            $rel_admin_hook = 'FHEE_do_other_page_hooks_' . $rel_admin;
370
+            $rel_admin_hook = 'FHEE_do_other_page_hooks_'.$rel_admin;
371 371
             add_filter($rel_admin_hook, [$this, 'load_admin_hook']);
372 372
             $this->_files_hooked[] = $rel_admin;
373 373
         }
@@ -401,7 +401,7 @@  discard block
 block discarded – undo
401 401
         $this->_load_page = true;
402 402
 
403 403
         // we don't need to do a page_request check here because it's only called via WP menu system.
404
-        $admin_page  = $this->_file_name . '_Admin_Page';
404
+        $admin_page  = $this->_file_name.'_Admin_Page';
405 405
         $hook_suffix = "{$menu_slug}_{$admin_page}";
406 406
         $admin_page  = apply_filters(
407 407
             "FHEE__EE_Admin_Page_Init___initialize_admin_page__admin_page__{$hook_suffix}",
@@ -411,14 +411,14 @@  discard block
 block discarded – undo
411 411
             return;
412 412
         }
413 413
         // define requested admin page class name then load the file and instantiate
414
-        $path_to_file = str_replace(['\\', '/'], '/', $this->_folder_path . $admin_page . '.core.php');
414
+        $path_to_file = str_replace(['\\', '/'], '/', $this->_folder_path.$admin_page.'.core.php');
415 415
         // so if the file would be in EE_ADMIN/attendees/Attendee_Admin_Page.core.php, the filter would be:
416 416
         // FHEE__EE_Admin_Page_Init___initialize_admin_page__path_to_file__attendees_Attendee_Admin_Page
417 417
         $path_to_file = apply_filters(
418 418
             "FHEE__EE_Admin_Page_Init___initialize_admin_page__path_to_file__{$hook_suffix}",
419 419
             $path_to_file
420 420
         );
421
-        if (! is_readable($path_to_file)) {
421
+        if ( ! is_readable($path_to_file)) {
422 422
             return;
423 423
         }
424 424
         // This is a place where EE plugins can hook in to make sure their own files are required in the appropriate place
@@ -434,7 +434,7 @@  discard block
 block discarded – undo
434 434
 
435 435
     public function get_admin_page_name()
436 436
     {
437
-        return $this->_file_name . '_Admin_Page';
437
+        return $this->_file_name.'_Admin_Page';
438 438
     }
439 439
 
440 440
 
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.011');
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.011');
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.
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.