class-recent-posts-controller.php
143 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()) ? intval(get_the_ID()) : false; |
| 22 | $numberOfPosts = $excludecurrent !== false ? intval($attributes['numberOfPosts']) + 1 : intval($attributes['numberOfPosts']); |
| 23 | $recent_posts_args = array("numberposts" => $numberOfPosts, "post_status" => "publish"); |
| 24 | |
| 25 | if (count($attributes['selectedCategories']) > 0) { |
| 26 | $recent_posts_args['category__in'] = $attributes['selectedCategories']; |
| 27 | } |
| 28 | if (count($attributes['selectedTags']) > 0) { |
| 29 | $recent_posts_args['tag__in'] = $attributes['selectedTags']; |
| 30 | } |
| 31 | $recent_posts_args = apply_filters('superbaddons_recent_posts_block_args', $recent_posts_args, $attributes); |
| 32 | |
| 33 | $recent_posts = wp_get_recent_posts($recent_posts_args); |
| 34 | |
| 35 | // Filter in PHP to preserve query cache across pages |
| 36 | if ($excludecurrent !== false) { |
| 37 | $filtered_posts = array(); |
| 38 | $limit = intval($attributes['numberOfPosts']); |
| 39 | $count = 0; |
| 40 | foreach ($recent_posts as $post) { |
| 41 | if (intval($post['ID']) !== $excludecurrent) { |
| 42 | $filtered_posts[] = $post; |
| 43 | if (++$count >= $limit) { |
| 44 | break; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | $recent_posts = $filtered_posts; |
| 49 | } |
| 50 | |
| 51 | return self::Render($attributes, $recent_posts); |
| 52 | } catch (Exception $ex) { |
| 53 | LogController::HandleException($ex); |
| 54 | return; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | private static function Render($attributes, $recent_posts) |
| 59 | { |
| 60 | ob_start(); |
| 61 | |
| 62 | // If no posts found |
| 63 | if (count($recent_posts) <= 0) : ?> |
| 64 | <div class="superbaddons-recentposts-wrapper"> |
| 65 | <p style="font-weight:500;"><?php echo esc_html__("No posts found", "superb-blocks"); ?></p> |
| 66 | </div> |
| 67 | <?php return ob_get_clean(); |
| 68 | endif; |
| 69 | |
| 70 | // If posts found |
| 71 | ?> |
| 72 | <div class="superbaddons-recentposts-wrapper superbaddons-recentposts-alignment-<?php echo esc_attr($attributes['toolbarAlignment']); ?>"> |
| 73 | <ul class="superbaddons-recentposts-list"> |
| 74 | <?php |
| 75 | $wrapperTag = $attributes['IsInEditor'] ? 'span' : 'a'; |
| 76 | foreach ($recent_posts as $post) { |
| 77 | $permalink = $attributes['IsInEditor'] ? "#" : get_permalink($post['ID']); |
| 78 | $the_post_title = $post['post_title'] === '' ? $post['post_name'] : $post['post_title']; |
| 79 | $temp_thumbnail_url = get_the_post_thumbnail_url($post['ID'], array($attributes['thumbnailSize'], $attributes['thumbnailSize'])); |
| 80 | $thumbnail_url = !$temp_thumbnail_url ? SUPERBADDONS_ASSETS_PATH . '/img/post-thumbnail-placeholder.png' : $temp_thumbnail_url; |
| 81 | ?> |
| 82 | <li class="superbaddons-recentposts-item"> |
| 83 | <<?php echo esc_html($wrapperTag); ?> href="<?php echo esc_url($permalink) ?>" <?php echo $attributes['linksTargetBlank'] && !$attributes['IsInEditor'] ? 'target="_blank"' : '' ?>> |
| 84 | <div class="superbaddons-recentposts-item-inner"> |
| 85 | <?php if ($attributes['displayThumbnails'] && ($attributes['displayPlaceholderThumbnails'] || $temp_thumbnail_url !== false)) : ?> |
| 86 | <div class="superbaddons-recentposts-item-left"> |
| 87 | <img width="<?php echo esc_attr($attributes['thumbnailSize']) ?>" height="<?php echo esc_attr($attributes['thumbnailSize']) ?>" src="<?php echo esc_url($thumbnail_url) ?>" <?php echo $attributes['imgBorderRadiusEnabled'] ? 'style="border-radius:' . esc_attr($attributes['imgBorderRadius'] / 2) . '%;"' : ""; ?> /> |
| 88 | </div> |
| 89 | <?php endif; ?> |
| 90 | <div class="superbaddons-recentposts-item-right"> |
| 91 | <?php if ($attributes['displayDate'] || $attributes['displayAuthor']) : ?> |
| 92 | <!-- Meta --> |
| 93 | <span style="font-size:<?php echo esc_attr($attributes['fontSizeMeta']) ?>px; color:<?php echo esc_attr($attributes['colorMeta']) ?>;"> |
| 94 | <?php if ($attributes['displayDate']) : ?> |
| 95 | <time class="superbaddons-recentposts-item-date"> |
| 96 | <?php echo esc_html(get_the_date(get_option('date_format', 'F j, Y'), $post['ID'])); ?> |
| 97 | </time> |
| 98 | <?php endif; ?> |
| 99 | <?php if ($attributes['displayAuthor']) : ?> |
| 100 | <span class="superbaddons-recentposts-item-author"> |
| 101 | <?php echo esc_html(__("by", "superb-blocks") . " " . get_the_author_meta($attributes['useAuthorDisplayName'] ? 'display_name' : 'user_nicename', $post['post_author'])); ?> |
| 102 | </span> |
| 103 | <?php endif; ?> |
| 104 | </span> |
| 105 | <?php endif; ?> |
| 106 | |
| 107 | <!-- Title --> |
| 108 | <span style="font-size:<?php echo esc_attr($attributes['fontSizeTitle']) ?>px; color:<?php echo esc_attr($attributes['colorTitle']) ?>;"><?php echo esc_html($the_post_title); ?></span> |
| 109 | |
| 110 | <?php if ($attributes['displayExcerpt']) : ?> |
| 111 | <!-- Excerpt --> |
| 112 | <span style="font-size:<?php echo esc_attr($attributes['fontSizeExcerpt']) ?>px; color:<?php echo esc_attr($attributes['colorExcerpt']) ?>;"> |
| 113 | <?php echo esc_html( |
| 114 | wp_trim_words( |
| 115 | excerpt_remove_blocks(strip_shortcodes($post['post_content'])), |
| 116 | $attributes['excerptLength'], |
| 117 | // Can't apply this filter using wp_trim_excerpt() as we want to apply the users custom excerpt length without affecting general excerpts. |
| 118 | // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound |
| 119 | apply_filters('excerpt_more', ' ' . '[…]') |
| 120 | ) |
| 121 | ); ?> |
| 122 | </span> |
| 123 | <?php endif; ?> |
| 124 | |
| 125 | <?php if ($attributes['displayCommentCount']) : ?> |
| 126 | <!-- Comment Count --> |
| 127 | <span style="font-size:<?php echo esc_attr($attributes['fontSizeCommentCount']); ?>px; color:<?php echo esc_attr($attributes['colorCommentCount']); ?>;"> |
| 128 | <?php echo esc_html(get_comment_count($post['ID'])['approved'] . " " . __("comment(s)", "superb-blocks")); ?> |
| 129 | </span> |
| 130 | <?php endif; ?> |
| 131 | </div> |
| 132 | </div> |
| 133 | </<?php echo esc_html($wrapperTag) ?>> |
| 134 | </li> |
| 135 | <?php |
| 136 | } |
| 137 | ?> |
| 138 | </ul> |
| 139 | </div> |
| 140 | <?php return ob_get_clean(); |
| 141 | } |
| 142 | } |
| 143 |