forked from dphiffer/wp-json-api
-
Notifications
You must be signed in to change notification settings - Fork 14
/
json-api.php
83 lines (73 loc) · 2.37 KB
/
json-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/*
Plugin Name: JSON API
Plugin URI: http://wordpress.org/extend/plugins/json-api/
Description: A RESTful API for WordPress
Version: 1.0.7
Author: Dan Phiffer
Author URI: http://phiffer.org/
*/
$dir = json_api_dir();
@include_once "$dir/singletons/api.php";
@include_once "$dir/singletons/query.php";
@include_once "$dir/singletons/introspector.php";
@include_once "$dir/singletons/response.php";
@include_once "$dir/models/post.php";
@include_once "$dir/models/comment.php";
@include_once "$dir/models/category.php";
@include_once "$dir/models/tag.php";
@include_once "$dir/models/author.php";
@include_once "$dir/models/attachment.php";
function json_api_init() {
global $json_api;
if (phpversion() < 5) {
add_action('admin_notices', 'json_api_php_version_warning');
return;
}
if (!class_exists('JSON_API')) {
add_action('admin_notices', 'json_api_class_warning');
return;
}
add_filter('rewrite_rules_array', 'json_api_rewrites');
$json_api = new JSON_API();
}
function json_api_php_version_warning() {
echo "<div id=\"json-api-warning\" class=\"updated fade\"><p>Sorry, JSON API requires PHP version 5.0 or greater.</p></div>";
}
function json_api_class_warning() {
echo "<div id=\"json-api-warning\" class=\"updated fade\"><p>Oops, JSON_API class not found. If you've defined a JSON_API_DIR constant, double check that the path is correct.</p></div>";
}
function json_api_activation() {
// Add the rewrite rule on activation
global $wp_rewrite;
add_filter('rewrite_rules_array', 'json_api_rewrites');
$wp_rewrite->flush_rules();
}
function json_api_deactivation() {
// Remove the rewrite rule on deactivation
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
function json_api_rewrites($wp_rules) {
$base = get_option('json_api_base', 'api');
if (empty($base)) {
return $wp_rules;
}
$json_api_rules = array(
"$base\$" => 'index.php?json=info',
"$base/(.+)\$" => 'index.php?json=$matches[1]'
);
return array_merge($json_api_rules, $wp_rules);
}
function json_api_dir() {
if (defined('JSON_API_DIR') && file_exists(JSON_API_DIR)) {
return JSON_API_DIR;
} else {
return dirname(__FILE__);
}
}
// Add initialization and activation hooks
add_action('init', 'json_api_init');
register_activation_hook("$dir/json-api.php", 'json_api_activation');
register_deactivation_hook("$dir/json-api.php", 'json_api_deactivation');
?>