Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.65% covered (success)
95.65%
66 / 69
40.00% covered (danger)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Indexnow
95.65% covered (success)
95.65%
66 / 69
40.00% covered (danger)
40.00%
2 / 5
23
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 register_routes
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
2
 status
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
4
 pause_reason
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
7.07
 save
95.00% covered (success)
95.00%
19 / 20
0.00% covered (danger)
0.00%
0 / 1
9
1<?php
2/**
3 * Opt-in, basic status and recovery for IndexNow notifications.
4 *
5 * @package automattic/jetpack-seo
6 */
7
8namespace Automattic\Jetpack\SEO;
9
10use Automattic\Jetpack\Status\Host;
11use WP_Error;
12
13/**
14 * Keeps service state independent of the settings page lifecycle.
15 */
16class Indexnow {
17    /** Register the owner-controlled settings endpoint. */
18    public static function init() {
19        add_action( 'rest_api_init', array( __CLASS__, 'register_routes' ) );
20    }
21
22    /** Register the owner-only opt-in and status endpoint. */
23    public static function register_routes() {
24        if ( ( new Host() )->is_vip_site() ) {
25            return;
26        }
27        register_rest_route(
28            'jetpack/v4',
29            '/seo/indexnow',
30            array(
31                array(
32                    'methods'             => 'GET',
33                    'callback'            => array( __CLASS__, 'status' ),
34                    'permission_callback' => array( Dashboard_Data::class, 'permission_check' ),
35                ),
36                array(
37                    'methods'             => 'POST',
38                    'callback'            => array( __CLASS__, 'save' ),
39                    'permission_callback' => array( Dashboard_Data::class, 'permission_check' ),
40                    'args'                => array(
41                        'enabled' => array(
42                            'required' => true,
43                            'type'     => 'boolean',
44                        ),
45                    ),
46                ),
47            )
48        );
49    }
50
51    /**
52     * Factual delivery state without pending URLs.
53     *
54     * @return array
55     */
56    public static function status() {
57        $latest   = get_option( Indexnow_Queue::STATUS_OPTION, array( 'state' => 'idle' ) );
58        $accepted = get_option( Indexnow_Queue::ACCEPTED_OPTION, null );
59        if ( is_array( $accepted ) ) {
60            $latest['accepted'] = $accepted;
61        }
62        $pending = Indexnow_Queue::size();
63        $runs    = array_filter( array( wp_next_scheduled( Indexnow_Queue::HOOK ), wp_next_scheduled( Indexnow_Queue::RECOVERY_HOOK ) ), 'is_int' );
64        return array(
65            'enabled'            => (bool) get_option( Indexnow_Client::ENABLED_OPTION, false ),
66            'pending'            => $pending,
67            'latest'             => $latest,
68            'enqueue_error'      => get_option( 'jetpack_seo_indexnow_enqueue_error', null ),
69            'cooldown_active'    => (int) get_option( Indexnow_Queue::COOLDOWN_OPTION, 0 ) > time(),
70            'scheduling_delayed' => $pending > 0 && ( ! $runs || min( $runs ) < time() - HOUR_IN_SECONDS ),
71            'pause_reason'       => self::pause_reason(),
72        );
73    }
74
75    /**
76     * Identify the condition that prevents the saved preference from operating.
77     *
78     * @return string
79     */
80    private static function pause_reason() {
81        if ( ! Indexnow_Client::site_is_public() ) {
82            return 'not_public';
83        }
84        if ( ! Initializer::is_seo_tools_active() ) {
85            return 'module_inactive';
86        }
87        if ( ! apply_filters( 'jetpack_seo_indexnow_can_notify', true ) || Seo_Tools::is_disabled() ) {
88            return 'provider';
89        }
90        if ( get_option( Indexnow_Client::ENABLED_OPTION, false ) && ! Indexnow_Client::matches_scope( get_option( Indexnow_Client::SCOPE_OPTION, '' ) ) ) {
91            return 'address_changed';
92        }
93        return '';
94    }
95
96    /**
97     * Save explicit consent without enqueueing historical content.
98     *
99     * @param \WP_REST_Request $request Current owner request.
100     * @return array|WP_Error
101     */
102    public static function save( $request ) {
103        $enabled = (bool) $request->get_param( 'enabled' );
104        if ( ! $enabled ) {
105            $was_enabled = (bool) get_option( Indexnow_Client::ENABLED_OPTION, false );
106            update_option( Indexnow_Client::ENABLED_OPTION, false, false );
107            if ( get_option( Indexnow_Client::ENABLED_OPTION, false ) ) {
108                return new WP_Error( 'jetpack_seo_indexnow_save_failed', __( 'The search engine notification setting could not be saved. Please try again.', 'jetpack-seo' ), array( 'status' => 500 ) );
109            }
110            if ( ! $was_enabled || false === has_action( 'update_option_' . Indexnow_Client::ENABLED_OPTION, array( Indexnow_Queue::class, 'preference_changed' ) ) ) {
111                Indexnow_Queue::clear();
112            }
113        } else {
114            $key = Indexnow_Client::key();
115            if ( ! Indexnow_Client::matches_scope( get_option( Indexnow_Client::SCOPE_OPTION, '' ) ) ) {
116                delete_option( Indexnow_Queue::STATUS_OPTION );
117                delete_option( Indexnow_Queue::ACCEPTED_OPTION );
118            }
119            update_option( Indexnow_Client::SCOPE_OPTION, Indexnow_Client::scope(), false );
120            if ( get_option( Indexnow_Client::KEY_OPTION, '' ) !== $key || ! Indexnow_Client::matches_scope( get_option( Indexnow_Client::SCOPE_OPTION, '' ) ) ) {
121                return new WP_Error( 'jetpack_seo_indexnow_save_failed', __( 'The search engine notification setting could not be saved. Please try again.', 'jetpack-seo' ), array( 'status' => 500 ) );
122            }
123            update_option( Indexnow_Client::ENABLED_OPTION, true, false );
124            if ( ! get_option( Indexnow_Client::ENABLED_OPTION, false ) ) {
125                return new WP_Error( 'jetpack_seo_indexnow_save_failed', __( 'The search engine notification setting could not be saved. Please try again.', 'jetpack-seo' ), array( 'status' => 500 ) );
126            }
127            Indexnow_Queue::ensure_recovery();
128        }
129        return self::status();
130    }
131}