self::MAX_URL_LENGTH ) { return ''; } // Only enqueue public, same-site HTTP(S) URLs. Do not allow userinfo, // non-standard ports or protocol-relative URLs into the HTTP worker. $parts = wp_parse_url( $url ); if ( ! is_array( $parts ) || empty( $parts['scheme'] ) || empty( $parts['host'] ) || ! in_array( strtolower( $parts['scheme'] ), array( 'http', 'https' ), true ) || isset( $parts['user'] ) || isset( $parts['pass'] ) || isset( $parts['port'] ) || isset( $parts['fragment'] ) ) { return ''; } if ( function_exists( 'home_url' ) ) { $home = wp_parse_url( home_url( '/' ) ); if ( ! is_array( $home ) || empty( $home['host'] ) || strtolower( $parts['host'] ) !== strtolower( $home['host'] ) || strtolower( $parts['scheme'] ) !== strtolower( $home['scheme'] ) ) { return ''; } } if ( filter_var( $parts['host'], FILTER_VALIDATE_IP ) ) { return ''; } return $url; } /** * Merge new URLs into an existing queue list (unique, capped). * * @param string[] $current Current queue. * @param string[] $incoming New URLs. * @return string[] */ public static function merge( array $current, array $incoming ) { $seen = array(); $out = array(); foreach ( array_merge( $current, $incoming ) as $url ) { $url = self::normalize_url( $url ); if ( '' === $url ) { continue; } $key = strtolower( $url ); if ( isset( $seen[ $key ] ) ) { continue; } $seen[ $key ] = true; $out[] = $url; if ( count( $out ) >= self::MAX_QUEUE ) { break; } } return $out; } /** * Take up to $n URLs from the front of the queue. * * @param string[] $queue Queue. * @param int $n Batch size. * @return array{0: string[], 1: string[]} [ batch, remaining ] */ public static function take( array $queue, $n ) { $n = max( 1, (int) $n ); $batch = array_slice( $queue, 0, $n ); $rest = array_slice( $queue, count( $batch ) ); return array( $batch, $rest ); } /** @return string[] */ public static function get() { $q = get_option( self::OPTION, array() ); return is_array( $q ) ? array_values( $q ) : array(); } /** @param string[] $urls */ public static function set( array $urls ) { update_option( self::OPTION, array_values( $urls ), false ); } /** @param string[] $urls */ public static function enqueue( array $urls ) { self::set( self::merge( self::get(), $urls ) ); } /** * Put URLs at the front of the queue (homepage / priority). * * @param string[] $urls URLs. */ public static function enqueue_priority( array $urls ) { $norm = array(); foreach ( $urls as $u ) { $u = self::normalize_url( $u ); if ( '' !== $u ) { $norm[] = $u; } } if ( ! $norm ) { return; } // Priority first, then existing (deduped). self::set( self::merge( $norm, self::get() ) ); } /** * Re-queue failed URLs at the end (capped). * * @param string[] $urls Failed URLs. */ public static function requeue_failed( array $urls ) { if ( ! $urls ) { return; } self::enqueue( $urls ); } public static function clear() { delete_option( self::OPTION ); } public static function count() { return count( self::get() ); } }