Guest User

Untitled

a guest
Sep 29th, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 44.30 KB | None | 0 0
  1. <?php
  2.  
  3. namespace ETC\App\Models\Widgets;
  4.  
  5. use ETC\App\Models\Widgets;
  6.  
  7. /**
  8.  * Instagram widget.
  9.  *
  10.  * @since      1.4.4
  11.  * @package    ETC
  12.  * @subpackage ETC/Models/Widgets
  13.  */
  14. class Instagram extends Widgets {
  15.  
  16.  
  17.     function __construct() {
  18.         $token           = false;
  19.         $API_type        = 'personal';
  20.         $account_type    = 'PERSONAL';
  21.         $this->force_tag = apply_filters( 'et_instagram_force_tag', false );
  22.         $global_user     = '';
  23.         $widget_ops      = array(
  24.             'classname'   => 'null-instagram-feed',
  25.             'description' => esc_html__( 'Displays your latest Instagram photos', 'xstore-core' )
  26.         );
  27.         parent::__construct( 'null-instagram-feed', '8theme - &nbsp;&#160;&nbsp;' . esc_html__( 'Instagram', 'xstore-core' ), $widget_ops );
  28.     }
  29.  
  30.     function widget( $args, $instance ) {
  31.         if (parent::admin_widget_preview(esc_html__('Instagram', 'xstore-core')) !== false) return;
  32.         if ( xstore_notice() ) return;
  33.         $ajax = ( !empty($instance['ajax'] ) ) ? $instance['ajax'] : '';
  34.  
  35.         if ( function_exists('etheme_enqueue_style')) {
  36.             etheme_enqueue_style( 'instagram', !apply_filters('et_ajax_widgets', $ajax) );
  37.             if ( class_exists( 'WPBMap' ) ) {
  38.                 etheme_enqueue_style( 'wpb-instagram', !apply_filters('et_ajax_widgets', $ajax) );
  39.             }
  40.         }
  41.  
  42.         if (apply_filters('et_ajax_widgets', $ajax)){
  43.             echo et_ajax_element_holder( 'Instagram', $instance, '', '', 'widget', $args );
  44.             return;
  45.         }
  46.  
  47.  
  48.         extract( $args, EXTR_SKIP );
  49.  
  50.         $username = empty( $instance['username'] ) ? '' : $instance['username'];
  51.         $limit    = empty( $instance['number'] ) ? 9 : $instance['number'];
  52.         $columns  = empty( $instance['columns'] ) ? 3 : (int) $instance['columns'];
  53.         $size     = empty( $instance['size'] ) ? 'thumbnail' : $instance['size'];
  54.         $img_type = empty( $instance['img_type'] ) ? 'default' : $instance['img_type'];
  55.         $target   = empty( $instance['target'] ) ? '_self' : $instance['target'];
  56.         $user     = empty( $instance['user'] ) ? '' : $instance['user'];
  57.         $link     = empty( $instance['link'] ) ? '' : $instance['link'];
  58.         $slider   = (empty( $instance['slider'] ) || $instance['slider'] == 'false') ? false : true;
  59.         $spacing  = (empty( $instance['spacing'] )|| $instance['spacing'] == 'false') ? false : true;
  60.         $type     = empty( $instance['type'] ) ? 'widget' : $instance['type'];
  61.         $tag_type = empty( $instance['tag_type'] ) ? false : $instance['tag_type'];
  62.  
  63.         // meta args
  64.         $show_likes = ( !isset( $instance['like_count'] ) ) ? true : $instance['like_count'];
  65.         $show_comments = ( !isset( $instance['comments_count'] ) ) ? true : $instance['comments_count'];
  66.         $hide_empty_meta = ( empty( $instance['hide_empty_meta'] ) ) ? false : $instance['hide_empty_meta'];
  67.  
  68.         // slider args
  69.         $large                     = empty( $instance['large'] ) ? 5 : $instance['large'];
  70.         $notebook                  = empty( $instance['notebook'] ) ? 4 : $instance['notebook'];
  71.         $tablet_land               = empty( $instance['tablet_land'] ) ? 3 : $instance['tablet_land'];
  72.         $tablet_portrait           = empty( $instance['tablet_portrait'] ) ? 2 : $instance['tablet_portrait'];
  73.         $mobile                    = empty( $instance['mobile'] ) ? 1 : $instance['mobile'];
  74.         $slider_autoplay           = empty( $instance['slider_autoplay'] ) ? false : true;
  75.         $slider_stop_on_hover      = empty( $instance['slider_stop_on_hover'] ) ? false : true;
  76.         $slider_speed              = empty( $instance['slider_speed'] ) ? 300 : $instance['slider_speed'];
  77.         $slider_interval           = empty( $instance['slider_interval'] ) ? 3000 : $instance['slider_interval'];
  78.         $slider_loop               = ( ! isset( $instance['slider_loop'] ) || empty( $instance['slider_loop'] ) ) ? false : true;
  79.         $pagination_type           = empty( $instance['pagination_type'] ) ? 'hide' : $instance['pagination_type'];
  80.         $default_color             = empty( $instance['default_color'] ) ? '#e6e6e6' : $instance['default_color'];
  81.         $active_color              = empty( $instance['active_color'] ) ? '#b3a089' : $instance['active_color'];
  82.         $hide_fo                   = empty( $instance['hide_fo'] ) ? '' : $instance['hide_fo'];
  83.         $navigation_type           = empty( $instance['navigation_type'] ) ? 'arrow' : $instance['navigation_type'];
  84.         $navigation_position_style = empty( $instance['navigation_position_style'] ) ? 'arrows-hover' : $instance['navigation_position_style'];
  85.         $navigation_style          = empty( $instance['navigation_style'] ) ? '' : $instance['navigation_style'];
  86.         $navigation_position       = empty( $instance['navigation_position'] ) ? 'middle' : $instance['navigation_position'];
  87.         $hide_buttons              = empty( $instance['hide_buttons'] ) ? false : true;
  88.         $hide_buttons_for          = empty( $instance['hide_buttons_for'] ) ? '' : $instance['hide_buttons_for'];
  89.         $is_preview                = empty( $instance['is_preview'] ) ? false : true;
  90.         $is_elementor              = (!isset($instance['is_elementor']) || empty( $instance['is_elementor'] )) ? false : true;
  91.  
  92.  
  93.         if ( $type == 'widget' ) {
  94.             $before_widget = str_replace( 'widget', 'sidebar-widget', $before_widget );
  95.             $before_widget = str_replace( array(
  96.                 'sidebar-sidebar-widget',
  97.                 'footer-sidebar-widget',
  98.                 'prefooter-sidebar-widget'
  99.             ), array( 'sidebar-widget', 'footer-widget', 'footer-widget' ), $before_widget );
  100.             $img_type      = 'squared';
  101.         }
  102.  
  103.         echo $before_widget;
  104.         echo parent::etheme_widget_title($args, $instance); // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped
  105.  
  106.         do_action( 'etheme_before_widget', $instance );
  107.  
  108.         $instagram['data'] = array();
  109.  
  110.         $tag = str_replace( '#', '', $username );
  111.  
  112.         // Remove it after instagram update
  113.         if ( ! $user ) {
  114.             $user = apply_filters( 'et_escape_instagram_user', $user );
  115.         }
  116.  
  117.         if ( ! $user ) {
  118.             echo '<div class="woocommerce-info">' . esc_html__( 'To use this element select instagram user', 'xstore-core' ) . '</div>';
  119.         } else {
  120.             $instagram = $this->et_get_instagram( $limit, $tag, '', $user, $type, $tag_type );
  121.  
  122.             if ( is_wp_error( $instagram ) ) {
  123.                 echo $instagram->get_error_message();
  124.             } else {
  125.  
  126.                 $box_id = rand( 1000, 10000 );
  127.  
  128.                 $imgclass = $swiper_container = $swiper_wrapper = '';
  129.                 $swiper_entry_class = array();
  130.                 $attr     = array();
  131.  
  132.                 if ( $slider && $type != 'widget' ) {
  133.                     $swiper_container = 'swiper-container';
  134.                     $swiper_container .= ( $slider_stop_on_hover ) ? ' stop-on-hover' : '';
  135.                     $swiper_container .= ' slider-' . $box_id;
  136.                     $swiper_container .= ( $pagination_type == 'lines' ) ? ' swiper-pagination-lines' : '';
  137.                     $swiper_wrapper   = 'swiper-wrapper';
  138.  
  139.                     $autoplay = '';
  140.                     $speed    = '300';
  141.  
  142.                     if ( $slider_autoplay ) {
  143.                         $autoplay = $slider_interval;
  144.                         $speed    = $slider_speed;
  145.                     }
  146.  
  147.                     $attr = array(
  148.                         'data-breakpoints="1"',
  149.                         'data-xs-slides="' . esc_attr( $mobile ) . '"',
  150.                         'data-sm-slides="' . esc_attr( $tablet_land ) . '"',
  151.                         'data-md-slides="' . esc_attr( $notebook ) . '"',
  152.                         'data-lt-slides="' . esc_attr( $large ) . '"',
  153.                         'data-slides-per-view="' . esc_attr( $large ) . '"',
  154.                         'data-autoplay="' . esc_attr( $autoplay ) . '"',
  155.                         'data-speed="' . esc_attr( $speed ) . '"'
  156.                     );
  157.                     if ( $slider_loop ) {
  158.                         $attr[] = 'data-loop="true"';
  159.                     }
  160.  
  161.                     if ( $spacing ) {
  162.                         $attr[] = 'data-space="0"';
  163.                     }
  164.  
  165.                     $swiper_entry_class[] = $navigation_position;
  166.                     $swiper_entry_class[] = $navigation_position_style;
  167.                 }
  168.  
  169.                 ?>
  170.                 <div class="swiper-entry <?php echo implode(' ', $swiper_entry_class); ?>">
  171.  
  172.                     <div class="widget null-instagram-feed <?php echo esc_attr( $swiper_container ); ?><?php if ( $spacing ) {
  173.                         echo ' instagram-no-space';
  174.                     } ?>"
  175.                         <?php echo implode( ' ', $attr ); ?>
  176.                     >
  177.  
  178.                         <ul class="<?php echo esc_attr( $swiper_wrapper ); ?> instagram-pics clearfix instagram-size-<?php echo esc_attr( $size ); ?> instagram-columns-<?php echo esc_attr( $columns ); ?>"
  179.                             data-force-tag="<?php echo esc_attr( $this->force_tag ); ?>">
  180.                             <?php
  181.  
  182.                             ob_start();
  183.  
  184.                             foreach ( $instagram['data'] as $item ) {
  185.  
  186.                                 if ( $this->API_type == 'old' || $this->force_tag ) {
  187.                                     esc_html_e( 'Error: Old API not supported', 'xstore-core' );
  188.                                     continue;
  189.                                 } else {
  190.                                     if ( isset( $item['thumbnail_url'] ) ) {
  191.                                         $image_src = $item['thumbnail_url'];
  192.                                     } elseif ( isset( $item['media_url'] ) && ! empty( $item['media_url'] ) ) {
  193.                                         $image_src = $item['media_url'];
  194.                                     } else {
  195.                                         $image_src = ET_CORE_URL . 'app/controllers/images/instagram/placeholder.jpg';
  196.                                     }
  197.                                 }
  198.  
  199.  
  200.                                 if ( $link != '' ) {
  201.                                     if ( $this->API_type == 'old' ) {
  202.                                         $username = $item['user']['username'];
  203.                                     } else {
  204.                                         $username = $this->global_user;
  205.                                     }
  206.                                 }
  207.  
  208.                                 $src = 'src="' . esc_url( $image_src ) . '"';
  209.  
  210.                                 $slider_src = 'class="swiper-lazy" data-src="' . esc_url( $image_src ) . '"';
  211.  
  212.                                 $style = 'background: url(' . esc_url( $image_src ) . ');background-size: cover;height: 100%;background-repeat: no-repeat;background-position: center;width: 100%;padding: 100% 0 0;';
  213.  
  214.                                 $img_style = 'visibility: hidden; opacity: 0; position: absolute;';
  215.  
  216.                                 if ( $img_type == 'default' ) {
  217.                                     $style = $img_style = '';
  218.                                 }
  219.  
  220.                                 $likes = $comments = '';
  221.  
  222.                                 if (
  223.                                     $show_likes
  224.                                     && isset($item['like_count'])
  225.                                     && $this->is_hide_by_count($hide_empty_meta, $item['like_count'])
  226.                                 ){
  227.                                     $likes = '<span class="insta-likes">'.$item['like_count'].'</span>';
  228.                                 }
  229.  
  230.                                 if (
  231.                                     $show_comments
  232.                                     && isset($item['comments_count'])
  233.                                     && $this->is_hide_by_count($hide_empty_meta, $item['comments_count'])
  234.                                 ){
  235.                                     $comments = '<span class="insta-comments">'.$item['comments_count'].'</span>';
  236.                                 }
  237.  
  238.                                 if ( $this->API_type == 'old' || $this->force_tag ) {
  239.                                     esc_html_e( 'Error: Old API not supported', 'xstore-core' );
  240.                                 } else {
  241.                                     if ( $this->account_type == 'PERSONAL' ) {
  242.                                         printf(
  243.                                             '<li %s>
  244.                                                    <a href="%s" target="%s" rel="noopener" class="%s" style="%s">
  245.                                                        %s
  246.                                                        <img %s  alt="%s" title="%s" width="1080" height="1080" class="%s" style="%s"/>
  247.                                                        <span class="et-inst-album-count">%s</span>
  248.                                                        <i class="et-inst-media-type media-type-%s"></i>
  249.                                                    </a>
  250.                                                </li>',
  251.                                             ( $slider ) ? 'class="swiper-slide"' : '',
  252.                                             esc_url( $item['permalink'] ),
  253.                                             esc_attr( $target ),
  254.                                             'et-image-type-'.$img_type,
  255.                                             $style,
  256.                                             ( $slider ) ? etheme_loader( false, 'swiper-lazy-preloader' ) : '',
  257.                                             ( $slider ) ? $slider_src : $src,
  258.                                             // esc_attr( $item['caption'] ),
  259.                                             '',
  260.                                             // esc_attr( $item['caption'] ),
  261.                                             '',
  262.                                             $imgclass,
  263.                                             $img_style,
  264.                                             isset($item['album_count']) ? $item['album_count'] . ' ' . _nx( 'item', 'items', $item['album_count'], 'instagram album count text', 'xstore-core' ) : '',
  265.                                             strtolower( $item['media_type'] )
  266.                                         );
  267.                                     } else {
  268.                                         printf(
  269.                                             '<li %s>
  270.                                                    <a href="%s" target="%s" rel="noopener" class="%s" style="%s">
  271.                                                        %s
  272.                                                        <img %s  alt="%s" title="%s" width="1080" height="1080" class="%s" style="%s"/>
  273.                                                         <div class="insta-info">
  274.                                                            %s
  275.                                                            %s
  276.                                                        </div>
  277.                                                        <span class="et-inst-album-count">%s</span>
  278.                                                        <i class="et-inst-media-type media-type-%s"></i>
  279.                                                    </a>
  280.                                                </li>',
  281.                                             ( $slider ) ? 'class="swiper-slide"' : '',
  282.                                             (isset($item['permalink'])) ? esc_url( $item['permalink'] ) : '',
  283.                                             esc_attr( $target ),
  284.                                             'et-image-type-'.$img_type,
  285.                                             $style,
  286.                                             ( $slider ) ? etheme_loader( false, 'swiper-lazy-preloader' ) : '',
  287.                                             ( $slider ) ? $slider_src : $src,
  288.                                             '',
  289.                                             '',
  290.                                             $imgclass,
  291.                                             $img_style,
  292.                                             $likes,
  293.                                             $comments,
  294.                                             isset($item['album_count']) ? $item['album_count'] . ' ' . _nx( 'item', 'items', $item['album_count'], 'instagram album count text', 'xstore-core' ) : '',
  295.                                             (isset($item['media_type'])) ? esc_url( strtolower( $item['media_type'] ) ) : ''
  296.                                         );
  297.                                     }
  298.  
  299.                                 }
  300.                             }
  301.  
  302.  
  303.                             echo apply_filters('etheme_instagram_content', ob_get_clean()); ?>
  304.  
  305.                         </ul>
  306.                         <?php if ( $slider && $pagination_type != "hide" ) {
  307.                             $pagination_class = '';
  308.                             if ( $hide_fo == 'mobile' ) {
  309.                                 $pagination_class = ' mob-hide';
  310.                             } elseif ( $hide_fo == 'desktop' ) {
  311.                                 $pagination_class = ' dt-hide';
  312.                             }
  313.                             echo '<div class="swiper-pagination etheme-css ' . esc_html( $pagination_class ) . '" data-css=".slider-' . $box_id . ' .swiper-pagination-bullet{background-color:' . $default_color . ';} .slider-' . $box_id . ' .swiper-pagination-bullet:hover{ background-color:' . $active_color . '; } .slider-' . $box_id . ' .swiper-pagination-bullet-active{ background-color:' . $active_color . '; }"></div>';
  314.                         } ?>
  315.                     </div>
  316.                     <?php
  317.                     if ( $slider && ( ! $hide_buttons || ( $hide_buttons && $hide_buttons_for != 'both' ) ) ) {
  318.                         $navigation_class = 'swiper-nav';
  319.                         if ( $is_elementor )
  320.                             $navigation_class .= ' et-swiper-elementor-nav';
  321.  
  322.                         if ( $hide_buttons_for == 'desktop' ) {
  323.                             $navigation_class .= ' dt-hide';
  324.                         } elseif ( $hide_buttons_for == 'mobile' ) {
  325.                             $navigation_class .= ' mob-hide';
  326.                         }
  327.  
  328.                         $navigation_left_class  = 'swiper-custom-left' . ' ' . $navigation_class;
  329.                         $navigation_right_class = 'swiper-custom-right' . ' ' . $navigation_class;
  330.  
  331.                         $navigation_left_class .= ' type-' . $navigation_type . ' ' . $navigation_style;
  332.                         $navigation_right_class .= ' type-' . $navigation_type . ' ' . $navigation_style;
  333.                         ?>
  334.                         <div class="swiper-button-prev <?php echo esc_attr( $navigation_left_class ); ?>"></div>
  335.                         <div class="swiper-button-next <?php echo esc_attr( $navigation_right_class ); ?>"></div>
  336.                     <?php } ?>
  337.                 </div>
  338.                 <?php
  339.             }
  340.         }
  341.  
  342.         if ( $link != '' ) { ?>
  343.             <p class="clear">
  344.             <a href="//instagram.com/<?php echo trim( $username ); ?>" rel="me"
  345.                target="<?php echo esc_attr( $target ); ?>">
  346.                 <?php echo $link; ?>
  347.             </a>
  348.             </p><?php
  349.         }
  350.  
  351.         do_action( 'etheme_after_widget', $instance );
  352.  
  353.         if ( $is_preview ) {
  354.             echo '<script>jQuery(document).ready(function(){ etTheme.global_image_lazy(); etTheme.swiperFunc(); }); </script>';
  355.         }
  356.  
  357.         echo $after_widget;
  358.     }
  359.  
  360.     function form( $instance ) {
  361.         $instance = wp_parse_args( (array) $instance, array(
  362.             'title'    => esc_html__( 'Instagram', 'xstore-core' ),
  363.             'username' => '',
  364.             'link'     => esc_html__( 'Follow Us', 'xstore-core' ),
  365.             'number'   => 12,
  366.             'size'     => 'thumbnail',
  367.             'target'   => '_self',
  368.             'user'     => '',
  369.         ) );
  370.         $title    = esc_attr( $instance['title'] );
  371.         $username = esc_attr( $instance['username'] );
  372.         $number   = absint( $instance['number'] );
  373.         $size     = esc_attr( $instance['size'] );
  374.         $columns  = ( ! isset( $instance['columns'] ) ) ? '' : $instance['columns'];
  375.         $target   = esc_attr( $instance['target'] );
  376.         $user     = esc_attr( $instance['user'] );
  377.         $link     = esc_attr( $instance['link'] );
  378.         $spacing  = ( ! isset( $instance['spacing'] ) ) ? '' : esc_attr( $instance['spacing'] );
  379.         $tag_type = ( ! isset( $instance['tag_type'] ) ) ? 'recent_media' : esc_attr( $instance['tag_type'] );
  380.         $ajax = isset( $instance['ajax'] ) ? (bool) $instance['ajax'] : false;
  381. //        $likes = ( ! isset( $instance['like_count'] ) ) ? '' : $instance['like_count'];
  382. //      $comments = ( ! isset( $instance['comments_count'] ) ) ? '' : $instance['comments_count'];
  383.  
  384.         ?>
  385.         <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title', 'xstore-core' ); ?>:
  386.                 <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>"
  387.                        name="<?php echo $this->get_field_name( 'title' ); ?>" type="text"
  388.                        value="<?php echo $title; ?>"/></label></p>
  389.  
  390.         <p>
  391.             <label for="<?php echo $this->get_field_id( 'user' ); ?>"><?php esc_html_e( 'Choose Instagram account', 'xstore-core' ); ?>
  392.                 :</label>
  393.             <select id="<?php echo $this->get_field_id( 'user' ); ?>"
  394.                     name="<?php echo $this->get_field_name( 'user' ); ?>" class="widefat">
  395.                 <option value="" <?php selected( '', $user ); ?>></option>
  396.                 <?php
  397.                 $api_data = get_option( 'etheme_instagram_api_data' );
  398.                 $api_data = json_decode( $api_data, true );
  399.  
  400.  
  401.                 if ( is_array( $api_data ) && count( $api_data ) ) {
  402.                     foreach ( $api_data as $key => $value ) {
  403.                         $value          = json_decode( $value, true );
  404.                         $value_username = __( 'Can not get the username', 'xstore-core' );
  405.  
  406.                         if ( isset( $value['data'] ) && isset( $value['data']['username'] ) ) {
  407.                             $value_username = $value['data']['username'] . ' (old API)';
  408.                         } elseif ( isset( $value['username'] ) ) {
  409.                             $value_username = $value['username'];
  410.                         }
  411.  
  412.                         ?>
  413.                         <option value="<?php echo $key ?>" <?php selected( $key, $user ); ?>><?php esc_html_e( $value_username ); ?></option>
  414.                         <?php
  415.                     }
  416.                 }
  417.                 ?>
  418.             </select>
  419.         </p>
  420.         <p>
  421.             <a href="<?php echo admin_url( 'admin.php?page=et-panel-social' ) ?>"
  422.                target="_blank"><?php esc_html_e( 'Add Instagram account?', 'xstore-core' ); ?></a>
  423.         </p>
  424.  
  425.         <p>
  426.             <label for="<?php echo $this->get_field_id( 'username' ); ?>"><?php esc_html_e( 'Hashtag (Only for Instagram business users) ', 'xstore-core' ); ?>
  427.                 : <input class="widefat" id="<?php echo $this->get_field_id( 'username' ); ?>"
  428.                          name="<?php echo $this->get_field_name( 'username' ); ?>" type="text"
  429.                          value="<?php echo $username; ?>"/></label></p>
  430.  
  431.         <p>
  432.             <label for="<?php echo $this->get_field_id( 'tag_type' ); ?>"><?php esc_html_e( 'Sort by', 'xstore-core' ); ?>
  433.                 :</label>
  434.             <select id="<?php echo $this->get_field_id( 'tag_type' ); ?>"
  435.                     name="<?php echo $this->get_field_name( 'tag_type' ); ?>" class="widefat">
  436.                 <option value="recent_media" <?php selected( 'recent_media', $tag_type ) ?>>Recent media</option>
  437.                 <option value="top_media" <?php selected( 'top_media', $tag_type ) ?>>Top media</option>
  438.             </select>
  439.         </p>
  440.  
  441.         <p>
  442.             <label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php esc_html_e( 'Number of photos', 'xstore-core' ); ?>
  443.                 : <input class="widefat" id="<?php echo $this->get_field_id( 'number' ); ?>"
  444.                          name="<?php echo $this->get_field_name( 'number' ); ?>" type="text"
  445.                          value="<?php echo $number; ?>"/></label></p>
  446.  
  447.         <p><label for="<?php echo $this->get_field_id( 'size' ); ?>"><?php esc_html_e( 'Image size', 'xstore-core' ); ?>
  448.                 :</label>
  449.             <select id="<?php echo $this->get_field_id( 'size' ); ?>"
  450.                     name="<?php echo $this->get_field_name( 'size' ); ?>" class="widefat">
  451.                 <option value="thumbnail" <?php selected( 'thumbnail', $size ) ?>><?php esc_html_e( 'Thumbnail', 'xstore-core' ); ?></option>
  452.                 <option value="medium" <?php selected( 'medium', $size ) ?>><?php esc_html_e( 'Medium', 'xstore-core' ); ?></option>
  453.                 <option value="large" <?php selected( 'large', $size ) ?>><?php esc_html_e( 'Large', 'xstore-core' ); ?></option>
  454.             </select>
  455.         </p>
  456.  
  457.         <p>
  458.             <label for="<?php echo $this->get_field_id( 'target' ); ?>"><?php esc_html_e( 'Open links in', 'xstore-core' ); ?>
  459.                 :</label>
  460.             <select id="<?php echo $this->get_field_id( 'target' ); ?>"
  461.                     name="<?php echo $this->get_field_name( 'target' ); ?>" class="widefat">
  462.                 <option value="_self" <?php selected( '_self', $target ) ?>><?php esc_html_e( 'Current window (_self)', 'xstore-core' ); ?></option>
  463.                 <option value="_blank" <?php selected( '_blank', $target ) ?>><?php esc_html_e( 'New window (_blank)', 'xstore-core' ); ?></option>
  464.             </select>
  465.         </p>
  466.         <p><label for="<?php echo $this->get_field_id( 'columns' ); ?>"><?php esc_html_e( 'Columns', 'xstore-core' ); ?>
  467.                 :</label>
  468.             <select id="<?php echo $this->get_field_id( 'columns' ); ?>"
  469.                     name="<?php echo $this->get_field_name( 'columns' ); ?>" class="widefat">
  470.                 <option value="2" <?php selected( 2, $columns ) ?>>2</option>
  471.                 <option value="3" <?php selected( 3, $columns ) ?>>3</option>
  472.                 <option value="4" <?php selected( 4, $columns ) ?>>4</option>
  473.             </select>
  474.         </p>
  475.         <p><label for="<?php echo $this->get_field_id( 'link' ); ?>"><?php esc_html_e( 'Link text', 'xstore-core' ); ?>:
  476.                 <input class="widefat" id="<?php echo $this->get_field_id( 'link' ); ?>"
  477.                        name="<?php echo $this->get_field_name( 'link' ); ?>" type="text" value="<?php echo $link; ?>"/></label>
  478.         </p>
  479.  
  480.         <p>
  481.             <input type="checkbox" <?php checked( true, $spacing, true ); ?>
  482.                    id="<?php echo $this->get_field_id( 'spacing' ); ?>"
  483.                    name="<?php echo $this->get_field_name( 'spacing' ); ?>">
  484.             <label for="<?php echo $this->get_field_id( 'spacing' ); ?>"><?php esc_html_e( 'Without spacing', 'xstore-core' ); ?></label>
  485.         </p>
  486.         <?php parent::widget_input_checkbox( esc_html__( 'Use ajax preload for this widget', 'xstore-core' ), $this->get_field_id( 'ajax' ), $this->get_field_name( 'ajax' ), checked( $ajax, true, false ), 1 );
  487.  
  488.  
  489.     }
  490.  
  491.     function update( $new_instance, $old_instance ) {
  492.         $instance             = $old_instance;
  493.         $instance['title']    = strip_tags( $new_instance['title'] );
  494.         $instance['username'] = trim( strip_tags( $new_instance['username'] ) );
  495.         $instance['tag_type'] = ( $new_instance['tag_type'] ) ? $new_instance['tag_type'] : 'recent_media';
  496.         $instance['number']   = ! absint( $new_instance['number'] ) ? 12 : $new_instance['number'];
  497.         $instance['columns']  = ! absint( $new_instance['columns'] ) ? 4 : $new_instance['columns'];
  498.         $instance['size']     = ( in_array( $new_instance['size'], array(
  499.             'thumbnail',
  500.             'medium',
  501.             'large',
  502.             'small'
  503.         ) ) ) ? $new_instance['size'] : 'thumbnail';
  504.         $instance['target']   = ( ( in_array( $new_instance['target'], array(
  505.             '_self',
  506.             '_blank'
  507.         ) ) ) ? $new_instance['target'] : '_self' );
  508.         $instance['user']     = ( $new_instance['user'] ) ? $new_instance['user'] : '';
  509.         $instance['link']     = strip_tags( $new_instance['link'] );
  510.         //$instance['slider']   = ( $new_instance['slider'] != '' ) ? true : false;
  511. //      $instance['spacing']  = ( $new_instance['spacing'] != '' ) ? true : false;
  512.         $instance['spacing']  = ( ! empty( $new_instance['spacing'] ) ) ? (int) $new_instance['spacing'] : '';
  513.         $instance['slider']   = ( ! empty( $new_instance['slider'] ) ) ? (int) $new_instance['slider'] : '';
  514.         $instance['ajax'] = ( ! empty( $new_instance['ajax'] ) ) ? (bool) $new_instance['ajax']: '';
  515.  
  516.         return $instance;
  517.     }
  518.  
  519.     function et_get_instagram( $number = '', $tag = false, $last = '', $token = false, $type = 'widget', $tag_type = false ) {
  520.         $count    = $number;
  521.         $api_data = get_option( 'etheme_instagram_api_data' );
  522.         $api_data = json_decode( $api_data, true );
  523.         $username = '';
  524.  
  525.         if ( ! is_array( $api_data ) ) {
  526.             $api_data = array();
  527.         }
  528.  
  529.         if ( ! count( $api_data ) ) {
  530.             return new \WP_Error( 'error', '<div class="woocommerce-info">' . esc_html__( 'To use this element select instagram user', 'xstore-core' ) . '</div>' );
  531.         }
  532.  
  533.         foreach ( $api_data as $key => $value ) {
  534.             $value = json_decode( $value, true );
  535.  
  536.             if ( $key == $token ) {
  537.  
  538.                 $this->api_type( $value );
  539.  
  540.                 if ( isset( $value['token'] ) ) {
  541.                     $token = $value['token'];
  542.                     $this->token = $value['token'];
  543.                 }
  544.  
  545.                 if ( $this->API_type == 'old' ) {
  546.                     $username = $value['data']['username'];
  547.                     $user_id  = '';
  548.  
  549.                 } else {
  550.                     $username          = $value['username'];
  551.                     $user_id           = $value['id'];
  552.                     $this->global_user = $username;
  553.                 }
  554.             }
  555.         }
  556.  
  557.         if ( ! $username ) {
  558.             return new \WP_Error( 'error', '<div class="woocommerce-info">' . esc_html__( 'To use this element select instagram user', 'xstore-core' ) . '</div>' );
  559.         }
  560.  
  561.         if ( $tag ) {
  562.             $instagram = get_transient( 'etheme-instagram-data-tag-' . $tag . '-' . $type );
  563.         } else {
  564.             if ( $username ) {
  565.                 $instagram = get_transient( 'etheme-instagram-data-user-' . $username . '-' . $type );
  566.             } else {
  567.                 return new \WP_Error( 'error', '<div class="woocommerce-info">' . esc_html__( 'To use this element select instagram user', 'xstore-core' ) . '</div>' );
  568.             }
  569.         }
  570.  
  571.         $callback = $instagram;
  572.  
  573.         if ( $last ) {
  574.             $instagram = false;
  575.         }
  576.  
  577.         if ( $instagram === false || isset( $_GET['et_reinit_instagram'] ) ) {
  578.             $api_settings = get_option( 'etheme_instagram_api_settings' );
  579.             $api_settings = json_decode( $api_settings, true );
  580.  
  581.             $insta_time = function_exists( 'etheme_get_option' ) ? etheme_get_option( 'instagram_time' ) : 2000;
  582.  
  583.             switch ( $api_settings['time_type'] ) {
  584.                 case 'min':
  585.                     $insta_time = $api_settings['time'] * MINUTE_IN_SECONDS;
  586.                     break;
  587.  
  588.                 case 'hour':
  589.                     $insta_time = $api_settings['time'] * HOUR_IN_SECONDS;
  590.                     break;
  591.  
  592.                 case 'day':
  593.                     $insta_time = $api_settings['time'] * DAY_IN_SECONDS;
  594.                     break;
  595.                 default:
  596.                     $insta_time = 2 * HOUR_IN_SECONDS;
  597.                     break;
  598.             }
  599.  
  600.             if ( ! $token ) {
  601.                 return new \WP_Error( 'error', esc_html__( 'Error: To use this element enter instagram access token', 'xstore-core' ) );
  602.             }
  603.  
  604.             if ( ! $number ) {
  605.                 $number = '&count=33';
  606.             } else {
  607.                 $number = '&count=' . $number;
  608.             }
  609.  
  610.             if ( $last ) {
  611.                 $last = '&max_id=' . $last;
  612.             }
  613.  
  614.             if ( $this->API_type == 'old' ) {
  615.                 $url = $this->old_api_url( $token, $last, $number, $tag );
  616.             } else {
  617.                 $url = $this->new_api_url( $number, $user_id, $token, $tag, $tag_type );
  618.             }
  619.  
  620.  
  621.             if ( is_wp_error( $url ) ) {
  622.                 return $url;
  623.             }
  624.  
  625.             // echo $url;
  626.             // die();
  627.  
  628.             // if (!empty( $tag )) {
  629.             if ( ! empty( $tag ) && ( $this->API_type == 'old' || $this->force_tag ) ) {
  630.                 $callback = $this->old_api_url( $token, $last, $number, $tag );
  631.  
  632.             } else {
  633.                 $callback = $this->instagram_callback( $url, $tag );
  634.             }
  635.  
  636.             if ( $tag ) {
  637.                 set_transient( 'etheme-instagram-data-tag-' . $tag . '-' . $type, $callback, $insta_time );
  638.             } else {
  639.                 set_transient( 'etheme-instagram-data-user-' . $username . '-' . $type, $callback, $insta_time );
  640.             }
  641.         }
  642.  
  643.         return $callback;
  644.     }
  645.  
  646.     function api_type( $data ) {
  647.         if ( isset( $data['data']['username'] ) ) {
  648.             $this->API_type = 'old';
  649.         } else {
  650.             $this->API_type     = 'personal';
  651.             $this->account_type = $data['account_type'];
  652.         }
  653.     }
  654.  
  655.     private function new_api_url( $number, $user_id, $token, $tag, $tag_type ) {
  656.         $number = str_replace( 'count', 'limit', $number );
  657.  
  658.         if ( $this->account_type == 'PERSONAL' ) {
  659.             $url = 'https://graph.instagram.com/' . $user_id . '/media?fields=id,media_type,media_url,permalink,thumbnail_url,caption&access_token=' . $token . $number;
  660.  
  661.             $callback = $personal_data_responce = wp_remote_get( $url );
  662.             $callback = wp_remote_retrieve_body( $callback );
  663.             $callback = json_decode( $callback, true );
  664.  
  665.             if ( 200 != wp_remote_retrieve_response_code( $personal_data_responce ) && ! isset( $callback['data'] ) ) {
  666.  
  667.                 if ( isset( $tag_data['error'] ) && isset( $tag_data['error']['message'] ) && isset( $tag_data['error']['error_user_title'] ) ) {
  668.                     return new \WP_Error( 'error', 'Error: ' . $tag_data['error']['message'] . '. ' . $tag_data['error']['error_user_title'] );
  669.                 } else {
  670.                     return new \WP_Error( 'error', esc_html__( 'Error: There is no media to show', 'xstore-core' ) );
  671.                 }
  672.             }
  673.         } else {
  674.  
  675.             if ( ! empty( $tag ) ) {
  676.                 //hashtag ID by user ID
  677.                 $tag_url  = 'https://graph.facebook.com/v6.0/ig_hashtag_search?user_id=' . $user_id . '&q=' . $tag . '&access_token=' . $token;
  678.                 $tag_data = $tag_data_responce = wp_remote_get( $tag_url );
  679.                 $tag_data = wp_remote_retrieve_body( $tag_data );
  680.                 $tag_data = json_decode( $tag_data, true );
  681.  
  682.  
  683.                 if ( 200 != wp_remote_retrieve_response_code( $tag_data_responce ) ) {
  684.  
  685.                     if ( isset( $tag_data['error'] ) && isset( $tag_data['error']['message'] ) && isset( $tag_data['error']['error_user_title'] ) ) {
  686.                         return new \WP_Error( 'error', 'Error: ' . $tag_data['error']['message'] . '. ' . $tag_data['error']['error_user_title'] );
  687.                     } elseif (
  688.                         isset( $tag_data['error'] )
  689.                         && isset( $tag_data['error']['message'] )
  690.                         && isset( $tag_data['error']['error_subcode'] )
  691.                         && isset( $tag_data['error']['code'] )
  692.                         && $tag_data['error']['error_subcode'] == '460'
  693.                         && $tag_data['error']['code'] == 190
  694.                     ){
  695.                         return new \WP_Error( 'error', 'Warning: Your Instagram token expired. Go to the XStore Authorization section, remove the old one and generate the new one.' );
  696.                     } else {
  697.                         return new \WP_Error( 'error', esc_html__( 'Error: Instagram "hashtag search" did not return a 200.', 'xstore-core' ) );
  698.                     }
  699.                 }
  700.  
  701.                 $tag_type = ( $tag_type ) ? $tag_type : 'recent_media';
  702.  
  703.                 $url = 'https://graph.facebook.com/' . $tag_data['data'][0]['id'] . '/' . $tag_type . '?user_id=' . $user_id . '&fields=id,media_type,comments_count,like_count,media_url,permalink&access_token=' . $token . $number;
  704.             } else {
  705.                 $url = 'https://graph.facebook.com/v6.0/' . $user_id . '/media?fields=id,media_type,media_url,like_count,permalink,comments_count,thumbnail_url&access_token=' . $token . $number;
  706.             }
  707.  
  708.             $callback = wp_remote_get( $url );
  709.  
  710.             if( is_wp_error( $callback ) ) {
  711.                 return new \WP_Error( 'error', $callback->get_error_message() );
  712.             }
  713.  
  714.             if ( ! isset($callback['response']) || ! isset($callback['response']['code']) || $callback['response']['code'] != 200 ) {
  715.                 // echo "sems you connected your business acc";
  716.                 $url = 'https://graph.instagram.com/' . $user_id . '/media?fields=id,media_type,media_url,permalink,thumbnail_url,caption&access_token=' . $token . $number;
  717.             }
  718.         }
  719.  
  720.         return $url;
  721.     }
  722.  
  723.     private function old_api_url( $token, $last, $number, $tag ) {
  724.         return new \WP_Error( 'error', esc_html__( 'Error: Old API not supported', 'xstore-core' ) );
  725.     }
  726.  
  727.     private function instagram_callback( $url, $tag ) {
  728.         $callback = wp_remote_get( $url );
  729.  
  730.         if ( is_wp_error( $callback ) || ! isset( $callback['response'] ) || ! is_array( $callback ) ) {
  731.             return new \WP_Error( 'error', esc_html__( 'Error: Can not get response', 'xstore-core' ) );
  732.         } elseif ( ! isset( $callback['response']['code'] ) ) {
  733.             return new \WP_Error( 'error', esc_html__( 'Error: Can not get response code', 'xstore-core' ) );
  734.         } elseif ( $callback['response']['code'] != 200 ) {
  735.  
  736.             $callback = wp_remote_retrieve_body( $callback );
  737.             $callback = json_decode( $callback );
  738.  
  739.             if ( isset($callback->meta->error_message) ) {
  740.                 return new \WP_Error( 'error', esc_html__( 'Error: ', 'xstore-core' ) . $callback->meta->error_message );
  741.             }elseif(isset($callback->error->message)){
  742.                 return new \WP_Error( 'error', esc_html__( 'Error: ', 'xstore-core' ) . $callback->error->message );
  743.             } else {
  744.                 return new \WP_Error( 'error', esc_html__( 'Error: Undefined error', 'xstore-core' ));
  745.             }
  746.         }
  747.  
  748.         $callback = wp_remote_retrieve_body( $callback );
  749.         $callback = json_decode( $callback, true );
  750.  
  751.  
  752.         if (apply_filters('et_force_instagram_meta', false)){
  753.             foreach ( $callback['data'] as $key => $value ) {
  754.                  $meta = $this->get_meta_count($value['permalink']);
  755.  
  756.                 $callback['data'][$key]['like_count'] = $meta['likes'];
  757.                 $callback['data'][$key]['comments_count'] = $meta['comments'];
  758.             }
  759.         }
  760.  
  761.         if ( empty( $callback ) ) {
  762.             return new \WP_Error( 'error', esc_html__( 'Error: instagram did not return any dada', 'xstore-core' ) );
  763.         }
  764.  
  765.         $api_settings = get_option( 'etheme_instagram_api_settings' );
  766.         $api_settings = json_decode( $api_settings, true );
  767.  
  768.         if ( $tag
  769.             || ( isset( $api_settings['count_albums_photo'] ) && $api_settings['count_albums_photo'] )
  770.             || ( isset( $api_settings['escape_albums'] ) && $api_settings['escape_albums'] )
  771.         ) {
  772.             foreach ( $callback['data'] as $key => $value ) {
  773.                 if ( $value['media_type'] === 'CAROUSEL_ALBUM' || $value['media_type'] === 'VIDEO' ) {
  774.  
  775.                     if ( isset( $api_settings['escape_albums'] ) && $api_settings['escape_albums'] ) {
  776.                         unset( $callback['data'][ $key ] );
  777.                     } elseif($tag) {
  778.  
  779.                         $image_url = 'https://graph.facebook.com/'.$value['id'].'/?fields=id,media_type,media_url&access_token=' . $this->token;
  780.                         $image_callback = wp_remote_get( $image_url );
  781.  
  782.                         if(
  783.                             is_wp_error( $image_callback )
  784.                             || ! isset( $image_callback['response'] )
  785.                             || ! is_array( $image_callback )
  786.                             || ! isset( $image_callback['response']['code'] )
  787.                             || $image_callback['response']['code'] != 200
  788.                         ){
  789.                             $callback['data'][ $key ]['media_url'] = ET_CORE_URL . 'app/controllers/images/instagram/placeholder.jpg';
  790.                         } else {
  791.                             $image_callback = wp_remote_retrieve_body( $image_callback );
  792.                             $image_callback = json_decode( $image_callback, true );
  793.  
  794.                             if( isset( $image_callback['media_url'] ) ){
  795.                                 $callback['data'][ $key ]['media_url'] = $image_callback['media_url'];
  796.                             } else {
  797.                                 $callback['data'][ $key ]['media_url'] = ET_CORE_URL . 'app/controllers/images/instagram/placeholder.jpg';
  798.                             }
  799.                         }
  800.                     }
  801.  
  802.                     if (
  803.                         isset( $api_settings['count_albums_photo'] )
  804.                         && $api_settings['count_albums_photo']
  805.                         && $value['media_type'] === 'CAROUSEL_ALBUM'
  806.                         && (
  807.                             !isset( $api_settings['escape_albums'] )
  808.                             || ! $api_settings['escape_albums']
  809.                         )
  810.                     ){
  811.                         $album_count = $this->get_album_count($value);
  812.                         if ($album_count){
  813.                             $callback['data'][ $key ]['album_count'] = $album_count;
  814.                         }
  815.                     }
  816.                 }
  817.             }
  818.         }
  819.         return $callback;
  820.     }
  821.  
  822.     public function get_album_count($album){
  823.         $responce = wp_remote_get($album['permalink']);
  824.  
  825.         if (wp_remote_retrieve_response_code($responce) == 200){
  826.             $responce = wp_remote_retrieve_body($responce);
  827.             if ($responce){
  828.                 $script = $this->get_between_strings($responce);
  829.                 if ($script){
  830.                     $script = json_decode($script, true);
  831.                     if (is_array($script) && isset($script['image']) && count($script['image'])){
  832.                         return count($script['image']);
  833.                     }
  834.                 }
  835.             }
  836.         }
  837.         return false;
  838.     }
  839.  
  840.     public function get_between_strings($str){
  841.         $matches = array();
  842.         preg_match("/<script[^>]*>(.*?)<\\/script>/si", $str, $matches);
  843.         return $matches[1];
  844.     }
  845.  
  846.     public function get_meta_count($url){
  847.         $data = wp_remote_get( $url );
  848.         $data = wp_remote_retrieve_body( $data );
  849.  
  850.         $result = array();
  851.  
  852.         $dom = new \DOMDocument;
  853.         libxml_use_internal_errors(true);
  854.         $dom->loadHTML($data);
  855.         libxml_clear_errors();
  856.  
  857.         $metaTags = $dom->getElementsByTagName('meta');
  858.         $descriptionContent = '';
  859.  
  860.         foreach ($metaTags as $meta) {
  861.             if ($meta->getAttribute('property') === 'og:description') {
  862.                 $descriptionContent = $meta->getAttribute('content');
  863.                 break;
  864.             }
  865.         }
  866.  
  867.         preg_match('/(\d+)\s+likes,\s+(\d+)\s+comments/', $descriptionContent, $matches);
  868.  
  869.         $result = [
  870.             'likes' => isset($matches[1]) ? (int)$matches[1] : 0,
  871.             'comments' => isset($matches[2]) ? (int)$matches[2] : 0,
  872.         ];
  873.  
  874.         return $result;
  875.     }
  876.  
  877.     public function is_hide_by_count($hide_empty_meta, $qty){
  878.         if ($hide_empty_meta){
  879.             if ($qty<=0){
  880.                 return false;
  881.             }
  882.         }
  883.         return true;
  884.     }
  885. }
Advertisement
Add Comment
Please, Sign In to add comment