PluginProbe ʕ •ᴥ•ʔ
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More / 4.0.0
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More v4.0.0
4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 4.0.0 trunk 1.0.0 2.0.0 2.0.1 2.0.2 2.0.3 3.0 3.0.1 3.0.2 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.2 3.1.3 3.2.0 3.2.1 3.2.2 3.2.4 3.2.5 3.2.7 3.2.8 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.4.5 3.4.6 3.5.0 3.5.1 3.5.2 3.5.3 3.5.4 3.5.6 3.5.7 3.5.8 3.5.9 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1
superb-blocks / src / gutenberg / block-api / class-recent-posts-controller.php
superb-blocks / src / gutenberg / block-api Last commit date
class-author-box-controller.php 1 month ago class-dynamic-block-assets.php 1 month ago class-recent-posts-controller.php 1 month ago class-table-of-contents-controller.php 1 month ago
class-recent-posts-controller.php
193 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 /**
59 * Legacy defaults for blocks saved before the native-color migration.
60 * Dynamic blocks cannot use WordPress' deprecated/migrate pipeline
61 * (no save function = validation never fails), so we fall back here
62 * when a block has no WPC slug and no explicit raw value.
63 */
64 private static $color_legacy_defaults = array(
65 'colorTitle' => '#444444',
66 'colorExcerpt' => '#7C7C7C',
67 'colorMeta' => '#7C7C7C',
68 'colorCommentCount' => '#7C7C7C',
69 );
70
71 /**
72 * Resolve a color value: prefer WPC slug as CSS custom property,
73 * then explicit raw value, then legacy default for backwards compat.
74 */
75 private static function resolveColor($attributes, $attrName)
76 {
77 $wpc = isset($attributes[$attrName . 'WPC']) ? $attributes[$attrName . 'WPC'] : '';
78 $raw = isset($attributes[$attrName]) ? $attributes[$attrName] : '';
79 if (!empty($wpc)) {
80 return 'var(--wp--preset--color--' . esc_attr($wpc) . ')';
81 }
82 if (!empty($raw)) {
83 return esc_attr($raw);
84 }
85 return isset(self::$color_legacy_defaults[$attrName]) ? esc_attr(self::$color_legacy_defaults[$attrName]) : '';
86 }
87
88 private static function Render($attributes, $recent_posts)
89 {
90 ob_start();
91
92 // If no posts found
93 if (count($recent_posts) <= 0) : ?>
94 <div class="superbaddons-recentposts-wrapper">
95 <p style="font-weight:500;"><?php echo esc_html__("No posts found", "superb-blocks"); ?></p>
96 </div>
97 <?php return ob_get_clean();
98 endif;
99
100 // If posts found
101 ?>
102 <div class="superbaddons-recentposts-wrapper superbaddons-recentposts-alignment-<?php echo esc_attr($attributes['toolbarAlignment']); ?>">
103 <ul class="superbaddons-recentposts-list">
104 <?php
105 $wrapperTag = $attributes['IsInEditor'] ? 'span' : 'a';
106 foreach ($recent_posts as $post) {
107 $permalink = $attributes['IsInEditor'] ? "#" : get_permalink($post['ID']);
108 $the_post_title = $post['post_title'] === '' ? $post['post_name'] : $post['post_title'];
109 $temp_thumbnail_url = get_the_post_thumbnail_url($post['ID'], array($attributes['thumbnailSize'], $attributes['thumbnailSize']));
110 $thumbnail_url = !$temp_thumbnail_url ? SUPERBADDONS_ASSETS_PATH . '/img/post-thumbnail-placeholder.png' : $temp_thumbnail_url;
111 ?>
112 <li class="superbaddons-recentposts-item">
113 <<?php echo esc_html($wrapperTag); ?> href="<?php echo esc_url($permalink) ?>" <?php echo $attributes['linksTargetBlank'] && !$attributes['IsInEditor'] ? 'target="_blank"' : '' ?>>
114 <div class="superbaddons-recentposts-item-inner">
115 <?php if ($attributes['displayThumbnails'] && ($attributes['displayPlaceholderThumbnails'] || $temp_thumbnail_url !== false)) : ?>
116 <div class="superbaddons-recentposts-item-left">
117 <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) . '%;"' : ""; ?> />
118 </div>
119 <?php endif; ?>
120 <div class="superbaddons-recentposts-item-right">
121 <?php if ($attributes['displayDate'] || $attributes['displayAuthor']) : ?>
122 <?php
123 // Meta
124 $colorMeta = self::resolveColor($attributes, 'colorMeta');
125 $metaStyle = 'font-size:' . esc_attr($attributes['fontSizeMeta']) . 'px;';
126 if ($colorMeta) $metaStyle .= ' color:' . $colorMeta . ';';
127 ?>
128 <span style="<?php echo esc_attr($metaStyle); ?>">
129 <?php if ($attributes['displayDate']) : ?>
130 <time class="superbaddons-recentposts-item-date">
131 <?php echo esc_html(get_the_date(get_option('date_format', 'F j, Y'), $post['ID'])); ?>
132 </time>
133 <?php endif; ?>
134 <?php if ($attributes['displayAuthor']) : ?>
135 <span class="superbaddons-recentposts-item-author">
136 <?php echo esc_html(__("by", "superb-blocks") . " " . get_the_author_meta($attributes['useAuthorDisplayName'] ? 'display_name' : 'user_nicename', $post['post_author'])); ?>
137 </span>
138 <?php endif; ?>
139 </span>
140 <?php endif; ?>
141
142 <?php
143 // Title
144 $colorTitle = self::resolveColor($attributes, 'colorTitle');
145 $titleStyle = 'font-size:' . esc_attr($attributes['fontSizeTitle']) . 'px;';
146 if ($colorTitle) $titleStyle .= ' color:' . $colorTitle . ';';
147 ?>
148 <span style="<?php echo esc_attr($titleStyle); ?>"><?php echo esc_html($the_post_title); ?></span>
149
150 <?php if ($attributes['displayExcerpt']) : ?>
151 <?php
152 // Excerpt
153 $colorExcerpt = self::resolveColor($attributes, 'colorExcerpt');
154 $excerptStyle = 'font-size:' . esc_attr($attributes['fontSizeExcerpt']) . 'px;';
155 if ($colorExcerpt) $excerptStyle .= ' color:' . $colorExcerpt . ';';
156 ?>
157 <span style="<?php echo esc_attr($excerptStyle); ?>">
158 <?php echo esc_html(
159 wp_trim_words(
160 excerpt_remove_blocks(strip_shortcodes($post['post_content'])),
161 $attributes['excerptLength'],
162 // Can't apply this filter using wp_trim_excerpt() as we want to apply the users custom excerpt length without affecting general excerpts.
163 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
164 apply_filters('excerpt_more', ' ' . '[&hellip;]')
165 )
166 ); ?>
167 </span>
168 <?php endif; ?>
169
170 <?php if ($attributes['displayCommentCount']) : ?>
171 <?php
172 // Comment Count
173 $colorCommentCount = self::resolveColor($attributes, 'colorCommentCount');
174 $commentStyle = 'font-size:' . esc_attr($attributes['fontSizeCommentCount']) . 'px;';
175 if ($colorCommentCount) $commentStyle .= ' color:' . $colorCommentCount . ';';
176 ?>
177 <span style="<?php echo esc_attr($commentStyle); ?>">
178 <?php echo esc_html(get_comment_count($post['ID'])['approved'] . " " . __("comment(s)", "superb-blocks")); ?>
179 </span>
180 <?php endif; ?>
181 </div>
182 </div>
183 </<?php echo esc_html($wrapperTag) ?>>
184 </li>
185 <?php
186 }
187 ?>
188 </ul>
189 </div>
190 <?php return ob_get_clean();
191 }
192 }
193