class-controller.php
2.68 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
declare( strict_types=1 );
namespace LearnDash\Hub\Framework;
use LearnDash\Hub\Component\API;
/**
* Class Controller
*
* @package LearnDash\Supports
*/
class Controller {
/**
* The page slug.
*
* @var string
*/
protected $slug;
/**
* @var string
*/
protected $layout = '';
/**
* The view render engine.
*
* @var View
*/
protected $render;
/**
* @var string
*/
protected $module_dir;
/**
* Controller constructor.
*/
public function __construct() {
$tmp_path = ( new \ReflectionClass( static::class ) )->getFileName();
$this->module_dir = trailingslashit(
substr_replace(
$tmp_path,
'',
intval(
strpos(
$tmp_path,
DIRECTORY_SEPARATOR . 'controller'
)
)
)
);
$this->render = new View( $this->module_dir . 'view' );
}
/**
* A helper to quickly create a page or sub page
*
* @param $title
* @param $slug
* @param $callback
* @param null $icon
* @param null $parent_slug
*/
public function register_page( $title, $slug, $callback, $parent_slug = null, $icon = null ) {
$hook = is_multisite() ? 'network_admin_menu' : 'admin_menu';
$function = function () use ( $title, $slug, $callback, $hook, $parent_slug, $icon ) {
$cap = is_multisite() ? 'manage_network_options' : 'manage_options';
if ( null === $parent_slug ) {
add_menu_page( $title, $title, $cap, $slug, $callback, $icon );
} else {
add_submenu_page( $parent_slug, $title, $title, $cap, $slug, $callback );
}
};
add_action( $hook, $function );
}
/**
* @param $view_file
* @param array $params
* @param bool $echo
*
* @return bool|string
*/
public function render( string $view_file, array $params = array(), bool $echo = true ) {
// assign controller to this
if ( ! isset( $params['controller'] ) ) {
$params['controller'] = $this;
}
$content = $this->render->render( $view_file, $params );
if ( ! empty( $this->layout ) ) {
$template = new View( $this->module_dir . DIRECTORY_SEPARATOR . 'layouts' );
$content = $template->render(
$this->layout,
array_merge(
$params,
array(
'controller' => $this,
'contents' => $content,
)
)
);
}
if ( false === $echo ) {
return $content;
}
echo $content;
}
/**
* Return all the data that requires for frontend.
*
* @return array
*/
protected function make_data(): array {
return array();
}
/**
* The API instance.
*
* @var API
*/
protected $api;
/**
* Get the API instance.
*
* @return API
*/
protected function get_api() {
if ( ! $this->api instanceof API ) {
$this->api = new API();
}
return $this->api;
}
}