PluginProbe
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more / 3.7.2
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more v3.7.2
3.7.2 3.7.1 3.7.0 3.6.0 3.6.1 3.5.2 trunk 2.0 2.1 2.2 2.3 2.4 2.4.1 2.4.2 2.4.3 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.6.0 2.6.1 All 73 releases
← All changes | src/class-stockpackquery.php +81 -5 3.6.0 → 3.7.2 View file →
@@ -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 ] ) ) {
@@ -392,8 +396,14 @@
392 396 );
393 397 }
394 398
395 399 public function images( $query = [] ) {
400 + if ( empty( $query['provider'] ) ) {
401 + $available = $this->settings->get_available_search_provider_slugs();
402 + if ( $available ) {
403 + $query['available_providers'] = implode( ',', $available );
404 + }
405 + }
396 406
397 407 return $this->call( 'GET', 'images/search', $query );
398 408 }
399 409
@@ -424,8 +434,34 @@
424 434 if ( is_array( $cached ) ) {
425 435 return $cached;
426 436 }
427 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() {
428 464 $response = $this->call( 'GET', 'generate/prices' );
429 465 $prices = array();
430 466
431 467 if ( isset( $response->data ) ) {
@@ -433,11 +469,38 @@
433 469 }
434 470
435 471 set_transient( 'stockpack_ai_prices', $prices, $prices ? DAY_IN_SECONDS : 5 * MINUTE_IN_SECONDS );
436 472
473 + if ( $prices ) {
474 + set_transient( 'stockpack_ai_wallet', $this->wallet_from( $prices ), MINUTE_IN_SECONDS );
475 + }
476 +
437 477 return $prices;
438 478 }
439 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 +
440 503 public function image( $media_id, $post_id, $description = '', $search = '', $must_license = 0, $provider = 0, $new_filename = '' ) {
441 504
442 505 do_action( 'stockpack_before_attachment_download', $provider,$new_filename, $media_id, $must_license );
443 506
@@ -468,9 +531,9 @@
468 531 if ( is_wp_error( $attachment_id ) ) {
469 532 return $attachment_id;
470 533 }
471 534 if ( ! $attachment_id ) {
472 - 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' ) );
473 536 }
474 537
475 538 update_post_meta( $attachment_id, 'stockpack_id', $media_id );
476 539
@@ -606,12 +669,25 @@
606 669 return strpos( $path, 'images/download' ) === 0 ? 30 : 10;
607 670 }
608 671
609 672 private function error_payload( $error ) {
610 - return array(
673 + $payload = array(
611 674 'code' => $error->get_error_code(),
612 675 'message' => $error->get_error_message(),
613 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;
614 690 }
615 691
616 692 private function server_error( $response, $response_code ) {
617 693 $body = json_decode( wp_remote_retrieve_body( $response ) );
@@ -616,9 +692,9 @@
616 692 private function server_error( $response, $response_code ) {
617 693 $body = json_decode( wp_remote_retrieve_body( $response ) );
618 694
619 695 if ( ! empty( $body->message ) ) {
620 - 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 );
621 697 }
622 698
623 699 return new WP_Error( 1, sprintf(
624 700 /* translators: %d: HTTP status code returned by the StockPack API */