Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.06% covered (success)
95.06%
154 / 162
81.48% covered (warning)
81.48%
22 / 27
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_Hosting_Feature_Page
95.65% covered (success)
95.65%
154 / 161
81.48% covered (warning)
81.48%
22 / 27
86
0.00% covered (danger)
0.00%
0 / 1
 has_feature
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 has_atomic_feature
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 site_has_feature
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
4.25
 is_atomic
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 init
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 get_rest_route
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 register_rest_routes
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 can_read_feature_status
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_feature_status
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 is_checkout_pending
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
4
 should_register
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 get_page_title
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_menu_title
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_initial_state
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
7
 load_atomic_lib
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 is_transfer_in_progress
75.00% covered (warning)
75.00%
6 / 8
0.00% covered (danger)
0.00%
0 / 1
5.39
 get_latest_transfer_status
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
6
 get_eligibility
60.00% covered (warning)
60.00%
3 / 5
0.00% covered (danger)
0.00%
0 / 1
3.58
 get_state
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
5
 get_transfer_errors
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
6
 get_transfer_warnings
94.74% covered (success)
94.74%
18 / 19
0.00% covered (danger)
0.00%
0 / 1
11.02
 get_support_url
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 get_upgrade_url
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 get_plan_upgrade_url
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
5
 get_live_feature_url
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 get_post_transfer_host
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 get_activate_url
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Shared base for in-wp-admin pages that prompt WordPress.com Simple and WoA sites
4 * to upgrade, or to transfer to WoA, before a hosting feature can run.
5 *
6 * @package automattic/jetpack-mu-wpcom
7 */
8
9namespace Automattic\Jetpack\Jetpack_Mu_Wpcom;
10
11use Automattic\Jetpack\Constants;
12
13require_once __DIR__ . '/../../common/class-wpcom-wp-build-page.php';
14
15/**
16 * Registers a hosting feature's admin page and its wp-build assets.
17 *
18 * Each page subclasses this, overriding the page constants.
19 */
20abstract class WPCOM_Hosting_Feature_Page extends WPCOM_WP_Build_Page {
21
22    /**
23     * Document title of the page. A product name, so not translated.
24     */
25    const PAGE_TITLE = '';
26
27    /**
28     * Menu entry label. A product name, so not translated.
29     */
30    const MENU_TITLE = '';
31
32    /**
33     * Name of the `WPCOM_Features` constant the site's plan must include.
34     */
35    const FEATURE = '';
36
37    /**
38     * Calypso flow that initiates the transfer, reports progress and returns here.
39     */
40    const TRANSFER_FLOW_URL = 'https://wordpress.com/setup/transferring-hosted-site';
41
42    /**
43     * Tracks context recorded for the WoA transfer.
44     */
45    const TRANSFER_CONTEXT = 'jetpack_product_activation';
46
47    /**
48     * Shared by every hosting feature page, whose boot code is shared too.
49     */
50    const INITIAL_STATE_VAR = 'wpcomHostingFeatureInitialState';
51
52    /**
53     * The pages record their prompts in Tracks.
54     */
55    const ENQUEUE_TRACKS = true;
56
57    /**
58     * Query arg the plan upgrade flow returns a WoA buyer with, set to CHECKOUT_COMPLETE.
59     */
60    const CHECKOUT_ARG = 'checkout';
61
62    /**
63     * Value of CHECKOUT_ARG once checkout has succeeded.
64     */
65    const CHECKOUT_COMPLETE = 'complete';
66
67    /**
68     * Site has no plan that includes the feature. Offer an upgrade.
69     */
70    const STATE_UPGRADE = 'upgrade';
71
72    /**
73     * A transfer to WoA is already running or queued, or on WoA the dashboard waits for the connection. Offer nothing; report status.
74     */
75    const STATE_IN_PROGRESS = 'in_progress';
76
77    /**
78     * Site has the plan and can transfer. Offer activation.
79     */
80    const STATE_ACTIVATE = 'activate';
81
82    /**
83     * A WoA site has the plan, but the Jetpack plugin's dashboard for it is turned off. Offer to turn it on.
84     */
85    const STATE_ENABLE = 'enable';
86
87    /**
88     * Whether the site's plan includes FEATURE. Without one it offers the upgrade, the one
89     * prompt that is never wrong for a site of unknown plan.
90     *
91     * @return bool
92     */
93    public static function has_feature() {
94        return self::site_has_feature( static::FEATURE );
95    }
96
97    /**
98     * Whether the site's plan includes WordPress.com's Atomic hosting, which these features run on.
99     *
100     * @return bool
101     */
102    public static function has_atomic_feature() {
103        return self::site_has_feature( 'ATOMIC' );
104    }
105
106    /**
107     * Whether the site's plan includes a WordPress.com feature.
108     *
109     * @param string $name Name of a `WPCOM_Features` constant.
110     * @return bool
111     */
112    private static function site_has_feature( $name ) {
113        $constant = '\WPCOM_Features::' . $name;
114
115        if ( '' === $name || ! function_exists( 'wpcom_site_has_feature' ) || ! defined( $constant ) ) {
116            return false;
117        }
118
119        // Called without a blog ID: on WoA the local ID is not the WordPress.com one, so wpcom resolves the current site.
120        return (bool) wpcom_site_has_feature( constant( $constant ) );
121    }
122
123    /**
124     * Whether the site runs on WordPress.com's Atomic infrastructure.
125     *
126     * Read through Constants rather than `defined()` so tests can exercise both
127     * platforms in one process.
128     *
129     * @return bool
130     */
131    public static function is_atomic() {
132        return Constants::is_true( 'IS_ATOMIC' );
133    }
134
135    /**
136     * Boot the page, plus, on WoA, the route its post-checkout screen polls.
137     *
138     * @return void
139     */
140    public static function init() {
141        // Registered even when the page steps aside, so a poll still in flight learns the feature arrived.
142        if ( self::is_atomic() ) {
143            add_action( 'rest_api_init', array( static::class, 'register_rest_routes' ) );
144        }
145
146        parent::init();
147    }
148
149    /**
150     * The route reporting whether the plan includes FEATURE yet, relative to `wpcom/v2`.
151     *
152     * @return string
153     */
154    public static function get_rest_route() {
155        return '/hosting-feature-page/' . static::MENU_SLUG;
156    }
157
158    /**
159     * Register the route that reports whether the plan includes FEATURE yet.
160     *
161     * @return void
162     */
163    public static function register_rest_routes() {
164        register_rest_route(
165            'wpcom/v2',
166            static::get_rest_route(),
167            array(
168                'methods'             => \WP_REST_Server::READABLE,
169                'callback'            => array( static::class, 'get_feature_status' ),
170                'permission_callback' => array( static::class, 'can_read_feature_status' ),
171            )
172        );
173    }
174
175    /**
176     * Whether the current user may poll the feature status, as they may see the page.
177     *
178     * @return bool
179     */
180    public static function can_read_feature_status() {
181        return current_user_can( static::CAPABILITY );
182    }
183
184    /**
185     * Whether the plan includes FEATURE, read from the site's own copy of its purchases, which the page renders from.
186     *
187     * Not WordPress.com's, which knows about a purchase before the copy synced to WoA does.
188     *
189     * @return \WP_REST_Response
190     */
191    public static function get_feature_status() {
192        $has_feature = static::has_feature();
193
194        // The Jetpack dashboard the page reloads into reads Jetpack's stored plan, which can lag that copy.
195        if ( $has_feature && method_exists( '\Automattic\Jetpack\Current_Plan', 'refresh_from_wpcom' ) ) {
196            \Automattic\Jetpack\Current_Plan::refresh_from_wpcom( array( 'timeout' => 5 ) );
197        }
198
199        return rest_ensure_response( array( 'hasFeature' => $has_feature ) );
200    }
201
202    /**
203     * Whether a WoA buyer just returned from checkout to a page that has not seen the purchase yet.
204     *
205     * @param string $state The resolved STATE_* constant.
206     * @return bool
207     */
208    public static function is_checkout_pending( $state ) {
209        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only flag set by the plan upgrade flow's return address.
210        $checkout = isset( $_GET[ self::CHECKOUT_ARG ] ) ? sanitize_key( wp_unslash( $_GET[ self::CHECKOUT_ARG ] ) ) : '';
211
212        return self::STATE_UPGRADE === $state && self::is_atomic() && self::CHECKOUT_COMPLETE === $checkout;
213    }
214
215    /**
216     * Whether this page should claim its slug at all.
217     *
218     * A plan that includes the feature only makes it live on Atomic infrastructure, so on
219     * WoA an entitled site already has it and this page steps aside. A Simple site is never
220     * Atomic, so the page always applies there.
221     *
222     * @return bool
223     */
224    public static function should_register() {
225        if ( ! self::is_atomic() ) {
226            return true;
227        }
228
229        return ! static::has_feature();
230    }
231
232    /**
233     * The page title.
234     *
235     * @return string
236     */
237    public static function get_page_title() {
238        return static::PAGE_TITLE;
239    }
240
241    /**
242     * The menu entry's label.
243     *
244     * @return string
245     */
246    public static function get_menu_title() {
247        return static::MENU_TITLE;
248    }
249
250    /**
251     * The resolved prompt and everything it links to.
252     *
253     * @return array
254     */
255    public static function get_initial_state() {
256        $blog_id = get_current_blog_id();
257        $domain  = wp_parse_url( home_url(), PHP_URL_HOST );
258        $state   = static::get_state( $blog_id );
259
260        // Only the activation prompt reads eligibility, and both lists come from the
261        // same result, so resolve it once and only for that state.
262        $eligibility     = self::STATE_ACTIVATE === $state
263            ? self::get_eligibility( $blog_id, get_current_user_id() )
264            : null;
265        $warnings        = self::get_transfer_warnings( $eligibility );
266        $transfer_status = self::STATE_IN_PROGRESS === $state ? null : self::get_latest_transfer_status( $blog_id );
267
268        $initial_state = array(
269            'state'               => $state,
270            'domain'              => $domain,
271            // A null result means the library was unavailable, not that the site
272            // failed a check; let the transfer flow reject it instead.
273            'isEligible'          => null === $eligibility || ! empty( $eligibility['is_eligible'] ),
274            'errors'              => self::get_transfer_errors( $eligibility ),
275            'warnings'            => $warnings,
276            'upgradeUrl'          => static::get_upgrade_url( $domain ),
277            'activateUrl'         => static::get_activate_url( $warnings ),
278            'supportUrl'          => self::get_support_url(),
279            // Each applies only to the prompt Calypso pairs it with.
280            'hasRevertedTransfer' => self::STATE_UPGRADE === $state && 'reverted' === $transfer_status,
281            'transferFailed'      => self::STATE_ACTIVATE === $state && 'error' === $transfer_status,
282            'checkoutPending'     => static::is_checkout_pending( $state ),
283        );
284
285        if ( $initial_state['checkoutPending'] ) {
286            $initial_state['featureStatusPath'] = '/wpcom/v2' . static::get_rest_route();
287        }
288
289        return $initial_state;
290    }
291
292    /**
293     * Load wpcom's Atomic library for the transfer checks, which callers still guard with function_exists().
294     *
295     * @return bool False on WoA, where there is nothing left to transfer.
296     */
297    private static function load_atomic_lib() {
298        if ( self::is_atomic() ) {
299            return false;
300        }
301
302        if ( function_exists( 'require_lib' ) ) {
303            require_lib( 'atomic' );
304        }
305
306        return true;
307    }
308
309    /**
310     * Whether a transfer to WoA is already underway.
311     *
312     * @param int $blog_id Blog ID.
313     * @return bool
314     */
315    public static function is_transfer_in_progress( $blog_id ) {
316        if ( ! self::load_atomic_lib() ) {
317            return false;
318        }
319
320        if ( function_exists( '\A8C\Atomic\has_site_pending_automated_transfer' ) ) {
321            if ( \A8C\Atomic\has_site_pending_automated_transfer( $blog_id ) ) {
322                return true;
323            }
324        }
325
326        if ( function_exists( '\A8C\Atomic\is_wpcom_atomic' ) ) {
327            // Second argument includes pending/active/provisioned transfers.
328            return (bool) \A8C\Atomic\is_wpcom_atomic( $blog_id, true );
329        }
330
331        return false;
332    }
333
334    /**
335     * Status of the site's latest transfer to WoA, or null when it has none.
336     *
337     * @param int $blog_id Blog ID.
338     * @return string|null A transfer record status, such as `reverted` or `error`.
339     */
340    public static function get_latest_transfer_status( $blog_id ) {
341        if ( ! self::load_atomic_lib() || ! function_exists( 'woa_get_latest_transfer' ) ) {
342            return null;
343        }
344
345        $transfer = woa_get_latest_transfer( $blog_id );
346
347        if ( is_wp_error( $transfer ) || ! is_object( $transfer ) || ! isset( $transfer->status ) ) {
348            return null;
349        }
350
351        return (string) $transfer->status;
352    }
353
354    /**
355     * Transfer eligibility for the site, or null when it cannot be determined.
356     *
357     * An in-progress transfer is reported as a `transfer_already_exists` error, so
358     * that state is checked first and never reaches here as a blocker.
359     *
360     * @param int $blog_id Blog ID.
361     * @param int $user_id User ID.
362     * @return array|null { is_eligible: bool, errors: array, warnings: array }
363     */
364    public static function get_eligibility( $blog_id, $user_id ) {
365        if ( ! self::load_atomic_lib() ) {
366            return null;
367        }
368
369        if ( ! function_exists( '\A8C\Atomic\Eligibility\get_status_for_site' ) ) {
370            return null;
371        }
372
373        return \A8C\Atomic\Eligibility\get_status_for_site( $blog_id, $user_id );
374    }
375
376    /**
377     * Resolve which prompt the page should show.
378     *
379     * @param int $blog_id Blog ID.
380     * @return string One of the STATE_* constants.
381     */
382    public static function get_state( $blog_id ) {
383        // On WoA the plan is the only question: the site is already on the
384        // infrastructure that runs the feature, so there is nothing to transfer, and
385        // should_register() has already stepped the page aside if it has the plan.
386        // On Simple, activation also needs the hosting platform, as Calypso requires.
387        if ( ! static::has_feature() || ! self::has_atomic_feature() || self::is_atomic() ) {
388            $state = self::STATE_UPGRADE;
389        } elseif ( self::is_transfer_in_progress( $blog_id ) ) {
390            $state = self::STATE_IN_PROGRESS;
391        } else {
392            // Eligibility failures are explained by the activation prompt's own
393            // confirmation step, so they do not get a state of their own.
394            $state = self::STATE_ACTIVATE;
395        }
396
397        return $state;
398    }
399
400    /**
401     * Reasons a site cannot be transferred, in the shape the API returns them.
402     *
403     * The code is kept because the page maps it to its own copy; the API's message comes
404     * along as the fallback the page renders for codes it does not recognize.
405     *
406     * @param array|null $eligibility Result of get_eligibility().
407     * @return array[] Errors as { code, message }.
408     */
409    public static function get_transfer_errors( $eligibility ) {
410        $errors = array();
411
412        if ( ! empty( $eligibility['errors'] ) && is_array( $eligibility['errors'] ) ) {
413            foreach ( $eligibility['errors'] as $error ) {
414                if ( empty( $error['code'] ) ) {
415                    continue;
416                }
417
418                $errors[] = array(
419                    'code'    => (string) $error['code'],
420                    'message' => isset( $error['message'] ) ? (string) $error['message'] : '',
421                );
422            }
423        }
424
425        return $errors;
426    }
427
428    /**
429     * Non-blocking warnings to confirm before a transfer starts.
430     *
431     * The API groups warnings by type; they are flattened here because the page
432     * renders one list and a PHP map would localize as a JSON array once empty.
433     *
434     * @param array|null $eligibility Result of get_eligibility().
435     * @return array[] Warnings in the API's own shape, minus the grouping.
436     */
437    public static function get_transfer_warnings( $eligibility ) {
438        $warnings = array();
439
440        if ( ! empty( $eligibility['warnings'] ) && is_array( $eligibility['warnings'] ) ) {
441            foreach ( $eligibility['warnings'] as $group ) {
442                if ( ! is_array( $group ) ) {
443                    continue;
444                }
445
446                foreach ( $group as $warning ) {
447                    if ( empty( $warning['id'] ) ) {
448                        continue;
449                    }
450
451                    $warnings[] = array(
452                        'id'           => (string) $warning['id'],
453                        'description'  => isset( $warning['description'] ) ? (string) $warning['description'] : '',
454                        'domain_names' => ( ! empty( $warning['domain_names']['current'] ) && ! empty( $warning['domain_names']['new'] ) )
455                            ? array(
456                                'current' => (string) $warning['domain_names']['current'],
457                                'new'     => (string) $warning['domain_names']['new'],
458                            )
459                            : null,
460                        // Rendered as a link, so only a web URL gets through.
461                        'support_url'  => isset( $warning['support_url'] ) ? esc_url_raw( (string) $warning['support_url'], array( 'http', 'https' ) ) : '',
462                    );
463                }
464            }
465        }
466
467        return $warnings;
468    }
469
470    /**
471     * Support's contact page, in the reader's language where WordPress.com has one.
472     *
473     * @return string
474     */
475    public static function get_support_url() {
476        $url = 'https://wordpress.com/help/contact';
477
478        return function_exists( 'localized_wpcom_url' ) ? localized_wpcom_url( $url ) : $url;
479    }
480
481    /**
482     * On Simple, checkout with the Business plan already in the cart; on WoA, the plan upgrade flow.
483     *
484     * `redirect_to` returns a buyer to activate what they just bought, and
485     * `checkoutBackUrl` returns someone who backs out; without it checkout falls back to /plans.
486     *
487     * @param string $domain Site domain.
488     * @return string
489     */
490    public static function get_upgrade_url( $domain ) {
491        // On WoA the purchase makes the feature live, so there is nothing to come back here for.
492        if ( self::is_atomic() ) {
493            return static::get_plan_upgrade_url();
494        }
495
496        $page_url = rawurlencode( static::get_page_url() );
497
498        return add_query_arg(
499            array(
500                'redirect_to'     => $page_url,
501                'checkoutBackUrl' => $page_url,
502            ),
503            'https://wordpress.com/checkout/' . rawurlencode( (string) $domain ) . '/business'
504        );
505    }
506
507    /**
508     * WordPress.com's plan upgrade flow, showing only plans with FEATURE, with Back to the page the link was on.
509     *
510     * The flow and checkout accept `back_to` and `redirect_to` only on the site's own host or WordPress.com.
511     *
512     * @param string $back_url The admin page the link is on; this page when empty or off this site.
513     * @return string
514     */
515    public static function get_plan_upgrade_url( $back_url = '' ) {
516        $back     = '' === $back_url ? '' : wp_validate_redirect( $back_url, '' );
517        $constant = '\WPCOM_Features::' . static::FEATURE;
518        $live_url = static::get_live_feature_url();
519
520        // Only this page reads the arg, and only a completed purchase follows `redirect_to`.
521        if ( static::get_page_url() === $live_url ) {
522            $live_url = add_query_arg( self::CHECKOUT_ARG, self::CHECKOUT_COMPLETE, $live_url );
523        }
524
525        return add_query_arg(
526            array_filter(
527                array(
528                    'siteSlug'    => rawurlencode( wpcom_get_site_slug() ),
529                    'feature'     => defined( $constant ) ? constant( $constant ) : '',
530                    'back_to'     => rawurlencode( $back ? $back : static::get_page_url() ),
531                    'redirect_to' => rawurlencode( $live_url ),
532                )
533            ),
534            'https://wordpress.com/setup/plan-upgrade'
535        );
536    }
537
538    /**
539     * Where a site lands once its plan makes the feature live and this page steps aside.
540     *
541     * By default the slug's next owner serves the feature at this page's own address.
542     *
543     * @param string|null $new_host The site's address after a transfer, when it changes.
544     * @return string
545     */
546    public static function get_live_feature_url( $new_host = null ) {
547        $page_url = static::get_page_url();
548
549        if ( null === $new_host ) {
550            return $page_url;
551        }
552
553        $parts = wp_parse_url( $page_url );
554
555        return 'https://' . $new_host . ( $parts['path'] ?? '' ) . ( isset( $parts['query'] ) ? '?' . $parts['query'] : '' );
556    }
557
558    /**
559     * The site's new address from the address-change warning, if the transfer changes it.
560     *
561     * Used rather than trusting the old address to redirect, which may not have propagated
562     * when the flow sends the reader back.
563     *
564     * @param array[] $warnings Result of get_transfer_warnings().
565     * @return string|null
566     */
567    public static function get_post_transfer_host( array $warnings ) {
568        foreach ( $warnings as $warning ) {
569            $new_host = $warning['domain_names']['new'] ?? '';
570
571            // A bare hostname only, so nothing in the payload can steer the scheme or path.
572            if ( is_string( $new_host ) && filter_var( $new_host, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME ) ) {
573                return strtolower( $new_host );
574            }
575        }
576
577        return null;
578    }
579
580    /**
581     * URL of the Calypso flow that transfers the site and switches the feature on.
582     *
583     * The flow waits for the transfer to land before following `redirect_to`.
584     *
585     * @param array[] $warnings Result of get_transfer_warnings(), for the post-transfer address.
586     * @return string
587     */
588    public static function get_activate_url( array $warnings = array() ) {
589        return add_query_arg(
590            array(
591                // Only Simple sites reach activation, and their local blog ID is the WordPress.com one; WoA's is not.
592                'siteId'                    => get_current_blog_id(),
593                'initiate_transfer_context' => self::TRANSFER_CONTEXT,
594                'redirect_to'               => rawurlencode( static::get_live_feature_url( self::get_post_transfer_host( $warnings ) ) ),
595            ),
596            self::TRANSFER_FLOW_URL
597        );
598    }
599}