| @@ -6,190 +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 | - * | |
| 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 | 19 | 'admin/main.html.twig', |
| 77 | 20 | [ |
| 78 | 21 | 'options' => $this->manager->all(), |
| 79 | - 'alert' => ['success' => 'Responsive Menu Database Rebuilt Successfully.'] | |
| 22 | + 'nav_menus' => $nav_menus, | |
| 23 | + 'location_menus' => $location_menus | |
| 80 | 24 | ] |
| 81 | 25 | ); |
| 82 | 26 | } |
| 83 | 27 | |
| 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 | - | |
| 28 | + public function rebuild($nav_menus, $location_menus) { | |
| 117 | 29 | return $this->view->render( |
| 118 | 30 | 'admin/main.html.twig', |
| 119 | 31 | [ |
| 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 | 32 | 'options' => $this->manager->all(), |
| 163 | - 'alert' => $alert | |
| 33 | + 'nav_menus' => $nav_menus, | |
| 34 | + 'location_menus' => $location_menus, | |
| 35 | + 'alert' => ['success' => 'Responsive Menu Database Rebuilt Successfully.'] | |
| 164 | 36 | ] |
| 165 | 37 | ); |
| 166 | 38 | } |
| 167 | 39 | |
| 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) { | |
| 40 | + public function update($valid_nonce, $new_options, $nav_menus, $location_menus) { | |
| 185 | 41 | $validator = new Validator(); |
| 186 | 42 | $errors = []; |
| 187 | - | |
| 188 | 43 | if(!$valid_nonce): |
| 189 | 44 | $alert = ['danger' => 'CSRF token not valid']; |
| 190 | 45 | $options = new OptionsCollection($new_options); |
| 191 | - | |
| 192 | 46 | elseif($validator->validate($new_options)): |
| 193 | 47 | try { |
| 194 | 48 | $options = $this->manager->updateOptions($new_options); |
| 195 | 49 | $task = new UpdateOptionsTask; |
| @@ -194,18 +48,15 @@ | ||
| 194 | 48 | $options = $this->manager->updateOptions($new_options); |
| 195 | 49 | $task = new UpdateOptionsTask; |
| 196 | 50 | $task->run($options, $this->view); |
| 197 | 51 | $alert = ['success' => 'Responsive Menu Options Updated Successfully.']; |
| 198 | - | |
| 199 | 52 | } catch (\Exception $e) { |
| 200 | 53 | $alert = ['danger' => $e->getMessage()]; |
| 201 | 54 | } |
| 202 | - | |
| 203 | 55 | else: |
| 204 | 56 | $options = new OptionsCollection($new_options); |
| 205 | 57 | $errors = $validator->getErrors(); |
| 206 | 58 | $alert = ['danger' => $errors]; |
| 207 | - | |
| 208 | 59 | endif; |
| 209 | 60 | |
| 210 | 61 | return $this->view->render( |
| 211 | 62 | 'admin/main.html.twig', |
| @@ -211,66 +62,38 @@ | ||
| 211 | 62 | 'admin/main.html.twig', |
| 212 | 63 | [ |
| 213 | 64 | 'options' => $options, |
| 214 | 65 | 'alert' => $alert, |
| 66 | + 'nav_menus' => $nav_menus, | |
| 67 | + 'location_menus' => $location_menus, | |
| 215 | 68 | 'errors' => $errors |
| 216 | 69 | ] |
| 217 | 70 | ); |
| 218 | 71 | } |
| 219 | 72 | |
| 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) { | |
| 73 | + public function reset($default_options, $nav_menus, $location_menus) { | |
| 236 | 74 | try { |
| 237 | 75 | $options = $this->manager->updateOptions($default_options); |
| 238 | 76 | $task = new UpdateOptionsTask; |
| 239 | 77 | $task->run($options, $this->view); |
| 240 | 78 | $alert = ['success' => 'Responsive Menu Options Reset Successfully']; |
| 241 | - | |
| 242 | 79 | } catch(\Exception $e) { |
| 243 | 80 | $alert = ['danger' => $e->getMessage()]; |
| 244 | 81 | } |
| 245 | - | |
| 246 | 82 | return $this->view->render( |
| 247 | 83 | 'admin/main.html.twig', |
| 248 | 84 | [ |
| 249 | 85 | 'options' => $options, |
| 250 | - 'alert' => $alert | |
| 86 | + 'alert' => $alert, | |
| 87 | + 'nav_menus' => $nav_menus, | |
| 88 | + 'location_menus' => $location_menus | |
| 251 | 89 | ] |
| 252 | 90 | ); |
| 253 | 91 | } |
| 254 | 92 | |
| 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) { | |
| 93 | + public function import($imported_options, $nav_menus, $location_menus) { | |
| 270 | 94 | $errors = []; |
| 271 | 95 | if(!empty($imported_options)): |
| 272 | - | |
| 273 | 96 | $validator = new Validator(); |
| 274 | 97 | if($validator->validate($imported_options)): |
| 275 | 98 | try { |
| 276 | 99 | unset($imported_options['button_click_trigger']); |
| @@ -281,20 +104,16 @@ | ||
| 281 | 104 | } catch(\Exception $e) { |
| 282 | 105 | $options = $this->manager->all(); |
| 283 | 106 | $alert = ['danger' => $e->getMessage()]; |
| 284 | 107 | } |
| 285 | - | |
| 286 | 108 | else: |
| 287 | 109 | $options = new OptionsCollection($imported_options); |
| 288 | 110 | $errors = $validator->getErrors(); |
| 289 | 111 | $alert = ['danger' => $errors]; |
| 290 | - | |
| 291 | 112 | endif; |
| 292 | - | |
| 293 | 113 | else: |
| 294 | 114 | $options = $this->manager->all(); |
| 295 | 115 | $alert = ['danger' => 'No import file selected']; |
| 296 | - | |
| 297 | 116 | endif; |
| 298 | 117 | |
| 299 | 118 | return $this->view->render( |
| 300 | 119 | 'admin/main.html.twig', |
| @@ -300,26 +119,15 @@ | ||
| 300 | 119 | 'admin/main.html.twig', |
| 301 | 120 | [ |
| 302 | 121 | 'options' => $options, |
| 303 | 122 | 'alert' => $alert, |
| 123 | + 'nav_menus' => $nav_menus, | |
| 124 | + 'location_menus' => $location_menus, | |
| 304 | 125 | 'errors' => $errors |
| 305 | 126 | ] |
| 306 | 127 | ); |
| 307 | 128 | } |
| 308 | 129 | |
| 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 | 130 | public function export() { |
| 323 | 131 | return json_encode( |
| 324 | 132 | $this->manager->all()->toArray() |
| 325 | 133 | ); |