<?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";
    }
}

/*************************************************************************************************************************
 * Delay pro notice after_setup_theme
 ************************************************************************************************************************/

if ( ! function_exists( 'prime_business_delay_pro_notice' ) ) :

function prime_business_delay_pro_notice() {

  global $pagenow;

  if ( is_admin() && ('themes.php' == $pagenow) && isset( $_GET['activated'] ) ) {
    if (!isset($_COOKIE['statepro'])) {
      setcookie('statepro', 'hidden',  time()+86400); // 1 day
    }
  }
}

endif;

add_action( 'after_setup_theme', 'prime_business_delay_pro_notice' );

/*************************************************************************************************************************
 * Prime Business Pro notice
 ************************************************************************************************************************/

function prime_business_pro_notice() {
    global $pagenow;
    if ( is_admin() && isset($_GET['activated'] ) && $pagenow == "themes.php" ) {
        $prime_business_pro_state = esc_html( 'hidden' );
    } else if( isset( $_COOKIE[ 'statepro' ] ) ) {
        $prime_business_pro_state = sanitize_text_field( wp_unslash( $_COOKIE[ 'statepro' ] ) );
    } else {
        $prime_business_pro_state = '';
    }

    ?>
    <div class="notice notice-warning is-dismissible pro-notice <?php echo esc_attr( $prime_business_pro_state ); ?>">
        <h2><?php esc_html_e( 'UPGRADE to Prime Business Pro and unlock all the PREMIUM Features!', 'prime-business' ); ?></h2>
        <p><?php _e( 'Get the best out of your your website with Prime Business Pro! <b>Build your full website with Elementor Page Builder and Crafthemes Premium Elementor Addons.</b> Prime Business Pro also features Sortable Frontpage Sections, Sticky Header, Preloader, One Click Demo Import, Replace Copyright Text and many more. You will also get Premium support for your theme.', 'prime-business' ); ?></p>
        <a href="https://www.crafthemes.com/theme/prime-business-pro/" target="_blank"><button id="buy-pro-btn" type="button" class="button button-primary"><?php esc_html_e( 'Upgrade to Pro!', 'prime-business' ); ?></button></a> <strong><a class="left-margin-compare" href="https://www.crafthemes.com/go/prime-business-pro-demo" target="_blank"><?php esc_html_e( 'Live Demo', 'prime-business' ); ?></a></strong><strong><a class="pro-notice-dismiss" href="#"><?php esc_html_e( 'Dismiss this notice', 'prime-business' ); ?></a></strong>
    </div>

    <script>
      ( function($) {

        function createCookie(name,value,days) {
            if (days) {
                var date = new Date();
                date.setTime(date.getTime()+(days*24*60*60*1000));
                var expires = "; expires="+date.toGMTString();
            } else {
                var expires = "";
            }
            document.cookie = name+"="+value+expires+"; path=/";
        }

        $( '.pro-notice-dismiss' ).click(function() {
          createCookie("statepro", "hidden", 10000000);
          $( '.pro-notice' ).addClass( 'hidden' );
        });
      } )( jQuery );
    </script>
    <?php
}
add_action( 'admin_notices', 'prime_business_pro_notice' );

/*************************************************************************************************************************
 *  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' );

/*************************************************************************************************************************
 *  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' );
  }
}
