File manager - Edit - /home/verseaumee/middletvnew26/wp-content/plugins/shopbuild/includes/class-storebuild-custom-fonts.php
Back
<?php namespace StoreBuild; if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. } /** * Class StoreBuild_Custom_Fonts * * Handles Custom Fonts functionality: settings, Elementor integration, and frontend CSS. * * @package StoreBuild */ class StoreBuild_Custom_Fonts { const OPTION_NAME = 'storebuild_custom_fonts'; public function __construct() { add_action( 'init', [ $this, 'register_settings' ] ); add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_media' ] ); add_filter( 'elementor/fonts/groups', [ $this, 'add_elementor_font_group' ] ); add_filter( 'elementor/fonts/additional_fonts', [ $this, 'add_elementor_fonts' ] ); add_action( 'wp_head', [ $this, 'print_font_css' ] ); add_action( 'elementor/editor/after_enqueue_styles', [ $this, 'print_font_css' ] ); add_filter( 'upload_mimes', [$this, 'mime_types'], 99 ); add_filter( 'wp_check_filetype_and_ext', [$this, 'check_filetype'], 99, 4 ); } public function check_filetype( $data, $file, $filename, $mimes ) { if ( ! empty( $data['ext'] ) && ! empty( $data['type'] ) ) { return $data; } $file_ext = strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) ); if ( in_array( $file_ext, ['woff', 'woff2', 'ttf', 'otf', 'eot', 'svg'] ) ) { switch( $file_ext ) { case 'woff': $type = 'font/woff'; break; case 'woff2': $type = 'font/woff2'; break; case 'ttf': $type = 'font/ttf'; break; case 'otf': $type = 'font/otf'; break; case 'eot': $type = 'application/vnd.ms-fontobject'; break; case 'svg': $type = 'image/svg+xml'; break; default: $type = 'application/octet-stream'; } return [ 'ext' => $file_ext, 'type' => $type, 'proper_filename' => $data['proper_filename'] ]; } return $data; } public function mime_types($mimes) { $mimes['svg'] = 'image/svg+xml'; $mimes['svgz'] = 'image/svg+xml'; // Use multiple MIME types to be safe against different browser/server detection $mimes['woff'] = 'font/woff|application/font-woff|application/x-font-woff|application/octet-stream'; $mimes['woff2'] = 'font/woff2|application/font-woff2|application/x-font-woff2|application/octet-stream'; $mimes['ttf'] = 'font/ttf|application/font-ttf|application/x-font-ttf|application/x-font-truetype|application/octet-stream'; $mimes['eot'] = 'application/vnd.ms-fontobject|application/octet-stream'; $mimes['otf'] = 'font/otf|application/font-sfnt|application/x-font-opentype|application/octet-stream'; return $mimes; } public function register_settings() { register_setting( 'options', self::OPTION_NAME, [ 'type' => 'array', 'description' => 'Custom Fonts Data', 'show_in_rest' => [ 'schema' => [ 'type' => 'array', 'items' => [ 'type' => 'object', 'properties' => [ 'name' => [ 'type' => 'string' ], 'weight' => [ 'type' => 'string' ], // e.g., '400', 'bold' 'style' => [ 'type' => 'string' ], // e.g., 'normal', 'italic' 'url' => [ 'type' => 'string' ], 'id' => [ 'type' => 'integer' ], // Attachment ID 'type' => [ 'type' => 'string' ], // woff, ttf, etc. ], ], ], ], 'default' => [], ] ); } public function enqueue_media( $hook ) { // Only enqueue if we are on the StoreBuild settings page if ( 'toplevel_page_shopbuild' === $hook || false !== strpos( $hook, 'shopbuild' ) ) { wp_enqueue_media(); } } public function add_elementor_font_group( $font_groups ) { $new_group = array( 'storebuild_fonts' => __( 'StoreBuild Custom Fonts', 'shopbuild' ) ); $font_groups = $new_group + $font_groups; return $font_groups; } public function add_elementor_fonts( $fonts ) { $custom_fonts = get_option( self::OPTION_NAME, [] ); if ( ! empty( $custom_fonts ) && is_array( $custom_fonts ) ) { foreach ( $custom_fonts as $font ) { if ( ! empty( $font['name'] ) ) { $fonts[ $font['name'] ] = 'storebuild_fonts'; } } } return $fonts; } public function print_font_css() { $custom_fonts = get_option( self::OPTION_NAME, [] ); if ( empty( $custom_fonts ) || ! is_array( $custom_fonts ) ) { return; } echo '<style id="storebuild-custom-fonts-css">'; // Group by font family to handle multiple weights/styles $grouped_fonts = []; foreach ($custom_fonts as $font) { $name = $font['name']; if (!isset($grouped_fonts[$name])) { $grouped_fonts[$name] = []; } $grouped_fonts[$name][] = $font; } foreach ( $grouped_fonts as $font_name => $variations ) { foreach ($variations as $font) { if ( empty( $font['url'] ) ) { continue; } $weight = ! empty( $font['weight'] ) ? $font['weight'] : 'normal'; $style = ! empty( $font['style'] ) ? $font['style'] : 'normal'; $url = esc_url( $font['url'] ); $family = esc_attr( $font['name'] ); // Try to determine format from URL if type is missing $format_string = ''; $ext = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION); switch($ext) { case 'woff': $format_string = "format('woff')"; break; case 'woff2': $format_string = "format('woff2')"; break; case 'ttf': $format_string = "format('truetype')"; break; case 'svg': $format_string = "format('svg')"; break; case 'eot': $format_string = "format('embedded-opentype')"; break; } echo "@font-face { font-family: '{$family}'; src: url('{$url}') {$format_string}; font-weight: {$weight}; font-style: {$style}; font-display: swap; }"; } } echo '</style>'; } }
| ver. 1.4 |
Github
|
.
| PHP 8.5.7 | Generation time: 0 |
proxy
|
phpinfo
|
Settings