ID ), (string) get_permalink( $post ) ) ) { return; } self::schedule( $post->ID, time() ); } /** * The scheduled event: sends the post's address and records the answer. * * @since 1.1.0 * * @param int $post_id The post. * @return void */ public static function submit( $post_id ) { $post = get_post( (int) $post_id ); if ( ! self::eligible( $post ) || ! self::active() ) { return; } $url = (string) get_permalink( $post ); $record = self::record( $post->ID ); if ( ! self::due( $record, $url ) ) { return; } $payload = self::payload( $url, self::key(), self::home() ); if ( null === $payload ) { return; } $answer = wp_remote_post( self::ENDPOINT, array( 'timeout' => 10, 'redirection' => 0, 'user-agent' => ThirdDraft_Connection::user_agent(), 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8' ), 'body' => (string) wp_json_encode( $payload, JSON_UNESCAPED_SLASHES ), ) ); $status = is_wp_error( $answer ) ? 0 : (int) wp_remote_retrieve_response_code( $answer ); $tries = ( $record['url'] === $url ? $record['tries'] : 0 ) + 1; update_post_meta( $post->ID, self::META, wp_slash( array( 'url' => $url, 'at' => time(), 'status' => $status, 'tries' => $tries, ) ) ); if ( self::worth_retrying( $status ) && $tries < self::TRIES ) { self::schedule( $post->ID, time() + self::RETRY ); } } /** * What IndexNow is sent for one address, or null when the address is not * the site's: on another host, or outside the path the site lives under, * which the key file does not cover. * * @since 1.1.0 * * @param string $url The post's address. * @param string $key The key. * @param string $home The site's home address. * @return array{host: string, key: string, keyLocation: string, urlList: string[]}|null */ public static function payload( $url, $key, $home ) { $site = wp_parse_url( $home ); $page = wp_parse_url( $url ); if ( ! is_array( $site ) || ! is_array( $page ) || empty( $site['host'] ) || empty( $page['host'] ) || empty( $page['scheme'] ) ) { return null; } $authority = self::authority( $page ); $base = self::base_path( $home ); $path = isset( $page['path'] ) ? $page['path'] : '/'; if ( self::authority( $site ) !== $authority || 0 !== strpos( $path, $base ) ) { return null; } return array( 'host' => strtolower( $page['host'] ), 'key' => $key, 'keyLocation' => strtolower( $page['scheme'] ) . '://' . $authority . $base . $key . '.txt', 'urlList' => array( $url ), ); } /** * Whether an address is to be sent, from what was sent before: a new * address always; the same one until it is taken, within its tries. * * @since 1.1.0 * * @param array{url: string, at: int, status: int, tries: int} $record What was sent last. * @param string $url The post's address now. * @return bool */ public static function due( $record, $url ) { if ( '' === $url ) { return false; } if ( $record['url'] !== $url ) { return true; } return ! self::taken( $record['status'] ) && $record['tries'] < self::TRIES; } /** * Whether IndexNow took an address: 200, or 202 while it checks the key. * * @since 1.1.0 * * @param int $status The HTTP status; 0 for no answer. * @return bool */ public static function taken( $status ) { return 200 === (int) $status || 202 === (int) $status; } /** * What was sent last for a post. * * @since 1.1.0 * * @param int $post_id The post. * @return array{url: string, at: int, status: int, tries: int} */ public static function record( $post_id ) { $record = get_post_meta( (int) $post_id, self::META, true ); $record = is_array( $record ) ? $record : array(); return array( 'url' => isset( $record['url'] ) ? (string) $record['url'] : '', 'at' => isset( $record['at'] ) ? (int) $record['at'] : 0, 'status' => isset( $record['status'] ) ? (int) $record['status'] : 0, 'tries' => isset( $record['tries'] ) ? (int) $record['tries'] : 0, ); } /** * The site's key, made the first time it is needed: 32 lowercase * hexadecimal characters. * * @since 1.1.0 * * @return string */ public static function key() { $key = self::stored_key(); if ( '' !== $key ) { return $key; } $key = bin2hex( random_bytes( 16 ) ); if ( add_option( self::KEY, $key, '', false ) ) { return $key; } $stored = self::stored_key(); if ( '' !== $stored ) { return $stored; } update_option( self::KEY, $key, false ); return $key; } /** * The key kept, or '' when none has been made. * * @return string */ private static function stored_key() { $key = get_option( self::KEY, '' ); return is_string( $key ) && preg_match( '/^[0-9a-f]{32}$/', $key ) ? $key : ''; } /** * A post IndexNow may hear of: published, public, without a password, * and made by this plugin. * * @param mixed $post The post. * @return bool * @phpstan-assert-if-true WP_Post $post */ private static function eligible( $post ) { if ( ! $post instanceof WP_Post || 'publish' !== $post->post_status || '' !== $post->post_password ) { return false; } return is_post_type_viewable( $post->post_type ) && '' !== (string) get_post_meta( $post->ID, ThirdDraft_Post_Writer::ARTICLE, true ); } /** * Whether anything is sent from this site: the setting on, on a * production site that lets search engines index it. * * @return bool */ private static function active() { return self::enabled() && 'production' === wp_get_environment_type() && '0' !== (string) get_option( 'blog_public', '1' ); } /** * The setting. * * @return bool */ private static function enabled() { $settings = ThirdDraft_Store::settings(); return $settings['indexnow']; } /** * Queues the post's sending, once: an event already due by then stands, * a later one is brought forward. * * @param int $post_id The post. * @param int $at When. * @return void */ private static function schedule( $post_id, $at ) { $args = array( (int) $post_id ); $next = wp_next_scheduled( self::EVENT, $args ); if ( false !== $next && $next <= $at ) { return; } if ( false !== $next ) { wp_unschedule_event( $next, self::EVENT, $args ); } wp_schedule_single_event( $at, self::EVENT, $args ); } /** * Whether a failed sending may go through later: no answer, too many * requests, or IndexNow's own error. * * @param int $status The HTTP status; 0 for no answer. * @return bool */ private static function worth_retrying( $status ) { return 0 === $status || 429 === $status || $status >= 500; } /** * The site's home address as saved in WordPress, which no other plugin's * language or domain filter rewrites. * * @return string */ private static function home() { return (string) get_option( 'home' ); } /** * The path the site lives under, with a slash at each end: '/' at the root. * * @param string $home The site's home address. * @return string */ private static function base_path( $home ) { $path = wp_parse_url( $home, PHP_URL_PATH ); return rtrim( is_string( $path ) ? $path : '', '/' ) . '/'; } /** * Host and port, as an address names them. * * @param array $parts The address, parsed. * @return string */ private static function authority( $parts ) { $host = isset( $parts['host'] ) ? strtolower( (string) $parts['host'] ) : ''; return isset( $parts['port'] ) ? $host . ':' . (int) $parts['port'] : $host; } }