| 1 |
<?php |
| 2 |
|
| 3 |
namespace Getwid\Blocks; |
| 4 |
|
| 5 |
class RecentPosts extends \Getwid\Blocks\AbstractBlock { |
| 6 |
|
| 7 |
protected static $blockName = 'getwid/recent-posts'; |
| 8 |
|
| 9 |
public function __construct() { |
| 10 |
|
| 11 |
parent::__construct( self::$blockName ); |
| 12 |
|
| 13 |
register_block_type( |
| 14 |
'getwid/recent-posts', |
| 15 |
array( |
| 16 |
'attributes' => array( |
| 17 |
'titleTag' => array( |
| 18 |
'type' => 'string', |
| 19 |
'default' => 'h3', |
| 20 |
), |
| 21 |
'imageSize' => array( |
| 22 |
'type' => 'string', |
| 23 |
'default' => 'large', |
| 24 |
), |
| 25 |
'cropImages' => array( |
| 26 |
'type' => 'boolean', |
| 27 |
'default' => true, |
| 28 |
), |
| 29 |
'categories' => array( |
| 30 |
'type' => 'string', |
| 31 |
), |
| 32 |
'postsToShow' => array( |
| 33 |
'type' => 'number', |
| 34 |
'default' => 5, |
| 35 |
), |
| 36 |
'showTitle' => array( |
| 37 |
'type' => 'boolean', |
| 38 |
'default' => true, |
| 39 |
), |
| 40 |
'showDate' => array( |
| 41 |
'type' => 'boolean', |
| 42 |
'default' => false, |
| 43 |
), |
| 44 |
'showCategories' => array( |
| 45 |
'type' => 'boolean', |
| 46 |
'default' => false, |
| 47 |
), |
| 48 |
'showCommentsCount' => array( |
| 49 |
'type' => 'boolean', |
| 50 |
'default' => false, |
| 51 |
), |
| 52 |
'showContent' => array( |
| 53 |
'type' => 'boolean', |
| 54 |
'default' => false, |
| 55 |
), |
| 56 |
'contentLength' => array( |
| 57 |
'type' => 'number', |
| 58 |
'default' => apply_filters('excerpt_length', 55), |
| 59 |
), |
| 60 |
'showFeaturedImage' => array( |
| 61 |
'type' => 'boolean', |
| 62 |
'default' => false, |
| 63 |
), |
| 64 |
'postLayout' => array( |
| 65 |
'type' => 'string', |
| 66 |
'default' => 'list', |
| 67 |
), |
| 68 |
'columns' => array( |
| 69 |
'type' => 'number', |
| 70 |
'default' => 3, |
| 71 |
), |
| 72 |
'align' => array( |
| 73 |
'type' => 'string', |
| 74 |
), |
| 75 |
'order' => array( |
| 76 |
'type' => 'string', |
| 77 |
'default' => 'desc', |
| 78 |
), |
| 79 |
'orderBy' => array( |
| 80 |
'type' => 'string', |
| 81 |
'default' => 'date', |
| 82 |
), |
| 83 |
|
| 84 |
'className' => array( |
| 85 |
'type' => 'string', |
| 86 |
), |
| 87 |
), |
| 88 |
'render_callback' => [ $this, 'render_callback' ] |
| 89 |
) |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
public function getLabel() { |
| 94 |
return __('Recent Posts', 'getwid'); |
| 95 |
} |
| 96 |
|
| 97 |
public function block_frontend_assets() { |
| 98 |
|
| 99 |
if ( is_admin() ) { |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
if ( FALSE == getwid()->assetsOptimization()->load_assets_on_demand() ) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
add_filter( 'getwid/optimize/assets', |
| 108 |
function ( $assets ) { |
| 109 |
$assets[] = getwid()->settings()->getPrefix() . '-blocks-common'; |
| 110 |
|
| 111 |
return $assets; |
| 112 |
} |
| 113 |
); |
| 114 |
|
| 115 |
add_filter( 'getwid/optimize/should_load_common_css', '__return_true' ); |
| 116 |
|
| 117 |
$rtl = is_rtl() ? '.rtl' : ''; |
| 118 |
|
| 119 |
wp_enqueue_style( |
| 120 |
self::$blockName, |
| 121 |
getwid_get_plugin_url( 'assets/blocks/recent-posts/style' . $rtl . '.css' ), |
| 122 |
[], |
| 123 |
getwid()->settings()->getVersion() |
| 124 |
); |
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
public function render_callback( $attributes, $content ) { |
| 129 |
|
| 130 |
$query_args = array( |
| 131 |
'posts_per_page' => $attributes['postsToShow'], |
| 132 |
'ignore_sticky_posts' => 1, |
| 133 |
'post_status' => 'publish', |
| 134 |
'order' => $attributes['order'], |
| 135 |
'orderby' => $attributes['orderBy'], |
| 136 |
); |
| 137 |
|
| 138 |
if ( isset( $attributes['categories'] ) ) { |
| 139 |
$query_args['tax_query'] = array( |
| 140 |
array( |
| 141 |
'taxonomy' => 'category', |
| 142 |
'field' => 'id', |
| 143 |
'terms' => $attributes['categories'] |
| 144 |
) |
| 145 |
); |
| 146 |
} |
| 147 |
|
| 148 |
$block_name = 'wp-block-getwid-recent-posts'; |
| 149 |
|
| 150 |
$extra_attr = array( |
| 151 |
'block_name' => $block_name |
| 152 |
); |
| 153 |
|
| 154 |
$class = $block_name; |
| 155 |
|
| 156 |
if ( isset( $attributes['align'] ) ) { |
| 157 |
$class .= ' align' . $attributes['align']; |
| 158 |
} |
| 159 |
if ( isset( $attributes['postLayout'] ) ) { |
| 160 |
$class .= ' has-layout-' . $attributes['postLayout']; |
| 161 |
} |
| 162 |
if ( isset( $attributes['showPostDate'] ) && $attributes['showPostDate'] ) { |
| 163 |
$class .= ' has-dates'; |
| 164 |
} |
| 165 |
if ( isset( $attributes['className'] ) ) { |
| 166 |
$class .= ' ' . $attributes['className']; |
| 167 |
} |
| 168 |
if( isset( $attributes['cropImages'] ) && $attributes['cropImages'] === true ){ |
| 169 |
$class .= ' has-cropped-images'; |
| 170 |
} |
| 171 |
|
| 172 |
$wrapper_class = $block_name . '__wrapper'; |
| 173 |
|
| 174 |
if ( isset( $attributes['columns'] ) && $attributes['postLayout'] === 'grid' ) { |
| 175 |
$wrapper_class .= ' getwid-columns getwid-columns-' . $attributes['columns']; |
| 176 |
} |
| 177 |
|
| 178 |
$attributes['titleTag'] = $this->validateHeadingHTMLTag( $attributes['titleTag'] ); |
| 179 |
|
| 180 |
$q = new \WP_Query( $query_args ); |
| 181 |
ob_start(); |
| 182 |
?> |
| 183 |
|
| 184 |
<div class="<?php echo esc_attr( $class ); ?>"> |
| 185 |
<div class="<?php echo esc_attr( $wrapper_class );?>"> |
| 186 |
<?php |
| 187 |
if ( $q->have_posts() ): |
| 188 |
ob_start(); |
| 189 |
|
| 190 |
while( $q->have_posts() ): |
| 191 |
$q->the_post(); |
| 192 |
getwid_get_template_part('recent-posts/post', $attributes, false, $extra_attr); |
| 193 |
endwhile; |
| 194 |
|
| 195 |
wp_reset_postdata(); |
| 196 |
ob_end_flush(); |
| 197 |
else: |
| 198 |
do_action( 'getwid/blocks/recent-posts/no-items', $attributes, $content ); |
| 199 |
endif; |
| 200 |
?> |
| 201 |
</div> |
| 202 |
</div> |
| 203 |
<?php |
| 204 |
|
| 205 |
$result = ob_get_clean(); |
| 206 |
|
| 207 |
$this->block_frontend_assets(); |
| 208 |
|
| 209 |
return $result; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
getwid()->blocksManager()->addBlock( |
| 214 |
new \Getwid\Blocks\RecentPosts() |
| 215 |
); |
| 216 |
|