| 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 |
echo wp_kses_post( $this->get_rss_posts_data() ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Get RSS Posts Data |
| 65 |
* |
| 66 |
* @return string |
| 67 |
*/ |
| 68 |
private function get_rss_posts_data() { |
| 69 |
$transient_key = $this->settings['transient_key'] . '_rss'; |
| 70 |
$cached_data = get_transient( $transient_key ); |
| 71 |
|
| 72 |
if ( ! empty( $cached_data ) ) { |
| 73 |
/** |
| 74 |
* Decode as associative array |
| 75 |
*/ |
| 76 |
$rss_items = json_decode( $cached_data, true ); |
| 77 |
} else { |
| 78 |
include_once ABSPATH . WPINC . '/feed.php'; |
| 79 |
|
| 80 |
$rss = fetch_feed( $this->settings['feed_link'] ); |
| 81 |
|
| 82 |
if ( is_wp_error( $rss ) ) { |
| 83 |
return '<li>' . esc_html__( 'Items Not Found', 'ultimate-post-kit' ) . '.</li>'; |
| 84 |
} |
| 85 |
|
| 86 |
$maxitems = $rss->get_item_quantity( 5 ); |
| 87 |
$rss_items = $rss->get_items( 0, $maxitems ); |
| 88 |
|
| 89 |
/** |
| 90 |
* Convert RSS items to a simpler array to avoid serialization issues |
| 91 |
*/ |
| 92 |
$simplified_rss_items = array_map( function ($item) { |
| 93 |
return [ |
| 94 |
'title' => $item->get_title(), |
| 95 |
'link' => $item->get_permalink(), |
| 96 |
'date' => $item->get_date( 'U' ), |
| 97 |
'content' => $item->get_content(), |
| 98 |
]; |
| 99 |
}, $rss_items ); |
| 100 |
|
| 101 |
set_transient( $transient_key, json_encode( $simplified_rss_items ), 6 * HOUR_IN_SECONDS ); |
| 102 |
$rss_items = $simplified_rss_items; |
| 103 |
} |
| 104 |
|
| 105 |
ob_start(); |
| 106 |
?> |
| 107 |
<div class="bdt-widget"> |
| 108 |
<ul> |
| 109 |
<?php if ( empty( $rss_items ) ) : ?> |
| 110 |
<li><?php esc_html_e( 'Items Not Found', 'ultimate-post-kit' ); ?>.</li> |
| 111 |
<?php else : ?> |
| 112 |
<?php foreach ( $rss_items as $item ) : ?> |
| 113 |
<li> |
| 114 |
<a target="_blank" href="<?php echo esc_url( $item['link'] ); ?>" |
| 115 |
title="<?php echo esc_html( $item['date'] ); ?>"> |
| 116 |
<?php if ( $this->is_feed_item_new( $item['date'] ) ) : ?> |
| 117 |
<span class="bdt-feed-badge bdt-feed-badge--new"><?php esc_html_e( 'New', 'ultimate-post-kit' ); ?></span> |
| 118 |
<?php endif; ?> |
| 119 |
<?php echo esc_html( $item['title'] ); ?> |
| 120 |
</a> |
| 121 |
<span class="bdt-date" style="display: block; margin: 0;"> |
| 122 |
<?php echo esc_html( human_time_diff( $item['date'], current_time( 'timestamp' ) ) . ' ' . __( 'ago', 'ultimate-post-kit' ) ); ?> |
| 123 |
</span> |
| 124 |
<div class="bdt-summary"> |
| 125 |
<?php echo esc_html( wp_html_excerpt( $item['content'], 120 ) . ' [...]' ); ?> |
| 126 |
</div> |
| 127 |
</li> |
| 128 |
<?php endforeach; ?> |
| 129 |
<?php endif; ?> |
| 130 |
</ul> |
| 131 |
</div> |
| 132 |
<p class="community-events-footer" style="margin: 12px -12px 6px -12px; padding: 12px 12px 0px;"> |
| 133 |
<?php |
| 134 |
foreach ( $this->settings['footer_links'] as $link ) { |
| 135 |
printf( |
| 136 |
'<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>', |
| 137 |
esc_url( $link['url'] ), |
| 138 |
esc_html( $link['title'] ) |
| 139 |
); |
| 140 |
|
| 141 |
if ( next( $this->settings['footer_links'] ) ) { |
| 142 |
echo ' | '; |
| 143 |
} |
| 144 |
} |
| 145 |
?> |
| 146 |
</p> |
| 147 |
<?php |
| 148 |
return ob_get_clean(); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Check if a feed item is "new" (published within the last 7 days). |
| 153 |
* |
| 154 |
* @param int|string $date Unix timestamp. |
| 155 |
* @return bool |
| 156 |
*/ |
| 157 |
private function is_feed_item_new( $date ) { |
| 158 |
$timestamp = is_numeric( $date ) ? (int) $date : strtotime( $date ); |
| 159 |
if ( ! $timestamp ) { |
| 160 |
return false; |
| 161 |
} |
| 162 |
$cutoff = time() - ( 7 * DAY_IN_SECONDS ); |
| 163 |
return $timestamp >= $cutoff; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- local settings array for this admin feed file. |
| 168 |
$settings = array( |
| 169 |
'feed_title' => 'BdThemes News & Updates', |
| 170 |
'transient_key' => 'bdthemes_product_feeds', |
| 171 |
'feed_link' => 'https://bdthemes.com/feed', |
| 172 |
'text_domain' => 'bdthemes', |
| 173 |
'footer_links' => [ |
| 174 |
[ |
| 175 |
'url' => 'https://bdthemes.com/blog/', |
| 176 |
'title' => 'Blog', |
| 177 |
], |
| 178 |
[ |
| 179 |
'url' => 'https://bdthemes.com/knowledge-base/', |
| 180 |
'title' => 'Docs', |
| 181 |
], |
| 182 |
[ |
| 183 |
'url' => 'https://store.bdthemes.com/', |
| 184 |
'title' => 'Get Pro', |
| 185 |
], |
| 186 |
[ |
| 187 |
'url' => 'https://feedback.elementpack.pro/announcements/', |
| 188 |
'title' => 'Changelog', |
| 189 |
], |
| 190 |
], |
| 191 |
); |
| 192 |
|
| 193 |
/** |
| 194 |
* The dashboard news widget contacts an external BdThemes server, so it is strictly |
| 195 |
* opt-in: it stays disabled until the site administrator enables "BdThemes News & |
| 196 |
* Updates" under Ultimate Post Kit > Other Settings. No request is made otherwise. |
| 197 |
*/ |
| 198 |
if ( 'on' === \ultimate_post_kit_option( 'news_feed', 'ultimate_post_kit_other_settings', 'off' ) ) { |
| 199 |
new Admin_Feeds( $settings ); |
| 200 |
} |
| 201 |
|
| 202 |
|