| 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 |
|
| 8 |
namespace Automattic\Jetpack\Newsletter; |
| 9 |
|
| 10 |
use Automattic\Jetpack\Connection\Urls; |
| 11 |
use Automattic\Jetpack\Modules; |
| 12 |
use Automattic\Jetpack\Status\Host; |
| 13 |
use WP_Admin_Bar; |
| 14 |
|
| 15 |
/** |
| 16 |
* Add a link to the WordPress.com Reader to the admin bar. |
| 17 |
*/ |
| 18 |
class 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() || ! is_admin_bar_showing() ) { |
| 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 |
$build_path = dirname( __DIR__ ) . '/build/reader-link.css'; |
| 63 |
if ( ! file_exists( $build_path ) ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
$asset_file = dirname( __DIR__ ) . '/build/reader-link.asset.php'; |
| 68 |
$version = file_exists( $asset_file ) |
| 69 |
? include $asset_file |
| 70 |
: array( 'version' => filemtime( $build_path ) ); |
| 71 |
|
| 72 |
wp_enqueue_style( |
| 73 |
'jetpack-newsletter-reader-link', |
| 74 |
plugins_url( '../build/reader-link.css', __FILE__ ), |
| 75 |
array(), |
| 76 |
$version['version'] ?? filemtime( $build_path ) |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Add the Reader menu. |
| 82 |
* |
| 83 |
* Hook into 'admin_bar_menu' to add to the wp-admin bar. |
| 84 |
* |
| 85 |
* @since 0.4.0 |
| 86 |
* |
| 87 |
* @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar core object. |
| 88 |
*/ |
| 89 |
public function add_reader_menu( $wp_admin_bar ) { |
| 90 |
$reader_menu_settings = array( |
| 91 |
'id' => 'reader', |
| 92 |
'title' => '<span class="ab-icon" title="' . __( 'Read the blogs and topics you follow', 'jetpack-newsletter' ) . '" aria-hidden="true"></span>' . |
| 93 |
'<span class="ab-label">' . __( 'Reader', 'jetpack-newsletter' ) . '</span>', |
| 94 |
'href' => method_exists( Urls::class, 'maybe_add_origin_site_id' ) |
| 95 |
? Urls::maybe_add_origin_site_id( 'https://wordpress.com/reader' ) |
| 96 |
: 'https://wordpress.com/reader', |
| 97 |
'meta' => array( |
| 98 |
'class' => 'wp-admin-bar-reader', |
| 99 |
), |
| 100 |
'parent' => 'top-secondary', |
| 101 |
); |
| 102 |
|
| 103 |
/* |
| 104 |
* On self-hosted sites, open the Reader link in a new tab |
| 105 |
* since they're not necessarily logged in to WordPress.com |
| 106 |
* and may not want to navigate away from their site. |
| 107 |
*/ |
| 108 |
if ( ! ( new Host() )->is_wpcom_platform() ) { |
| 109 |
$reader_menu_settings['meta']['target'] = '_blank'; |
| 110 |
} |
| 111 |
|
| 112 |
$wp_admin_bar->add_menu( $reader_menu_settings ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Activate the wpcom-reader module when a site is first connected to WordPress.com. |
| 117 |
* |
| 118 |
* Only activates on truly fresh connections. If modules were previously initialized |
| 119 |
* (e.g., the user disconnected and reconnected), we respect their prior module choices. |
| 120 |
* |
| 121 |
* @since 0.4.0 |
| 122 |
*/ |
| 123 |
public static function activate_on_connection() { |
| 124 |
if ( \Jetpack_Options::get_option( 'active_modules_initialized' ) ) { |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
( new Modules() )->activate( 'wpcom-reader', false, false ); |
| 129 |
} |
| 130 |
} |
| 131 |
|