This repository has been archived by the owner on Sep 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
wp-simple-web-services.php
173 lines (131 loc) · 3.58 KB
/
wp-simple-web-services.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
<?php
/*
Plugin Name: WP Simple Web Services
Plugin URI: http://www.barrykooij.com/
Description: Simple WordPress Rest Web Services. Add JSON REST web services to your WordPress website with a few clicks.
Version: 1.1.0
Author: Barry Kooij
Author URI: http://www.barrykooij.com/
License: GPL v3
WP Simple Web Services
Copyright (C) 2013, Barry Kooij - barry@cageworks.nl
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/>.
*/
if ( ! defined( 'WPSWS_PLUGIN_DIR' ) ) {
define( 'WPSWS_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}
if ( ! defined( 'WPSWS_PLUGIN_FILE' ) ) {
define( 'WPSWS_PLUGIN_FILE', __FILE__ );
}
class WP_Simple_Web_Service {
const WEBSERVICE_REWRITE = 'webservice/([a-zA-Z0-9_-]+)$';
const OPTION_KEY = 'wpw_options';
private static $instance = null;
/**
* Get singleton instance of class
*
* @return null|WP_Simple_Web_Service
*/
public static function get() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Function that runs on install
*/
public static function install() {
// Clear the permalinks
flush_rewrite_rules();
}
/**
* Constructor
*/
private function __construct() {
// Load files
$this->includes();
// Init
$this->init();
}
/**
* Load required files
*/
private function includes() {
require_once( WPSWS_PLUGIN_DIR . 'classes/class-wpsws-rewrite-rules.php' );
require_once( WPSWS_PLUGIN_DIR . 'classes/class-wpsws-webservice-get-posts.php' );
if ( is_admin() ) {
// Backend
require_once( WPSWS_PLUGIN_DIR . 'classes/class-wpsws-settings.php' );
}
else {
// Frondend
require_once( WPSWS_PLUGIN_DIR . 'classes/class-wpsws-catch-request.php' );
require_once( WPSWS_PLUGIN_DIR . 'classes/class-wpsws-output.php' );
}
}
/**
* Initialize class
*/
private function init() {
// Setup Rewrite Rules
WPSWS_Rewrite_Rules::get();
// Default webservice
WPSWS_Webservice_get_posts::get();
if ( is_admin() ) {
// Backend
// Setup settings
WPSWS_Settings::get();
}
else {
// Frondend
// Catch request
WPSWS_Catch_Request::get();
}
}
/**
* The correct way to throw an error in a webservice
*
* @param $error_string
*/
public function throw_error( $error_string ) {
wp_die( '<b>Webservice error:</b> ' . $error_string );
}
/**
* Function to get the plugin options
*
* @return array
*/
public function get_options() {
return get_option( self::OPTION_KEY, array() );
}
/**
* Function to save the plugin options
*
* @param $options
*/
public function save_options( $options ) {
update_option( self::OPTION_KEY, $options );
}
}
/**
* Function that returns singleton instance of WP_Simple_Web_Service class
*
* @return null|WP_Simple_Web_Service
*/
function WP_Simple_Web_Service() {
return WP_Simple_Web_Service::get();
}
// Load plugin
add_action( 'plugins_loaded', create_function( '', 'WP_Simple_Web_Service::get();' ) );
// Install hook
register_activation_hook( WPSWS_PLUGIN_FILE, array( 'WP_Simple_Web_Service', 'install' ) );