| 1 |
<?php |
| 2 |
namespace Depicter\Services; |
| 3 |
|
| 4 |
use Averta\WordPress\Utility\JSON; |
| 5 |
use Depicter\GuzzleHttp\Exception\GuzzleException; |
| 6 |
|
| 7 |
/** |
| 8 |
* A bridge for fetching assets from averta assets API |
| 9 |
* |
| 10 |
* @package Depicter\Services |
| 11 |
*/ |
| 12 |
class AssetsAPIService |
| 13 |
{ |
| 14 |
/** |
| 15 |
* Search Media AssetsProvider |
| 16 |
* |
| 17 |
* @param string $assetType |
| 18 |
* @param array $options |
| 19 |
* |
| 20 |
* @return array|mixed |
| 21 |
* @throws GuzzleException |
| 22 |
*/ |
| 23 |
public static function searchAssets( string $assetType = 'photos', array $options = [] ) |
| 24 |
{ |
| 25 |
$availableTypes = ['photos', 'videos', 'vectors', 'icons']; |
| 26 |
if ( !in_array( $assetType, $availableTypes ) ) { |
| 27 |
return []; |
| 28 |
} |
| 29 |
|
| 30 |
$response = \Depicter::remote()->get( 'v1/search/'. $assetType, [ "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, string $size = 'full', array $args = [ 'forcePreview' => 'false' ] ){ |
| 46 |
$endpointNumber = 1; |
| 47 |
|
| 48 |
/** |
| 49 |
* Regex to find possible endpoint number other than default one (1) |
| 50 |
* |
| 51 |
* @example if $id = @Fd2X@UnSiqwe8TLRnG8k, $endpointNumber is 2, and $id @UnSiqwe8TLRnG8k |
| 52 |
*/ |
| 53 |
preg_match( '/^@Fd(\d{1,})X(.*)/', $id, $matches ); |
| 54 |
if( !empty( $matches[1] ) && !empty( $matches[2] ) ){ |
| 55 |
$endpointNumber = $matches[1]; |
| 56 |
$id = $matches[2]; // set extracted id |
| 57 |
} |
| 58 |
|
| 59 |
$endpoint = \Depicter::remote()->endpoint( $endpointNumber ) . 'v1/media/' . $id . '/' . $size . '/'; |
| 60 |
|
| 61 |
if ( !empty( $args ) && is_array( $args ) ) { |
| 62 |
$endpoint = add_query_arg( $args, $endpoint ); |
| 63 |
} |
| 64 |
|
| 65 |
return $endpoint; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Get Asset Content |
| 70 |
* |
| 71 |
* @param string $id Media ID |
| 72 |
* @param string $size Media size to return |
| 73 |
* |
| 74 |
* @param array $args |
| 75 |
* |
| 76 |
* @return mixed |
| 77 |
* @throws GuzzleException |
| 78 |
*/ |
| 79 |
public static function getContent( $id, string $size = 'full', array $args = [] ){ |
| 80 |
$endpointNumber = 1; |
| 81 |
|
| 82 |
/** |
| 83 |
* Regex to find possible endpoint number other than default one (1) |
| 84 |
* |
| 85 |
* @example if $id = @Fd2X@UnSiqwe8TLRnG8k, $endpointNumber is 2, and $id @UnSiqwe8TLRnG8k |
| 86 |
*/ |
| 87 |
preg_match( '/^@Fd(\d{1,})X(.*)/', $id, $matches ); |
| 88 |
if( !empty( $matches[1] ) && !empty( $matches[2] ) ){ |
| 89 |
$endpointNumber = $matches[1]; |
| 90 |
$id = $matches[2]; // set extracted id |
| 91 |
} |
| 92 |
|
| 93 |
$endpoint = \Depicter::remote()->endpoint( $endpointNumber ) . 'v1/media-content/' . $id . '/'; |
| 94 |
|
| 95 |
if ( !empty( $args ) && is_array( $args ) ) { |
| 96 |
$endpoint = add_query_arg( $args, $endpoint ); |
| 97 |
} |
| 98 |
|
| 99 |
$response = \Depicter::remote()->get( $endpoint ); |
| 100 |
|
| 101 |
return $response->getBody(); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Get Urls of an asset |
| 106 |
* |
| 107 |
* @param string $id Asset ID |
| 108 |
* |
| 109 |
* @return mixed |
| 110 |
* @throws GuzzleException |
| 111 |
*/ |
| 112 |
public static function getAssetUrls( $id ){ |
| 113 |
$response = \Depicter::remote()->get( 'v1/asset/' . $id . '/urls' ); |
| 114 |
return JSON::decode( $response->getBody(), true ); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Search Elements |
| 119 |
* |
| 120 |
* @param array $options |
| 121 |
* |
| 122 |
* @return mixed |
| 123 |
* @throws GuzzleException |
| 124 |
*/ |
| 125 |
public static function searchElements( array $options = [] ) { |
| 126 |
$response = \Depicter::remote()->get( |
| 127 |
'v1/curated/elements', |
| 128 |
[ 'query' => $options ], |
| 129 |
$options['directory'] ?? 1 |
| 130 |
); |
| 131 |
|
| 132 |
return JSON::decode( $response->getBody(), true ); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Search Document Templates |
| 137 |
* |
| 138 |
* @param array $options |
| 139 |
* |
| 140 |
* @return mixed |
| 141 |
* @throws GuzzleException |
| 142 |
*/ |
| 143 |
public static function searchDocumentTemplates( array $options = [] ) { |
| 144 |
$options['plt'] = 'wordpress'; |
| 145 |
$response = \Depicter::remote()->get( |
| 146 |
'v1/curated/document/templates', |
| 147 |
[ 'query' => $options ], |
| 148 |
$options['directory'] ?? 2 |
| 149 |
); |
| 150 |
|
| 151 |
return JSON::decode( $response->getBody(), true ); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Get templates categories |
| 156 |
* |
| 157 |
* @param array $options |
| 158 |
* |
| 159 |
* @return mixed |
| 160 |
* @throws GuzzleException |
| 161 |
*/ |
| 162 |
public static function getDocumentTemplateCategories( array $options = [] ) { |
| 163 |
$response = \Depicter::remote()->get( |
| 164 |
'v1/curated/document/templates/categories', |
| 165 |
[ 'query' => $options ], |
| 166 |
$options['directory'] ?? 2 |
| 167 |
); |
| 168 |
|
| 169 |
return JSON::decode( $response->getBody(), true ); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Get Template Data |
| 174 |
* |
| 175 |
* @param int $templateID Template ID |
| 176 |
* @param array $options |
| 177 |
* @param bool $associative |
| 178 |
* |
| 179 |
* @return mixed |
| 180 |
* @throws GuzzleException |
| 181 |
*/ |
| 182 |
public static function getDocumentTemplateData( $templateID, $options = [], $associative = false ) { |
| 183 |
$response = \Depicter::remote()->get( |
| 184 |
'v1/curated/document/templates/' . $templateID, |
| 185 |
[ 'query' => $options ], |
| 186 |
$options['directory'] ?? 2 |
| 187 |
); |
| 188 |
|
| 189 |
return JSON::decode( $response->getBody(), $associative ); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Preview a Document Template |
| 194 |
* |
| 195 |
* @param int $templateID |
| 196 |
* @param int|null $directory |
| 197 |
* |
| 198 |
* @return mixed |
| 199 |
* @throws GuzzleException |
| 200 |
*/ |
| 201 |
public static function previewDocumentTemplate( $templateID, $directory = null ) { |
| 202 |
$directory = $directory ?? 2; |
| 203 |
$response = \Depicter::remote()->get( |
| 204 |
'v1/curated/document/templates/preview', |
| 205 |
[ "query" => [ "id" => $templateID, "directory" => $directory ] ], |
| 206 |
$directory |
| 207 |
); |
| 208 |
|
| 209 |
return $response->getBody(); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Search Animations |
| 214 |
* |
| 215 |
* @param array $options |
| 216 |
* |
| 217 |
* @return mixed |
| 218 |
* @throws GuzzleException |
| 219 |
*/ |
| 220 |
public static function searchAnimations( array $options = [] ) { |
| 221 |
$response = \Depicter::remote()->get( |
| 222 |
'v1/curated/animations', |
| 223 |
[ 'query' => $options ], |
| 224 |
$options['directory'] ?? 1 |
| 225 |
); |
| 226 |
|
| 227 |
return JSON::decode( $response->getBody(), true ); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Retrieving animation phases |
| 232 |
* |
| 233 |
* @param array $options |
| 234 |
* |
| 235 |
* @return mixed |
| 236 |
* @throws GuzzleException |
| 237 |
*/ |
| 238 |
public static function getAnimationsCategories( array $options = [] ) { |
| 239 |
$response = \Depicter::remote()->get( |
| 240 |
'v1/curated/animations/categories', |
| 241 |
[ 'query' => $options ], |
| 242 |
$options['directory'] ?? 1 |
| 243 |
); |
| 244 |
|
| 245 |
return JSON::decode( $response->getBody(), true ); |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Get Template Groups |
| 250 |
* |
| 251 |
* @param array $options |
| 252 |
* |
| 253 |
* @return void |
| 254 |
* @throws GuzzleException |
| 255 |
*/ |
| 256 |
public static function getDocumentTemplateGroups( array $options = [] ) { |
| 257 |
$options['plt'] = 'wordpress'; |
| 258 |
$response = \Depicter::remote()->get( |
| 259 |
'v2/curated/document/templates/groups', |
| 260 |
[ 'query' => $options ], |
| 261 |
$options['directory'] ?? 1 |
| 262 |
); |
| 263 |
|
| 264 |
return JSON::decode( $response->getBody(), true ); |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Search Document Templates |
| 269 |
* |
| 270 |
* @param array $options |
| 271 |
* |
| 272 |
* @return mixed |
| 273 |
* @throws GuzzleException |
| 274 |
*/ |
| 275 |
public static function searchDocumentTemplatesV2( array $options = [] ) { |
| 276 |
$options['plt'] = 'wordpress'; |
| 277 |
$response = \Depicter::remote()->get( |
| 278 |
'v2/curated/document/templates', |
| 279 |
[ 'query' => $options ], |
| 280 |
$options['directory'] ?? 2 |
| 281 |
); |
| 282 |
|
| 283 |
return JSON::decode( $response->getBody(), true ); |
| 284 |
} |
| 285 |
} |
| 286 |
|