| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wpxero\Marqueex\Integrations\Gutenberg\Render; |
| 4 |
|
| 5 |
use Wpxero\Marqueex\Traits\MarqueeSource; |
| 6 |
use Wpxero\Marqueex\Traits\Singleton; |
| 7 |
|
| 8 |
if (!defined('ABSPATH')) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Post-source helper for dynamic MarqueeX Gutenberg blocks. |
| 14 |
* |
| 15 |
* Wraps the shared {@see MarqueeSource} trait (the same query/duplication/url |
| 16 |
* logic the Elementor widgets use) and exposes a small, Elementor-free surface |
| 17 |
* the block `render.php` files can call. Keeping this in the Gutenberg namespace |
| 18 |
* means the dynamic blocks never depend on Elementor being installed. |
| 19 |
*/ |
| 20 |
class PostSource { |
| 21 |
use Singleton; |
| 22 |
use MarqueeSource; |
| 23 |
|
| 24 |
/** |
| 25 |
* Run the marquee post query for a block's `query` attribute and return a |
| 26 |
* list padded to the minimum needed for a seamless loop. |
| 27 |
* |
| 28 |
* @param array $query Block `query` attribute (postType, postsPerPage, orderby, order, categoryIds). |
| 29 |
* @return \WP_Post[] |
| 30 |
*/ |
| 31 |
public function get_posts(array $query) { |
| 32 |
$args = $this->get_query_args([ |
| 33 |
'post_type' => $query['postType'] ?? 'post', |
| 34 |
'posts_per_page' => $query['postsPerPage'] ?? 6, |
| 35 |
'orderby' => $query['orderby'] ?? 'date', |
| 36 |
'order' => $query['order'] ?? 'desc', |
| 37 |
'category_ids' => $query['categoryIds'] ?? [], |
| 38 |
]); |
| 39 |
|
| 40 |
return $this->ensure_minimum_posts(get_posts($args)); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Pad a custom item list to the minimum needed for a seamless loop. |
| 45 |
* |
| 46 |
* @param array $items |
| 47 |
* @return array |
| 48 |
*/ |
| 49 |
public function pad_items(array $items) { |
| 50 |
return $this->ensure_minimum_posts($items); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Resolve the public URL for a post, honoring custom URLs. |
| 55 |
* |
| 56 |
* @param \WP_Post $post |
| 57 |
* @return string |
| 58 |
*/ |
| 59 |
public function url($post) { |
| 60 |
return $this->get_post_url($post); |
| 61 |
} |
| 62 |
} |
| 63 |
|