-
Notifications
You must be signed in to change notification settings - Fork 1
/
wp-api-react-poc.php
executable file
·140 lines (123 loc) · 3.28 KB
/
wp-api-react-poc.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
<?php
/**
* Plugin Name: WP API React Proof of Concept
* Plugin URL: http://jimfrenette.com/wordpress
* Description: WordPress plugin to edit posts from the front end using the WP REST API and React
* Version: 1.0
* Author: Jim Frenette
* Author URI: http://jimfrenette.com
* Text Domain: wp-api-react-poc
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
if ( ! class_exists( 'WP_API_React_PoC' ) ) :
/**
* Main WP_API_React_PoC Class.
*
* @class WP_API_React_PoC
* @version 1.0
* @dependencies page slug, e.g., "api-test"
*/
final class WP_API_React_PoC {
/**
* The single instance of the class.
*
* @var WP_API_React_PoC
* @since 1.0
*/
protected static $_instance = null;
/**
* Main WP_API_React_PoC Instance.
*
* Ensures only one instance of WP_API_React_PoC is loaded or can be loaded.
*
* @since 1.0
* @static
* @see WARP()
* @return WP_API_React_PoC - Main instance.
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Cloning is forbidden.
* @since 1.0
*/
public function __clone() {
_doing_it_wrong( __FUNCTION__, __( 'forbidden', 'wp-api-react-poc' ), '1.0' );
}
/**
* Unserializing instances of this class is forbidden.
* @since 1.0
*/
public function __wakeup() {
_doing_it_wrong( __FUNCTION__, __( 'forbidden', 'wp-api-react-poc' ), '1.0' );
}
/**
* WP_API_React_PoC Constructor.
*/
public function __construct() {
$this->define_constants();
$this->includes();
do_action( 'wp-api-react-poc_loaded' );
}
/**
* Define WARP Constants.
*/
private function define_constants() {
$this->define( 'WARP_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
$this->define( 'WARP_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
/**
* Define constant if not already set.
*
* @param string $name
* @param string|bool $value
*/
private function define( $name, $value ) {
if ( ! defined( $name ) ) {
define( $name, $value );
}
}
/**
* What type of request is this?
*
* @param string $type admin, ajax, cron or frontend.
* @return bool
*/
private function is_request( $type ) {
switch ( $type ) {
case 'admin' :
return is_admin();
case 'ajax' :
return defined( 'DOING_AJAX' );
case 'cron' :
return defined( 'DOING_CRON' );
case 'frontend' :
return ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' );
}
}
public function includes() {
if ( $this->is_request( 'frontend' ) ) {
include( WARP_PLUGIN_PATH . 'class-api-rpoc-page.php' );
}
}
}
endif;
/**
* Main instance of WP_API_React_PoC.
*
* Returns the main instance of WARP to prevent the need to use globals.
*
* @since 1.0
* @return WP_API_React_PoC
*/
function WARP() {
return WP_API_React_PoC::instance();
}
// Global for backwards compatibility.
$GLOBALS['wp-api-react-poc'] = WARP();