query = new JSON_API_Query(); $this->introspector = new JSON_API_Introspector(); $this->response = new JSON_API_Response(); add_action('template_redirect', array(&$this, 'template_redirect')); add_action('admin_menu', array(&$this, 'admin_menu')); add_action('update_option_json_api_base', array(&$this, 'flush_rewrite_rules')); add_action('pre_update_option_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(',', get_option('json_api_controllers', 'core')); $active_controllers = array_intersect($available_controllers, $enabled_controllers); if ($controller) { 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) { $this->response->setup(); // Run action hooks for method do_action("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 admin_menu() { add_options_page('JSON API Settings', 'JSON API', 'manage_options', '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.') ); } $available_controllers = $this->get_controllers(); $active_controllers = explode(',', get_option('json_api_controllers', 'core')); 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]); } } } } $this->save_option('json_api_controllers', implode(',', $active_controllers)); } if (isset($_REQUEST['json_api_base'])) { $this->save_option('json_api_base', $_REQUEST['json_api_base']); } } ?>