PluginProbe
Whols – Wholesale Prices and B2B Store Solution for WooCommerce / trunk
Whols – Wholesale Prices and B2B Store Solution for WooCommerce vtrunk
2.4.12 2.4.11 trunk 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 All 55 releases
whols / includes / Admin / class-dashboard-widget-api.php

class-dashboard-widget-api.php in Whols – Wholesale Prices and B2B Store Solution for WooCommerce trunk, at includes/Admin/class-dashboard-widget-api.php

54 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Whols\Admin;
3
4 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
5
6 class Dashboard_Widget_Api {
7
8 /**
9 * Define necessary variables
10 */
11 const REMOTE_BASE_URL = 'https://feed.hasthemes.com/notices/news-feed';
12 // const REMOTE_BASE_URL = 'http://news-feed.test'; // local dev — swap back before release
13 const ENDPOINT_FILE = 'news-data.json';
14 const TRANSIENT_KEY = 'whols_news_feed_data';
15
16 /**
17 * Get news feed data.
18 * Retrieve the banner + feed data from the HasThemes news-feed server.
19 *
20 * @param bool $force_update Optional. Whether to force the data update.
21 * @return array News Feed data ( 'banner' => [...], 'feed' => [...] ).
22 */
23 public static function get_remote_data( $force_update = false ) {
24 $cache_key = self::TRANSIENT_KEY;
25
26 $info_data = get_transient( $cache_key );
27
28 if ( $force_update || false === $info_data ) {
29 $timeout = ( $force_update ) ? 25 : 8;
30
31 $response = wp_remote_get( sprintf( '%s/%s', self::REMOTE_BASE_URL, self::ENDPOINT_FILE ), [
32 'timeout' => $timeout,
33 ] );
34
35 if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) {
36 set_transient( $cache_key, [], HOUR_IN_SECONDS );
37 return [];
38 }
39
40 $info_data = json_decode( wp_remote_retrieve_body( $response ), true );
41
42 if ( empty( $info_data ) || ! is_array( $info_data ) ) {
43 set_transient( $cache_key, [], HOUR_IN_SECONDS );
44 return [];
45 }
46
47 set_transient( $cache_key, $info_data, 12 * HOUR_IN_SECONDS );
48 }
49
50 return empty( $info_data ) ? [] : $info_data;
51 }
52
53 }
54