PluginProbe
Responsive Menu – Create Mobile-Friendly Menu / 3.1.13
Responsive Menu – Create Mobile-Friendly Menu v3.1.13
4.7.3 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.10 4.1.11 4.1.12 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2.0 All 90 releases
← All changes | app/Controllers/AdminController.php +20 -261 4.1.33.1.13 View file →
@@ -6,220 +6,44 @@
6 6 use ResponsiveMenu\Validation\Validator;
7 7 use ResponsiveMenu\Tasks\UpdateOptionsTask;
8 8 use ResponsiveMenu\Collections\OptionsCollection;
9 9
10 -/**
11 -* Entry point for all admin functionality.
12 -*
13 -* All routing for the admin comes through the functions below. When any
14 -* button is pressed in the admin view, it will come through here.
15 -*
16 -* @author Peter Featherstone <peter@featherstone.me>
17 -*
18 -* @since 3.0
19 -*/
20 10 class AdminController {
21 11
22 - /**
23 - * Constructor for setting up the AdminController.
24 - *
25 - * The constructor allows us to switch implementations for managing options
26 - * and for rendering views. Useful for switching out mocked or stubbed
27 - * classes during testing.
28 - *
29 - * @author Peter Featherstone <peter@featherstone.me>
30 - *
31 - * @since 3.0
32 - *
33 - * @param OptionManager $manager Instance of a Management options class.
34 - * @param View $view Instance of a View class for rendering.
35 - */
36 12 public function __construct(OptionManager $manager, View $view) {
37 13 $this->manager = $manager;
38 14 $this->view = $view;
39 15 }
40 16
41 - /**
42 - * Main GET route for the admin section.
43 - *
44 - * This is the default view for the plugin on an initial GET request to the
45 - * page.
46 - *
47 - * @author Peter Featherstone <peter@featherstone.me>
48 - *
49 - * @since 3.0
50 - *
51 - * @return string Output HTML from rendered view.
52 - */
53 - public function index() {
17 + public function index($nav_menus, $location_menus) {
54 18 return $this->view->render(
55 - 'admin/main.html.twig', ['options' => $this->manager->all()]
56 - );
57 - }
58 -
59 - /**
60 - * Rebuild database POST route for the admin section.
61 - *
62 - * This route is called when the Rebuild Database button is clicked from
63 - * inside the admin section. The intention is to set the version back to
64 - * a pre 3.0 version, which will then force a rebuild.
65 - *
66 - * @author Peter Featherstone <peter@featherstone.me>
67 - *
68 - * @since 3.0
69 - * @param bool $valid_nonce Is the form nonce valid or not.
70 - *
71 - * @return string Output HTML from rendered view.
72 - */
73 - public function rebuild( $valid_nonce ) {
74 -
75 - // Check form nonce is valid or not.
76 - if ( ! $valid_nonce ) {
77 - return $this->view->render(
78 - 'admin/main.html.twig',
79 - [
80 - 'options' => $this->manager->all(),
81 - 'alert' => [ 'danger' => 'CSRF token not valid' ]
82 - ]
83 - );
84 - }
85 -
86 - update_option('responsive_menu_version', '2.8.9');
87 -
88 - return $this->view->render(
89 19 'admin/main.html.twig',
90 20 [
91 21 'options' => $this->manager->all(),
92 - 'alert' => ['success' => 'Responsive Menu Database Rebuilt Successfully.']
22 + 'nav_menus' => $nav_menus,
23 + 'location_menus' => $location_menus
93 24 ]
94 25 );
95 26 }
96 27
97 -
98 - /**
99 - * Apply a specific theme options and commit to the database.
100 - *
101 - * This route is called when the Apply Theme button is pressed inside the
102 - * admin area. It loads in the options file for the theme and updates the
103 - * options accordingly.
104 - *
105 - * @author Peter Featherstone <peter@featherstone.me>
106 - *
107 - * @since 3.1.16
108 - *
109 - * @param string $theme The theme name to apply
110 - * @param bool $valid_nonce Is the form nonce valid or not.
111 - *
112 - * @return string Output HTML from rendered view.
113 - */
114 - public function apply_theme( $theme, $valid_nonce ) {
115 - $options = $this->manager->all();
116 -
117 - // Check form nonce is valid or not.
118 - if ( ! $valid_nonce ) {
119 - return $this->view->render(
120 - 'admin/main.html.twig',
121 - [
122 - 'options' => $options,
123 - 'alert' => [ 'danger' => 'CSRF token not valid' ]
124 - ]
125 - );
126 - }
127 -
128 - $upload_folder = wp_upload_dir()['basedir'];
129 - $theme_folder = $upload_folder . '/responsive-menu-themes/';
130 - $options_file_location = $theme_folder . $theme . '/options.json';
131 -
132 - $theme_options_file = file_get_contents($options_file_location);
133 - $theme_options = json_decode($theme_options_file, true);
134 -
135 - foreach($theme_options as $key => $value)
136 - $options[$key] = $value;
137 -
138 - $options['menu_theme'] = $theme;
139 -
140 - $this->manager->updateOptions($options->toArray());
141 -
28 + public function rebuild($nav_menus, $location_menus) {
142 29 return $this->view->render(
143 30 'admin/main.html.twig',
144 31 [
145 - 'options' => $options,
146 - 'alert' => ['success' => 'Responsive Menu Theme Applied Successfully.']
147 - ]
148 - );
149 - }
150 -
151 - /**
152 - * Import a theme from a zip file.
153 - *
154 - * This route is called when the Upload Theme button is pressed inside the
155 - * admin area. It loads in the selected zip file for the theme and unpacks
156 - * it in the uploads directory.
157 - *
158 - * @author Peter Featherstone <peter@featherstone.me>
159 - *
160 - * @since 3.1.16
161 - *
162 - * @param string $theme The theme file location to unzip
163 - * @param bool $valid_nonce Is the form nonce valid or not.
164 - *
165 - * @return string Output HTML from rendered view.
166 - */
167 - public function import_theme( $theme, $valid_nonce ) {
168 -
169 - // Check nonce is valid or not.
170 - if ( ! $valid_nonce ):
171 - $alert = [ 'danger' => 'CSRF token not valid' ];
172 - elseif ( ! empty( $theme ) ):
173 - WP_Filesystem();
174 - $upload_folder = wp_upload_dir()['basedir'] . '/responsive-menu-themes';
175 -
176 - $unzipfile = unzip_file($theme, $upload_folder);
177 -
178 - if(is_wp_error($unzipfile)) {
179 - $alert = ['danger' => $unzipfile->get_error_message()];
180 - } else {
181 - $alert = ['success' => 'Responsive Menu Theme Imported Successfully.'];
182 - }
183 -
184 - else:
185 - $alert = ['danger' => 'No import file selected'];
186 -
187 - endif;
188 -
189 - return $this->view->render(
190 - 'admin/main.html.twig',
191 - [
192 32 'options' => $this->manager->all(),
193 - 'alert' => $alert
33 + 'nav_menus' => $nav_menus,
34 + 'location_menus' => $location_menus,
35 + 'alert' => ['success' => 'Responsive Menu Database Rebuilt Successfully.']
194 36 ]
195 37 );
196 38 }
197 39
198 - /**
199 - * Update the options based on submitted admin form.
200 - *
201 - * This route is called whenever the Update Options button is pressed and is
202 - * the most commonly called route. Takes the submitted options and runs the
203 - * update options task.
204 - *
205 - * @author Peter Featherstone <peter@featherstone.me>
206 - *
207 - * @since 3.0
208 - *
209 - * @param bool $valid_nonce Is the form nonce valid or not
210 - * @param array $new_options An array of the submitted options.
211 - *
212 - * @return string Output HTML from rendered view.
213 - */
214 - public function update($valid_nonce, $new_options) {
40 + public function update($valid_nonce, $new_options, $nav_menus, $location_menus) {
215 41 $validator = new Validator();
216 42 $errors = [];
217 -
218 43 if(!$valid_nonce):
219 44 $alert = ['danger' => 'CSRF token not valid'];
220 45 $options = new OptionsCollection($new_options);
221 -
222 46 elseif($validator->validate($new_options)):
223 47 try {
224 48 $options = $this->manager->updateOptions($new_options);
225 49 $task = new UpdateOptionsTask;
@@ -224,18 +48,15 @@
224 48 $options = $this->manager->updateOptions($new_options);
225 49 $task = new UpdateOptionsTask;
226 50 $task->run($options, $this->view);
227 51 $alert = ['success' => 'Responsive Menu Options Updated Successfully.'];
228 -
229 52 } catch (\Exception $e) {
230 53 $alert = ['danger' => $e->getMessage()];
231 54 }
232 -
233 55 else:
234 56 $options = new OptionsCollection($new_options);
235 57 $errors = $validator->getErrors();
236 58 $alert = ['danger' => $errors];
237 -
238 59 endif;
239 60
240 61 return $this->view->render(
241 62 'admin/main.html.twig',
@@ -241,85 +62,38 @@
241 62 'admin/main.html.twig',
242 63 [
243 64 'options' => $options,
244 65 'alert' => $alert,
66 + 'nav_menus' => $nav_menus,
67 + 'location_menus' => $location_menus,
245 68 'errors' => $errors
246 69 ]
247 70 );
248 71 }
249 72
250 - /**
251 - * Reset the options back to the defaults.
252 - *
253 - * This route is called whenever the Reset Options button is pressed. Resets
254 - * all options back to their default states and runs the update options
255 - * task.
256 - *
257 - * @author Peter Featherstone <peter@featherstone.me>
258 - *
259 - * @since 3.0
260 - *
261 - * @param array $default_options An array of the default options.
262 - * @param bool $valid_nonce Is the form nonce valid or not.
263 - *
264 - * @return string Output HTML from rendered view.
265 - */
266 - public function reset($default_options, $valid_nonce ) {
267 -
268 - // Check form nonce is valid or not.
269 - if ( ! $valid_nonce ) {
270 - return $this->view->render(
271 - 'admin/main.html.twig',
272 - [
273 - 'options' => $this->manager->all(),
274 - 'alert' => [ 'danger' => 'CSRF token not valid' ]
275 - ]
276 - );
277 - }
278 -
73 + public function reset($default_options, $nav_menus, $location_menus) {
279 74 try {
280 75 $options = $this->manager->updateOptions($default_options);
281 76 $task = new UpdateOptionsTask;
282 77 $task->run($options, $this->view);
283 78 $alert = ['success' => 'Responsive Menu Options Reset Successfully'];
284 -
285 79 } catch(\Exception $e) {
286 80 $alert = ['danger' => $e->getMessage()];
287 81 }
288 -
289 82 return $this->view->render(
290 83 'admin/main.html.twig',
291 84 [
292 85 'options' => $options,
293 - 'alert' => $alert
86 + 'alert' => $alert,
87 + 'nav_menus' => $nav_menus,
88 + 'location_menus' => $location_menus
294 89 ]
295 90 );
296 91 }
297 92
298 - /**
299 - * Import options from a file.
300 - *
301 - * This route is called when the Import Options button is pressed inside the
302 - * admin area. It loads in the selected json file and updates the options.
303 - *
304 - * @author Peter Featherstone <peter@featherstone.me>
305 - *
306 - * @since 3.0
307 - *
308 - * @param array $imported_options An array of the imported options.
309 - * @param bool $valid_nonce Is the form nonce valid or not.
310 - *
311 - * @return string Output HTML from rendered view.
312 - */
313 - public function import( $imported_options, $valid_nonce ) {
93 + public function import($imported_options, $nav_menus, $location_menus) {
314 94 $errors = [];
315 -
316 - // Check nonce is valid or not.
317 - if ( ! $valid_nonce ) {
318 - $alert = [ 'danger' => 'CSRF token not valid' ];
319 - $options = $this->manager->all();
320 - } elseif( ! empty( $imported_options ) ) {
321 -
95 + if(!empty($imported_options)):
322 96 $validator = new Validator();
323 97 if($validator->validate($imported_options)):
324 98 try {
325 99 unset($imported_options['button_click_trigger']);
@@ -330,45 +104,30 @@
330 104 } catch(\Exception $e) {
331 105 $options = $this->manager->all();
332 106 $alert = ['danger' => $e->getMessage()];
333 107 }
334 -
335 108 else:
336 109 $options = new OptionsCollection($imported_options);
337 110 $errors = $validator->getErrors();
338 111 $alert = ['danger' => $errors];
339 -
340 112 endif;
341 -
342 - } else {
113 + else:
343 114 $options = $this->manager->all();
344 115 $alert = ['danger' => 'No import file selected'];
116 + endif;
345 117
346 - }
347 -
348 118 return $this->view->render(
349 119 'admin/main.html.twig',
350 120 [
351 121 'options' => $options,
352 122 'alert' => $alert,
123 + 'nav_menus' => $nav_menus,
124 + 'location_menus' => $location_menus,
353 125 'errors' => $errors
354 126 ]
355 127 );
356 128 }
357 129
358 - /**
359 - * Export all current options to a json file.
360 - *
361 - * This route is called when the Export Options button is pressed inside
362 - * the admin area. It returns a json file of all the current options as a
363 - * download attachment to the browser.
364 - *
365 - * @author Peter Featherstone <peter@featherstone.me>
366 - *
367 - * @since 3.0
368 - *
369 - * @return attachment json file attachment of plugin options.
370 - */
371 130 public function export() {
372 131 return json_encode(
373 132 $this->manager->all()->toArray()
374 133 );