Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 63
0.00% covered (danger)
0.00%
0 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Activity_Log
0.00% covered (danger)
0.00%
0 / 61
0.00% covered (danger)
0.00%
0 / 10
702
0.00% covered (danger)
0.00%
0 / 1
 initialize
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 add_wp_admin_submenu
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
30
 is_available
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 admin_init
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 load_wp_build
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
 alias_screen_id_for_wp_build
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 enqueue_initial_state
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 render_fallback
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 is_activity_log_admin_request
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 register_rest_routes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Primary class for the Jetpack Activity Log package.
4 *
5 * @package automattic/jetpack-activity-log
6 */
7
8namespace Automattic\Jetpack\Activity_Log;
9
10if ( ! defined( 'ABSPATH' ) ) {
11    exit( 0 );
12}
13
14use Automattic\Jetpack\Activity_Log\Initial_State as Activity_Log_Initial_State;
15use Automattic\Jetpack\Admin_UI\Admin_Menu;
16use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State;
17use Automattic\Jetpack\Connection\Manager as Connection_Manager;
18use Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills;
19use function add_action;
20use function add_filter;
21use function current_user_can;
22use function did_action;
23use function do_action;
24use function is_admin;
25use function is_multisite;
26use function sanitize_text_field;
27use function wp_add_inline_script;
28use function wp_enqueue_script;
29use function wp_register_script;
30use function wp_unslash;
31use function wp_verify_nonce;
32
33/**
34 * Class Jetpack_Activity_Log
35 *
36 * Registers the Activity Log admin page and its REST routes inside the
37 * main Jetpack plugin.
38 */
39class Jetpack_Activity_Log {
40
41    /**
42     * Admin page slug.
43     *
44     * @var string
45     */
46    const PAGE_SLUG = 'jetpack-activity-log';
47
48    /**
49     * Page slug for the wp-build dashboard. Distinct from the wp-admin menu
50     * slug (`PAGE_SLUG`) so the user-facing URL stays `admin.php?page=jetpack-activity-log`;
51     * we alias the current screen id to this value so wp-build's
52     * screen-match enqueue callback fires. Must match the `page` in
53     * `routes/dashboard/package.json` and the `wpPlugin.pages` entry.
54     *
55     * @var string
56     */
57    const WP_BUILD_PAGE_SLUG = 'jetpack-activity-log-dashboard';
58
59    /**
60     * Handle for the classic script that carries the React initial state.
61     * The dashboard is a wp-build script module, so there is no classic
62     * bundle handle to attach inline data to — this empty handle exists
63     * purely to print `JPACTIVITYLOG_INITIAL_STATE` and the Connection
64     * initial state before boot runs.
65     *
66     * @var string
67     */
68    const DATA_SCRIPT_HANDLE = 'jetpack-activity-log-data';
69
70    /**
71     * Nonce action for refreshing the access flag after a checkout
72     * return. Used by `admin_init()` below and exposed to the client via
73     * Initial_State so the upsell CTA can embed a valid nonce in its
74     * `redirect_to`. Same shape as `Social_Admin_Page::REFRESH_PLAN_NONCE_ACTION`.
75     *
76     * @var string
77     */
78    const REFRESH_ACCESS_NONCE_ACTION = 'jetpack_activity_log_refresh_access';
79
80    /**
81     * Entry point. Idempotent: safe to call from multiple bootstraps.
82     */
83    public static function initialize() {
84        if ( did_action( 'jetpack_activity_log_initialized' ) ) {
85            return;
86        }
87
88        add_action( 'admin_menu', array( __CLASS__, 'add_wp_admin_submenu' ) );
89        add_action( 'rest_api_init', array( __CLASS__, 'register_rest_routes' ) );
90        add_filter( 'jetpack_package_versions', array( Package_Version::class, 'send_package_version_to_tracker' ) );
91
92        /**
93         * Fires once the Jetpack Activity Log package has wired its hooks.
94         *
95         * @since 0.1.0
96         */
97        do_action( 'jetpack_activity_log_initialized' );
98    }
99
100    /**
101     * Register the Activity Log submenu under Jetpack.
102     *
103     * Mirrors the gating used by the legacy my-jetpack "Activity Log" menu
104     * item (connected user + non-multisite).
105     *
106     * @return string|null The resulting page's hook suffix, if registered.
107     */
108    public static function add_wp_admin_submenu() {
109        if ( ! self::is_available() ) {
110            return null;
111        }
112
113        // Load wp-build only on the Activity Log request so its generated
114        // render function exists before the menu callback runs, and its
115        // enqueue pipeline/polyfills stay off every other admin page. Alias
116        // the screen id here too — `current_screen` fires before the
117        // `load-{$page_suffix}` hook that runs admin_init(), so registering
118        // the alias from admin_init() would be too late for wp-build's
119        // screen-matched enqueue callback.
120        if ( self::is_activity_log_admin_request() ) {
121            self::load_wp_build();
122            add_action( 'current_screen', array( __CLASS__, 'alias_screen_id_for_wp_build' ) );
123        }
124
125        // The menu item must appear on every admin page, but the generated
126        // render function is only loaded on the Activity Log request (above).
127        // The callback is only ever invoked while rendering our page — where
128        // the function is loaded — so the fallback is purely defensive.
129        $render_callback = function_exists( 'jetpack_activity_log_jetpack_activity_log_dashboard_wp_admin_render_page' )
130            ? 'jetpack_activity_log_jetpack_activity_log_dashboard_wp_admin_render_page'
131            : array( __CLASS__, 'render_fallback' );
132
133        $page_suffix = Admin_Menu::add_menu(
134            /** "Activity Log" is a product name, do not translate. */
135            'Activity Log',
136            'Activity Log',
137            'manage_options',
138            self::PAGE_SLUG,
139            $render_callback
140        );
141
142        if ( $page_suffix ) {
143            add_action( 'load-' . $page_suffix, array( __CLASS__, 'admin_init' ) );
144        }
145
146        return $page_suffix;
147    }
148
149    /**
150     * Whether the Activity Log page should be shown to the current user.
151     *
152     * @return bool
153     */
154    public static function is_available() {
155        if ( is_multisite() ) {
156            return false;
157        }
158
159        if ( ! current_user_can( 'manage_options' ) ) {
160            return false;
161        }
162
163        return ( new Connection_Manager() )->is_user_connected();
164    }
165
166    /**
167     * Fires when the admin page is loaded.
168     *
169     * When the user is returning from a successful checkout, the upsell
170     * CTA appends `?refresh_access=1&_wpnonce=…` to the `redirect_to`
171     * value it hands off to WordPress.com. Detect that here, verify the
172     * nonce, and drop the cached paid-plan signal so
173     * `Initial_State::get_data()` (which runs later in the same request,
174     * when the bundle is enqueued) rehydrates from WPCOM instead of
175     * re-serving the pre-checkout value. Mirrors the pattern in
176     * `Automattic\Jetpack\Publicize\Social_Admin_Page::admin_init()`.
177     */
178    public static function admin_init() {
179        if ( isset( $_GET['refresh_access'] ) && isset( $_GET['_wpnonce'] ) ) {
180            $nonce = sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) );
181            if ( wp_verify_nonce( $nonce, self::REFRESH_ACCESS_NONCE_ACTION ) ) {
182                REST_Controller::clear_access_cache();
183            }
184        }
185
186        add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_initial_state' ) );
187    }
188
189    /**
190     * Require the generated wp-build entry and register the script/module
191     * polyfills the boot bundle depends on.
192     *
193     * The boot bundle depends on `@wordpress/*` handles (e.g. `wp-theme`,
194     * pulled in via `@wordpress/ui`) that Core does not register on older
195     * WordPress versions. Without them WP_Scripts silently drops the bundle
196     * and the page renders blank, so register the polyfills here. Scoped to
197     * the Activity Log request by the sole caller, since the register() call
198     * can force-replace Core handles and must not fire on every admin page.
199     *
200     * @return void
201     */
202    private static function load_wp_build() {
203        $build_index = dirname( __DIR__ ) . '/build/build.php';
204
205        if ( ! file_exists( $build_index ) ) {
206            return;
207        }
208
209        require_once $build_index;
210
211        // The generated `modules.php` registers standalone script modules (the
212        // `@jetpack-activity-log/init` i18n bootstrap) on `wp_default_scripts`.
213        // We load wp-build lazily on `admin_menu`, which can run after that
214        // action has already fired — so the hook may be added too late and the
215        // init module never registers, leaving it out of the import map and
216        // breaking boot. Register directly here (mirroring the polyfills call
217        // below); the generated function guards against double-registration.
218        if ( function_exists( 'jetpack_activity_log_register_script_modules' ) ) {
219            jetpack_activity_log_register_script_modules(); // @phan-suppress-current-line PhanUndeclaredFunction -- Checked with function_exists(); defined in the generated build/modules.php, which Phan excludes.
220        }
221
222        WP_Build_Polyfills::register(
223            'jetpack-activity-log',
224            array_merge(
225                WP_Build_Polyfills::SCRIPT_HANDLES,
226                WP_Build_Polyfills::MODULE_IDS
227            )
228        );
229    }
230
231    /**
232     * Alias the current screen id to the wp-build page slug.
233     *
234     * The wp-build-generated enqueue callback only fires when the screen id
235     * equals the wp-build page slug. Our menu slug stays `jetpack-activity-log`,
236     * so alias the screen id in place to make the check pass without changing
237     * the user-facing URL. Hooked on `current_screen` only for the Activity Log
238     * request, so this never affects any other screen.
239     *
240     * @param \WP_Screen|null $screen The current screen object (passed by WP).
241     * @return void
242     */
243    public static function alias_screen_id_for_wp_build( $screen ) {
244        if ( is_object( $screen ) ) {
245            $screen->id = self::WP_BUILD_PAGE_SLUG;
246        }
247    }
248
249    /**
250     * Print the React initial state and the Connection initial state, and load
251     * the Tracks transport.
252     *
253     * The initial state is attached to a dedicated empty classic handle because
254     * the dashboard is a wp-build script module — there is no classic bundle
255     * handle to hang the inline data on. Boot defers its own execution to
256     * `DOMContentLoaded`, so this inline data is always set on `window` first.
257     *
258     * `jp-tracks` (stats.wp.com/w.js) is required for analytics: the dashboard's
259     * `@automattic/jetpack-analytics` events only queue into `window._tkq`
260     * (the package's own w.js loader is disabled), so without this handle no
261     * `jetpack_activity_log_*` event ever flushes. Mirrors Newsletter's
262     * `Settings::load_admin_scripts()`.
263     *
264     * @return void
265     */
266    public static function enqueue_initial_state() {
267        wp_register_script( self::DATA_SCRIPT_HANDLE, false, array(), Package_Version::PACKAGE_VERSION, true );
268        wp_enqueue_script( self::DATA_SCRIPT_HANDLE );
269
270        wp_add_inline_script( self::DATA_SCRIPT_HANDLE, ( new Activity_Log_Initial_State() )->render(), 'before' );
271        Connection_Initial_State::render_script( self::DATA_SCRIPT_HANDLE );
272
273        wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js', array(), gmdate( 'YW' ), true );
274
275        // The dashboard is a wp-build script module: it externalizes
276        // `@wordpress/i18n` to the shared `wp.i18n` global but has no
277        // `wp_set_script_translations()` equivalent to load its JS catalog.
278        // Enqueue Jetpack's i18n loader (`wp.jpI18nLoader`, from jetpack-assets,
279        // registered on `wp_default_scripts`) so the `@jetpack-activity-log/init`
280        // boot module can fetch and install the translation catalog before the
281        // app renders. Without this the UI ships in English on non-English sites.
282        if ( wp_script_is( 'wp-jp-i18n-loader', 'registered' ) ) {
283            wp_enqueue_script( 'wp-jp-i18n-loader' );
284        }
285    }
286
287    /**
288     * Fallback page body if the generated wp-build render function is
289     * unavailable (e.g. assets not built). Keeps the menu from fataling and
290     * still gives boot its mount container.
291     *
292     * @return void
293     */
294    public static function render_fallback() {
295        echo '<div class="wrap"><div id="jetpack-activity-log-dashboard-wp-admin-app"></div></div>';
296    }
297
298    /**
299     * Whether the current request targets the Activity Log admin page.
300     *
301     * The `$_GET['page']` value is populated by wp-admin/admin.php before any
302     * of our hooks fire, so this check is reliable from `admin_menu` onwards.
303     *
304     * @return bool
305     */
306    private static function is_activity_log_admin_request() {
307        if ( ! is_admin() || ! isset( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
308            return false;
309        }
310
311        return sanitize_text_field( wp_unslash( $_GET['page'] ) ) === self::PAGE_SLUG; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
312    }
313
314    /**
315     * Register the REST routes backing the Activity Log UI.
316     *
317     * Routes are added in Phase 2. This method exists now so that the
318     * `jetpack/v4/activity-log` namespace is reserved and the hook is wired.
319     */
320    public static function register_rest_routes() {
321        REST_Controller::register_rest_routes();
322    }
323}