| 1 |
<?php |
| 2 |
namespace Depicter\Services; |
| 3 |
|
| 4 |
use Averta\WordPress\Utility\JSON; |
| 5 |
|
| 6 |
/** |
| 7 |
* A bridge for fetching assets from averta assets API |
| 8 |
* |
| 9 |
* @package Depicter\Services |
| 10 |
*/ |
| 11 |
class AssetsAPIService |
| 12 |
{ |
| 13 |
/** |
| 14 |
* Search Media AssetsProvider |
| 15 |
* |
| 16 |
* @param string $assetType |
| 17 |
* @param array $options |
| 18 |
* |
| 19 |
* @return array|mixed |
| 20 |
* @throws \Depicter\GuzzleHttp\Exception\GuzzleException |
| 21 |
*/ |
| 22 |
public static function searchAssets( $assetType = 'photos', $options = [] ) |
| 23 |
{ |
| 24 |
$availableTypes = ['photos', 'videos', 'vectors']; |
| 25 |
if ( !in_array( $assetType, $availableTypes ) ) { |
| 26 |
return []; |
| 27 |
} |
| 28 |
|
| 29 |
$endpoint = \Depicter::remote()->endpoint(1) . 'v1/search/' . $assetType; |
| 30 |
$response = \Depicter::remote()->get("$endpoint?" . http_build_query($options)); |
| 31 |
|
| 32 |
return JSON::decode( $response->getBody(), true ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Get Asset Hotlink URL |
| 37 |
* |
| 38 |
* @param string $id Media ID |
| 39 |
* @param string $size Media size to return |
| 40 |
* |
| 41 |
* @param array $args |
| 42 |
* |
| 43 |
* @return mixed |
| 44 |
*/ |
| 45 |
public static function getHotlink( $id, $size = 'full', $args = [ 'forcePreview' => 'false' ] ) |
| 46 |
{ |
| 47 |
$endpointNumber = 1; |
| 48 |
|
| 49 |
/** |
| 50 |
* Regex to find possible endpoint number other than default one (1) |
| 51 |
* |
| 52 |
* @example if $id = @Fd2X@UnSiqwe8TLRnG8k, $endpointNumber is 2, and $id @UnSiqwe8TLRnG8k |
| 53 |
*/ |
| 54 |
preg_match( '/^@Fd(\d{1,})X(.*)/', $id, $matches ); |
| 55 |
if( !empty( $matches[1] ) && !empty( $matches[2] ) ){ |
| 56 |
$endpointNumber = $matches[1]; |
| 57 |
$id = $matches[2]; // set extracted id |
| 58 |
} |
| 59 |
|
| 60 |
$endpoint = \Depicter::remote()->endpoint( $endpointNumber ) . 'v1/media/' . $id . '/' . $size . '/'; |
| 61 |
|
| 62 |
if ( !empty( $args ) && is_array( $args ) ) { |
| 63 |
$endpoint = add_query_arg( $args, $endpoint ); |
| 64 |
} |
| 65 |
|
| 66 |
return $endpoint; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Search Elements |
| 71 |
* |
| 72 |
* @param array $options |
| 73 |
* |
| 74 |
* @return mixed |
| 75 |
* @throws \Depicter\GuzzleHttp\Exception\GuzzleException |
| 76 |
*/ |
| 77 |
public static function searchElements( $options = [] ) { |
| 78 |
|
| 79 |
$endpoint = \Depicter::remote()->endpoint(1) . 'v1/curated/elements'; |
| 80 |
$response = \Depicter::remote()->get( "$endpoint?" . http_build_query($options) ); |
| 81 |
|
| 82 |
return JSON::decode( $response->getBody(), true ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Search Document Templates |
| 87 |
* |
| 88 |
* @param array $options |
| 89 |
* |
| 90 |
* @return mixed |
| 91 |
* @throws \Depicter\GuzzleHttp\Exception\GuzzleException |
| 92 |
*/ |
| 93 |
public static function searchDocumentTemplates( $options = [] ) { |
| 94 |
|
| 95 |
$endpoint = \Depicter::remote()->endpoint(2) . 'v1/curated/document/templates'; |
| 96 |
$response = \Depicter::remote()->get( "$endpoint?" . http_build_query($options) ); |
| 97 |
|
| 98 |
return JSON::decode( $response->getBody(), true ); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Get templates categories |
| 103 |
* |
| 104 |
* @param array $options |
| 105 |
* |
| 106 |
* @return mixed |
| 107 |
* @throws \Depicter\GuzzleHttp\Exception\GuzzleException |
| 108 |
*/ |
| 109 |
public static function getDocumentTemplateCategories( $options = [] ) { |
| 110 |
|
| 111 |
$endpoint = \Depicter::remote()->endpoint(2) . 'v1/curated/document/templates/categories'; |
| 112 |
$response = \Depicter::remote()->get( "$endpoint?" . http_build_query($options) ); |
| 113 |
|
| 114 |
return JSON::decode( $response->getBody(), true ); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Get Template Data |
| 119 |
* |
| 120 |
* @param $templateID |
| 121 |
* |
| 122 |
* @return mixed |
| 123 |
* @throws \Depicter\GuzzleHttp\Exception\GuzzleException |
| 124 |
*/ |
| 125 |
public static function getDocumentTemplateData( $templateID ) { |
| 126 |
$endpoint = \Depicter::remote()->endpoint(2) . 'v1/curated/document/templates/' . $templateID; |
| 127 |
$response = \Depicter::remote()->get( $endpoint ); |
| 128 |
|
| 129 |
return JSON::decode( $response->getBody(), false ); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Preview a Document Template |
| 134 |
* |
| 135 |
* @param $templateID |
| 136 |
* |
| 137 |
* @return mixed |
| 138 |
* @throws \Depicter\GuzzleHttp\Exception\GuzzleException |
| 139 |
*/ |
| 140 |
public static function previewDocumentTemplate( $templateID ) { |
| 141 |
$endpoint = \Depicter::remote()->endpoint(2) . 'v1/curated/document/templates/preview/?id=' . $templateID; |
| 142 |
$response = \Depicter::remote()->get( $endpoint ); |
| 143 |
|
| 144 |
return $response->getBody(); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Search Animations |
| 149 |
* |
| 150 |
* @param array $options |
| 151 |
* |
| 152 |
* @return mixed |
| 153 |
* @throws \Depicter\GuzzleHttp\Exception\GuzzleException |
| 154 |
*/ |
| 155 |
public static function searchAnimations( $options = [] ) { |
| 156 |
|
| 157 |
$endpoint = \Depicter::remote()->endpoint(1) . 'v1/curated/animations'; |
| 158 |
$response = \Depicter::remote()->get( "$endpoint?" . http_build_query($options) ); |
| 159 |
|
| 160 |
return JSON::decode( $response->getBody(), true ); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* retrieving animation phases |
| 165 |
* |
| 166 |
* @param array $options |
| 167 |
* |
| 168 |
* @return mixed |
| 169 |
* @throws \Depicter\GuzzleHttp\Exception\GuzzleException |
| 170 |
*/ |
| 171 |
public static function getAnimationsCategories( $options = [] ) { |
| 172 |
|
| 173 |
$endpoint = \Depicter::remote()->endpoint(1) . 'v1/curated/animations/categories'; |
| 174 |
$response = \Depicter::remote()->get( "$endpoint?" . http_build_query($options) ); |
| 175 |
|
| 176 |
return JSON::decode( $response->getBody(), true ); |
| 177 |
} |
| 178 |
} |
| 179 |
|