PluginProbe
Responsive Menu – Create Mobile-Friendly Menu / 4.1.2
Responsive Menu – Create Mobile-Friendly Menu v4.1.2
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
responsive-menu / app / Controllers / AdminController.php

AdminController.php in Responsive Menu – Create Mobile-Friendly Menu 4.1.2, at app/Controllers/AdminController.php

378 lines 11.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ResponsiveMenu\Controllers;
4 use ResponsiveMenu\View\View;
5 use ResponsiveMenu\Management\OptionManager;
6 use ResponsiveMenu\Validation\Validator;
7 use ResponsiveMenu\Tasks\UpdateOptionsTask;
8 use ResponsiveMenu\Collections\OptionsCollection;
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 class AdminController {
21
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 public function __construct(OptionManager $manager, View $view) {
37 $this->manager = $manager;
38 $this->view = $view;
39 }
40
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() {
54 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 'admin/main.html.twig',
90 [
91 'options' => $this->manager->all(),
92 'alert' => ['success' => 'Responsive Menu Database Rebuilt Successfully.']
93 ]
94 );
95 }
96
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
142 return $this->view->render(
143 'admin/main.html.twig',
144 [
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 'options' => $this->manager->all(),
193 'alert' => $alert
194 ]
195 );
196 }
197
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) {
215 $validator = new Validator();
216 $errors = [];
217
218 if(!$valid_nonce):
219 $alert = ['danger' => 'CSRF token not valid'];
220 $options = new OptionsCollection($new_options);
221
222 elseif($validator->validate($new_options)):
223 try {
224 $options = $this->manager->updateOptions($new_options);
225 $task = new UpdateOptionsTask;
226 $task->run($options, $this->view);
227 $alert = ['success' => 'Responsive Menu Options Updated Successfully.'];
228
229 } catch (\Exception $e) {
230 $alert = ['danger' => $e->getMessage()];
231 }
232
233 else:
234 $options = new OptionsCollection($new_options);
235 $errors = $validator->getErrors();
236 $alert = ['danger' => $errors];
237
238 endif;
239
240 return $this->view->render(
241 'admin/main.html.twig',
242 [
243 'options' => $options,
244 'alert' => $alert,
245 'errors' => $errors
246 ]
247 );
248 }
249
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
279 try {
280 $options = $this->manager->updateOptions($default_options);
281 $task = new UpdateOptionsTask;
282 $task->run($options, $this->view);
283 $alert = ['success' => 'Responsive Menu Options Reset Successfully'];
284
285 } catch(\Exception $e) {
286 $alert = ['danger' => $e->getMessage()];
287 }
288
289 return $this->view->render(
290 'admin/main.html.twig',
291 [
292 'options' => $options,
293 'alert' => $alert
294 ]
295 );
296 }
297
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 ) {
314 $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
322 $validator = new Validator();
323 if($validator->validate($imported_options)):
324 try {
325 unset($imported_options['button_click_trigger']);
326 $options = $this->manager->updateOptions($imported_options);
327 $task = new UpdateOptionsTask;
328 $task->run($options, $this->view);
329 $alert = ['success' => 'Responsive Menu Options Imported Successfully.'];
330 } catch(\Exception $e) {
331 $options = $this->manager->all();
332 $alert = ['danger' => $e->getMessage()];
333 }
334
335 else:
336 $options = new OptionsCollection($imported_options);
337 $errors = $validator->getErrors();
338 $alert = ['danger' => $errors];
339
340 endif;
341
342 } else {
343 $options = $this->manager->all();
344 $alert = ['danger' => 'No import file selected'];
345
346 }
347
348 return $this->view->render(
349 'admin/main.html.twig',
350 [
351 'options' => $options,
352 'alert' => $alert,
353 'errors' => $errors
354 ]
355 );
356 }
357
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 public function export() {
372 return json_encode(
373 $this->manager->all()->toArray()
374 );
375 }
376
377 }
378