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