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