| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDeveloper\BetterDocs\Editors\BlockEditor; |
| 4 |
|
| 5 |
use WPDeveloper\BetterDocs\Utils\Base; |
| 6 |
|
| 7 |
class FontLoader extends Base { |
| 8 |
|
| 9 |
public static $registered_blocks = [ |
| 10 |
'betterdocs/betterdocs-print', |
| 11 |
'betterdocs/breadcrumb', |
| 12 |
'betterdocs/categorybox', |
| 13 |
'betterdocs/categorygrid', |
| 14 |
'betterdocs/category-slate-layout', |
| 15 |
'betterdocs/archive-doc-list', |
| 16 |
'betterdocs/single-doc-content', |
| 17 |
'betterdocs/date', |
| 18 |
'betterdocs/doc-navigation', |
| 19 |
'betterdocs/feedback-form', |
| 20 |
'betterdocs/reactions', |
| 21 |
'betterdocs/searchbox', |
| 22 |
'betterdocs/sidebar', |
| 23 |
'betterdocs/social-share', |
| 24 |
'betterdocs/table-of-contents', |
| 25 |
'betterdocs/multiple-kb', |
| 26 |
'betterdocs/betterdocs-encyclopedia', |
| 27 |
'betterdocs/glossary-single-template', |
| 28 |
'betterdocs/faq', |
| 29 |
'betterdocs/multiple-kb-tab', |
| 30 |
'betterdocs/categoryhandbook', |
| 31 |
'betterdocs/popular-docs', |
| 32 |
'betterdocs/related-categories', |
| 33 |
'betterdocs/code-snippet', |
| 34 |
'betterdocs/article-summary' |
| 35 |
]; |
| 36 |
|
| 37 |
public static $retrieved_fonts = []; |
| 38 |
|
| 39 |
public function __construct() { |
| 40 |
add_filter( 'render_block', [ $this, 'get_betterdocs_blocks' ], 10, 2 ); |
| 41 |
add_action( 'wp_footer', [ $this, 'enqueue_google_fonts' ], 10 ); |
| 42 |
} |
| 43 |
|
| 44 |
public function get_betterdocs_blocks( $block_content, $block ) { |
| 45 |
if ( isset( $block['attrs'] ) ) { |
| 46 |
if ( in_array( $block['blockName'], self::$registered_blocks ) ) { |
| 47 |
$fonts = self::get_fonts_family( $block['attrs'] ); |
| 48 |
self::$retrieved_fonts = array_unique( array_merge( self::$retrieved_fonts, $fonts ) ); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
return $block_content; |
| 53 |
} |
| 54 |
|
| 55 |
public static function get_fonts_family( $attributes ) { |
| 56 |
$keys = preg_grep( '/^(\w+)FontFamily/i', array_keys( $attributes ), 0 ); |
| 57 |
$google_font_family = []; |
| 58 |
foreach ( $keys as $key ) { |
| 59 |
$google_font_family[ $attributes[ $key ] ] = $attributes[ $key ]; |
| 60 |
} |
| 61 |
return $google_font_family; |
| 62 |
} |
| 63 |
|
| 64 |
public function enqueue_google_fonts() { |
| 65 |
if ( ! empty( self::$retrieved_fonts ) ) { |
| 66 |
$google_fonts = ''; |
| 67 |
$google_fonts_attr = ':100,100italic,200,200italic,300,300italic,400,400italic,500,500italic,600,600italic,700,700italic,800,800italic,900,900italic'; |
| 68 |
foreach ( self::$retrieved_fonts as $font ) { |
| 69 |
$google_fonts .= str_replace( ' ', '+', trim( $font ) ) . $google_fonts_attr . '|'; |
| 70 |
} |
| 71 |
if ( ! empty( $google_fonts ) ) { |
| 72 |
$query_args = [ |
| 73 |
'family' => $google_fonts |
| 74 |
]; |
| 75 |
wp_register_style( |
| 76 |
'betterdocs-goggle-fonts', |
| 77 |
add_query_arg( $query_args, '//fonts.googleapis.com/css' ), |
| 78 |
[], |
| 79 |
betterdocs()->version |
| 80 |
); |
| 81 |
wp_enqueue_style( 'betterdocs-goggle-fonts' ); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|