-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
wp-query-console.php
333 lines (294 loc) · 7.4 KB
/
wp-query-console.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
<?php
/**
* Contributors: lubus, ajitbohra
* Plugin Name: WP Query Console
* Plugin URI: https://www.lubus.in
* Description: A plugin to quickly test various WP Queries
* Author: LUBUS
* Author URI: https://lubus.in
* Version: 1.0
* Text Domain: wqc
* Domain Path: /languages
* GitHub Plugin URI: https://github.com/lubusIN/WP-Query-Console
* Tags: admin, test, query, development
* Requires at least: 3.0.1
* Tested up to: 4.9.4
* Stable tag: 1.0
* License: GPLv3 or later
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*
* @package WP_Query_Console
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
if ( ! class_exists( 'WP_Query_Console' ) ) :
/**
* WP_Query_Console Class.
*
* Main Class.
*
* @since 1.0.0
*/
class WP_Query_Console {
/**
* Instance.
*
* @since
* @access private
* @var WP_Query_Console
*/
static private $instance;
/**
* Singleton pattern.
*
* @since
* @access private
*/
private function __construct() {
$this->setup_constants();
$this->init_hooks();
}
/**
* Get instance.
*
* @since
* @access public
* @return WP_Query_Console
*/
public static function get_instance() {
if ( null === static::$instance ) {
self::$instance = new static();
self::$instance->init();
}
return self::$instance;
}
/**
* Hook into actions and filters.
*
* @since 1.0.0
*/
private function init_hooks() {
// Set up localization on init Hook.
add_action( 'init', array( $this, 'load_textdomain' ), 0 );
add_action( 'admin_menu', array( $this, 'add_menu' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'register_script' ) );
add_action( 'rest_api_init', array( $this, 'register_routes' ) );
// Disable emojis
add_action( 'init', array( $this, 'disable_emojis' ) );
}
/**
* Class Constructor
*
* Set up the WP Query Console class.
*
* @since 1.0.0
* @access private
*/
private function init() {
// Plugin Init
}
/**
* Throw error on object clone
*
* The whole idea of the singleton design pattern is that there is a single
* object, therefore we don't want the object to be cloned.
*
* @since 1.0
* @access protected
*
* @return void
*/
public function __clone() {
// Cloning instances of the class is forbidden.
wqc_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'wqc' ), '1.0' );
}
/**
* Disable unserializing of the class
*
* @since 1.0
* @access protected
*
* @return void
*/
public function __wakeup() {
// Unserializing instances of the class is forbidden.
wqc_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'wqc' ), '1.0' );
}
/**
* Setup plugin constants
*
* @since 1.0
* @access private
*
* @return void
*/
private function setup_constants() {
// Plugin version
if ( ! defined( 'WQC_VERSION' ) ) {
define( 'WQC_VERSION', '1.0.0' );
}
// Plugin Root File
if ( ! defined( 'WQC_PLUGIN_FILE' ) ) {
define( 'WQC_PLUGIN_FILE', __FILE__ );
}
// Plugin Folder Path
if ( ! defined( 'WQC_PLUGIN_DIR' ) ) {
define( 'WQC_PLUGIN_DIR', plugin_dir_path( WQC_PLUGIN_FILE ) );
}
// Plugin Folder URL
if ( ! defined( 'WQC_PLUGIN_URL' ) ) {
define( 'WQC_PLUGIN_URL', plugin_dir_url( WQC_PLUGIN_FILE ) );
}
// Plugin Basename aka: "WP-Query-Console/wp-query-console.php"
if ( ! defined( 'WQC_PLUGIN_BASENAME' ) ) {
define( 'WQC_PLUGIN_BASENAME', plugin_basename( WQC_PLUGIN_FILE ) );
}
}
/**
* Loads the plugin language files.
*
* @since 1.0.0
* @access public
*
* @return void
*/
public function load_textdomain() {
$locale = apply_filters( 'plugin_locale', get_locale(), 'WQC' );
// wp-content/languages/plugin-name/plugin-name-en_EN.mo.
load_textdomain( 'WQC', trailingslashit( WP_LANG_DIR ) . 'WP-Query-Console' . '/' . 'TAF' . '-' . $locale . '.mo' );
// wp-content/plugins/plugin-name/languages/plugin-name-en_EN.mo.
load_plugin_textdomain( 'WQC', false, basename( WQC_PLUGIN_DIR ) . '/languages/' );
}
/**
* Add plugin menu
*
* @since 1.0.0
* @access public
*
* @return void
*/
public function add_menu() {
add_menu_page(
'WP Query Console',
'Query Console',
'manage_options',
'wp-query-console',
array( $this, 'render_page' ),
'dashicons-controls-play'
);
}
/**
* Registers scripts
*
* @since 1.0.0
* @access public
*
* @return void
*/
public function register_script(){
wp_register_script(
'wqc-script',
WQC_PLUGIN_URL . 'build/admin.js',
array( 'wp-api' ) ,
filemtime( WQC_PLUGIN_DIR . 'build/admin.js' ),
true
);
wp_enqueue_script( 'wqc-script' );
wp_register_style( 'wqc-style', WQC_PLUGIN_URL . 'build/admin.css' );
wp_enqueue_style( 'wqc-style' );
wp_localize_script( 'wqc-script', 'wqcRestApi',
array(
'nonce' => wp_create_nonce( 'wp_rest' ),
)
);
}
public function disable_emojis(){
// Diable emojis
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('admin_print_scripts', 'print_emoji_detection_script');
remove_action('admin_print_styles', 'print_emoji_styles');
}
/**
* Render plugin page
*
* @since 1.0.0
* @access public
*
* @return void
*/
public function render_page() {
echo '<div id="wp-query-console" class="wrap"></div>';
}
/**
* Register API route
* @todo : prevent cross domain api request
*
* @since 1.0.0
* @access public
*/
public function register_routes() {
register_rest_route('wqc/v1', '/query', array(
'methods' => array( 'GET', 'POST' ),
'callback' => array( $this, 'get_query_result' ),
) );
}
/**
* Rest fetch query result callback
*
* @param WP_REST_Request $request
*
* @access public
* @return array|mixed|object
*/
public function get_query_result( $request ) {
$parameters = $request->get_params();
// Bailout
if ( empty( $parameters ) || empty( $parameters['queryArgs'] ) ) {
return new WP_REST_Response( array('status' => 'error', 'message' => 'Query args missing'), 400 );
}
$args_string = $parameters['queryArgs'];
$selected_type = $parameters['queryType'];
// Check for valid array value.
try {
$args_array = eval( "return $args_string;" ); // Converting user input into array.
$args_array = (is_array( $args_array ) ? $args_array : null); // Check is input is valid array.
} catch (ParseError $e) {
$args_array = null;
}
if ( $args_array ) {
switch ( $selected_type ) {
case 'WP_Query':
$query = new WP_Query( $args_array );
$query = $query->posts;
break;
case 'WP_User_Query':
$query = new WP_User_Query( $args_array );
$query = $query->results;
break;
case 'WP_Comment_Query':
$query = new WP_Comment_Query( $args_array );
break;
case 'WP_Term_Query':
$query = new WP_Term_Query( $args_array );
break;
case 'WP_Network_Query':
$query = new WP_Network_Query( $args_array );
break;
case 'WP_Site_Query':
$query = new WP_Site_Query( $args_array );
break;
default:
$query = new WP_Query( $args_array );
break;
}
return new WP_REST_Response(array('status' => 'success', 'data' => json_encode( $query ) , 'message' => 'Query executed successfully'), 200);
} else {
return new WP_REST_Response(array('status' => 'error', 'data' => 'null' ,'message' => 'Query args invalid'), 400);
}
}
}
endif;
WP_Query_Console::get_instance();
?>