-
Notifications
You must be signed in to change notification settings - Fork 2
/
class.controller.php
112 lines (83 loc) · 2.49 KB
/
class.controller.php
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
<?php
namespace WPS;
class Controller {
public $post_type,
$current_query,
$template,
$request_action,
$request_data;
public function __construct($post_type, $current_query = null, $template) {
$this->post_type = $post_type;
$this->current_query = $this->set_current_query($current_query);
$this->template = $template;
$this->set_request_action();
$this->render_post_route_template($template);
}
public function index() {
include $this->template;
}
public function single() {
include $this->template;
}
public function create() {}
private function render_post_route_template($template) {
if(!empty($this->request_action) && $this->request_action !== 'single') {
if(method_exists($this, $this->request_action)) {
$this->{$this->request_action}();
return;
}
}
if(!$this->is_index_of_post_type()) {
$this->render_single_template($template);
return;
}
$this->render_index_template($template);
}
private function set_request_action() {
$request_method = !empty($_SERVER['REQUEST_METHOD']) ?
$_SERVER['REQUEST_METHOD'] :
'GET';
$request_actions = [
'POST' => 'create',
'GET' => 'single'
];
$this->request_data = !empty($_REQUEST) ? $_REQUEST : false;
$this->request_action = array_key_exists($request_method, $request_actions) ?
$request_actions[$request_method] :
false;
}
private function is_index_of_post_type() {
$is_blog_index = is_home();
$this->index_template_prefix = $is_blog_index ? 'index' : 'archive';
return is_post_type_archive() || $is_blog_index;
}
private function render_single_template($template) {
if(is_single()) {
$single_template = WPS_VIEWS_DIR .
"/{$this->post_type}/single-{$this->post_type}.php";
if(file_exists($single_template)) {
$this->template = $single_template;
$this->single();
return;
}
}
$this->single();
return;
}
private function render_index_template($template) {
$archive_template = WPS_VIEWS_DIR .
"/{$this->post_type}/{$this->index_template_prefix}-{$this->post_type}" .
'.php';
if(file_exists($archive_template)) {
$this->template = $archive_template;
$this->index();
return;
}
$this->index();
}
private function set_current_query($query) {
if(!empty($current_query)) return $query;
global $wp_query;
return $wp_query;
}
}