<?php
/**
 * Functions which enhance the theme by hooking into WordPress
 *
 * @package anews
 */

/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function anews_body_classes( $classes ) {
	// Adds a class of hfeed to non-singular pages.
	if ( ! is_singular() ) {
		$classes[] = 'hfeed';
	}

	// Adds a class of no-sidebar when there is no sidebar present.
	if ( ! is_active_sidebar( 'sidebar-1' ) ) {
		$classes[] = 'no-sidebar';
	}

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

/**
 * Add a pingback url auto-discovery header for single posts, pages, or attachments.
 */
function anews_pingback_header() {
	if ( is_singular() && pings_open() ) {
		printf( '<link rel="pingback" href="%s">', esc_url( get_bloginfo( 'pingback_url' ) ) );
	}
}
add_action( 'wp_head', 'anews_pingback_header' );


if ( !function_exists( 'anews_next_prev_post_link' ) ) {
    function anews_next_prev_post_link() {?>
        <div class="navigation anews-post-pagination">
            <?php
            the_post_navigation(
                array(
                    'prev_text' => '<span class="anews-nav-title">%title</span>',
                    'next_text' => '<span class="anews-nav-title">%title</span>',
                )
            );?>
        </div>
        <?php
    }
}


/*--------------------------------
GET COMMENT COUNT TEXT
----------------------------------*/
if ( !function_exists( 'anews_comment_count_text' ) ) {
    function anews_comment_count_text( $post_id ) {
        $comments_number = get_comments_number( $post_id );
        if ( $comments_number == 0 ) {
            $comment_text = esc_html__( 'No comment', 'anews' );
        } elseif ( $comments_number == 1 ) {
            $comment_text = esc_html__( 'One comment', 'anews' );
        } elseif ( $comments_number > 1 ) {
            $comment_text = $comments_number . esc_html__( ' Comments', 'anews' );
        }
        echo esc_html( $comment_text );
    }
}
/**
 * anews_move_comment_field_to_bottom() Remove cookie field and move comment field bottom.
 * @param  $fields array()
 * @return return comment form fields
 */
function anews_move_comment_field_to_bottom( $fields ) {
    $comment_field = $fields['comment'];
    unset( $fields['comment'] );
    unset( $fields['cookies'] );
    $fields['comment'] = $comment_field;
    return $fields;
}
add_filter( 'comment_form_fields', 'anews_move_comment_field_to_bottom' );

if ( !function_exists( 'anews_comments_pagination' ) ) {
    function anews_comments_pagination() {
        the_comments_pagination( array(
            'screen_reader_text' => ' ',
            'prev_text'          => '<i class="fa fa-angle-left"></i>',
            'next_text'          => '<i class="fa fa-angle-right"></i>',
            'type'               => 'list',
            'mid_size'           => 1,
        ) );
    }
}

if ( !function_exists('anews_pagination') ) {
    function anews_pagination(){
        the_posts_pagination(array(
            'screen_reader_text' => '',
            'prev_text'          => '<i class="fa fa-angle-left"></i>',
            'next_text'          => '<i class="fa fa-angle-right"></i>',
            'type'               => 'list',
            'mid_size'           => 1,
        ));
    }
}


/**
 * List of posts for post choices.
 * @return Array Array of post ids and name.
 */
function anews_post_choices() {
    $posts = get_posts( array( 'numberposts' => -1 ) );
    $choices = array();
    $choices[0] = esc_html__( '--Select--', 'anews' );
    foreach ( $posts as $post ) {
        $choices[ $post->ID ] = $post->post_title;
    }
    return  $choices;
}

/**
 * List of category for category choices.
 * @return Array Array of post ids and name.
 */
function anews_category_choices() {
    $tax_args = array(
        'hierarchical' => 0,
        'taxonomy'     => 'category',
    );
    $taxonomies = get_categories( $tax_args );
    $choices = array();
    $choices[0] = esc_html__( '--Select--', 'anews' );
    foreach ( $taxonomies as $tax ) {
        $choices[ $tax->term_id ] = $tax->name;
    }
    return  $choices;
}


/**
 * Delete the `Hello world!` post after successful demo import.
 */
function anews_delete_hello_world() {
	$post = get_page_by_path('hello-world', OBJECT, 'post');
    if ($post) {
        wp_delete_post($post->ID,true);
    }
}

add_filter( 'ocdi/import_files', 'anews_delete_hello_world' );