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 +429 -130 2.5.7 → 3.7.2 View file →
@@ -1,12 +1,7 @@
1 1 <?php
2 2
3 3
4 -use GuzzleHttp\Client;
5 -use GuzzleHttp\Cookie\CookieJar;
6 -use GuzzleHttp\Exception\GuzzleException;
7 -use GuzzleHttp\TransferStats;
8 -
9 4 if ( ! defined( 'ABSPATH' ) ) {
10 5 exit;
11 6 }
12 7 if ( ! class_exists( 'StockpackQuery' ) ) {
@@ -24,20 +19,12 @@
24 19 * Api version
25 20 */
26 21 const VERSION = 'v1';
27 22
28 - /**
29 - * @var string
30 - */
31 - private $api_key;
23 + public $admin;
32 24
33 - /**
34 - * @var Client
35 - */
36 - private $client;
25 + public $settings;
37 26
38 - public $admin;
39 -
40 27 public static function get_instance() {
41 28 if ( null === self::$instance ) {
42 29 self::$instance = new self();
43 30 }
@@ -62,8 +49,9 @@
62 49 *
63 50 */
64 51 public function init() {
65 52 $this->admin = \StockpackAdmin::get_instance();
53 + $this->settings = \StockpackSettings::get_instance();
66 54 $this->actions();
67 55 $this->filters();
68 56 }
69 57
@@ -70,9 +58,9 @@
70 58 /**
71 59 *
72 60 */
73 61 public function filters() {
74 - add_filter( 'stockpack_caption', array( $this, 'caption' ), 10, 2 );
62 +
75 63 }
76 64
77 65 /**
78 66 *
@@ -83,12 +71,21 @@
83 71 $this,
84 72 'license_cost'
85 73 ) ); // executed when logged in
86 74 add_action( 'wp_ajax_download-stockpack', array( $this, 'download' ) ); // executed when logged in
75 + add_action( 'wp_ajax_generate-stockpack', array( $this, 'generate' ) ); // executed when logged in
76 + add_action( 'wp_ajax_generate_status-stockpack', array(
77 + $this,
78 + 'generate_status'
79 + ) ); // executed when logged in
87 80 add_action( 'wp_ajax_cache-stockpack', array( $this, 'cache' ) ); // executed when logged in
88 81 add_action( 'wp_ajax_validate-stockpack', array( $this, 'validate' ) ); // executed when logged in
89 82 add_action( 'wp_ajax_terms-stockpack', array( $this, 'terms' ) ); // executed when logged in
90 83 add_action( 'wp_ajax_token-stockpack', array( $this, 'token' ) ); // executed when logged in
84 +
85 + if($this->settings->get_url_debugging_setting() ==='yes') {
86 + add_action('http_api_debug', array($this, 'debug_wp_remote_post_and_get_request'), 10, 5);
87 + }
91 88 }
92 89
93 90 public function terms() {
94 91 check_ajax_referer( 'stockpack_terms', 'security' );
@@ -132,13 +129,38 @@
132 129 if ( $response->data->cost == 1 && ! $response->data->cost_message ) {
133 130 $response->data->cost_message = __( 'You will be charged for one image on you provider account', 'stockpack' );
134 131 }
135 132
133 + if ( $this->already_in_library( $media_id ) ) {
134 + $response->data->cost_message = __( 'This image is already in your library. Adding it again reuses the file you downloaded before, so it will not use another download.', 'stockpack' );
135 + }
136 +
136 137 wp_send_json_success( $response->data, true );
137 138 }
138 139
140 + private function already_in_library( $media_id ) {
141 + if ( ! $media_id ) {
142 + return false;
143 + }
144 +
145 + $query = new WP_Query( array(
146 + 'post_type' => 'attachment',
147 + 'post_status' => 'inherit',
148 + 'fields' => 'ids',
149 + 'posts_per_page' => 1,
150 + 'meta_query' => array(
151 + array(
152 + 'key' => 'stockpack_id',
153 + 'value' => $media_id
154 + )
155 + )
156 + ) );
157 +
158 + return $query->have_posts();
159 + }
160 +
139 161 public function token() {
140 - global $stockpack;
162 + $stockpack = StockPack::get_instance();
141 163 check_ajax_referer( 'stockpack_token', 'security' );
142 164 $token = '';
143 165 if ( isset( $_REQUEST['token'] ) ) {
144 166 $token = sanitize_text_field( $_REQUEST['token'] );
@@ -170,9 +192,9 @@
170 192 if ( isset( $_REQUEST['query'] ) ) {
171 193 $query = array_map( 'sanitize_text_field', wp_unslash( $_REQUEST['query'] ) );
172 194 }
173 195
174 - if ( ! $query['query'] ) {
196 + if ( ! isset( $query['query'] ) ) {
175 197 return wp_send_json_success( array() );
176 198 }
177 199
178 200 $images = $this->images( $query );
@@ -189,8 +211,99 @@
189 211
190 212 wp_send_json_success( $this->format( $images->data ) );
191 213 }
192 214
215 + public function generate() {
216 + check_ajax_referer( 'stockpack_generate', 'security' );
217 + if ( ! current_user_can( 'upload_files' ) ) {
218 + wp_send_json_error( array( 'message' => __( 'You are not allowed to upload files', 'stockpack' ) ) );
219 + }
220 +
221 + /** @var StockPack $stockpack */
222 + $stockpack = StockPack::get_instance();
223 + if ( ! $stockpack->settings->get_api_key() ) {
224 + wp_send_json_error( array(
225 + 'message' => __( 'Add your StockPack API key in the plugin settings before generating images.',
226 + 'stockpack' )
227 + ) );
228 + }
229 +
230 + $args = array(
231 + 'action' => $this->request_value( 'generate_action', 'generate' ),
232 + );
233 +
234 + if ( 'upscale' === $args['action'] ) {
235 + $args['image_url'] = esc_url_raw( $this->request_value( 'image_url' ) );
236 + $args['scale_factor'] = (int) $this->request_value( 'scale_factor', 2 );
237 +
238 + if ( ! $args['image_url'] ) {
239 + wp_send_json_error( array( 'message' => __( 'Pick an image to upscale.', 'stockpack' ) ) );
240 + }
241 + } else {
242 + $args['prompt'] = $this->request_value( 'prompt' );
243 + $args['model'] = $this->request_value( 'model', 'mystic-25' );
244 + $args['resolution'] = $this->request_value( 'resolution', '2k' );
245 +
246 + $refine = esc_url_raw( $this->request_value( 'refine_url' ) );
247 +
248 + if ( $refine ) {
249 + $args['refine_url'] = $refine;
250 + }
251 +
252 + if ( ! $args['prompt'] ) {
253 + wp_send_json_error( array( 'message' => __( 'Describe the image you want first.', 'stockpack' ) ) );
254 + }
255 + }
256 +
257 + $response = $this->call( 'POST', 'generate', $args );
258 +
259 + if ( is_wp_error( $response ) ) {
260 + wp_send_json_error( $this->error_payload( $response ) );
261 + }
262 +
263 + if ( isset( $response->error ) ) {
264 + wp_send_json_error( $this->handle_search_errors( $response ) );
265 + }
266 +
267 + wp_send_json_success( $this->with_wallet( $response->data ) );
268 + }
269 +
270 + public function generate_status() {
271 + check_ajax_referer( 'stockpack_generate', 'security' );
272 + if ( ! current_user_can( 'upload_files' ) ) {
273 + wp_send_json_error( array( 'message' => __( 'You are not allowed to upload files', 'stockpack' ) ) );
274 + }
275 +
276 + $task = $this->request_value( 'task' );
277 + if ( ! $task ) {
278 + wp_send_json_error( array( 'message' => __( 'Unknown generation task.', 'stockpack' ) ) );
279 + }
280 +
281 + $response = $this->call( 'GET', 'generate/' . rawurlencode( $task ) );
282 +
283 + if ( is_wp_error( $response ) ) {
284 + wp_send_json_error( $this->error_payload( $response ) );
285 + }
286 +
287 + if ( isset( $response->error ) ) {
288 + wp_send_json_error( $this->handle_search_errors( $response ) );
289 + }
290 +
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 ) );
296 + }
297 +
298 + private function request_value( $key, $default = '' ) {
299 + if ( ! isset( $_REQUEST[ $key ] ) ) {
300 + return $default;
301 + }
302 +
303 + return sanitize_text_field( wp_unslash( $_REQUEST[ $key ] ) );
304 + }
305 +
193 306 /**
194 307 *
195 308 */
196 309 public function cache() {
@@ -247,14 +360,17 @@
247 360 $must_license = false;
248 361 if ( isset( $_REQUEST['must_license'] ) ) {
249 362 $must_license = sanitize_text_field( $_REQUEST['must_license'] );
250 363 }
251 - $new_filename="";
364 + $new_filename = "";
252 365 if ( isset( $_REQUEST['new_filename'] ) ) {
253 366 $new_filename = sanitize_text_field( $_REQUEST['new_filename'] );
254 367 }
255 - $search = isset( $_REQUEST['search'] ) ? sanitize_title( $_REQUEST['search'] ) : $this->get_domain();
368 + $search = isset( $_REQUEST['search_key'] ) ? sanitize_title( $_REQUEST['search_key'] ) : $this->get_domain();
256 369 $description = isset( $_REQUEST['description'] ) ? sanitize_textarea_field( $_REQUEST['description'] ) : '';
370 +
371 +
372 +
257 373 $image = $this->image( $id, $post_id, $description, $search, $must_license, $provider, $new_filename );
258 374
259 375 if ( is_wp_error( $image ) ) {
260 376 if ( strpos( $image->get_error_code(), 'limit_reached' ) !== false ) {
@@ -267,28 +383,32 @@
267 383 wp_send_json_success( wp_prepare_attachment_for_js( $image ) );
268 384
269 385 }
270 386
271 - public function caption( $caption, $image ) {
272 - if ( $this->is_provider_premium( $image->data->provider ) ) {
273 - if ( $this->admin->get_premium_providers_caption_setting() === 'yes' ) {
274 - return $caption;
275 - }
276 387
277 - return '';
278 - }
279 388
280 - return $caption;
281 - }
282 -
283 389 public function is_provider_premium( $provider ) {
284 390 return in_array( $provider, array(
285 391 'adobe_stock',
392 + 'getty',
393 + 'istock',
286 394 'deposit_photos'
287 395 )
288 396 );
289 397 }
290 398
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 + }
406 +
407 + return $this->call( 'GET', 'images/search', $query );
408 + }
409 +
410 +
291 411 /**
292 412 * @param $data
293 413 *
294 414 * @return array
@@ -296,56 +416,104 @@
296 416 private function format( $data ) {
297 417 return $data;
298 418 }
299 419
300 - /**
301 - * @param $image
302 - *
303 - * @return array
304 - */
305 - private function individual( $image ) {
306 - return [
307 - 'id' => $image->id,
308 - 'description' => $image->description,
309 - 'caption' => $image->caption ? $image->caption : '',
310 - 'url' => $image->assets->huge_thumb->url,
311 - 'sizes' => [
312 - 'full' => $this->size( $image, 'huge_thumb' ),
313 - 'medium' => $this->size( $image, 'preview' ),
314 - 'thumbnail' => $this->size( $image, 'large_thumb' )
315 - ]
316 - ];
317 - }
318 420
319 - private function size( $image, $format ) {
320 - return [
321 - 'height' => $image->assets->$format->height,
322 - 'width' => $image->assets->$format->width,
323 - 'orientation' => ( $image->aspect > 1 ? 'landscape' : 'portrait' ),
324 - 'url' => $image->assets->$format->url
325 - ];
326 - }
421 + public function get_license_cost( $media_id, $provider ) {
327 422
328 - private function get_license_cost( $media_id, $provider ) {
329 -
330 - $response = $this->call( 'GET', 'images/license-cost', array(
423 + return $this->call( 'GET', 'images/license-cost', array(
331 424 'id' => $media_id,
332 425 'locale' => get_locale(),
333 426 'provider' => $provider
334 427 ) );
335 428
336 - return $response;
429 + }
337 430
431 + public function get_ai_prices() {
432 + $cached = get_transient( 'stockpack_ai_prices' );
338 433
434 + if ( is_array( $cached ) ) {
435 + return $cached;
436 + }
437 +
438 + return $this->fetch_ai_prices();
339 439 }
340 440
341 - private function image( $media_id, $post_id, $description = '', $search = '', $must_license = 0, $provider = 0, $new_filename = '' ) {
441 + public function get_ai_wallet( $fresh = false ) {
442 + $cached = get_transient( 'stockpack_ai_wallet' );
342 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() {
464 + $response = $this->call( 'GET', 'generate/prices' );
465 + $prices = array();
466 +
467 + if ( isset( $response->data ) ) {
468 + $prices = json_decode( wp_json_encode( $response->data ), true );
469 + }
470 +
471 + set_transient( 'stockpack_ai_prices', $prices, $prices ? DAY_IN_SECONDS : 5 * MINUTE_IN_SECONDS );
472 +
473 + if ( $prices ) {
474 + set_transient( 'stockpack_ai_wallet', $this->wallet_from( $prices ), MINUTE_IN_SECONDS );
475 + }
476 +
477 + return $prices;
478 + }
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 +
503 + public function image( $media_id, $post_id, $description = '', $search = '', $must_license = 0, $provider = 0, $new_filename = '' ) {
504 +
505 + do_action( 'stockpack_before_attachment_download', $provider,$new_filename, $media_id, $must_license );
506 +
507 + $big_image_threshold = (int) apply_filters( 'big_image_size_threshold', 2560, array( 0, 0 ), '', 0 );
508 +
343 509 $image = $this->call( 'GET', 'images/download',
344 510 array(
345 511 'id' => $media_id,
346 512 'must_license' => $must_license,
347 - 'provider' => $provider
513 + 'provider' => $provider,
514 + 'min_width'=> $big_image_threshold,
515 + 'min_height'=> $big_image_threshold,
348 516 )
349 517 );
350 518
351 519 if ( ! isset( $image->data->download ) ) {
@@ -355,21 +523,17 @@
355 523
356 524 return $this->handle_download_errors( $image->data );
357 525 }
358 526
359 - $filename = $image->data->name;
527 + $filename = $this->getFilename($image, $new_filename, $must_license);
360 528
361 - if ( $new_filename ) {
362 - $filename = $new_filename;
363 - }
364 -
365 529 $caption = $image->data->caption ?: '';
366 - $attachment_id = $this->upload_remote_image_and_attach( $image->data->download, $post_id, $description, $filename, $search, $caption, $image );
530 + $attachment_id = $this->upload_remote_image_and_attach( $image->data->download, $post_id, $description, $filename,$image, $search, $caption );
367 531 if ( is_wp_error( $attachment_id ) ) {
368 532 return $attachment_id;
369 533 }
370 534 if ( ! $attachment_id ) {
371 - 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' ) );
372 536 }
373 537
374 538 update_post_meta( $attachment_id, 'stockpack_id', $media_id );
375 539
@@ -381,8 +545,12 @@
381 545 switch ( $response->code ) {
382 546 case 'token_expired':
383 547 return new WP_Error( 'license_cost_failure', __( 'Please reconnect the account on StockPack, the permissions have expired. Go the providers page on stockpack.co/providers', 'stockpack' ) );
384 548 }
549 +
550 + if ( ! empty( $response->message ) ) {
551 + return new WP_Error( $response->code, $response->message );
552 + }
385 553 }
386 554
387 555 return new WP_Error( 'license_cost_failure', __( 'There has been a problem fetching the cost. Please try to reconnect the account on the providers page on stockpack.co/providers', 'stockpack' ) );
388 556 }
@@ -390,10 +558,14 @@
390 558 private function handle_download_errors( $response ) {
391 559 if ( isset( $response->code ) ) {
392 560 switch ( $response->code ) {
393 561 case 'token_expired':
394 - return new WP_Error( 'download_failure', __( 'Please reconnect the account on StockPack, the permissions have expired. Go the providers page on stockpack.co/providers', 'stockpack' ) );
562 + return new WP_Error( 'token_expired', __( 'Please reconnect the account on StockPack, the permissions have expired. Go the providers page on stockpack.co/providers', 'stockpack' ) );
395 563 }
564 +
565 + if ( ! empty( $response->message ) ) {
566 + return new WP_Error( $response->code, $response->message );
567 + }
396 568 }
397 569
398 570 return new WP_Error( 'download_failure', __( 'There has been a problem with the download. Try to reconnect the account for the provider on StockPack. If the issue persists, contact stockpack support.', 'stockpack' ) );
399 571 }
@@ -401,9 +573,9 @@
401 573 private function handle_search_errors( $response ) {
402 574 if ( isset( $response->code ) ) {
403 575 switch ( $response->code ) {
404 576 case 'token_expired':
405 - return new WP_Error( 'search_failure', __( 'Please reconnect the account on stockpack, the permissions have expired. Go to the providers page on stockpack.co/providers', 'stockpack' ) );
577 + return new WP_Error( 'token_expired', __( 'Please reconnect the account on stockpack, the permissions have expired. Go to the providers page on stockpack.co/providers', 'stockpack' ) );
406 578 }
407 579 }
408 580
409 581 return new WP_Error( 'search_failure', __( 'There has been a problem with the api, please check the accounts on the providers page in stockpack.co/providers', 'stockpack' ) );
@@ -409,13 +581,9 @@
409 581 return new WP_Error( 'search_failure', __( 'There has been a problem with the api, please check the accounts on the providers page in stockpack.co/providers', 'stockpack' ) );
410 582
411 583 }
412 584
413 - private function images( $query = [] ) {
414 585
415 - return $this->call( 'GET', 'images/search', $query );
416 - }
417 -
418 586 /**
419 587 * @param $method
420 588 * @param $path
421 589 * @param array $query
@@ -423,57 +591,126 @@
423 591 * @return mixed|null
424 592 */
425 593 private function call( $method, $path, $query = [] ) {
426 594 /** @var StockPack $stockpack */
427 - global $stockpack;
595 + $stockpack = StockPack::get_instance();
428 596
429 - try {
430 - if ( ! isset( $query['key'] ) ) {
431 - $api_key = $stockpack->admin->get_api_key();
432 - if ( $api_key ) {
433 - $query = array_merge( $query,
434 - [
435 - 'api_token' => $api_key
436 - ]
437 - );
438 - } else {
439 - $path .= '-trial';
440 - }
597 +
598 + if ( ! isset( $query['key'] ) ) {
599 + $api_key = $stockpack->settings->get_api_key();
600 + if ( $api_key ) {
601 + $query = array_merge( $query,
602 + [
603 + 'api_token' => $api_key
604 + ]
605 + );
606 + } else {
607 + $path .= '-trial';
441 608 }
609 + }
442 610
443 - $query = array_merge( $query,
444 - [
445 - 'referral' => get_bloginfo( 'url' ),
446 - 'safe_search' => $stockpack->admin->get_safe_search(),
447 - 'user_data' => $stockpack->admin->get_license_state(),
448 - ]
449 - );
611 + $query = array_merge( $query,
612 + [
613 + 'referral' => get_bloginfo( 'url' ),
614 + 'safe_search' => $stockpack->settings->get_safe_search(),
615 + 'user_data' => $stockpack->settings->get_license_state(),
616 + ]
617 + );
450 618
451 - $response = $this->client()->request( $method, $path,
452 - [
453 - 'query' => $query
454 - ]
455 - );
619 + $url = $this->url . '/' . self::VERSION . '/' . $path;
620 + $is_post = 'post' === strtolower( $method );
456 621
457 - return json_decode( $response->getBody() );
458 - } catch ( GuzzleException $exception ) {
459 - switch ( $exception->getCode() ) {
622 + if ( ! $is_post ) {
623 + $url .= '?' . http_build_query( $query );
624 + }
625 +
626 + do_action( 'stockpack_before_api_request', $url, $query );
627 +
628 + $headers = array( 'Accept' => 'application/json' );
629 +
630 + if ( $is_post ) {
631 + $response = wp_remote_post( $url, array(
632 + 'timeout' => 20,
633 + 'sslverify' => false,
634 + 'headers' => $headers,
635 + 'body' => $query,
636 + ) );
637 + } else {
638 + $response = wp_remote_get( $url, array(
639 + 'timeout' => apply_filters( 'stockpack_api_timeout', $this->timeout_for( $path ), $path ),
640 + 'sslverify' => false,
641 + 'headers' => $headers,
642 + ) );
643 + }
644 +
645 + do_action( 'stockpack_after_api_request', $url, $query, $response );
646 +
647 + if ( is_wp_error( $response ) ) {
648 + return $response;
649 + }
650 +
651 + $response_code = wp_remote_retrieve_response_code( $response );
652 + if ( $response_code !== 200 ) {
653 + switch ( $response_code ) {
460 654 case 429: // too many requests
461 - return $this->limit_exceeded( $exception );
462 -
655 + return $this->limit_exceeded( $response );
656 + case 401: // access denied
657 + return $this->invalid_token( $response );
463 658 default:
464 - return new WP_Error( 1, __( 'Can\'t connect to StockPack server.', 'stockpack' ) );
659 + return $this->server_error( $response, $response_code );
465 660 }
466 661 }
662 +
663 + $response_body = wp_remote_retrieve_body( $response );
664 +
665 + return json_decode( $response_body );
467 666 }
468 667
469 - private function limit_exceeded( $exception ) {
668 + private function timeout_for( $path ) {
669 + return strpos( $path, 'images/download' ) === 0 ? 30 : 10;
670 + }
671 +
672 + private function error_payload( $error ) {
673 + $payload = array(
674 + 'code' => $error->get_error_code(),
675 + 'message' => $error->get_error_message(),
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;
690 + }
691 +
692 + private function server_error( $response, $response_code ) {
693 + $body = json_decode( wp_remote_retrieve_body( $response ) );
694 +
695 + if ( ! empty( $body->message ) ) {
696 + return new WP_Error( ! empty( $body->code ) ? $body->code : 1, $body->message, $body );
697 + }
698 +
699 + return new WP_Error( 1, sprintf(
700 + /* translators: %d: HTTP status code returned by the StockPack API */
701 + __( 'The StockPack server returned an error (HTTP %d). If this keeps happening, contact StockPack support.', 'stockpack' ),
702 + $response_code
703 + ) );
704 + }
705 +
706 + private function limit_exceeded( $response ) {
470 707 /** @var StockPack $stockpack */
471 - global $stockpack;
708 + $stockpack = StockPack::get_instance();
472 709
473 - $response = $exception->getResponse();
474 - $limit = $response->getHeaderLine( 'x-ratelimit-limit' );
475 - $retry_after = $response->getHeaderLine( 'retry-after' );
710 +
711 + $limit = wp_remote_retrieve_header( $response, 'x-ratelimit-limit' );
712 + $retry_after = wp_remote_retrieve_header( $response, 'retry-after' );
476 713 $retry_after = $this->human_seconds( $retry_after );
477 714 $retry_after = '<span class ="stockpack-timer">' . $retry_after . '</span>';
478 715 if ( $limit > 50 ) {
479 716 return new WP_Error( 'premium_limit_reached', __( 'Request limit reached, the reset will be in ', 'stockpack' ) . $retry_after );
@@ -486,22 +723,10 @@
486 723
487 724 return new WP_Error( 'anonymous_limit_reached', __( 'Request limit reached, the reset will be in ', 'stockpack' ) . $retry_after );
488 725 }
489 726
490 - /**
491 - * @return Client
492 - */
493 - private function client() {
494 -
495 - if ( ! $this->client ) {
496 - $this->client = new Client(
497 - [
498 - 'base_uri' => $this->url . '/' . self::VERSION . '/',
499 - ]
500 - );
501 - }
502 -
503 - return $this->client;
727 + private function invalid_token( $response ) {
728 + return new WP_Error( 'invalid_token', __( 'Request denied. Please check your token!', 'stockpack' ) );
504 729 }
505 730
506 731 private function support_caption_translation( $caption ) {
507 732 if ( ! $caption ) {
@@ -526,23 +751,31 @@
526 751 * @param string $caption
527 752 *
528 753 * @return bool|int|WP_Error
529 754 */
530 - private function upload_remote_image_and_attach( $image_url, $parent_id, $description, $name, $search = '', $caption = '', $image ) {
755 + private function upload_remote_image_and_attach( $image_url, $parent_id, $description, $name, $image, $search = '', $caption = '' ) {
531 756
532 - $get = wp_remote_get( $image_url, array( 'timeout' => 30 ) );
757 + $get = wp_remote_get( $image_url, array(
758 + 'timeout' => apply_filters( 'stockpack_download_timeout', 30 )
759 + )
760 + );
533 761 if ( is_wp_error( $get ) ) {
534 762 return new WP_Error( 100, __( 'Image download failed, please try again. Errors:', 'stockpack' ) . PHP_EOL . $get->get_error_message() );
535 763 }
536 764 $type = wp_remote_retrieve_header( $get, 'content-type' );
537 765 if ( ! $type ) {
766 + $type = wp_check_filetype( $name )['type'];
767 + }
768 + if ( ! $type ) {
538 769 return new WP_Error( 100, __( 'Image type couldn\'t be determined', 'stockpack' ) );
539 770 }
540 771 $name = $this->update_extension( $name, $type );
541 772
773 + $name = apply_filters( 'stockpack_attachment_filename', $name, $image );
774 +
542 775 $mirror = wp_upload_bits( $name, '', wp_remote_retrieve_body( $get ) );
543 776 $attachment = array(
544 - 'post_title' => $name,
777 + 'post_title' => $this->strip_extension($name),
545 778 'post_content' => $description,
546 779 'post_excerpt' => apply_filters( 'stockpack_caption', $this->support_caption_translation( $caption ), $image ),
547 780 'post_mime_type' => $type
548 781 );
@@ -547,16 +780,45 @@
547 780 'post_mime_type' => $type
548 781 );
549 782 $attach_id = wp_insert_attachment( $attachment, $mirror['file'], $parent_id );
550 783 require_once( ABSPATH . 'wp-admin/includes/image.php' );
784 +
785 + // An upscale that WordPress immediately scales back down is an
786 + // upscale the user paid for and did not get, so the threshold is
787 + // lifted for the result of one. Priority 999 so it wins over a theme
788 + // or plugin that lowers it.
789 + $is_upscale = isset( $image->data->meta->action ) && 'upscale' === $image->data->meta->action;
790 +
791 + if ( $is_upscale ) {
792 + add_filter( 'big_image_size_threshold', '__return_false', 999 );
793 + }
794 +
551 795 $attach_data = wp_generate_attachment_metadata( $attach_id, $mirror['file'] );
552 796 wp_update_attachment_metadata( $attach_id, $attach_data );
797 +
798 + if ( $is_upscale ) {
799 + remove_filter( 'big_image_size_threshold', '__return_false', 999 );
800 + }
553 801 update_post_meta( $attach_id, '_wp_attachment_image_alt', $description );
802 + $this->store_attachment_meta($attach_id,$image);
554 803
804 + do_action( 'stockpack_after_attachment_uploaded', $attach_id, $image );
805 +
555 806 return $attach_id;
556 807
557 808 }
558 809
810 + private function store_attachment_meta($attach_id, $image){
811 + $data = $image->data;
812 + $meta = isset($data->meta) ? $data->meta : new stdClass();
813 + update_post_meta($attach_id,'stockpack_media_id',$data->id);
814 + update_post_meta($attach_id,'stockpack_provider',isset($meta->provider) ? $meta->provider : '');
815 + update_post_meta($attach_id,'stockpack_author_url',isset($meta->author_url) ? $meta->author_url : '');
816 + update_post_meta($attach_id,'stockpack_author_name',isset($meta->author_name) ? $meta->author_name : '');
817 + update_post_meta($attach_id,'stockpack_image_url',isset($meta->image_url) ? $meta->image_url : '');
818 + update_post_meta($attach_id,'stockpack_download_timestamp',time());
819 + }
820 +
559 821 /**
560 822 * @return mixed
561 823 */
562 824 private function get_domain() {
@@ -581,8 +843,43 @@
581 843 return $name[0] . '.' . $name[1];
582 844
583 845 }
584 846
847 + private function strip_extension( $name ) {
848 + $name = explode( '.', $name );
849 + return $name[0];
850 + }
851 +
852 + private function getFilename($image, $new_filename, $must_license)
853 + {
854 + $stockpack = StockPack::get_instance();
855 +
856 + $filename = $image->data->name;
857 +
858 + if($new_filename) {
859 + $filename = $new_filename;
860 + }
861 +
862 + if(!$this->is_provider_premium($image->data->provider) || $must_license) {
863 + return $filename;
864 + }
865 +
866 + if($stockpack->settings->add_license_state_to_filename()==='yes') {
867 + $filename = '--sto--placeholder--' . $image->data->provider.'--'.$image->data->id.'--'.$filename;
868 + }
869 +
870 + return $filename;
871 + }
872 +
873 + public function debug_wp_remote_post_and_get_request( $response, $context, $class, $r, $url ) {
874 + error_log( '------------------------------' );
875 + error_log( $url );
876 + error_log( json_encode( $response ) );
877 + error_log( $class );
878 + error_log( $context );
879 + error_log( json_encode( $r ) );
880 + }
881 +
585 882 private function mime2ext( $mime ) {
586 883 $mime = str_replace( ';charset=UTF-8', '', $mime );
587 884 $mime_map = [
588 885 'video/3gpp2' => '3g2',
@@ -657,10 +954,10 @@
657 954 'image/jp2' => 'jp2',
658 955 'video/mj2' => 'jp2',
659 956 'image/jpx' => 'jp2',
660 957 'image/jpm' => 'jp2',
661 - 'image/jpeg' => 'jpeg',
662 - 'image/pjpeg' => 'jpeg',
958 + 'image/jpeg' => 'jpg',
959 + 'image/pjpeg' => 'jpg',
663 960 'application/x-javascript' => 'js',
664 961 'application/json' => 'json',
665 962 'text/json' => 'json',
666 963 'application/vnd.google-earth.kml+xml' => 'kml',
@@ -771,8 +1068,10 @@
771 1068 ];
772 1069
773 1070 return isset( $mime_map[ $mime ] ) === true ? $mime_map[ $mime ] : false;
774 1071 }
1072 +
1073 +
775 1074
776 1075 }
777 1076
778 1077 $GLOBALS['stockpack_query'] = StockpackQuery::get_instance();