Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/bdthemes-element-pack/admin/admin-feeds.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 +
3 + namespace ElementPack;
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...', 'bdthemes-element-pack' ); ?>
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', 'bdthemes-element-pack' ) . '.</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="rss-widget">
158 + <ul>
159 + <?php if ( empty( $rss_items ) ) : ?>
160 + <li><?php esc_html_e( 'Items Not Found', 'bdthemes-element-pack' ); ?>.</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 echo esc_html( $item['title'] ); ?>
167 + </a>
168 + <span class="rss-date" style="display: block; margin: 0;">
169 + <?php echo esc_html( human_time_diff( $item['date'], current_time( 'timestamp' ) ) . ' ' . __( 'ago', 'bdthemes-element-pack' ) ); ?>
170 + </span>
171 + <div class="rss-summary">
172 + <?php echo esc_html( wp_html_excerpt( $item['content'], 120 ) . ' [...]' ); ?>
173 + </div>
174 + </li>
175 + <?php endforeach; ?>
176 + <?php endif; ?>
177 + </ul>
178 + </div>
179 + <p class="community-events-footer" style="margin: 12px -12px 6px -12px; padding: 12px 12px 0px;">
180 + <?php
181 + foreach ( $this->settings['footer_links'] as $link ) {
182 + printf(
183 + '<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>',
184 + esc_url( $link['url'] ),
185 + esc_html( $link['title'] )
186 + );
187 +
188 + if ( next( $this->settings['footer_links'] ) ) {
189 + echo ' | ';
190 + }
191 + }
192 + ?>
193 + </p>
194 + <?php
195 + return ob_get_clean();
196 + }
197 + }
198 +
199 + $settings = array(
200 + 'feed_title' => 'BdThemes News & Updates',
201 + 'transient_key' => 'bdthemes_product_feeds',
202 + 'feed_link' => 'https://bdthemes.com/feed',
203 + 'remote_feed_link' => 'https://dashboard.bdthemes.io/wp-json/bdthemes/v1/product-feed/?product_category=element-pack',
204 + 'text_domain' => 'bdthemes-element-pack',
205 + 'footer_links' => [
206 + [
207 + 'url' => 'https://bdthemes.com/blog/',
208 + 'title' => 'Blog',
209 + ],
210 + [
211 + 'url' => 'https://bdthemes.com/knowledge-base/',
212 + 'title' => 'Docs',
213 + ],
214 + [
215 + 'url' => 'https://store.bdthemes.com/',
216 + 'title' => 'Get Pro',
217 + ],
218 + [
219 + 'url' => 'https://feedback.elementpack.pro/announcements/',
220 + 'title' => 'Changelog',
221 + ],
222 + ],
223 + );
224 +
225 + new Admin_Feeds( $settings );
226 +