Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
WP_REST_WPCOM_Block_Editor_First_Post_Published_Modal_Controller
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 4
56
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 / 11
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
 should_show_first_post_published_modal
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * WP_REST_WPCOM_Block_Editor_First_Post_Published_Modal_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_First_Post_Published_Modal_Controller.
12 */
13class WP_REST_WPCOM_Block_Editor_First_Post_Published_Modal_Controller extends \WP_REST_Controller {
14    /**
15     * WP_REST_WPCOM_Block_Editor_First_Post_Published_Modal_Controller constructor.
16     */
17    public function __construct() {
18        $this->namespace = 'wpcom/v2';
19        $this->rest_base = 'block-editor/should-show-first-post-published-modal';
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, 'should_show_first_post_published_modal' ),
33                    'permission_callback' => array( $this, 'permission_callback' ),
34                ),
35            )
36        );
37    }
38
39    /**
40     * Callback to determine whether the request can proceed.
41     *
42     * @return boolean
43     */
44    public function permission_callback() {
45        return current_user_can( 'read' );
46    }
47
48    /**
49     * Should we show the first post published modal
50     *
51     * @return \WP_REST_Response
52     */
53    public function should_show_first_post_published_modal() {
54        // As we has synced the `has_never_published_post` option to part of atomic sites but we cannot
55        // update the value now, always return false to avoid showing the modal at every publishing until
56        // we can update the value on atomic sites. See D69932-code.
57        if ( defined( 'IS_ATOMIC' ) && IS_ATOMIC ) {
58            return rest_ensure_response(
59                array(
60                    'should_show_first_post_published_modal' => false,
61                )
62            );
63        }
64
65        $has_never_published_post               = (bool) get_option( 'has_never_published_post', false );
66        $intent                                 = get_option( 'site_intent', '' );
67        $should_show_first_post_published_modal = $has_never_published_post && 'write' === $intent;
68
69        return rest_ensure_response(
70            array(
71                'should_show_first_post_published_modal' => $should_show_first_post_published_modal,
72            )
73        );
74    }
75}