<?php
/**
 * Custom functions that act independently of the theme templates
 *
 * Eventually, some of the functionality here could be replaced by core features
 *
 * @package BeAgency
 */

/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function beagency_body_classes( $classes ) {
	// Adds a class of group-blog to blogs with more than 1 published author.
	if ( is_multi_author() ) {
		$classes[] = 'group-blog';
	}

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

/**
 * Get theme version.
 *
 * @return string $theme_version The theme version.
 */
function beagency_get_version() {
	$theme_info = wp_get_theme();

	// If it's a child theme, then get parent theme.
	if ( is_child_theme() ) {
		$theme_info = wp_get_theme( $theme_info->parent_theme );
	}

	$theme_version = $theme_info->display( 'Version' );

	return $theme_version;
}

/**
 * Register the required plugins for this theme.
 *
 * @link https://github.com/TGMPA/TGM-Plugin-Activation
 */
function beagency_register_required_plugins() {
	// Required plugin.
	$plugins = array(
		array(
			'name'               => 'BeAgency Lite Plugin',
			'slug'               => 'beagency-lite-plugin',
			'source'             => get_template_directory_uri() . '/plugins/beagency-lite-plugin.zip',
			'required'           => true,
			'version'            => '1.0.1',
			'force_activation'   => true,
			'force_deactivation' => true
		),
	);

	// Array of configuration settings.
	$config = array(
		'id'           => 'beagency_tgmpa',
		'default_path' => '',
		'menu'         => 'beagency-install-plugins',
		'parent_slug'  => 'themes.php',
		'capability'   => 'edit_theme_options',
		'has_notices'  => true,
		'dismissable'  => true,
		'is_automatic' => true
	);

	tgmpa( $plugins, $config );
}
add_action( 'tgmpa_register', 'beagency_register_required_plugins' );

/**
 * Include the Portfolio template for the content.
 */
function beagency_portfolio_ajax() {
	get_template_part( 'template-parts/content', 'portfolio-ajax' );

	wp_die();
}
add_action( 'wp_ajax_portfolio_ajax', 'beagency_portfolio_ajax' );
add_action( 'wp_ajax_nopriv_portfolio_ajax', 'beagency_portfolio_ajax' );

/**
 * The notification to upgrade to Pro version.
 *
 * @return string $output The upgrade notification.
 */
function beagency_premium_info() {
	$output = sprintf (
				'%1$s <a href="%2$s" target="_blank">BeAgency Pro</a> %3$s <a href="%4$s" class="thickbox" title="BeAgency Pro">%5$s</a> %6$s.',
				esc_html__( 'Upgrade to', 'beagency' ),
				esc_url( 'http://betheme.me/themes/beagency/' ),
				esc_html__( 'to enjoy', 'beagency' ),
				esc_url( 'http://betheme.me/themes/beagency/?TB_iframe=true&width=1024&height=800' ),
				esc_html__( 'Premium Features', 'beagency' ),
				esc_html__( 'and support continued development', 'beagency' )
			);

	return $output;
}

/**
 * Filter to remove thumbnail image dimension attributes.
 *
 * @return string $html The HTML codes without width and height attributes.
 */
function beagency_remove_thumbnail_dimensions( $html, $post_id, $post_image_id ) {
    $html = preg_replace( '/(width|height)=\"\d*\"\s/', '', $html );

    return $html;
}
add_filter( 'post_thumbnail_html', 'beagency_remove_thumbnail_dimensions', 10, 3 );

/**
 * Remove the content more link text.
 */
function beagency_remove_read_more_link() {
	return false;
}
add_filter( 'the_content_more_link', 'beagency_remove_read_more_link' );

/**
 * Change the excerpt length.
 */
function beagency_custom_excerpt_length( $length ) {
	return 25;
}
add_filter( 'excerpt_length', 'beagency_custom_excerpt_length', 999 );

/**
 * Change the excerpt more string at the end.
 */
function beagency_new_excerpt_more( $more ) {
	return ' ...';
}
add_filter( 'excerpt_more', 'beagency_new_excerpt_more' );

/**
 * Add support for excerpts in Pages.
 */
function beagency_add_excerpts() {
	add_post_type_support( 'page', 'excerpt' );
}
add_action( 'init', 'beagency_add_excerpts' );

/**
 * Add class to links generated by next_posts_link and previous_posts_link.
 */
function beagency_next_posts_link_attributes() {
    return 'class="button button-lg"';
}

function beagency_previous_posts_link_attributes() {
    return 'class="button button-lg"';
}
add_filter( 'next_posts_link_attributes', 'beagency_next_posts_link_attributes' );
add_filter( 'previous_posts_link_attributes', 'beagency_previous_posts_link_attributes' );

/**
 * Set/unset post as image post type if post has thumbnail.
 *
 * @param int $post_id The post ID.
 */
function beagency_set_post_type( $post_id ) {
	global $pagenow; 

	if ( in_array( $pagenow, array( 'post.php', 'post-new.php' ) ) ) {
		if ( get_post_type( $post_id ) == 'post' ) {
			if ( has_post_thumbnail( $post_id ) ) {
				set_post_format( $post->ID, 'image' );
			}

			if ( ! has_post_thumbnail( $post_id ) ) {
				set_post_format( $post->ID, '' );
			}
		}
	}
}
add_action( 'save_post', 'beagency_set_post_type', 10, 3 );

/**
 * Remove WordPress Admin Bar style from header.
 */
function beagency_remove_admin_bar_style() {
	remove_action( 'wp_head', '_admin_bar_bump_cb' );
}
add_action( 'get_header', 'beagency_remove_admin_bar_style' );

/**
 * Remove Recent Comments Widget style from header.
 */
function beagency_remove_recent_comments_style() {  
	global $wp_widget_factory;  

	remove_action( 'wp_head', array( $wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style' ) );  
}  
add_action( 'widgets_init', 'beagency_remove_recent_comments_style' );

/**
 * Only show blog posts in search results.
 *
 * @param array $query The WP_Query object.
 */

function beagency_search_filter( $query ) {
    if ( ! is_admin() && $query->is_main_query() ) {
		if ( $query->is_search ) {
			$query->set( 'post_type', array( 'post' ) );
		}
	}
}
add_filter( 'pre_get_posts', 'beagency_search_filter' );

/**
 * Update option after Customizer save.
 */
function beagency_customize_save_after() {
	update_option( 'blogname', Kirki::get_option( 'general_site_title' ) );

	if ( Kirki::get_option( 'general_front_page' ) != '0' ) {
		update_option( 'show_on_front ', 'page' );
		update_option( 'page_on_front', Kirki::get_option( 'general_front_page' ) );
		update_option( 'page_for_posts', Kirki::get_option( 'general_posts_page' ) );
	}
}
add_action( 'customize_save_after', 'beagency_customize_save_after' );

/**
 * Set option data when Customizer controls are initialized.
 */
function beagency_customize_controls_init() {
	set_theme_mod( 'general_site_title', get_bloginfo( 'name' ) );
	set_theme_mod( 'general_front_page', get_option( 'page_on_front' ) );
	set_theme_mod( 'general_posts_page', get_option( 'page_for_posts' ) );
}
add_action( 'customize_controls_init', 'beagency_customize_controls_init' );

/**
 * Enqueue scripts and styles for admin pages.
 */
function beagency_admin_scripts() {
	$screen = get_current_screen();

	if( $screen->id === 'themes' || $screen->id === 'update-core' ) {
		wp_enqueue_script( 'betheme-admin-script', get_template_directory_uri() . '/js/admin.js', array( 'jquery' ), beagency_get_version(), true );
	}

	// Localize the script with new data.
	wp_localize_script( 'betheme-admin-script', 'admin_vars', array(
		'upgrade_available' => esc_html__( 'Upgrade to Pro Version', 'beagency' ),
		'upgrade_info'      => beagency_premium_info()
	) );
}
add_action( 'admin_enqueue_scripts', 'beagency_admin_scripts' );
