PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.0
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.0
1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 1.9.14 All 163 releases
woocommerce-pos / includes / Sync / Product_Images.php

Product_Images.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.0, at includes/Sync/Product_Images.php

54 lines 1.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WCPOS sync read surface.
4 *
5 * @package WCPOS\WooCommercePOS\Sync
6 */
7
8 namespace WCPOS\WooCommercePOS\Sync;
9
10 /**
11 * Serves medium product images across catalog read surfaces.
12 */
13 final class Product_Images {
14 /**
15 * THE image augmentation, registered ONCE with {@see Augmentation_Pipeline}
16 * and projected onto both read lanes: replace full-size product image URLs
17 * with their WordPress medium size.
18 *
19 * @param mixed $payload Serialized product record.
20 * @param null|mixed $object Product object, when the lane has one loaded.
21 * @param null|mixed $request Request context.
22 */
23 public static function augment_record( $payload, $object = null, $request = null ) {
24 if ( ! \is_array( $payload ) ) {
25 return $payload;
26 }
27
28 return self::downsize_images( $payload );
29 }
30
31 /**
32 * Replace each image source with its medium URL when available.
33 *
34 * @param array $record Product response record.
35 */
36 private static function downsize_images( array $record ): array {
37 if ( ! isset( $record['images'] ) || ! \is_array( $record['images'] ) ) {
38 return $record;
39 }
40 foreach ( $record['images'] as $index => $entry ) {
41 if ( ! \is_array( $entry ) || ! isset( $entry['id'] ) || 0 >= (int) $entry['id'] ) {
42 continue;
43 }
44 $medium = image_downsize( (int) $entry['id'], 'medium' );
45 if ( $medium ) {
46 $entry['src'] = $medium[0];
47 $record['images'][ $index ] = $entry;
48 }
49 }
50
51 return $record;
52 }
53 }
54