-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-mobile.php
355 lines (314 loc) · 10.9 KB
/
wp-mobile.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
/**
* Plugin Name: WP Mobile App
* Plugin URI: http://wpm.ciphersoul.com/
* Description: Plugin let you allow to create mobile application in wordpress
* Version: 1.0.0
* Author: Dhaval Parekh
* Author URI: https://about.me/Dmparekh/
* Text Domain: wp-mobile
* Domain Path: /languages
*
* Plugin let you allow to create mobile application in wordpress
*
* @package wp-mobile
* @version 1.0.0
* @author Dhaval Parekh <[email protected]>
* @copyright
* @link http://wpm.ciphersoul.com/
* @license
*/
if ( ! defined( 'REST_NAMESPACE' ) ) {
//define( 'REST_NAMESPACE', 'api' );
}
/**
* WP_Mobile class
*
* @access public
* @since 1.0.0
*/
class WP_Mobile {
/**
* rest namespace
*
* @var string
*/
private $namespace = 'wp/v2/';
/**
* plugin directory path
*
* @access private
* @var string
* @since 1.0.0
*/
private $plugin_path = '';
/**
* plugin directory URI
*
* @access private
* @var string
* @since 1.0.0
*/
private $plugin_uri = '';
/**
* vendor directory path
*
* @access private
* @var string
* @since 1.0.0
*/
private $vendor_dir = '';
/**
* library directory path
*
* @access private
* @var string
* @since 1.0.0
*/
private $lib_dir = '';
/**
* include directory path
*
* @access private
* @var string
* @since 1.0.0
*/
private $inc_dir = '';
/**
* construct method
*
* @access private
* @return bool
* @since 1.0.0
*/
public function __construct() {
$this->setup();
include_once( $this->inc_dir . 'debug.php' );
if ( ! $this->is_required_plugin_installed() ) {
include_once( $this->vendor_dir . 'class-tgm-plugin-activation.php' );
include_once( $this->inc_dir . 'required_plugins.php' );
return false;
}
if ( defined( 'REST_NAMESPACE' ) ) {
$this->namespace = REST_NAMESPACE;
}
$this->init();
// add_action( 'init', array( $this, 'init' ), 0 );
return true;
}
/**
* initilization
*
* @access public
* @since 1.0.0
* @return bool
*/
public function init() {
$this->includes();
add_action( 'rest_api_init', array( $this, 'overwrite_rest_api' ), -1 );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ), 15 );
return true;
}
public function enqueue() {
$prefix = '.min';
if ( defined( 'SCRIPT_DEBUG' ) && true === SCRIPT_DEBUG ) {
$prefix = '';
}
wp_enqueue_script( 'wp-mobile-admin', WP_MOBILE_URL . 'assets/js/wp-mobile-admin' . $prefix . '.js', array( 'jquery' ) );
wp_enqueue_style( 'wp-mobile-admin', WP_MOBILE_URL . 'assets/css/wp-mobile-admin' . $prefix . '.css' );
}
/**
* setup plugin variable
*
* @access public
* @since 1.0.0
* @return void
*/
private function setup() {
// Main plugin directory path and URI.
$this->plugin_path = trailingslashit( plugin_dir_path( __FILE__ ) );
$this->plugin_uri = trailingslashit( plugin_dir_url( __FILE__ ) );
// Plugin directory path
$this->vendor_dir = trailingslashit( $this->plugin_path . 'vendor' );
$this->lib_dir = trailingslashit( $this->plugin_path . 'lib' );
$this->inc_dir = trailingslashit( $this->plugin_path . 'inc' );
$this->template_dir = trailingslashit( $this->plugin_path . 'templates' );
// define constanst
$this->define_constants();
}
/**
* define plugin constants
*
* @since 1.0.0
* @access private
*/
private function define_constants() {
$this->define( 'WP_MOBILE_PATH', $this->plugin_path );
$this->define( 'WP_MOBILE_URL', $this->plugin_uri );
$this->define( 'WP_MOBILE_INC_PATH', $this->inc_dir );
$this->define( 'WP_MOBILE_LIB_PATH', $this->lib_dir );
$this->define( 'WP_MOBILE_VENDOR_PATH', $this->vendor_dir );
$this->define( 'WP_MOBILE_TEMPLATE_PATH', $this->template_dir );
}
/**
* set constant if not already set
*
* @since 1.0.0
* @access private
* @param string name
* @param string|bool value
*/
private function define( $name, $value ) {
if ( ! defined( $name ) ) {
define( $name, $value );
}
}
private function includes() {
// include lib
require_once $this->lib_dir . 'class-wp-mobile-crypto.php';
// include lib
require_once $this->lib_dir . 'class-wp-mobile-controls.php';
require_once $this->lib_dir . 'class-wp-mobile-plugin-settings.php';
// include inc
require_once $this->inc_dir . 'helpers.php';
require_once $this->inc_dir . 'class-wp-mobile-authenticate-user.php';
require_once $this->inc_dir . 'plugin-settings/class-wp-mobile-general-settings.php';
require_once $this->inc_dir . 'rest-api/class-wp-mobile-posts-controller.php';
require_once $this->inc_dir . 'rest-api/class-wp-mobile-users-controller.php';
require_once $this->inc_dir . 'rest-api/class-wp-mobile-attachments-controller.php';
require_once $this->inc_dir . 'rest-api/class-wp-mobile-taxonomies-controller.php';
require_once $this->inc_dir . 'rest-api/class-wp-mobile-post-types-controller.php';
require_once $this->inc_dir . 'rest-api/class-wp-mobile-post-statuses-controller.php';
require_once $this->inc_dir . 'rest-api/class-wp-mobile-comments-controller.php';
require_once $this->inc_dir . 'rest-api/class-wp-mobile-revisions-controller.php';
require_once $this->inc_dir . 'rest-api/class-wp-mobile-terms-controller.php';
}
/**
* check requried plugin installed or not
*
* @access public
* @since 1.0.0
* @return boolean
*/
private function is_required_plugin_installed() {
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
$requvired_plugin_list = array(
'rest-api/plugin.php',
'butterbean/butterbean.php',
);
foreach ( $requvired_plugin_list as $plugin ) {
if ( ! is_plugin_active( $plugin ) ) {
return false;
}
}
return true;
}
public function overwrite_rest_api() {
global $wp_post_types, $wp_taxonomies;
add_filter( 'get_rest_namespace', array( $this, 'get_rest_namespace' ), 15 );
$controller = new WP_Mobile_Users_Controller();
$controller->register_routes();
add_filter( 'get_users', array( $this, 'overwrite_rest_api_response' ), 15, 1 );
add_filter( 'get_user', array( $this, 'overwrite_rest_api_response' ), 15, 1 );
add_filter( 'create_user', array( $this, 'overwrite_rest_api_response' ), 15, 1 );
add_filter( 'update_user', array( $this, 'overwrite_rest_api_response' ), 15, 1 );
add_filter( 'delete_user', array( $this, 'overwrite_rest_api_response' ), 15, 1 );
$controller = new WP_Mobile_Taxonomies_Controller();
$controller->register_routes();
add_filter( 'get_taxonomies', array( $this, 'overwrite_rest_api_response' ), 15, 1 );
add_filter( 'get_taxonomy', array( $this, 'overwrite_rest_api_response' ), 15, 1 );
$controller = new WP_Mobile_Post_Types_Controller();
$controller->register_routes();
add_filter( 'get_post_types', array( $this, 'overwrite_rest_api_response' ), 15, 1 );
add_filter( 'get_post_type', array( $this, 'overwrite_rest_api_response' ), 15, 1 );
$controller = new WP_Mobile_Post_Statuses_Controller();
$controller->register_routes();
add_filter( 'get_post_statuses', array( $this, 'overwrite_rest_api_response' ), 15, 1 );
add_filter( 'get_post_status', array( $this, 'overwrite_rest_api_response' ), 15, 1 );
$controller = new WP_Mobile_Comments_Controller();
$controller->register_routes();
add_filter( 'get_comments', array( $this, 'overwrite_rest_api_response' ), 15, 1 );
add_filter( 'get_comment', array( $this, 'overwrite_rest_api_response' ), 15, 1 );
add_filter( 'create_comment', array( $this, 'overwrite_rest_api_response' ), 15, 1 );
add_filter( 'update_comment', array( $this, 'overwrite_rest_api_response' ), 15, 1 );
add_filter( 'delete_comment', array( $this, 'overwrite_rest_api_response' ), 15, 1 );
$post_types = array( 'post', 'page', 'attachment' );
$post_types = apply_filters( 'wp_mobile_post_type_to_overwrite', $post_types );
$wp_post_types['attachment']->rest_controller_class = 'WP_Mobile_Attachments_Controller';
foreach ( $post_types as $post_type ) {
if ( isset( $wp_post_types[ $post_type ] ) ) {
if ( ! isset( $wp_post_types[ $post_type ]->rest_controller_class ) || 'WP_REST_Posts_Controller' === $wp_post_types[ $post_type ]->rest_controller_class ) {
$wp_post_types[ $post_type ]->rest_controller_class = 'WP_Mobile_Posts_Controller';
}
}
add_filter( "get_{$post_type}_items", array( $this, 'overwrite_rest_api_response' ), 15, 1 );
add_filter( "get_{$post_type}_item", array( $this, 'overwrite_rest_api_response' ), 15, 1 );
add_filter( "create_{$post_type}_item", array( $this, 'overwrite_rest_api_response' ), 15, 1 );
add_filter( "update_{$post_type}_item", array( $this, 'overwrite_rest_api_response' ), 15, 1 );
add_filter( "delete_{$post_type}_item", array( $this, 'overwrite_rest_api_response' ), 15, 1 );
$controller = new WP_Mobile_Revisions_Controller( $post_type );
$controller->register_routes();
}
add_filter( 'get_revisions', array( $this, 'overwrite_rest_api_response' ), 15, 1 );
add_filter( 'get_revision', array( $this, 'overwrite_rest_api_response' ), 15, 1 );
add_filter( 'delete_revision', array( $this, 'overwrite_rest_api_response' ), 15, 1 );
$taxonomies = array( 'category', 'post_tag' );
$taxonomies = apply_filters( 'wp_mobile_taxonomy_to_overwrite', $taxonomies );
foreach ( $taxonomies as $taxonomy ) {
if ( isset( $wp_taxonomies[ $taxonomy ] ) ) {
if ( ! isset( $wp_taxonomies[ $taxonomy ]->rest_controller_class ) || 'WP_REST_Terms_Controller' === $wp_taxonomies[ $taxonomy ]->rest_controller_class ) {
$wp_taxonomies[ $taxonomy ]->rest_controller_class = 'WP_Mobile_Terms_Controller';
}
}
add_filter( "get_{$taxonomy}_items", array( $this, 'overwrite_rest_api_response' ), 15, 1 );
add_filter( "get_{$taxonomy}_item", array( $this, 'overwrite_rest_api_response' ), 15, 1 );
add_filter( "create_{$taxonomy}_item", array( $this, 'overwrite_rest_api_response' ), 15, 1 );
add_filter( "update_{$taxonomy}_item", array( $this, 'overwrite_rest_api_response' ), 15, 1 );
add_filter( "delete_{$taxonomy}_item", array( $this, 'overwrite_rest_api_response' ), 15, 1 );
}
do_action( 'wp_mobile_overwrite_rest_api', $this );
}
public function get_rest_namespace() {
return untrailingslashit( $this->namespace );
}
/**
* add wrapper in the rest api response
*
* @hooked
* @access public
* @since 1.0.0
* @param WP_REST_Response $response request object
* @return WP_REST_Response response object
*/
public function overwrite_rest_api_response( $response ) {
if ( is_wp_error( $response ) ) {
return $response;
}
if ( ! $response instanceof WP_REST_Response ) {
return $response;
}
$data = $response->data;
if ( ! isset( $data['code'] ) ) {
$response->data = array(
'code' => 'rest_success',
'message' => 'Rest Success',
'data' => $data,
);
}
unset( $data );
return $response;
}
}
if ( ! function_exists( 'wp_mobile_init' ) ) {
/**
* initilized wp_mobile plugin
*
* @access public
* @since 1.0.0
* @return void
*/
function wp_mobile_init() {
new WP_Mobile();
}
wp_mobile_init();
}