Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.00% covered (warning)
85.00%
34 / 40
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Reader_Link
85.00% covered (warning)
85.00%
34 / 40
25.00% covered (danger)
25.00%
1 / 4
11.41
0.00% covered (danger)
0.00%
0 / 1
 init
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
3.01
 enqueue_stylesheet
86.67% covered (warning)
86.67%
13 / 15
0.00% covered (danger)
0.00%
0 / 1
4.04
 add_reader_menu
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
2
 activate_on_connection
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * A class that adds a link to the WordPress.com Reader to the admin bar.
4 *
5 * @package automattic/jetpack-newsletter
6 */
7
8namespace Automattic\Jetpack\Newsletter;
9
10use Automattic\Jetpack\Modules;
11use Automattic\Jetpack\Status\Host;
12use WP_Admin_Bar;
13
14/**
15 * Add a link to the WordPress.com Reader to the admin bar.
16 */
17class Reader_Link {
18    /**
19     * Whether the class has been initialized.
20     *
21     * @var bool
22     */
23    private static $initialized = false;
24
25    /**
26     * Initialize the Reader Link functionality.
27     *
28     * This method sets up all necessary hooks for the Reader menu item
29     * and its associated styles. It can be called multiple times safely
30     * as it will only initialize once.
31     *
32     * @since 0.4.0
33     *
34     * @return void
35     */
36    public static function init() {
37        if ( self::$initialized ) {
38            return;
39        }
40
41        if ( ! is_user_logged_in() ) {
42            return;
43        }
44
45        self::$initialized = true;
46
47        $instance = new self();
48        add_action( 'admin_bar_menu', array( $instance, 'add_reader_menu' ), 11 );
49        add_action( 'wp_enqueue_scripts', array( $instance, 'enqueue_stylesheet' ) );
50        add_action( 'admin_enqueue_scripts', array( $instance, 'enqueue_stylesheet' ) );
51    }
52
53    /**
54     * Enqueue the stylesheet used to display the Reader icon.
55     *
56     * @since 0.4.0
57     *
58     * @return void
59     */
60    public function enqueue_stylesheet() {
61        if ( ! is_admin_bar_showing() ) {
62            return;
63        }
64
65        $build_path = dirname( __DIR__ ) . '/build/reader-link.css';
66        if ( ! file_exists( $build_path ) ) {
67            return;
68        }
69
70        $asset_file = dirname( __DIR__ ) . '/build/reader-link.asset.php';
71        $version    = file_exists( $asset_file )
72            ? include $asset_file
73            : array( 'version' => filemtime( $build_path ) );
74
75        wp_enqueue_style(
76            'jetpack-newsletter-reader-link',
77            plugins_url( '../build/reader-link.css', __FILE__ ),
78            array(),
79            $version['version'] ?? filemtime( $build_path )
80        );
81    }
82
83    /**
84     * Add the Reader menu.
85     *
86     * Hook into 'admin_bar_menu' to add to the wp-admin bar.
87     *
88     * @since 0.4.0
89     *
90     * @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar core object.
91     */
92    public function add_reader_menu( $wp_admin_bar ) {
93        $reader_menu_settings = array(
94            'id'     => 'reader',
95            'title'  => '<span class="ab-icon" title="' . __( 'Read the blogs and topics you follow', 'jetpack-newsletter' ) . '" aria-hidden="true"></span>' .
96                        '<span class="ab-label">' . __( 'Reader', 'jetpack-newsletter' ) . '</span>',
97            'href'   => 'https://wordpress.com/reader',
98            'meta'   => array(
99                'class' => 'wp-admin-bar-reader',
100            ),
101            'parent' => 'top-secondary',
102        );
103
104        /*
105         * On self-hosted sites, open the Reader link in a new tab
106         * since they're not necessarily logged in to WordPress.com
107         * and may not want to navigate away from their site.
108         */
109        if ( ! ( new Host() )->is_wpcom_platform() ) {
110            $reader_menu_settings['meta']['target'] = '_blank';
111        }
112
113        $wp_admin_bar->add_menu( $reader_menu_settings );
114    }
115
116    /**
117     * Activate the wpcom-reader module when a site is first connected to WordPress.com.
118     *
119     * Only activates on truly fresh connections. If modules were previously initialized
120     * (e.g., the user disconnected and reconnected), we respect their prior module choices.
121     *
122     * @since 0.4.0
123     */
124    public static function activate_on_connection() {
125        if ( \Jetpack_Options::get_option( 'active_modules_initialized' ) ) {
126            return;
127        }
128
129        ( new Modules() )->activate( 'wpcom-reader', false, false );
130    }
131}