1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the WPSymfonyForm plugin. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2015-2016 LIN3S <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace LIN3S\WPSymfonyForm\Admin; |
13
|
|
|
|
14
|
|
|
use LIN3S\WPSymfonyForm\Admin\Storage\InMemoryStorage; |
15
|
|
|
use LIN3S\WPSymfonyForm\Admin\Storage\Storage; |
16
|
|
|
use LIN3S\WPSymfonyForm\Admin\Storage\YamlStorage; |
17
|
|
|
use LIN3S\WPSymfonyForm\Admin\Views\Components\FormsTable; |
18
|
|
|
use LIN3S\WPSymfonyForm\Admin\Views\Components\LogsTable; |
19
|
|
|
use LIN3S\WPSymfonyForm\Admin\Views\Form; |
20
|
|
|
use LIN3S\WPSymfonyForm\Admin\Views\General; |
21
|
|
|
use LIN3S\WPSymfonyForm\Registry\FormWrapperRegistry; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Main admin class. |
25
|
|
|
* |
26
|
|
|
* @author Beñat Espiña <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class Admin |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* The logs table. |
32
|
|
|
* |
33
|
|
|
* @var LogsTable |
34
|
|
|
*/ |
35
|
|
|
public $logsTable; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The form wrapper registry. |
39
|
|
|
* |
40
|
|
|
* @var FormWrapperRegistry |
41
|
|
|
*/ |
42
|
|
|
private $formWrapperRegistry; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Array which contains the forms. |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
private $forms; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The storage strategy. |
53
|
|
|
* |
54
|
|
|
* @var Storage |
55
|
|
|
*/ |
56
|
|
|
private $storage; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Constructor. |
60
|
|
|
* |
61
|
|
|
* @param FormWrapperRegistry $formWrapperRegistry The form wrapper registry |
62
|
|
|
*/ |
63
|
|
|
public function __construct(FormWrapperRegistry $formWrapperRegistry) |
64
|
|
|
{ |
65
|
|
|
$this->storage = new YamlStorage(); // TODO: This is hardcoded for now |
66
|
|
|
$this->formWrapperRegistry = $formWrapperRegistry; |
67
|
|
|
$this->forms = []; |
68
|
|
|
add_filter('set-screen-option', function ($status, $option, $value) { |
|
|
|
|
69
|
|
|
return $value; |
70
|
|
|
}, 10, 3); |
|
|
|
|
71
|
|
|
|
72
|
|
|
add_action('admin_menu', [$this, 'menu']); |
|
|
|
|
73
|
|
|
add_action('admin_menu', [$this, 'subMenu']); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Loads the menu inside WordPress admin sidebar. |
78
|
|
|
*/ |
79
|
|
|
public function menu() |
80
|
|
|
{ |
81
|
|
|
$view = new General( |
82
|
|
|
new FormsTable( |
83
|
|
|
new InMemoryStorage( |
84
|
|
|
$this->forms() |
85
|
|
|
) |
86
|
|
|
) |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$menu = add_menu_page( |
90
|
|
|
'Symfony Forms', |
91
|
|
|
'Symfony Forms', |
92
|
|
|
'manage_options', |
93
|
|
|
'symfony-form', |
94
|
|
|
[$view, 'display'] |
95
|
|
|
); |
96
|
|
|
add_action("load-$menu", [$view, 'screenOptions']); |
|
|
|
|
97
|
|
|
|
98
|
|
|
add_submenu_page( |
99
|
|
|
'symfony-form', |
100
|
|
|
__('General', \WPSymfonyForm::TRANSLATION_DOMAIN), |
101
|
|
|
__('General', \WPSymfonyForm::TRANSLATION_DOMAIN), |
102
|
|
|
'manage_options', |
103
|
|
|
'symfony-form' |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Loads the form sub menus inside WordPress admin sidebar. |
109
|
|
|
*/ |
110
|
|
|
public function subMenu() |
111
|
|
|
{ |
112
|
|
|
foreach ($this->formWrapperRegistry->get() as $key => $formWrapper) { |
|
|
|
|
113
|
|
|
$slug = 'wp-symfony-form-' . preg_replace('/\s+/', '', strtolower($formWrapper->getName())); |
114
|
|
|
$name = ucfirst($formWrapper->getName()); |
115
|
|
|
$view = new Form( |
116
|
|
|
$name, |
117
|
|
|
new LogsTable( |
118
|
|
|
$formWrapper->getName(), |
119
|
|
|
$this->storage |
120
|
|
|
) |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
$subMenu = add_submenu_page( |
124
|
|
|
'symfony-form', |
125
|
|
|
$name, |
126
|
|
|
$name, |
127
|
|
|
'manage_options', |
128
|
|
|
$slug, |
129
|
|
|
[$view, 'display'] |
130
|
|
|
); |
131
|
|
|
add_action("load-$subMenu", [$view, 'screenOptions']); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Populates the forms with its name and its link, returning the built array. |
137
|
|
|
* |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
private function forms() |
141
|
|
|
{ |
142
|
|
|
if (!empty($this->forms)) { |
143
|
|
|
return $this->forms; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
foreach ($this->formWrapperRegistry->get() as $key => $formWrapper) { |
|
|
|
|
147
|
|
|
$slug = 'wp-symfony-form-' . preg_replace('/\s+/', '', strtolower($formWrapper->getName())); |
148
|
|
|
$name = ucfirst($formWrapper->getName()); |
149
|
|
|
|
150
|
|
|
$this->forms[] = [ |
151
|
|
|
'name' => $name, |
152
|
|
|
'link' => '<a href="?page=' . $slug . '">/wp-admin/admin.php?page=' . $slug . '</a>', |
153
|
|
|
]; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $this->forms; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|