set_header('content-type', 'application/json'); $r->set_body(wp_json_encode(['action'=>$action] + $body)); $result = picsart_video_internal($r); if (is_wp_error($result)) throw new Picsart_Runtime_Error(esc_html($result->get_error_code()), esc_html($result->get_error_message()), absint($result->get_error_data()['status'] ?? 400)); return $result; } function picsart_runtime_https($url) { picsart_runtime_require(is_string($url) && wp_parse_url($url, PHP_URL_SCHEME) === 'https' && wp_http_validate_url($url), 'MEDIA', __('This media does not have a usable public HTTPS URL.', 'picsart-commerce')); return $url; } function picsart_runtime_drive_items($store, $folder, $type) { $r = picsart_runtime_account($store, '/drive/v1/files/search', ['mode'=>$folder ? 'BY_PARENT' : 'OWNED', 'limit'=>100, 'sort'=>[['field'=>'updatedAt','direction'=>'DESC']], 'filter'=>['op'=>'and','filters'=>[['op'=>'eq','field'=>'parentFolderId','value'=>$folder ?: null], $type === 'folder' ? ['op'=>'eq','field'=>'type','value'=>'FOLDER'] : ['op'=>'startsWith','field'=>'mimeType','value'=>$type . '/']]]]); picsart_runtime_require(is_array($r['items'] ?? null), 'DRIVE', __('Picsart Drive returned an unexpected response.', 'picsart-commerce'), 502); return array_map(static fn($f)=>['uid'=>$f['id'],'name'=>$f['name'],'url'=>$f['thumbnail']['url'] ?? ''], $r['items']); } function picsart_runtime_drive_media($store, $uid, $type, $folder = null) { picsart_runtime_require(is_string($uid) && strlen($uid) <= 512 && in_array($type, ['image','video'], true), 'MEDIA', __('Choose a Picsart Drive file.', 'picsart-commerce')); $listed = picsart_runtime_drive_items($store, $folder, $type); picsart_runtime_require(in_array($uid, array_column($listed, 'uid'), true), 'MEDIA', __('This file is no longer in the selected folder. Refresh Picsart Drive.', 'picsart-commerce'), 404); $file = picsart_runtime_account($store, '/drive/v1/files/' . rawurlencode($uid)); picsart_runtime_require(($file['id'] ?? '') === $uid && str_starts_with($file['mimeType'] ?? '', $type . '/'), 'MEDIA', __('Choose a supported Picsart Drive file.', 'picsart-commerce')); return ['uid'=>$uid,'name'=>$file['name'],'url'=>picsart_runtime_https($file['sourceUrl'] ?? '')]; } function picsart_runtime_image_dimensions($url) { require_once ABSPATH . 'wp-admin/includes/file.php'; $tmp = wp_tempnam(); try { $r = wp_safe_remote_get(picsart_runtime_https($url), ['stream'=>true,'filename'=>$tmp,'timeout'=>30,'redirection'=>2,'limit_response_size'=>15 * MB_IN_BYTES + 1]); picsart_runtime_require(!is_wp_error($r) && wp_remote_retrieve_response_code($r) === 200 && filesize($tmp) <= 15 * MB_IN_BYTES, 'PHOTO', __('Could not read this photo within the 15 MB limit.', 'picsart-commerce')); $size = wp_getimagesize($tmp); picsart_runtime_require($size && min($size[0], $size[1]) >= 640 && max($size[0], $size[1]) <= 8192 && in_array($size['mime'] ?? '', ['image/png','image/jpeg','image/webp'], true), 'PHOTO', __('Choose a JPEG, PNG or WebP photo from 640 to 8192 pixels.', 'picsart-commerce')); return ['width'=>$size[0],'height'=>$size[1]]; } finally { wp_delete_file($tmp); } } function picsart_runtime_retain($store, $url, $name, $type) { $file = picsart_runtime_account($store, '/cloud-storage/v1/me/files', ['name'=>$name,'sourceUrl'=>picsart_runtime_https($url),'content'=>['type'=>'STANDALONE','resourceType'=>$type === 'image' ? 'PHOTO' : 'VIDEO','sourcePlatform'=>'WEB']]); picsart_runtime_require(!empty($file['uid']), 'DRIVE', __('Picsart Drive did not confirm the saved file. Check Drive before retrying.', 'picsart-commerce'), 502); return ['uid'=>$file['uid'],'url'=>$file['sourceUrl'] ?? $url]; } function picsart_runtime_upload_bytes($store, $bytes, $mime, $name) { picsart_runtime_require(strlen($bytes) <= 15 * MB_IN_BYTES && in_array($mime, ['image/jpeg','image/png','image/webp'], true), 'PHOTO', __('Choose a JPEG, PNG or WebP photo up to 15 MB.', 'picsart-commerce')); $c = picsart_runtime_token($store); $boundary = 'Picsart' . wp_generate_password(24, false); $body = '--' . $boundary . "\r\nContent-Disposition: form-data; name=\"type\"\r\n\r\nediting-temp\r\n--" . $boundary . "\r\nContent-Disposition: form-data; name=\"file\"; filename=\"photo\"\r\nContent-Type: " . $mime . "\r\n\r\n" . $bytes . "\r\n--" . $boundary . "--\r\n"; $r = picsart_runtime_http('https://upload.picsart.com/v2/files', ['method'=>'POST','headers'=>['Authorization'=>'Bearer ' . $c['accessToken'],'user-id'=>$c['subject'],'X-Platform'=>'web','X-Touchpoint'=>'product-photos','Content-Type'=>'multipart/form-data; boundary=' . $boundary],'body'=>$body,'timeout'=>60]); picsart_runtime_require($r['status'] >= 200 && $r['status'] < 300, 'UPLOAD', __('Picsart could not receive this photo.', 'picsart-commerce'), 502); $url = picsart_runtime_https($r['body']['response']['url'] ?? $r['body']['url'] ?? ''); return picsart_runtime_retain($store, $url, $name, 'image'); } function picsart_runtime_source($store, $id, $prepare = false) { $subject = picsart_runtime_token($store)['subject']; foreach ($store->data['sources'] as $i=>$source) { if ($source['id'] !== $id || (!empty($source['subject']) && $source['subject'] !== $subject)) continue; if (!empty($source['productId']) && $prepare && empty($source['subject'])) { $p = picsart_runtime_platform('source', ['productId'=>$source['productId'],'sourceId'=>$source['id']]); $bytes = base64_decode($p['base64'], true); $size = getimagesizefromstring($bytes); picsart_runtime_require($size, 'PHOTO', __('Product photo is unavailable.', 'picsart-commerce')); $file = picsart_runtime_upload_bytes($store, $bytes, $size['mime'], 'WordPress Product Photo ' . wp_generate_uuid4()); $source['url'] = $file['url']; $source['subject'] = $subject; $store->data['sources'][$i] = $source; $store->save(); } return $source; } throw new Picsart_Runtime_Error('SOURCE', esc_html__('Choose an available photo for this account.', 'picsart-commerce'), 404); }