<?php
/**
 * @author      CodeGearThemes
 * @category    WordPress
 * @package     Acoustics
 * @version     1.0.0
 *
 * Set the content width in pixels, based on the theme's design and stylesheet.
 *
 * Priority 0 to make it available to lower priority callbacks.
 *
 * @global int $content_width
 */
function acoustics_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'acoustics_content_width', 680 );
}
add_action( 'after_setup_theme', 'acoustics_content_width', 0 );

if ( ! function_exists( 'acoustics_breadcrumb' ) ) :
    function acoustics_breadcrumb() {
        $breadcrumb_args = array(
            'container'   => 'nav',
            'show_browse' => false,
        );
        breadcrumb_trail( $breadcrumb_args );
    }
endif;

/**
*
* Excerpt Length
* @since 1.0.0
*
*/
if ( ! function_exists( 'acoustics_excerpt_length' ) ) :
	function acoustics_excerpt_length( $length ) {
	     if ( is_admin() ) {
			return $length;
		}
	    return 75;
	}
	add_filter( 'excerpt_length', 'acoustics_excerpt_length', 100 );
endif;

if ( ! function_exists( 'acoustics_excerpt_more' ) ) :
	/**
	 * Replaces "[...]" (appended to automatically generated excerpts) with ... and a option from customizer
	 *
	 * @return string option from customizer prepended with an ellipsis.
	 */
	function acoustics_excerpt_more( $link ) {
		if ( is_admin() ) {
			return $link;
		}
	    return sprintf( '<a class="read-more more-link" href="%1$s">%2$s</a>',
	        get_permalink( get_the_ID() ),
	        __( 'Read more', 'acoustics' )
	    );
	}
endif;
add_filter( 'excerpt_more', 'acoustics_excerpt_more' );

if ( ! function_exists( 'acoustics_custom_excerpt_more' ) ) :
	/**
	 * Replaces "[...]" (appended to automatically generated excerpts) with ... and a option from customizer
	 *
	 * @return string option from customizer prepended with an ellipsis.
	 */
	function acoustics_custom_excerpt_more( $excerpt ) {
		if ( has_excerpt() && ! is_attachment() ) {
		    $link = sprintf( '<a class="read-more more-link" href="%1$s">%2$s</a>',
		        get_permalink( get_the_ID() ),
		        __( 'Read more', 'acoustics' )
		    );
			$excerpt .= $link;
		}
		return $excerpt;
	}
endif;
add_filter( 'get_the_excerpt', 'acoustics_custom_excerpt_more' );

if( ! function_exists('acoustics_categories')):
	function acoustics_categories() {
		$category_list = array();
	    $categories = get_categories(
	            array(
	                'hide_empty' => 0,
	            )
	    );
	    $category_list[0] = esc_html__('Select Category', 'acoustics');
	    foreach ($categories as $category):
			$category_list[$category->term_id] = $category->name;
		endforeach;
	    return $category_list;
}
endif;

if ( function_exists( 'acoustics_footer_social' ) ):
	add_action( 'footer_social', 'acoustics_footer_social', 10 );
endif;


/**
 * Check if google fonts is being either locally load or not and insert
 * the needed stylesheet version. That's needed because the new google API (css2)
 * isn't compatible with wp_enqueue_style().
 *
 * Reference: https://core.trac.wordpress.org/ticket/49742#comment:7
 */
function acoustics_google_fonts_version() {
	$acoustics_googlefont_load_locally = get_theme_mod('acoustics_load_google_fonts_locally', 0);
	if( $acoustics_googlefont_load_locally ) {
		return ACOUSTICS_VERSION;
	}

	return NULL;
}

/**
 * Google fonts preconnect
 */
function acoustics_preconnect_google_fonts() {

	$defaults = json_encode(
		array(
			'font' 			=> 'System default',
			'regularweight' => 'regular',
			'category' 		=> 'sans-serif'
		)
	);

	$acoustics_body_fonts		= get_theme_mod( 'acoustics_base_font', $defaults );
	$acoustics_heading_fonts 	= get_theme_mod( 'acoustics_heading_font', $defaults );
	$acoustics_navigation_fonts = get_theme_mod( 'acoustics_menu_font', $defaults );

	$acoustics_body_fonts 		= json_decode( $acoustics_body_fonts, true );
	$acoustics_heading_fonts 	= json_decode( $acoustics_heading_fonts, true );

	$acoustics_googlefont_load_locally = get_theme_mod('acoustics_load_google_fonts_locally', 0);
	if( $acoustics_googlefont_load_locally ) return;

	if ( 'System default' === $acoustics_body_fonts['font'] && 'System default' === $acoustics_heading_fonts['font'] && 'System default' === $acoustics_navigation_fonts) {
		return;
	}

	echo '<link rel="preconnect" href="//fonts.googleapis.com">';
	echo '<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>';
}
add_action( 'wp_head', 'acoustics_preconnect_google_fonts', 1);
