| 1 |
<?php |
| 2 |
|
| 3 |
namespace Getwid\Blocks; |
| 4 |
|
| 5 |
class RecentPosts extends AbstractBlock { |
| 6 |
|
| 7 |
public function __construct() { |
| 8 |
|
| 9 |
parent::__construct( 'getwid/recent-posts' ); |
| 10 |
|
| 11 |
register_block_type( |
| 12 |
getwid_get_plugin_path( 'assets/blocks/recent-posts' ), |
| 13 |
array( |
| 14 |
'render_callback' => array( $this, 'render_callback' ), |
| 15 |
) |
| 16 |
); |
| 17 |
} |
| 18 |
|
| 19 |
public function get_label() { |
| 20 |
return __( 'Recent Posts', 'getwid' ); |
| 21 |
} |
| 22 |
|
| 23 |
public function render_callback( $attributes, $content ) { |
| 24 |
|
| 25 |
$query_args = array( |
| 26 |
'posts_per_page' => $attributes['postsToShow'], |
| 27 |
'ignore_sticky_posts' => 1, |
| 28 |
'post_status' => 'publish', |
| 29 |
'order' => $attributes['order'], |
| 30 |
'orderby' => $attributes['orderBy'], |
| 31 |
); |
| 32 |
|
| 33 |
if ( isset( $attributes['categories'] ) ) { |
| 34 |
$query_args['tax_query'] = array( |
| 35 |
array( |
| 36 |
'taxonomy' => 'category', |
| 37 |
'field' => 'id', |
| 38 |
'terms' => $attributes['categories'], |
| 39 |
), |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
$block_name = 'wp-block-getwid-recent-posts'; |
| 44 |
|
| 45 |
$extra_attr = array( |
| 46 |
'block_name' => $block_name, |
| 47 |
); |
| 48 |
|
| 49 |
$class = $block_name; |
| 50 |
|
| 51 |
if ( isset( $attributes['align'] ) ) { |
| 52 |
$class .= ' align' . $attributes['align']; |
| 53 |
} |
| 54 |
if ( isset( $attributes['postLayout'] ) ) { |
| 55 |
$class .= ' has-layout-' . $attributes['postLayout']; |
| 56 |
} |
| 57 |
if ( isset( $attributes['showPostDate'] ) && $attributes['showPostDate'] ) { |
| 58 |
$class .= ' has-dates'; |
| 59 |
} |
| 60 |
if ( isset( $attributes['className'] ) ) { |
| 61 |
$class .= ' ' . $attributes['className']; |
| 62 |
} |
| 63 |
if ( isset( $attributes['cropImages'] ) && true === $attributes['cropImages'] ) { |
| 64 |
$class .= ' has-cropped-images'; |
| 65 |
} |
| 66 |
|
| 67 |
$wrapper_class = $block_name . '__wrapper'; |
| 68 |
|
| 69 |
if ( isset( $attributes['columns'] ) && 'grid' === $attributes['postLayout'] ) { |
| 70 |
$wrapper_class .= ' getwid-columns getwid-columns-' . $attributes['columns']; |
| 71 |
} |
| 72 |
|
| 73 |
$attributes['titleTag'] = $this->validate_heading_html_tag( $attributes['titleTag'] ); |
| 74 |
|
| 75 |
$q = new \WP_Query( $query_args ); |
| 76 |
ob_start(); |
| 77 |
?> |
| 78 |
|
| 79 |
<div class="<?php echo esc_attr( $class ); ?>"> |
| 80 |
<div class="<?php echo esc_attr( $wrapper_class ); ?>"> |
| 81 |
<?php |
| 82 |
if ( $q->have_posts() ) : |
| 83 |
ob_start(); |
| 84 |
|
| 85 |
while ( $q->have_posts() ) : |
| 86 |
$q->the_post(); |
| 87 |
getwid_get_template_part( 'recent-posts/post', $attributes, false, $extra_attr ); |
| 88 |
endwhile; |
| 89 |
|
| 90 |
wp_reset_postdata(); |
| 91 |
ob_end_flush(); |
| 92 |
else : |
| 93 |
do_action( 'getwid/blocks/recent-posts/no-items', $attributes, $content ); |
| 94 |
endif; |
| 95 |
?> |
| 96 |
</div> |
| 97 |
</div> |
| 98 |
<?php |
| 99 |
|
| 100 |
$result = ob_get_clean(); |
| 101 |
|
| 102 |
return $result; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
getwid()->blocksManager()->addBlock( |
| 107 |
new RecentPosts() |
| 108 |
); |
| 109 |
|