| @@ -1,10 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace WPDataAccess\API; |
| 4 | 4 | |
| 5 | +use WPDataAccess\Data_Dictionary\WPDA_Dictionary_Lists; | |
| 5 | 6 | use WPDataAccess\Query_Builder\WPDA_Query_Builder; |
| 6 | 7 | use WPDataAccess\Query_Builder\WPDA_Query_Builder_Scheduler; |
| 8 | +use WPDataAccess\Utilities\WPDA_Mail; | |
| 7 | 9 | use WPDataAccess\WPDA; |
| 8 | 10 | class WPDA_QB extends WPDA_API_Core { |
| 9 | 11 | const QUERY_BUILDER_AUTO_COMPLETE = 'wpda_query_builder_auto_complete'; |
| 10 | 12 | |
| @@ -45,21 +47,28 @@ | ||
| 45 | 47 | 'methods' => array('POST'), |
| 46 | 48 | 'callback' => array($this, 'save'), |
| 47 | 49 | 'permission_callback' => '__return_true', |
| 48 | 50 | 'args' => array( |
| 49 | - 'access' => $this->get_param( 'access' ), | |
| 50 | - 'dbs' => $this->get_param( 'dbs' ), | |
| 51 | - 'name' => $this->get_param( 'name' ), | |
| 52 | - 'query' => $this->get_param( 'query' ), | |
| 53 | - 'vqb' => $this->get_param( 'vqb' ), | |
| 54 | - 'old_name' => 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 | + 'is_visual' => array( | |
| 55 | 56 | 'required' => false, |
| 57 | + 'type' => 'boolean', | |
| 58 | + 'description' => __( 'Is Visual Query?', 'wp-data-access' ), | |
| 59 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 60 | + 'validate_callback' => 'rest_validate_request_arg', | |
| 61 | + ), | |
| 62 | + 'vqb' => $this->get_param( 'vqb' ), | |
| 63 | + 'old_name' => array( | |
| 64 | + 'required' => false, | |
| 56 | 65 | 'type' => 'string', |
| 57 | 66 | 'description' => __( 'Old query name', 'wp-data-access' ), |
| 58 | 67 | 'sanitize_callback' => 'sanitize_text_field', |
| 59 | 68 | 'validate_callback' => 'rest_validate_request_arg', |
| 60 | 69 | ), |
| 61 | - 'insert' => array( | |
| 70 | + 'insert' => array( | |
| 62 | 71 | 'required' => false, |
| 63 | 72 | 'type' => 'boolean', |
| 64 | 73 | 'description' => __( 'Perform insert', 'wp-data-access' ), |
| 65 | 74 | 'sanitize_callback' => 'sanitize_text_field', |
| @@ -64,9 +73,9 @@ | ||
| 64 | 73 | 'description' => __( 'Perform insert', 'wp-data-access' ), |
| 65 | 74 | 'sanitize_callback' => 'sanitize_text_field', |
| 66 | 75 | 'validate_callback' => 'rest_validate_request_arg', |
| 67 | 76 | ), |
| 68 | - 'params' => $this->get_param( 'params' ), | |
| 77 | + 'params' => $this->get_param( 'params' ), | |
| 69 | 78 | ), |
| 70 | 79 | ) ); |
| 71 | 80 | register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/delete', array( |
| 72 | 81 | 'methods' => array('POST'), |
| @@ -149,10 +158,59 @@ | ||
| 149 | 158 | 'name' => $this->get_param( 'name' ), |
| 150 | 159 | 'params' => $this->get_param( 'params' ), |
| 151 | 160 | ), |
| 152 | 161 | ) ); |
| 162 | + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/vqb/get', array( | |
| 163 | + 'methods' => array('POST'), | |
| 164 | + 'callback' => array($this, 'vqb_get'), | |
| 165 | + 'permission_callback' => '__return_true', | |
| 166 | + 'args' => array( | |
| 167 | + 'access' => $this->get_param( 'access' ), | |
| 168 | + 'name' => $this->get_param( 'name' ), | |
| 169 | + ), | |
| 170 | + ) ); | |
| 171 | + register_rest_route( WPDA_API::WPDA_NAMESPACE, 'qb/vqb/tbl', array( | |
| 172 | + 'methods' => array('POST'), | |
| 173 | + 'callback' => array($this, 'vqb_tbl'), | |
| 174 | + 'permission_callback' => '__return_true', | |
| 175 | + 'args' => array( | |
| 176 | + 'dbs' => $this->get_param( 'dbs' ), | |
| 177 | + 'tbl' => $this->get_param( 'tbl' ), | |
| 178 | + ), | |
| 179 | + ) ); | |
| 153 | 180 | } |
| 154 | 181 | |
| 182 | + public function vqb_tbl( $request ) { | |
| 183 | + if ( !$this->current_user_can_access() ) { | |
| 184 | + return $this->unauthorized(); | |
| 185 | + } | |
| 186 | + if ( !$this->current_user_token_valid( $request ) ) { | |
| 187 | + return $this->invalid_nonce(); | |
| 188 | + } | |
| 189 | + $dbs = $request->get_param( 'dbs' ); | |
| 190 | + $tbl = $request->get_param( 'tbl' ); | |
| 191 | + $cols = WPDA_Dictionary_Lists::get_table_widget_info( $dbs, $tbl ); | |
| 192 | + return $this->WPDA_Rest_Response( '', $cols ); | |
| 193 | + } | |
| 194 | + | |
| 195 | + public function vqb_get( $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 | + $name = $request->get_param( 'name' ); | |
| 204 | + $qb = new WPDA_Query_Builder(); | |
| 205 | + if ( 'user' === $access ) { | |
| 206 | + $query = $qb->get_visual_query( $name ); | |
| 207 | + } else { | |
| 208 | + $query = maybe_unserialize( $qb->get_visual_query_global( $name ) ); | |
| 209 | + } | |
| 210 | + return $this->WPDA_Rest_Response( '', $query ); | |
| 211 | + } | |
| 212 | + | |
| 155 | 213 | public function open( $request ) { |
| 156 | 214 | if ( !$this->current_user_can_access() ) { |
| 157 | 215 | return $this->unauthorized(); |
| 158 | 216 | } |
| @@ -204,8 +262,9 @@ | ||
| 204 | 262 | $access = $request->get_param( 'access' ); |
| 205 | 263 | $dbs = $request->get_param( 'dbs' ); |
| 206 | 264 | $name = $request->get_param( 'name' ); |
| 207 | 265 | $query = $request->get_param( 'query' ); |
| 266 | + $is_visual = '1' === $request->get_param( 'is_visual' ); | |
| 208 | 267 | $vqb = $request->get_param( 'vqb' ); |
| 209 | 268 | $old_name = $request->get_param( 'old_name' ) ?? ''; |
| 210 | 269 | $insert = '1' === $request->get_param( 'insert' ); |
| 211 | 270 | $params = $request->get_param( 'params' ); |
| @@ -221,8 +280,9 @@ | ||
| 221 | 280 | $dbs, |
| 222 | 281 | $name, |
| 223 | 282 | $query, |
| 224 | 283 | $old_name, |
| 284 | + $is_visual, | |
| 225 | 285 | $vqb, |
| 226 | 286 | $params |
| 227 | 287 | ); |
| 228 | 288 | } |
| @@ -236,8 +296,9 @@ | ||
| 236 | 296 | $dbs, |
| 237 | 297 | $name, |
| 238 | 298 | $query, |
| 239 | 299 | $old_name, |
| 300 | + $is_visual, | |
| 240 | 301 | $vqb, |
| 241 | 302 | $params |
| 242 | 303 | ); |
| 243 | 304 | } |
| @@ -281,8 +342,9 @@ | ||
| 281 | 342 | $access_to = $request->get_param( 'access_to' ); |
| 282 | 343 | $to_name = $request->get_param( 'to' ); |
| 283 | 344 | $qb = new WPDA_Query_Builder(); |
| 284 | 345 | $source = ( 'user' === $access_from ? $qb->get_query( $from_name ) : $qb->get_query_global( $from_name ) ); |
| 346 | + $visual = ( 'user' === $access_from ? $qb->get_visual_query( $from_name ) : maybe_unserialize( $qb->get_visual_query_global( $from_name ) ) ); | |
| 285 | 347 | if ( 0 === count( $source ) ) { |
| 286 | 348 | return new \WP_Error('error', 'Source query not found', array( |
| 287 | 349 | 'status' => 403, |
| 288 | 350 | )); |
| @@ -290,13 +352,19 @@ | ||
| 290 | 352 | if ( 'user' === $access_to ) { |
| 291 | 353 | $saved = $qb->get_query( $to_name ); |
| 292 | 354 | if ( 0 === count( $saved ) ) { |
| 293 | 355 | $qb->add_query( $to_name, $source ); |
| 356 | + if ( !empty( $visual ) ) { | |
| 357 | + $qb->upd_visual_query( $to_name, $visual ); | |
| 358 | + } | |
| 294 | 359 | } |
| 295 | 360 | } else { |
| 296 | 361 | $saved = $qb->get_query_global( $to_name ); |
| 297 | 362 | if ( 0 === count( $saved ) ) { |
| 298 | 363 | $qb->add_query_global( $to_name, $source ); |
| 364 | + if ( !empty( $visual ) ) { | |
| 365 | + $qb->upd_visual_query_global( $to_name, $visual ); | |
| 366 | + } | |
| 299 | 367 | } |
| 300 | 368 | } |
| 301 | 369 | if ( 0 === count( $saved ) ) { |
| 302 | 370 | return $this->WPDA_Rest_Response( '' ); |
| @@ -341,31 +409,19 @@ | ||
| 341 | 409 | } |
| 342 | 410 | } |
| 343 | 411 | |
| 344 | 412 | public function cron_schedules( $request ) { |
| 345 | - if ( !$this->current_user_can_access() ) { | |
| 346 | - return $this->unauthorized(); | |
| 347 | - } | |
| 348 | - if ( !$this->current_user_token_valid( $request ) ) { | |
| 349 | - return $this->invalid_nonce(); | |
| 350 | - } | |
| 413 | + // This feature is implemented in the premium version | |
| 414 | + return $this->unauthorized(); | |
| 351 | 415 | } |
| 352 | 416 | |
| 353 | 417 | public function cron_add( $request ) { |
| 354 | - if ( !$this->current_user_can_access() ) { | |
| 355 | - return $this->unauthorized(); | |
| 356 | - } | |
| 357 | - if ( !$this->current_user_token_valid( $request ) ) { | |
| 358 | - return $this->invalid_nonce(); | |
| 359 | - } | |
| 418 | + // This feature is implemented in the premium version | |
| 419 | + return $this->unauthorized(); | |
| 360 | 420 | } |
| 361 | 421 | |
| 362 | 422 | public function cron_delete( $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 | - } | |
| 423 | + // This feature is implemented in the premium version | |
| 424 | + return $this->unauthorized(); | |
| 369 | 425 | } |
| 370 | 426 | |
| 371 | 427 | } |