Edit File by line
/home/zeestwma/ceyloniy.../wp-inclu.../fonts
File: class-wp-font-collection.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Font Collection class.
[2] Fix | Delete
*
[3] Fix | Delete
* This file contains the Font Collection class definition.
[4] Fix | Delete
*
[5] Fix | Delete
* @package WordPress
[6] Fix | Delete
* @subpackage Fonts
[7] Fix | Delete
* @since 6.5.0
[8] Fix | Delete
*/
[9] Fix | Delete
[10] Fix | Delete
/**
[11] Fix | Delete
* Font Collection class.
[12] Fix | Delete
*
[13] Fix | Delete
* @since 6.5.0
[14] Fix | Delete
*
[15] Fix | Delete
* @see wp_register_font_collection()
[16] Fix | Delete
*/
[17] Fix | Delete
final class WP_Font_Collection {
[18] Fix | Delete
/**
[19] Fix | Delete
* The unique slug for the font collection.
[20] Fix | Delete
*
[21] Fix | Delete
* @since 6.5.0
[22] Fix | Delete
* @var string
[23] Fix | Delete
*/
[24] Fix | Delete
public $slug;
[25] Fix | Delete
[26] Fix | Delete
/**
[27] Fix | Delete
* Font collection data.
[28] Fix | Delete
*
[29] Fix | Delete
* @since 6.5.0
[30] Fix | Delete
* @var array|WP_Error|null
[31] Fix | Delete
*/
[32] Fix | Delete
private $data;
[33] Fix | Delete
[34] Fix | Delete
/**
[35] Fix | Delete
* Font collection JSON file path or URL.
[36] Fix | Delete
*
[37] Fix | Delete
* @since 6.5.0
[38] Fix | Delete
* @var string|null
[39] Fix | Delete
*/
[40] Fix | Delete
private $src;
[41] Fix | Delete
[42] Fix | Delete
/**
[43] Fix | Delete
* WP_Font_Collection constructor.
[44] Fix | Delete
*
[45] Fix | Delete
* @since 6.5.0
[46] Fix | Delete
*
[47] Fix | Delete
* @param string $slug Font collection slug. May only contain alphanumeric characters, dashes,
[48] Fix | Delete
* and underscores. See sanitize_title().
[49] Fix | Delete
* @param array $args Font collection data. See wp_register_font_collection() for information on accepted arguments.
[50] Fix | Delete
*/
[51] Fix | Delete
public function __construct( string $slug, array $args ) {
[52] Fix | Delete
$this->slug = sanitize_title( $slug );
[53] Fix | Delete
if ( $this->slug !== $slug ) {
[54] Fix | Delete
_doing_it_wrong(
[55] Fix | Delete
__METHOD__,
[56] Fix | Delete
/* translators: %s: Font collection slug. */
[57] Fix | Delete
sprintf( __( 'Font collection slug "%s" is not valid. Slugs must use only alphanumeric characters, dashes, and underscores.' ), $slug ),
[58] Fix | Delete
'6.5.0'
[59] Fix | Delete
);
[60] Fix | Delete
}
[61] Fix | Delete
[62] Fix | Delete
$required_properties = array( 'name', 'font_families' );
[63] Fix | Delete
[64] Fix | Delete
if ( isset( $args['font_families'] ) && is_string( $args['font_families'] ) ) {
[65] Fix | Delete
// JSON data is lazy loaded by ::get_data().
[66] Fix | Delete
$this->src = $args['font_families'];
[67] Fix | Delete
unset( $args['font_families'] );
[68] Fix | Delete
[69] Fix | Delete
$required_properties = array( 'name' );
[70] Fix | Delete
}
[71] Fix | Delete
[72] Fix | Delete
$this->data = $this->sanitize_and_validate_data( $args, $required_properties );
[73] Fix | Delete
}
[74] Fix | Delete
[75] Fix | Delete
/**
[76] Fix | Delete
* Retrieves the font collection data.
[77] Fix | Delete
*
[78] Fix | Delete
* @since 6.5.0
[79] Fix | Delete
*
[80] Fix | Delete
* @return array|WP_Error An array containing the font collection data, or a WP_Error on failure.
[81] Fix | Delete
*/
[82] Fix | Delete
public function get_data() {
[83] Fix | Delete
if ( is_wp_error( $this->data ) ) {
[84] Fix | Delete
return $this->data;
[85] Fix | Delete
}
[86] Fix | Delete
[87] Fix | Delete
// If the collection uses JSON data, load it and cache the data/error.
[88] Fix | Delete
if ( isset( $this->src ) ) {
[89] Fix | Delete
$this->data = $this->load_from_json( $this->src );
[90] Fix | Delete
}
[91] Fix | Delete
[92] Fix | Delete
if ( is_wp_error( $this->data ) ) {
[93] Fix | Delete
return $this->data;
[94] Fix | Delete
}
[95] Fix | Delete
[96] Fix | Delete
// Set defaults for optional properties.
[97] Fix | Delete
$defaults = array(
[98] Fix | Delete
'description' => '',
[99] Fix | Delete
'categories' => array(),
[100] Fix | Delete
);
[101] Fix | Delete
[102] Fix | Delete
return wp_parse_args( $this->data, $defaults );
[103] Fix | Delete
}
[104] Fix | Delete
[105] Fix | Delete
/**
[106] Fix | Delete
* Loads font collection data from a JSON file or URL.
[107] Fix | Delete
*
[108] Fix | Delete
* @since 6.5.0
[109] Fix | Delete
*
[110] Fix | Delete
* @param string $file_or_url File path or URL to a JSON file containing the font collection data.
[111] Fix | Delete
* @return array|WP_Error An array containing the font collection data on success,
[112] Fix | Delete
* else an instance of WP_Error on failure.
[113] Fix | Delete
*/
[114] Fix | Delete
private function load_from_json( $file_or_url ) {
[115] Fix | Delete
$url = wp_http_validate_url( $file_or_url );
[116] Fix | Delete
$file = file_exists( $file_or_url ) ? wp_normalize_path( realpath( $file_or_url ) ) : false;
[117] Fix | Delete
[118] Fix | Delete
if ( ! $url && ! $file ) {
[119] Fix | Delete
// translators: %s: File path or URL to font collection JSON file.
[120] Fix | Delete
$message = __( 'Font collection JSON file is invalid or does not exist.' );
[121] Fix | Delete
_doing_it_wrong( __METHOD__, $message, '6.5.0' );
[122] Fix | Delete
return new WP_Error( 'font_collection_json_missing', $message );
[123] Fix | Delete
}
[124] Fix | Delete
[125] Fix | Delete
$data = $url ? $this->load_from_url( $url ) : $this->load_from_file( $file );
[126] Fix | Delete
[127] Fix | Delete
if ( is_wp_error( $data ) ) {
[128] Fix | Delete
return $data;
[129] Fix | Delete
}
[130] Fix | Delete
[131] Fix | Delete
$data = array(
[132] Fix | Delete
'name' => $this->data['name'],
[133] Fix | Delete
'font_families' => $data['font_families'],
[134] Fix | Delete
);
[135] Fix | Delete
[136] Fix | Delete
if ( isset( $this->data['description'] ) ) {
[137] Fix | Delete
$data['description'] = $this->data['description'];
[138] Fix | Delete
}
[139] Fix | Delete
[140] Fix | Delete
if ( isset( $this->data['categories'] ) ) {
[141] Fix | Delete
$data['categories'] = $this->data['categories'];
[142] Fix | Delete
}
[143] Fix | Delete
[144] Fix | Delete
return $data;
[145] Fix | Delete
}
[146] Fix | Delete
[147] Fix | Delete
/**
[148] Fix | Delete
* Loads the font collection data from a JSON file path.
[149] Fix | Delete
*
[150] Fix | Delete
* @since 6.5.0
[151] Fix | Delete
*
[152] Fix | Delete
* @param string $file File path to a JSON file containing the font collection data.
[153] Fix | Delete
* @return array|WP_Error An array containing the font collection data on success,
[154] Fix | Delete
* else an instance of WP_Error on failure.
[155] Fix | Delete
*/
[156] Fix | Delete
private function load_from_file( $file ) {
[157] Fix | Delete
$data = wp_json_file_decode( $file, array( 'associative' => true ) );
[158] Fix | Delete
if ( empty( $data ) ) {
[159] Fix | Delete
return new WP_Error( 'font_collection_decode_error', __( 'Error decoding the font collection JSON file contents.' ) );
[160] Fix | Delete
}
[161] Fix | Delete
[162] Fix | Delete
return $this->sanitize_and_validate_data( $data, array( 'font_families' ) );
[163] Fix | Delete
}
[164] Fix | Delete
[165] Fix | Delete
/**
[166] Fix | Delete
* Loads the font collection data from a JSON file URL.
[167] Fix | Delete
*
[168] Fix | Delete
* @since 6.5.0
[169] Fix | Delete
*
[170] Fix | Delete
* @param string $url URL to a JSON file containing the font collection data.
[171] Fix | Delete
* @return array|WP_Error An array containing the font collection data on success,
[172] Fix | Delete
* else an instance of WP_Error on failure.
[173] Fix | Delete
*/
[174] Fix | Delete
private function load_from_url( $url ) {
[175] Fix | Delete
// Limit key to 167 characters to avoid failure in the case of a long URL.
[176] Fix | Delete
$transient_key = substr( 'wp_font_collection_url_' . $url, 0, 167 );
[177] Fix | Delete
$data = get_site_transient( $transient_key );
[178] Fix | Delete
[179] Fix | Delete
if ( false === $data ) {
[180] Fix | Delete
$response = wp_safe_remote_get( $url );
[181] Fix | Delete
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
[182] Fix | Delete
return new WP_Error(
[183] Fix | Delete
'font_collection_request_error',
[184] Fix | Delete
sprintf(
[185] Fix | Delete
// translators: %s: Font collection URL.
[186] Fix | Delete
__( 'Error fetching the font collection data from "%s".' ),
[187] Fix | Delete
$url
[188] Fix | Delete
)
[189] Fix | Delete
);
[190] Fix | Delete
}
[191] Fix | Delete
[192] Fix | Delete
$data = json_decode( wp_remote_retrieve_body( $response ), true );
[193] Fix | Delete
if ( empty( $data ) ) {
[194] Fix | Delete
return new WP_Error( 'font_collection_decode_error', __( 'Error decoding the font collection data from the HTTP response JSON.' ) );
[195] Fix | Delete
}
[196] Fix | Delete
[197] Fix | Delete
// Make sure the data is valid before storing it in a transient.
[198] Fix | Delete
$data = $this->sanitize_and_validate_data( $data, array( 'font_families' ) );
[199] Fix | Delete
if ( is_wp_error( $data ) ) {
[200] Fix | Delete
return $data;
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
set_site_transient( $transient_key, $data, DAY_IN_SECONDS );
[204] Fix | Delete
}
[205] Fix | Delete
[206] Fix | Delete
return $data;
[207] Fix | Delete
}
[208] Fix | Delete
[209] Fix | Delete
/**
[210] Fix | Delete
* Sanitizes and validates the font collection data.
[211] Fix | Delete
*
[212] Fix | Delete
* @since 6.5.0
[213] Fix | Delete
*
[214] Fix | Delete
* @param array $data Font collection data to sanitize and validate.
[215] Fix | Delete
* @param array $required_properties Required properties that must exist in the passed data.
[216] Fix | Delete
* @return array|WP_Error Sanitized data if valid, otherwise a WP_Error instance.
[217] Fix | Delete
*/
[218] Fix | Delete
private function sanitize_and_validate_data( $data, $required_properties = array() ) {
[219] Fix | Delete
$schema = self::get_sanitization_schema();
[220] Fix | Delete
$data = WP_Font_Utils::sanitize_from_schema( $data, $schema );
[221] Fix | Delete
[222] Fix | Delete
foreach ( $required_properties as $property ) {
[223] Fix | Delete
if ( empty( $data[ $property ] ) ) {
[224] Fix | Delete
$message = sprintf(
[225] Fix | Delete
// translators: 1: Font collection slug, 2: Missing property name, e.g. "font_families".
[226] Fix | Delete
__( 'Font collection "%1$s" has missing or empty property: "%2$s".' ),
[227] Fix | Delete
$this->slug,
[228] Fix | Delete
$property
[229] Fix | Delete
);
[230] Fix | Delete
_doing_it_wrong( __METHOD__, $message, '6.5.0' );
[231] Fix | Delete
return new WP_Error( 'font_collection_missing_property', $message );
[232] Fix | Delete
}
[233] Fix | Delete
}
[234] Fix | Delete
[235] Fix | Delete
return $data;
[236] Fix | Delete
}
[237] Fix | Delete
[238] Fix | Delete
/**
[239] Fix | Delete
* Retrieves the font collection sanitization schema.
[240] Fix | Delete
*
[241] Fix | Delete
* @since 6.5.0
[242] Fix | Delete
*
[243] Fix | Delete
* @return array Font collection sanitization schema.
[244] Fix | Delete
*/
[245] Fix | Delete
private static function get_sanitization_schema() {
[246] Fix | Delete
return array(
[247] Fix | Delete
'name' => 'sanitize_text_field',
[248] Fix | Delete
'description' => 'sanitize_text_field',
[249] Fix | Delete
'font_families' => array(
[250] Fix | Delete
array(
[251] Fix | Delete
'font_family_settings' => array(
[252] Fix | Delete
'name' => 'sanitize_text_field',
[253] Fix | Delete
'slug' => static function ( $value ) {
[254] Fix | Delete
return _wp_to_kebab_case( sanitize_title( $value ) );
[255] Fix | Delete
},
[256] Fix | Delete
'fontFamily' => 'WP_Font_Utils::sanitize_font_family',
[257] Fix | Delete
'preview' => 'sanitize_url',
[258] Fix | Delete
'fontFace' => array(
[259] Fix | Delete
array(
[260] Fix | Delete
'fontFamily' => 'sanitize_text_field',
[261] Fix | Delete
'fontStyle' => 'sanitize_text_field',
[262] Fix | Delete
'fontWeight' => 'sanitize_text_field',
[263] Fix | Delete
'src' => static function ( $value ) {
[264] Fix | Delete
return is_array( $value )
[265] Fix | Delete
? array_map( 'sanitize_text_field', $value )
[266] Fix | Delete
: sanitize_text_field( $value );
[267] Fix | Delete
},
[268] Fix | Delete
'preview' => 'sanitize_url',
[269] Fix | Delete
'fontDisplay' => 'sanitize_text_field',
[270] Fix | Delete
'fontStretch' => 'sanitize_text_field',
[271] Fix | Delete
'ascentOverride' => 'sanitize_text_field',
[272] Fix | Delete
'descentOverride' => 'sanitize_text_field',
[273] Fix | Delete
'fontVariant' => 'sanitize_text_field',
[274] Fix | Delete
'fontFeatureSettings' => 'sanitize_text_field',
[275] Fix | Delete
'fontVariationSettings' => 'sanitize_text_field',
[276] Fix | Delete
'lineGapOverride' => 'sanitize_text_field',
[277] Fix | Delete
'sizeAdjust' => 'sanitize_text_field',
[278] Fix | Delete
'unicodeRange' => 'sanitize_text_field',
[279] Fix | Delete
),
[280] Fix | Delete
),
[281] Fix | Delete
),
[282] Fix | Delete
'categories' => array( 'sanitize_title' ),
[283] Fix | Delete
),
[284] Fix | Delete
),
[285] Fix | Delete
'categories' => array(
[286] Fix | Delete
array(
[287] Fix | Delete
'name' => 'sanitize_text_field',
[288] Fix | Delete
'slug' => 'sanitize_title',
[289] Fix | Delete
),
[290] Fix | Delete
),
[291] Fix | Delete
);
[292] Fix | Delete
}
[293] Fix | Delete
}
[294] Fix | Delete
[295] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function