-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathController.php
More file actions
123 lines (108 loc) · 2.65 KB
/
Controller.php
File metadata and controls
123 lines (108 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace BEA\PB\Controllers;
use BEA\PB\Routes\Router;
/**
* This class is the base class for the controllers
* It allows you to get the current controller based on the query var
* Basically you have to :
* - extend this class
* - fill the page_slug property
* - Add action on the wp tag and add the elements
*
* All the controllers needs to be implemented on all classes
*
* Class Controller
* @package BEA\PB
*/
abstract class Controller {
/**
* The page slug on the rewrite rule
*
* @var string
*/
protected $page_slug;
/**
* The query var page slug to check
* This is the same slug as the slug used on the rewrite
* Like in the hm_rewrite rule:
*
* 'query' => 'index.php?registration=true&step=1&bea_pb_page=registration',
* Here the "bea_pb_page" is the slug to check on
*
* @var string
*/
protected $page_query_var = 'bea_pb_page';
/**
* Check if the current page rewrited is the right page to execute or not methods
*
* @return bool
* @author Nicolas Juen
*/
protected function is_page() {
return get_query_var( $this->page_query_var, null ) === $this->page_slug;
}
/**
* Return the form url base
* Dashboard/$this->page_slug/
*
* @param array $args arg to add
*
* @return false|string
* @author Nicolas Juen
*/
public function get_form_url( $args = [] ) {
return Router::get_url_complex( [ $this->page_slug ], $args );
}
/**
* Redirect to the form url with the data
*
* @param array $args
*
* @author Nicolas Juen
*/
protected function redirect( $args = [] ): void {
wp_safe_redirect( $this->get_form_url( $args ) );
exit;
}
/**
* Get among all the controller the right one for the current page
*
* @author Nicolas Juen
* @return \WP_Error|self
*/
public static function get_current_controller() {
$classes = array_filter( get_declared_classes(), [ __CLASS__, 'filter_classes' ] );
// Check there is classes
if ( empty( $classes ) ) {
return new \WP_Error( 'no-controller', 'No controller found' );
}
// Get the filtered controller
$class = reset( $classes );
// Give the controller full
return $class::get_instance();
}
/**
* Get among all classes the right one
*
* @param string $class
*
* @return bool
* @author Nicolas Juen
*/
public static function filter_classes( string $class_name ): bool {
if ( false === is_subclass_of( $class_name, '\BEA\PB\Controller', true ) ) {
return false;
}
return $class_name::get_instance()->is_page();
}
/**
* Get all the default data for the controller
* Like the form data
*
* @return array
* @author Nicolas Juen
*/
public function get_default_data() {
return [];
}
}