Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
WP_REST_WPCOM_Block_Editor_NUX_Status_Controller
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 6
210
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 register_rest_route
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
2
 permission_callback
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 show_wpcom_welcome_guide
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_nux_status
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
56
 update_nux_status
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * WP_REST_WPCOM_Block_Editor_NUX_Status_Controller file.
4 *
5 * @package Aautomattic/jetpack-mu-wpcom
6 */
7
8namespace Automattic\Jetpack\Jetpack_Mu_Wpcom\NUX;
9
10/**
11 * Class WP_REST_WPCOM_Block_Editor_NUX_Status_Controller.
12 */
13class WP_REST_WPCOM_Block_Editor_NUX_Status_Controller extends \WP_REST_Controller {
14    /**
15     * WP_REST_WPCOM_Block_Editor_NUX_Status_Controller constructor.
16     */
17    public function __construct() {
18        $this->namespace = 'wpcom/v2';
19        $this->rest_base = 'block-editor/nux';
20    }
21
22    /**
23     * Register available routes.
24     */
25    public function register_rest_route() {
26        register_rest_route(
27            $this->namespace,
28            $this->rest_base,
29            array(
30                array(
31                    'methods'             => \WP_REST_Server::READABLE,
32                    'callback'            => array( $this, 'get_nux_status' ),
33                    'permission_callback' => array( $this, 'permission_callback' ),
34                ),
35                array(
36                    'methods'             => \WP_REST_Server::EDITABLE,
37                    'callback'            => array( $this, 'update_nux_status' ),
38                    'permission_callback' => array( $this, 'permission_callback' ),
39                ),
40            )
41        );
42    }
43
44    /**
45     * Callback to determine whether the request can proceed.
46     *
47     * @return boolean
48     */
49    public function permission_callback() {
50        return is_user_logged_in();
51    }
52
53    /**
54     * Should we show the wpcom welcome guide (i.e. welcome tour or nux modal)
55     *
56     * @param mixed $nux_status Can be "enabled", "dismissed", or undefined.
57     * @return boolean
58     */
59    public function show_wpcom_welcome_guide( $nux_status ) {
60        return 'enabled' === $nux_status;
61    }
62
63    /**
64     * Return the WPCOM NUX status
65     *
66     * This is only called for sites where the user hasn't already dismissed the tour.
67     * Once the tour has been dismissed, the closed state is saved in local storage (for the current site)
68     * see src/block-editor-nux.js
69     *
70     * @return \WP_REST_Response
71     */
72    public function get_nux_status() {
73
74        $should_open_patterns_panel = (bool) get_option( 'was_created_with_blank_canvas_design' );
75
76        if ( $should_open_patterns_panel ) {
77            $variant = 'blank-canvas-tour';
78        } else {
79            $variant = 'tour';
80        }
81
82        if ( defined( 'IS_ATOMIC' ) && IS_ATOMIC ) {
83            $is_p2 = false;
84        } else {
85            $blog_id = get_current_blog_id();
86            $is_p2   = \WPForTeams\is_wpforteams_site( $blog_id );
87        }
88
89        if ( $is_p2 ) {
90            // disable welcome tour for authoring P2s.
91            // see: https://github.com/Automattic/wp-calypso/issues/62973.
92            $nux_status = 'disabled';
93        } elseif ( has_filter( 'wpcom_block_editor_nux_get_status' ) ) {
94            $nux_status = apply_filters( 'wpcom_block_editor_nux_get_status', false );
95        } elseif ( ! metadata_exists( 'user', get_current_user_id(), 'wpcom_block_editor_nux_status' ) ) {
96            $nux_status = 'enabled';
97        } else {
98            $nux_status = get_user_meta( get_current_user_id(), 'wpcom_block_editor_nux_status', true );
99        }
100
101        $show_welcome_guide = $this->show_wpcom_welcome_guide( $nux_status );
102
103        return rest_ensure_response(
104            array(
105                'show_welcome_guide' => $show_welcome_guide,
106                'variant'            => $variant,
107            )
108        );
109    }
110
111    /**
112     * Update the WPCOM NUX status
113     *
114     * @param \WP_REST_Request $request Request object.
115     * @return \WP_REST_Response
116     */
117    public function update_nux_status( $request ) {
118        $params     = $request->get_json_params();
119        $nux_status = $params['show_welcome_guide'] ? 'enabled' : 'dismissed';
120        if ( has_action( 'wpcom_block_editor_nux_update_status' ) ) {
121            do_action( 'wpcom_block_editor_nux_update_status', $nux_status );
122        }
123        update_user_meta( get_current_user_id(), 'wpcom_block_editor_nux_status', $nux_status );
124        return rest_ensure_response( array( 'show_welcome_guide' => $this->show_wpcom_welcome_guide( $nux_status ) ) );
125    }
126}