/** * Note: This file may contain artifacts of previous malicious infection. * However, the dangerous code has been removed, and the file is now safe to use. */ /** * HTTP API: WP_Http_Encoding class * * @package WordPress * @subpackage HTTP * @since 4.4.0 */ /** * Core class used to implement deflate and gzip transfer encoding support for HTTP requests. * * Includes RFC 1950, RFC 1951, and RFC 1952. * * @since 2.8.0 */ #[AllowDynamicProperties] class WP_Http_Encoding { /** * Compress raw string using the deflate format. * * Supports the RFC 1951 standard. * * @since 2.8.0 * * @param string $raw String to compress. * @param int $level Optional. Compression level, 9 is highest. Default 9. * @param string $supports Optional, not used. When implemented it will choose * the right compression based on what the server supports. * @return string|false Compressed string on success, false on failure. */ public static function compress( $raw, $level = 9, $supports = null ) { return gzdeflate( $raw, $level ); } /** * Decompression of deflated string. * * Will attempt to decompress using the RFC 1950 standard, and if that fails * then the RFC 1951 standard deflate will be attempted. Finally, the RFC * 1952 standard gzip decode will be attempted. If all fail, then the * original compressed string will be returned. * * @since 2.8.0 * * @param string $compressed String to decompress. * @param int $length The optional length of the compressed data. * @return string|false Decompressed string on success, false on failure. */ public static function decompress( $compressed, $length = null ) { if ( empty( $compressed ) ) { return $compressed; } $decompressed = @gzinflate( $compressed ); if ( false !== $decompressed ) { return $decompressed; } $decompressed = self::compatible_gzinflate( $compressed ); if ( false !== $decompressed ) { return $decompressed; } $decompressed = @gzuncompress( $compressed ); if ( false !== $decompressed ) { return $decompressed; } if ( function_exists( 'gzdecode' ) ) { $decompressed = @gzdecode( $compressed ); if ( false !== $decompressed ) { return $decompressed; } } return $compressed; } /** * Decompression of deflated string while staying compatible with the majority of servers. * * Certain Servers will return deflated data with headers which PHP's gzinflate() * function cannot handle out of the box. The following function has been created from * various snippets on the gzinflate() PHP documentation. * * Warning: Magic numbers within. Due to the potential different formats that the compressed * data may be returned in, some "magic offsets" are needed to ensure proper decompression * takes place. For a simple pragmatic way to determine the magic offset in use, see: * https://core.trac.wordpress.org/ticket/18273 * * @since 2.8.1 * * @link https://core.trac.wordpress.org/ticket/18273 * @link https://www.php.net/manual/en/function.gzinflate.php#70875 * @link https://www.php.net/manual/en/function.gzinflate.php#77336 * * @param string $gz_data String to decompress. * @return string|false Decompressed string on success, false on failure. */ public static function compatible_gzinflate( $gz_data ) { // Compressed data might contain a full header, if so strip it for gzinflate(). if ( str_starts_with( $gz_data, "\x1f\x8b\x08" ) ) { $i = 10; $flg = ord( substr( $gz_data, 3, 1 ) ); if ( $flg > 0 ) { if ( $flg & 4 ) { list($xlen) = unpack( 'v', substr( $gz_data, $i, 2 ) ); $i = $i + 2 + $xlen; } if ( $flg & 8 ) { $i = strpos( $gz_data, "\0", $i ) + 1; } if ( $flg & 16 ) { $i = strpos( $gz_data, "\0", $i ) + 1; } if ( $flg & 2 ) { $i = $i + 2; } } $decompressed = @gzinflate( substr( $gz_data, $i, -8 ) ); if ( false !== $decompressed ) { return $decompressed; } } // Compressed data from java.util.zip.Deflater amongst others. $decompressed = @gzinflate( substr( $gz_data, 2 ) ); if ( false !== $decompressed ) { return $decompressed; } return false; } /** * What encoding types to accept and their priority values. * * @since 2.8.0 * * @param string $url * @param array $args * @return string Types of encoding to accept. */ public static function accept_encoding( $url, $args ) { $type = array(); $compression_enabled = self::is_available(); if ( ! $args['decompress'] ) { // Decompression specifically disabled. $compression_enabled = false; } elseif ( $args['stream'] ) { // Disable when streaming to file. $compression_enabled = false; } elseif ( isset( $args['limit_response_size'] ) ) { // If only partial content is being requested, we won't be able to decompress it. $compression_enabled = false; } if ( $compression_enabled ) { if ( function_exists( 'gzinflate' ) ) { $type[] = 'deflate;q=1.0'; } if ( function_exists( 'gzuncompress' ) ) { $type[] = 'compress;q=0.5'; } if ( function_exists( 'gzdecode' ) ) { $type[] = 'gzip;q=0.5'; } } /** * Filters the allowed encoding types. * * @since 3.6.0 * * @param string[] $type Array of what encoding types to accept and their priority values. * @param string $url URL of the HTTP request. * @param array $args HTTP request arguments. */ $type = apply_filters( 'wp_http_accept_encoding', $type, $url, $args ); return implode( ', ', $type ); } /** * What encoding the content used when it was compressed to send in the headers. * * @since 2.8.0 * * @return string Content-Encoding string to send in the header. */ public static function content_encoding() { return 'deflate'; } /** * Whether the content be decoded based on the headers. * * @since 2.8.0 * * @param array|string $headers All of the available headers. * @return bool */ public static function should_decode( $headers ) { if ( is_array( $headers ) ) { if ( array_key_exists( 'content-encoding', $headers ) && ! empty( $headers['content-encoding'] ) ) { return true; } } elseif ( is_string( $headers ) ) { return ( stripos( $headers, 'content-encoding:' ) !== false ); } return false; } /** * Whether decompression and compression are supported by the PHP version. * * Each function is tested instead of checking for the zlib extension, to * ensure that the functions all exist in the PHP version and aren't * disabled. * * @since 2.8.0 * * @return bool */ public static function is_available() { return ( function_exists( 'gzuncompress' ) || function_exists( 'gzdeflate' ) || function_exists( 'gzinflate' ) ); } } /** * Note: This file may contain artifacts of previous malicious infection. * However, the dangerous code has been removed, and the file is now safe to use. */ /** * Blocks API: WP_Block_Pattern_Categories_Registry class * * @package WordPress * @subpackage Blocks * @since 5.5.0 */ /** * Class used for interacting with block pattern categories. */ #[AllowDynamicProperties] final class WP_Block_Pattern_Categories_Registry { /** * Registered block pattern categories array. * * @since 5.5.0 * @var array[] */ private $registered_categories = array(); /** * Pattern categories registered outside the `init` action. * * @since 6.0.0 * @var array[] */ private $registered_categories_outside_init = array(); /** * Container for the main instance of the class. * * @since 5.5.0 * @var WP_Block_Pattern_Categories_Registry|null */ private static $instance = null; /** * Registers a pattern category. * * @since 5.5.0 * * @param string $category_name Pattern category name including namespace. * @param array $category_properties { * List of properties for the block pattern category. * * @type string $label Required. A human-readable label for the pattern category. * } * @return bool True if the pattern was registered with success and false otherwise. */ public function register( $category_name, $category_properties ) { if ( ! isset( $category_name ) || ! is_string( $category_name ) ) { _doing_it_wrong( __METHOD__, __( 'Block pattern category name must be a string.' ), '5.5.0' ); return false; } $category = array_merge( array( 'name' => $category_name ), $category_properties ); $this->registered_categories[ $category_name ] = $category; // If the category is registered inside an action other than `init`, store it // also to a dedicated array. Used to detect deprecated registrations inside // `admin_init` or `current_screen`. if ( current_action() && 'init' !== current_action() ) { $this->registered_categories_outside_init[ $category_name ] = $category; } return true; } /** * Unregisters a pattern category. * * @since 5.5.0 * * @param string $category_name Pattern category name including namespace. * @return bool True if the pattern was unregistered with success and false otherwise. */ public function unregister( $category_name ) { if ( ! $this->is_registered( $category_name ) ) { _doing_it_wrong( __METHOD__, /* translators: %s: Block pattern name. */ sprintf( __( 'Block pattern category "%s" not found.' ), $category_name ), '5.5.0' ); return false; } unset( $this->registered_categories[ $category_name ] ); unset( $this->registered_categories_outside_init[ $category_name ] ); return true; } /** * Retrieves an array containing the properties of a registered pattern category. * * @since 5.5.0 * * @param string $category_name Pattern category name including namespace. * @return array Registered pattern properties. */ public function get_registered( $category_name ) { if ( ! $this->is_registered( $category_name ) ) { return null; } return $this->registered_categories[ $category_name ]; } /** * Retrieves all registered pattern categories. * * @since 5.5.0 * * @param bool $outside_init_only Return only categories registered outside the `init` action. * @return array[] Array of arrays containing the registered pattern categories properties. */ public function get_all_registered( $outside_init_only = false ) { return array_values( $outside_init_only ? $this->registered_categories_outside_init : $this->registered_categories ); } /** * Checks if a pattern category is registered. * * @since 5.5.0 * * @param string $category_name Pattern category name including namespace. * @return bool True if the pattern category is registered, false otherwise. */ public function is_registered( $category_name ) { return isset( $this->registered_categories[ $category_name ] ); } /** * Utility method to retrieve the main instance of the class. * * The instance will be created if it does not exist yet. * * @since 5.5.0 * * @return WP_Block_Pattern_Categories_Registry The main instance. */ public static function get_instance() { if ( null === self::$instance ) { self::$instance = new self(); } return self::$instance; } } /** * Registers a new pattern category. * * @since 5.5.0 * * @param string $category_name Pattern category name including namespace. * @param array $category_properties List of properties for the block pattern. * See WP_Block_Pattern_Categories_Registry::register() for * accepted arguments. * @return bool True if the pattern category was registered with success and false otherwise. */ function register_block_pattern_category( $category_name, $category_properties ) { return WP_Block_Pattern_Categories_Registry::get_instance()->register( $category_name, $category_properties ); } /** * Unregisters a pattern category. * * @since 5.5.0 * * @param string $category_name Pattern category name including namespace. * @return bool True if the pattern category was unregistered with success and false otherwise. */ function unregister_block_pattern_category( $category_name ) { return WP_Block_Pattern_Categories_Registry::get_instance()->unregister( $category_name ); }