Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.75% covered (success)
93.75%
15 / 16
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Whats_New
93.75% covered (success)
93.75%
15 / 16
66.67% covered (warning)
66.67%
2 / 3
6.01
0.00% covered (danger)
0.00%
0 / 1
 init
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is_enabled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 register
92.86% covered (success)
92.86%
13 / 14
0.00% covered (danger)
0.00%
0 / 1
4.01
1<?php
2/**
3 * What VideoPress publishes on the What's new surface it hosts.
4 *
5 * @package automattic/jetpack-videopress
6 */
7
8namespace Automattic\Jetpack\VideoPress;
9
10use Automattic\Jetpack\Feature_Flags\Feature_Flags;
11use function Automattic\Jetpack\Announcements\get_announcements_from_feed;
12use function Automattic\Jetpack\Announcements\register_announcement;
13
14/**
15 * Publishes the posts of Jetpack.com about VideoPress as announcements of VideoPress.
16 */
17class Whats_New {
18
19    /**
20     * Flag that turns What's new on, in every Jetpack product that shows it. My Jetpack registers it here.
21     */
22    const FLAG = 'jetpack-whats-new';
23
24    /**
25     * Channel the VideoPress pages subscribe to, and the first half of the ids VideoPress registers.
26     */
27    const CHANNEL = 'videopress';
28
29    /**
30     * The feed the announcements come from.
31     */
32    const FEED_URL = 'https://jetpack.com/feed/';
33
34    /**
35     * What the feed is asked for: the posts in both categories that carry the tag.
36     *
37     * A `+` between two slugs asks for the posts in both. A comma would ask for the posts in either.
38     */
39    const FEED_QUERY = array(
40        'category_name' => 'jetpack-news+videopress',
41        'tag'           => 'videopress',
42    );
43
44    /**
45     * How old a post may be to count as news, in seconds.
46     */
47    const MAX_AGE = 90 * DAY_IN_SECONDS;
48
49    /**
50     * Hook the registrant.
51     *
52     * @return void
53     */
54    public static function init() {
55        add_action( 'jetpack_register_announcements', array( __CLASS__, 'register' ) );
56    }
57
58    /**
59     * Whether VideoPress shows What's new.
60     *
61     * @return bool
62     */
63    public static function is_enabled() {
64        return Feature_Flags::is_enabled( self::FLAG );
65    }
66
67    /**
68     * Register the recent posts of Jetpack.com about VideoPress, while the flag is on.
69     *
70     * @return void
71     */
72    public static function register() {
73        if ( ! self::is_enabled() ) {
74            return;
75        }
76
77        // Another plugin may load an older copy of the package first.
78        if ( ! function_exists( 'Automattic\\Jetpack\\Announcements\\get_announcements_from_feed' ) ) {
79            return;
80        }
81
82        $announcements = get_announcements_from_feed(
83            self::FEED_URL,
84            self::CHANNEL,
85            array(
86                'max_age' => self::MAX_AGE,
87                'query'   => self::FEED_QUERY,
88            )
89        );
90
91        foreach ( $announcements as $announcement ) {
92            register_announcement( $announcement['id'], $announcement['args'] + array( 'channels' => array( self::CHANNEL ) ) );
93        }
94    }
95}