PluginProbe
Responsive Menu – Create Mobile-Friendly Menu / 3.1.16
Responsive Menu – Create Mobile-Friendly Menu v3.1.16
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 3.1.16, at app/Controllers/AdminController.php

329 lines 9.9 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 *
70 * @return string Output HTML from rendered view.
71 */
72 public function rebuild() {
73 update_option('responsive_menu_version', '2.8.9');
74
75 return $this->view->render(
76 'admin/main.html.twig',
77 [
78 'options' => $this->manager->all(),
79 'alert' => ['success' => 'Responsive Menu Database Rebuilt Successfully.']
80 ]
81 );
82 }
83
84
85 /**
86 * Apply a specific theme options and commit to the database.
87 *
88 * This route is called when the Apply Theme button is pressed inside the
89 * admin area. It loads in the options file for the theme and updates the
90 * options accordingly.
91 *
92 * @author Peter Featherstone <peter@featherstone.me>
93 *
94 * @since 3.1.16
95 *
96 * @param string $theme The theme name to apply
97 *
98 * @return string Output HTML from rendered view.
99 */
100 public function apply_theme($theme) {
101 $options = $this->manager->all();
102
103 $upload_folder = wp_upload_dir()['basedir'];
104 $theme_folder = $upload_folder . '/responsive-menu-themes/';
105 $options_file_location = $theme_folder . $theme . '/options.json';
106
107 $theme_options_file = file_get_contents($options_file_location);
108 $theme_options = json_decode($theme_options_file, true);
109
110 foreach($theme_options as $key => $value)
111 $options[$key] = $value;
112
113 $options['menu_theme'] = $theme;
114
115 $this->manager->updateOptions($options->toArray());
116
117 return $this->view->render(
118 'admin/main.html.twig',
119 [
120 'options' => $options,
121 'alert' => ['success' => 'Responsive Menu Theme Applied Successfully.']
122 ]
123 );
124 }
125
126 /**
127 * Import a theme from a zip file.
128 *
129 * This route is called when the Upload Theme button is pressed inside the
130 * admin area. It loads in the selected zip file for the theme and unpacks
131 * it in the uploads directory.
132 *
133 * @author Peter Featherstone <peter@featherstone.me>
134 *
135 * @since 3.1.16
136 *
137 * @param string $theme The theme file location to unzip
138 *
139 * @return string Output HTML from rendered view.
140 */
141 public function import_theme($theme) {
142 if($theme):
143 WP_Filesystem();
144 $upload_folder = wp_upload_dir()['basedir'] . '/responsive-menu-themes';
145
146 $unzipfile = unzip_file($theme, $upload_folder);
147
148 if(is_wp_error($unzipfile)) {
149 $alert = ['danger' => $unzipfile->get_error_message()];
150 } else {
151 $alert = ['success' => 'Responsive Menu Theme Imported Successfully.'];
152 }
153
154 else:
155 $alert = ['danger' => 'No import file selected'];
156
157 endif;
158
159 return $this->view->render(
160 'admin/main.html.twig',
161 [
162 'options' => $this->manager->all(),
163 'alert' => $alert
164 ]
165 );
166 }
167
168 /**
169 * Update the options based on submitted admin form.
170 *
171 * This route is called whenever the Update Options button is pressed and is
172 * the most commonly called route. Takes the submitted options and runs the
173 * update options task.
174 *
175 * @author Peter Featherstone <peter@featherstone.me>
176 *
177 * @since 3.0
178 *
179 * @param bool $valid_nonce Is the form nonce valid or not
180 * @param array $new_options An array of the submitted options.
181 *
182 * @return string Output HTML from rendered view.
183 */
184 public function update($valid_nonce, $new_options) {
185 $validator = new Validator();
186 $errors = [];
187
188 if(!$valid_nonce):
189 $alert = ['danger' => 'CSRF token not valid'];
190 $options = new OptionsCollection($new_options);
191
192 elseif($validator->validate($new_options)):
193 try {
194 $options = $this->manager->updateOptions($new_options);
195 $task = new UpdateOptionsTask;
196 $task->run($options, $this->view);
197 $alert = ['success' => 'Responsive Menu Options Updated Successfully.'];
198
199 } catch (\Exception $e) {
200 $alert = ['danger' => $e->getMessage()];
201 }
202
203 else:
204 $options = new OptionsCollection($new_options);
205 $errors = $validator->getErrors();
206 $alert = ['danger' => $errors];
207
208 endif;
209
210 return $this->view->render(
211 'admin/main.html.twig',
212 [
213 'options' => $options,
214 'alert' => $alert,
215 'errors' => $errors
216 ]
217 );
218 }
219
220 /**
221 * Reset the options back to the defaults.
222 *
223 * This route is called whenever the Reset Options button is pressed. Resets
224 * all options back to their default states and runs the update options
225 * task.
226 *
227 * @author Peter Featherstone <peter@featherstone.me>
228 *
229 * @since 3.0
230 *
231 * @param array $default_options An array of the default options.
232 *
233 * @return string Output HTML from rendered view.
234 */
235 public function reset($default_options) {
236 try {
237 $options = $this->manager->updateOptions($default_options);
238 $task = new UpdateOptionsTask;
239 $task->run($options, $this->view);
240 $alert = ['success' => 'Responsive Menu Options Reset Successfully'];
241
242 } catch(\Exception $e) {
243 $alert = ['danger' => $e->getMessage()];
244 }
245
246 return $this->view->render(
247 'admin/main.html.twig',
248 [
249 'options' => $options,
250 'alert' => $alert
251 ]
252 );
253 }
254
255 /**
256 * Import options from a file.
257 *
258 * This route is called when the Import Options button is pressed inside the
259 * admin area. It loads in the selected json file and updates the options.
260 *
261 * @author Peter Featherstone <peter@featherstone.me>
262 *
263 * @since 3.0
264 *
265 * @param array $imported_options An array of the imported options.
266 *
267 * @return string Output HTML from rendered view.
268 */
269 public function import($imported_options) {
270 $errors = [];
271 if(!empty($imported_options)):
272
273 $validator = new Validator();
274 if($validator->validate($imported_options)):
275 try {
276 unset($imported_options['button_click_trigger']);
277 $options = $this->manager->updateOptions($imported_options);
278 $task = new UpdateOptionsTask;
279 $task->run($options, $this->view);
280 $alert = ['success' => 'Responsive Menu Options Imported Successfully.'];
281 } catch(\Exception $e) {
282 $options = $this->manager->all();
283 $alert = ['danger' => $e->getMessage()];
284 }
285
286 else:
287 $options = new OptionsCollection($imported_options);
288 $errors = $validator->getErrors();
289 $alert = ['danger' => $errors];
290
291 endif;
292
293 else:
294 $options = $this->manager->all();
295 $alert = ['danger' => 'No import file selected'];
296
297 endif;
298
299 return $this->view->render(
300 'admin/main.html.twig',
301 [
302 'options' => $options,
303 'alert' => $alert,
304 'errors' => $errors
305 ]
306 );
307 }
308
309 /**
310 * Export all current options to a json file.
311 *
312 * This route is called when the Export Options button is pressed inside
313 * the admin area. It returns a json file of all the current options as a
314 * download attachment to the browser.
315 *
316 * @author Peter Featherstone <peter@featherstone.me>
317 *
318 * @since 3.0
319 *
320 * @return attachment json file attachment of plugin options.
321 */
322 public function export() {
323 return json_encode(
324 $this->manager->all()->toArray()
325 );
326 }
327
328 }
329