forms
5 days ago
wpr-ajax-search.php
5 days ago
wpr-filter-grid-media.php
5 days ago
wpr-form-handlers.php
5 days ago
wpr-grid-helpers.php
5 days ago
wpr-load-more-instagram-posts.php
5 days ago
wpr-load-more-tweets.php
5 days ago
wpr-post-likes.php
5 days ago
wpr-woo-grid-helpers.php
5 days ago
wpr-load-more-tweets.php
476 lines
| 1 | <?php |
| 2 | namespace WprAddons\Classes\Modules; |
| 3 | |
| 4 | use Elementor\Utils; |
| 5 | use Elementor\Group_Control_Image_Size; |
| 6 | use WprAddons\Classes\Utilities; |
| 7 | |
| 8 | |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit; // Exit if accessed directly. |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * WPR_Load_More_Tweets setup |
| 15 | * |
| 16 | * @since 3.4.6 |
| 17 | */ |
| 18 | |
| 19 | class WPR_Load_More_Tweets { |
| 20 | |
| 21 | public function __construct() { |
| 22 | add_action('wp_ajax_wpr_load_more_tweets', [$this, 'wpr_load_more_tweets_function']); |
| 23 | add_action('wp_ajax_nopriv_wpr_load_more_tweets', [$this, 'wpr_load_more_tweets_function']); |
| 24 | } |
| 25 | |
| 26 | // Render User Name |
| 27 | public function render_post_username( $settings, $class, $item ) { |
| 28 | |
| 29 | $tags_whitelist = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'span', 'p']; |
| 30 | $element_username_tag = Utilities::validate_html_tags_wl( $settings['element_username_tag'], 'h2', $tags_whitelist ); |
| 31 | |
| 32 | echo '<'. esc_attr($element_username_tag) .' class="'. esc_attr($class) .'">'; |
| 33 | echo '<div class="inner-block">'; |
| 34 | echo '<a>'. $item['user']['name'] .'</a>'; |
| 35 | echo '</div>'; |
| 36 | echo '</'. esc_attr($element_username_tag) .'>'; |
| 37 | } |
| 38 | |
| 39 | // Render User Account |
| 40 | public function render_post_user_profile_name( $settings, $class, $item ) { |
| 41 | echo '<div class="'. esc_attr($class) .'">'; |
| 42 | echo '<div class="inner-block">'; |
| 43 | echo '<small>'; |
| 44 | echo '<a href="https://twitter.com/' . $item['user']['screen_name'] .'">@'. $item['user']['screen_name'] .'</a>'; |
| 45 | echo '</small>'; |
| 46 | echo '</div>'; |
| 47 | echo '</div>'; |
| 48 | } |
| 49 | |
| 50 | // Render User Account |
| 51 | public function render_post_user_profile_picture( $settings, $class, $item ) { |
| 52 | echo '<div class="'. esc_attr($class) .'">'; |
| 53 | echo '<div class="inner-block">'; |
| 54 | ?> |
| 55 | <figure> |
| 56 | <img src="<?php echo $item['user']['profile_image_url'] ?>" alt="Image"> |
| 57 | </figure> |
| 58 | <?php |
| 59 | echo '</div>'; |
| 60 | echo '</div>'; |
| 61 | } |
| 62 | |
| 63 | public function render_post_caption($settings, $class, $item) { |
| 64 | |
| 65 | if ( !isset($item['full_text']) || '' === $item['full_text'] ) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | echo '<div class="'. esc_attr($class) .'">'; |
| 70 | echo '<div class="inner-block">'; |
| 71 | echo '<p>'; |
| 72 | $string = preg_replace("~[[:alpha:]]+://[^<p>[:space:]]+[[:alnum:]/]~", "<a href=\"\\0\">\\0</a>", $item['full_text']); |
| 73 | $new_string = preg_replace('/\#([a-z0-9]+)/i', '<a href="https://twitter.com/hashtag/$1?src=hashtag_click">#$1</a>', $string); |
| 74 | echo preg_replace('/\@([a-z0-9]+)/i', '<a href="https://twitter.com/$1">@$1</a>' ,$new_string); |
| 75 | echo '</p>'; |
| 76 | echo '</div>'; |
| 77 | echo '</div>'; |
| 78 | } |
| 79 | |
| 80 | public function render_post_date($settings, $class, $item) { |
| 81 | echo '<div class="'. esc_attr($class) .'">'; |
| 82 | echo '<div class="inner-block">'; |
| 83 | echo '<span>'; |
| 84 | |
| 85 | // Text: Before |
| 86 | // if ( 'before' === $settings['element_extra_text_pos'] ) { |
| 87 | // echo '<span class="wpr-twitter-feed-extra-text-left">'. esc_html( $settings['element_extra_text'] ) .'</span>'; |
| 88 | // } |
| 89 | // Icon: Before |
| 90 | // if ( 'before' === $settings['element_extra_icon_pos'] ) { |
| 91 | // echo '<i class="wpr-twitter-feed-extra-icon-left '. esc_attr( $settings['element_extra_icon']['value'] ) .'"></i>'; |
| 92 | // } |
| 93 | |
| 94 | // echo wp_date(get_option( 'date_format' ), strtotime($item['created_at'])) |
| 95 | echo human_time_diff(strtotime($item['created_at'])) .' '. esc_html__('ago', 'wpr-addons'); |
| 96 | |
| 97 | // Icon: After |
| 98 | // if ( 'after' === $settings['element_extra_icon_pos'] ) { |
| 99 | // echo '<i class="wpr-twitter-feed-extra-icon-right '. esc_attr( $settings['element_extra_icon']['value'] ) .'"></i>'; |
| 100 | // } |
| 101 | // Text: After |
| 102 | // if ( 'after' === $settings['element_extra_text_pos'] ) { |
| 103 | // echo '<span class="wpr-twitter-feed-extra-text-right">'. esc_html( $settings['element_extra_text'] ) .'</span>'; |
| 104 | // } |
| 105 | |
| 106 | echo '</span>'; |
| 107 | echo '</div>'; |
| 108 | echo '</div>'; |
| 109 | } |
| 110 | |
| 111 | public function render_post_lightbox( $settings, $class, $item ) { |
| 112 | echo '<div class="'. esc_attr($class) .'">'; |
| 113 | echo '<div class="inner-block">'; |
| 114 | $lightbox_source = $item['']; |
| 115 | |
| 116 | // Lightbox Button |
| 117 | echo '<span data-src="'. esc_url( $lightbox_source ) .'">'; |
| 118 | |
| 119 | // Text: Before |
| 120 | if ( 'before' === $settings['element_extra_text_pos'] ) { |
| 121 | echo '<span class="wpr-twitter-feed-extra-text-left">'. esc_html( $settings['element_extra_text'] ) .'</span>'; |
| 122 | } |
| 123 | |
| 124 | // Lightbox Icon |
| 125 | echo '<i class="'. esc_attr( $settings['element_extra_icon']['value'] ) .'"></i>'; |
| 126 | |
| 127 | // Text: After |
| 128 | if ( 'after' === $settings['element_extra_text_pos'] ) { |
| 129 | echo '<span class="wpr-twitter-feed-extra-text-right">'. esc_html( $settings['element_extra_text'] ) .'</span>'; |
| 130 | } |
| 131 | |
| 132 | echo '</span>'; |
| 133 | |
| 134 | // Media Overlay |
| 135 | if ( 'yes' === $settings['element_lightbox_overlay'] ) { |
| 136 | echo '<div class="wpr-twitter-feed-lightbox-overlay"></div>'; |
| 137 | } |
| 138 | echo '</div>'; |
| 139 | echo '</div>'; |
| 140 | } |
| 141 | |
| 142 | public function render_post_likes($settings, $class, $item) { |
| 143 | |
| 144 | echo '<div class="'. esc_attr($class) .'">'; |
| 145 | echo '<div class="inner-block">'; |
| 146 | |
| 147 | ?> |
| 148 | <a href="https://twitter.com/intent/like?tweet_id=<?php echo $item['id'] ?>&related=<?php echo $item['user']['screen_name'] ?>" target="_blank" title="Likes"> |
| 149 | <span class=""><i class="fas fa-heart"></i></span> |
| 150 | <span class="wpr-tweet-likes"> |
| 151 | <?php echo $item['favorite_count'] ?> |
| 152 | </span> |
| 153 | </a> |
| 154 | <?php |
| 155 | |
| 156 | echo '</div>'; |
| 157 | echo '</div>'; |
| 158 | } |
| 159 | |
| 160 | public function render_post_comment($settings, $class, $item) { |
| 161 | |
| 162 | echo '<div class="'. esc_attr($class) .'">'; |
| 163 | echo '<div class="inner-block">'; |
| 164 | |
| 165 | ?> |
| 166 | <a href="https://twitter.com/intent/tweet?in_reply_to=<?php echo $item['id'] ?>&related=<?php echo $item['user']['screen_name'] ?>" target="_blank" title="Comments"> |
| 167 | <span class=""><i class="fas fa-comment"></i></span> |
| 168 | </a> |
| 169 | <?php |
| 170 | |
| 171 | echo '</div>'; |
| 172 | echo '</div>'; |
| 173 | } |
| 174 | |
| 175 | public function render_post_retweets($settings, $class, $item) { |
| 176 | |
| 177 | echo '<div class="'. esc_attr($class) .'">'; |
| 178 | echo '<div class="inner-block">'; |
| 179 | |
| 180 | ?> |
| 181 | <a href="https://twitter.com/intent/retweet?tweet_id=<?php echo $item['id'] ?>&related=<?php echo $item['user']['screen_name'] ?>" target="_blank" title="Retweets"> |
| 182 | <span class=""><i class="fa fa-retweet"></i></span> |
| 183 | <span class="wpr-retweets"> |
| 184 | <?php echo $item['retweet_count'] ?> |
| 185 | </span> |
| 186 | </a> |
| 187 | <?php |
| 188 | |
| 189 | echo '</div>'; |
| 190 | echo '</div>'; |
| 191 | } |
| 192 | |
| 193 | // Render Post Element Separator |
| 194 | public function render_post_element_separator( $settings, $class ) { |
| 195 | echo '<div class="wpr-twitter-feed-sep-style-1 '. esc_attr($class) .'">'; |
| 196 | echo '<div class="inner-block"><span></span></div>'; |
| 197 | echo '</div>'; |
| 198 | } |
| 199 | |
| 200 | public function render_post_media($settings, $class, $item) { |
| 201 | |
| 202 | if ( isset($item['extended_entities']) && null !== $item['extended_entities'] ) { |
| 203 | if ( $item['extended_entities']['media'] ) { |
| 204 | $media = $item['extended_entities']['media']; |
| 205 | } else if ( isset( $item['retweeted_status']['entities']['media'] ) ) { |
| 206 | $media = $item['retweeted_status']['entities']['media']; |
| 207 | } else if ( isset( $item['quoted_status']['entities']['media'] ) ) { |
| 208 | $media = $item['quoted_status']['entities']['media']; |
| 209 | } else { |
| 210 | $media = []; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | if ( !empty($media) ) { |
| 215 | echo '<div class="'. esc_attr($class) .'">'; |
| 216 | echo '<div class="inner-block">'; |
| 217 | // && $media[0]['type'] == 'photo' |
| 218 | echo (isset( $media[0] )) ? '<img class="wpr-twit-image" src="' . $media[0]['media_url_https'] . '">' : ''; |
| 219 | echo '</div>'; |
| 220 | echo '</div>'; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | // Get Elements |
| 225 | public function get_elements( $type, $settings, $class, $item ) { |
| 226 | if ( 'pro-lk' == $type || 'pro-shr' == $type || 'pro-cf' == $type ) { |
| 227 | $type = 'title'; |
| 228 | } |
| 229 | |
| 230 | switch ( $type ) { |
| 231 | |
| 232 | |
| 233 | case 'username': |
| 234 | $this->render_post_username( $settings, $class, $item ); |
| 235 | break; |
| 236 | |
| 237 | case 'profile-name': |
| 238 | $this->render_post_user_profile_name( $settings, $class, $item ); |
| 239 | break; |
| 240 | |
| 241 | case 'twit': |
| 242 | $this->render_post_caption( $settings, $class, $item ); |
| 243 | break; |
| 244 | |
| 245 | case 'date': |
| 246 | $this->render_post_date( $settings, $class, $item ); |
| 247 | break; |
| 248 | |
| 249 | case 'likes': |
| 250 | $this->render_post_likes( $settings, $class, $item ); |
| 251 | break; |
| 252 | |
| 253 | case 'comment': |
| 254 | $this->render_post_comment( $settings, $class, $item ); |
| 255 | break; |
| 256 | |
| 257 | case 'retweets': |
| 258 | $this->render_post_retweets( $settings, $class, $item ); |
| 259 | break; |
| 260 | |
| 261 | case 'media': |
| 262 | $this->render_post_media( $settings, $class, $item ); |
| 263 | break; |
| 264 | |
| 265 | case 'profile-picture': |
| 266 | $this->render_post_user_profile_picture( $settings, $class, $item ); |
| 267 | break; |
| 268 | |
| 269 | // case 'read-more': |
| 270 | // $this->render_post_account_name( $settings, $class ); |
| 271 | // break; |
| 272 | |
| 273 | // case 'lightbox': |
| 274 | // $this->render_post_lightbox( $settings, $class, $item ); |
| 275 | // break; |
| 276 | |
| 277 | case 'separator': |
| 278 | $this->render_post_element_separator( $settings, $class ); |
| 279 | break; |
| 280 | } |
| 281 | |
| 282 | } |
| 283 | |
| 284 | // Get Elements by Location |
| 285 | public function get_elements_by_location( $location, $settings, $item ) { |
| 286 | $locations = []; |
| 287 | |
| 288 | foreach ( $settings['twitter_feed_elements'] as $data ) { |
| 289 | $place = $data['element_location']; |
| 290 | $align_vr = $data['element_align_vr']; |
| 291 | |
| 292 | if ( !defined('WPR_ADDONS_PRO_VERSION') || !wpr_fs()->can_use_premium_code() ) { |
| 293 | $align_vr = 'middle'; |
| 294 | } |
| 295 | |
| 296 | if ( ! isset($locations[$place]) ) { |
| 297 | $locations[$place] = []; |
| 298 | } |
| 299 | |
| 300 | if ( 'over' === $place ) { |
| 301 | if ( ! isset($locations[$place][$align_vr]) ) { |
| 302 | $locations[$place][$align_vr] = []; |
| 303 | } |
| 304 | |
| 305 | array_push( $locations[$place][$align_vr], $data ); |
| 306 | } else { |
| 307 | array_push( $locations[$place], $data ); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | if ( ! empty( $locations[$location] ) ) { |
| 312 | |
| 313 | if ( 'over' === $location ) { |
| 314 | foreach ( $locations[$location] as $align => $thiss ) { |
| 315 | |
| 316 | if ( 'middle' === $align ) { |
| 317 | echo '<div class="wpr-cv-container"><div class="wpr-cv-outer"><div class="wpr-cv-inner">'; |
| 318 | } |
| 319 | |
| 320 | echo '<div class="wpr-twitter-feed-media-hover-'. esc_attr($align) .' elementor-clearfix">'; |
| 321 | foreach ( $thiss as $data ) { |
| 322 | |
| 323 | // Get Class |
| 324 | $class = 'wpr-twitter-feed-item-'. $data['element_select']; |
| 325 | $class .= ' elementor-repeater-item-'. $data['_id']; |
| 326 | $class .= ' wpr-twitter-feed-item-display-'. $data['element_display']; |
| 327 | $class .= ' wpr-twitter-feed-item-align-'. $data['element_align_hr']; |
| 328 | $class .= $this->get_animation_class( $data, 'element' ); |
| 329 | |
| 330 | // Element |
| 331 | $this->get_elements( $data['element_select'], $data, $class, $item ); |
| 332 | } |
| 333 | echo '</div>'; |
| 334 | |
| 335 | if ( 'middle' === $align ) { |
| 336 | echo '</div></div></div>'; |
| 337 | } |
| 338 | } |
| 339 | } else { |
| 340 | echo '<div class="wpr-twitter-feed-item-'. esc_attr($location) .'-content elementor-clearfix">'; |
| 341 | foreach ( $locations[$location] as $data ) { |
| 342 | |
| 343 | // Get Class |
| 344 | $class = 'wpr-twitter-feed-item-'. $data['element_select']; |
| 345 | $class .= ' elementor-repeater-item-'. $data['_id']; |
| 346 | $class .= ' wpr-twitter-feed-item-display-'. $data['element_display']; |
| 347 | $class .= ' wpr-twitter-feed-item-align-'. $data['element_align_hr']; |
| 348 | |
| 349 | // Element |
| 350 | $this->get_elements( $data['element_select'], $data, $class, $item ); |
| 351 | } |
| 352 | echo '</div>'; |
| 353 | } |
| 354 | |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | public function wpr_load_more_tweets_function() { |
| 359 | |
| 360 | if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'wpr-addons-js' ) ) { |
| 361 | wp_send_json_error( array( 'message' => esc_html__( 'Security check failed.', 'wpr-addons' ) ) ); |
| 362 | } |
| 363 | |
| 364 | if ( empty( $_POST['wpr_load_more_settings'] ) || ! is_array( $_POST['wpr_load_more_settings'] ) ) { |
| 365 | wp_send_json_error( array( 'message' => esc_html__( 'Invalid request.', 'wpr-addons' ) ) ); |
| 366 | } |
| 367 | |
| 368 | $settings = wp_unslash( $_POST['wpr_load_more_settings'] ); |
| 369 | |
| 370 | // Consumer key/secret must never be sent from frontend; use server-stored options or skip this flow. |
| 371 | $consumer_key = isset( $settings['twitter_feed_consumer_key'] ) ? $settings['twitter_feed_consumer_key'] : ''; |
| 372 | $consumer_secret = isset( $settings['twitter_feed_consumer_secret'] ) ? $settings['twitter_feed_consumer_secret'] : ''; |
| 373 | if ( '' === $consumer_key || '' === $consumer_secret ) { |
| 374 | wp_send_json_error( array( 'message' => esc_html__( 'Twitter API credentials are not configured for load more.', 'wpr-addons' ) ) ); |
| 375 | } |
| 376 | |
| 377 | $credentials = base64_encode( $consumer_key . ':' . $consumer_secret ); |
| 378 | |
| 379 | add_filter('https_ssl_verify', '__return_false'); |
| 380 | |
| 381 | $response = wp_remote_post('https://api.twitter.com/oauth2/token', [ |
| 382 | 'method' => 'POST', |
| 383 | 'httpversion' => '1.1', |
| 384 | 'blocking' => true, |
| 385 | 'headers' => [ |
| 386 | 'Authorization' => 'Basic ' . $credentials, |
| 387 | 'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8', |
| 388 | ], |
| 389 | 'body' => ['grant_type' => 'client_credentials'], |
| 390 | ]); |
| 391 | |
| 392 | $body = json_decode(wp_remote_retrieve_body($response)); |
| 393 | |
| 394 | if ($body) { |
| 395 | $token = $body->access_token; |
| 396 | } |
| 397 | |
| 398 | add_filter('https_ssl_verify', '__return_false'); |
| 399 | |
| 400 | $response = []; |
| 401 | $items_array = []; |
| 402 | |
| 403 | foreach ($settings['twitter_accounts'] as $key=>$value) { |
| 404 | |
| 405 | $response[$key] = wp_remote_get('https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=' . $value['twitter_feed_account_name'] . '&count='. ($settings['number_of_posts'] + $_POST['next_post_index']) .'&tweet_mode=extended', [ |
| 406 | 'httpversion' => '1.1', |
| 407 | 'blocking' => true, |
| 408 | 'headers' => [ |
| 409 | 'Authorization' => "Bearer $token", |
| 410 | ], |
| 411 | ]); |
| 412 | |
| 413 | if ( is_wp_error( $response[$key] ) ) { |
| 414 | return $response[$key]; |
| 415 | } |
| 416 | |
| 417 | if ( ! empty( $response[$key]['response'] ) && $response[$key]['response']['code'] == 200 ) { |
| 418 | $items_array[] = json_decode( wp_remote_retrieve_body( $response[$key] ), true ); |
| 419 | } |
| 420 | } |
| 421 | foreach ( $items_array as $key=>$items ) : |
| 422 | |
| 423 | if ($settings['twitter_feed_hashtag_name']) { |
| 424 | $hashtag_names = explode(',', str_replace(' ', '', $settings['twitter_feed_hashtag_name'])); |
| 425 | |
| 426 | foreach ($items as $key => $item) { |
| 427 | $match = false; |
| 428 | |
| 429 | if ($item['entities']['hashtags']) { |
| 430 | foreach ($item['entities']['hashtags'] as $tag) { |
| 431 | if (in_array($tag['text'], $hashtag_names)) { |
| 432 | $match = true; |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | if ($match == false) { |
| 438 | unset($items[$key]); |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | foreach ( $items as $key=>$item) : |
| 444 | |
| 445 | if ( $key >= 6 && (!defined('WPR_ADDONS_PRO_VERSION') || !wpr_fs()->can_use_premium_code()) ) { |
| 446 | break; |
| 447 | } |
| 448 | |
| 449 | if ($key < $_POST['next_post_index']) : |
| 450 | continue; |
| 451 | endif; |
| 452 | |
| 453 | $banner_placeholder = WPR_ADDONS_ASSETS_URL . 'img/placeholder.png'; |
| 454 | $banner = $item['user']['profile_banner_url'] ? $item['user']['profile_banner_url'] : $banner_placeholder; |
| 455 | ?> |
| 456 | <div class="wpr-tweet"> |
| 457 | <img src="<?php echo $banner ?>" alt=""> |
| 458 | <article class="media"> |
| 459 | |
| 460 | <?php |
| 461 | // Content: Above Media |
| 462 | echo $this->get_elements_by_location( 'above', $settings, $item ); |
| 463 | ?> |
| 464 | |
| 465 | </article> |
| 466 | </div> |
| 467 | <?php |
| 468 | |
| 469 | endforeach; |
| 470 | endforeach; |
| 471 | |
| 472 | die(); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | new WPR_Load_More_Tweets(); |