<?php
/**
 * Class for adding Our Features Section Widget
 *
 * @package Canyon Themes
 * @subpackage Sewa
 * @since 1.0.0
 */
if( !class_exists( 'Sewa_Feature_Widget') ){

    class Sewa_Feature_Widget extends WP_Widget
    {
        private function defaults()
        {
            /*defaults values for fields*/
            $defaults = array(
                 
                 'features_title'      => esc_html__('CORE FEATURES', 'sewa'),
                 'sub_title'           => esc_html__('Check Our All Features','sewa'),
                 'features_background' => '',
                  'features' => ''
            );
            return $defaults;
        }

        public function __construct()
        
        {
            parent::__construct(
                /*Widget ID*/
                'sewa_feature_widget',
                /*Widget name*/
                 esc_html__('Sewa Feature Widget', 'sewa'),
                 /*Widget Description*/
                 array('description' => esc_html__('Sewa Feature Section', 'sewa'))
            );
        }
        
        /**
         * Function to Creating widget front-end. This is where the action happens
         *
         * @access public
         * @since 1.0
         *
         * @param array $args widget setting
         * @param array $instance saved values
         *
         * @return void
         *
         */

        public function widget( $args, $instance )
        {
            if ( !empty( $instance ) ) 
             {
                $instance = wp_parse_args( (array) $instance, $this->defaults() );
                /*default values*/
             
                $features_title = apply_filters( 'widget_title', !empty( $instance['features_title'] ) ? esc_html( $instance['features_title'] ) : '', $instance, $this->id_base);

                $subtitle     =  esc_html( $instance['sub_title'] );
             
                $features_background  = esc_url($instance['features_background']);

                $features = ( ! empty( $instance['features'] ) ) ? $instance['features'] : array();

                echo $args['before_widget'];
             
                ?>
                <section id="section1" class="section1 section-margine">
                    <?php if (isset($features) && !empty($features['main'])) : ?>    
                    <div class="container">
                        <div class="row">
                            <div class="col-lg-6 col-md-12 col-xs-12">
                                <div class="item-holder" style="background: url(<?php echo $features_background; ?>);background-repeat: no-repeat;"> 
                                </div>                                        
                            </div>
                            <div class="col-lg-6 col-md-12 col-xs-12">
                                    <?php if(!empty( $features_title ) ) { ?>
                                    <div class="feature-title">
                                        <div class="sec-title two">
                                          <h6><?php echo $subtitle; ?></h6>
                                          <h2><?php echo esc_html( $features_title );?></h2>
                                        </div>
                                    </div>
                                    <?php } 

                                 $post_in = array();

                                if  (count($features) > 0 && is_array($features) )
                                {

                                      $post_in[0] = $features['main'];

                                      foreach ( $features as $our_feature )
                                    {
                                          
                                        if( isset( $our_feature['page_ids'] ) && !empty( $our_feature['page_ids'] ) )

                                           {
                                            
                                               $post_in[] = $our_feature['page_ids'];
                                            
                                            }
                                    }
                                      

                                }     
                                
                                if( !empty( $post_in )) :
                                    $features_page_args = array(
                                            'post__in'            => $post_in,
                                            'orderby'             => 'post__in',
                                            'posts_per_page'      => count( $post_in ),
                                            'post_type'           => 'page',
                                            'no_found_rows'       => true,
                                            'post_status'         => 'publish'
                                    );

                                    $features_query = new WP_Query( $features_page_args );

                                    /*The Loop*/
                                    if ( $features_query->have_posts() ):
                                        $i = 1;
                                        while ( $features_query->have_posts() ):$features_query->the_post();
                                                        
                                              $icon = get_post_meta( get_the_ID(), 'sewa_icon', true );
                                              
                                              ?><a href="<?php the_permalink(); ?>">
                                              <div class="section-1-box" data-wow-delay=".<?php echo esc_attr($i); ?>">
                                                    <?php
                                                    if(!empty($icon))
                                                    {
                                                    ?>
                                                        <div class="section-1-box-icon-background">
                                                            <i class="fa <?php echo esc_attr($icon); ?>"></i>
                                                        </div>
                                              <?php } ?>
                                              
                                                    <h4><?php the_title() ?></h4>
                                                    <p><?php the_excerpt(); ?></p>
                                              </div>
                                           </a>
                                      <?php
                                          endwhile;
                                    endif;
                                    wp_reset_postdata();
                              endif;
                                ?>                                    
                              
                            </div>
                        </div>
                    </div>
                   <?php endif; ?> 
                </section>
                <?php
                echo $args['after_widget'];
            }
        }

        /**
         * Function to Updating widget replacing old instances with new
         *
         * @access public
         * @since 1.0
         *
         * @param array $new_instance new arrays value
         * @param array $old_instance old arrays value
         *
         * @return array
         *
         */
        public function update( $new_instance, $old_instance )
         {
            $instance           = $old_instance;
            $instance['features_title'] = (isset($new_instance['features_title'])) ? sanitize_text_field( $new_instance['features_title'] ) : '';
            $instance['sub_title'] = ( isset( $new_instance['sub_title'])) ? sanitize_text_field($new_instance['sub_title']) : ''; 
            $instance['features_background'] = esc_url_raw($new_instance['features_background']);

            if (isset($new_instance['features']))
            {
				/* PHP 8 compatibility removed the below code 
                foreach($new_instance['features'] as $feature){
                  
                  $feature['page_ids'] = absint($feature['page_ids']);
                }
				*/
                $instance['features'] = $new_instance['features'];
            }
            
            return $instance;


        }
        /*Widget Backend*/
        public function form( $instance )
        {
            $instance           = wp_parse_args( (array) $instance, $this->defaults() );
            /*default values*/
            $sewa_features_title = esc_attr( $instance[ 'features_title' ] );
            $features_background   = esc_url( $instance['features_background'] );
            $subtitle   = esc_attr( $instance['sub_title'] );
            $features              = ( ! empty( $instance['features'] ) ) ? $instance['features'] : array(); 
  
            ?>
           <span class="pt-sewa-additional"> 
            <p>
                <label for="<?php echo esc_attr( $this->get_field_id('features_title')); ?>">
                    <?php esc_html_e('Title', 'sewa'); ?>
                </label><br/>
                <input type="text" name="<?php echo esc_attr( $this->get_field_name( 'features_title') ); ?>" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'features_title' ) ); ?>" value="<?php echo $sewa_features_title?>">
            </p>

            <p>
                <label for="<?php echo esc_attr( $this->get_field_id('sub_title') ); ?>">
                    <?php esc_html_e( 'Sub Title', 'sewa'); ?>
                </label><br/>
                <input type="text" name="<?php echo esc_attr($this->get_field_name('sub_title')); ?>" class="widefat" id="<?php echo esc_attr($this->get_field_id('sub_title')); ?>" value="<?php echo $subtitle; ?>">
            </p>

           <hr>

            <label><strong><?php _e( 'Select Pages', 'sewa' ); ?>:</strong></label>
            <br/>
            <small><?php _e( 'Add Page and Remove. Please do not forget to add Icon and Excerpt  on selected pages.', 'sewa' ); ?></small>
          
              <?php

                if  (count($features) >=  1 && is_array($features) ){
                  
                   $selected = $features['main'];

                }
                else
                {
                  $selected = "";
                }

                $repeater_id   = $this->get_field_id( 'features' ).'-main';
                $repeater_name = $this->get_field_name( 'features'). '[main]';

                $args = array(
                    'selected'          => $selected,
                    'name'              => $repeater_name,
                    'id'                => $repeater_id,
                    'class'             => 'widefat pt-select',
                    'show_option_none'  => __( 'Select Page', 'sewa'),
                    'option_none_value' => 0 // string
                );
                wp_dropdown_pages( $args );
              ?>
              
              <?php
           
            $counter = 0;
           
            if ( count( $features ) > 0 ) {
                foreach( $features as $feature ) {

                    if ( isset( $feature['page_ids'] ) ) { ?>
                      <div class="pt-sewa-sec">
      
                          <?php
        
                            $repeater_id     = $this->get_field_id( 'features' ) .'-'. $counter.'-page_ids';
                            $repeater_name   = $this->get_field_name( 'features' ) . '['.$counter.'][page_ids]';

                            $args = array(
                                'selected'          => $feature['page_ids'],
                                'name'              => $repeater_name,
                                'id'                => $repeater_id,
                                'class'             => 'widefat pt-select',
                                'show_option_none'  => __( 'Select Page', 'sewa'),
                                'option_none_value' => 0 // string
                            );
                            wp_dropdown_pages( $args );
                            ?>

                            <a onclick="triggerSewaRemove(this);" class="pt-sewa-remove delete"></strong><?php esc_html_e('Remove Section','sewa') ?></strong></a>
                      </div>
                      <?php
                      $counter++;
                   }
                }
            }

            ?>

           </span>
           <a class="pt-sewa-add button" data-id="sewa_feature_widget" id="<?php echo $repeater_id; ?>"><?php _e('Add New Section', 'sewa'); ?></a> 
           <hr>
            <p>
                <label for="<?php echo $this->get_field_id('features_background'); ?>">
                    <strong><?php _e( 'Select Background Image', 'sewa' ); ?>:
               </strong> </label>
                <span class="img-preview-wrap" <?php  if ( empty( $features_background ) ){ ?> style="display:none;" <?php  } ?>>
                    <img class="widefat" src="<?php echo esc_url( $features_background ); ?>" alt="<?php esc_attr_e( 'Image preview', 'sewa' ); ?>"  />
                </span><!-- .img-preview-wrap -->
                <input type="text" class="widefat" name="<?php echo $this->get_field_name('features_background'); ?>" id="<?php echo $this->get_field_id('features_background'); ?>" value="<?php echo esc_url( $features_background ); ?>" />
                <input type="button" id="custom_media_button"  value="<?php esc_attr_e( 'Upload Image', 'sewa' ); ?>" class="button media-image-upload" data-title="<?php esc_attr_e( 'Select Background Image','sewa'); ?>" data-button="<?php esc_attr_e( 'Select Background Image','sewa'); ?>"/>
                <input type="button" id="remove_media_button" value="<?php esc_attr_e( 'Remove Image', 'sewa' ); ?>" class="button media-image-remove" />
           </p>
            <?php
        }
    }
}

add_action( 'widgets_init', 'sewa_feature_widget' );
function sewa_feature_widget()
{
    register_widget( 'Sewa_Feature_Widget' );
}










