| @@ -1,213 +1,108 @@ | ||
| 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 | - $q = new \WP_Query( $query_args ); | |
| 179 | - ob_start(); | |
| 180 | - ?> | |
| 181 | - | |
| 182 | - <div class="<?php echo esc_attr( $class ); ?>"> | |
| 183 | - <div class="<?php echo esc_attr( $wrapper_class );?>"> | |
| 184 | - <?php | |
| 185 | - if ( $q->have_posts() ): | |
| 186 | - ob_start(); | |
| 187 | - | |
| 188 | - while( $q->have_posts() ): | |
| 189 | - $q->the_post(); | |
| 190 | - getwid_get_template_part('recent-posts/post', $attributes, false, $extra_attr); | |
| 191 | - endwhile; | |
| 192 | - | |
| 193 | - wp_reset_postdata(); | |
| 194 | - ob_end_flush(); | |
| 195 | - else: | |
| 196 | - do_action( 'getwid/blocks/recent-posts/no-items', $attributes, $content ); | |
| 197 | - endif; | |
| 198 | - ?> | |
| 199 | - </div> | |
| 200 | - </div> | |
| 201 | - <?php | |
| 202 | - | |
| 203 | - $result = ob_get_clean(); | |
| 204 | - | |
| 205 | - $this->block_frontend_assets(); | |
| 206 | - | |
| 207 | - return $result; | |
| 208 | - } | |
| 209 | -} | |
| 210 | - | |
| 211 | -getwid()->blocksManager()->addBlock( | |
| 212 | - new \Getwid\Blocks\RecentPosts() | |
| 213 | -); | |
| 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 | +); | |