Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
87.88% |
87 / 99 |
|
70.00% |
7 / 10 |
CRAP | |
0.00% |
0 / 1 |
| Indexnow_Client | |
87.88% |
87 / 99 |
|
70.00% |
7 / 10 |
61.58 | |
0.00% |
0 / 1 |
| init | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| key | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| enabled | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
8 | |||
| site_is_public | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
5 | |||
| key_location | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| scope | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| in_scope | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
14 | |||
| maybe_serve_key | |
41.67% |
5 / 12 |
|
0.00% |
0 / 1 |
16.73 | |||
| batch_size | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| submit | |
92.73% |
51 / 55 |
|
0.00% |
0 / 1 |
14.08 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * IndexNow ownership and bounded notifications. |
| 4 | * |
| 5 | * @package automattic/jetpack-seo |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\SEO; |
| 9 | |
| 10 | use Automattic\Jetpack\Modules; |
| 11 | use Automattic\Jetpack\Status; |
| 12 | use Automattic\Jetpack\Status\Host; |
| 13 | |
| 14 | /** |
| 15 | * Supplies verification without requiring a physical file. |
| 16 | */ |
| 17 | class Indexnow_Client { |
| 18 | const KEY_OPTION = 'jetpack_seo_indexnow_key'; |
| 19 | const ENABLED_OPTION = 'jetpack_seo_indexnow_enabled'; |
| 20 | const SCOPE_OPTION = 'jetpack_seo_indexnow_enabled_scope'; |
| 21 | const ENDPOINT = 'https://api.indexnow.org/indexnow'; |
| 22 | const BATCH_SIZE = 100; |
| 23 | |
| 24 | /** Register the ownership handler without enabling notifications. */ |
| 25 | public static function init() { |
| 26 | add_action( 'template_redirect', array( __CLASS__, 'maybe_serve_key' ), 0 ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * The persisted public verification key. |
| 31 | * |
| 32 | * @return string |
| 33 | */ |
| 34 | public static function key() { |
| 35 | $key = get_option( self::KEY_OPTION, '' ); |
| 36 | if ( ! is_string( $key ) || ! preg_match( '/^[a-f0-9]{32}$/', $key ) ) { |
| 37 | $key = bin2hex( random_bytes( 16 ) ); |
| 38 | update_option( self::KEY_OPTION, $key, false ); |
| 39 | } |
| 40 | return $key; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Whether notifications are enabled on a public site. |
| 45 | * |
| 46 | * @return bool |
| 47 | */ |
| 48 | public static function enabled() { |
| 49 | /** |
| 50 | * Filters whether Jetpack may deliver IndexNow notifications on this host. |
| 51 | * |
| 52 | * @since $$next-version$$ |
| 53 | * @param bool $allowed Whether another host/provider permits Jetpack delivery. |
| 54 | */ |
| 55 | $allowed = (bool) apply_filters( 'jetpack_seo_indexnow_can_notify', true ); |
| 56 | return (bool) get_option( self::ENABLED_OPTION, false ) |
| 57 | && $allowed |
| 58 | && get_option( self::SCOPE_OPTION, '' ) === self::scope() |
| 59 | && Initializer::is_available() && ( new Modules() )->is_active( 'seo-tools' ) |
| 60 | && ! ( new Host() )->is_vip_site() && self::site_is_public() |
| 61 | && ! Seo_Tools::is_disabled(); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Whether anonymous discovery is permitted on this host. |
| 66 | * |
| 67 | * @return bool |
| 68 | */ |
| 69 | public static function site_is_public() { |
| 70 | return 1 === (int) get_option( 'blog_public', 1 ) |
| 71 | && 'production' === wp_get_environment_type() && ! ( new Status() )->in_safe_mode() |
| 72 | && ( ! ( new Host() )->is_wpcom_platform() || ! ( new Status() )->is_coming_soon() ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Verification location within this site's URL scope. |
| 77 | * |
| 78 | * @return string |
| 79 | */ |
| 80 | public static function key_location() { |
| 81 | return self::scope() . self::key() . '.txt'; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Site scope independent of the current request's transport. |
| 86 | * |
| 87 | * @since $$next-version$$ |
| 88 | * @return string |
| 89 | */ |
| 90 | public static function scope() { |
| 91 | return home_url( '/', wp_parse_url( get_option( 'home' ), PHP_URL_SCHEME ) ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Restrict HTTP(S) notifications to this site's host and home path. |
| 96 | * |
| 97 | * @param string $url Candidate URL. |
| 98 | * @return bool |
| 99 | */ |
| 100 | public static function in_scope( $url ) { |
| 101 | if ( ! is_string( $url ) || strlen( $url ) > 2048 || preg_match( '/[\x00-\x20\x7f]/', $url ) ) { |
| 102 | return false; |
| 103 | } |
| 104 | $home = wp_parse_url( self::scope() ); |
| 105 | $target = wp_parse_url( $url ); |
| 106 | if ( ! is_array( $target ) || isset( $target['user'] ) || isset( $target['pass'] ) || isset( $target['fragment'] ) ) { |
| 107 | return false; |
| 108 | } |
| 109 | if ( ! in_array( $target['scheme'] ?? '', array( 'http', 'https' ), true ) || strtolower( $target['host'] ?? '' ) !== strtolower( $home['host'] ?? '' ) || ( $target['port'] ?? null ) !== ( $home['port'] ?? null ) ) { |
| 110 | return false; |
| 111 | } |
| 112 | $path = rawurldecode( $target['path'] ?? '/' ); |
| 113 | if ( false !== strpos( $path, '\\' ) || preg_match( '~(?:^|/)\.{1,2}(?:/|$)~', $path ) || preg_match( '/%(?:2e|2f|5c)/i', $path ) ) { |
| 114 | return false; |
| 115 | } |
| 116 | return 0 === strpos( $path, trailingslashit( $home['path'] ?? '/' ) ); |
| 117 | } |
| 118 | |
| 119 | /** Serve the public key only while the feature can operate. */ |
| 120 | public static function maybe_serve_key() { |
| 121 | $method = sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ?? 'GET' ) ); |
| 122 | if ( ! in_array( $method, array( 'GET', 'HEAD' ), true ) ) { |
| 123 | return; |
| 124 | } |
| 125 | $path = wp_parse_url( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ?? '' ) ), PHP_URL_PATH ); |
| 126 | if ( ! is_string( $path ) || ! preg_match( '~/[a-f0-9]{32}\.txt$~', $path ) || ! self::enabled() || wp_parse_url( self::key_location(), PHP_URL_PATH ) !== $path ) { |
| 127 | return; |
| 128 | } |
| 129 | nocache_headers(); |
| 130 | header( 'Content-Type: text/plain; charset=utf-8' ); |
| 131 | status_header( 200 ); |
| 132 | if ( 'HEAD' !== $method ) { |
| 133 | echo esc_html( self::key() ); |
| 134 | } |
| 135 | exit; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Bound the configurable request size to the protocol maximum. |
| 140 | * |
| 141 | * @return int |
| 142 | */ |
| 143 | public static function batch_size() { |
| 144 | /** |
| 145 | * Filters the maximum URLs in one IndexNow request. |
| 146 | * |
| 147 | * @since $$next-version$$ |
| 148 | * @param int $size Maximum URLs, between 1 and 10,000. |
| 149 | */ |
| 150 | $size = apply_filters( 'jetpack_seo_indexnow_batch_size', self::BATCH_SIZE ); |
| 151 | return is_int( $size ) ? max( 1, min( 10000, $size ) ) : self::BATCH_SIZE; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Send an already eligible batch to one participating endpoint. |
| 156 | * |
| 157 | * @param string[] $urls Public URLs. |
| 158 | * @return array |
| 159 | */ |
| 160 | public static function submit( $urls ) { |
| 161 | if ( ! self::enabled() || ! is_array( $urls ) || ! $urls ) { |
| 162 | return array( |
| 163 | 'state' => 'disabled_or_invalid', |
| 164 | 'retry' => false, |
| 165 | ); |
| 166 | } |
| 167 | if ( ! preg_match( '/^[a-f0-9]{32}$/', self::key() ) ) { |
| 168 | return array( |
| 169 | 'state' => 'invalid_key', |
| 170 | 'retry' => false, |
| 171 | ); |
| 172 | } |
| 173 | foreach ( $urls as $url ) { |
| 174 | if ( ! self::in_scope( $url ) ) { |
| 175 | return array( |
| 176 | 'state' => 'invalid_scope', |
| 177 | 'retry' => false, |
| 178 | ); |
| 179 | } |
| 180 | } |
| 181 | $urls = array_values( array_unique( $urls ) ); |
| 182 | if ( count( $urls ) > self::batch_size() ) { |
| 183 | return array( |
| 184 | 'state' => 'disabled_or_invalid', |
| 185 | 'retry' => false, |
| 186 | ); |
| 187 | } |
| 188 | $response = wp_safe_remote_post( |
| 189 | self::ENDPOINT, |
| 190 | array( |
| 191 | 'timeout' => 5, |
| 192 | 'redirection' => 0, |
| 193 | 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8' ), |
| 194 | 'body' => wp_json_encode( |
| 195 | array( |
| 196 | 'host' => wp_parse_url( self::scope(), PHP_URL_HOST ), |
| 197 | 'key' => self::key(), |
| 198 | 'keyLocation' => self::key_location(), |
| 199 | 'urlList' => $urls, |
| 200 | ), |
| 201 | JSON_UNESCAPED_SLASHES |
| 202 | ), |
| 203 | ) |
| 204 | ); |
| 205 | if ( is_wp_error( $response ) ) { |
| 206 | return array( |
| 207 | 'state' => 'transport_failure', |
| 208 | 'retry' => true, |
| 209 | ); |
| 210 | } |
| 211 | $code = wp_remote_retrieve_response_code( $response ); |
| 212 | $retry = 429 === $code || $code >= 500; |
| 213 | $retry_after = wp_remote_retrieve_header( $response, 'retry-after' ); |
| 214 | $retry_after = is_string( $retry_after ) ? $retry_after : ''; |
| 215 | $retry_after = ctype_digit( $retry_after ) ? (int) $retry_after : max( 0, (int) strtotime( $retry_after ) - time() ); |
| 216 | return array( |
| 217 | 'state' => 200 === $code ? 'accepted' : ( 202 === $code ? 'verification_pending' : 'http_' . $code ), |
| 218 | 'retry' => $retry, |
| 219 | 'code' => $code, |
| 220 | 'retry_after' => min( WEEK_IN_SECONDS, $retry_after ), |
| 221 | ); |
| 222 | } |
| 223 | } |