'', 'Form Settings' => '-settings-edit_form', 'Email Settings' => '-settings-edit_email', 'Form Preview' => '-settings-form_preview', 'Account' => '-settings-edit_account', ]; protected $_ajax_actions = [ 'private' => [ // Fields settings 'Add field' => 'amoforms_add_field', 'Duplicate field' => 'amoforms_duplicate_field', 'Delete field' => 'amoforms_delete_field', 'Edit field' => 'amoforms_edit_field', 'Update fields' => 'amoforms_update_fields', 'Update submit button' => 'amoforms_update_submit_button', // Account settings 'Check amo user is free' => 'amoforms_check_email', 'Check amo acc credentials' => 'amoforms_has_connection', 'Check amo form settings' => 'amoforms_update_form', 'Check amo email settings' => 'amoforms_update_email', // Other 'Delete all forms' => 'amoforms_delete_all_forms', ], 'protected' => [ 'Submit amo form' => 'amoforms_submit', ], 'public' => [ ], ]; /** * @since 1.0.0 */ protected function __construct() { $this->_is_ajax = (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtoupper($_SERVER['HTTP_X_REQUESTED_WITH']) === 'XMLHTTPREQUEST'); $this->_page = !empty($_GET['page']) ? (string)$_GET['page'] : ''; $parts = explode('-', $this->_page); if (!empty($parts[1])) { $this->_controller = $parts[1]; } elseif (isset($_GET['controller'])) { $this->_controller = ucfirst($_GET['controller']); } else { $this->_controller = 'Settings'; } $this->_action = !empty($_REQUEST['action']) ? $_REQUEST['action'] : (!empty($parts[2]) ? $parts[2] : ''); if ($this->_is_ajax && strpos($this->_action, $this->_ajax_prefix) === 0) { $this->_action = str_replace($this->_ajax_prefix, '', $this->_action); } $this->init_admin_menu(); $this->init_ajax_actions(); } /** * Check, whether the request is ajax * @return bool */ public function is_ajax() { return $this->_is_ajax; } /** * Init admin menu * @since 1.0.0 */ protected function init_admin_menu() { //TODO: check capabilities in controller add_action('admin_menu', function () { add_menu_page($this->_admin_menu_title, $this->_admin_menu_title, $this->_settings_capability, $this->_menu_slug, [$this, 'navigate']); foreach ($this->_admin_menu_items as $title => $menu_slug) { add_submenu_page($this->_menu_slug, $title, $title, $this->_settings_capability, $this->_menu_slug . $menu_slug, [$this, 'navigate']); } }); } /** * Init ajax actions * @since 2.0.1 */ protected function init_ajax_actions() { foreach($this->_ajax_actions as $type => $actions) { foreach ($actions as $action) { switch($type) { case 'private': add_action("wp_ajax_{$action}", [$this, 'navigate_ajax']); break; case 'protected': add_action("wp_ajax_{$action}", [$this, 'navigate_ajax']); add_action("wp_ajax_nopriv_{$action}", [$this, 'navigate_ajax']); break; case 'public': add_action("wp_ajax_nopriv_{$action}", [$this, 'navigate_ajax']); break; } } } } /** * @since 1.0.0 * @throws Exceptions\Runtime * @throws Exceptions\Validate */ public function navigate() { try { $controller_name = '\\' . __NAMESPACE__ . '\Controllers\\' . ucfirst($this->_controller); $action = $this->_action . '_action'; if (class_exists($controller_name)) { $controller = new $controller_name(); if (method_exists($controller, $action)) { $controller->$action(); return; } } if ($action === 'submit_amoform_action') { $controller = new Form(); $controller->submit_action(); } else { $controller = new Settings(); $controller->edit_fields_action(); } } catch (\Exception $e) { Helpers::handle_exception($e); } } /** * Navigate for ajax requests */ public function navigate_ajax() { header('Cache-Control: no-cache, must-revalidate'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Content-type: application/json'); $this->navigate(); die; } }