PluginProbe
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more / trunk
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more vtrunk
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 3.0.0 3.0.1 3.0.2 All 70 releases
stockpack / src / class-stockpackquery.php

class-stockpackquery.php in StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more trunk, at src/class-stockpackquery.php

1,009 lines 48.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7 if ( ! class_exists( 'StockpackQuery' ) ) {
8 class StockpackQuery {
9 /**
10 * @var Singleton The reference the *Singleton* instance of this class
11 */
12 private static $instance;
13
14 /**
15 * Api url
16 */
17 private $url = 'https://api.stockpack.co/api';
18 /**
19 * Api version
20 */
21 const VERSION = 'v1';
22
23 public $admin;
24
25 public $settings;
26
27 public static function get_instance() {
28 if ( null === self::$instance ) {
29 self::$instance = new self();
30 }
31
32 return self::$instance;
33 }
34
35 /**
36 * StockPack constructor.
37 */
38 protected function __construct() {
39 if ( ! defined( 'STOCKPACK_URL' ) ) {
40 $this->url = 'https://api.stockpack.co/api';
41 } else {
42 $this->url = STOCKPACK_URL;
43 }
44
45 add_action( 'init', array( $this, 'init' ) );
46 }
47
48 /**
49 *
50 */
51 public function init() {
52 $this->admin = \StockpackAdmin::get_instance();
53 $this->settings = \StockpackSettings::get_instance();
54 $this->actions();
55 $this->filters();
56 }
57
58 /**
59 *
60 */
61 public function filters() {
62
63 }
64
65 /**
66 *
67 */
68 public function actions() {
69 add_action( 'wp_ajax_query-stockpack', array( $this, 'query' ) ); // executed when logged in
70 add_action( 'wp_ajax_license_cost-stockpack', array(
71 $this,
72 'license_cost'
73 ) ); // executed when logged in
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
80 add_action( 'wp_ajax_cache-stockpack', array( $this, 'cache' ) ); // executed when logged in
81 add_action( 'wp_ajax_validate-stockpack', array( $this, 'validate' ) ); // executed when logged in
82 add_action( 'wp_ajax_terms-stockpack', array( $this, 'terms' ) ); // executed when logged in
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 }
88 }
89
90 public function terms() {
91 check_ajax_referer( 'stockpack_terms', 'security' );
92
93 $provider = '';
94 if ( isset( $_REQUEST['provider'] ) ) {
95 $provider = sanitize_key( $_REQUEST['provider'] );
96 }
97
98 wp_send_json_success( update_option( 'terms_accepted_' . $provider, true ) );
99 }
100
101 public function license_cost() {
102 check_ajax_referer( 'stockpack_license_cost', 'security' );
103 $media_id = '';
104 if ( isset( $_REQUEST['media_id'] ) ) {
105 $media_id = sanitize_text_field( $_REQUEST['media_id'] );
106 }
107
108 $provider = 0;
109 if ( isset( $_REQUEST['provider'] ) ) {
110 $provider = sanitize_text_field( $_REQUEST['provider'] );
111 }
112
113 $response = $this->get_license_cost( $media_id, $provider );
114
115 if ( isset( $response->data->error ) ) {
116 wp_send_json_error( $this->handle_license_errors( $response->data ) );
117 die();
118 }
119
120 if ( is_wp_error( $response ) ) {
121 wp_send_json_error( $this->handle_license_errors( $response ) );
122 die();
123 }
124
125 if ( $response->data->cost == - 1 ) {
126 $response->data->cost_message = __( 'You already own the asset, you can proceed without additional cost', 'stockpack' );
127 }
128
129 if ( $response->data->cost == 1 && ! $response->data->cost_message ) {
130 $response->data->cost_message = __( 'You will be charged for one image on you provider account', 'stockpack' );
131 }
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
137 wp_send_json_success( $response->data, true );
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
161 public function token() {
162 $stockpack = StockPack::get_instance();
163 check_ajax_referer( 'stockpack_token', 'security' );
164 $token = '';
165 if ( isset( $_REQUEST['token'] ) ) {
166 $token = sanitize_text_field( $_REQUEST['token'] );
167 }
168 wp_send_json_success( $stockpack->admin->set_api_key( $token ), true );
169 }
170
171 /**
172 *
173 */
174 public function validate() {
175 check_ajax_referer( 'stockpack_validate', 'security' );
176 $key = '';
177 if ( isset( $_REQUEST['key'] ) ) {
178 $key = sanitize_key( $_REQUEST['key'] );
179 }
180 wp_send_json_success( $this->call( 'GET', 'validate/token', array( 'key' => $key ) ) );
181 }
182
183 /**
184 *
185 */
186 public function query() {
187 check_ajax_referer( 'stockpack_query', 'security' );
188 if ( ! current_user_can( 'upload_files' ) ) {
189 wp_send_json_error();
190 }
191 $query = array();
192 if ( isset( $_REQUEST['query'] ) ) {
193 $query = array_map( 'sanitize_text_field', wp_unslash( $_REQUEST['query'] ) );
194 }
195
196 if ( ! isset( $query['query'] ) ) {
197 return wp_send_json_success( array() );
198 }
199
200 $images = $this->images( $query );
201 if ( is_wp_error( $images ) ) {
202 /** @var WP_Error $images */
203 wp_send_json_error( $images );
204 die();
205 }
206
207 if ( isset( $images->data->error ) ) {
208 wp_send_json_error( $this->handle_search_errors( $images->data ) );
209 die();
210 }
211
212 wp_send_json_success( $this->format( $images->data ) );
213 }
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( $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 wp_send_json_success( $response->data );
292 }
293
294 private function request_value( $key, $default = '' ) {
295 if ( ! isset( $_REQUEST[ $key ] ) ) {
296 return $default;
297 }
298
299 return sanitize_text_field( wp_unslash( $_REQUEST[ $key ] ) );
300 }
301
302 /**
303 *
304 */
305 public function cache() {
306 check_ajax_referer( 'stockpack_cache', 'security' );
307
308 if ( ! current_user_can( 'upload_files' ) ) {
309 wp_send_json_error();
310 }
311 $id = 0;
312
313 if ( isset( $_REQUEST['media_id'] ) ) {
314 $id = sanitize_text_field( $_REQUEST['media_id'] );
315 }
316 $args = array(
317 'post_type' => 'attachment',
318 'post_status' => 'inherit',
319 'meta_query' => array(
320 array(
321 'key' => 'stockpack_id',
322 'value' => $id
323 )
324 )
325 );
326 $query = new WP_Query( $args );
327 if ( ! $query->have_posts() ) {
328 wp_send_json_error( array( 'message' => __( 'Image not found in cache', 'stockpack' ) ) );
329 }
330
331 wp_send_json_success( wp_prepare_attachment_for_js( $query->post->ID ) );
332
333 }
334
335 /**
336 *
337 */
338 public function download() {
339 check_ajax_referer( 'stockpack_download', 'security' );
340 if ( ! current_user_can( 'upload_files' ) ) {
341 wp_send_json_error( array( 'message' => __( 'You are not allowed to upload files', 'stockpack' ) ) );
342 }
343
344 $id = 0;
345 if ( isset( $_REQUEST['media_id'] ) ) {
346 $id = sanitize_text_field( $_REQUEST['media_id'] );
347 }
348 $post_id = 0;
349 if ( isset( $_REQUEST['post_id'] ) ) {
350 $post_id = sanitize_text_field( $_REQUEST['post_id'] );
351 }
352 $provider = 0;
353 if ( isset( $_REQUEST['provider'] ) ) {
354 $provider = sanitize_text_field( $_REQUEST['provider'] );
355 }
356 $must_license = false;
357 if ( isset( $_REQUEST['must_license'] ) ) {
358 $must_license = sanitize_text_field( $_REQUEST['must_license'] );
359 }
360 $new_filename = "";
361 if ( isset( $_REQUEST['new_filename'] ) ) {
362 $new_filename = sanitize_text_field( $_REQUEST['new_filename'] );
363 }
364 $search = isset( $_REQUEST['search_key'] ) ? sanitize_title( $_REQUEST['search_key'] ) : $this->get_domain();
365 $description = isset( $_REQUEST['description'] ) ? sanitize_textarea_field( $_REQUEST['description'] ) : '';
366
367
368
369 $image = $this->image( $id, $post_id, $description, $search, $must_license, $provider, $new_filename );
370
371 if ( is_wp_error( $image ) ) {
372 if ( strpos( $image->get_error_code(), 'limit_reached' ) !== false ) {
373 wp_send_json_error( $image );
374 die();
375 }
376 wp_send_json_error( array( 'message' => $image->get_error_message() ) );
377 }
378
379 wp_send_json_success( wp_prepare_attachment_for_js( $image ) );
380
381 }
382
383
384
385 public function is_provider_premium( $provider ) {
386 return in_array( $provider, array(
387 'adobe_stock',
388 'getty',
389 'istock',
390 'deposit_photos'
391 )
392 );
393 }
394
395 public function images( $query = [] ) {
396 if ( empty( $query['provider'] ) ) {
397 $available = $this->settings->get_available_search_provider_slugs();
398 if ( $available ) {
399 $query['available_providers'] = implode( ',', $available );
400 }
401 }
402
403 return $this->call( 'GET', 'images/search', $query );
404 }
405
406
407 /**
408 * @param $data
409 *
410 * @return array
411 */
412 private function format( $data ) {
413 return $data;
414 }
415
416
417 public function get_license_cost( $media_id, $provider ) {
418
419 return $this->call( 'GET', 'images/license-cost', array(
420 'id' => $media_id,
421 'locale' => get_locale(),
422 'provider' => $provider
423 ) );
424
425 }
426
427 public function get_ai_prices() {
428 $cached = get_transient( 'stockpack_ai_prices' );
429
430 if ( is_array( $cached ) ) {
431 return $cached;
432 }
433
434 $response = $this->call( 'GET', 'generate/prices' );
435 $prices = array();
436
437 if ( isset( $response->data ) ) {
438 $prices = json_decode( wp_json_encode( $response->data ), true );
439 }
440
441 set_transient( 'stockpack_ai_prices', $prices, $prices ? DAY_IN_SECONDS : 5 * MINUTE_IN_SECONDS );
442
443 return $prices;
444 }
445
446 public function image( $media_id, $post_id, $description = '', $search = '', $must_license = 0, $provider = 0, $new_filename = '' ) {
447
448 do_action( 'stockpack_before_attachment_download', $provider,$new_filename, $media_id, $must_license );
449
450 $big_image_threshold = (int) apply_filters( 'big_image_size_threshold', 2560, array( 0, 0 ), '', 0 );
451
452 $image = $this->call( 'GET', 'images/download',
453 array(
454 'id' => $media_id,
455 'must_license' => $must_license,
456 'provider' => $provider,
457 'min_width'=> $big_image_threshold,
458 'min_height'=> $big_image_threshold,
459 )
460 );
461
462 if ( ! isset( $image->data->download ) ) {
463 if ( is_wp_error( $image ) ) {
464 return $image;
465 }
466
467 return $this->handle_download_errors( $image->data );
468 }
469
470 $filename = $this->getFilename($image, $new_filename, $must_license);
471
472 $caption = $image->data->caption ?: '';
473 $attachment_id = $this->upload_remote_image_and_attach( $image->data->download, $post_id, $description, $filename,$image, $search, $caption );
474 if ( is_wp_error( $attachment_id ) ) {
475 return $attachment_id;
476 }
477 if ( ! $attachment_id ) {
478 return new WP_Error( __( 'upload_failure', 'There has been a problem with the upload. Please try again', 'stockpack' ) );
479 }
480
481 update_post_meta( $attachment_id, 'stockpack_id', $media_id );
482
483 return $attachment_id;
484 }
485
486 private function handle_license_errors( $response ) {
487 if ( isset( $response->code ) ) {
488 switch ( $response->code ) {
489 case 'token_expired':
490 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' ) );
491 }
492
493 if ( ! empty( $response->message ) ) {
494 return new WP_Error( $response->code, $response->message );
495 }
496 }
497
498 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' ) );
499 }
500
501 private function handle_download_errors( $response ) {
502 if ( isset( $response->code ) ) {
503 switch ( $response->code ) {
504 case 'token_expired':
505 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' ) );
506 }
507
508 if ( ! empty( $response->message ) ) {
509 return new WP_Error( $response->code, $response->message );
510 }
511 }
512
513 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' ) );
514 }
515
516 private function handle_search_errors( $response ) {
517 if ( isset( $response->code ) ) {
518 switch ( $response->code ) {
519 case 'token_expired':
520 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' ) );
521 }
522 }
523
524 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' ) );
525
526 }
527
528
529 /**
530 * @param $method
531 * @param $path
532 * @param array $query
533 *
534 * @return mixed|null
535 */
536 private function call( $method, $path, $query = [] ) {
537 /** @var StockPack $stockpack */
538 $stockpack = StockPack::get_instance();
539
540
541 if ( ! isset( $query['key'] ) ) {
542 $api_key = $stockpack->settings->get_api_key();
543 if ( $api_key ) {
544 $query = array_merge( $query,
545 [
546 'api_token' => $api_key
547 ]
548 );
549 } else {
550 $path .= '-trial';
551 }
552 }
553
554 $query = array_merge( $query,
555 [
556 'referral' => get_bloginfo( 'url' ),
557 'safe_search' => $stockpack->settings->get_safe_search(),
558 'user_data' => $stockpack->settings->get_license_state(),
559 ]
560 );
561
562 $url = $this->url . '/' . self::VERSION . '/' . $path;
563 $is_post = 'post' === strtolower( $method );
564
565 if ( ! $is_post ) {
566 $url .= '?' . http_build_query( $query );
567 }
568
569 do_action( 'stockpack_before_api_request', $url, $query );
570
571 $headers = array( 'Accept' => 'application/json' );
572
573 if ( $is_post ) {
574 $response = wp_remote_post( $url, array(
575 'timeout' => 20,
576 'sslverify' => false,
577 'headers' => $headers,
578 'body' => $query,
579 ) );
580 } else {
581 $response = wp_remote_get( $url, array(
582 'timeout' => apply_filters( 'stockpack_api_timeout', $this->timeout_for( $path ), $path ),
583 'sslverify' => false,
584 'headers' => $headers,
585 ) );
586 }
587
588 do_action( 'stockpack_after_api_request', $url, $query, $response );
589
590 if ( is_wp_error( $response ) ) {
591 return $response;
592 }
593
594 $response_code = wp_remote_retrieve_response_code( $response );
595 if ( $response_code !== 200 ) {
596 switch ( $response_code ) {
597 case 429: // too many requests
598 return $this->limit_exceeded( $response );
599 case 401: // access denied
600 return $this->invalid_token( $response );
601 default:
602 return $this->server_error( $response, $response_code );
603 }
604 }
605
606 $response_body = wp_remote_retrieve_body( $response );
607
608 return json_decode( $response_body );
609 }
610
611 private function timeout_for( $path ) {
612 return strpos( $path, 'images/download' ) === 0 ? 30 : 10;
613 }
614
615 private function error_payload( $error ) {
616 return array(
617 'code' => $error->get_error_code(),
618 'message' => $error->get_error_message(),
619 );
620 }
621
622 private function server_error( $response, $response_code ) {
623 $body = json_decode( wp_remote_retrieve_body( $response ) );
624
625 if ( ! empty( $body->message ) ) {
626 return new WP_Error( ! empty( $body->code ) ? $body->code : 1, $body->message );
627 }
628
629 return new WP_Error( 1, sprintf(
630 /* translators: %d: HTTP status code returned by the StockPack API */
631 __( 'The StockPack server returned an error (HTTP %d). If this keeps happening, contact StockPack support.', 'stockpack' ),
632 $response_code
633 ) );
634 }
635
636 private function limit_exceeded( $response ) {
637 /** @var StockPack $stockpack */
638 $stockpack = StockPack::get_instance();
639
640
641 $limit = wp_remote_retrieve_header( $response, 'x-ratelimit-limit' );
642 $retry_after = wp_remote_retrieve_header( $response, 'retry-after' );
643 $retry_after = $this->human_seconds( $retry_after );
644 $retry_after = '<span class ="stockpack-timer">' . $retry_after . '</span>';
645 if ( $limit > 50 ) {
646 return new WP_Error( 'premium_limit_reached', __( 'Request limit reached, the reset will be in ', 'stockpack' ) . $retry_after );
647 }
648 $api_key = $stockpack->admin->get_api_key();
649 if ( $api_key ) {
650 return new WP_Error( 'free_limit_reached', __( 'Request limit reached, the reset will be in ', 'stockpack' ) . $retry_after );
651 }
652
653
654 return new WP_Error( 'anonymous_limit_reached', __( 'Request limit reached, the reset will be in ', 'stockpack' ) . $retry_after );
655 }
656
657 private function invalid_token( $response ) {
658 return new WP_Error( 'invalid_token', __( 'Request denied. Please check your token!', 'stockpack' ) );
659 }
660
661 private function support_caption_translation( $caption ) {
662 if ( ! $caption ) {
663 return '';
664 }
665
666 $caption = str_replace( 'Photo by', __( 'Photo by', 'stockpack' ), $caption );
667 $caption = str_replace( ' on ', _x( ' on ', 'Make sure to keep the space', 'stockpack' ), $caption );
668
669 return $caption;
670 }
671
672 /**
673 * @param $image_url
674 * @param $parent_id
675 *
676 * @param $description
677 *
678 * @param $name
679 * @param string $search
680 *
681 * @param string $caption
682 *
683 * @return bool|int|WP_Error
684 */
685 private function upload_remote_image_and_attach( $image_url, $parent_id, $description, $name, $image, $search = '', $caption = '' ) {
686
687 $get = wp_remote_get( $image_url, array(
688 'timeout' => apply_filters( 'stockpack_download_timeout', 30 )
689 )
690 );
691 if ( is_wp_error( $get ) ) {
692 return new WP_Error( 100, __( 'Image download failed, please try again. Errors:', 'stockpack' ) . PHP_EOL . $get->get_error_message() );
693 }
694 $type = wp_remote_retrieve_header( $get, 'content-type' );
695 if ( ! $type ) {
696 $type = wp_check_filetype( $name )['type'];
697 }
698 if ( ! $type ) {
699 return new WP_Error( 100, __( 'Image type couldn\'t be determined', 'stockpack' ) );
700 }
701 $name = $this->update_extension( $name, $type );
702
703 $name = apply_filters( 'stockpack_attachment_filename', $name, $image );
704
705 $mirror = wp_upload_bits( $name, '', wp_remote_retrieve_body( $get ) );
706 $attachment = array(
707 'post_title' => $this->strip_extension($name),
708 'post_content' => $description,
709 'post_excerpt' => apply_filters( 'stockpack_caption', $this->support_caption_translation( $caption ), $image ),
710 'post_mime_type' => $type
711 );
712 $attach_id = wp_insert_attachment( $attachment, $mirror['file'], $parent_id );
713 require_once( ABSPATH . 'wp-admin/includes/image.php' );
714
715 // An upscale that WordPress immediately scales back down is an
716 // upscale the user paid for and did not get, so the threshold is
717 // lifted for the result of one. Priority 999 so it wins over a theme
718 // or plugin that lowers it.
719 $is_upscale = isset( $image->data->meta->action ) && 'upscale' === $image->data->meta->action;
720
721 if ( $is_upscale ) {
722 add_filter( 'big_image_size_threshold', '__return_false', 999 );
723 }
724
725 $attach_data = wp_generate_attachment_metadata( $attach_id, $mirror['file'] );
726 wp_update_attachment_metadata( $attach_id, $attach_data );
727
728 if ( $is_upscale ) {
729 remove_filter( 'big_image_size_threshold', '__return_false', 999 );
730 }
731 update_post_meta( $attach_id, '_wp_attachment_image_alt', $description );
732 $this->store_attachment_meta($attach_id,$image);
733
734 do_action( 'stockpack_after_attachment_uploaded', $attach_id, $image );
735
736 return $attach_id;
737
738 }
739
740 private function store_attachment_meta($attach_id, $image){
741 $data = $image->data;
742 $meta = isset($data->meta) ? $data->meta : new stdClass();
743 update_post_meta($attach_id,'stockpack_media_id',$data->id);
744 update_post_meta($attach_id,'stockpack_provider',isset($meta->provider) ? $meta->provider : '');
745 update_post_meta($attach_id,'stockpack_author_url',isset($meta->author_url) ? $meta->author_url : '');
746 update_post_meta($attach_id,'stockpack_author_name',isset($meta->author_name) ? $meta->author_name : '');
747 update_post_meta($attach_id,'stockpack_image_url',isset($meta->image_url) ? $meta->image_url : '');
748 update_post_meta($attach_id,'stockpack_download_timestamp',time());
749 }
750
751 /**
752 * @return mixed
753 */
754 private function get_domain() {
755 $url = get_bloginfo( 'url' );
756 $parse = wp_parse_url( $url );
757
758 return strtok( $parse['host'], '.' );
759 }
760
761
762 private function human_seconds( $seconds, $separator = ":" ) {
763 return sprintf( "%02d%s%02d%s%02d", floor( $seconds / 3600 ), $separator, ( $seconds / 60 ) % 60, $separator, $seconds % 60 );
764 }
765
766 private function update_extension( $name, $mime ) {
767 $name = explode( '.', $name );
768 $extension = $this->mime2ext( $mime );
769 if ( $extension ) {
770 return $name[0] . '.' . $extension;
771 }
772
773 return $name[0] . '.' . $name[1];
774
775 }
776
777 private function strip_extension( $name ) {
778 $name = explode( '.', $name );
779 return $name[0];
780 }
781
782 private function getFilename($image, $new_filename, $must_license)
783 {
784 $stockpack = StockPack::get_instance();
785
786 $filename = $image->data->name;
787
788 if($new_filename) {
789 $filename = $new_filename;
790 }
791
792 if(!$this->is_provider_premium($image->data->provider) || $must_license) {
793 return $filename;
794 }
795
796 if($stockpack->settings->add_license_state_to_filename()==='yes') {
797 $filename = '--sto--placeholder--' . $image->data->provider.'--'.$image->data->id.'--'.$filename;
798 }
799
800 return $filename;
801 }
802
803 public function debug_wp_remote_post_and_get_request( $response, $context, $class, $r, $url ) {
804 error_log( '------------------------------' );
805 error_log( $url );
806 error_log( json_encode( $response ) );
807 error_log( $class );
808 error_log( $context );
809 error_log( json_encode( $r ) );
810 }
811
812 private function mime2ext( $mime ) {
813 $mime = str_replace( ';charset=UTF-8', '', $mime );
814 $mime_map = [
815 'video/3gpp2' => '3g2',
816 'video/3gp' => '3gp',
817 'video/3gpp' => '3gp',
818 'application/x-compressed' => '7zip',
819 'audio/x-acc' => 'aac',
820 'audio/ac3' => 'ac3',
821 'application/postscript' => 'ai',
822 'audio/x-aiff' => 'aif',
823 'audio/aiff' => 'aif',
824 'audio/x-au' => 'au',
825 'video/x-msvideo' => 'avi',
826 'video/msvideo' => 'avi',
827 'video/avi' => 'avi',
828 'application/x-troff-msvideo' => 'avi',
829 'application/macbinary' => 'bin',
830 'application/mac-binary' => 'bin',
831 'application/x-binary' => 'bin',
832 'application/x-macbinary' => 'bin',
833 'image/bmp' => 'bmp',
834 'image/x-bmp' => 'bmp',
835 'image/x-bitmap' => 'bmp',
836 'image/x-xbitmap' => 'bmp',
837 'image/x-win-bitmap' => 'bmp',
838 'image/x-windows-bmp' => 'bmp',
839 'image/ms-bmp' => 'bmp',
840 'image/x-ms-bmp' => 'bmp',
841 'application/bmp' => 'bmp',
842 'application/x-bmp' => 'bmp',
843 'application/x-win-bitmap' => 'bmp',
844 'application/cdr' => 'cdr',
845 'application/coreldraw' => 'cdr',
846 'application/x-cdr' => 'cdr',
847 'application/x-coreldraw' => 'cdr',
848 'image/cdr' => 'cdr',
849 'image/x-cdr' => 'cdr',
850 'zz-application/zz-winassoc-cdr' => 'cdr',
851 'application/mac-compactpro' => 'cpt',
852 'application/pkix-crl' => 'crl',
853 'application/pkcs-crl' => 'crl',
854 'application/x-x509-ca-cert' => 'crt',
855 'application/pkix-cert' => 'crt',
856 'text/css' => 'css',
857 'text/x-comma-separated-values' => 'csv',
858 'text/comma-separated-values' => 'csv',
859 'application/vnd.msexcel' => 'csv',
860 'application/x-director' => 'dcr',
861 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx',
862 'application/x-dvi' => 'dvi',
863 'message/rfc822' => 'eml',
864 'application/x-msdownload' => 'exe',
865 'video/x-f4v' => 'f4v',
866 'audio/x-flac' => 'flac',
867 'video/x-flv' => 'flv',
868 'image/gif' => 'gif',
869 'application/gpg-keys' => 'gpg',
870 'application/x-gtar' => 'gtar',
871 'application/x-gzip' => 'gzip',
872 'application/mac-binhex40' => 'hqx',
873 'application/mac-binhex' => 'hqx',
874 'application/x-binhex40' => 'hqx',
875 'application/x-mac-binhex40' => 'hqx',
876 'text/html' => 'html',
877 'image/x-icon' => 'ico',
878 'image/x-ico' => 'ico',
879 'image/vnd.microsoft.icon' => 'ico',
880 'text/calendar' => 'ics',
881 'application/java-archive' => 'jar',
882 'application/x-java-application' => 'jar',
883 'application/x-jar' => 'jar',
884 'image/jp2' => 'jp2',
885 'video/mj2' => 'jp2',
886 'image/jpx' => 'jp2',
887 'image/jpm' => 'jp2',
888 'image/jpeg' => 'jpg',
889 'image/pjpeg' => 'jpg',
890 'application/x-javascript' => 'js',
891 'application/json' => 'json',
892 'text/json' => 'json',
893 'application/vnd.google-earth.kml+xml' => 'kml',
894 'application/vnd.google-earth.kmz' => 'kmz',
895 'text/x-log' => 'log',
896 'audio/x-m4a' => 'm4a',
897 'application/vnd.mpegurl' => 'm4u',
898 'audio/midi' => 'mid',
899 'application/vnd.mif' => 'mif',
900 'video/quicktime' => 'mov',
901 'video/x-sgi-movie' => 'movie',
902 'audio/mpeg' => 'mp3',
903 'audio/mpg' => 'mp3',
904 'audio/mpeg3' => 'mp3',
905 'audio/mp3' => 'mp3',
906 'video/mp4' => 'mp4',
907 'video/mpeg' => 'mpeg',
908 'application/oda' => 'oda',
909 'audio/ogg' => 'ogg',
910 'video/ogg' => 'ogg',
911 'application/ogg' => 'ogg',
912 'application/x-pkcs10' => 'p10',
913 'application/pkcs10' => 'p10',
914 'application/x-pkcs12' => 'p12',
915 'application/x-pkcs7-signature' => 'p7a',
916 'application/pkcs7-mime' => 'p7c',
917 'application/x-pkcs7-mime' => 'p7c',
918 'application/x-pkcs7-certreqresp' => 'p7r',
919 'application/pkcs7-signature' => 'p7s',
920 'application/pdf' => 'pdf',
921 'application/octet-stream' => 'pdf',
922 'application/x-x509-user-cert' => 'pem',
923 'application/x-pem-file' => 'pem',
924 'application/pgp' => 'pgp',
925 'application/x-httpd-php' => 'php',
926 'application/php' => 'php',
927 'application/x-php' => 'php',
928 'text/php' => 'php',
929 'text/x-php' => 'php',
930 'application/x-httpd-php-source' => 'php',
931 'image/png' => 'png',
932 'image/x-png' => 'png',
933 'application/powerpoint' => 'ppt',
934 'application/vnd.ms-powerpoint' => 'ppt',
935 'application/vnd.ms-office' => 'ppt',
936 'application/msword' => 'doc',
937 'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'pptx',
938 'application/x-photoshop' => 'psd',
939 'image/vnd.adobe.photoshop' => 'psd',
940 'audio/x-realaudio' => 'ra',
941 'audio/x-pn-realaudio' => 'ram',
942 'application/x-rar' => 'rar',
943 'application/rar' => 'rar',
944 'application/x-rar-compressed' => 'rar',
945 'audio/x-pn-realaudio-plugin' => 'rpm',
946 'application/x-pkcs7' => 'rsa',
947 'text/rtf' => 'rtf',
948 'text/richtext' => 'rtx',
949 'video/vnd.rn-realvideo' => 'rv',
950 'application/x-stuffit' => 'sit',
951 'application/smil' => 'smil',
952 'text/srt' => 'srt',
953 'image/svg+xml' => 'svg',
954 'application/x-shockwave-flash' => 'swf',
955 'application/x-tar' => 'tar',
956 'application/x-gzip-compressed' => 'tgz',
957 'image/tiff' => 'tiff',
958 'text/plain' => 'txt',
959 'text/x-vcard' => 'vcf',
960 'application/videolan' => 'vlc',
961 'text/vtt' => 'vtt',
962 'audio/x-wav' => 'wav',
963 'audio/wave' => 'wav',
964 'audio/wav' => 'wav',
965 'application/wbxml' => 'wbxml',
966 'video/webm' => 'webm',
967 'audio/x-ms-wma' => 'wma',
968 'application/wmlc' => 'wmlc',
969 'video/x-ms-wmv' => 'wmv',
970 'video/x-ms-asf' => 'wmv',
971 'application/xhtml+xml' => 'xhtml',
972 'application/excel' => 'xl',
973 'application/msexcel' => 'xls',
974 'application/x-msexcel' => 'xls',
975 'application/x-ms-excel' => 'xls',
976 'application/x-excel' => 'xls',
977 'application/x-dos_ms_excel' => 'xls',
978 'application/xls' => 'xls',
979 'application/x-xls' => 'xls',
980 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlsx',
981 'application/vnd.ms-excel' => 'xlsx',
982 'application/xml' => 'xml',
983 'text/xml' => 'xml',
984 'text/xsl' => 'xsl',
985 'application/xspf+xml' => 'xspf',
986 'application/x-compress' => 'z',
987 'application/x-zip' => 'zip',
988 'application/zip' => 'zip',
989 'application/x-zip-compressed' => 'zip',
990 'application/s-compressed' => 'zip',
991 'multipart/x-zip' => 'zip',
992 'text/x-scriptzsh' => 'zsh',
993 'application/illustrator' => 'ai',
994 'application/eps' => 'eps',
995 'application/x-eps' => 'eps',
996 'image/eps' => 'eps',
997 'image/x-eps' => 'eps'
998 ];
999
1000 return isset( $mime_map[ $mime ] ) === true ? $mime_map[ $mime ] : false;
1001 }
1002
1003
1004
1005 }
1006
1007 $GLOBALS['stockpack_query'] = StockpackQuery::get_instance();
1008 }
1009