Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
81.82% |
45 / 55 |
|
25.00% |
1 / 4 |
CRAP | n/a |
0 / 0 |
|
| wpcom_expiry_notices_admin_modal_data | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
9 | |||
| wpcom_expiry_notices_modal_items | |
80.95% |
17 / 21 |
|
0.00% |
0 / 1 |
4.11 | |||
| wpcom_expiry_notices_enqueue_admin_modal_assets | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
2.01 | |||
| wpcom_expiry_notices_render_admin_modal_root | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Wp-admin modal for plans that have expired, in grace or after it. |
| 4 | * |
| 5 | * Copy lives here rather than in the React that renders it, because this |
| 6 | * package extracts PHP strings for translation and not JS. |
| 7 | * |
| 8 | * @package automattic/jetpack-mu-wpcom |
| 9 | */ |
| 10 | |
| 11 | use Automattic\Jetpack\Jetpack_Mu_Wpcom\Expiry_Notices\Expiry_Data; |
| 12 | use Automattic\Jetpack\Jetpack_Mu_Wpcom\Expiry_Notices\Expiry_Domain; |
| 13 | use Automattic\Jetpack\Jetpack_Mu_Wpcom\Expiry_Notices\Expiry_Notice_Dismiss; |
| 14 | use Automattic\Jetpack\Jetpack_Mu_Wpcom\Expiry_Notices\Expiry_Owner; |
| 15 | |
| 16 | /** |
| 17 | * What the modal renders from, or null if it shouldn't show. |
| 18 | * |
| 19 | * Owners only: the modal exists to interrupt someone into acting, and an admin |
| 20 | * who cannot renew has nothing to act on. The banner tells them why. |
| 21 | * |
| 22 | * @return array<string,mixed>|null |
| 23 | */ |
| 24 | function wpcom_expiry_notices_admin_modal_data(): ?array { |
| 25 | $state = wpcom_expiry_notices_eligible_state(); |
| 26 | if ( null === $state |
| 27 | || ! wpcom_expiry_notices_revert_applies_to_site( $state ) |
| 28 | || ! Expiry_Notice_Dismiss::should_show_modal( $state ) |
| 29 | || ! Expiry_Owner::current_user_is_owner( $state ) ) { |
| 30 | return null; |
| 31 | } |
| 32 | |
| 33 | $is_grace = Expiry_Data::STATE_EXPIRED_GRACE === $state['state']; |
| 34 | $urls = Expiry_Data::get_cta_urls( $state, wpcom_expiry_notices_current_url() ); |
| 35 | |
| 36 | return array( |
| 37 | 'state' => $state, |
| 38 | 'metaKey' => Expiry_Notice_Dismiss::modal_meta_key( $state ), |
| 39 | 'title' => wpcom_expiry_notices_expired_heading( $state ), |
| 40 | 'description' => $is_grace |
| 41 | ? __( 'Your site will be moved to the Free plan. We will also make these changes to your site:', 'jetpack-mu-wpcom' ) |
| 42 | : __( 'Your site has been moved to the Free plan and set to private. Contact support to get help restoring it.', 'jetpack-mu-wpcom' ), |
| 43 | 'listIntro' => $is_grace ? '' : __( 'Here’s what changed:', 'jetpack-mu-wpcom' ), |
| 44 | 'items' => wpcom_expiry_notices_modal_items( $is_grace ), |
| 45 | 'primary' => $is_grace ? $urls['primary'] : wpcom_expiry_notices_support_cta( $state ), |
| 46 | // Nothing to compare against once the site is already on Free. |
| 47 | 'secondary' => $is_grace ? $urls['secondary'] : null, |
| 48 | 'imageUrl' => plugins_url( 'images/plan-expired.svg', __FILE__ ), |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * The listed changes, in the tense the site's state calls for. |
| 54 | * |
| 55 | * @param bool $is_grace Whether the site is still inside the grace period. |
| 56 | * @return array<int,string> |
| 57 | */ |
| 58 | function wpcom_expiry_notices_modal_items( bool $is_grace ): array { |
| 59 | $domain = Expiry_Domain::get_revert_domain(); |
| 60 | |
| 61 | if ( $is_grace ) { |
| 62 | $items = array(); |
| 63 | if ( null !== $domain ) { |
| 64 | $items[] = sprintf( |
| 65 | /* translators: %s is a WordPress.com domain name (e.g. example.wordpress.com). */ |
| 66 | __( 'Use %s as your primary domain.', 'jetpack-mu-wpcom' ), |
| 67 | $domain |
| 68 | ); |
| 69 | } |
| 70 | $items[] = __( 'Remove your installed themes, plugins, and their data.', 'jetpack-mu-wpcom' ); |
| 71 | $items[] = __( 'Switch to the settings and theme you had before you upgraded.', 'jetpack-mu-wpcom' ); |
| 72 | $items[] = __( 'Your site will be set to private.', 'jetpack-mu-wpcom' ); |
| 73 | return $items; |
| 74 | } |
| 75 | |
| 76 | $items = array( __( 'Your site is now private.', 'jetpack-mu-wpcom' ) ); |
| 77 | if ( null !== $domain ) { |
| 78 | $items[] = sprintf( |
| 79 | /* translators: %s is a WordPress.com domain name (e.g. example.wordpress.com). */ |
| 80 | __( 'Your primary domain was switched to %s.', 'jetpack-mu-wpcom' ), |
| 81 | $domain |
| 82 | ); |
| 83 | } |
| 84 | $items[] = __( 'Your installed themes, plugins, and their data were removed from your site.', 'jetpack-mu-wpcom' ); |
| 85 | $items[] = __( 'Your settings and theme reverted to what you had before upgrading.', 'jetpack-mu-wpcom' ); |
| 86 | return $items; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Enqueue the modal's JS/CSS. |
| 91 | */ |
| 92 | function wpcom_expiry_notices_enqueue_admin_modal_assets() { |
| 93 | $data = wpcom_expiry_notices_admin_modal_data(); |
| 94 | if ( null === $data ) { |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | $state = $data['state']; |
| 99 | unset( $data['state'] ); |
| 100 | $data['trackProps'] = wpcom_expiry_notices_track_props( $state, true, 'wp_admin' ); |
| 101 | |
| 102 | // The bundle declares the components scripts but not their stylesheet. |
| 103 | wp_enqueue_style( 'wp-components' ); |
| 104 | wpcom_expiry_notices_enqueue_surface( 'expiry-notices-admin-modal', 'wpcomExpiryModal', $data, 'expiry-notices-admin-modal' ); |
| 105 | } |
| 106 | add_action( 'admin_enqueue_scripts', 'wpcom_expiry_notices_enqueue_admin_modal_assets' ); |
| 107 | |
| 108 | /** |
| 109 | * Render the element the modal mounts into. |
| 110 | */ |
| 111 | function wpcom_expiry_notices_render_admin_modal_root() { |
| 112 | if ( null === wpcom_expiry_notices_admin_modal_data() ) { |
| 113 | return; |
| 114 | } |
| 115 | echo '<div id="wpcom-expiry-modal-root"></div>'; |
| 116 | } |
| 117 | add_action( 'admin_footer', 'wpcom_expiry_notices_render_admin_modal_root' ); |