AbstractProductListBlock.php
2 months ago
ProductListBlock.php
2 months ago
ProductPageBlock.php
2 months ago
ProductReviewBlock.php
4 months ago
ProductReviewListBlock.php
4 months ago
RelatedProductsBlock.php
2 months ago
AbstractProductListBlock.php
217 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Models\Blocks; |
| 4 | |
| 5 | /** |
| 6 | * Abstract product list block. |
| 7 | */ |
| 8 | abstract class AbstractProductListBlock { |
| 9 | /** |
| 10 | * The block. |
| 11 | * |
| 12 | * @var \WP_Block |
| 13 | */ |
| 14 | protected $block; |
| 15 | |
| 16 | /** |
| 17 | * The URL. |
| 18 | * |
| 19 | * @var object |
| 20 | */ |
| 21 | protected $url; |
| 22 | |
| 23 | /** |
| 24 | * The query. |
| 25 | * |
| 26 | * @var \WP_Query |
| 27 | */ |
| 28 | protected $query; |
| 29 | |
| 30 | /** |
| 31 | * Constructor. |
| 32 | * |
| 33 | * @param \WP_Block $block The block. |
| 34 | */ |
| 35 | public function __construct( \WP_Block $block ) { |
| 36 | $this->block = $block; |
| 37 | $this->url = \SureCart::block()->urlParams( 'products' ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Get the URL. |
| 42 | * |
| 43 | * @return object|null |
| 44 | */ |
| 45 | public function urlParams() { |
| 46 | return $this->url; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Get a query attribute with fallback to context and default. |
| 51 | * |
| 52 | * @param string $key The attribute key to fetch. |
| 53 | * @param mixed $default_value The default value if not found. |
| 54 | * |
| 55 | * @return mixed |
| 56 | */ |
| 57 | protected function getQueryAttribute( $key, $default_value = null ) { |
| 58 | return $this->block->parsed_block['attrs']['query'][ $key ] ?? $this->block->context['query'][ $key ] ?? $default_value; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get the query attribute. |
| 63 | * |
| 64 | * @param string $key The key. |
| 65 | * @return mixed |
| 66 | */ |
| 67 | public function __get( $key ) { |
| 68 | if ( 'has_pagination' === $key ) { |
| 69 | $max_page = $this->getQueryAttribute( 'pages', 0 ); |
| 70 | |
| 71 | // if the max page is 1, we don't have a next page. |
| 72 | if ( 1 === $max_page ) { |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | return $this->max_num_pages > 1; |
| 77 | } |
| 78 | |
| 79 | // handle pagination. |
| 80 | if ( 'next_page_link' === $key ) { |
| 81 | $max_page = $this->getQueryAttribute( 'pages', 0 ); |
| 82 | |
| 83 | // if the max page is 1, we don't have a next page. |
| 84 | if ( 1 === $max_page ) { |
| 85 | return ''; |
| 86 | } |
| 87 | |
| 88 | // if the max page is greater than or equal to the query max num pages, we don't have a next page. |
| 89 | if ( $max_page && $max_page === $this->paged ) { |
| 90 | return ''; |
| 91 | } |
| 92 | |
| 93 | return $this->max_num_pages && $this->max_num_pages !== $this->paged ? $this->url->addPageArg( $this->paged + 1 )->url() : ''; |
| 94 | } |
| 95 | |
| 96 | if ( 'previous_page_link' === $key ) { |
| 97 | $max_page = $this->getQueryAttribute( 'pages', 0 ); |
| 98 | |
| 99 | // if the max page is 1, we don't have a next page. |
| 100 | if ( 1 === $max_page ) { |
| 101 | return ''; |
| 102 | } |
| 103 | |
| 104 | return $this->paged > 1 ? $this->url->addPageArg( $this->paged - 1 )->url() : ''; |
| 105 | } |
| 106 | |
| 107 | if ( 'pagination_links' === $key ) { |
| 108 | $max_page = $this->getQueryAttribute( 'pages', 0 ); |
| 109 | |
| 110 | // if the max page is 1, we don't have a next page. |
| 111 | if ( 1 === $max_page ) { |
| 112 | return []; |
| 113 | } |
| 114 | |
| 115 | return array_map( |
| 116 | function ( $i ) { |
| 117 | return array( |
| 118 | 'href' => $this->url->addPageArg( $i )->url(), |
| 119 | 'name' => $i, |
| 120 | 'current' => (int) $i === (int) $this->paged, |
| 121 | ); |
| 122 | }, |
| 123 | range( 1, $max_page ? min( $this->max_num_pages, $max_page ) : $this->max_num_pages ) |
| 124 | ); |
| 125 | } |
| 126 | |
| 127 | if ( 'products' === $key ) { |
| 128 | // Check if we have a query object (for WP_Query based lists). |
| 129 | if ( ! empty( $this->query ) && ! empty( $this->query->posts ) ) { |
| 130 | return array_map( |
| 131 | function ( $post ) { |
| 132 | return sc_get_product( $post ); |
| 133 | }, |
| 134 | $this->query->posts |
| 135 | ); |
| 136 | } |
| 137 | return []; |
| 138 | } |
| 139 | |
| 140 | // Try to get from query object first. |
| 141 | if ( ! empty( $this->query ) ) { |
| 142 | return $this->query->$key ?? $this->query->query[ $key ] ?? $this->query->query_vars[ $key ] ?? null; |
| 143 | } |
| 144 | |
| 145 | return null; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Batch-prime gallery attachment caches for all products in the query. |
| 150 | * |
| 151 | * Collects all numeric attachment IDs from each product's gallery_ids |
| 152 | * metadata and loads them in a single query via WordPress core, |
| 153 | * eliminating N+1 get_post() calls in GalleryItemAttachment::create(). |
| 154 | */ |
| 155 | protected function primeAttachmentCaches() { |
| 156 | if ( empty( $this->query->posts ) ) { |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | $ids = $this->collectGalleryAttachmentIds( $this->query->posts ); |
| 161 | |
| 162 | if ( ! empty( $ids ) ) { |
| 163 | _prime_post_caches( array_unique( $ids ), false, true ); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Collect all numeric WP attachment IDs from product gallery metadata. |
| 169 | * |
| 170 | * @param \WP_Post[] $posts The product posts. |
| 171 | * |
| 172 | * @return int[] |
| 173 | */ |
| 174 | protected function collectGalleryAttachmentIds( array $posts ) { |
| 175 | $ids = []; |
| 176 | |
| 177 | foreach ( $posts as $post ) { |
| 178 | $product = sc_get_product( $post ); |
| 179 | if ( empty( $product ) ) { |
| 180 | continue; |
| 181 | } |
| 182 | |
| 183 | $gallery_ids = $product->gallery_ids ?? []; |
| 184 | if ( ! is_array( $gallery_ids ) ) { |
| 185 | continue; |
| 186 | } |
| 187 | |
| 188 | foreach ( $gallery_ids as $item ) { |
| 189 | if ( is_numeric( $item ) ) { |
| 190 | $ids[] = (int) $item; |
| 191 | } elseif ( is_object( $item ) && isset( $item->id ) && is_numeric( $item->id ) ) { |
| 192 | $ids[] = (int) $item->id; |
| 193 | } elseif ( is_array( $item ) && isset( $item['id'] ) && is_numeric( $item['id'] ) ) { |
| 194 | $ids[] = (int) $item['id']; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | return $ids; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Call the query method. |
| 204 | * |
| 205 | * @param string $method The method. |
| 206 | * @param array $args The arguments. |
| 207 | * |
| 208 | * @return mixed |
| 209 | */ |
| 210 | public function __call( $method, $args ) { |
| 211 | if ( ! empty( $this->query ) ) { |
| 212 | return $this->query->$method( ...$args ); |
| 213 | } |
| 214 | return null; |
| 215 | } |
| 216 | } |
| 217 |