*/ public static function defaults(): array { return array( 'enabled' => false, 'auto' => true, 'post_types' => array( 'post', 'page' ), 'endpoint' => 'https://api.indexnow.org/indexnow', ); } /** * Endpoints users can pick — all share the same submission network. * * @return array */ public static function endpoints(): array { return array( 'https://api.indexnow.org/indexnow' => 'IndexNow (all participating engines)', 'https://www.bing.com/indexnow' => 'Bing', 'https://yandex.com/indexnow' => 'Yandex', 'https://search.seznam.cz/indexnow' => 'Seznam', ); } public function register( Container $container ): void {} public function boot( Container $container ): void { add_action( 'rest_api_init', array( $this, 'routes' ) ); add_action( 'init', array( self::class, 'add_rewrite' ) ); add_filter( 'query_vars', static function ( array $vars ): array { $vars[] = self::QUERY_VAR; return $vars; } ); Guard::action( 'template_redirect', array( $this, 'serve_key_file' ), 0 ); add_action( 'transition_post_status', array( $this, 'auto_submit' ), 10, 3 ); } /** * The key file must be reachable at /{key}.txt containing the key itself. */ public static function add_rewrite(): void { add_rewrite_rule( '^([a-f0-9]{32})\.txt$', 'index.php?' . self::QUERY_VAR . '=$matches[1]', 'top' ); } public function serve_key_file(): void { $requested = (string) get_query_var( self::QUERY_VAR ); if ( '' === $requested ) { return; } if ( ! hash_equals( $this->key(), $requested ) ) { status_header( 404 ); exit; } status_header( 200 ); header( 'Content-Type: text/plain; charset=utf-8' ); echo esc_html( $this->key() ); exit; } /** * The site's IndexNow key, generated once. */ public function key(): string { $key = get_option( self::OPT_KEY ); if ( is_string( $key ) && 1 === preg_match( '/^[a-f0-9]{32}$/', $key ) ) { return $key; } $key = bin2hex( random_bytes( 16 ) ); update_option( self::OPT_KEY, $key, false ); return $key; } public function regenerate_key(): string { delete_option( self::OPT_KEY ); return $this->key(); } /** * Submit URLs. IndexNow accepts up to 10,000 per request. * * @param array $urls URLs on this site. * @return array{submitted: int}|WP_Error */ public function submit( array $urls ): array|WP_Error { $settings = $this->settings(); $host = (string) wp_parse_url( home_url(), PHP_URL_HOST ); $clean = array(); foreach ( $urls as $url ) { $url = esc_url_raw( (string) $url ); if ( '' === $url || wp_parse_url( $url, PHP_URL_HOST ) !== $host ) { continue; } $clean[] = $url; } $clean = array_values( array_unique( $clean ) ); if ( empty( $clean ) ) { return new WP_Error( 'airanksuite_indexnow_no_urls', __( 'No valid URLs on this site to submit.', 'ai-rank-suite' ), array( 'status' => 400 ) ); } $endpoint = isset( self::endpoints()[ $settings['endpoint'] ] ) ? (string) $settings['endpoint'] : 'https://api.indexnow.org/indexnow'; $response = wp_remote_post( $endpoint, array( 'timeout' => 20, 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8' ), 'body' => (string) wp_json_encode( array( 'host' => $host, 'key' => $this->key(), 'keyLocation' => home_url( '/' . $this->key() . '.txt' ), 'urlList' => array_slice( $clean, 0, 10000 ), ) ), ) ); if ( $response instanceof WP_Error ) { $this->log( $clean, 0, $response->get_error_message() ); return $response; } $code = (int) wp_remote_retrieve_response_code( $response ); // 200 accepted, 202 accepted but key validation pending. if ( 200 !== $code && 202 !== $code ) { $message = match ( $code ) { 400 => __( 'IndexNow rejected the request as invalid.', 'ai-rank-suite' ), 403 => __( 'The key file could not be verified. Make sure pretty permalinks are on and the key file is reachable.', 'ai-rank-suite' ), 422 => __( 'The URLs do not belong to the host, or the key does not match.', 'ai-rank-suite' ), 429 => __( 'Too many requests — try again later.', 'ai-rank-suite' ), default => sprintf( /* translators: %d: HTTP status */ __( 'IndexNow responded with HTTP %d.', 'ai-rank-suite' ), $code ), }; $this->log( $clean, $code, $message ); return new WP_Error( 'airanksuite_indexnow_failed', $message, array( 'status' => 502 ) ); } $this->log( $clean, $code, '' ); return array( 'submitted' => count( $clean ) ); } /** * Submit on publish, update and removal. * * @param string $new_status New status. * @param string $old_status Old status. * @param WP_Post $post The post. */ public function auto_submit( string $new_status, string $old_status, $post ): void { if ( ! $post instanceof WP_Post ) { return; } $settings = $this->settings(); if ( empty( $settings['enabled'] ) || empty( $settings['auto'] ) ) { return; } if ( ! in_array( $post->post_type, (array) $settings['post_types'], true ) ) { return; } if ( wp_is_post_revision( $post ) || wp_is_post_autosave( $post ) ) { return; } if ( 'publish' !== $new_status && 'publish' !== $old_status ) { return; } $this->submit( array( (string) get_permalink( $post ) ) ); } /** * @param array $urls Submitted URLs. * @param int $code HTTP status. * @param string $error Error message. */ private function log( array $urls, int $code, string $error ): void { $log = get_option( self::OPT_LOG ); $log = is_array( $log ) ? $log : array(); array_unshift( $log, array( 'count' => count( $urls ), 'first' => (string) ( $urls[0] ?? '' ), 'code' => $code, 'ok' => in_array( $code, array( 200, 202 ), true ), 'error' => mb_substr( $error, 0, 200 ), 'time' => time(), ) ); update_option( self::OPT_LOG, array_slice( $log, 0, self::LOG_MAX ), false ); } public function routes(): void { register_rest_route( self::NAMESPACE, '/settings/indexnow', array( array( 'methods' => 'GET', 'callback' => array( $this, 'get_settings' ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ), array( 'methods' => 'PUT', 'callback' => array( $this, 'update_settings' ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), 'args' => array( 'enabled' => array( 'type' => 'boolean' ), 'auto' => array( 'type' => 'boolean' ), 'post_types' => array( 'type' => 'array' ), 'endpoint' => array( 'type' => 'string', 'sanitize_callback' => 'esc_url_raw', ), ), ), ) ); register_rest_route( self::NAMESPACE, '/indexnow/submit', array( 'methods' => 'POST', 'callback' => array( $this, 'submit_urls' ), 'permission_callback' => static function () { if ( ! current_user_can( 'manage_options' ) ) { return self::forbidden(); } return self::spend( 'indexnow', 60 ); }, 'args' => array( 'urls' => array( 'type' => 'array' ), 'scope' => array( 'type' => 'string', 'enum' => array( 'all' ), ), ), ) ); register_rest_route( self::NAMESPACE, '/indexnow/key', array( 'methods' => 'POST', 'callback' => array( $this, 'new_key' ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ) ); } public function get_settings(): WP_REST_Response { $log = get_option( self::OPT_LOG ); return new WP_REST_Response( array( 'settings' => $this->settings(), 'key' => $this->key(), 'key_url' => home_url( '/' . $this->key() . '.txt' ), 'endpoints' => self::endpoints(), 'post_types' => array_values( array_diff( get_post_types( array( 'public' => true ) ), array( 'attachment' ) ) ), 'recent' => array_slice( is_array( $log ) ? $log : array(), 0, 20 ), 'permalinks' => (bool) get_option( 'permalink_structure' ), ) ); } public function update_settings( WP_REST_Request $request ): WP_REST_Response { $defaults = self::defaults(); $values = array(); foreach ( array_keys( $defaults ) as $key ) { if ( ! $request->has_param( $key ) ) { continue; } if ( 'post_types' === $key ) { $values[ $key ] = array_values( array_intersect( (array) $request->get_param( $key ), get_post_types( array( 'public' => true ) ) ) ); continue; } $values[ $key ] = $request->get_param( $key ); } \AiRankSuite\Kernel::service( Options::class )->set( 'indexnow', $values, $defaults ); self::add_rewrite(); flush_rewrite_rules(); return $this->get_settings(); } public function submit_urls( WP_REST_Request $request ): WP_REST_Response|WP_Error { $urls = (array) $request->get_param( 'urls' ); if ( 'all' === $request->get_param( 'scope' ) ) { $urls = array(); foreach ( get_posts( array( 'post_type' => (array) $this->settings()['post_types'], 'post_status' => 'publish', 'posts_per_page' => 500, ) ) as $post ) { $urls[] = (string) get_permalink( $post ); } } $result = $this->submit( $urls ); if ( $result instanceof WP_Error ) { return $result; } $data = (array) $this->get_settings()->get_data(); return new WP_REST_Response( array_merge( $data, array( 'result' => $result ) ) ); } public function new_key(): WP_REST_Response { $this->regenerate_key(); self::add_rewrite(); flush_rewrite_rules(); return $this->get_settings(); } /** @return array */ private function settings(): array { return \AiRankSuite\Kernel::service( Options::class )->get( 'indexnow', self::defaults() ); } }