get_parent_id() ) { $p = wc_get_product( $p->get_parent_id() ); if ( ! $p ) { return; } } if ( 'publish' !== $p->get_status() || 'hidden' === $p->get_catalog_visibility() || post_password_required( $p->get_id() ) ) { return; } $url = (string) get_permalink( $p->get_id() ); if ( '' === $url ) { return; } self::enqueue( array( $url ) ); } public static function enqueue( array $urls ): void { $q = get_option( self::QUEUE_OPTION, array() ); $q = is_array( $q ) ? $q : array(); $q = array_values( array_unique( array_merge( $q, $urls ) ) ); $q = array_slice( $q, -10000 ); update_option( self::QUEUE_OPTION, $q, false ); if ( ! wp_next_scheduled( self::CRON ) ) { $state = self::state(); $last = isset( $state['last_ping_ts'] ) ? (int) $state['last_ping_ts'] : 0; $when = max( time() + self::BATCH_DELAY, $last + self::MIN_INTERVAL ); wp_schedule_single_event( $when, self::CRON ); } } public static function state(): array { $s = get_option( self::STATE_OPTION, array() ); return is_array( $s ) ? $s : array(); } /** Cron: send the queue in one request (runs in WP-Cron, never in the save request). */ public static function flush(): void { $q = get_option( self::QUEUE_OPTION, array() ); $q = is_array( $q ) ? array_values( $q ) : array(); if ( ! $q ) { return; } if ( ! self::enabled() ) { update_option( self::QUEUE_OPTION, array(), false ); return; } $state = self::state(); if ( isset( $state['last_ping_ts'] ) && time() - (int) $state['last_ping_ts'] < self::MIN_INTERVAL ) { wp_schedule_single_event( (int) $state['last_ping_ts'] + self::MIN_INTERVAL, self::CRON ); return; } $batch = array_slice( $q, 0, 10000 ); update_option( self::QUEUE_OPTION, array_slice( $q, count( $batch ) ), false ); $body = array( 'host' => (string) wp_parse_url( home_url( '/' ), PHP_URL_HOST ), 'key' => Settings::indexnow_key(), 'keyLocation' => self::key_url(), 'urlList' => $batch, ); $res = wp_remote_post( self::ENDPOINT, array( 'timeout' => 10, 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8' ), 'body' => wp_json_encode( $body ), ) ); $status = is_wp_error( $res ) ? 'error: ' . $res->get_error_message() : (string) wp_remote_retrieve_response_code( $res ); update_option( self::STATE_OPTION, array( 'last_ping' => Util::iso(), 'last_ping_ts' => time(), 'last_status' => $status, 'last_count' => count( $batch ), ), false ); } }