Events.php
87 lines
| 1 | <?php |
| 2 | namespace NestedPages\Form; |
| 3 | |
| 4 | /** |
| 5 | * Registers the WP Actions/Handlers |
| 6 | */ |
| 7 | class Events |
| 8 | { |
| 9 | /** |
| 10 | * Actions |
| 11 | * @var array |
| 12 | */ |
| 13 | private $actions; |
| 14 | |
| 15 | /** |
| 16 | * Handler Class |
| 17 | * @var stdClass obj |
| 18 | */ |
| 19 | private $handlers; |
| 20 | |
| 21 | public function __construct() |
| 22 | { |
| 23 | $this->registerEvents(); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Set the Form Actions |
| 28 | */ |
| 29 | public function registerEvents() |
| 30 | { |
| 31 | $this->actions = [ |
| 32 | 'wp_ajax_npsort', |
| 33 | 'wp_ajax_npquickEdit', |
| 34 | 'wp_ajax_npsyncMenu', |
| 35 | 'wp_ajax_npnestToggle', |
| 36 | 'wp_ajax_npquickEditLink', |
| 37 | 'wp_ajax_npgetTaxonomies', |
| 38 | 'wp_ajax_npnewChild', |
| 39 | 'wp_ajax_npnewBeforeAfter', |
| 40 | 'admin_post_npListingSort', |
| 41 | 'wp_ajax_npEmptyTrash', |
| 42 | 'admin_post_npSearch', |
| 43 | 'wp_ajax_npclonePost', |
| 44 | 'wp_ajax_npmenuSearch', |
| 45 | 'wp_ajax_npnewMenuItem', |
| 46 | 'admin_post_npCategoryFilter', |
| 47 | 'admin_post_npBulkActions', |
| 48 | 'wp_ajax_npmanualMenuSync', |
| 49 | 'admin_post_npBulkEdit', |
| 50 | 'wp_ajax_nptrashWithChildren', |
| 51 | 'wp_ajax_nppostSearch', |
| 52 | 'wp_ajax_npWpmlTranslations', |
| 53 | 'wp_ajax_npresetSettings', |
| 54 | 'wp_ajax_npresetUserPreferences', |
| 55 | 'wp_ajax_npresetAdminMenuSettings', |
| 56 | 'wp_ajax_nptoggleStatusDisplay' |
| 57 | ]; |
| 58 | $this->setHandlers(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Set the Handlers Object |
| 63 | */ |
| 64 | public function setHandlers() |
| 65 | { |
| 66 | foreach($this->actions as $key => $action){ |
| 67 | $class = str_replace('admin_post_np', '', $action); // Non-AJAX forms |
| 68 | $class = ucfirst(str_replace('wp_ajax_np', '', $class)); // AJAX forms |
| 69 | $this->handlers[$key] = new \stdClass(); |
| 70 | $this->handlers[$key]->action = $action; |
| 71 | $this->handlers[$key]->class = 'NestedPages\Form\Listeners\\' . $class; |
| 72 | } |
| 73 | $this->build(); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Register the WP Actions |
| 78 | */ |
| 79 | public function build() |
| 80 | { |
| 81 | foreach($this->handlers as $handler){ |
| 82 | add_action($handler->action, function() use ($handler) { |
| 83 | new $handler->class; |
| 84 | }); |
| 85 | } |
| 86 | } |
| 87 | } |