forked from dannyvankooten/plugin-endpoints
-
Notifications
You must be signed in to change notification settings - Fork 5
/
class-edd-sl-api-endpoint.php
115 lines (93 loc) · 2.57 KB
/
class-edd-sl-api-endpoint.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
<?php
if( stristr( $_SERVER['REQUEST_URI'], '/edd-sl-api' ) !== false ) {
/**
* Class EDD_SL_API_Endpoint
*
* Creates an endpoint for EDD Software Licensing requests
* Halves the memory consumption and runtime of all remote requests
*/
class EDD_SL_API_Endpoint {
public function __construct() {
// set constant to use later on
define( 'EDD_SL_DOING_API', true );
// disable cronjobs for this request
define('DISABLE_WP_CRON', true);
// prevent session query caused by EDD if not set already
if( ! defined( 'EDD_USE_PHP_SESSIONS' ) ) {
define( 'EDD_USE_PHP_SESSIONS', true );
}
// filter active plugins
add_filter( 'option_active_plugins', array( $this, 'filter_active_plugins' ) );
// disable loading of any widgets
add_filter( 'after_setup_theme', array( $this, 'disable_widgets' ) );
// throw error if a result hasn't been returned on init:99
add_action( 'init', array( $this, 'throw_api_error' ), 99 );
}
/**
* Disable all widgets
*/
public function disable_widgets() {
remove_all_actions( 'widgets_init' );
}
/**
* For all requests to the EDD SL API, we only need to load Easy Digital Downloads + Software Licensing
*
* @param $active_plugins
*
* @return array
*/
public function filter_active_plugins( $active_plugins ) {
$active_plugins = array(
//'blackbox-debug-bar/index.php',
'easy-digital-downloads/easy-digital-downloads.php',
'edd-software-licensing/edd-software-licenses.php',
'edd-all-access/edd-all-access.php',
'edd-sl-variable-price-limits/edd-sl-variable-price-limits.php'
);
return $active_plugins;
}
/**
* By now, the EDD SL API should have sent a response and died.
*
* If the request reaches this hook callback, die.
*/
public function throw_api_error() {
$this->send_header( '400 Bad Request' );
$this->send_response(
array(
'status' => 'error',
'message' => 'Something went wrong.'
)
);
}
/**
* @param string $header
*
* Send a header
*/
private function send_header( $header ) {
header( $_SERVER['SERVER_PROTOCOL'] . ' ' . $header );
}
/**
* Send a JSON response
*
* @param array $response
*/
private function send_response( $response ) {
// set correct Content Type header
header( 'Content-Type: application/json' );
echo json_encode( $response );
die();
}
}
new EDD_SL_API_Endpoint;
/**
* Override get_currentuserinfo to prevent an user query
*
* @return bool
*/
function get_currentuserinfo() {
wp_set_current_user( 0 );
return false;
}
}