Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.00% covered (success)
96.00%
24 / 25
100.00% covered (success)
100.00%
2 / 2
CRAP
n/a
0 / 0
wpcom_expiry_notices_editor_notice_data
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
5
wpcom_expiry_notices_enqueue_editor_notice_assets
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * Block-editor notice for plans in their final week, in grace, or post-grace.
4 *
5 * Core hides legacy admin notices on block-editor screens, so the banner's
6 * message is carried into the editor's own notices store instead.
7 *
8 * @package automattic/jetpack-mu-wpcom
9 */
10
11use Automattic\Jetpack\Jetpack_Mu_Wpcom\Expiry_Notices\Expiry_Data;
12use Automattic\Jetpack\Jetpack_Mu_Wpcom\Expiry_Notices\Expiry_Notice_Dismiss;
13
14/**
15 * What the editor notice renders from, or null if it shouldn't show.
16 *
17 * The banner's own answer: an editor screen is never the Dashboard, so the
18 * early reminder is already excluded.
19 *
20 * @return array<string,mixed>|null
21 */
22function wpcom_expiry_notices_editor_notice_data(): ?array {
23    $data = wpcom_expiry_notices_banner_data();
24    if ( null === $data ) {
25        return null;
26    }
27
28    $state   = $data['state'];
29    $urls    = $data['urls'];
30    $surface = array(
31        'site-editor' => 'site_editor',
32        'widgets'     => 'widgets',
33    )[ wpcom_expiry_notices_current_screen_id() ] ?? 'post_editor';
34
35    return array(
36        'metaKey'       => Expiry_Notice_Dismiss::banner_meta_key(),
37        'content'       => wpcom_expiry_notices_banner_sentence( $state, $data['is_owner'] ),
38        'primary'       => null === $urls ? null : $urls['primary'],
39        'secondary'     => null !== $urls && Expiry_Data::STATE_EXPIRED_GRACE === $state['state'] ? $urls['secondary'] : null,
40        'isDismissible' => $data['is_dismissible'],
41        'surface'       => $surface,
42        'trackProps'    => wpcom_expiry_notices_track_props( $state, $data['is_owner'], $surface ),
43    );
44}
45
46/**
47 * Enqueue the editor notice's JS.
48 *
49 * The Customizer fires enqueue_block_editor_assets too but renders no store
50 * notices, hence the positive screen check.
51 */
52function wpcom_expiry_notices_enqueue_editor_notice_assets() {
53    if ( ! wpcom_expiry_notices_is_block_editor_screen() ) {
54        return;
55    }
56    $data = wpcom_expiry_notices_editor_notice_data();
57    if ( null === $data ) {
58        return;
59    }
60    wpcom_expiry_notices_enqueue_surface( 'expiry-notices-editor-notice', 'wpcomExpiryEditorNotice', $data );
61}
62add_action( 'enqueue_block_editor_assets', 'wpcom_expiry_notices_enqueue_editor_notice_assets' );