Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
52 / 52
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
Feed_Announcements
100.00% covered (success)
100.00%
52 / 52
100.00% covered (success)
100.00%
4 / 4
23
100.00% covered (success)
100.00%
1 / 1
 get
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
10
 feed_item_to_announcement
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
5
 get_image
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
7
 to_text
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Announcements read from a feed.
4 *
5 * @package automattic/jetpack-announcements
6 */
7
8namespace Automattic\Jetpack\Announcements;
9
10/**
11 * Turns the items of a feed into announcements a registrant can register.
12 */
13class Feed_Announcements {
14
15    /**
16     * How long a feed that failed is left alone, in seconds. `fetch_feed()` caches no failure.
17     */
18    const FAILURE_PAUSE = HOUR_IN_SECONDS;
19
20    /**
21     * Words the excerpt of an announcement keeps.
22     */
23    const EXCERPT_WORDS = 30;
24
25    /**
26     * Reads a feed and returns its items as announcements.
27     *
28     * @param string $url    URL of the feed.
29     * @param string $vendor First half of the id of each announcement.
30     * @param array  $args   Settings of the read, as `get_announcements_from_feed()` documents them.
31     * @return array[] One entry per item, as `get_announcements_from_feed()` documents it.
32     */
33    public static function get( $url, $vendor, $args = array() ) {
34        if ( ! empty( $args['query'] ) && is_array( $args['query'] ) ) {
35            // `add_query_arg()` encodes nothing.
36            $url = add_query_arg( rawurlencode_deep( $args['query'] ), $url );
37        }
38
39        $failed = 'jetpack_announcements_feed_failed_' . md5( $url );
40
41        // A site transient, as core's cache of the feed is.
42        if ( get_site_transient( $failed ) ) {
43            return array();
44        }
45
46        $feed = fetch_feed( $url );
47
48        if ( is_wp_error( $feed ) ) {
49            set_site_transient( $failed, 1, self::FAILURE_PAUSE );
50            return array();
51        }
52
53        $oldest = isset( $args['max_age'] ) ? time() - (int) $args['max_age'] : null;
54
55        $announcements = array();
56        foreach ( (array) $feed->get_items() as $item ) {
57            $announcement = self::feed_item_to_announcement( $item, $vendor );
58            if ( $announcement && ( null === $oldest || strtotime( $announcement['args']['date'] ) >= $oldest ) ) {
59                $announcements[] = $announcement;
60            }
61        }
62
63        return $announcements;
64    }
65
66    /**
67     * Turns an item of a feed into the data of an announcement: plain text, URLs and a date.
68     *
69     * @param \SimplePie\Item $item   The item.
70     * @param string          $vendor First half of the id.
71     * @return array|null `id`, `args` and `categories`, or null for an item without a title, a date or a link.
72     */
73    private static function feed_item_to_announcement( $item, $vendor ) {
74        $title = self::to_text( $item->get_title() );
75        $date  = (string) $item->get_gmdate( 'c' );
76        $link  = esc_url_raw( (string) $item->get_permalink(), array( 'http', 'https' ) );
77
78        if ( '' === $title || '' === $date || '' === $link ) {
79            return null;
80        }
81
82        $categories = array();
83        foreach ( (array) $item->get_categories() as $category ) {
84            $categories[] = self::to_text( $category->get_label() );
85        }
86
87        return array(
88            // Not `get_id( true )`, which hashes the title and the content: an edit would change the id.
89            'id'         => $vendor . '/' . md5( (string) $item->get_id() ),
90            'args'       => array(
91                'date'    => $date,
92                'title'   => $title,
93                'excerpt' => wp_trim_words( self::to_text( $item->get_description() ), self::EXCERPT_WORDS, '…' ),
94                'link'    => $link,
95                'media'   => self::get_image( $item ),
96            ),
97            'categories' => array_values( array_unique( array_filter( $categories, 'strlen' ) ) ),
98        );
99    }
100
101    /**
102     * The image of an item, when the feed gives one over https.
103     *
104     * @param \SimplePie\Item $item The item.
105     * @return array|null The `media` of an announcement, or null.
106     */
107    private static function get_image( $item ) {
108        $thumbnail = $item->get_thumbnail();
109        $enclosure = $item->get_enclosure();
110        $urls      = array( is_array( $thumbnail ) ? ( $thumbnail['url'] ?? '' ) : '' );
111
112        if ( $enclosure ) {
113            $urls[] = $enclosure->get_thumbnail();
114
115            if ( 'image' === $enclosure->get_medium() || 0 === strpos( (string) $enclosure->get_type(), 'image/' ) ) {
116                $urls[] = $enclosure->get_link();
117            }
118        }
119
120        foreach ( $urls as $url ) {
121            $url = esc_url_raw( (string) $url, array( 'https' ) );
122            if ( '' !== $url ) {
123                return array(
124                    'type' => 'image',
125                    'url'  => $url,
126                    'alt'  => '',
127                );
128            }
129        }
130
131        return null;
132    }
133
134    /**
135     * The plain text of a value of a feed, which may carry markup and encoded entities.
136     *
137     * @param mixed $value The value.
138     * @return string The text.
139     */
140    private static function to_text( $value ) {
141        // Decoded first, so markup that came encoded is stripped as well.
142        return trim( wp_strip_all_tags( html_entity_decode( (string) $value, ENT_QUOTES | ENT_HTML5, 'UTF-8' ), true ) );
143    }
144}