GalleryItem.php
| 1 | <?php |
| 2 | namespace SureCart\Models; |
| 3 | |
| 4 | use ArrayAccess; |
| 5 | use JsonSerializable; |
| 6 | use SureCart\Concerns\Arrayable; |
| 7 | use SureCart\Concerns\Objectable; |
| 8 | use SureCart\Models\Traits\HasAttributes; |
| 9 | |
| 10 | /** |
| 11 | * GalleryItem model |
| 12 | */ |
| 13 | abstract class GalleryItem implements ArrayAccess, JsonSerializable, Arrayable, Objectable { |
| 14 | use HasAttributes; |
| 15 | |
| 16 | /** |
| 17 | * The item. |
| 18 | * |
| 19 | * @var \WP_Post|\SureCart\Models\ProductMedia |
| 20 | */ |
| 21 | protected $item = null; |
| 22 | |
| 23 | /** |
| 24 | * Whether to include lightbox. |
| 25 | * |
| 26 | * @var bool |
| 27 | */ |
| 28 | protected $with_lightbox = false; |
| 29 | |
| 30 | /** |
| 31 | * Set the lightbox attribute. |
| 32 | * |
| 33 | * @param bool $with_lightbox Whether to include lightbox. |
| 34 | * |
| 35 | * @return self |
| 36 | */ |
| 37 | public function withLightbox( $with_lightbox = true ) { |
| 38 | $this->with_lightbox = $with_lightbox; |
| 39 | return $this; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Convert object to array. |
| 44 | * |
| 45 | * @return Array |
| 46 | */ |
| 47 | public function toArray() { |
| 48 | if ( isset( $this->item->ID ) ) { |
| 49 | $this->item->id = $this->item->ID; |
| 50 | } |
| 51 | if ( method_exists( $this->item, 'toArray' ) ) { |
| 52 | return $this->item->toArray(); |
| 53 | } |
| 54 | if ( $this->item->guid ) { |
| 55 | $this->item->url = $this->item->guid; |
| 56 | } |
| 57 | return (array) $this->item; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Get the image markup. |
| 62 | * |
| 63 | * @param string $key The key to get. |
| 64 | * |
| 65 | * @return string |
| 66 | */ |
| 67 | public function __get( $key ) { |
| 68 | // normalize the variant option. |
| 69 | if ( 'variant_option' === $key && isset( $this->item->ID ) ) { |
| 70 | return get_post_meta( $this->item->ID, 'sc_variant_option', true ); |
| 71 | } |
| 72 | |
| 73 | // normalize the ID. |
| 74 | if ( 'id' === $key && isset( $this->item->ID ) ) { |
| 75 | return $this->item->ID; |
| 76 | } |
| 77 | |
| 78 | if ( isset( $this->item->{$key} ) ) { |
| 79 | return $this->item->{$key}; |
| 80 | } |
| 81 | return null; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Determine if the given attribute exists. |
| 86 | * |
| 87 | * @param mixed $offset Name. |
| 88 | * @return bool |
| 89 | */ |
| 90 | public function offsetExists( $offset ): bool { |
| 91 | return ! is_null( $this->item->{$offset} ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Get the value for a given offset. |
| 96 | * |
| 97 | * @param mixed $offset Name. |
| 98 | * @return mixed |
| 99 | */ |
| 100 | #[\ReturnTypeWillChange] |
| 101 | public function offsetGet( $offset ) { |
| 102 | return $this->item->{$offset}; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Set the value for a given offset. |
| 107 | * |
| 108 | * @param mixed $offset Name. |
| 109 | * @param mixed $value Value. |
| 110 | * @return void |
| 111 | */ |
| 112 | public function offsetSet( $offset, $value ): void { |
| 113 | $this->item->{$offset} = $value; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Unset the value for a given offset. |
| 118 | * |
| 119 | * @param mixed $offset Name. |
| 120 | * @return void |
| 121 | */ |
| 122 | public function offsetUnset( $offset ): void { |
| 123 | $this->item->{$offset} = null; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Determine if an attribute or relation exists on the model. |
| 128 | * |
| 129 | * @param string $key Name. |
| 130 | * @return bool |
| 131 | */ |
| 132 | public function __isset( $key ) { |
| 133 | // normalize the variant option. |
| 134 | if ( 'variant_option' === $key && isset( $this->item->ID ) ) { |
| 135 | return ! empty( get_post_meta( $this->item->ID, 'sc_variant_option', true ) ); |
| 136 | } |
| 137 | |
| 138 | return isset( $this->item->{$key} ); |
| 139 | } |
| 140 | } |
| 141 |