Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.02% |
40 / 43 |
|
66.67% |
2 / 3 |
CRAP | n/a |
0 / 0 |
|
| wpcom_expiry_notices_admin_banner_data | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| wpcom_expiry_notices_enqueue_admin_banner_assets | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
2.00 | |||
| wpcom_expiry_notices_render_admin_banner | |
100.00% |
28 / 28 |
|
100.00% |
1 / 1 |
8 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Wp-admin banner notice for plans approaching expiry, in grace, or post-grace. |
| 4 | * |
| 5 | * @package automattic/jetpack-mu-wpcom |
| 6 | */ |
| 7 | |
| 8 | use Automattic\Jetpack\Jetpack_Mu_Wpcom\Expiry_Notices\Expiry_Data; |
| 9 | use Automattic\Jetpack\Jetpack_Mu_Wpcom\Expiry_Notices\Expiry_Notice_Dismiss; |
| 10 | |
| 11 | /** |
| 12 | * What the banner renders from, or null if it shouldn't show. |
| 13 | * |
| 14 | * Not on block-editor screens: core hides admin notices there, the editor |
| 15 | * notice carries the message, and the script would still count an impression. |
| 16 | * |
| 17 | * @return array<string,mixed>|null |
| 18 | */ |
| 19 | function wpcom_expiry_notices_admin_banner_data(): ?array { |
| 20 | return wpcom_expiry_notices_is_block_editor_screen() ? null : wpcom_expiry_notices_banner_data(); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Enqueue the banner's JS/CSS on admin_enqueue_scripts so the stylesheet lands in <head>. |
| 25 | */ |
| 26 | function wpcom_expiry_notices_enqueue_admin_banner_assets() { |
| 27 | $data = wpcom_expiry_notices_admin_banner_data(); |
| 28 | if ( null === $data ) { |
| 29 | return; |
| 30 | } |
| 31 | wpcom_expiry_notices_enqueue_surface( |
| 32 | 'expiry-notices-banner', |
| 33 | 'wpcomExpiryBanner', |
| 34 | array( |
| 35 | 'metaKey' => Expiry_Notice_Dismiss::banner_meta_key(), |
| 36 | 'trackProps' => wpcom_expiry_notices_track_props( $data['state'], $data['is_owner'], 'wp_admin' ), |
| 37 | ), |
| 38 | 'expiry-notices-admin-banner' |
| 39 | ); |
| 40 | } |
| 41 | add_action( 'admin_enqueue_scripts', 'wpcom_expiry_notices_enqueue_admin_banner_assets' ); |
| 42 | |
| 43 | /** |
| 44 | * Render the banner on admin_notices. |
| 45 | */ |
| 46 | function wpcom_expiry_notices_render_admin_banner() { |
| 47 | $data = wpcom_expiry_notices_admin_banner_data(); |
| 48 | if ( null === $data ) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | $state = $data['state']; |
| 53 | $urls = $data['urls']; |
| 54 | $is_dismissible = $data['is_dismissible']; |
| 55 | $notice_class = $data['is_early_warning'] ? 'notice-warning' : 'notice-error'; |
| 56 | $is_grace = Expiry_Data::STATE_EXPIRED_GRACE === $state['state']; |
| 57 | ?> |
| 58 | <div id="wpcom-expiry-banner" class="notice <?php echo esc_attr( $notice_class ); ?>" data-wpcom-expiry-banner> |
| 59 | <p><strong><?php echo esc_html( wpcom_expiry_notices_banner_heading( $state ) ); ?></strong></p> |
| 60 | <p><?php echo esc_html( wpcom_expiry_notices_banner_body( $state, $data['is_owner'] ) ); ?></p> |
| 61 | <?php if ( null !== $urls || $is_dismissible ) : ?> |
| 62 | <p class="wpcom-expiry-banner__actions"> |
| 63 | <?php if ( null !== $urls ) : ?> |
| 64 | <?php wpcom_expiry_notices_render_cta_link( $urls['primary'], 'primary', 'button button-primary' ); ?> |
| 65 | <?php if ( $is_grace ) : ?> |
| 66 | <?php wpcom_expiry_notices_render_cta_link( $urls['secondary'], 'secondary', 'button' ); ?> |
| 67 | <?php endif; ?> |
| 68 | <?php endif; ?> |
| 69 | <?php if ( $is_dismissible ) : ?> |
| 70 | <button type="button" class="button wpcom-expiry-banner__dismiss" data-wpcom-expiry-dismiss> |
| 71 | <?php esc_html_e( 'Dismiss', 'jetpack-mu-wpcom' ); ?> |
| 72 | </button> |
| 73 | <?php endif; ?> |
| 74 | </p> |
| 75 | <?php endif; ?> |
| 76 | </div> |
| 77 | <?php |
| 78 | } |
| 79 | add_action( 'admin_notices', 'wpcom_expiry_notices_render_admin_banner' ); |