set_settings(); $widget_ops = [ 'classname' => 'tweets', 'description' => __( 'Adds your X timeline', 'Avada' ), ]; $control_ops = [ 'id_base' => 'tweets-widget', ]; parent::__construct( 'tweets-widget', __( 'Avada: X', 'Avada' ), $widget_ops, $control_ops ); } /** * Set settings for this widget, * with a fallback to some sane defaults if no customized settings are saved in the database. * * @access public * @since 4.0 * @return void */ public function set_settings() { $fusion_settings = class_exists( 'Fusion_Settings' ) ? awb_get_fusion_settings() : false; if ( $fusion_settings ) { $this->settings = [ 'link_color' => $fusion_settings->get( 'link_color' ), 'timeline_color' => $fusion_settings->get( 'timeline_color' ), ]; } else { $this->settings = [ 'link_color' => '#333333', 'timeline_color' => '#ebeaea', ]; } } /** * Echoes the widget content. * * @access public * @param array $args Display arguments including 'before_title', 'after_title', * 'before_widget', and 'after_widget'. * @param array $instance The settings for the particular instance of the widget. */ public function widget( $args, $instance ) { extract( $args ); // phpcs:ignore WordPress.PHP.DontExtract.extract_extract $title = apply_filters( 'widget_title', isset( $instance['title'] ) ? $instance['title'] : '', $instance, $this->id_base ); $twitter_id = isset( $instance['twitter_id'] ) ? $instance['twitter_id'] : ''; $icon_color = isset( $instance['icon_color'] ) ? $instance['icon_color'] : ''; $consumer_key = isset( $instance['consumer_key'] ) ? $instance['consumer_key'] : ''; $consumer_secret = isset( $instance['consumer_secret'] ) ? $instance['consumer_secret'] : ''; $access_token = isset( $instance['access_token'] ) ? $instance['access_token'] : ''; $access_token_secret = isset( $instance['access_token_secret'] ) ? $instance['access_token_secret'] : ''; $widget_id = isset( $instance['widget_id'] ) ? $instance['widget_id'] : ''; $widget_type = isset( $instance['widget_type'] ) ? $instance['widget_type'] : 'avada_style'; $width = isset( $instance['width'] ) ? $instance['width'] : '400'; $height = isset( $instance['height'] ) ? $instance['height'] : '400'; $theme = isset( $instance['theme'] ) ? $instance['theme'] : 'light'; $link_color = isset( $instance['link_color'] ) ? $instance['link_color'] : $this->settings['link_color']; $border_color = isset( $instance['border_color'] ) ? $instance['border_color'] : $this->settings['timeline_color']; $show_header = isset( $instance['show_header'] ) ? $instance['show_header'] : 1; $show_footer = isset( $instance['show_footer'] ) ? $instance['show_footer'] : 1; $show_borders = isset( $instance['show_borders'] ) ? $instance['show_borders'] : 1; $transparent_bg = isset( $instance['transparent_bg'] ) ? $instance['transparent_bg'] : 0; $chrome = $this->get_chrome( $instance ); echo $before_widget; // phpcs:ignore WordPress.Security.EscapeOutput if ( $title ) { echo $before_title . $title . $after_title; // phpcs:ignore WordPress.Security.EscapeOutput } if ( 'twitter_style' === $widget_type || ( 'twitter_preconfig_style' === $widget_type && $widget_id ) ) { $widget_params = [ 'title' => $title, 'twitter_id' => $twitter_id, 'widget_id' => $widget_id, 'widget_type' => $widget_type, 'width' => $width, 'height' => $height, 'theme' => $theme, 'link_color' => $link_color, 'border_color' => $border_color, 'show_header' => $show_header, 'show_footer' => $show_footer, 'show_borders' => $show_borders, 'transparent_bg' => $transparent_bg, 'chrome' => $chrome, ]; $this->render_new_widget( $widget_params ); } else { if ( $twitter_id && $consumer_key && $consumer_secret && $access_token && $access_token_secret ) { $widget_id = $args['widget_id']; $widget_params = [ 'title' => $title, 'twitter_id' => $twitter_id, 'widget_id' => $widget_id, 'icon_color' => $icon_color, 'consumer_key' => $consumer_key, 'consumer_secret' => $consumer_secret, 'access_token' => $access_token, 'access_token_secret' => $access_token_secret, ]; $this->render_old_widget( $widget_params ); } } echo $after_widget; // phpcs:ignore WordPress.Security.EscapeOutput } /** * Updates a particular instance of a widget. * * This function should check that `$new_instance` is set correctly. The newly-calculated * value of `$instance` should be returned. If false is returned, the instance won't be * saved/updated. * * @access public * @param array $new_instance New settings for this instance as input by the user via * WP_Widget::form(). * @param array $old_instance Old settings for this instance. * @return array Settings to save or bool false to cancel saving. */ public function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance['title'] = strip_tags( $new_instance['title'] ); // phpcs:ignore WordPress.WP.AlternativeFunctions $instance['twitter_id'] = $new_instance['twitter_id']; $instance['icon_color'] = $new_instance['icon_color']; $instance['consumer_key'] = $new_instance['consumer_key']; $instance['consumer_secret'] = $new_instance['consumer_secret']; $instance['access_token'] = $new_instance['access_token']; $instance['access_token_secret'] = $new_instance['access_token_secret']; $instance['widget_id'] = $new_instance['widget_id']; $instance['widget_type'] = $new_instance['widget_type']; $instance['width'] = $new_instance['width']; $instance['height'] = $new_instance['height']; $instance['theme'] = $new_instance['theme']; $instance['link_color'] = $new_instance['link_color']; $instance['border_color'] = $new_instance['border_color']; $instance['show_header'] = ! empty( $new_instance['show_header'] ) ? $new_instance['show_header'] : 'off'; $instance['show_footer'] = ! empty( $new_instance['show_footer'] ) ? $new_instance['show_footer'] : 'off'; $instance['show_borders'] = ! empty( $new_instance['show_borders'] ) ? $new_instance['show_borders'] : 'off'; $instance['transparent_bg'] = ! empty( $new_instance['transparent_bg'] ) ? $new_instance['transparent_bg'] : 'off'; return $instance; } /** * Outputs the settings update form. * * @access public * @param array $instance Current settings. * @return void */ public function form( $instance ) { $defaults = [ 'title' => __( 'Recent Tweets', 'Avada' ), 'twitter_id' => '', 'icon_color' => '', 'consumer_key' => '', 'consumer_secret' => '', 'access_token' => '', 'access_token_secret' => '', 'widget_id' => '', 'widget_type' => 'avada_style', 'width' => '', 'height' => '', 'theme' => 'light', 'link_color' => $this->settings['link_color'], 'border_color' => $this->settings['timeline_color'], 'show_header' => 'on', 'show_footer' => 'on', 'show_borders' => 'on', 'transparent_bg' => 'off', ]; $instance = wp_parse_args( (array) $instance, $defaults ); $twitter_doc_url = 'https://avada.com/documentation/twitter-widget/'; ?>

' . esc_attr__( 'How to setup the Avada twitter widget.', 'Avada' ) . '' ); ?>

id="get_field_id( 'show_header' ) ); ?>" name="get_field_name( 'show_header' ) ); ?>" />

id="get_field_id( 'show_footer' ) ); ?>" name="get_field_name( 'show_footer' ) ); ?>" />

id="get_field_id( 'show_borders' ) ); ?>" name="get_field_name( 'show_borders' ) ); ?>" />

id="get_field_id( 'transparent_bg' ) ); ?>" name="get_field_name( 'transparent_bg' ) ); ?>" />

get_preview_status() ) { $builder_status = true; } } ?> settings->get( 'privacy_embeds' ) && ! Avada()->privacy_embeds->get_consent( 'twitter' ); ?>
privacy_embeds->script_placeholder( 'twitter' ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
encode_input( $credentials ); // Http post arguments. $args = [ 'method' => 'POST', 'httpversion' => '1.1', 'blocking' => true, 'headers' => [ 'Authorization' => 'Basic ' . $to_send, 'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8', ], 'body' => [ 'grant_type' => 'client_credentials', ], ]; add_filter( 'https_ssl_verify', '__return_false' ); $response = wp_remote_post( 'https://api.twitter.com/oauth2/token', $args ); $keys = json_decode( wp_remote_retrieve_body( $response ) ); if ( $keys && isset( $keys->access_token ) ) { // Saving token to wp_options table. update_option( 'cfTwitterToken_' . $widget_id, $keys->access_token ); $token = $keys->access_token; } } // We have bearer token wether we obtained it from API or from options. $args = [ 'httpversion' => '1.1', 'blocking' => true, 'headers' => [ 'Authorization' => "Bearer $token", ], ]; add_filter( 'https_ssl_verify', '__return_false' ); $api_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=' . $twitter_id; $response = wp_remote_get( $api_url, $args ); $tweets_body = wp_remote_retrieve_body( $response ); set_site_transient( $consumer_key, $tweets_body, 960 ); } $tweets = false; if ( ! empty( $tweets_body ) ) { $tweets = json_decode( $tweets_body, true ); if ( ! is_array( $tweets ) ) { $tweets = false; } } if ( $tweets && is_array( $tweets ) ) { if ( '' !== $icon_color ) { echo ''; } ?>
  • tweet_get_html( $tweet ); if ( ! $latest_tweet ) { continue; } echo $latest_tweet; // phpcs:ignore WordPress.Security.EscapeOutput ?>

    ago( $twitter_time ); ?>
' . $e['display_url'] . ''; $entities[] = $temp; } } if ( $users && is_array( $tweet['entities']['user_mentions'] ) ) { foreach ( $tweet['entities']['user_mentions'] as $e ) { $temp['start'] = $e['indices'][0]; $temp['end'] = $e['indices'][1]; $temp['replacement'] = '@' . $e['screen_name'] . ''; $entities[] = $temp; } } if ( $hashtags && is_array( $tweet['entities']['hashtags'] ) ) { foreach ( $tweet['entities']['hashtags'] as $e ) { $temp['start'] = $e['indices'][0]; $temp['end'] = $e['indices'][1]; $temp['replacement'] = '#' . $e['text'] . ''; $entities[] = $temp; } } if ( $media && array_key_exists( 'media', $tweet['entities'] ) ) { foreach ( $tweet['entities']['media'] as $e ) { $temp['start'] = $e['indices'][0]; $temp['end'] = $e['indices'][1]; $temp['replacement'] = '' . $e['display_url'] . ''; $entities[] = $temp; } } usort( $entities, [ $this, 'sort_tweets' ] ); foreach ( $entities as $item ) { $return = $this->mb_substr_replace( $return, $item['replacement'], $item['start'], $item['end'] - $item['start'] ); } return $return; } /** * The PHP substr_replace equivalent for multibyte encoded strings. * * @param string $string The string in which replacement should take place. * @param string $replacement The replacement string. * @param int $start The index where the replacement should start. * @param int $length The length of $string that should be replaced. * @return array|string The correctly replaced string. When the result is an array, it runs recursively. */ public function mb_substr_replace( $string, $replacement, $start, $length = null ) { if ( is_array( $string ) ) { $num = count( $string ); // Calculate $replacement. $replacement = is_array( $replacement ) ? array_slice( $replacement, 0, $num ) : array_pad( [ $replacement ], $num, $replacement ); // Calculate $start. if ( is_array( $start ) ) { $start = array_slice( $start, 0, $num ); foreach ( $start as $key => $value ) { $start[ $key ] = is_int( $value ) ? $value : 0; } } else { $start = array_pad( [ $start ], $num, $start ); } // Calculate $length. if ( ! isset( $length ) ) { $length = array_fill( 0, $num, 0 ); } elseif ( is_array( $length ) ) { $length = array_slice( $length, 0, $num ); foreach ( $length as $key => $value ) { $length[ $key ] = isset( $value ) ? ( is_int( $value ) ? $value : $num ) : 0; } } else { $length = array_pad( [ $length ], $num, $length ); } // Recursive call. return array_map( __FUNCTION__, $string, $replacement, $start, $length ); } preg_match_all( '/./us', (string) $string, $smatches ); preg_match_all( '/./us', (string) $replacement, $rmatches ); if ( null === $length ) { $length = mb_strlen( $string ); } array_splice( $smatches[0], $start, $length, $rmatches[0] ); return join( $smatches[0] ); } /** * Compare the start indices of two twitter entities. * * @param array $a A twitter entity. * @param array $b A twitter entity. * @return int The difference of the start indices. */ public function sort_tweets( $a, $b ) { return ( $b['start'] - $a['start'] ); } /** * Function to display the correct time format for each tweet. * * @param int $time A timestamp for the tweet publishing date. * @return string The formatted date for the twwet's publishing date. */ public function ago( $time ) { /* translators: time difference. */ return sprintf( _x( '%s ago', '%s = human-readable time difference', 'Avada' ), human_time_diff( $time, current_time( 'timestamp' ) ) ); // phpcs:ignore WordPress.DateTime } } /* Omit closing PHP tag to avoid "Headers already sent" issues. */