Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
25.93% |
7 / 27 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Surface_Visibility | |
25.93% |
7 / 27 |
|
60.00% |
3 / 5 |
26.92 | |
0.00% |
0 / 1 |
| is_visible | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| is_optin_available | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| inject_optin_availability | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| register_optin_route | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
| handle_optin | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * SEO dashboard visibility and legacy opt-in compatibility. |
| 4 | * |
| 5 | * @package automattic/jetpack-seo-package |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\SEO; |
| 9 | |
| 10 | use Automattic\Jetpack\Modules; |
| 11 | |
| 12 | /** |
| 13 | * Makes the dashboard discoverable while retaining the legacy opt-in API. |
| 14 | */ |
| 15 | class Surface_Visibility { |
| 16 | |
| 17 | /** |
| 18 | * Whether the dashboard is available, regardless of an older opt-in preference. |
| 19 | * |
| 20 | * @return bool |
| 21 | */ |
| 22 | public static function is_visible() { |
| 23 | return Initializer::is_available(); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Retain the discovery signal without offering a redundant opt-in. |
| 28 | * |
| 29 | * @return bool |
| 30 | */ |
| 31 | public static function is_optin_available() { |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Expose legacy opt-in and dashboard visibility state to other admin screens. |
| 37 | * |
| 38 | * @param array $data Script data being injected onto the page. |
| 39 | * @return array |
| 40 | */ |
| 41 | public static function inject_optin_availability( $data ) { |
| 42 | if ( ! is_array( $data ) ) { |
| 43 | $data = array(); |
| 44 | } |
| 45 | |
| 46 | $data[ Initializer::SCRIPT_DATA_KEY ]['optin_available'] = self::is_optin_available(); |
| 47 | // Eligible sites use the dashboard; VIP keeps its existing Traffic controls. |
| 48 | $data[ Initializer::SCRIPT_DATA_KEY ]['surface_visible'] = self::is_visible(); |
| 49 | |
| 50 | return $data; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Retain the opt-in REST route for older clients that still offer migration. |
| 55 | * |
| 56 | * An explicit request still preserves its activation contract. Migration itself does |
| 57 | * not call this route or change module choices. See {@see self::handle_optin()}. |
| 58 | * |
| 59 | * @return void |
| 60 | */ |
| 61 | public static function register_optin_route() { |
| 62 | register_rest_route( |
| 63 | 'jetpack/v4', |
| 64 | '/seo/opt-in', |
| 65 | array( |
| 66 | 'methods' => \WP_REST_Server::CREATABLE, |
| 67 | 'callback' => array( __CLASS__, 'handle_optin' ), |
| 68 | 'permission_callback' => function () { |
| 69 | return current_user_can( 'manage_options' ); |
| 70 | }, |
| 71 | ) |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Opt an existing install into the new SEO experience: mark the surface visible and |
| 77 | * activate the `seo-tools` module, then hand back the dashboard URL to redirect to. |
| 78 | * |
| 79 | * Idempotent — re-opting-in is harmless. `Modules::activate()` is called with |
| 80 | * `$exit = false, $redirect = false`; the defaults would `exit()` and send a 302, |
| 81 | * which break a REST response. |
| 82 | * |
| 83 | * @return \WP_REST_Response |
| 84 | */ |
| 85 | public static function handle_optin() { |
| 86 | update_option( Initializer::VISIBILITY_OPTION, true ); |
| 87 | |
| 88 | if ( class_exists( 'Automattic\\Jetpack\\Modules' ) ) { |
| 89 | ( new Modules() )->activate( 'seo-tools', false, false ); |
| 90 | } |
| 91 | |
| 92 | return rest_ensure_response( |
| 93 | array( |
| 94 | 'success' => true, |
| 95 | 'redirect' => Admin_Page::get_url(), |
| 96 | ) |
| 97 | ); |
| 98 | } |
| 99 | } |