root_menu_slug = $root_menu_slug; $this->query = new A2WL_JSON_API_Query(); $this->response = new A2WL_JSON_API_Response(); add_action('a2w_template_redirect', array(&$this, 'template_redirect')); add_action('a2w_after_template_redirect', array(&$this, 'after_template_redirect')); if(!has_action('template_redirect', 'a2w_global_template_redirect')){ add_action('template_redirect', 'a2w_global_template_redirect'); } // disable menu if (defined('A2WL_DEBUG_PAGE') && A2WL_DEBUG_PAGE) { add_action('admin_menu', array(&$this, 'admin_menu')); } add_action('update_option_a2wl_json_api_base', array(&$this, 'flush_rewrite_rules')); add_action('pre_update_option_a2wl_json_api_controllers', array(&$this, 'update_controllers')); } function template_redirect() { // Check to see if there's an appropriate API controller + method $controller = strtolower($this->query->get_controller()); $available_controllers = $this->get_controllers(); $enabled_controllers = explode(',', a2wl_get_setting('json_api_controllers')); $active_controllers = array_intersect($available_controllers, $enabled_controllers); if ($controller) { if (empty($this->query->dev)) { //error_reporting(0); } if (!in_array($controller, $active_controllers)) { $this->error("Unknown controller '$controller'."); } $controller_path = $this->controller_path($controller); if (file_exists($controller_path)) { require_once $controller_path; } $controller_class = $this->controller_class($controller); if (!class_exists($controller_class)) { $this->error("Unknown controller '$controller_class'."); } $this->controller = new $controller_class(); $method = $this->query->get_method($controller); if ($method) { if(method_exists($this->controller, 'permissions') && !$this->controller->permissions($method)){ // skip call if no have permissions return; } // Run action hooks for method do_action("a2wl_json_api", $controller, $method); do_action("a2wl_json_api-{$controller}-$method"); // Error out if nothing is found if ($method == '404') { $this->error('Not found'); } // Run the method $result = $this->controller->$method(); // Handle the result $this->response->respond($result); // Done! exit; } } } function after_template_redirect() { $controller = strtolower($this->query->get_controller()); if ($controller) { // If this is a request to the API and we got here, then the authorization key is missing in the request // Need return auth error $this->error("Invalid authentication."); } } function admin_menu() { add_submenu_page($this->root_menu_slug, 'JSON API Settings', 'JSON API', 'manage_options', 'a2wl-json-api', array(&$this, 'admin_options')); } function admin_options() { if (!current_user_can('manage_options')) { wp_die(__('You do not have sufficient permissions to access this page.', 'ali2woo-lite')); } $available_controllers = $this->get_controllers(); $active_controllers = explode(',', a2wl_get_setting('json_api_controllers')); if (count($active_controllers) == 1 && empty($active_controllers[0])) { $active_controllers = array(); } if (!empty($_REQUEST['_wpnonce']) && wp_verify_nonce($_REQUEST['_wpnonce'], "update-options")) { if ((!empty($_REQUEST['action']) || !empty($_REQUEST['action2'])) && (!empty($_REQUEST['controller']) || !empty($_REQUEST['controllers']))) { if (!empty($_REQUEST['action'])) { $action = $_REQUEST['action']; } else { $action = $_REQUEST['action2']; } if (!empty($_REQUEST['controllers'])) { $controllers = $_REQUEST['controllers']; } else { $controllers = array($_REQUEST['controller']); } foreach ($controllers as $controller) { if (in_array($controller, $available_controllers)) { if ($action == 'activate' && !in_array($controller, $active_controllers)) { $active_controllers[] = $controller; } else if ($action == 'deactivate') { $index = array_search($controller, $active_controllers); if ($index !== false) { unset($active_controllers[$index]); } } } } a2wl_set_setting('json_api_controllers', implode(',', $active_controllers)); } if (isset($_REQUEST['a2wl_json_api_base'])) { a2wl_set_setting('json_api_base', $_REQUEST['a2wl_json_api_base']); } } ?>