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