-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathAdsAsset.php
316 lines (277 loc) · 8.83 KB
/
AdsAsset.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\API\Google;
use Automattic\WooCommerce\GoogleListingsAndAds\Google\Ads\GoogleAdsClient;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsAwareTrait;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsAwareInterface;
use Google\Ads\GoogleAds\V12\Services\GoogleAdsRow;
use Google\Ads\GoogleAds\V12\Enums\AssetTypeEnum\AssetType;
use Google\Ads\GoogleAds\V12\Resources\Asset;
use Google\Ads\GoogleAds\V12\Services\AssetOperation;
use Google\Ads\GoogleAds\V12\Services\MutateOperation;
use Google\Ads\GoogleAds\Util\V12\ResourceNames;
use Google\Ads\GoogleAds\V12\Common\TextAsset;
use Google\Ads\GoogleAds\V12\Common\ImageAsset;
use Google\Ads\GoogleAds\V12\Common\CallToActionAsset;
use Automattic\WooCommerce\GoogleListingsAndAds\Proxies\WP;
use Google\ApiCore\ApiException;
use Exception;
/**
* Class AdsAsset
*
* Used for the Performance Max Campaigns
* https://developers.google.com/google-ads/api/docs/performance-max/assets
*
* @since x.x.x
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\API\Google
*/
class AdsAsset implements OptionsAwareInterface {
use OptionsAwareTrait;
/**
* WP Proxy
*
* @var WP
*/
protected WP $wp;
/**
* The Google Ads Client.
*
* @var GoogleAdsClient
*/
protected GoogleAdsClient $client;
/**
* Maximum payload size in bytes.
*
* @var int
*/
protected const MAX_PAYLOAD_BYTES = 30 * 1024 * 1024;
/**
* Maximum image size in bytes.
*
* @var int
*/
protected const MAX_IMAGE_SIZE_BYTES = 5 * 1024 * 1024;
/**
* AdsAsset constructor.
*
* @param GoogleAdsClient $client The Google Ads client.
* @param WP $wp The WordPress proxy.
*/
public function __construct( GoogleAdsClient $client, WP $wp ) {
$this->client = $client;
$this->wp = $wp;
}
/**
* Temporary ID to use within a batch job.
* A negative number which is unique for all the created resources.
*
* @var int
*/
protected static $temporary_id = -5;
/**
* Return a temporary resource name for the asset.
*
* @param int $temporary_id The temporary ID to use for the asset.
*
* @return string The Asset resource name.
*/
protected function temporary_resource_name( int $temporary_id ): string {
return ResourceNames::forAsset( $this->options->get_ads_id(), $temporary_id );
}
/**
* Returns the asset type for the given field type.
*
* @param string $field_type The field type.
*
* @return int The asset type.
* @throws Exception If the field type is not supported.
*/
protected function get_asset_type_by_field_type( string $field_type ): int {
switch ( $field_type ) {
case AssetFieldType::LOGO:
case AssetFieldType::MARKETING_IMAGE:
case AssetFieldType::SQUARE_MARKETING_IMAGE:
case AssetFieldType::PORTRAIT_MARKETING_IMAGE:
return AssetType::IMAGE;
case AssetFieldType::CALL_TO_ACTION_SELECTION:
return AssetType::CALL_TO_ACTION;
case AssetFieldType::HEADLINE:
case AssetFieldType::LONG_HEADLINE:
case AssetFieldType::DESCRIPTION:
case AssetFieldType::BUSINESS_NAME:
return AssetType::TEXT;
default:
throw new Exception( 'Asset Field type not supported' );
}
}
/**
* Returns the image data.
*
* @param string $url The image url.
*
* @return array The image data.
* @throws Exception If the image url is not a valid url or the image size is too large.
*/
protected function get_image_data( string $url ): array {
$image_data = $this->wp->wp_remote_get( $url );
if ( is_wp_error( $image_data ) || empty( $image_data['body'] ) ) {
throw new Exception( sprintf( 'There was a problem loading the url: %s', $url ) );
}
$size = $image_data['headers']->offsetGet( 'content-length' );
if ( $size > self::MAX_IMAGE_SIZE_BYTES ) {
throw new Exception( 'Image size is too large.' );
}
return [
'body' => $image_data['body'],
'size' => $size,
];
}
/**
* Returns a list of batches of assets.
*
* @param array $assets A list of assets.
* @param int $max_size The maximum size of the payload in bytes.
*
* @return array A list of batches of assets.
* @throws Exception If the image url is not a valid url, if the field type is not supported or the image size is too big.
*/
protected function create_batches( array $assets, int $max_size = self::MAX_PAYLOAD_BYTES ): array {
$batch_size = 0;
$index = 0;
$batches = [];
foreach ( $assets as $asset ) {
if ( $this->get_asset_type_by_field_type( $asset['field_type'] ) === AssetType::IMAGE ) {
$image_data = $this->get_image_data( $asset['content'] );
$asset['body'] = $image_data['body'];
$batch_size += $image_data['size'];
if ( $batch_size > $max_size ) {
$batches[ ++$index ][] = $asset;
$batch_size = $image_data['size'];
continue;
}
}
$batches[ $index ][] = $asset;
}
return $batches;
}
/**
* Creates the assets so they can be used in the asset groups.
*
* @param array $assets The assets to create.
* @param int $batch_size The maximum size of the payload in bytes.
*
* @return array A list of Asset's ARN created.
*
* @throws Exception If the asset type is not supported or if the image url is not a valid url.
* @throws ApiException If any of the operations fail.
*/
public function create_assets( array $assets, int $batch_size = self::MAX_PAYLOAD_BYTES ): array {
if ( empty( $assets ) ) {
return [];
}
$batches = $this->create_batches( $assets, $batch_size );
$arns = [];
foreach ( $batches as $batch ) {
$operations = [];
foreach ( $batch as $asset ) {
$operations[] = $this->create_operation( $asset, self::$temporary_id-- );
}
// If the mutate operation fails, it will throw an exception that will be caught by the caller.
$arns = [ ...$arns, ...$this->mutate( $operations ) ];
}
return $arns;
}
/**
* Returns an operation to create a text asset.
*
* @param array $data The asset data.
* @param int $temporary_id The temporary ID to use for the asset.
*
* @return MutateOperation The create asset operation.
* @throws Exception If the asset type is not supported.
*/
protected function create_operation( array $data, int $temporary_id ): MutateOperation {
$asset = new Asset(
[
'resource_name' => $this->temporary_resource_name( $temporary_id ),
]
);
switch ( $this->get_asset_type_by_field_type( $data['field_type'] ) ) {
case AssetType::CALL_TO_ACTION:
$asset->setCallToActionAsset( new CallToActionAsset( [ 'call_to_action' => CallToActionType::number( $data['content'] ) ] ) );
break;
case AssetType::IMAGE:
$asset->setImageAsset( new ImageAsset( [ 'data' => $data['body'] ] ) );
$asset->setName( basename( $data['content'] ) );
break;
case AssetType::TEXT:
$asset->setTextAsset( new TextAsset( [ 'text' => $data['content'] ] ) );
break;
default:
throw new Exception( 'Asset type not supported' );
}
$operation = ( new AssetOperation() )->setCreate( $asset );
return ( new MutateOperation() )->setAssetOperation( $operation );
}
/**
* Returns the asset content for the given row.
*
* @param GoogleAdsRow $row Data row returned from a query request.
*
* @return string The asset content.
*/
protected function get_asset_content( GoogleAdsRow $row ): string {
/** @var Asset $asset */
$asset = $row->getAsset();
switch ( $asset->getType() ) {
case AssetType::IMAGE:
return $asset->getImageAsset()->getFullSize()->getUrl();
case AssetType::TEXT:
return $asset->getTextAsset()->getText();
case AssetType::CALL_TO_ACTION:
// When CallToActionType::UNSPECIFIED is returned, does not have a CallToActionAsset.
if ( ! $asset->getCallToActionAsset() ) {
return CallToActionType::UNSPECIFIED;
}
return CallToActionType::label( $asset->getCallToActionAsset()->getCallToAction() );
default:
return '';
}
}
/**
* Convert Asset data to an array.
*
* @param GoogleAdsRow $row Data row returned from a query request.
*
* @return array The asset data converted.
*/
public function convert_asset( GoogleAdsRow $row ): array {
return [
'id' => $row->getAsset()->getId(),
'content' => $this->get_asset_content( $row ),
];
}
/**
* Send a batch of operations to mutate assets.
*
* @param MutateOperation[] $operations
*
* @return array A list of Asset's ARN created.
* @throws ApiException If any of the operations fail.
*/
protected function mutate( array $operations ): array {
$arns = [];
$responses = $this->client->getGoogleAdsServiceClient()->mutate(
$this->options->get_ads_id(),
$operations
);
foreach ( $responses->getMutateOperationResponses() as $response ) {
if ( 'asset_result' === $response->getResponse() ) {
$asset_result = $response->getAssetResult();
$arns[] = $asset_result->getResourceName();
}
}
return $arns;
}
}