name = esc_html__( 'Custom Fonts', 'powerkit' ); $this->desc = esc_html__( 'Adds the ability to download custom fonts.', 'powerkit' ); $this->slug = 'custom_fonts'; $this->type = 'default'; $this->category = 'basic'; $this->priority = 1040; $this->public = true; $this->enabled = false; $this->badge = esc_html__( 'Advanced', 'powerkit' ); $this->load_extensions = array( 'fonts', ); $this->links = array( array( 'name' => esc_html__( 'Go to settings', 'powerkit' ), 'url' => powerkit_get_page_url( 'fonts&tab=' . $this->slug, 'themes' ), ), array( 'name' => esc_html__( 'View documentation', 'powerkit' ), 'url' => powerkit_get_setting( 'documentation' ) . '/content-presentation/custom-fonts/', 'target' => '_blank', ), ); } /** * Initialize module */ public function initialize() { add_filter( 'powerkit_fonts_list', array( $this, 'custom_fonts' ), 10 ); add_filter( 'csco_customizer_fonts_choices', array( $this, 'csco_custom_fonts' ), 20 ); add_filter( 'upload_mimes', array( $this, 'allow_mimes' ) ); add_filter( 'powerkit_fonts_register_settings', array( $this, 'register_settings' ), 10 ); add_action( 'customize_controls_print_styles', array( $this, 'frontend_enqueue' ), 100 ); add_action( 'wp_head', array( $this, 'frontend_enqueue' ), 100 ); add_action( 'admin_head', array( $this, 'editor_enqueue' ), 100 ); add_filter( 'init', array( $this, 'set_load_method' ) ); add_filter( 'elementor/fonts/groups', array( $this, 'elementor_group' ) ); add_filter( 'elementor/fonts/additional_fonts', array( $this, 'add_elementor_fonts' ) ); } /** * Set Webfonts method */ public function set_load_method() { $this->load_method = apply_filters( 'powerkit_webfonts_load_method', 'async' ); } /** * Add custom fonts * * @since 1.0.0 * @param array $fonts List fonts. * @return array */ public function custom_fonts( $fonts ) { if ( is_customize_preview() ) { $custom_fonts = get_option( 'powerkit_custom_fonts_list' ); if ( is_array( $custom_fonts ) && $custom_fonts ) { $exclude = array(); $fonts['families']['custom_fonts'] = array( 'text' => esc_html__( 'Custom Fonts', 'powerkit' ), 'children' => array(), ); foreach ( $custom_fonts as $key => $item ) { if ( 'clone' === $key ) { continue; } $id = sanitize_title( $item['name'] ); if ( $id ) { if ( ! in_array( $id, $exclude, true ) ) { $fonts['families']['custom_fonts']['children'][] = array( 'id' => $id, 'text' => $item['name'], ); $exclude[] = $id; } $variant = str_replace( '400', 'regular', $item['weight'] ); if ( 'italic' === $item['style'] ) { $variant .= 'italic'; } if ( isset( $fonts['variants'][ $id ] ) && $fonts['variants'][ $id ] ) { if ( ! in_array( $variant, $fonts['variants'][ $id ], true ) ) { $fonts['variants'][ $id ][] = $variant; } } else { $fonts['variants'][ $id ][] = $variant; } } } } } return $fonts; } /** * Add custom fonts to csco theme * * @since 1.0.0 * @param array $fonts List fonts. * @return array */ public function csco_custom_fonts( $fonts ) { if ( is_customize_preview() ) { $custom_fonts = get_option( 'powerkit_custom_fonts_list' ); if ( is_array( $custom_fonts ) && $custom_fonts ) { $exclude = array(); $fonts['fonts']['families']['custom_fonts'] = array( 'text' => esc_html__( 'Custom Fonts', 'powerkit' ), 'children' => array(), ); foreach ( $custom_fonts as $key => $item ) { if ( 'clone' === $key ) { continue; } $id = sanitize_title( $item['name'] ); if ( $id ) { if ( ! in_array( $id, $exclude, true ) ) { $fonts['fonts']['families']['custom_fonts']['children'][] = array( 'id' => $id, 'text' => $item['name'], ); $exclude[] = $id; } $variant = str_replace( '400', 'regular', $item['weight'] ); if ( 'italic' === $item['style'] ) { $variant .= 'italic'; } if ( isset( $fonts['fonts']['variants'][ $id ] ) && $fonts['fonts']['variants'][ $id ] ) { if ( ! in_array( $variant, $fonts['fonts']['variants'][ $id ], true ) ) { $fonts['fonts']['variants'][ $id ][] = $variant; } } else { $fonts['fonts']['variants'][ $id ][] = $variant; } } } } } return $fonts; } /** * Add Custom Font group to elementor font list. * * Group name "Custom" is added as the first element in the array. * * @param Array $font_groups default font groups in elementor. * @return Array Modified font groups with newly added font group. */ public function elementor_group( $font_groups ) { $new_group[ self::$font_base ] = esc_html__( 'Custom Fonts', 'powerkit' ); $font_groups = $new_group + $font_groups; return $font_groups; } /** * Add Custom Fonts to the Elementor Page builder's font param. * * @param Array $fonts Custom Font's array. */ public function add_elementor_fonts( $fonts ) { $fonts_list = get_option( 'powerkit_custom_fonts_list' ); if ( is_array( $fonts_list ) && $fonts_list ) { foreach ( $fonts_list as $item ) { $id = sanitize_title( $item['name'] ); if ( $id ) { $fonts[ $id ] = self::$font_base; } } } return $fonts; } /** * Register new mime types and file extensions. * * @since 1.0.0 * @param array $mimes Current array of mime types.. */ public function allow_mimes( $mimes ) { $mimes['woff'] = 'application/x-font-woff'; $mimes['woff2'] = 'application/x-font-woff2'; return $mimes; } /** * Register settings * * @since 1.0.0 * @param array $settings List settings. * @return array */ public function register_settings( $settings ) { $settings[] = array( 'id' => $this->slug, 'name' => esc_html__( 'Custom Fonts', 'powerkit' ), 'function' => array( $this, 'build_setting_page' ), ); return $settings; } /** * Build admin page * * @since 1.0.0 */ public function build_setting_page() { powerkit_uuid_hash(); $this->save_options_page(); $powerkit_custom_fonts_id = get_option( 'powerkit_custom_fonts_counter', 1 ); $params = array( 'name' => '', 'weight' => '400', 'style' => 'normal', 'file_woff' => '', 'file_woff2' => '', ); $action = 'new'; // Check wpnonce. if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'] ) ) { // Input var ok; sanitization ok. return; } // Check custom action. if ( isset( $_REQUEST['action'] ) ) { // Input var ok; sanitization ok. $action = sanitize_title( $_REQUEST['action'] ); // Input var ok; sanitization ok. if ( isset( $_REQUEST['powerkit_custom_fonts_id'] ) && 'edit' === $action ) { // Input var ok. $powerkit_custom_fonts_id = sanitize_key( $_REQUEST['powerkit_custom_fonts_id'] ); // Input var ok. } } // Edit font. if ( 'edit' === $action ) { $font_list = get_option( 'powerkit_custom_fonts_list' ); if ( isset( $font_list[ $powerkit_custom_fonts_id ] ) ) { $params = array_merge( (array) $params, (array) $font_list[ $powerkit_custom_fonts_id ] ); } } // Delete font. if ( 'delete' === $action ) { $this->delete_font(); } // Settings page link. $powerkit_custom_fonts_link = powerkit_get_page_url( 'fonts&tab=' . $this->slug, 'themes' ); ?>

  1. %2$s', esc_url( 'https://www.fontsquirrel.com/tools/webfont-generator' ), esc_html__( 'Font Squirrel', 'powerkit' ) ); // translators: args - format woff, format woff, format woff2, link webfont-generator. echo sprintf( esc_html__( 'Prepare your webfont files in %1$s and %2$s formats. You may convert your %3$s fonts at %4$s.', 'powerkit' ), 'woff', 'woff2', 'ttf', wp_kses( $link_webfont, 'post' ) ); ?>

$row ) { $families_value[ $key ] = $row['name']; $families_name[ $key ] = $row['weight']; $families_order[ $key ] = $row['style']; } array_multisort( $families_value, SORT_DESC, $families_order, SORT_DESC, $families_name, SORT_ASC, $families, $families_keys ); $families = array_combine( $families_keys, $families ); } // Loop families. if ( $families ) { foreach ( $families as $key => $family ) { $edit_link = add_query_arg( array( '_wpnonce' => wp_create_nonce(), 'powerkit_custom_fonts_id' => $key, 'action' => 'edit', ), $powerkit_custom_fonts_link ); $delete_link = add_query_arg( array( '_wpnonce' => wp_create_nonce(), 'powerkit_custom_fonts_id' => $key, 'action' => 'delete', ), $powerkit_custom_fonts_link ); ?>
| |

%s

', esc_html__( 'Font successfully deleted.', 'powerkit' ) ); } } /** * Settings save * * @since 1.0.0 */ protected function save_options_page() { if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'] ) ) { // Input var ok; sanitization ok. return; } if ( ! isset( $_POST['powerkit_custom_fonts_id'] ) ) { // Input var ok. return; } // Update id counter. if ( isset( $_POST['save_settings'] ) ) { // Input var ok. update_option( 'powerkit_custom_fonts_counter', intval( get_option( 'powerkit_custom_fonts_counter', 1 ) ) + 1 ); } // ID Font. $id = sanitize_key( $_POST['powerkit_custom_fonts_id'] ); // Input var ok. // Custom List. $font_list = (array) get_option( 'powerkit_custom_fonts_list', array() ); // Reset current settings. $font_list[ $id ] = array(); // Set new settings. if ( isset( $_POST['powerkit_custom_fonts_name'] ) ) { // Input var ok. $font_list[ $id ]['slug'] = sanitize_title( $_POST['powerkit_custom_fonts_name'] ); // Input var ok; sanitization ok. } if ( isset( $_POST['powerkit_custom_fonts_name'] ) ) { // Input var ok. $font_list[ $id ]['name'] = sanitize_text_field( $_POST['powerkit_custom_fonts_name'] ); // Input var ok; sanitization ok. } if ( isset( $_POST['powerkit_custom_fonts_file_woff'] ) ) { // Input var ok. $font_list[ $id ]['file_woff'] = sanitize_text_field( $_POST['powerkit_custom_fonts_file_woff'] ); // Input var ok; sanitization ok. } if ( isset( $_POST['powerkit_custom_fonts_file_woff2'] ) ) { // Input var ok. $font_list[ $id ]['file_woff2'] = sanitize_text_field( $_POST['powerkit_custom_fonts_file_woff2'] ); // Input var ok; sanitization ok. } if ( isset( $_POST['powerkit_custom_fonts_weight'] ) ) { // Input var ok. $font_list[ $id ]['weight'] = sanitize_text_field( $_POST['powerkit_custom_fonts_weight'] ); // Input var ok; sanitization ok. } if ( isset( $_POST['powerkit_custom_fonts_style'] ) ) { // Input var ok. $font_list[ $id ]['style'] = sanitize_text_field( $_POST['powerkit_custom_fonts_style'] ); // Input var ok; sanitization ok. } // Save list. update_option( 'powerkit_custom_fonts_list', $font_list ); // Output message. printf( '

%s

', esc_html__( 'Settings saved.', 'powerkit' ) ); } /** * Register fonts in the editor. */ public function editor_enqueue() { global $pagenow; if ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) { $this->frontend_enqueue(); } } /** * Frontend enqueue * * @since 1.0.0 */ public function frontend_enqueue() { $custom_fonts = get_option( 'powerkit_custom_fonts_list' ); if ( is_array( $custom_fonts ) && $custom_fonts ) { $exclude = array(); $font_face = null; foreach ( $custom_fonts as $key => $item ) { $id = sanitize_title( $item['name'] ); if ( $id ) { $src = array(); $files = array(); $files['woff'] = wp_get_attachment_url( $item['file_woff'] ); $files['woff2'] = wp_get_attachment_url( $item['file_woff2'] ); foreach ( $files as $key_file => $file_url ) { if ( $file_url ) { $src[] = sprintf( 'url("%1$s") format("%2$s")', $file_url, $key_file ); } } if ( $src ) { $src_line = implode( ',', $src ); $display = 'async' === $this->load_method ? 'swap' : 'auto'; $font_face .= sprintf( '@font-face { font-family: "%1$s"; src: %2$s; font-display: %3$s; font-weight: %4$s; font-style: %5$s;}', $id, $src_line, $display, $item['weight'], $item['style'] ); } } } if ( $font_face ) { ?>