|
8 | 8 | Author URI: https://getshifter.io/ |
9 | 9 | */ |
10 | 10 |
|
11 | | -$shifter_github_hosting_autoload = __DIR__ . '/vendor/autoload.php'; |
12 | | - |
13 | | -if ( ! file_exists( $shifter_github_hosting_autoload ) ) { |
14 | | - add_action( 'admin_notices', function() { |
15 | | - printf( |
16 | | - '<div class="notice notice-error"><p>%s</p></div>', |
17 | | - esc_html__( 'Shifter GitHub hosting plugin sample requires Composer dependencies. Please run "composer install" in the plugin directory, or install the plugin from a GitHub Release zip.', 'shifter-github-hosting-plugin-sample' ) |
18 | | - ); |
19 | | - }); |
20 | | - return; |
21 | | -} |
22 | | - |
23 | | -require $shifter_github_hosting_autoload; |
24 | | - |
25 | 11 | add_action( 'admin_notices', function() { |
26 | 12 | // get Shifter News |
27 | 13 | $transient_key = 'shifter-news-posts'; |
28 | | - if (false === ($posts = get_transient($transient_key))) { |
29 | | - $url = 'https://www.getshifter.io/feed/'; |
30 | | - $res = YuzuruS\Rss\Feed::load($url); |
31 | | - |
| 14 | + if ( false === ( $posts = get_transient( $transient_key ) ) ) { |
| 15 | + $url = 'https://www.getshifter.io/feed/'; |
| 16 | + $feed = fetch_feed( $url ); |
| 17 | + |
| 18 | + if ( is_wp_error( $feed ) ) { |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + $items = $feed->get_items( 0, 10 ); |
32 | 23 | $posts = []; |
33 | | - foreach ($res['item'] as $r) { |
| 24 | + foreach ( $items as $item ) { |
34 | 25 | $posts[] = sprintf( |
35 | 26 | '<a href="%s" title="%s">%s</a>', |
36 | | - esc_url_raw($r['link']), |
37 | | - esc_attr($r['title']), |
38 | | - $r['title'] |
| 27 | + esc_url_raw( $item->get_permalink() ), |
| 28 | + esc_attr( $item->get_title() ), |
| 29 | + $item->get_title() |
39 | 30 | ); |
40 | 31 | } |
41 | | - set_transient($transient_key, $posts, HOUR_IN_SECONDS); |
| 32 | + |
| 33 | + if ( empty( $posts ) ) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + set_transient( $transient_key, $posts, HOUR_IN_SECONDS ); |
42 | 38 | } |
| 39 | + |
| 40 | + if ( empty( $posts ) ) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
43 | 44 | $shifter_news = $posts[ mt_rand( 0, count( $posts ) - 1 ) ]; |
44 | 45 |
|
45 | 46 | printf( |
|
0 commit comments