Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
35.48% covered (danger)
35.48%
11 / 31
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Surface_Visibility
35.48% covered (danger)
35.48%
11 / 31
60.00% covered (warning)
60.00%
3 / 5
43.49
0.00% covered (danger)
0.00%
0 / 1
 is_visible
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 is_optin_available
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 inject_optin_availability
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 register_optin_route
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 handle_optin
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * The discoverability cohort for the Jetpack SEO surface, and the opt-in
4 * that flips it: whether the admin menu registers on this site, and the
5 * REST route existing self-hosted installs use to switch over.
6 *
7 * @package automattic/jetpack-seo-package
8 */
9
10namespace Automattic\Jetpack\SEO;
11
12use Automattic\Jetpack\Modules;
13use Automattic\Jetpack\Status\Host;
14
15/**
16 * Decides whether the SEO surface is discoverable and handles the opt-in.
17 */
18class Surface_Visibility {
19
20    /**
21     * Whether the Jetpack SEO surface should be discoverable (admin menu registered).
22     *
23     * WordPress.com sites (Simple + Atomic) are always discoverable — how SEO presents
24     * there is a Dotcom decision, independent of the self-hosted rollout. On self-hosted
25     * sites the durable {@see Initializer::VISIBILITY_OPTION} cohort flag decides: fresh installs
26     * are seeded visible, existing installs stay hidden until they opt in. Defaults to
27     * hidden when the option is absent (e.g. before the plugin's seed has run), so an
28     * existing site is never surprised by the new surface before its cohort is recorded.
29     *
30     * @return bool
31     */
32    public static function is_visible() {
33        if ( ! Initializer::is_available() ) {
34            return false;
35        }
36
37        if ( class_exists( 'Automattic\\Jetpack\\Status\\Host' ) && ( new Host() )->is_wpcom_platform() ) {
38            return true;
39        }
40
41        return (bool) get_option( Initializer::VISIBILITY_OPTION, false );
42    }
43
44    /**
45     * Whether to offer an existing install the chance to opt into the new SEO experience.
46     *
47     * The single source of truth for the opt-in surfaces (legacy Traffic-page banner, My
48     * Jetpack card). True only when the SEO product is available and the surface isn't
49     * visible yet — and since {@see self::is_visible()} already returns true for
50     * WordPress.com and for self-hosted installs that have opted in, "not visible"
51     * cleanly means "a self-hosted install that hasn't opted in".
52     *
53     * @return bool
54     */
55    public static function is_optin_available() {
56        return Initializer::is_available() && ! self::is_visible();
57    }
58
59    /**
60     * Expose SEO opt-in and surface visibility state to other admin screens.
61     *
62     * @param array $data Script data being injected onto the page.
63     * @return array
64     */
65    public static function inject_optin_availability( $data ) {
66        if ( ! is_array( $data ) ) {
67            $data = array();
68        }
69
70        $data[ Initializer::SCRIPT_DATA_KEY ]['optin_available'] = self::is_optin_available();
71        // Read by the legacy Traffic page to hide its SEO / Sitemaps sections once the
72        // site is on the new experience (fresh install / opted-in / WordPress.com), so the
73        // two surfaces never show at once. The legacy sections stay for self-hosted installs
74        // that haven't opted in.
75        $data[ Initializer::SCRIPT_DATA_KEY ]['surface_visible'] = self::is_visible();
76
77        return $data;
78    }
79
80    /**
81     * Register the opt-in REST route that switches an existing self-hosted install over to
82     * the new SEO experience.
83     *
84     * Lives on the `jetpack/v4` namespace and is registered ahead of the cohort gate, so a
85     * site whose SEO surface is still hidden can reach it from the legacy Traffic page or
86     * My Jetpack. See {@see self::handle_optin()}.
87     *
88     * @return void
89     */
90    public static function register_optin_route() {
91        register_rest_route(
92            'jetpack/v4',
93            '/seo/opt-in',
94            array(
95                'methods'             => \WP_REST_Server::CREATABLE,
96                'callback'            => array( __CLASS__, 'handle_optin' ),
97                'permission_callback' => function () {
98                    return current_user_can( 'manage_options' );
99                },
100            )
101        );
102    }
103
104    /**
105     * Opt an existing install into the new SEO experience: mark the surface visible and
106     * activate the `seo-tools` module, then hand back the dashboard URL to redirect to.
107     *
108     * Idempotent — re-opting-in is harmless. `Modules::activate()` is called with
109     * `$exit = false, $redirect = false`; the defaults would `exit()` and send a 302,
110     * which break a REST response.
111     *
112     * @return \WP_REST_Response
113     */
114    public static function handle_optin() {
115        update_option( Initializer::VISIBILITY_OPTION, true );
116
117        if ( class_exists( 'Automattic\\Jetpack\\Modules' ) ) {
118            ( new Modules() )->activate( 'seo-tools', false, false );
119        }
120
121        return rest_ensure_response(
122            array(
123                'success'  => true,
124                'redirect' => Admin_Page::get_url(),
125            )
126        );
127    }
128}