class-recent-posts-controller.php
118 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SuperbAddons\Gutenberg\BlocksAPI\Controllers; |
| 4 | |
| 5 | use SuperbAddons\Data\Controllers\LogController; |
| 6 | use Exception; |
| 7 | |
| 8 | defined('ABSPATH') || exit(); |
| 9 | |
| 10 | class RecentPostsController |
| 11 | { |
| 12 | public static function DynamicRender($attributes, $content) |
| 13 | { |
| 14 | try { |
| 15 | if ((!$attributes['displayBlockOnFront'] && is_front_page()) || |
| 16 | (!$attributes['displayBlockOnBlog'] && is_home()) || |
| 17 | (!$attributes['displayBlockOnPagesPosts'] && !is_front_page() && !is_home()) |
| 18 | ) { |
| 19 | return '<!-- Superb Recent Posts Block Hidden -->'; |
| 20 | } |
| 21 | $excludecurrent = ($attributes['excludeCurrent'] && !is_front_page() && !is_home()) ? array(get_the_ID()) : array(); |
| 22 | $recent_posts_args = array("numberposts" => $attributes['numberOfPosts'], "post_status" => "publish", "exclude" => $excludecurrent); |
| 23 | |
| 24 | if (count($attributes['selectedCategories']) > 0) { |
| 25 | $recent_posts_args['category__in'] = $attributes['selectedCategories']; |
| 26 | } |
| 27 | if (count($attributes['selectedTags']) > 0) { |
| 28 | $recent_posts_args['tag__in'] = $attributes['selectedTags']; |
| 29 | } |
| 30 | $recent_posts_args = apply_filters('superbaddons_recent_posts_block_args', $recent_posts_args, $attributes); |
| 31 | |
| 32 | $recent_posts = wp_get_recent_posts($recent_posts_args); |
| 33 | |
| 34 | return self::Render($attributes, $recent_posts); |
| 35 | } catch (Exception $ex) { |
| 36 | LogController::HandleException($ex); |
| 37 | return; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | private static function Render($attributes, $recent_posts) |
| 42 | { |
| 43 | ob_start(); |
| 44 | |
| 45 | // If no posts found |
| 46 | if (count($recent_posts) <= 0) : ?> |
| 47 | <div class="superbaddons-recentposts-wrapper"> |
| 48 | <p style="font-weight:500;"><?= esc_html__("No posts found", "superbrecentposts"); ?></p> |
| 49 | </div> |
| 50 | <?php return ob_get_clean(); |
| 51 | endif; |
| 52 | |
| 53 | // If posts found |
| 54 | ?> |
| 55 | <div class="superbaddons-recentposts-wrapper superbaddons-recentposts-alignment-<?= esc_attr($attributes['toolbarAlignment']); ?>"> |
| 56 | <ul class="superbaddons-recentposts-list"> |
| 57 | <?php |
| 58 | $wrapperTag = $attributes['IsInEditor'] ? 'span' : 'a'; |
| 59 | foreach ($recent_posts as $post) { |
| 60 | $permalink = $attributes['IsInEditor'] ? "#" : get_permalink($post['ID']); |
| 61 | $the_post_title = $post['post_title'] === '' ? $post['post_name'] : $post['post_title']; |
| 62 | $temp_thumbnail_url = get_the_post_thumbnail_url($post['ID'], array($attributes['thumbnailSize'], $attributes['thumbnailSize'])); |
| 63 | $thumbnail_url = !$temp_thumbnail_url ? SUPERBADDONS_ASSETS_PATH . '/img/post-thumbnail-placeholder.png' : $temp_thumbnail_url; |
| 64 | ?> |
| 65 | <li class="superbaddons-recentposts-item"> |
| 66 | <<?= $wrapperTag ?> href="<?= esc_url($permalink) ?>" <?= $attributes['linksTargetBlank'] && !$attributes['IsInEditor'] ? 'target="_blank"' : '' ?>> |
| 67 | <div class="superbaddons-recentposts-item-inner"> |
| 68 | <?php if ($attributes['displayThumbnails'] && ($attributes['displayPlaceholderThumbnails'] || $temp_thumbnail_url !== false)) : ?> |
| 69 | <div class="superbaddons-recentposts-item-left"> |
| 70 | <img width="<?= esc_attr($attributes['thumbnailSize']) ?>" height="<?= esc_attr($attributes['thumbnailSize']) ?>" src="<?= esc_url($thumbnail_url) ?>" <?= $attributes['imgBorderRadiusEnabled'] ? 'style="border-radius:' . esc_attr($attributes['imgBorderRadius'] / 2) . '%;"' : ""; ?> /> |
| 71 | </div> |
| 72 | <?php endif; ?> |
| 73 | <div class="superbaddons-recentposts-item-right"> |
| 74 | <?php if ($attributes['displayDate'] || $attributes['displayAuthor']) : ?> |
| 75 | <!-- Meta --> |
| 76 | <span style="font-size:<?= esc_attr($attributes['fontSizeMeta']) ?>px; color:<?= esc_attr($attributes['colorMeta']) ?>;"> |
| 77 | <?php if ($attributes['displayDate']) : ?> |
| 78 | <time class="superbaddons-recentposts-item-date"> |
| 79 | <?= esc_html(get_the_date(get_option('date_format', 'F j, Y'), $post['ID'])); ?> |
| 80 | </time> |
| 81 | <?php endif; ?> |
| 82 | <?php if ($attributes['displayAuthor']) : ?> |
| 83 | <span class="superbaddons-recentposts-item-author"> |
| 84 | <?= esc_html(__("by", "superbrecentposts") . " " . get_the_author_meta($attributes['useAuthorDisplayName'] ? 'display_name' : 'user_nicename', $post['post_author'])); ?> |
| 85 | </span> |
| 86 | <?php endif; ?> |
| 87 | </span> |
| 88 | <?php endif; ?> |
| 89 | |
| 90 | <!-- Title --> |
| 91 | <span style="font-size:<?= esc_attr($attributes['fontSizeTitle']) ?>px; color:<?= esc_attr($attributes['colorTitle']) ?>;"><?= esc_html($the_post_title); ?></span> |
| 92 | |
| 93 | <?php if ($attributes['displayExcerpt']) : ?> |
| 94 | <!-- Excerpt --> |
| 95 | <span style="font-size:<?= esc_attr($attributes['fontSizeExcerpt']) ?>px; color:<?= esc_attr($attributes['colorExcerpt']) ?>;"> |
| 96 | <?= esc_html(wp_trim_words(excerpt_remove_blocks(strip_shortcodes($post['post_content'])), $attributes['excerptLength'], apply_filters('excerpt_more', ' ' . '[…]'))); ?> |
| 97 | </span> |
| 98 | <?php endif; ?> |
| 99 | |
| 100 | <?php if ($attributes['displayCommentCount']) : ?> |
| 101 | <!-- Comment Count --> |
| 102 | <span style="font-size:<?= esc_attr($attributes['fontSizeCommentCount']); ?>px; color:<?= esc_attr($attributes['colorCommentCount']); ?>;"> |
| 103 | <?= esc_html(get_comment_count($post['ID'])['approved'] . " " . __("comment(s)", "superbrecentposts")); ?> |
| 104 | </span> |
| 105 | <?php endif; ?> |
| 106 | </div> |
| 107 | </div> |
| 108 | </<?= $wrapperTag ?>> |
| 109 | </li> |
| 110 | <?php |
| 111 | } |
| 112 | ?> |
| 113 | </ul> |
| 114 | </div> |
| 115 | <?php return ob_get_clean(); |
| 116 | } |
| 117 | } |
| 118 |