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 | * Metadata for the gallery item. |
| 32 | * |
| 33 | * @var array |
| 34 | */ |
| 35 | protected $metadata = []; |
| 36 | |
| 37 | /** |
| 38 | * Set the lightbox attribute. |
| 39 | * |
| 40 | * @param bool $with_lightbox Whether to include lightbox. |
| 41 | * |
| 42 | * @return self |
| 43 | */ |
| 44 | public function withLightbox( $with_lightbox = true ) { |
| 45 | $this->with_lightbox = $with_lightbox; |
| 46 | return $this; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Check if the gallery item exists. |
| 51 | * |
| 52 | * @return bool |
| 53 | */ |
| 54 | public function exists() { |
| 55 | return isset( $this->item->ID ) || isset( $this->item->id ); // For both WP_Post and ProductMedia. |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Convert object to array. |
| 60 | * |
| 61 | * @return array |
| 62 | */ |
| 63 | public function toArray() { |
| 64 | if ( isset( $this->item->ID ) ) { |
| 65 | $this->item->id = $this->item->ID; |
| 66 | } |
| 67 | if ( method_exists( $this->item, 'toArray' ) ) { |
| 68 | return $this->item->toArray(); |
| 69 | } |
| 70 | if ( $this->item->guid ) { |
| 71 | $this->item->url = $this->item->guid; |
| 72 | } |
| 73 | return (array) $this->item; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Get the image markup. |
| 78 | * |
| 79 | * @param string $key The key to get. |
| 80 | * |
| 81 | * @return string |
| 82 | */ |
| 83 | public function __get( $key ) { |
| 84 | // normalize the ID. |
| 85 | if ( 'id' === $key && isset( $this->item->ID ) ) { |
| 86 | return $this->item->ID; |
| 87 | } |
| 88 | |
| 89 | if ( isset( $this->item->{$key} ) ) { |
| 90 | return $this->item->{$key}; |
| 91 | } |
| 92 | return null; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Check if this gallery item is a video. |
| 97 | * |
| 98 | * @return bool |
| 99 | */ |
| 100 | public function isVideo(): bool { |
| 101 | return $this instanceof GalleryItemVideoAttachment; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Determine if the given attribute exists. |
| 106 | * |
| 107 | * @param mixed $offset Name. |
| 108 | * @return bool |
| 109 | */ |
| 110 | public function offsetExists( $offset ): bool { |
| 111 | return ! is_null( $this->item->{$offset} ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Get the value for a given offset. |
| 116 | * |
| 117 | * @param mixed $offset Name. |
| 118 | * @return mixed |
| 119 | */ |
| 120 | #[\ReturnTypeWillChange] |
| 121 | public function offsetGet( $offset ) { |
| 122 | return $this->item->{$offset}; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Set the value for a given offset. |
| 127 | * |
| 128 | * @param mixed $offset Name. |
| 129 | * @param mixed $value Value. |
| 130 | * @return void |
| 131 | */ |
| 132 | public function offsetSet( $offset, $value ): void { |
| 133 | $this->item->{$offset} = $value; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Unset the value for a given offset. |
| 138 | * |
| 139 | * @param mixed $offset Name. |
| 140 | * @return void |
| 141 | */ |
| 142 | public function offsetUnset( $offset ): void { |
| 143 | $this->item->{$offset} = null; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Determine if an attribute or relation exists on the model. |
| 148 | * |
| 149 | * @param string $key Name. |
| 150 | * @return bool |
| 151 | */ |
| 152 | public function __isset( $key ) { |
| 153 | return isset( $this->item->{$key} ) || isset( $this->metadata[ $key ] ); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Get metadata for the gallery item. |
| 158 | * |
| 159 | * If $key is provided, returns the single metadata value (or null). If no |
| 160 | * key is provided, returns the full metadata array. |
| 161 | * |
| 162 | * @param string|null $key The key to check or null to get all metadata. |
| 163 | * |
| 164 | * @return mixed|null|array |
| 165 | */ |
| 166 | public function getMetadata( $key = null ) { |
| 167 | // If no key requested, return the full metadata array. |
| 168 | if ( empty( $key ) ) { |
| 169 | return $this->metadata; |
| 170 | } |
| 171 | |
| 172 | if ( isset( $this->metadata[ $key ] ) ) { |
| 173 | return $this->metadata[ $key ]; |
| 174 | } |
| 175 | |
| 176 | // Backward compatibility for variant_option with post meta - sc_variant_option. |
| 177 | if ( 'variant_option' === $key && isset( $this->item->ID ) ) { |
| 178 | return get_post_meta( $this->item->ID, 'sc_variant_option', true ); |
| 179 | } |
| 180 | |
| 181 | return null; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Set the metadata for the gallery item. |
| 186 | * |
| 187 | * @param string $key The key to set. |
| 188 | * @param mixed $value The value to set. |
| 189 | * |
| 190 | * @return self |
| 191 | */ |
| 192 | public function setMetadata( $key, $value ): self { |
| 193 | $this->metadata[ $key ] = $value; |
| 194 | return $this; |
| 195 | } |
| 196 | } |
| 197 |