<?php

/**
 * Elementor Box Content Responsive Widget.
 *
 */

class Article_1 extends \Elementor\Widget_Base
{


  public function get_name()
  {
    return 'article-1';
  }

  public function get_title()
  {
    return __('Article 1', 'leora');
  }

  public function get_icon()
  {
    return ' eicon-posts-grid';
  }

  public function get_categories()
  {
    return ['elementor-widget-be'];
  }



  private function grid_get_all_post_type_categories($post_type)
  {
    $options = array();

    if ($post_type == 'post') {
      $taxonomy = 'category';
    } else {
      $taxonomy = 'product_cat';
    }

    // Get categories for post type.
    $terms = get_terms(
      array(
        'taxonomy' => $taxonomy,
        'hide_empty' => false,
      )
    );

    foreach ($terms as $term) {
      $options[$term->slug] = $term->name;
    }

    return $options;
  }
  private function grid_get_all_post_types()
  {
    $options = array();
    $exclude = array('attachment', 'elementor_widget_be'); // excluded post types

    $args = array(
      'public' => true,
    );

    foreach (get_post_types($args, 'objects') as $post_type) {

      if (!isset($post_type->name)) {
        continue;
      }

      if (!isset($post_type->label)) {
        continue;
      }

      if (in_array($post_type->name, $exclude) === true) {
        continue;
      }

      $options[$post_type->name] = $post_type->label;
    }

    return $options;
  }
      protected function register_controls()
    {
        $this->init_content_general_controls();
    }

    protected function init_content_general_controls()
  {
    $this->start_controls_section(
      'section_content',
      [
        'label' => __('Content', 'leora'),
      ]
    );
    $this->add_control(
      'title',
      [
        'label' => __('Title', 'leora'),
        'type' => \Elementor\Controls_Manager::TEXT,
      ]
    );
    $this->add_control(
      'title_more',
      [
        'label' => __('Title More', 'leora'),
        'type' => \Elementor\Controls_Manager::TEXT,
      ]
    );
    // Post type.
    $this->add_control(
      'grid_post_type',
      [
        'type'    => \Elementor\Controls_Manager::SELECT,
        'label'   => '<i class="fa fa-tag"></i> ' . __('Post Type', 'leora'),
        'default' => 'post',
        'options' => $this->grid_get_all_post_types(),
      ]
    );
    $this->add_control(
      'grid_items',
      [
        'type'        => \Elementor\Controls_Manager::NUMBER,
        'label'       => '<i class="fa fa-th-large"></i> ' . __('Items', 'leora'),
        'placeholder' => __('How many items?', 'leora'),
        'default'     => 4,
      ]
    );

    // Post categories.
    $this->add_control(
      'grid_post_categories',
      [
        'type'    => \Elementor\Controls_Manager::SELECT,
        'label'   => '<i class="fa fa-folder"></i> ' . __('Category', 'leora'),
        'options' => $this->grid_get_all_post_type_categories('post'),
        'condition' => [
          'grid_post_type' => 'post',
        ],
      ]
    );
    $this->add_control(
      'show_more',
      [
        'label' => __('Show more', 'leora'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => __('Show', 'leora'),
        'label_off' => __('Hide', 'leora'),
        'return_value' => 'yes',
        'default' => 'yes',
      ]
    );
    $this->add_control(
      'website_link',
      [
        'label' => __('Link More', 'leora'),
        'type' => \Elementor\Controls_Manager::URL,
        'placeholder' => __('https://your-link.com', 'leora'),
        'show_external' => true,
        'default' => [
          'url' => '',
          'is_external' => true,
          'nofollow' => true,
        ],
      ]
    );
    $this->add_control(
      'grid_pagination',
      [
        'label' => '<i class="fa fa-arrow-circle-right"></i> ' . __('Pagination', 'leora'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'default' => '',
      ]
    );
    // Order by.
    $this->add_control(
      'grid_order_by',
      [
        'type'    => \Elementor\Controls_Manager::SELECT,
        'label'   => '<i class="fa fa-sort"></i> ' . __('Order by', 'leora'),
        'default' => 'date',
        'options' => [
          'date'          => __('Date', 'leora'),
          'title'         => __('Title', 'leora'),
          'modified'      => __('Modified date', 'leora'),
          'comment_count' => __('Comment count', 'leora'),
          'rand'          => __('Random', 'leora'),
        ],
      ]
    );

    // Price.
    $this->add_control(
      'grid_content_price',
      [
        'label' => '<i class="fa fa-usd"></i> ' . __('Price', 'leora'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'default' => 'yes',
        'condition' => [
          'section_grid.grid_post_type' => 'product',
        ],
      ]
    );
    $this->end_controls_section();
    $this->start_controls_section(
      'section_style',
      [
        'label' => __( 'Style', 'leora' ),
        'tab' => \Elementor\Controls_Manager::TAB_STYLE,
      ]
    );
    $this->add_control(
     'background',
     [
       'label' => __('background color', 'leora'),
       'type' => \Elementor\Controls_Manager::COLOR,
       'default' => '#fff',
       
       'selectors' => [
         '{{WRAPPER}} .article-1' => 'background-color: {{VALUE}}!important;',
       ],
     ]
   );
   $this->add_control(
     'box_title',
     [
       'label' => __('title', 'leora'),
       'type' => \Elementor\Controls_Manager::COLOR,
       'default' => '#535353',
       
       'selectors' => [
         '{{WRAPPER}} .article-1 .Survey-title' => 'color: {{VALUE}}!important;',
       ],
     ]
   );
   $this->add_control(
     'box_title_more',
     [
       'label' => __('Title More', 'leora'),
       'type' => \Elementor\Controls_Manager::COLOR,
       'default' => '#eee',
       
       'selectors' => [
         '{{WRAPPER}} .article-1 .Survey-bt-mor' => 'background-color: {{VALUE}}!important;',
       ],
     ]
   );
   $this->add_control(
     'box_title_more_text',
     [
       'label' => __('Title More text', 'leora'),
       'type' => \Elementor\Controls_Manager::COLOR,
       'default' => '#535353',
       
       'selectors' => [
         '{{WRAPPER}} .article-1 .Survey-bt-mor' => 'color: {{VALUE}}!important;',
       ],
     ]
   );
    $this->add_control(
     'border_radius_article_blog_1',
     [
       'label' => __( 'border radius', 'leora' ),
       'type' => \Elementor\Controls_Manager::SLIDER,
       'size_units' => [ 'px', '%' ],
       'range' => [
         'px' => [
           'min' => 0,
           'max' => 100,
           'step' => 1,
         ],
         '%' => [
           'min' => 0,
           'max' => 100,
         ],
       ],
       'default' => [
         'unit' => 'px',
         'size' => 5,
       ],
       'selectors' => [
        '{{WRAPPER}} .article-1' => 'border-radius: {{SIZE}}{{UNIT}}!important;',
      ],
     ]
   );
    $this->end_controls_section();
    $this->start_controls_section(
     'section_style_box',
     [
       'label' => __( 'box', 'leora' ),
       'tab' => \Elementor\Controls_Manager::TAB_STYLE,
     ]
   );
   $this->add_control(
     'border_radius_article',
     [
       'label' => __( 'border radius', 'leora' ),
       'type' => \Elementor\Controls_Manager::SLIDER,
       'size_units' => [ 'px', '%' ],
       'range' => [
         'px' => [
           'min' => 0,
           'max' => 100,
           'step' => 1,
         ],
         '%' => [
           'min' => 0,
           'max' => 100,
         ],
       ],
       'default' => [
         'unit' => 'px',
         'size' => 5,
       ],
       'selectors' => [
         '{{WRAPPER}} .article-1 .archive-artic' => 'border-radius: {{SIZE}}{{UNIT}}!important;',
       ],
     ]
   );
    $this->end_controls_section();
   }

  protected function render()
  {
    $settings = $this->get_settings();
    $target = $settings['website_link']['is_external'] ? ' target="_blank"' : '';
    $nofollow = $settings['website_link']['nofollow'] ? ' rel="nofollow"' : '';
?>
    <div class="container">
      <section class="background_box_article article-1 col-12">
        <header class="col title-five">
          <a class="inner-card" href="<?php the_permalink(); ?>">
            <h4 class="Survey-title"><?php echo $settings['title']; ?></h4>
            <?php
            if ('yes' === $settings['show_more']) {
              echo '<a class="Survey-bt-mor" href="' . $settings['website_link']['url'] . '"' . $target . $nofollow . '>';
              echo $settings['title_more'];
              ''; ?>
          </a>
        <?php } ?>
        </a>
        </header>
        <?php
        $args = array();
        $args['post_status'] = 'publish';
        $args['ignore_sticky_posts'] = 1;
        if (!empty($settings['grid_post_type']) && post_type_exists($settings['grid_post_type'])) {
          $args['post_type'] = $settings['grid_post_type'];
        }
        if (!empty($settings['grid_post_categories']) && $settings['grid_post_type'] == 'post') {
          $args['category_name'] = $settings['grid_post_categories'];
        }
        if (!empty($settings['grid_product_categories']) && $settings['grid_post_type'] == 'product') {
          $args['tax_query'] = array(
            'relation' => 'AND',
            array(
              'taxonomy' => 'product_cat',
              'field' => 'slug',
              'terms' => $settings['grid_product_categories'],
            ),
          );
        }
        if (!empty($settings['grid_items']) && intval($settings['grid_items']) == $settings['grid_items']) {
          $args['posts_per_page'] = $settings['grid_items'];
        }
        if (!empty($settings['grid_order_by'])) {
          $args['orderby'] = $settings['grid_order_by'];
        }
        if (!empty($settings['grid_pagination'])) {
          $paged         = get_query_var('paged');
          if (empty($paged)) {
            $paged         = get_query_var('page');
          }
          $args['paged'] = $paged;
        }
        $query = new \WP_Query($args);
        if ($query->have_posts()) {
          while ($query->have_posts()) {
            $query->the_post();
        ?>
            <div class="col-12 col-xl-3 col-md-3 col-sm-6 col-xs-6 right">
              <div class="archive-artic">
                <div class="photo">
                  <a href="<?php the_permalink(); ?>">
                    <?php
                    if (has_post_thumbnail()) {
                      the_post_thumbnail(array(300, 300), array('alt' => '' . get_the_title(), 'title' => '' . get_the_title() . ''));
                    } else { ?>
                      <img src="<?php print(get_template_directory_uri()); ?>/assets/images/thumb.gif" width="60" height="60" alt="<?php _e('Image not found', 'leora'); ?>" title="<?php _e('Image not found', 'leora'); ?>">
                    <?php } ?>
                  </a>
                  <div class="social">
                    <a class="twit-area" href="https://twitter.com/home?status=Reading: <?php the_permalink(); ?>" title="Share this post on Twitter!" target="_blank">
                      <i class="fa-brands fa-x-twitter"></i></a>
                    <a class="face-area" href="https://www.facebook.com/sharer.php?u=<?php the_permalink(); ?>&t=<?php the_title(); ?>" target="_blank">
                      <i class="fab fa-facebook-f"></i></a>
                    <a class="in-area" href="http://www.linkedin.com/shareArticle?mini=true&amp;title=<?php the_title(); ?>&amp;url=<?php the_permalink(); ?>" title="Share on LinkedIn" rel="external nofollow" rel="nofollow" target="_blank">
                      <i class="fab fa-linkedin-in"></i></a>
                    <a rel="nofollow" target="_blank" class="what-area" href="whatsapp://send?text=<?php the_permalink(); ?>" data-action="share/whatsapp/share">
                      <i class="fab fa-whatsapp"></i></a>
                    <a class="tlg" href="https://t.me/share/url?url=<?php the_permalink(); ?>&amp;title=<?php the_title(); ?>" title="share on Telegram" rel="nofollow" target="_blank">
                      <i class="fal fa-paper-plane"></i></a>
                      <a href=""><?php do_action('type_article'); ?></a>
                    <a href="<?php the_permalink(); ?>"><i class="fal fa-link"></i></a>
                  </div>
                </div>
                <div class="arch-title">
                  <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                </div>
              </div>
            </div>
          <?php
          }
          if (!empty($settings['grid_pagination'])) {
          ?>
            <div class="pagination root" style="width: 100%;">
              <?php
              $big = 999999999;
              $totalpages = $query->max_num_pages;
              $current = max(1, $paged);
              $paginate_args = array(
                'base'      => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
                'format'    => '?paged=%#%',
                'current'   => $current,
                'total'     => $totalpages,
                'show_all'  => false,
                'end_size'  => 1,
                'mid_size'  => 3,
                'prev_next' => true,
                'prev_text'          => __('<i class="fa fa-chevron-right" aria-hidden="true"></i>', 'leora'),
                'next_text'          => __('<i class="fa fa-chevron-left" aria-hidden="true"></i>', 'leora'),
                'screen_reader_text' => __('A', 'leora')
              );
              $pagination = paginate_links($paginate_args);
              ?>
              <nav class="pagenumbers">
                <?php echo $pagination; ?>
              </nav>
            </div>
        <?php
          }
        } // end if; 
        ?>
      </section>
    </div>
<?php
  }
}
