This repository was archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathb3-rest-api.php
More file actions
160 lines (136 loc) · 4.1 KB
/
b3-rest-api.php
File metadata and controls
160 lines (136 loc) · 4.1 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
* B3 REST API Extensions
*
* A WordPress plugin that extends the WP API in order to support B3 and your
* projects.
*
* @package B3
* @subpackage B3/API
* @author The B3 Team <b3@beebeebee.be>
* @license GPL-2.0+
* @link http://beebeebee.be
*
* @wordpress-plugin
* Plugin Name: B3 REST API Extensions
* Version: 0.1.0-alpha
* Description: This plugin extends the WP-API in order to support B3 and
* your projects.
* Author: The B3 Team
* Author URI: http://beebeebee.be
* Plugin URI: http://beebeebee.be
* Text Domain: b3-rest-api
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Domain Path: /languages
* GitHub Plugin URI: https://github.com/B3ST/B3-REST-API
*/
if ( ! defined( 'WPINC' ) ) {
die;
}
require_once dirname( __FILE__ ) . '/functions.php';
require_once dirname( __FILE__ ) . '/resources/B3_API.php';
require_once dirname( __FILE__ ) . '/helpers/B3_RoutesHelper.php';
require_once dirname( __FILE__ ) . '/helpers/B3_SettingsHelper.php';
class B3_JSON_REST_API {
const VERSION = '0.1.0-alpha';
/**
* Unique identifier for the B3 REST API plugin.
* @var string
*/
protected $plugin_slug = 'b3-rest-api';
/**
* Plugin instance.
* @var B3_JSON_REST_API
*/
protected static $instance = null;
/**
* WP API server.
* @var WP_JSON_ResponseHandler
*/
protected $server;
/**
* Resources provided by this extension.
* @var array
*/
protected $resources = array();
/**
* Plugin constructor.
*/
public function __construct() {
$this->resources = array(
'B3_Comment' => null,
'B3_Media' => null,
'B3_Menu' => null,
'B3_Post' => null,
'B3_Settings' => null,
'B3_Sidebar' => null,
);
add_action( 'init', array( $this, 'init' ), 99 );
}
/**
* Retrieve or create an instance of the plugin to be used by
* WordPress.
*
* It is NOT my intention for this to be a singleton, which is
* why the constructor is exposed and additonal instances may be
* created (for testing, etc.)
*
* @return B3_JSON_REST_API Plugin instance.
*/
public static function get_instance() {
if ( null === static::$instance ) {
static::$instance = new static;
}
return static::$instance;
}
/**
* Retrieve this plugin slug.
*
* @return string Plugin slug variable.
*/
public function get_plugin_slug() {
return $this->plugin_slug;
}
/**
* Initializes the plugin.
*
* Called by the `init` action.
*/
public function init() {
$domain = $this->plugin_slug;
$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
// Setup internationalization support:
load_textdomain( $domain, trailingslashit( WP_LANG_DIR ) . $domain . '/' . $domain . '-' . $locale . '.mo' );
load_plugin_textdomain( $domain, FALSE, basename( plugin_dir_path( dirname( __FILE__ ) ) ) . '/languages/' );
add_action( 'wp_json_server_before_serve', array( $this, 'default_filters' ), 10, 1 );
}
/**
* Hooks B3 extensions to the WP API.
*
* Called by the `wp_json_server_before_serve` action.
*
* @param WP_JSON_ResponseHandler $server WP API response handler.
*/
public function default_filters( WP_JSON_ResponseHandler $server ) {
$this->server = $server;
foreach ( $this->resources as $class => $resource ) {
include_once dirname( __FILE__ ) . '/resources/' . $class . '.php';
$this->resources[ $class ] = $resource = new $class( $server );
add_filter( 'json_endpoints', array( $resource, 'register_routes' ), 10, 1 );
}
add_filter( 'json_prepare_post', array( $this->resources['B3_Comment'], 'prepare_post' ), 10, 3 );
}
/**
* Generates a REST API error.
*
* @param string $code Error code.
* @param string $message Error message.
* @param int $status HTTP status code (default: 500).
* @return WP_Error Error object.
*/
public static function error( $code, $message, $status = 500 ) {
return new WP_Error( $code, $message, array( 'status' => $status ) );
}
}
add_action( 'plugins_loaded', array( 'B3_JSON_REST_API', 'get_instance' ) );