<?php
/**
 * Functions related to typography
 */


/**
 * Originally found in Redux field_typography.php
 * @param  [type] $fonts [description]
 * @return [type]        [description]
 */
function intense_create_google_font_link( $fonts ) {
    $link    = "";
    $subsets = array();

    foreach ( $fonts as $family => $font ) {
        if ( ! empty( $link ) ) {
            $link .= "%7C"; // Append a new font to the string
        }

        $family = urlencode( $family );

        $link .= $family;

        if ( ! empty( $font['font-style'] ) || ! empty( $font['all-styles'] ) ) {
            $link .= ':';
            if ( ! empty( $font['all-styles'] ) ) {
                $link .= implode( ',', $font['all-styles'] );
            } else if ( ! empty( $font['font-style'] ) ) {
                $link .= implode( ',', $font['font-style'] );
            }
        }

        if ( ! empty( $font['subset'] ) ) {
            foreach ( $font['subset'] as $subset ) {
                if ( ! in_array( $subset, $subsets ) ) {
                    array_push( $subsets, $subset );
                }
            }
        }
    }

    if ( ! empty( $subsets ) ) {
        $link .= "&amp;subset=" . implode( ',', $subsets );
    }


    return '//fonts.googleapis.com/css?family=' . str_replace( '|', '%7C', $link );
}