PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.5
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.5
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
jetpack / jetpack_vendor / automattic / jetpack-stats-admin / src / class-dashboard.php

class-dashboard.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-a.5, at jetpack_vendor/automattic/jetpack-stats-admin/src/class-dashboard.php

199 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * A class that adds a stats dashboard to wp-admin.
4 *
5 * @package automattic/jetpack-stats-admin
6 */
7
8 namespace Automattic\Jetpack\Stats_Admin;
9
10 use Automattic\Jetpack\Admin_UI\Admin_Menu;
11 use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State;
12 use Automattic\Jetpack\Stats\Options as Stats_Options;
13
14 /**
15 * Responsible for adding a stats dashboard to wp-admin.
16 *
17 * @package jetpack-stats-admin
18 */
19 class Dashboard {
20 /**
21 * Whether the class has been initialized
22 *
23 * @var boolean
24 */
25 private static $initialized = false;
26
27 /**
28 * Priority for the dashboard menu
29 * For Jetpack sites: Jetpack uses 998 and 'Admin_Menu' uses 1000, so we need to use 999.
30 *
31 * Admin_Menu registers what it has queued at priority 1000, so this has to stay below it.
32 *
33 * @var int
34 */
35 protected $menu_priority = 999;
36
37 /**
38 * Init Stats dashboard.
39 */
40 public static function init() {
41 if ( ! self::$initialized ) {
42 self::$initialized = true;
43 ( new self() )->init_hooks();
44 }
45 }
46
47 /**
48 * Initialize the hooks.
49 */
50 public function init_hooks() {
51 self::$initialized = true;
52 // Jetpack uses 998 and 'Admin_Menu' uses 1000.
53 add_action( 'admin_menu', array( $this, 'add_wp_admin_menu' ), $this->menu_priority );
54 }
55
56 /**
57 * Add a "Stats" top-level admin menu.
58 *
59 * Declares no `product` gate: that resolves false without the Jetpack plugin, which is
60 * exactly when the standalone Stats plugin registers this page.
61 *
62 * @return void
63 */
64 public function add_wp_admin_menu() {
65 /**
66 * Disable this menu for dashboard.wordpress.com because older versions of Jetpack need to fetch the old Stats UI.
67 *
68 * If this menu is registered, it will conflict with the back-end and break non-odyssey Stats.
69 */
70 if ( defined( 'IS_WPCOM' ) && IS_WPCOM && 120742 === get_current_blog_id() ) {
71 return;
72 }
73
74 $page_title = __( 'Stats', 'jetpack-stats-admin' );
75 $menu_title = _x( 'Stats', 'product name shown in menu', 'jetpack-stats-admin' );
76 $capability = $this->get_capability();
77 $callback = array( $this, 'render' );
78
79 // An older admin-ui, loaded first by another plugin, may predate add_top_level_menu().
80 if ( method_exists( Admin_Menu::class, 'add_top_level_menu' ) ) {
81 // The key the legacy Stats screen in the Jetpack plugin also declares, so hosts name Stats once.
82 $page_suffix = Admin_Menu::add_top_level_menu( $page_title, $menu_title, $capability, 'stats', $callback, 'dashicons-chart-bar', 2, array( 'key' => 'jetpack-stats' ) );
83 } else {
84 $page_suffix = add_menu_page( $page_title, $menu_title, $capability, 'stats', $callback, 'dashicons-chart-bar', 2 );
85 }
86
87 if ( $page_suffix ) {
88 add_action( 'load-' . $page_suffix, array( $this, 'admin_init' ) );
89 // The dashboard renders full bleed, so core notices stacked above it look broken.
90 if ( method_exists( Admin_Menu::class, 'hide_core_admin_notices' ) ) {
91 add_action( 'load-' . $page_suffix, array( Admin_Menu::class, 'hide_core_admin_notices' ) );
92 }
93 }
94 }
95
96 /**
97 * Capability a user needs to reach the dashboard.
98 *
99 * Until the site is connected the page exists to pick a plan and connect, which only a user
100 * who can manage the connection can act on. Once connected it is a reporting page, open to
101 * everyone allowed to view stats.
102 *
103 * Pre-connection that is `jetpack_connect`: it maps to `manage_options` on a normal site,
104 * but is `do_not_allow` in offline mode and honours the same multisite/filter rules as the
105 * register endpoint, so the menu is not offered where the connection cannot be completed.
106 *
107 * @return string
108 */
109 protected function get_capability() {
110 return Main::is_site_connected() ? 'view_stats' : 'jetpack_connect';
111 }
112
113 /**
114 * Override render funtion
115 */
116 public function render() {
117 // Record the number of views of the stats dashboard on the initial several loads for the
118 // purpose of showing feedback notice. Views before the site is connected show the plan
119 // choice rather than the dashboard, and there is nothing to give feedback on yet.
120 $views = intval( Stats_Options::get_option( 'views' ) ) + 1;
121 if ( $views <= Notices::VIEWS_TO_SHOW_FEEDBACK && Main::is_site_connected() ) {
122 Stats_Options::set_option( 'views', $views );
123 }
124
125 ?>
126 <div id="wpcom" class="jp-stats-dashboard" style="min-height: calc(100vh - 100px);">
127 <div class="hide-if-js"><?php esc_html_e( 'Your Jetpack Stats dashboard requires JavaScript to function properly.', 'jetpack-stats-admin' ); ?></div>
128 <div class="hide-if-no-js" style="height: 100%">
129 <img
130 class="jp-stats-dashboard-loading-spinner"
131 width="32"
132 height="32"
133 style="position: absolute; left: 50%; top: 50%;"
134 alt=<?php echo esc_attr( __( 'Loading', 'jetpack-stats-admin' ) ); ?>
135 src="//en.wordpress.com/i/loading/loading-64.gif"
136 />
137 </div>
138 </div>
139 <?php
140 }
141
142 /**
143 * The dashboard bootstrap: load the icon sprite, and keep in-app links inside the dashboard.
144 *
145 * @return string
146 */
147 private function get_bootstrap_script() {
148 return <<<'JS'
149 jQuery(document).ready(function($) {
150 // Load SVG sprite.
151 $.get("https://widgets.wp.com/odyssey-stats/common/gridicons-506499ddac13811fee8e.svg", function(data) {
152 var div = document.createElement("div");
153 div.innerHTML = new XMLSerializer().serializeToString(data.documentElement);
154 div.style = 'display: none';
155 document.body.insertBefore(div, document.body.childNodes[0]);
156 });
157 // we intercept on all anchor tags and change it to hashbang style.
158 $("#wpcom").on('click', 'a', function (e) {
159 const link = e && e.currentTarget && e.currentTarget.attributes && e.currentTarget.attributes.href && e.currentTarget.attributes.href.value;
160 if( link && link.startsWith( '/stats' ) ) {
161 location.hash = `#!${link}`;
162 return false;
163 }
164 });
165 });
166 JS;
167 }
168
169 /**
170 * Initialize the admin resources.
171 */
172 public function admin_init() {
173 add_action( 'admin_enqueue_scripts', array( $this, 'load_admin_scripts' ) );
174 }
175
176 /**
177 * Load the admin scripts.
178 */
179 public function load_admin_scripts() {
180 ( new Odyssey_Assets() )->load_admin_scripts( 'jp-stats-dashboard', 'build.min', array( 'config_variable_name' => 'jetpackStatsOdysseyAppConfigData' ) );
181
182 // The bootstrap runs on jQuery, which the Odyssey bundle does not depend on. It gets its own
183 // handle rather than jQuery being added to that bundle, which the dashboard widget shares
184 // and which has no use for it.
185 wp_register_script( 'jp-stats-dashboard-bootstrap', false, array( 'jquery' ), Main::VERSION, true );
186 wp_enqueue_script( 'jp-stats-dashboard-bootstrap' );
187 wp_add_inline_script( 'jp-stats-dashboard-bootstrap', $this->get_bootstrap_script() );
188
189 // The app is served from our CDN and so cannot bundle the connection package. Print the
190 // state Search and Protect print on their own pages, so it can read the connection status
191 // and register the site through the connection REST API itself. Connected sites fetch
192 // `jetpack/v4/connection` over REST instead, and must not receive registrationNonce and
193 // the connected-plugin list on a page that `view_stats` users can open.
194 if ( ! Main::is_site_connected() ) {
195 Connection_Initial_State::render_script( 'jp-stats-dashboard' );
196 }
197 }
198 }
199