<?php 
if ( ! defined( 'ABSPATH' ) ) {
  exit; // Exit if accessed directly.
}
/*
 * @package ele blog
 * since 1.0.0
 * 
*/

 //select category
function ele_blog_post_category() {

  $terms = get_terms( array(
    'taxonomy'       => 'category',
    'hide_empty'     => false,
    'posts_per_page' => - 1,
  ) );

  $category_list = [];
  foreach ( $terms as $post ) {
    $category_list[ $post->term_id ] = [ $post->name ];
  }

  return $category_list;

}


//select tag
function ele_blog_post_tag() {

  $terms = get_terms( array(
    'taxonomy'       => 'post_tag',
    'hide_empty'     => false,
    'posts_per_page' => - 1,
  ) );

  $tag_list = [];
  foreach ( $terms as $post ) {
    $tag_list[ $post->term_id ] = [ $post->name ];
  }

  return $tag_list;

}

//select post 
function eleblog_select_post() {

  $args       = array( 'post_type' => 'post', 'posts_per_page' => - 1 );
  $post_lists = [];

  if ( $postlists = get_posts( $args ) ) {
    foreach ( $postlists as $postlist ) {
      (int) $post_lists[ $postlist->ID ] = $postlist->post_title;
    }
  } else {
    (int) $post_lists['0'] = esc_html__( 'No Post Found', 'ele-blog' );
  }

  return $post_lists;

}

if( ! function_exists( 'elegblog_get_template' ) ){

    function elegblog_get_template( $style='style-one' ){
        include ELE_BLOG_ELEMENTOR.'/templates/'.$style.'.php'; 
    }
}

/******************************
    pagination
*******************************/
add_action( 'wp_ajax_eleblog_pagination', 'eleblog_pagination' );
add_action( 'wp_ajax_nopriv_eleblog_pagination', 'eleblog_pagination' );
function eleblog_pagination() { 

   $paged = isset( $_POST[ 'paged' ] ) ? sanitize_text_field( $_POST[ 'paged' ])  : 1 ;

   $id = isset( $_POST[ 'AjaxId' ] ) ? sanitize_text_field( $_POST[ 'AjaxId' ])  : '' ;

   $Style = isset( $_POST[ 'Style' ] ) ? sanitize_text_field( $_POST[ 'Style' ])  : 'style-one' ;

   $settings = get_option( $id );

      $args  = array(
        'post_type'           => 'post',
        'post_status'         => 'publish',
        'ignore_sticky_posts' => 1,
        'posts_per_page'      => $settings['ppr'],
        'paged'=> $paged,
    );

    $args['orderby'] = $settings['orderby'];
    $args['order']   = $settings['order'];
    if ( ! empty( $settings['exclude_cat'] ) ) {
        $args['category__not_in'] = $settings['exclude_cat'];
    }


    if ( ! empty( $settings['select_cat'] ) ) {
        $catgory             = implode( ', ', $settings['select_cat'] );
        $args['tax_query'][] = array(
            'taxonomy' => 'category',
            'field'    => 'id',
            'terms'    => array_values( $settings['select_cat'] )
        );
    }

    if ( ! empty( $settings['select_tag'] ) ) {
        $args['tax_query'][] = array(
            'taxonomy' => 'post_tag',
            'field'    => 'id',
            'terms'    => array_values( $settings['select_tag'] )
        );
    }

    $posts_query = new \WP_Query( $args );

   ?>
   <?php
   
    include ELE_BLOG_ELEMENTOR.'/templates/'.$Style.'.php'; 

    include ELE_BLOG_ELEMENTOR.'/templates/ajax-pagination.php'; 

    die();
}


/*
* post tag
*/

function eleblog_post_date(){
    $time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
      if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
        $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
      }

      $time_string = sprintf( $time_string,
        esc_attr( get_the_date( DATE_W3C ) ),
        esc_html( get_the_date() ),
        esc_attr( get_the_modified_date( DATE_W3C ) ),
        esc_html( get_the_modified_date() )
      );

      $posted_on = sprintf(
      /* translators: %s: post date. */
        esc_html_x( '%s', 'post date', 'ele-blog' ), // phpcs:ignore WordPress.WP.I18n.NoEmptyStrings
        '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'
      );

      echo '<span class="date"><i class="fa fa-clock-o"></i>' . $posted_on . '</span>'; // WPCS: XSS OK.
}

/*
* inline style
*/

if ( ! function_exists( 'eleblog_dynamic_styles' ) ) {

  function eleblog_dynamic_styles() {

    ob_start();

    /*------------------------------------------------------------------------------------
     * taxonomy color
    ------------------------------------------------------------------------------------ */

    $eleblog_taxonomy            = get_terms( 'category', 'orderby=count&hide_empty=0' );

    foreach ( $eleblog_taxonomy as $term ) :

      $ele_blog_cat_meta = get_term_meta( $term->term_id, 'ele_blog', true ); //color
      $ele_blog_cat_color       = ( isset( $ele_blog_cat_meta['catcolor'] ) ? $ele_blog_cat_meta['catcolor'] : '#097bed' );
      $ele_blog_cat_hover_color = ( isset( $ele_blog_cat_meta['cathovercolor'] ) ? $ele_blog_cat_meta['cathovercolor'] : '#fff' );
      $ele_blog_bg_color        = ( isset( $ele_blog_cat_meta['bgcolor'] ) ? $ele_blog_cat_meta['bgcolor'] : '#e6f2fd' );
      $ele_blog_hover_bg_color  = ( isset( $ele_blog_cat_meta['bg_hover_color'] ) ? $ele_blog_cat_meta['bg_hover_color'] : '#e6f2fd' );

      ?>

      .ele-<?php echo esc_attr( str_replace( ' ', '', $term->slug ) ); ?> {
          background: <?php echo sanitize_hex_color( $ele_blog_bg_color ); ?>;
          color: <?php echo sanitize_hex_color( $ele_blog_cat_color ); ?>;
          }

        .ele-<?php echo esc_attr( str_replace( ' ', '', $term->slug ) ); ?>:hover{
          background:<?php echo sanitize_hex_color( $ele_blog_hover_bg_color ); ?>;
          color: <?php echo sanitize_hex_color( $ele_blog_cat_hover_color ); ?>;
          }

    <?php endforeach;

    $output = ob_get_clean();

    return $output;


  } //end  eleblog_dynamic_styles

} //endif


function ele_blog_style_method() {

  $custom_css = eleblog_dynamic_styles();


  wp_add_inline_style( 'ele-blog-main', $custom_css );
}

add_action( 'wp_enqueue_scripts', 'ele_blog_style_method',99 );


if ( class_exists( 'CSF' ) ) {

$eleblog_prefix = 'ele_blog';

//
// Create taxonomy options
  CSF::createTaxonomyOptions( $eleblog_prefix, array(
    'taxonomy'  => array( 'category' ),
    'data_type' => 'serialize', // The type of the database save options. `serialize` or `unserialize`
  ) );

//
// Create a section
  CSF::createSection( $eleblog_prefix, array(
    'fields' => array(
      array(
        'id'      => 'catcolor',
        'type'    => 'color',
        'title'   => esc_html__( 'Text Color', 'ele-blog' ),
        'default' => '#fff'
      ),
      array(
        'id'      => 'cathovercolor',
        'type'    => 'color',
        'title'   => esc_html__( 'Hover Text Color', 'ele-blog' ),
        'default' => '#fff'
      ),
      array(
        'id'      => 'bgcolor',
        'type'    => 'color',
        'title'   => esc_html__( 'Background Color', 'ele-blog' ),
        'default' => '#097bed'
      ),
      array(
        'id'      => 'bg_hover_color',
        'type'    => 'color',
        'title'   => esc_html__( 'Hover Background Color', 'ele-blog' ),
        'default' => '#097bed'
      ),
      array(
        'id'      => 'cat_img',
        'title'   => esc_html__( 'Small Image', 'ele-blog' ),
        'type'    => 'media',
        'height'  =>175,
        'width'  => 75,
        'library' => 'image',
      ),

    )
  ) );


  //video meta
  CSF::createMetabox( $eleblog_prefix . '_post_video', array(
    'title'     => esc_html__( 'Add Video', 'ele-blog' ),
    'post_type' => 'post',
  ) );

  CSF::createSection( $eleblog_prefix . '_post_video', array(
    'fields' => array(
      array(
        'id'      => 'videoid',
        'type'    => 'text',
        'title'   => esc_html__( 'Enter Youtube Video Id', 'ele-blog' ),
        'desc'    => esc_html__( 'Keep blank to hide video section in post details', 'ele-blog' ),
        'default' => 'MlmnxZ24A20'
      ),
    )
  ) );


}
/*
* post view count
*/

// set view count
function eleblog_set_popular_post_widget_views_count( $postID ) {
  $count_key = 'eleblog_post_views';
  $count     = get_post_meta( $postID, $count_key, true );
  if ( $count == '' ) {
    delete_post_meta( $postID, $count_key );
    add_post_meta( $postID, $count_key, '1' );
  } else {
    $count ++;
    update_post_meta( $postID, $count_key, $count );

  }
}

/*
 * track post views
 */
function eleblog_track_popular_post_views( $post_id ) {
  if ( is_single() ) {
    if ( empty ( $post_id ) ) {
      global $post;
      $post_id = $post->ID;
      eleblog_set_popular_post_widget_views_count( $post_id );
    }
  }

}

add_action( 'wp_head', 'eleblog_track_popular_post_views' );


// caategory slug
function eleblog_get_cat_slug( $cat_id ) {
  $cat_id   = (int) $cat_id;
  $category = get_term( $cat_id, 'category' );

  if ( ! $category || is_wp_error( $category ) ) {
    return '';
  }

  return $category->slug;
}