Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
43.33% covered (danger)
43.33%
13 / 30
75.00% covered (warning)
75.00%
3 / 4
CRAP
n/a
0 / 0
jetpack_wpcomreader_feed_id
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
jetpack_wpcomreader_track_set_up_posts
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
jetpack_wpcomreader_set_up_post_id
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
jetpack_wpcomreader_post_id
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * This provides minor tweaks to improve the experience for Jetpack feed in the WordPress.com Reader.
4 *
5 * This does not make sites available in the Reader—that depends on the public access to /feed/ as a method on the WP.com side
6 * to check if a site is public. It also does not add any content to the feed. Any content that should not be displayed in the Reader
7 * or other RSS readers should be filtered out elsewhere.
8 *
9 * These hooks were originally part of the now-deprecated Enhanced Distribution.
10 *
11 * @since 13.3
12 * @package Automattic/jetpack
13 */
14
15use Automattic\Jetpack\Connection\Manager as Connection_Manager;
16use Automattic\Jetpack\Status;
17use Automattic\Jetpack\Status\Host;
18
19if ( ! defined( 'ABSPATH' ) ) {
20    exit( 0 );
21}
22
23foreach ( array( 'rss_head', 'rss1_head', 'rss2_head' ) as $rss_head_action ) {
24    add_action( $rss_head_action, 'jetpack_wpcomreader_feed_id' );
25    add_action( $rss_head_action, 'jetpack_wpcomreader_track_set_up_posts' );
26}
27foreach ( array( 'rss_item', 'rss1_item', 'rss2_item' ) as $rss_item_action ) {
28    add_action( $rss_item_action, 'jetpack_wpcomreader_post_id' );
29}
30
31/**
32 * Output feed identifier based on blog ID.
33 *
34 * @return void
35 */
36function jetpack_wpcomreader_feed_id() {
37    if (
38        ( new Host() )->is_wpcom_simple()
39        || (
40            ( new Connection_Manager() )->is_connected()
41            && ! ( new Status() )->is_offline_mode()
42        )
43    ) {
44        $blog_id = Connection_Manager::get_site_id( true ); // Silence since we're not wanting to handle the error state.
45        if ( ! $blog_id ) {
46            return;
47        }
48
49        printf(
50            '<site xmlns="com-wordpress:feed-additions:1">%d</site>',
51            (int) $blog_id
52        );
53    }
54}
55
56/**
57 * Start tracking the posts set up by the feed's loop.
58 *
59 * @since 16.5
60 *
61 * @return void
62 */
63function jetpack_wpcomreader_track_set_up_posts() {
64    jetpack_wpcomreader_set_up_post_id();
65    add_action( 'the_post', 'jetpack_wpcomreader_set_up_post_id' );
66}
67
68/**
69 * Track the post most recently set up by a loop, and hand its ID out once.
70 *
71 * @since 16.5
72 *
73 * @param WP_Post|null $post Post being set up, or null to read and clear the tracked ID.
74 * @return int ID of the post set up before this call, or 0.
75 */
76function jetpack_wpcomreader_set_up_post_id( $post = null ) {
77    static $set_up_post_id = 0;
78
79    $previous_id    = $set_up_post_id;
80    $set_up_post_id = $post instanceof WP_Post ? (int) $post->ID : 0;
81
82    return $previous_id;
83}
84
85/**
86 * Output feed item identifier based on current post ID.
87 *
88 * Custom feed templates can fire the item actions without setting up a post per item, so the
89 * global post is still the main query's; only print an ID that was set up for this item.
90 *
91 * @return void
92 */
93function jetpack_wpcomreader_post_id() {
94    $id = get_the_ID();
95    if ( ! $id || jetpack_wpcomreader_set_up_post_id() !== $id ) {
96        return;
97    }
98
99    printf(
100        '<post-id xmlns="com-wordpress:feed-additions:1">%d</post-id>',
101        (int) $id
102    );
103}