| @@ -6,44 +6,220 @@ | ||
| 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 | +*/ | |
| 10 | 20 | class AdminController { |
| 11 | 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 | + */ | |
| 12 | 36 | public function __construct(OptionManager $manager, View $view) { |
| 13 | 37 | $this->manager = $manager; |
| 14 | 38 | $this->view = $view; |
| 15 | 39 | } |
| 16 | 40 | |
| 17 | - public function index($nav_menus, $location_menus) { | |
| 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() { | |
| 18 | 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( | |
| 19 | 89 | 'admin/main.html.twig', |
| 20 | 90 | [ |
| 21 | 91 | 'options' => $this->manager->all(), |
| 22 | - 'nav_menus' => $nav_menus, | |
| 23 | - 'location_menus' => $location_menus | |
| 92 | + 'alert' => ['success' => 'Responsive Menu Database Rebuilt Successfully.'] | |
| 24 | 93 | ] |
| 25 | 94 | ); |
| 26 | 95 | } |
| 27 | 96 | |
| 28 | - public function rebuild($nav_menus, $location_menus) { | |
| 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 | + | |
| 29 | 142 | return $this->view->render( |
| 30 | 143 | 'admin/main.html.twig', |
| 31 | 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 | + [ | |
| 32 | 192 | 'options' => $this->manager->all(), |
| 33 | - 'nav_menus' => $nav_menus, | |
| 34 | - 'location_menus' => $location_menus, | |
| 35 | - 'alert' => ['success' => 'Responsive Menu Database Rebuilt Successfully.'] | |
| 193 | + 'alert' => $alert | |
| 36 | 194 | ] |
| 37 | 195 | ); |
| 38 | 196 | } |
| 39 | 197 | |
| 40 | - public function update($valid_nonce, $new_options, $nav_menus, $location_menus) { | |
| 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) { | |
| 41 | 215 | $validator = new Validator(); |
| 42 | 216 | $errors = []; |
| 217 | + | |
| 43 | 218 | if(!$valid_nonce): |
| 44 | 219 | $alert = ['danger' => 'CSRF token not valid']; |
| 45 | 220 | $options = new OptionsCollection($new_options); |
| 221 | + | |
| 46 | 222 | elseif($validator->validate($new_options)): |
| 47 | 223 | try { |
| 48 | 224 | $options = $this->manager->updateOptions($new_options); |
| 49 | 225 | $task = new UpdateOptionsTask; |
| @@ -48,15 +224,18 @@ | ||
| 48 | 224 | $options = $this->manager->updateOptions($new_options); |
| 49 | 225 | $task = new UpdateOptionsTask; |
| 50 | 226 | $task->run($options, $this->view); |
| 51 | 227 | $alert = ['success' => 'Responsive Menu Options Updated Successfully.']; |
| 228 | + | |
| 52 | 229 | } catch (\Exception $e) { |
| 53 | 230 | $alert = ['danger' => $e->getMessage()]; |
| 54 | 231 | } |
| 232 | + | |
| 55 | 233 | else: |
| 56 | 234 | $options = new OptionsCollection($new_options); |
| 57 | 235 | $errors = $validator->getErrors(); |
| 58 | 236 | $alert = ['danger' => $errors]; |
| 237 | + | |
| 59 | 238 | endif; |
| 60 | 239 | |
| 61 | 240 | return $this->view->render( |
| 62 | 241 | 'admin/main.html.twig', |
| @@ -62,38 +241,85 @@ | ||
| 62 | 241 | 'admin/main.html.twig', |
| 63 | 242 | [ |
| 64 | 243 | 'options' => $options, |
| 65 | 244 | 'alert' => $alert, |
| 66 | - 'nav_menus' => $nav_menus, | |
| 67 | - 'location_menus' => $location_menus, | |
| 68 | 245 | 'errors' => $errors |
| 69 | 246 | ] |
| 70 | 247 | ); |
| 71 | 248 | } |
| 72 | 249 | |
| 73 | - public function reset($default_options, $nav_menus, $location_menus) { | |
| 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 | + | |
| 74 | 279 | try { |
| 75 | 280 | $options = $this->manager->updateOptions($default_options); |
| 76 | 281 | $task = new UpdateOptionsTask; |
| 77 | 282 | $task->run($options, $this->view); |
| 78 | 283 | $alert = ['success' => 'Responsive Menu Options Reset Successfully']; |
| 284 | + | |
| 79 | 285 | } catch(\Exception $e) { |
| 80 | 286 | $alert = ['danger' => $e->getMessage()]; |
| 81 | 287 | } |
| 288 | + | |
| 82 | 289 | return $this->view->render( |
| 83 | 290 | 'admin/main.html.twig', |
| 84 | 291 | [ |
| 85 | 292 | 'options' => $options, |
| 86 | - 'alert' => $alert, | |
| 87 | - 'nav_menus' => $nav_menus, | |
| 88 | - 'location_menus' => $location_menus | |
| 293 | + 'alert' => $alert | |
| 89 | 294 | ] |
| 90 | 295 | ); |
| 91 | 296 | } |
| 92 | 297 | |
| 93 | - public function import($imported_options, $nav_menus, $location_menus) { | |
| 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 ) { | |
| 94 | 314 | $errors = []; |
| 95 | - if(!empty($imported_options)): | |
| 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 | + | |
| 96 | 322 | $validator = new Validator(); |
| 97 | 323 | if($validator->validate($imported_options)): |
| 98 | 324 | try { |
| 99 | 325 | unset($imported_options['button_click_trigger']); |
| @@ -104,30 +330,45 @@ | ||
| 104 | 330 | } catch(\Exception $e) { |
| 105 | 331 | $options = $this->manager->all(); |
| 106 | 332 | $alert = ['danger' => $e->getMessage()]; |
| 107 | 333 | } |
| 334 | + | |
| 108 | 335 | else: |
| 109 | 336 | $options = new OptionsCollection($imported_options); |
| 110 | 337 | $errors = $validator->getErrors(); |
| 111 | 338 | $alert = ['danger' => $errors]; |
| 339 | + | |
| 112 | 340 | endif; |
| 113 | - else: | |
| 341 | + | |
| 342 | + } else { | |
| 114 | 343 | $options = $this->manager->all(); |
| 115 | 344 | $alert = ['danger' => 'No import file selected']; |
| 116 | - endif; | |
| 117 | 345 | |
| 346 | + } | |
| 347 | + | |
| 118 | 348 | return $this->view->render( |
| 119 | 349 | 'admin/main.html.twig', |
| 120 | 350 | [ |
| 121 | 351 | 'options' => $options, |
| 122 | 352 | 'alert' => $alert, |
| 123 | - 'nav_menus' => $nav_menus, | |
| 124 | - 'location_menus' => $location_menus, | |
| 125 | 353 | 'errors' => $errors |
| 126 | 354 | ] |
| 127 | 355 | ); |
| 128 | 356 | } |
| 129 | 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 | + */ | |
| 130 | 371 | public function export() { |
| 131 | 372 | return json_encode( |
| 132 | 373 | $this->manager->all()->toArray() |
| 133 | 374 | ); |