<?php if ( ! defined( 'ABSPATH' ) ) { die( 'Direct access forbidden.' ); }


/**
 * Enqueue Google fonts style to admin screen for custom header display.
 * @internal
 */
function wishbone_action_theme_admin_fonts() {
	wp_enqueue_style( 'wishbone-theme-opensans', wishbone_theme_font_url(), array(), '1.0' );
}
add_action( 'admin_print_scripts-appearance_page_custom-header', 'wishbone_action_theme_admin_fonts' );


if ( ! function_exists( '_action_theme_setup' ) ) :
/**
 * Theme setup.
 *
 * Set up theme defaults and registers support for various WordPress features.
 *
 * Note that this function is hooked into the after_setup_theme hook, which
 * runs before the init hook. The init hook is too late for some features, such
 * as indicating support post thumbnails.
 * @internal
 */ {
	function wishbone_action_theme_setup() {

		/*
		 * Make Theme available for translation.
		 */
		load_theme_textdomain( 'jt-wishbone', get_template_directory() . '/languages' );

		// This theme styles the visual editor to resemble the theme style.
		add_editor_style( array( 'assets/css/editor-style.css', wishbone_theme_font_url() ) );

		// Add RSS feed links to <head> for posts and comments.
		add_theme_support( 'automatic-feed-links' );

		/*
		 * Let WordPress manage the document title.
		 * By adding theme support, we declare that this theme does not use a
		 * hard-coded <title> tag in the document head, and expect WordPress to
		 * provide it for us.
		 */
		add_theme_support( 'title-tag' );

		// Enable support for Post Thumbnails, and declare two sizes.
		add_theme_support( 'post-thumbnails' );
		add_image_size( 'wishbone_stdthumb', 750, 400, true );
		add_image_size( 'wishbone_wishbonecarousel', 600, 372, true );
		add_image_size( 'wishbone_wishbonewideslider', 800, 500, true );
		add_image_size( 'wishbone_wishbonegridbig', 500, 500, true );
		add_image_size( 'wishbone_wishbonegridsmall', 310, 248, true );
		add_image_size( 'wishbone_masonry', 365, null, false );
		add_image_size( 'wishbone_eighty', 80, 80, true );

		/*
		 * Switch default core markup for search form, comment form, and comments
		 * to output valid HTML5.
		 */
		add_theme_support( 'html5', array(
			'search-form',
			'comment-form',
			'comment-list',
			'gallery',
			'caption'
		) );

		/*
		 * Enable support for Post Formats.
		 * See http://codex.wordpress.org/Post_Formats
		 */
		add_theme_support( 'post-formats', array(
			'video',
			'audio',
			'gallery',
		) );

		// This theme uses its own gallery styles.
		add_filter( 'use_default_gallery_style', '__return_false' );
	}
}
endif;
add_action( 'init', 'wishbone_action_theme_setup' );


/**
 * Post Format Chooser
 */
function wishbone_custom_metabox( $hook ) {

    global $post;

    if ( $hook == 'post-new.php' || $hook == 'post.php' ) {
        wp_enqueue_script(  'wishbone_admin-metabox', get_template_directory_uri().'/assets/js/metabox.js' );
        wp_enqueue_style( 'wishbone_metabox', get_template_directory_uri().'/assets/css/metabox.css' );
    }

    wp_enqueue_script(  'wishbone_admin-fwhacks', get_template_directory_uri().'/assets/js/fw-hacks.js', array( 'fw' ) );

}
add_action( 'admin_enqueue_scripts', 'wishbone_custom_metabox', 10, 1 );

/**
 * Set up Custom Query Vars
 *
 * @since jt-wishbone 1.0
 */
function wishbone_query_vats($vars) {
  $vars[] = "headertype";
  return $vars;
}
add_filter('query_vars', 'wishbone_query_vats');

/**
 * Adjust content_width value for image attachment template.
 * @internal
 */
function wishbone_action_theme_content_width() {
	if ( is_attachment() && wp_attachment_is_image() ) {
		$GLOBALS['content_width'] = 810;
	}
}
add_action( 'template_redirect', 'wishbone_action_theme_content_width' );


/**
 * Custom Search Form Markup. Useing Filter
 */
function wishbone_filter_get_search_form( $form ) { 

	$form = 
	'<form action="'. esc_url( home_url('/') ) .'">
		<input type="text" name="s" placeholder="'. esc_attr__( 'Search...', 'jt-wishbone' ) .'" value="' . get_search_query() . '"/>
		<button>
			'.lnr_icon_raw('lnr-magnifier').'
		</button>
	</form>';

    
    return $form; 
};
         
// add the filter 
add_filter( 'get_search_form', 'wishbone_filter_get_search_form', 10, 1 ); 

/**
 * Extend the default WordPress body classes.
 *
 * Adds body classes to denote:
 * 1. Single or multiple authors.
 * 2. Presence of header image.
 * 3. Index views.
 * 4. Full-width content layout.
 * 5. Presence of footer widgets.
 * 6. Single views.
 * 7. Featured content layout.
 *
 * @param array $classes A list of existing body class values.
 *
 * @return array The filtered body class list.
 * @internal
 */
function wishbone_filter_theme_body_classes( $classes ) {

	if ( is_singular() && ! is_front_page() ) {
		$classes[] = 'singular';
	}

	// Get class from theme options
	$basecolor = $predefinedcolor = '';
	$predefinedcolor = wishbone_option('predefinedcolor');
	$stickymenu = wishbone_option('stickymenu');

	if( 'none' == $predefinedcolor && !empty( $predefinedcolor ) ){
		$classes[] = esc_attr( wishbone_option('basecolor', 'dark') );
	} else {
		if ( strpos( $predefinedcolor, 'dark') !== false ) {
		    $classes[] = esc_attr('dark');
		} elseif( strpos( $predefinedcolor, 'light') !== false ) {
			$classes[] = esc_attr('light');
		}
	}

	if( true == $stickymenu ){
		$classes[] = esc_attr('stickymenuenabled');
	}

	return $classes;
}
add_filter( 'body_class', 'wishbone_filter_theme_body_classes' );

/**
 * Extend the default WordPress post classes.
 *
 * Adds a post class to denote:
 * Non-password protected page with a post thumbnail.
 *
 * @param array $classes A list of existing post class values.
 *
 * @return array The filtered post class list.
 * @internal
 */
function wishbone_filter_theme_post_classes( $classes ) {
	if ( ! post_password_required() && ! is_attachment() && has_post_thumbnail() ) {
		$classes[] = 'has-post-thumbnail';
	}

	return $classes;
}
add_filter( 'post_class', 'wishbone_filter_theme_post_classes' );


/**
 * Flush out the transients for Share Count
 * @internal
 */
function wishbone_save_post_hook( $post_id ) {

	// If this is just a revision, don't send the email.
	if ( wp_is_post_revision( $post_id ) )
		return;

	delete_transient( 'zl_post_share_'.$post_id );
}
add_action( 'save_post', 'wishbone_save_post_hook' );

// Framework Additional CSS
function wishbone_admin_enqueue() {
    wp_register_style( 'custom-fw-styles', get_template_directory_uri() . '/assets/css/fw-styles.css', false, '1.0.0' );
    wp_enqueue_style( 'custom-fw-styles' );
}
add_action( 'admin_enqueue_scripts', 'wishbone_admin_enqueue' );

/**
 * Register widget areas.
 * @internal
 */
function wishbone_action_theme_widgets_init() {
	register_sidebar( array(
		'name'          => esc_html__( 'Sidebar Widget Area', 'jt-wishbone' ),
		'id'            => 'sidebar-1',
		'description'   => esc_html__( 'Appears in the sidebar section of the site.', 'jt-wishbone' ),
		'before_widget' => '<div id="%1$s" class="wishbone_widget %2$s">',
		'after_widget'  => '<div class="wishbone_widfakebg"></div></div>',
		'before_title'  => '<h4 class="wishbone_widtle"><span><span>',
		'after_title'   => '</span></span></h4>',
	) );


	if( function_exists('fw_get_db_settings_option') ){
		$sections = fw_get_db_settings_option('footer_section');
		if(!empty($sections)){
			foreach ($sections as $section) {
				$footer_widgets = $section['footer_widgets'];
				if(!empty($footer_widgets)){
					$i=0;
					foreach ($footer_widgets as $footer_widget) {
						register_sidebar( array(
							'name'          => $footer_widget['footer_widget_name'],
							'id'            => strtolower(str_replace(' ', '-', $footer_widget['footer_widget_name'])),
							'before_widget' => '<div class="wishbone_foo_widget" id="%1$s">',
							'after_widget'  => '<div class="clear"></div></div>',
							'before_title'  => '<h3 class="wishbone_foo_wid_head"><span>',
							'after_title'   => '</span></h3>',
						) );
					$i++;
					}
				}
			}
		}
	}
}

add_action( 'widgets_init', 'wishbone_action_theme_widgets_init' );

/**
 * Setup Author Social Profile URL's
 *
 * @since wishbone 1.0
 */
function wishbone_add_to_author_profile( $contactmethods ) {

	$contactmethods['rss_url'] = 'RSS URL';
	$contactmethods['google_profile'] = 'Google URL';
	$contactmethods['twitter_profile'] = 'Twitter URL';
	$contactmethods['facebook_profile'] = 'Facebook URL';
	$contactmethods['linkedin_profile'] = 'Linkedin URL';
	//$contactmethods['cover_image'] = 'Cover Image';

	return $contactmethods;
}
add_filter( 'user_contactmethods', 'wishbone_add_to_author_profile', 10, 1);

/**
 * Lets add Open Graph Meta Info
 *
 * @since wishbone 1.0
 */
function wishbone_insert_fb_in_head() {
	$thumbnail_src = $desc = '';
	$desc = wishbone_post_excerpt( get_the_ID() );
	if ( !is_singular()) //if it is not a post or a page
		return;
        echo '<meta property="og:title" content="' . esc_attr( get_the_title() ) . '"/>'."\n";
        echo '<meta property="og:type" content="article"/>'."\n";
        echo '<meta property="og:url" content="' . esc_url( get_permalink() ) . '"/>'."\n";
        echo '<meta property="og:site_name" content="' . esc_attr( get_bloginfo( 'name' ) ) . '"/>'."\n";
        echo '<meta property="og:description" content="' . esc_attr( wp_trim_words( $desc, 35, null ) ) . '"/>'."\n";
	if( has_post_thumbnail( get_the_ID() ) ) { //the post does not have featured image, use a default image
		$thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'large' );
		echo '<meta property="og:image" content="' . esc_url( $thumbnail_src[0] ) . '"/>'."\n";
	}
	echo "";
}
add_action( 'wp_head', 'wishbone_insert_fb_in_head', 5 );


/**
 * Add custom icon sets
 *
 * @since wishbone 1.0
 */

function wishbone_filter_theme_new_icon_set($sets) {
        $sets['zocials'] = array(
        'font-style-src' => get_template_directory_uri() . '/assets/css/fontello.css',
        'container-class' => '', // some fonts need special wrapper class to display properly
        'groups' => array(
            'socials' => esc_html__('Social icons', 'jt-wishbone'),
        ),
        'icons' => array(
            // Web Application Icons
            'icon-wikipedia' => array('group' => 'socials'),
			'icon-duckduckgo' => array('group' => 'socials'),
			'icon-aim' => array('group' => 'socials'),
			'icon-delicious' => array('group' => 'socials'),
			'icon-paypal' => array('group' => 'socials'),
			'icon-flattr' => array('group' => 'socials'),
			'icon-android' => array('group' => 'socials'),
			'icon-eventful' => array('group' => 'socials'),
			'icon-smashmag' => array('group' => 'socials'),
			'icon-lanyrd' => array('group' => 'socials'),
			'icon-calendar-1' => array('group' => 'socials'),
			'icon-stumbleupon' => array('group' => 'socials'),
			'icon-fivehundredpx' => array('group' => 'socials'),
			'icon-pinterest-1' => array('group' => 'socials'),
			'icon-bitcoin' => array('group' => 'socials'),
			'icon-w3c' => array('group' => 'socials'),
			'icon-foursquare' => array('group' => 'socials'),
			'icon-html5' => array('group' => 'socials'),
			'icon-ie' => array('group' => 'socials'),
			'icon-call' => array('group' => 'socials'),
			'icon-grooveshark' => array('group' => 'socials'),
			'icon-ninetyninedesigns' => array('group' => 'socials'),
			'icon-forrst' => array('group' => 'socials'),
			'icon-digg' => array('group' => 'socials'),
			'icon-spotify' => array('group' => 'socials'),
			'icon-reddit' => array('group' => 'socials'),
			'icon-guest' => array('group' => 'socials'),
			'icon-gowalla' => array('group' => 'socials'),
			'icon-appstore' => array('group' => 'socials'),
			'icon-blogger' => array('group' => 'socials'),
			'icon-cc' => array('group' => 'socials'),
			'icon-dribbble-1' => array('group' => 'socials'),
			'icon-evernote' => array('group' => 'socials'),
			'icon-flickr-1' => array('group' => 'socials'),
			'icon-google' => array('group' => 'socials'),
			'icon-viadeo' => array('group' => 'socials'),
			'icon-instapaper' => array('group' => 'socials'),
			'icon-weibo' => array('group' => 'socials'),
			'icon-klout' => array('group' => 'socials'),
			'icon-linkedin-1' => array('group' => 'socials'),
			'icon-meetup' => array('group' => 'socials'),
			'icon-vk' => array('group' => 'socials'),
			'icon-plancast' => array('group' => 'socials'),
			'icon-disqus' => array('group' => 'socials'),
			'icon-rss-1' => array('group' => 'socials'),
			'icon-skype-1' => array('group' => 'socials'),
			'icon-twitter' => array('group' => 'socials'),
			'icon-youtube' => array('group' => 'socials'),
			'icon-vimeo-1' => array('group' => 'socials'),
			'icon-windows' => array('group' => 'socials'),
			'icon-xing' => array('group' => 'socials'),
			'icon-yahoo' => array('group' => 'socials'),
			'icon-chrome' => array('group' => 'socials'),
			'icon-email' => array('group' => 'socials'),
			'icon-macstore' => array('group' => 'socials'),
			'icon-myspace' => array('group' => 'socials'),
			'icon-podcast' => array('group' => 'socials'),
			'icon-amazon' => array('group' => 'socials'),
			'icon-steam' => array('group' => 'socials'),
			'icon-cloudapp' => array('group' => 'socials'),
			'icon-dropbox' => array('group' => 'socials'),
			'icon-ebay' => array('group' => 'socials'),
			'icon-facebook-1' => array('group' => 'socials'),
			'icon-github-1' => array('group' => 'socials'),
			'icon-github-circled-1' => array('group' => 'socials'),
			'icon-googleplay' => array('group' => 'socials'),
			'icon-itunes' => array('group' => 'socials'),
			'icon-plurk' => array('group' => 'socials'),
			'icon-songkick' => array('group' => 'socials'),
			'icon-lastfm-1' => array('group' => 'socials'),
			'icon-gmail' => array('group' => 'socials'),
			'icon-pinboard' => array('group' => 'socials'),
			'icon-openid' => array('group' => 'socials'),
			'icon-quora' => array('group' => 'socials'),
			'icon-soundcloud' => array('group' => 'socials'),
			'icon-tumblr' => array('group' => 'socials'),
			'icon-eventasaurus' => array('group' => 'socials'),
			'icon-wordpress' => array('group' => 'socials'),
			'icon-yelp' => array('group' => 'socials'),
			'icon-intensedebate' => array('group' => 'socials'),
			'icon-eventbrite' => array('group' => 'socials'),
			'icon-scribd' => array('group' => 'socials'),
			'icon-posterous' => array('group' => 'socials'),
			'icon-stripe' => array('group' => 'socials'),
			'icon-opentable' => array('group' => 'socials'),
			'icon-cart' => array('group' => 'socials'),
			'icon-print-1' => array('group' => 'socials'),
			'icon-angellist' => array('group' => 'socials'),
			'icon-instagram' => array('group' => 'socials'),
			'icon-dwolla' => array('group' => 'socials'),
			'icon-appnet' => array('group' => 'socials'),
			'icon-statusnet' => array('group' => 'socials'),
			'icon-acrobat' => array('group' => 'socials'),
			'icon-drupal' => array('group' => 'socials'),
			'icon-buffer' => array('group' => 'socials'),
			'icon-pocket' => array('group' => 'socials'),
			'icon-bitbucket' => array('group' => 'socials'),
			'icon-lego' => array('group' => 'socials'),
			'icon-login' => array('group' => 'socials'),
			'icon-stackoverflow' => array('group' => 'socials'),
			'icon-hackernews' => array('group' => 'socials'),
			'icon-lkdto' => array('group' => 'socials'),
			'icon-gplus' => array('group' => 'socials'),
        ),
    );
    return $sets;
}
add_filter('fw_option_type_icon_sets', 'wishbone_filter_theme_new_icon_set');

/**
 * Add Default Font from wishbone theme
 */
function wishbone_add_defaultfont($fonts) {

    // array_unshift($fonts, 'Test');
    return $fonts;
}
add_filter('fw_option_type_typography_v2_standard_fonts', 'wishbone_add_defaultfont');

/**
 * Delete Inline style cache
 * @see wishbone-theme-style.php:
 */
function wishbone_reset_css_transient(){
	delete_transient( 'wishbone_custom_css' );
}
add_action( 'fw_settings_form_saved', 'wishbone_reset_css_transient' );
add_action( 'fw_settings_form_reset', 'wishbone_reset_css_transient' );



/*
 * Change the comment reply link to use 'Reply to &lt;Author First Name>'
 */
function wishbone_add_comment_author_to_reply_link($link, $args, $comment){

    $comment = get_comment( $comment );

    // If no comment author is blank, use 'Anonymous'
    if ( empty($comment->comment_author) ) {
        if (!empty($comment->user_id)){
            $user = get_userdata($comment->user_id);
            $author=$user->user_login;
        } else {
            $author = esc_html__('Anonymous', 'jt-wishbone');
        }
    } else {
        $author = $comment->comment_author;
    }

    // If the user provided more than a first name, use only first name
    if(strpos($author, ' ')){
        $author = substr($author, 0, strpos($author, ' '));
    }

    // Replace Reply Link with "Reply to &lt;Author First Name>"
    $reply_link_text = $args['reply_text'];
    $replytext = esc_html__('Reply to', 'jt-wishbone');
    $link = str_replace($reply_link_text, lnr_icon_raw('lnr-bubble').' '.$replytext.' ' . $author, $link);

    return $link;
}
add_filter('comment_reply_link', 'wishbone_add_comment_author_to_reply_link', 10, 3);



/**
 * Demo Package Installer
 * @param FW_Ext_Backups_Demo[] $demos
 * @return FW_Ext_Backups_Demo[]
 */
function wishbone_filter_theme_fw_ext_backups_demos($demos) {
    $demos = array(
        'wishbone-demo-light' => array(
            'title' => esc_html__('Light Demo', 'jt-wishbone'),
            'screenshot' => get_template_directory_uri().'/assets/img/demoss/screenshot.jpg',
            'preview_link' => esc_url('http://zatolab.com/wishbone/light'),
        ),
		'wishbone-demo-dark' => array(
            'title' => esc_html__('Dark Demo', 'jt-wishbone'),
            'screenshot' => get_template_directory_uri().'/screenshot.png',
            'preview_link' => esc_url('http://zatolab.com/wishbone/dark'),
        ),
    );

    $download_url = esc_url('http://zatolab.com/democontents/wishbone/');

    foreach ($demos as $id => $data) {
        $demo = new FW_Ext_Backups_Demo($id, 'piecemeal', array(
            'url' => $download_url,
            'file_id' => $id,
        ));
        $demo->set_title($data['title']);
        $demo->set_screenshot($data['screenshot']);
        $demo->set_preview_link($data['preview_link']);

        $demos[ $demo->get_id() ] = $demo;

        unset($demo);
    }

    return $demos;
}
add_filter('fw:ext:backups-demo:demos', 'wishbone_filter_theme_fw_ext_backups_demos');


/**
 * Google Font Printer
 * Credit: valeriuzdrobau'comments
 * @link https://github.com/ThemeFuse/Unyson/issues/433#issuecomment-90490605
 */
if(!function_exists('wishbone_action_theme_process_google_fonts')) {
    function wishbone_action_theme_process_google_fonts()
    {
        $include_from_google = array();
        $google_fonts = fw_get_google_fonts();

        $font_pair_1 = fw_get_db_settings_option('font_pair_1');
        $font_pair_2 = fw_get_db_settings_option('font_pair_2');

        // if is google font
        if( isset($google_fonts[$font_pair_1['family']]) ){
            $include_from_google[$font_pair_1['family']] =   $google_fonts[$font_pair_1['family']];
        }

        if( isset($google_fonts[$font_pair_1['family']]) ){
            $include_from_google[$font_pair_2['family']] =   $google_fonts[$font_pair_2['family']];
        }

        $google_fonts_links = wishbone_fw_theme_get_remote_fonts($include_from_google);
        // set a option in db for save google fonts link
        update_option( 'fw_theme_google_fonts_link', $google_fonts_links );
    }
    add_action('fw_settings_form_saved', 'wishbone_action_theme_process_google_fonts', 999, 2);
}

if (!function_exists('wishbone_fw_theme_get_remote_fonts')) :
    function wishbone_fw_theme_get_remote_fonts($include_from_google) {
        /**
         * Get remote fonts
         * @param array $include_from_google
         */
        if ( ! sizeof( $include_from_google ) ) {
            return '';
        }

        $html = "<link href='".esc_url('//fonts.googleapis.com/css?family=');

        foreach ( $include_from_google as $font => $styles ) {
            $html .= str_replace( ' ', '+', $font ) . ':' . implode( ',', $styles['variants'] ) . '|';
        }

        $html = substr( $html, 0, - 1 );
        $html .= "' rel='stylesheet' type='text/css'>";

        return $html;
    }
endif;

/** 
 * Print Google fonts smartly
 */
if (!function_exists('wishbone_action_theme_print_google_fonts_link')) :
    function wishbone_action_theme_print_google_fonts_link() {
        /**
         * Print google fonts link
         */
        $use_custom_typo = '';
		$use_custom_typo = wishbone_option('use_custom_typo');
        $google_fonts_link = get_option('fw_theme_google_fonts_link', '');
        if($google_fonts_link != '' AND $use_custom_typo == true){
            echo wishbone_pass_html( $google_fonts_link );
        }
    }
    add_action('wp_head', 'wishbone_action_theme_print_google_fonts_link');
endif;

/** 
 * REMOVE REST API link tag!
 * it causes validator error
 */
function wishbone_remove_api() {
	remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
	remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 );
}
add_action( 'after_setup_theme', 'wishbone_remove_api' );