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 (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) { $this->response->setup(); // Run action hooks for method do_action("json_api", $controller, $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']); } } ?>

JSON API Settings

Controllers

print_controller_actions(); ?> controller_info($controller); if (is_string($info)) { $active = false; $error = true; $info = array( 'name' => $controller, 'description' => "

Error: $info

", 'methods' => array(), 'url' => null ); } ?>
Controller Description
Controller Description
' . __('Deactivate') . ''; } else if (!$error) { echo '' . __('Activate') . ''; } if (!empty($info['url'])) { echo ' | '; echo 'Docs
'; } ?>

get_method_url($controller, $method); if ($active) { echo "$method "; } else { echo "$method "; } } ?>

print_controller_actions('action2'); ?>

Address

Specify a base URL for JSON API. For example, using api as your API base URL would enable the following /api/get_recent_posts/. If you assign a blank value the API will only be available by setting a json query variable.

API base /

Note: User-friendly permalinks are not currently enabled. Change Permalinks

$value) { $args[] = urlencode($key) . '=' . urlencode($value); } $args = implode('&', $args); } else { $args = $options; } if ($controller != 'core') { $method = "$controller/$method"; } if (!empty($base) && !empty($permalink_structure)) { if (!empty($args)) { $args = "?$args"; } return "$url/$base/$method/$args"; } else { return "$url?json=$method&$args"; } } function save_option($id, $value) { $option_exists = (get_option($id, null) !== null); if ($option_exists) { update_option($id, $value); } else { add_option($id, $value); } } function get_controllers() { $controllers = array(); $dir = json_api_dir(); $this->check_directory_for_controllers("$dir/controllers", $controllers); $this->check_directory_for_controllers(get_stylesheet_directory(), $controllers); $controllers = apply_filters('json_api_controllers', $controllers); return array_map('strtolower', $controllers); } function check_directory_for_controllers($dir, &$controllers) { $dh = opendir($dir); while ($file = readdir($dh)) { if (preg_match('/(.+)\.php$/i', $file, $matches)) { $src = file_get_contents("$dir/$file"); if (preg_match("/class\s+JSON_API_{$matches[1]}_Controller/i", $src)) { $controllers[] = $matches[1]; } } } } function controller_is_active($controller) { if (defined('JSON_API_CONTROLLERS')) { $default = JSON_API_CONTROLLERS; } else { $default = 'core'; } $active_controllers = explode(',', get_option('json_api_controllers', $default)); return (in_array($controller, $active_controllers)); } function update_controllers($controllers) { if (is_array($controllers)) { return implode(',', $controllers); } else { return $controllers; } } function controller_info($controller) { $path = $this->controller_path($controller); $class = $this->controller_class($controller); $response = array( 'name' => $controller, 'description' => '(No description available)', 'methods' => array() ); if (file_exists($path)) { $source = file_get_contents($path); if (preg_match('/^\s*Controller name:(.+)$/im', $source, $matches)) { $response['name'] = trim($matches[1]); } if (preg_match('/^\s*Controller description:(.+)$/im', $source, $matches)) { $response['description'] = trim($matches[1]); } if (preg_match('/^\s*Controller URI:(.+)$/im', $source, $matches)) { $response['docs'] = trim($matches[1]); } if (!class_exists($class)) { require_once($path); } $response['methods'] = get_class_methods($class); return $response; } else if (is_admin()) { return "Cannot find controller class '$class' (filtered path: $path)."; } else { $this->error("Unknown controller '$controller'."); } return $response; } function controller_class($controller) { return "json_api_{$controller}_controller"; } function controller_path($controller) { $json_api_dir = json_api_dir(); $json_api_path = "$json_api_dir/controllers/$controller.php"; $theme_dir = get_stylesheet_directory(); $theme_path = "$theme_dir/$controller.php"; if (file_exists($theme_path)) { $path = $theme_path; } else if (file_exists($json_api_path)) { $path = $json_api_path; } else { $path = null; } $controller_class = $this->controller_class($controller); return apply_filters("{$controller_class}_path", $path); } function get_nonce_id($controller, $method) { $controller = strtolower($controller); $method = strtolower($method); return "json_api-$controller-$method"; } function flush_rewrite_rules() { global $wp_rewrite; $wp_rewrite->flush_rules(); } function error($message = 'Unknown error', $http_status = 404) { $this->response->respond(array( 'error' => $message ), 'error', $http_status); } function include_value($key) { return $this->response->is_value_included($key); } } ?>