| @@ -263,9 +263,9 @@ | ||
| 263 | 263 | if ( isset( $response->error ) ) { |
| 264 | 264 | wp_send_json_error( $this->handle_search_errors( $response ) ); |
| 265 | 265 | } |
| 266 | 266 | |
| 267 | - wp_send_json_success( $response->data ); | |
| 267 | + wp_send_json_success( $this->with_wallet( $response->data ) ); | |
| 268 | 268 | } |
| 269 | 269 | |
| 270 | 270 | public function generate_status() { |
| 271 | 271 | check_ajax_referer( 'stockpack_generate', 'security' ); |
| @@ -287,9 +287,13 @@ | ||
| 287 | 287 | if ( isset( $response->error ) ) { |
| 288 | 288 | wp_send_json_error( $this->handle_search_errors( $response ) ); |
| 289 | 289 | } |
| 290 | 290 | |
| 291 | - wp_send_json_success( $response->data ); | |
| 291 | + if ( isset( $response->data->status ) && 'pending' === $response->data->status ) { | |
| 292 | + wp_send_json_success( $response->data ); | |
| 293 | + } | |
| 294 | + | |
| 295 | + wp_send_json_success( $this->with_wallet( $response->data ) ); | |
| 292 | 296 | } |
| 293 | 297 | |
| 294 | 298 | private function request_value( $key, $default = '' ) { |
| 295 | 299 | if ( ! isset( $_REQUEST[ $key ] ) ) { |
| @@ -430,8 +434,34 @@ | ||
| 430 | 434 | if ( is_array( $cached ) ) { |
| 431 | 435 | return $cached; |
| 432 | 436 | } |
| 433 | 437 | |
| 438 | + return $this->fetch_ai_prices(); | |
| 439 | + } | |
| 440 | + | |
| 441 | + public function get_ai_wallet( $fresh = false ) { | |
| 442 | + $cached = get_transient( 'stockpack_ai_wallet' ); | |
| 443 | + | |
| 444 | + if ( is_array( $cached ) && ! $fresh ) { | |
| 445 | + return $cached; | |
| 446 | + } | |
| 447 | + | |
| 448 | + $prices = $this->fetch_ai_prices(); | |
| 449 | + | |
| 450 | + if ( ! $prices && is_array( $cached ) ) { | |
| 451 | + return $cached; | |
| 452 | + } | |
| 453 | + | |
| 454 | + return $this->wallet_from( $prices ); | |
| 455 | + } | |
| 456 | + | |
| 457 | + public function dashboard_url( $path = '' ) { | |
| 458 | + $base = defined( 'STOCKPACK_DASHBOARD_URL' ) ? STOCKPACK_DASHBOARD_URL : 'https://stockpack.co'; | |
| 459 | + | |
| 460 | + return rtrim( $base, '/' ) . ( $path ? '/' . ltrim( $path, '/' ) : '' ); | |
| 461 | + } | |
| 462 | + | |
| 463 | + private function fetch_ai_prices() { | |
| 434 | 464 | $response = $this->call( 'GET', 'generate/prices' ); |
| 435 | 465 | $prices = array(); |
| 436 | 466 | |
| 437 | 467 | if ( isset( $response->data ) ) { |
| @@ -439,11 +469,38 @@ | ||
| 439 | 469 | } |
| 440 | 470 | |
| 441 | 471 | set_transient( 'stockpack_ai_prices', $prices, $prices ? DAY_IN_SECONDS : 5 * MINUTE_IN_SECONDS ); |
| 442 | 472 | |
| 473 | + if ( $prices ) { | |
| 474 | + set_transient( 'stockpack_ai_wallet', $this->wallet_from( $prices ), MINUTE_IN_SECONDS ); | |
| 475 | + } | |
| 476 | + | |
| 443 | 477 | return $prices; |
| 444 | 478 | } |
| 445 | 479 | |
| 480 | + private function wallet_from( $prices ) { | |
| 481 | + $wallet = isset( $prices['wallet'] ) && is_array( $prices['wallet'] ) ? $prices['wallet'] : array(); | |
| 482 | + $balance = isset( $wallet['balance'] ) ? (int) $wallet['balance'] : null; | |
| 483 | + | |
| 484 | + return array( | |
| 485 | + 'mode' => isset( $wallet['mode'] ) ? $wallet['mode'] : 'byok', | |
| 486 | + 'balance' => $balance, | |
| 487 | + 'balance_text' => null === $balance ? '' : number_format_i18n( $balance ), | |
| 488 | + 'top_up_url' => ! empty( $wallet['top_up_url'] ) ? $wallet['top_up_url'] : $this->dashboard_url( 'billing/credits' ), | |
| 489 | + 'connect_url' => $this->dashboard_url( 'providers' ), | |
| 490 | + ); | |
| 491 | + } | |
| 492 | + | |
| 493 | + private function with_wallet( $data ) { | |
| 494 | + $wallet = $this->get_ai_wallet(); | |
| 495 | + | |
| 496 | + if ( 'managed' === $wallet['mode'] ) { | |
| 497 | + $data->wallet = $this->get_ai_wallet( true ); | |
| 498 | + } | |
| 499 | + | |
| 500 | + return $data; | |
| 501 | + } | |
| 502 | + | |
| 446 | 503 | public function image( $media_id, $post_id, $description = '', $search = '', $must_license = 0, $provider = 0, $new_filename = '' ) { |
| 447 | 504 | |
| 448 | 505 | do_action( 'stockpack_before_attachment_download', $provider,$new_filename, $media_id, $must_license ); |
| 449 | 506 | |
| @@ -474,9 +531,9 @@ | ||
| 474 | 531 | if ( is_wp_error( $attachment_id ) ) { |
| 475 | 532 | return $attachment_id; |
| 476 | 533 | } |
| 477 | 534 | if ( ! $attachment_id ) { |
| 478 | - return new WP_Error( __( 'upload_failure', 'There has been a problem with the upload. Please try again', 'stockpack' ) ); | |
| 535 | + return new WP_Error( 'upload_failure', __( 'There has been a problem with the upload. Please try again', 'stockpack' ) ); | |
| 479 | 536 | } |
| 480 | 537 | |
| 481 | 538 | update_post_meta( $attachment_id, 'stockpack_id', $media_id ); |
| 482 | 539 | |
| @@ -612,12 +669,25 @@ | ||
| 612 | 669 | return strpos( $path, 'images/download' ) === 0 ? 30 : 10; |
| 613 | 670 | } |
| 614 | 671 | |
| 615 | 672 | private function error_payload( $error ) { |
| 616 | - return array( | |
| 673 | + $payload = array( | |
| 617 | 674 | 'code' => $error->get_error_code(), |
| 618 | 675 | 'message' => $error->get_error_message(), |
| 619 | 676 | ); |
| 677 | + $wallet = $this->get_ai_wallet(); | |
| 678 | + | |
| 679 | + if ( 'insufficient_credits' === $payload['code'] ) { | |
| 680 | + $data = $error->get_error_data(); | |
| 681 | + $payload['link'] = ! empty( $data->top_up_url ) ? $data->top_up_url : $wallet['top_up_url']; | |
| 682 | + $payload['link_text'] = __( 'Top up credits', 'stockpack' ); | |
| 683 | + } | |
| 684 | + | |
| 685 | + if ( 'managed' === $wallet['mode'] ) { | |
| 686 | + $payload['wallet'] = $this->get_ai_wallet( true ); | |
| 687 | + } | |
| 688 | + | |
| 689 | + return $payload; | |
| 620 | 690 | } |
| 621 | 691 | |
| 622 | 692 | private function server_error( $response, $response_code ) { |
| 623 | 693 | $body = json_decode( wp_remote_retrieve_body( $response ) ); |
| @@ -622,9 +692,9 @@ | ||
| 622 | 692 | private function server_error( $response, $response_code ) { |
| 623 | 693 | $body = json_decode( wp_remote_retrieve_body( $response ) ); |
| 624 | 694 | |
| 625 | 695 | if ( ! empty( $body->message ) ) { |
| 626 | - return new WP_Error( ! empty( $body->code ) ? $body->code : 1, $body->message ); | |
| 696 | + return new WP_Error( ! empty( $body->code ) ? $body->code : 1, $body->message, $body ); | |
| 627 | 697 | } |
| 628 | 698 | |
| 629 | 699 | return new WP_Error( 1, sprintf( |
| 630 | 700 | /* translators: %d: HTTP status code returned by the StockPack API */ |