<?php
/**
 * enqueue Script for admin dashboard.
 */

if (!function_exists('sewa_widgets_backend_enqueue')) :
    function sewa_widgets_backend_enqueue($hook)
    {
    if ('widgets.php' != $hook) {
            return;
        }

       

        wp_enqueue_style('sewa-pt-admin', get_template_directory_uri() . '/assets/css/pt-admin-css.css', array(), '2.0.1');

         wp_register_script('sewa-custom-widgets', get_template_directory_uri() . '/assets/js/widgets.js', array('jquery'), '1.2',true);
        wp_enqueue_media();
        wp_enqueue_script('sewa-custom-widgets');
    }

    add_action('admin_enqueue_scripts', 'sewa_widgets_backend_enqueue');
endif;


/**
 * enqueue Admins style for admin dashboard.
 */

if ( !function_exists( 'sewa_admin_css_enqueue' ) ) :
    
    function sewa_admin_css_enqueue($hook)
    
    {
        if ( 'post.php' != $hook ) {
            return;
        }
        wp_enqueue_style('sewa-admin', get_template_directory_uri() . '/assets/css/admin.css', array(), '2.0.0');

        wp_register_script('sewa-page-builder-widgets', get_template_directory_uri() . '/assets/js/page-builder-widgets.js', array('jquery'), true);
        wp_enqueue_media();
        wp_enqueue_script('sewa-page-builder-widgets');

        
         }
    add_action('admin_enqueue_scripts', 'sewa_admin_css_enqueue');
endif;


if ( !function_exists( 'sewa_admin_css_enqueue_new_post')) :
    
    function sewa_admin_css_enqueue_new_post( $hook )
    
    {
        if ( 'post-new.php' != $hook ) {
            return;
        }
        wp_enqueue_style('sewa-admin', get_template_directory_uri() . '/assets/css/admin.css', array(), '2.0.0');


      wp_register_script('sewa-page-builder-widget', get_template_directory_uri() . '/assets/js/page-builder-widgets.js', array('jquery'), true);

        wp_enqueue_media();

        wp_enqueue_script('sewa-page-builder-widget');
    }
    add_action( 'admin_enqueue_scripts', 'sewa_admin_css_enqueue_new_post' );
endif;

/**
 * Functions for get_theme_mod()
 *
 * @package Canyon Themes
 * @subpackage Sewa
 */

if ( !function_exists( 'sewa_get_option' ) ) :

    /**
     * Get theme option.
     *
     * @since 1.0.0
     *
     * @param string $key Option key.
     * @return mixed Option value.
     */
    function sewa_get_option( $key = '' )
    {
        if (empty( $key ) ) {
            return;
        }
        $sewa_default_options = sewa_get_default_theme_options();

        $default      = (isset($sewa_default_options[$key])) ? $sewa_default_options[$key] : '';

        $theme_option = get_theme_mod($key, $default);

        return $theme_option;

    }

endif;

/**
 * Search Icons
 * =====================================
 */
if ( !function_exists('sewa_search_icon') ) :
    function sewa_search_icon() {
        $search_icon = sewa_get_option('sewa_header_search_icon');
        if( 1 == $search_icon ) {  ?>
            <span class="search-toggle d-none d-md-block"><i class="fa fa-search"></i></span>
            <div id="header-search-box" class="search-box-wrapper">
                <div class="search-box">
                    <?php get_search_form(); ?>
                </div>
            </div>
        <?php
        
         }
    }
add_action( 'header_search_icon', 'sewa_search_icon' );
endif;
/**
 * footer social
 * =====================================
 */
if ( !function_exists('sewa_footer_social') ) :
function sewa_footer_social() {
$sewa_footer_social = sewa_get_option('sewa_footer_social');
    if (has_nav_menu('social-link') && $sewa_footer_social ==1 ) {
        wp_nav_menu(array(
                'theme_location'  => 'social-link',
                'depth'           => 1,
                'container'       => 'div',
                'container_class' => 'social-links',
                'container_id'    => 'social-foot-menu',
                'menu_class'      => 'nav navbar-nav pull-right',
                'fallback_cb'     => 'wp_bootstrap_navwalker::fallback',
                'walker'          => new WP_Bootstrap_Navwalker()
            )
        );
    }
}
add_action( 'sewa_show_footer_social', 'sewa_footer_social', 20 );
endif;

/**
 * Goto Top functions
 *
 * @since Sewa 1.0.0
 *
 */

if (!function_exists('sewa_go_to_top' )) :

				function sewa_go_to_top()
				{
						 $sewa_to_top = sewa_get_option('sewa_footer_go_to_top');
						 if( $sewa_to_top == 1 )
						 {
						?>
								<a id="toTop" class="go-to-top" href="#" title="<?php esc_attr_e('Go to Top', 'sewa'); ?>">
										<i class="fa fa-angle-double-up"></i>
								</a>
					<?php
						}
				}

add_action('sewa_go_to_top_hook', 'sewa_go_to_top', 20 );
endif;


/**
 * define size of logo.
 */

if (!function_exists('sewa_custom_logo_setup')) :
    function sewa_custom_logo_setup()
    {
        add_theme_support('custom-logo', array(
            'height' => 35,
            'width' => 190,
            'flex-width' => true,
        ));
    }

    add_action('after_setup_theme', 'sewa_custom_logo_setup');
endif;

/**
 * Display related posts from same category
 *
 * @since Sewa 1.0.0
 *
 * @param int $post_id
 * @return void
 *
*/
if ( !function_exists('sewa_related_post') ) :

    function sewa_related_post( $post_id ) {

        $related_post_hide = absint(sewa_get_option( 'sewa_related_post_show_hide'));
        $related_post_title = esc_html(sewa_get_option('sewa_related_post_title'));

        if( 0 == $related_post_hide ){
            return;
        }
        $categories = get_the_category( $post_id );
        if ($categories) {
            $category_ids = array();
            foreach ($categories as $category) {
                $category_ids[] = $category->term_id;
            }
            ?>
            <div class="related-pots-block">
                <h2 class="widget-title">
                    <?php echo $related_post_title; ?>
                </h2>
                <ul class="related-post clear row">
                    <?php
                    $sewa_cat_post_args = array(
                        'category__in'       => $category_ids,
                        'post__not_in'       => array($post_id),
                        'post_type'          => 'post',
                        'posts_per_page'     => 3,
                        'post_status'        => 'publish',
                        'ignore_sticky_posts'=> true
                    );
                    $sewa_featured_query = new WP_Query( $sewa_cat_post_args );
                    while ( $sewa_featured_query->have_posts() ) : $sewa_featured_query->the_post();
                        ?>
                        <div class="col-sm-4">
                            <li class=" related-box">
                                <?php
                                if ( has_post_thumbnail() ):
                                    ?>
                                    <figure class="widget-image">
                                        <a href="<?php the_permalink()?>">
                                            <?php the_post_thumbnail('sewa-small-thumb'); ?>
                                        </a>
                                    </figure>
                                <?php
                                endif;
                                    /**
                                     * sewa_meta_fields_section hook
                                     * @since Sewa 1.0.0
                                     *
                                     * @hooked sewa_meta_fields_section_action -  10
                                     */
                                    do_action('sewa_meta_fields_section_action');
                                 ?>
                                <div class="featured-desc">
                                    <a href="<?php the_permalink()?>">
                                        <h2 class="title">
                                           <?php the_title(); ?>
                                        </h2>
                                        <p>
                                            <?php echo esc_html( wp_trim_words( get_the_content(), 10) ); ?>
                                        </p>
                                    </a>
                                </div>
                            </li>
                        </div>
                    <?php
                    endwhile;
                    wp_reset_postdata();
                    ?>
                </ul>
            </div> <!-- .related-post-block -->
        <?php
        }
    }
endif;
add_action( 'sewa_related_post_action', 'sewa_related_post', 10, 1 );

/**
 * Meta Fields in Post and Related
 *
 * @since Sewa 1.0.0
 *
 * @param int $post_id
 * @return void
 *
*/
if ( !function_exists('sewa_meta_fields_section') ) :

    function sewa_meta_fields_section( $post_id ) {
        /**
         * sewa_author information section
         * @since Sewa 1.0.0
         *
        */
        ?>
        <div class="meta-fields">
            <div class="date">
                <span><?php echo esc_html( get_the_date('M') ); ?></span>
                <span><?php echo esc_html( get_the_date('d') ); ?></span>
                <span><?php echo esc_html( get_the_date('Y') ); ?></span>
            </div>
            <div class="text-left author-post">
              <a href="<?php echo esc_url( get_author_posts_url(get_the_author_meta('ID')) ); ?> "><?php the_author(); ?></a>
            </div>
        </div>
<?php  }
endif;
add_action( 'sewa_meta_fields_section_action', 'sewa_meta_fields_section', 10, 1 );


/**
 * Post Navigation in Single Post
 *
 * @since Sewa 1.0.0
 *
 * @param int $post_id
 * @return void
 *
*/
if ( !function_exists('sewa_post_navigation') ) :

    function sewa_post_navigation( $post_id ) {
            
            the_post_navigation(array(
                'next_text' => '<span class="meta-nav" aria-hidden="true">' . esc_html__('Next', 'sewa') . '</span> ' .
                    '<span class="screen-reader-text">' . esc_html__('Next post:', 'sewa'),
                'prev_text' => '<span class="meta-nav" aria-hidden="true">' . esc_html__('Previous', 'sewa') . '</span> ' .
                    '<span class="screen-reader-text">' . esc_html__('Previous post:', 'sewa'),
            ));
}
endif;
add_action( 'sewa_post_navigation_action', 'sewa_post_navigation', 10, 1 );

/**
 * Post Navigation Function
 *
 * @since Sewa 1.0.0
 *
 * @param null
 * @return void
 *
 */
if (!function_exists('sewa_posts_navigation')) :
    function sewa_posts_navigation()
    {
        $pagination_types = sewa_get_option( 'sewa_blog_pagination_types' );

        if ('default' == $pagination_types) {
            the_posts_navigation();
        }
        else{
            global $wp_query;
            $big = 999999999; // need an unlikely integer
            echo paginate_links(array(
                'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
                'format' => '?paged=%#%',
                'current' => max(1, get_query_var('paged')),
                'total' => $wp_query->max_num_pages,
                'prev_text' => __('&laquo; Prev', 'sewa'),
                'next_text' => __('Next &raquo;', 'sewa'),
            ));
        }
    }
endif;
add_action('sewa_action_navigation', 'sewa_posts_navigation', 10);

/**
 * Sanitize Multiple Category
 * =====================================
 */

if ( !function_exists('sewa_sanitize_multiple_category') ) :

function sewa_sanitize_multiple_category( $values ) {

    $multi_values = !is_array( $values ) ? explode( ',', $values ) : $values;

    return !empty( $multi_values ) ? array_map( 'sanitize_text_field', $multi_values ) : array();
}

endif;

/**
 * remove [..] from excerpt
 * =====================================
 */
function sewa_excerpt_more( $more ) {
    if( !is_admin() ){
     return '';
    }
}
add_filter('excerpt_more', 'sewa_excerpt_more');

/**
 * Run ajax request.
 */
if ( ! function_exists('sewa_wp_pages_plucker') ) :
    /**
     * Sending pages with ids
     */
    function sewa_wp_pages_plucker()
    {
        $pages = get_pages(
            array (
                'parent'  => 0, // replaces 'depth' => 1,
            )
        );
        $ids = wp_list_pluck( $pages, 'post_title', 'ID' );
        return wp_send_json($ids);
    }
endif;
add_action( 'wp_ajax_sewa_wp_pages_plucker', 'sewa_wp_pages_plucker' );
add_action( 'wp_ajax_nopriv_sewa_wp_pages_plucker', 'sewa_wp_pages_plucker' );

/**
 * Load Metabox file
 * =====================================
 *
 * /**
 * Metabox for Page Layout Option
 */

require get_template_directory() . '/canyonthemes/metabox/metabox-sidebars.php';

/**
 * Metabox for Fontawesome Class
 */

require get_template_directory() . '/canyonthemes/metabox/metabox-icon.php';

/*
* Including Custom Widget Files
=====================================
/**
 * Custom quote Widget
 */
require get_template_directory() . '/canyonthemes/widget/quote-widget.php';

/**
 * Custom Welcome Message Widget
 */
require get_template_directory() . '/canyonthemes/widget/welcome-msg-widget.php';

/**
 * Custom Feature Widget
 */
require get_template_directory() . '/canyonthemes/widget/feature-widget.php';

/**
 * Custom Services Widget
 */
require get_template_directory() . '/canyonthemes/widget/services-widget.php';

/**
 * Custom Mission Widget
 */
require get_template_directory() . '/canyonthemes/widget/mission-widget.php';

/**
 * Custom Recent Post Widget
 */
require get_template_directory() . '/canyonthemes/widget/recent-post-widget.php';

/**
 * Custom Testimonial  Widget
 */
require get_template_directory() . '/canyonthemes/widget/testimonial-widget.php';


/**
 * Custom Our Work Widget
 */
require get_template_directory() . '/canyonthemes/widget/our-work-widget.php';
