get_all_settings(); if ( empty( $s['indexnow_enabled'] ) || ! apply_filters( 'qwsc_indexnow_enabled', true ) ) { return; } $key = isset( $s['indexnow_key'] ) ? preg_replace( '/[^a-zA-Z0-9\-]/', '', (string) $s['indexnow_key'] ) : ''; if ( strlen( $key ) < 8 || strlen( $key ) > 128 ) { return; } $url = get_permalink( $post ); if ( ! $url ) { return; } $host = wp_parse_url( home_url(), PHP_URL_HOST ); $endpoint = 'https://api.indexnow.org/indexnow'; $body = wp_json_encode( array( 'host' => $host, 'key' => $key, 'keyLocation' => home_url( '/' . $key . '.txt' ), 'urlList' => array( $url ), ) ); wp_safe_remote_post( $endpoint, array( 'timeout' => 2, 'blocking' => false, 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8' ), 'body' => $body, ) ); } /** * Serve /{key}.txt for IndexNow verification. */ public function serve_key_file() { $s = \QWSpeedCache\Plugin::instance()->get_all_settings(); if ( empty( $s['indexnow_enabled'] ) ) { return; } $key = isset( $s['indexnow_key'] ) ? preg_replace( '/[^a-zA-Z0-9\-]/', '', (string) $s['indexnow_key'] ) : ''; if ( strlen( $key ) < 8 || strlen( $key ) > 128 ) { return; } $uri = isset( $_SERVER['REQUEST_URI'] ) && is_string( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; $path = wp_parse_url( $uri, PHP_URL_PATH ); $base = wp_parse_url( home_url( '/' ), PHP_URL_PATH ); $expected = trailingslashit( is_string( $base ) ? $base : '/' ) . $key . '.txt'; if ( is_string( $path ) && $expected === $path ) { status_header( 200 ); header( 'Content-Type: text/plain; charset=utf-8' ); header( 'X-Robots-Tag: noindex' ); echo $key; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped exit; } } /** * Batch ping URLs (IndexNow allows large batches). * * @param string[] $urls URLs. * @return void */ public static function batch_ping( array $urls ) { $s = \QWSpeedCache\Plugin::instance()->get_all_settings(); if ( empty( $s['indexnow_enabled'] ) ) { return; } $key = isset( $s['indexnow_key'] ) ? preg_replace( '/[^a-zA-Z0-9\-]/', '', (string) $s['indexnow_key'] ) : ''; if ( strlen( $key ) < 8 || strlen( $key ) > 128 ) { return; } $host = wp_parse_url( home_url(), PHP_URL_HOST ); if ( ! is_string( $host ) || '' === $host ) { return; } $urls = array_filter( $urls, 'is_string' ); $urls = array_values( array_unique( array_filter( array_map( 'esc_url_raw', $urls ), static function ( $url ) use ( $host ) { $scheme = wp_parse_url( $url, PHP_URL_SCHEME ); $url_host = wp_parse_url( $url, PHP_URL_HOST ); return in_array( $scheme, array( 'http', 'https' ), true ) && is_string( $url_host ) && 0 === strcasecmp( $host, $url_host ); } ) ) ); $urls = array_slice( $urls, 0, 10000 ); if ( ! $urls ) { return; } $host = wp_parse_url( home_url(), PHP_URL_HOST ); $body = wp_json_encode( array( 'host' => $host, 'key' => $key, 'keyLocation' => home_url( '/' . $key . '.txt' ), 'urlList' => $urls, ) ); wp_safe_remote_post( 'https://api.indexnow.org/indexnow', array( 'timeout' => 2, 'blocking' => false, 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8' ), 'body' => $body, ) ); } public function admin_submit_sitemap() { check_admin_referer( 'qwsc_indexnow_sitemap' ); if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Unauthorized', 'qw-optimize' ), '', array( 'response' => 403 ) ); } $urls = array(); if ( class_exists( '\QWSpeedCache\Preload\Manager' ) && method_exists( '\QWSpeedCache\Preload\Manager', 'collect_urls' ) ) { $urls = \QWSpeedCache\Preload\Manager::collect_urls(); } self::batch_ping( is_array( $urls ) ? $urls : array() ); wp_safe_redirect( admin_url( 'admin.php?page=qw-speed-cache-advanced&indexnow=1' ) ); exit; } }