<?php

/**
 * All theme custom functions are delared here
 */

/*************************************************************************************************************************
 * Loads google fonts to the theme
 * Thanks to themeshaper.com
 ************************************************************************************************************************/

if ( ! function_exists( 'prime_business_fonts_url' ) ) :

function prime_business_fonts_url() {
  $prime_business_fonts_url  = '';
  $prime_business_lora   = _x( 'on', 'Lora font: on or off', 'prime-business' );
  $prime_business_rubik = _x( 'on', 'Rubik font: on or off', 'prime-business' );

  if ( 'off' !== $prime_business_lora || 'off' !== $prime_business_rubik ) {
    $prime_business_font_families = array();

    if ( 'off' !== $prime_business_lora ) {
      $prime_business_font_families[] = 'Lora:700i';
    }

    if ( 'off' !== $prime_business_rubik ) {
      $prime_business_font_families[] = 'Rubik:400,500,700';
    }
  }

  $prime_business_query_args = array(
    'family' => urlencode( implode( '|', $prime_business_font_families ) ),
    'subset' => urlencode( 'cyrillic-ext,cyrillic,vietnamese,latin-ext,latin' )
  );

  $prime_business_fonts_url = add_query_arg( $prime_business_query_args, 'https://fonts.googleapis.com/css' );

  return esc_url_raw( $prime_business_fonts_url );
}

endif;

/*************************************************************************************************************************
 * Set the content width
 ************************************************************************************************************************/

if ( ! isset( $content_width ) ) {
  $content_width = 900;
}

/*************************************************************************************************************************
 *  Adds a span tag with dropdown icon after the unordered list
 *  that has a sub menu on the mobile menu.
 ************************************************************************************************************************/

class Prime_Business_Dropdown_Toggle_Walker_Nav_Menu extends Walker_Nav_Menu {
    function start_lvl( &$prime_business_output, $prime_business_depth = 0, $prime_business_args = array() ) {
        $prime_business_indent = str_repeat( "\t", $prime_business_depth );
        if( 'mobile_menu' == $prime_business_args->theme_location ) {
            $prime_business_output .='<i class="fa fa-ellipsis-v dropdown-toggle"></i>';
        }
        $prime_business_output .= "\n$prime_business_indent<ul class=\"sub-menu\">\n";
    }
}

/*************************************************************************************************************************
 *  Adds Metabox to pages
 ************************************************************************************************************************/
// Checkbox Meta
add_action( "add_meta_boxes", "prime_business_checkbox_init" );

function prime_business_checkbox_init(){
  add_meta_box( "header-settings",
                "Header Settings",
                "prime_business_header_settings_callback",
                "page",
                "side",
                "high"
              );
}

function prime_business_header_settings_callback( $post ) {
  $prime_business_custom = get_post_custom( $post->ID );
  $prime_business_field_id = ( isset( $prime_business_custom["field_id"][0] ) ) ? $prime_business_custom["field_id"][0] : '' ;

  $prime_business_field_id_value = get_post_meta( $post->ID, 'field_id', true );

  if( $prime_business_field_id_value == "yes" ) {
    $prime_business_field_id_checked = 'checked="checked"';
  } else {
    $prime_business_field_id_checked = '';
  }
?>
    <input type="checkbox" name="field_id" value="yes" <?php echo esc_attr( $prime_business_field_id_checked ); ?> />
    <label><?php echo esc_html( 'Disable Title?' ); ?></label>
  <?php
}

// Save Meta Details
add_action( 'save_post', 'prime_business_save_details' );

function prime_business_save_details(){
  global $post;

  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
      return $post->ID;
  }

  if( isset( $_POST[ "field_id" ] ) ) {
    $prime_business_field_id = sanitize_text_field( wp_unslash( $_POST[ "field_id" ] ) );
    update_post_meta( $post->ID, "field_id", $prime_business_field_id );
  } else {
    update_post_meta( $post->ID, "field_id", 'no' );
  }
}

/*************************************************************************************************************************
 *  Adds Excerpt to pages
 ************************************************************************************************************************/

add_action( 'init', 'prime_business_add_excerpts_to_pages' );

function prime_business_add_excerpts_to_pages() {
    add_post_type_support( 'page', 'excerpt' );
}

/*************************************************************************************************************************
 *  Filter the excerpt "read more" string.
 ************************************************************************************************************************/

function prime_business_excerpt_more() {
    return '...';
}

add_filter( 'excerpt_more', 'prime_business_excerpt_more' );
