PluginProbe
Ultimate Post Kit – Elementor Post Grid, Post Carousel, Post Slider & Blog Layout Widgets / 4.2.3
Ultimate Post Kit – Elementor Post Grid, Post Carousel, Post Slider & Blog Layout Widgets v4.2.3
4.5.4 4.2.1 4.2.2 4.2.3 4.5.0 4.5.2 4.5.3 4.2.0 4.1.18 4.1.17 4.1.16 4.1.15 4.1.14 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.0.9 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 All 146 releases
ultimate-post-kit / admin / admin-feeds.php

admin-feeds.php in Ultimate Post Kit – Elementor Post Grid, Post Carousel, Post Slider & Blog Layout Widgets 4.2.3, at admin/admin-feeds.php

245 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace UltimatePostKit;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 /**
10 * Class Admin Feeds
11 */
12 class Admin_Feeds {
13
14 private $settings;
15
16 /**
17 * Static variable to track if the feed has been displayed
18 */
19 private static $feed_displayed = false;
20
21 /**
22 * Admin_Feeds constructor.
23 */
24 public function __construct( $settings ) {
25 $this->settings = $settings;
26 add_action( 'wp_dashboard_setup', [ $this, 'register_rss_feeds' ] );
27 }
28
29 /**
30 * Register RSS Feeds for Element Pack
31 */
32 public function register_rss_feeds() {
33 if ( self::$feed_displayed ) {
34 /**
35 * If the feed has already been displayed, do not add it again
36 */
37 return;
38 }
39
40 wp_add_dashboard_widget(
41 'bdt-dashboard-overview',
42 esc_html( $this->settings['feed_title'] ),
43 [ $this, 'display_rss_feeds_content' ],
44 null,
45 null,
46 'column4',
47 'core'
48 );
49
50 /**
51 * Mark the feed as displayed
52 */
53 self::$feed_displayed = true;
54 }
55
56 /**
57 * Display RSS Feeds Content
58 */
59 public function display_rss_feeds_content() {
60 $feeds = $this->get_remote_feeds_data();
61 if ( is_array( $feeds ) ) {
62 foreach ( $feeds as $feed ) {
63 ?>
64 <div class="activity-block">
65 <a href="<?php echo esc_url( $feed->demo_link ); ?>" target="_blank" style="margin-bottom:10px; display: inline-block;">
66 <img src="<?php echo esc_url( $feed->image ); ?>" style="width:100%;min-height:240px;">
67 </a>
68 <p>
69 <?php echo wp_kses_post( wp_trim_words( wp_strip_all_tags( $feed->content ), 50 ) ); ?>
70 <a href="<?php echo esc_url( $feed->demo_link ); ?>" target="_blank">
71 <?php esc_html_e( 'Learn more...', 'ultimate-post-kit' ); ?>
72 </a>
73 </p>
74 </div>
75 <?php
76 }
77 }
78 echo wp_kses_post( $this->get_rss_posts_data() );
79 }
80
81 /**
82 * Get Remote Feeds Data
83 *
84 * @return array|mixed
85 */
86 private function get_remote_feeds_data() {
87 $transient_key = $this->settings['transient_key'];
88 $cached_data = get_transient( $transient_key );
89
90 if ( ! empty( $cached_data ) ) {
91 return json_decode( $cached_data );
92 }
93
94 $response = wp_remote_get( $this->settings['remote_feed_link'],
95 array(
96 'timeout' => 30,
97 'headers' => array(
98 'Accept' => 'application/json',
99 ),
100 )
101 );
102
103 if ( is_wp_error( $response ) ) {
104 return [];
105 }
106
107 $response_body = wp_remote_retrieve_body( $response );
108 set_transient( $transient_key, $response_body, 6 * HOUR_IN_SECONDS );
109
110 return json_decode( $response_body );
111 }
112
113 /**
114 * Get RSS Posts Data
115 *
116 * @return string
117 */
118 private function get_rss_posts_data() {
119 $transient_key = $this->settings['transient_key'] . '_rss';
120 $cached_data = get_transient( $transient_key );
121
122 if ( ! empty( $cached_data ) ) {
123 /**
124 * Decode as associative array
125 */
126 $rss_items = json_decode( $cached_data, true );
127 } else {
128 include_once ABSPATH . WPINC . '/feed.php';
129
130 $rss = fetch_feed( $this->settings['feed_link'] );
131
132 if ( is_wp_error( $rss ) ) {
133 return '<li>' . esc_html__( 'Items Not Found', 'ultimate-post-kit' ) . '.</li>';
134 }
135
136 $maxitems = $rss->get_item_quantity( 5 );
137 $rss_items = $rss->get_items( 0, $maxitems );
138
139 /**
140 * Convert RSS items to a simpler array to avoid serialization issues
141 */
142 $simplified_rss_items = array_map( function ($item) {
143 return [
144 'title' => $item->get_title(),
145 'link' => $item->get_permalink(),
146 'date' => $item->get_date( 'U' ),
147 'content' => $item->get_content(),
148 ];
149 }, $rss_items );
150
151 set_transient( $transient_key, json_encode( $simplified_rss_items ), 6 * HOUR_IN_SECONDS );
152 $rss_items = $simplified_rss_items;
153 }
154
155 ob_start();
156 ?>
157 <div class="bdt-widget">
158 <ul>
159 <?php if ( empty( $rss_items ) ) : ?>
160 <li><?php esc_html_e( 'Items Not Found', 'ultimate-post-kit' ); ?>.</li>
161 <?php else : ?>
162 <?php foreach ( $rss_items as $item ) : ?>
163 <li>
164 <a target="_blank" href="<?php echo esc_url( $item['link'] ); ?>"
165 title="<?php echo esc_html( $item['date'] ); ?>">
166 <?php if ( $this->is_feed_item_new( $item['date'] ) ) : ?>
167 <span class="bdt-feed-badge bdt-feed-badge--new"><?php esc_html_e( 'New', 'ultimate-post-kit' ); ?></span>
168 <?php endif; ?>
169 <?php echo esc_html( $item['title'] ); ?>
170 </a>
171 <span class="bdt-date" style="display: block; margin: 0;">
172 <?php echo esc_html( human_time_diff( $item['date'], current_time( 'timestamp' ) ) . ' ' . __( 'ago', 'ultimate-post-kit' ) ); ?>
173 </span>
174 <div class="bdt-summary">
175 <?php echo esc_html( wp_html_excerpt( $item['content'], 120 ) . ' [...]' ); ?>
176 </div>
177 </li>
178 <?php endforeach; ?>
179 <?php endif; ?>
180 </ul>
181 </div>
182 <p class="community-events-footer" style="margin: 12px -12px 6px -12px; padding: 12px 12px 0px;">
183 <?php
184 foreach ( $this->settings['footer_links'] as $link ) {
185 printf(
186 '<a href="%s" target="_blank">%s <span class="screen-reader-text"> (opens in a new tab)</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a>',
187 esc_url( $link['url'] ),
188 esc_html( $link['title'] )
189 );
190
191 if ( next( $this->settings['footer_links'] ) ) {
192 echo ' | ';
193 }
194 }
195 ?>
196 </p>
197 <?php
198 return ob_get_clean();
199 }
200
201 /**
202 * Check if a feed item is "new" (published within the last 7 days).
203 *
204 * @param int|string $date Unix timestamp.
205 * @return bool
206 */
207 private function is_feed_item_new( $date ) {
208 $timestamp = is_numeric( $date ) ? (int) $date : strtotime( $date );
209 if ( ! $timestamp ) {
210 return false;
211 }
212 $cutoff = time() - ( 7 * DAY_IN_SECONDS );
213 return $timestamp >= $cutoff;
214 }
215 }
216
217 $settings = array(
218 'feed_title' => 'BdThemes News & Updates',
219 'transient_key' => 'bdthemes_product_feeds',
220 'feed_link' => 'https://bdthemes.com/feed',
221 'remote_feed_link' => 'https://dashboard.bdthemes.io/wp-json/bdthemes/v1/product-feed/?product_category=element-pack',
222 'text_domain' => 'bdthemes',
223 'footer_links' => [
224 [
225 'url' => 'https://bdthemes.com/blog/',
226 'title' => 'Blog',
227 ],
228 [
229 'url' => 'https://bdthemes.com/knowledge-base/',
230 'title' => 'Docs',
231 ],
232 [
233 'url' => 'https://store.bdthemes.com/',
234 'title' => 'Get Pro',
235 ],
236 [
237 'url' => 'https://feedback.elementpack.pro/announcements/',
238 'title' => 'Changelog',
239 ],
240 ],
241 );
242
243 new Admin_Feeds( $settings );
244
245