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\Action\ActionNotFoundException; |
15
|
|
|
use LIN3S\WPSymfonyForm\Action\LocalStorageAction; |
16
|
|
|
use LIN3S\WPSymfonyForm\Admin\Storage\InMemoryStorage; |
17
|
|
|
use LIN3S\WPSymfonyForm\Admin\Storage\Storage; |
18
|
|
|
use LIN3S\WPSymfonyForm\Admin\Storage\YamlStorage; |
19
|
|
|
use LIN3S\WPSymfonyForm\Admin\Views\Components\FormsTable; |
20
|
|
|
use LIN3S\WPSymfonyForm\Admin\Views\Components\LogsTable; |
21
|
|
|
use LIN3S\WPSymfonyForm\Admin\Views\Form; |
22
|
|
|
use LIN3S\WPSymfonyForm\Admin\Views\General; |
23
|
|
|
use LIN3S\WPSymfonyForm\Registry\FormWrapperRegistry; |
24
|
|
|
use LIN3S\WPSymfonyForm\Wrapper\FormWrapper; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Main admin class. |
28
|
|
|
* |
29
|
|
|
* @author Beñat Espiña <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class Admin |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* The form wrapper registry. |
35
|
|
|
* |
36
|
|
|
* @var FormWrapperRegistry |
37
|
|
|
*/ |
38
|
|
|
private $formWrapperRegistry; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Array which contains the forms. |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
private $forms; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Constructor. |
49
|
|
|
* |
50
|
|
|
* @param FormWrapperRegistry $formWrapperRegistry The form wrapper registry |
51
|
|
|
*/ |
52
|
|
|
public function __construct(FormWrapperRegistry $formWrapperRegistry) |
53
|
|
|
{ |
54
|
|
|
$this->formWrapperRegistry = $formWrapperRegistry; |
55
|
|
|
$this->forms = []; |
56
|
|
|
add_filter('set-screen-option', function ($status, $option, $value) { |
|
|
|
|
57
|
|
|
return $value; |
58
|
|
|
}, 10, 3); |
|
|
|
|
59
|
|
|
|
60
|
|
|
add_action('admin_menu', [$this, 'menu']); |
|
|
|
|
61
|
|
|
add_action('admin_menu', [$this, 'subMenu']); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Loads the menu inside WordPress admin sidebar. |
66
|
|
|
*/ |
67
|
|
|
public function menu() |
68
|
|
|
{ |
69
|
|
|
$view = new General( |
70
|
|
|
new FormsTable( |
71
|
|
|
new InMemoryStorage( |
72
|
|
|
$this->forms() |
73
|
|
|
) |
74
|
|
|
) |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$menu = add_menu_page( |
78
|
|
|
'Symfony Forms', |
79
|
|
|
'Symfony Forms', |
80
|
|
|
'manage_options', |
81
|
|
|
'symfony-form', |
82
|
|
|
[$view, 'display'] |
83
|
|
|
); |
84
|
|
|
add_action("load-$menu", [$view, 'screenOptions']); |
|
|
|
|
85
|
|
|
|
86
|
|
|
add_submenu_page( |
87
|
|
|
'symfony-form', |
88
|
|
|
__('General', \WPSymfonyForm::TRANSLATION_DOMAIN), |
89
|
|
|
__('General', \WPSymfonyForm::TRANSLATION_DOMAIN), |
90
|
|
|
'manage_options', |
91
|
|
|
'symfony-form' |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Loads the form sub menus inside WordPress admin sidebar. |
97
|
|
|
*/ |
98
|
|
|
public function subMenu() |
99
|
|
|
{ |
100
|
|
|
foreach ($this->formWrapperRegistry->get() as $key => $formWrapper) { |
|
|
|
|
101
|
|
|
$slug = 'wp-symfony-form-' . preg_replace('/\s+/', '', strtolower($formWrapper->getName())); |
102
|
|
|
$name = ucfirst($formWrapper->getName()); |
103
|
|
|
$view = new Form( |
104
|
|
|
$name, |
105
|
|
|
new LogsTable( |
106
|
|
|
$formWrapper->getName(), |
107
|
|
|
$this->storage($formWrapper) |
108
|
|
|
) |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$subMenu = add_submenu_page( |
112
|
|
|
'symfony-form', |
113
|
|
|
$name, |
114
|
|
|
$name, |
115
|
|
|
'manage_options', |
116
|
|
|
$slug, |
117
|
|
|
[$view, 'display'] |
118
|
|
|
); |
119
|
|
|
add_action("load-$subMenu", [$view, 'screenOptions']); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Resolves and gets the proper storage for the given form wrapper. |
125
|
|
|
* |
126
|
|
|
* @param FormWrapper $formWrapper The form wrapper |
127
|
|
|
* |
128
|
|
|
* @throws ActionNotFoundException when required action type not found |
129
|
|
|
* |
130
|
|
|
* @return Storage |
131
|
|
|
*/ |
132
|
|
|
private function storage(FormWrapper $formWrapper) |
133
|
|
|
{ |
134
|
|
|
foreach ($formWrapper->getSuccessActions() as $action) { |
135
|
|
|
if ($action instanceof LocalStorageAction) { |
136
|
|
|
return new YamlStorage(); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
throw new ActionNotFoundException(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Populates the forms with its name and its link, returning the built array. |
145
|
|
|
* |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
|
|
private function forms() |
149
|
|
|
{ |
150
|
|
|
if (!empty($this->forms)) { |
151
|
|
|
return $this->forms; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
foreach ($this->formWrapperRegistry->get() as $key => $formWrapper) { |
|
|
|
|
155
|
|
|
$slug = 'wp-symfony-form-' . preg_replace('/\s+/', '', strtolower($formWrapper->getName())); |
156
|
|
|
$name = ucfirst($formWrapper->getName()); |
157
|
|
|
|
158
|
|
|
$this->forms[] = [ |
159
|
|
|
'name' => $name, |
160
|
|
|
'link' => '<a href="?page=' . $slug . '">/wp-admin/admin.php?page=' . $slug . '</a>', |
161
|
|
|
]; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $this->forms; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|