array( 'blockType' => array( 'type' => 'string', 'default' => 'p', ), 'fontID' => array( 'type' => 'string', 'default' => '', ), 'variant' => array( 'type' => 'string', 'default' => 'normal', ), 'fontSize' => array( 'type' => 'number', ), 'lineHeight' => array( 'type' => 'number', ), 'align' => array( 'type' => 'string', ), 'content' => array( 'type' => 'string', ), 'color' => array( 'type' => 'string', ), ), 'render_callback' => 'olympus_google_fonts_block_render', ) ); } add_action( 'init', 'olympus_google_fonts_register_block' ); /** * Front end render function for 'olympus-google-fonts/google-fonts'. * * @param array $attributes The block attributes. */ function olympus_google_fonts_block_render( $attributes ) { $block_type = isset( $attributes['blockType'] ) ? esc_attr( $attributes['blockType'] ) : 'p'; $font_id = isset( $attributes['fontID'] ) ? sanitize_text_field( $attributes['fontID'] ) : ''; $variant = isset( $attributes['variant'] ) ? sanitize_text_field( $attributes['variant'] ) : ''; $font_size = isset( $attributes['fontSize'] ) ? intval( $attributes['fontSize'] ) : ''; $line_height = isset( $attributes['lineHeight'] ) ? floatval( $attributes['lineHeight'] ) : ''; $align = isset( $attributes['align'] ) ? sanitize_text_field( $attributes['align'] ) : ''; $content = isset( $attributes['content'] ) ? wp_kses_post( $attributes['content'] ) : ''; $color = isset( $attributes['color'] ) ? sanitize_text_field( $attributes['color'] ) : ''; $class_name = isset( $attributes['className'] ) ? sanitize_text_field( $attributes['className'] ) : ''; $output = ''; $style = ''; $forced = ogf_is_forced(); $is_custom_font = OGF_Fonts_Taxonomy::get_by_name($font_id); if ( $font_id ) { // standardize the format. $font_id_standardized = str_replace( '+', '-', strtolower( $font_id ) ); if ( array_key_exists( $font_id_standardized, OGF_Fonts::$google_fonts ) ) { $variants = OGF_Fonts::$google_fonts[ $font_id_standardized ]['v']; $variants_for_url = join( ',', array_keys( $variants ) ); wp_enqueue_style( 'google-font-' . $font_id_standardized, 'https://fonts.googleapis.com/css?family=' . $font_id . ':' . $variants_for_url . '&display=swap', array(), OGF_VERSION ); $font_family = esc_attr( str_replace( '+', ' ', $font_id ) ); } elseif ( isset( $is_custom_font['family'] ) ) { $font_family = $is_custom_font['family']; } else { $font_family = $font_id; } $style = "font-family: {$font_family}{$forced};"; } if ( $variant && '0' !== $variant ) { $style .= "font-weight: {$variant}{$forced};"; } if ( $font_size ) { $style .= "font-size: {$font_size}px{$forced};"; } if ( $line_height ) { $style .= "line-height: {$line_height}{$forced};"; } if ( $align ) { $style .= "text-align: {$align}{$forced};"; } if ( $color ) { $style .= "color: {$color}{$forced};"; } $output .= '<' . $block_type . ' class="fonts-plugin-block ' . $class_name . '" style="' . esc_attr( $style ) . '">'; $output .= $content; $output .= ''; return wp_kses_post( $output ); }