-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestful-wp.php
executable file
·187 lines (152 loc) · 4.54 KB
/
restful-wp.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
* A REST API for WordPress.
*
* A plugin developed as an alternative to the official WordPress API.
*
* Plugin Name: RESTful WP
* Plugin URI: https://expandedfronts.com
* Description: A RESTful API for WordPress.
* Version: 0.1
* Author: Expanded Fronts, LLC
* Author URI: http://expandedfronts.com/
* License: GPL-3.0+
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt
* Text Domain: restful-wp
* Domain Path: /languages
* Network: true
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* The main RESTful WP class. Initializes the plugin and loads any
* required hooks and dependencies.
*
* @since 1.0
*/
final class RESTful_WP {
/**
* Stores the current instance of RESTful WP.
* @var RESTful_WP
*/
private static $instance;
/**
* Stores an instance of ZendFramework.
* @var Zend\MVC\Application
*/
public static $zend_instance;
/**
* Stores an array of user options and preferences.
* @var array
*/
public $options;
/**
* Empty construct, use revisr() instead.
* @access private
*/
private function __construct() {
// Do nothing here.
}
/**
* Prevent direct __clones by making the method private.
* @access private
*/
private function __clone() {
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'restful-wp'), '1.8' );
}
/**
* Prevent direct unserialization by making the method private.
* @access private
*/
private function __wakeup() {
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'restful-wp'), '1.8' );
}
/**
* Returns an instance of RESTful WP,
* creating one if it doesn't already exist.
*
* @access public
* @return object
*/
public static function get_instance() {
if ( null == self::$instance ) {
// Create a new instance.
self::$instance = new self;
//self::$instance->options = self::$instance->get_options();
// Define any constants.
self::$instance->define_constants();
// Load the instance after everything else has had a chance to load.
add_action( 'after_setup_theme', array( __CLASS__, 'load_instance' ) );
}
return self::$instance;
}
/**
* Defines the constants used throughout the RESTful WP plugin.
* @access private
*/
private function define_constants() {
// The base plugin file.
define( 'RESTFULWP_FILE', __FILE__ );
// The full path used for includes.
define( 'RESTFULWP_PATH', plugin_dir_path( RESTFULWP_FILE ) );
// The URL of the plugin base directory.
define( 'RESTFULWP_URL', plugin_dir_url( RESTFULWP_FILE ) );
// The current version of the plugin.
define( 'RESTFULWP_VERSION', '1.0' );
}
/**
* Loads dependencies and initiates any action hooks.
* @access public
*/
public static function load_instance() {
// Load up Zend/Apigility
include RESTFULWP_PATH . 'vendor/autoload.php';
$config = include RESTFULWP_PATH . 'config/application.config.php';
// Allows for customizing the API url.
add_filter( 'restful_wp_api_slug', array( __CLASS__, 'get_api_slug' ) );
// Only run on API requests.
if ( strpos( $_SERVER['REQUEST_URI'], RESTful_WP::get_api_slug() ) !== false ) {
// Run the application!
self::$zend_instance = Zend\Mvc\Application::init( $config )->run();
// Tell WordPress to beat it, maybe.
if ( defined( 'RESTFULWP_REQUEST_SERVED') && RESTFULWP_REQUEST_SERVED ) {
exit();
}
}
// Load any admin-side hooks.
if ( current_user_can( 'install_plugins' ) && is_admin() ) {
// None yet!
}
}
/**
* Returns the API slug/prefix.
* @access public
* @return string
*/
public static function get_api_slug() {
return get_option( 'restful_wp_api_slug' ) ? get_option( 'restful_wp_api_slug' ) : 'restful-wp';
}
}
/**
* Returns an instance of RESTful WP.
* @access public
* @return object
*/
function restful_wp() {
return RESTful_WP::get_instance();
}
// Let's go!
restful_wp();