| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDataAccess\API; |
| 4 |
|
| 5 |
use WPDataAccess\Data_Dictionary\WPDA_Dictionary_Lists; |
| 6 |
use WPDataAccess\Query_Builder\WPDA_Query_Builder; |
| 7 |
use WPDataAccess\Query_Builder\WPDA_Query_Builder_Scheduler; |
| 8 |
use WPDataAccess\Utilities\WPDA_Mail; |
| 9 |
use WPDataAccess\WPDA; |
| 10 |
class WPDA_QB extends WPDA_API_Core { |
| 11 |
const QUERY_BUILDER_AUTO_COMPLETE = 'wpda_query_builder_auto_complete'; |
| 12 |
|
| 13 |
public function register_rest_routes() { |
| 14 |
register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/open', array( |
| 15 |
'methods' => array('POST'), |
| 16 |
'callback' => array($this, 'open'), |
| 17 |
'permission_callback' => '__return_true', |
| 18 |
'args' => array( |
| 19 |
'access' => $this->get_param( 'access' ), |
| 20 |
), |
| 21 |
) ); |
| 22 |
register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/run', array( |
| 23 |
'methods' => array('POST'), |
| 24 |
'callback' => array($this, 'run'), |
| 25 |
'permission_callback' => '__return_true', |
| 26 |
'args' => array( |
| 27 |
'dbs' => $this->get_param( 'dbs' ), |
| 28 |
'query' => $this->get_param( 'query' ), |
| 29 |
'limit' => array( |
| 30 |
'required' => false, |
| 31 |
'type' => 'string', |
| 32 |
'description' => __( 'Limit query output', 'wp-data-access' ), |
| 33 |
'sanitize_callback' => 'sanitize_text_field', |
| 34 |
'validate_callback' => 'rest_validate_request_arg', |
| 35 |
), |
| 36 |
'protect' => array( |
| 37 |
'required' => false, |
| 38 |
'type' => 'boolean', |
| 39 |
'description' => __( 'Protect WordPress tables', 'wp-data-access' ), |
| 40 |
'sanitize_callback' => 'sanitize_text_field', |
| 41 |
'validate_callback' => 'rest_validate_request_arg', |
| 42 |
), |
| 43 |
'params' => $this->get_param( 'params' ), |
| 44 |
), |
| 45 |
) ); |
| 46 |
register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/save', array( |
| 47 |
'methods' => array('POST'), |
| 48 |
'callback' => array($this, 'save'), |
| 49 |
'permission_callback' => '__return_true', |
| 50 |
'args' => array( |
| 51 |
'access' => $this->get_param( 'access' ), |
| 52 |
'dbs' => $this->get_param( 'dbs' ), |
| 53 |
'name' => $this->get_param( 'name' ), |
| 54 |
'query' => $this->get_param( 'query' ), |
| 55 |
'vqb' => $this->get_param( 'vqb' ), |
| 56 |
'old_name' => array( |
| 57 |
'required' => false, |
| 58 |
'type' => 'string', |
| 59 |
'description' => __( 'Old query name', 'wp-data-access' ), |
| 60 |
'sanitize_callback' => 'sanitize_text_field', |
| 61 |
'validate_callback' => 'rest_validate_request_arg', |
| 62 |
), |
| 63 |
'insert' => array( |
| 64 |
'required' => false, |
| 65 |
'type' => 'boolean', |
| 66 |
'description' => __( 'Perform insert', 'wp-data-access' ), |
| 67 |
'sanitize_callback' => 'sanitize_text_field', |
| 68 |
'validate_callback' => 'rest_validate_request_arg', |
| 69 |
), |
| 70 |
'params' => $this->get_param( 'params' ), |
| 71 |
), |
| 72 |
) ); |
| 73 |
register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/delete', array( |
| 74 |
'methods' => array('POST'), |
| 75 |
'callback' => array($this, 'delete'), |
| 76 |
'permission_callback' => '__return_true', |
| 77 |
'args' => array( |
| 78 |
'access' => $this->get_param( 'access' ), |
| 79 |
'name' => $this->get_param( 'name' ), |
| 80 |
), |
| 81 |
) ); |
| 82 |
register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/copy', array( |
| 83 |
'methods' => array('POST'), |
| 84 |
'callback' => array($this, 'copy'), |
| 85 |
'permission_callback' => '__return_true', |
| 86 |
'args' => array( |
| 87 |
'access_from' => $this->get_param( 'access' ), |
| 88 |
'from' => $this->get_param( 'name' ), |
| 89 |
'access_to' => $this->get_param( 'access' ), |
| 90 |
'to' => $this->get_param( 'name' ), |
| 91 |
), |
| 92 |
) ); |
| 93 |
register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/hints', array( |
| 94 |
'methods' => array('POST'), |
| 95 |
'callback' => array($this, 'hints'), |
| 96 |
'permission_callback' => '__return_true', |
| 97 |
'args' => array( |
| 98 |
'dbs' => $this->get_param( 'dbs' ), |
| 99 |
), |
| 100 |
) ); |
| 101 |
register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/ac', array( |
| 102 |
'methods' => array('POST'), |
| 103 |
'callback' => array($this, 'ac'), |
| 104 |
'permission_callback' => '__return_true', |
| 105 |
'args' => array( |
| 106 |
'enable' => array( |
| 107 |
'required' => false, |
| 108 |
'type' => 'boolean', |
| 109 |
'description' => __( 'Get|set auto complete', 'wp-data-access' ), |
| 110 |
'sanitize_callback' => 'sanitize_text_field', |
| 111 |
'validate_callback' => 'rest_validate_request_arg', |
| 112 |
), |
| 113 |
), |
| 114 |
) ); |
| 115 |
register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/cron/schedules', array( |
| 116 |
'methods' => array('POST'), |
| 117 |
'callback' => array($this, 'cron_schedules'), |
| 118 |
'permission_callback' => '__return_true', |
| 119 |
'args' => array(), |
| 120 |
) ); |
| 121 |
register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/cron/add', array( |
| 122 |
'methods' => array('POST'), |
| 123 |
'callback' => array($this, 'cron_add'), |
| 124 |
'permission_callback' => '__return_true', |
| 125 |
'args' => array( |
| 126 |
'access' => $this->get_param( 'access' ), |
| 127 |
'name' => $this->get_param( 'name' ), |
| 128 |
'params' => $this->get_param( 'params' ), |
| 129 |
'start' => array( |
| 130 |
'required' => true, |
| 131 |
'type' => 'integer', |
| 132 |
'description' => __( 'Start date/time', 'wp-data-access' ), |
| 133 |
'sanitize_callback' => 'sanitize_text_field', |
| 134 |
'validate_callback' => 'rest_validate_request_arg', |
| 135 |
), |
| 136 |
'interval' => array( |
| 137 |
'required' => true, |
| 138 |
'type' => 'string', |
| 139 |
'description' => __( 'Recurrence', 'wp-data-access' ), |
| 140 |
'sanitize_callback' => 'sanitize_text_field', |
| 141 |
'validate_callback' => 'rest_validate_request_arg', |
| 142 |
), |
| 143 |
), |
| 144 |
) ); |
| 145 |
register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/cron/delete', array( |
| 146 |
'methods' => array('POST'), |
| 147 |
'callback' => array($this, 'cron_delete'), |
| 148 |
'permission_callback' => '__return_true', |
| 149 |
'args' => array( |
| 150 |
'access' => $this->get_param( 'access' ), |
| 151 |
'name' => $this->get_param( 'name' ), |
| 152 |
'params' => $this->get_param( 'params' ), |
| 153 |
), |
| 154 |
) ); |
| 155 |
register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/vqb/get', array( |
| 156 |
'methods' => array('POST'), |
| 157 |
'callback' => array($this, 'vqb_get'), |
| 158 |
'permission_callback' => '__return_true', |
| 159 |
'args' => array( |
| 160 |
'access' => $this->get_param( 'access' ), |
| 161 |
'name' => $this->get_param( 'name' ), |
| 162 |
), |
| 163 |
) ); |
| 164 |
register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/vqb/tbl', array( |
| 165 |
'methods' => array('POST'), |
| 166 |
'callback' => array($this, 'vqb_tbl'), |
| 167 |
'permission_callback' => '__return_true', |
| 168 |
'args' => array( |
| 169 |
'dbs' => $this->get_param( 'dbs' ), |
| 170 |
'tbl' => $this->get_param( 'tbl' ), |
| 171 |
), |
| 172 |
) ); |
| 173 |
} |
| 174 |
|
| 175 |
public function vqb_tbl( $request ) { |
| 176 |
if ( !$this->current_user_can_access() ) { |
| 177 |
return $this->unauthorized(); |
| 178 |
} |
| 179 |
if ( !$this->current_user_token_valid( $request ) ) { |
| 180 |
return $this->invalid_nonce(); |
| 181 |
} |
| 182 |
return $this->unauthorized(); |
| 183 |
} |
| 184 |
|
| 185 |
public function vqb_get( $request ) { |
| 186 |
if ( !$this->current_user_can_access() ) { |
| 187 |
return $this->unauthorized(); |
| 188 |
} |
| 189 |
if ( !$this->current_user_token_valid( $request ) ) { |
| 190 |
return $this->invalid_nonce(); |
| 191 |
} |
| 192 |
return $this->unauthorized(); |
| 193 |
} |
| 194 |
|
| 195 |
public function open( $request ) { |
| 196 |
if ( !$this->current_user_can_access() ) { |
| 197 |
return $this->unauthorized(); |
| 198 |
} |
| 199 |
if ( !$this->current_user_token_valid( $request ) ) { |
| 200 |
return $this->invalid_nonce(); |
| 201 |
} |
| 202 |
$access = $request->get_param( 'access' ); |
| 203 |
$qb = new WPDA_Query_Builder(); |
| 204 |
if ( 'user' === $access ) { |
| 205 |
$queries = $qb->get_query_list(); |
| 206 |
} else { |
| 207 |
$queries = $qb->get_query_list_global(); |
| 208 |
} |
| 209 |
if ( is_array( $queries ) ) { |
| 210 |
uksort( $queries, 'strnatcasecmp' ); |
| 211 |
} |
| 212 |
return $this->WPDA_Rest_Response( '', $queries ); |
| 213 |
} |
| 214 |
|
| 215 |
public function run( $request ) { |
| 216 |
if ( !$this->current_user_can_access() ) { |
| 217 |
return $this->unauthorized(); |
| 218 |
} |
| 219 |
if ( !$this->current_user_token_valid( $request ) ) { |
| 220 |
return $this->invalid_nonce(); |
| 221 |
} |
| 222 |
$dbs = $request->get_param( 'dbs' ); |
| 223 |
$query = $request->get_param( 'query' ); |
| 224 |
$limit = $request->get_param( 'limit' ); |
| 225 |
$protect = $request->get_param( 'protect' ); |
| 226 |
$params = $request->get_param( 'params' ); |
| 227 |
$qb = new WPDA_Query_Builder(); |
| 228 |
return $this->WPDA_Rest_Response( '', $qb->execute_query( |
| 229 |
$dbs, |
| 230 |
$query, |
| 231 |
$limit, |
| 232 |
$protect, |
| 233 |
$params |
| 234 |
) ); |
| 235 |
} |
| 236 |
|
| 237 |
public function save( $request ) { |
| 238 |
if ( !$this->current_user_can_access() ) { |
| 239 |
return $this->unauthorized(); |
| 240 |
} |
| 241 |
if ( !$this->current_user_token_valid( $request ) ) { |
| 242 |
return $this->invalid_nonce(); |
| 243 |
} |
| 244 |
$access = $request->get_param( 'access' ); |
| 245 |
$dbs = $request->get_param( 'dbs' ); |
| 246 |
$name = $request->get_param( 'name' ); |
| 247 |
$query = $request->get_param( 'query' ); |
| 248 |
$vqb = $request->get_param( 'vqb' ); |
| 249 |
$old_name = $request->get_param( 'old_name' ) ?? ''; |
| 250 |
$insert = '1' === $request->get_param( 'insert' ); |
| 251 |
$params = $request->get_param( 'params' ); |
| 252 |
$qb = new WPDA_Query_Builder(); |
| 253 |
$saved = array(); |
| 254 |
if ( 'user' === $access ) { |
| 255 |
if ( $insert ) { |
| 256 |
// Check if query name is already used |
| 257 |
$saved = $qb->get_query( $name ); |
| 258 |
} |
| 259 |
if ( 0 === count( $saved ) ) { |
| 260 |
$qb->update_query( |
| 261 |
$dbs, |
| 262 |
$name, |
| 263 |
$query, |
| 264 |
$old_name, |
| 265 |
$vqb, |
| 266 |
$params |
| 267 |
); |
| 268 |
} |
| 269 |
} else { |
| 270 |
if ( $insert ) { |
| 271 |
// Check if query name is already used |
| 272 |
$saved = $qb->get_query_global( $name ); |
| 273 |
} |
| 274 |
if ( 0 === count( $saved ) ) { |
| 275 |
$qb->update_query_global( |
| 276 |
$dbs, |
| 277 |
$name, |
| 278 |
$query, |
| 279 |
$old_name, |
| 280 |
$vqb, |
| 281 |
$params |
| 282 |
); |
| 283 |
} |
| 284 |
} |
| 285 |
if ( 0 === count( $saved ) ) { |
| 286 |
return $this->WPDA_Rest_Response( '' ); |
| 287 |
} else { |
| 288 |
return new \WP_Error('error', 'Query name already exists', array( |
| 289 |
'status' => 403, |
| 290 |
)); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
public function delete( $request ) { |
| 295 |
if ( !$this->current_user_can_access() ) { |
| 296 |
return $this->unauthorized(); |
| 297 |
} |
| 298 |
if ( !$this->current_user_token_valid( $request ) ) { |
| 299 |
return $this->invalid_nonce(); |
| 300 |
} |
| 301 |
$access = $request->get_param( 'access' ); |
| 302 |
$name = $request->get_param( 'name' ); |
| 303 |
$qb = new WPDA_Query_Builder(); |
| 304 |
if ( 'user' === $access ) { |
| 305 |
$qb->delete_query( $name ); |
| 306 |
} else { |
| 307 |
$qb->delete_query_global( $name ); |
| 308 |
} |
| 309 |
return $this->WPDA_Rest_Response( '' ); |
| 310 |
} |
| 311 |
|
| 312 |
public function copy( $request ) { |
| 313 |
if ( !$this->current_user_can_access() ) { |
| 314 |
return $this->unauthorized(); |
| 315 |
} |
| 316 |
if ( !$this->current_user_token_valid( $request ) ) { |
| 317 |
return $this->invalid_nonce(); |
| 318 |
} |
| 319 |
$access_from = $request->get_param( 'access_from' ); |
| 320 |
$from_name = $request->get_param( 'from' ); |
| 321 |
$access_to = $request->get_param( 'access_to' ); |
| 322 |
$to_name = $request->get_param( 'to' ); |
| 323 |
$qb = new WPDA_Query_Builder(); |
| 324 |
$source = ( 'user' === $access_from ? $qb->get_query( $from_name ) : $qb->get_query_global( $from_name ) ); |
| 325 |
if ( 0 === count( $source ) ) { |
| 326 |
return new \WP_Error('error', 'Source query not found', array( |
| 327 |
'status' => 403, |
| 328 |
)); |
| 329 |
} |
| 330 |
if ( 'user' === $access_to ) { |
| 331 |
$saved = $qb->get_query( $to_name ); |
| 332 |
if ( 0 === count( $saved ) ) { |
| 333 |
$qb->add_query( $to_name, $source ); |
| 334 |
} |
| 335 |
} else { |
| 336 |
$saved = $qb->get_query_global( $to_name ); |
| 337 |
if ( 0 === count( $saved ) ) { |
| 338 |
$qb->add_query_global( $to_name, $source ); |
| 339 |
} |
| 340 |
} |
| 341 |
if ( 0 === count( $saved ) ) { |
| 342 |
return $this->WPDA_Rest_Response( '' ); |
| 343 |
} else { |
| 344 |
return new \WP_Error('error', 'Query name already exists', array( |
| 345 |
'status' => 403, |
| 346 |
)); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
public function hints( $request ) { |
| 351 |
if ( !$this->current_user_can_access() ) { |
| 352 |
return $this->unauthorized(); |
| 353 |
} |
| 354 |
if ( !$this->current_user_token_valid( $request ) ) { |
| 355 |
return $this->invalid_nonce(); |
| 356 |
} |
| 357 |
$dbs = $request->get_param( 'dbs' ); |
| 358 |
$qb = new WPDA_Query_Builder(); |
| 359 |
return $this->WPDA_Rest_Response( '', $qb->get_hints( $dbs ) ); |
| 360 |
} |
| 361 |
|
| 362 |
public function ac( $request ) { |
| 363 |
if ( !$this->current_user_can_access() ) { |
| 364 |
return $this->unauthorized(); |
| 365 |
} |
| 366 |
if ( !$this->current_user_token_valid( $request ) ) { |
| 367 |
return $this->invalid_nonce(); |
| 368 |
} |
| 369 |
$enable = $request->get_param( 'enable' ); |
| 370 |
if ( null === $enable ) { |
| 371 |
// Return current value |
| 372 |
return $this->WPDA_Rest_Response( get_user_meta( WPDA::get_current_user_id(), self::QUERY_BUILDER_AUTO_COMPLETE, true ) ); |
| 373 |
} elseif ( '1' == $enable ) { |
| 374 |
// Enable auto complete |
| 375 |
update_user_meta( WPDA::get_current_user_id(), self::QUERY_BUILDER_AUTO_COMPLETE, true ); |
| 376 |
return $this->WPDA_Rest_Response( '' ); |
| 377 |
} else { |
| 378 |
// Enable auto complete |
| 379 |
update_user_meta( WPDA::get_current_user_id(), self::QUERY_BUILDER_AUTO_COMPLETE, false ); |
| 380 |
return $this->WPDA_Rest_Response( '' ); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
public function cron_schedules( $request ) { |
| 385 |
if ( !$this->current_user_can_access() ) { |
| 386 |
return $this->unauthorized(); |
| 387 |
} |
| 388 |
if ( !$this->current_user_token_valid( $request ) ) { |
| 389 |
return $this->invalid_nonce(); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
public function cron_add( $request ) { |
| 394 |
if ( !$this->current_user_can_access() ) { |
| 395 |
return $this->unauthorized(); |
| 396 |
} |
| 397 |
if ( !$this->current_user_token_valid( $request ) ) { |
| 398 |
return $this->invalid_nonce(); |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
public function cron_delete( $request ) { |
| 403 |
if ( !$this->current_user_can_access() ) { |
| 404 |
return $this->unauthorized(); |
| 405 |
} |
| 406 |
if ( !$this->current_user_token_valid( $request ) ) { |
| 407 |
return $this->invalid_nonce(); |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
} |
| 412 |
|