/home/techb158/public_html/wp-content/plugins/meta-box/inc/fields
Edit: /home/techb158/public_html/wp-content/plugins/meta-box/inc/fields/icon.php (8813B)
$field['icon_file'],
'icon_dir' => $field['icon_dir'],
'icon_css' => is_string( $field['icon_css'] ) ? $field['icon_css'] : '',
];
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
$cache_key = md5( serialize( $params ) ) . '-icons';
$icons = wp_cache_get( $cache_key, self::CACHE_GROUP );
if ( false !== $icons ) {
return $icons;
}
$data = self::parse_icon_data( $field );
// Reformat icons.
$icons = [];
foreach ( $data as $key => $icon ) {
$icon = self::normalize_icon( $field, $key, $icon );
if ( is_numeric( key( $icon ) ) ) {
$icons = array_merge( $icons, $icon );
continue;
}
$icons[] = $icon;
}
// Cache the result.
wp_cache_set( $cache_key, $icons, self::CACHE_GROUP );
return $icons;
}
private static function parse_icon_data( array $field ): array {
$keys = [
'icon_file',
'icon_css',
'icon_dir',
];
foreach ( $keys as $key ) {
if ( ! empty( $field[ $key ] ) && is_string( $field[ $key ] ) ) {
return call_user_func( [ __CLASS__, "parse_$key" ], $field );
}
}
return [];
}
private static function parse_icon_file( array $field ): array {
if ( ! file_exists( $field['icon_file'] ) ) {
return [];
}
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$data = (string) file_get_contents( $field['icon_file'] );
$decoded = json_decode( $data, true );
// JSON file.
if ( JSON_ERROR_NONE === json_last_error() ) {
return $decoded;
}
// Text file: each icon on a line.
return array_map( 'trim', explode( "\n", $data ) );
}
private static function parse_icon_css( array $field ): array {
// Parse local CSS file only.
$file = self::url_to_path( $field['icon_css'] );
if ( ! file_exists( $file ) ) {
return [];
}
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$css = (string) file_get_contents( $file );
preg_match_all( '/\.([^\s:]+):before/', $css, $matches );
if ( empty( $matches[1] ) ) {
preg_match_all( '/\.([^\s:]+)/', $css, $matches );
}
return $matches[1];
}
private static function parse_icon_dir( array $field ): array {
$dir = $field['icon_dir'];
if ( ! is_dir( $dir ) ) {
return [];
}
$icons = [];
$files = glob( trailingslashit( $dir ) . '*.svg' );
foreach ( $files as $file ) {
$filename = substr( basename( $file ), 0, -4 );
$icons[] = [
'value' => $filename,
'label' => $filename,
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
'svg' => file_get_contents( $file ),
];
}
return $icons;
}
private static function normalize_icon( array $field, $key, $icon ): array {
// Default: Font Awesome Free.
if ( $field['icon_set'] === 'font-awesome-free' ) {
$style = $icon['styles'][0];
return [
'value' => "fa-{$style} fa-{$key}",
'label' => $icon['label'],
'svg' => $icon['svg'][ $style ]['raw'],
];
}
// Font Awesome Pro.
if ( $field['icon_set'] === 'font-awesome-pro' ) {
$icons = [];
foreach ( $icon['styles'] as $style ) {
$icons[] = [
'value' => "fa-{$style} fa-{$key}",
'label' => "{$icon[ 'label' ]} ({$style})",
'svg' => $icon['svg'][ $style ]['raw'],
];
}
return $icons;
}
// JSON file: "icon-class": { "label": "Label", "svg": "
" } or from `icon_dir`.
if ( is_array( $icon ) ) {
return [
'value' => $icon['value'] ?? $key,
'label' => $icon['label'] ?? $key,
'svg' => $icon['svg'] ?? '',
];
}
// JSON file: "icon-class": "Label" or "icon-class": "".
if ( is_string( $key ) ) {
$label = str_contains( $icon, '