<?php
/**
 * ChromaticCapital functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package ChromaticCapital
 */

if ( ! function_exists( 'chromaticcapital_setup' ) ) :
	/**
	 * Sets 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 for post thumbnails.
	 */
	function chromaticcapital_setup() {
		/*
		 * Make theme available for translation.
		 * Translations can be filed in the /languages/ directory.
		 * If you're building a theme based on ChromaticCapital, use a find and replace
		 * to change 'chromaticcapital' to the name of your theme in all the template files.
		 */
		load_theme_textdomain( 'chromaticcapital', get_template_directory() . '/languages' );

		// Add default posts and comments RSS feed links to head.
		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 on posts and pages.
		 *
		 * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
		 */
		add_theme_support( 'post-thumbnails' );

		// This theme uses wp_nav_menu() in one location.
		register_nav_menus( array(
			'menu-1' => esc_html__( 'Primary', 'chromaticcapital' ),
		) );

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

		// Set up the WordPress core custom background feature.
		add_theme_support( 'custom-background', apply_filters( 'chromaticcapital_custom_background_args', array(
			'default-color' => 'ffffff',
			'default-image' => '',
		) ) );

		// Add theme support for selective refresh for widgets.
		add_theme_support( 'customize-selective-refresh-widgets' );

		/**
		 * Add support for core custom logo.
		 *
		 * @link https://codex.wordpress.org/Theme_Logo
		 */
		add_theme_support( 'custom-logo', array(
			'height'      => 250,
			'width'       => 250,
			'flex-width'  => true,
			'flex-height' => true,
		) );
	}
endif;
add_action( 'after_setup_theme', 'chromaticcapital_setup' );

/**
 * 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 chromaticcapital_content_width() {
	// This variable is intended to be overruled from themes.
	// Open WPCS issue: {@link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/1043}.
	// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
	$GLOBALS['content_width'] = apply_filters( 'chromaticcapital_content_width', 640 );
}
add_action( 'after_setup_theme', 'chromaticcapital_content_width', 0 );

/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */
function chromaticcapital_widgets_init() {
	register_sidebar( array(
		'name'          => esc_html__( 'Sidebar', 'chromaticcapital' ),
		'id'            => 'sidebar-1',
		'description'   => esc_html__( 'Add widgets here.', 'chromaticcapital' ),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );
}
add_action( 'widgets_init', 'chromaticcapital_widgets_init' );

/**
 * Enqueue scripts and styles.
 */

function chromaticcapital_scripts() {

	wp_enqueue_style( 'chromaticcapital-carousel', get_template_directory_uri() . '/css/owl.carousel.css' );
    wp_enqueue_style( 'chromaticcapital-bootstrap', get_template_directory_uri() . '/css/bootstrap.css' );
	wp_enqueue_style( 'chromaticcapital-awesome', get_template_directory_uri() . '/css/font-awesome.css' );
	wp_enqueue_style( 'chromaticcapital-theme', get_template_directory_uri() . '/css/theme.css' );
	wp_enqueue_style( 'chromaticcapital-style', get_stylesheet_uri() );

	wp_enqueue_script( 'jquery', 'http://nikunj.creativamotions.com/ChromaticCapital/wp-includes/js/jquery/jquery.js', array(), '20151215', true );
	wp_enqueue_script( 'chromaticcapital-jquery', get_template_directory_uri() . '/js/jquery.min.js', array(), '20151215', true );
	wp_enqueue_script( 'chromaticcapital-bootstrap-js', get_template_directory_uri() . '/js/bootstrap.js', array(), '20151215', true );
    wp_enqueue_script( 'chromaticcapital-carousel-js', get_template_directory_uri() . '/js/owl.carousel.js', array(), '20151215', true );

}
add_action( 'wp_enqueue_scripts', 'chromaticcapital_scripts' );

if( function_exists('acf_add_options_page') ) {
    acf_add_options_page();
}

add_action('after_setup_theme', 'remove_admin_bar');

function remove_admin_bar() {
    if (!current_user_can('administrator') && !is_admin()) {
        show_admin_bar(false);
    }
}

add_action( 'wp_login_failed', 'pippin_login_fail' );  // hook failed login
function pippin_login_fail( $username ) {
    $referrer = $_SERVER['HTTP_REFERER'];  // where did the post submission come from?
    // if there's a valid referrer, and it's not the default log-in screen
    if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') ) {
        wp_redirect(home_url() . '/?login=failed' );  // let's append some information (login=failed) to the URL for the theme to use
        exit;
    }
}



/**
 * Generate custom search form
 *
 * @param string $form Form HTML.
 * @return string Modified form HTML.
 */
add_filter('get_search_form', 'my_search_form');
function my_search_form($html) {
    $html = '<div class="input_search navbar-right"><form action="' . esc_url( home_url( '/' ) ) . '" method="get" class="search-form"><input class="form-control input_pstheme" type="text" name="s" id="s" class="search" placeholder="Search articles" value="' . esc_attr( get_search_query() ) . '" required><button type="submit" id="search-submit" class="search-submit"></button></form></div>'; // here you can either customize the WordPress builtin search form or totally overwrite it
 return $html;
}

/**
 * Implement the Custom Header feature.
 */
require get_template_directory() . '/inc/custom-header.php';

/**
 * Custom template tags for this theme.
 */
require get_template_directory() . '/inc/template-tags.php';

/**
 * Functions which enhance the theme by hooking into WordPress.
 */
require get_template_directory() . '/inc/template-functions.php';

/**
 * Customizer additions.
 */
require get_template_directory() . '/inc/customizer.php';

/**
 * Load Jetpack compatibility file.
 */
if ( defined( 'JETPACK__VERSION' ) ) {
	require get_template_directory() . '/inc/jetpack.php';
}


add_action('wp_ajax_load_posts_cat', 'load_posts_cat_by_ajax_callback');
add_action('wp_ajax_nopriv_load_posts_cat', 'load_posts_cat_by_ajax_callback');
function load_posts_cat_by_ajax_callback() {
    check_ajax_referer('load_more_posts', 'security');
    $paged = $_POST['page'];
    $cat_id = $_POST['cat'];
    if( $cat_id == 0 ){
	    $args = array(
	        'post_type' => 'post',
	        'paged' => $paged,
	        'posts_per_page' => 5,
	    );
	}else{
		 $args = array(
	        'post_type' => 'post',
	        'paged' => $paged,
	        'posts_per_page' => 5,
	        'tax_query' => array(
	            array (
	                'taxonomy' => 'category',
	                'field' => 'term_id',
	                'terms' =>  $cat_id,
	            )
	        ),
	    );
	}
    $my_posts = new WP_Query( $args );

    if ( $my_posts->have_posts() ) :
        ?>
        <?php while ( $my_posts->have_posts() ) : $my_posts->the_post();
             ?>
             	<div class="blog_tablist all">
                    <span class="b_date"><?php the_date(); ?></span>
                    <a href="<?php the_permalink(); ?>"  class="title"><?php echo get_the_title(); ?></a>
                    <?php the_excerpt(); ?>
                    <ul class="blog_tag">
                        <li>
                            <div class="media">
                                <div class="media-left">
                                    <img src="<?= get_template_directory_uri(); ?>/img/b-man.png" alt=""/>
                                </div>
                                <div class="media-body">
                                    <span><?php echo get_the_author_meta('first_name').' '.get_the_author_meta('last_name');?></span>
                                </div>
                            </div>
                        </li>
                        <li>
                            <div class="media">
                                <div class="media-left">
                                    <img src="<?= get_template_directory_uri(); ?>/img/b-folder.png" alt=""/>
                                </div>
                                <div class="media-body">
                                    <span><?php
                                        $category_detail=get_the_category();//$post->ID
                                        foreach($category_detail as $cd){
                                            echo $cd->cat_name;
                                        }
                                        ?></span>
                                </div>
                            </div>
                        </li>
                        <li>
                            <div class="media">
                                <?php
                                $tags = get_the_tags();
                                if (!empty($tags)) : ?>
                                    <div class="media-left">
                                        <img src="<?= get_template_directory_uri(); ?>/img/b-tag.png" alt="tag-blue"/>
                                    </div>
                                    <div class="media-body">
                                                    <span>
                                                    <?php
                                                    $tagsCount = 1;
                                                    $numItems = count($tags);
                                                    foreach ( $tags as $tag ) { $tagsCount++;
                                                        $tag_link = get_tag_link( $tag->term_id ); ?>
                                                        <?php echo $tag->name; ?><?php if($numItems != $tagsCount - 1) { echo ","; } ?>

                                                        <?php
                                                    }
                                                    ?></span>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </li>
                    </ul>
                </div>
             <?php 
        ?>
        <?php endwhile; echo '||'.  $my_posts->found_posts; ?>
        <?php
    endif;
 
    wp_die();
}