TemplateLayouts
1 month ago
AirTable.php
1 year ago
Boomplay.php
1 year ago
Calendly.php
2 years ago
Canva.php
1 year ago
FITE.php
1 year ago
GettyImages.php
9 months ago
Giphy.php
2 years ago
GitHub.php
3 years ago
GoogleCalendar.php
8 months ago
GoogleDocs.php
2 years ago
GoogleDrive.php
2 years ago
GoogleMaps.php
2 years ago
GooglePhotos.php
3 months ago
Gumroad.php
2 years ago
InstagramFeed.php
1 month ago
LinkedIn.php
2 years ago
Meetup.php
6 months ago
NRKRadio.php
2 years ago
OneDrive.php
11 months ago
OpenSea.php
9 months ago
SelfHosted.php
2 months ago
Spreaker.php
1 year ago
TikTok.php
2 years ago
Twitch.php
2 years ago
Wistia.php
3 months ago
Wrapper.php
3 years ago
X.php
2 years ago
Youtube.php
1 month ago
index.html
7 years ago
InstagramFeed.php
803 lines
| 1 | <?php |
| 2 | |
| 3 | namespace EmbedPress\Providers; |
| 4 | |
| 5 | use EmbedPress\Includes\Classes\Helper; |
| 6 | use Embera\Provider\ProviderAdapter; |
| 7 | use Embera\Provider\ProviderInterface; |
| 8 | use Embera\Provider\Instagram; |
| 9 | use Embera\Url; |
| 10 | |
| 11 | (defined('ABSPATH') && defined('EMBEDPRESS_IS_LOADED')) or die("No direct script access allowed."); |
| 12 | |
| 13 | /** |
| 14 | * Entity responsible to support InstagramFeed embeds. |
| 15 | * |
| 16 | * @package EmbedPress |
| 17 | * @subpackage EmbedPress/Providers |
| 18 | * @author EmbedPress <help@embedpress.com> |
| 19 | * @copyright Copyright (C) 2020 WPDeveloper. All rights reserved. |
| 20 | * @license GPLv3 or later |
| 21 | * @since 1.0.0 |
| 22 | */ |
| 23 | |
| 24 | |
| 25 | class InstagramFeed extends Instagram |
| 26 | { |
| 27 | /** inline {@inheritdoc} */ |
| 28 | protected $shouldSendRequest = false; |
| 29 | |
| 30 | /** |
| 31 | * Method that verifies if the embed URL belongs to InstagramFeed. |
| 32 | * |
| 33 | * @param Url $url |
| 34 | * @return boolean |
| 35 | * @since 1.0.0 |
| 36 | * |
| 37 | */ |
| 38 | |
| 39 | /** @var array Array with allowed params for the current Provider */ |
| 40 | protected $allowedParams = [ |
| 41 | 'maxwidth', |
| 42 | 'maxheight', |
| 43 | 'instaLayout', |
| 44 | 'instafeedColumns', |
| 45 | 'instafeedColumnsGap', |
| 46 | 'instafeedPostsPerPage', |
| 47 | 'instafeedProfileImage', |
| 48 | 'instafeedProfileImageUrl', |
| 49 | 'instafeedTab', |
| 50 | 'instafeedFollowBtn', |
| 51 | 'instafeedFollowBtnLabel', |
| 52 | 'instafeedPostsCount', |
| 53 | 'instafeedPostsCountText', |
| 54 | 'instafeedFollowersCount', |
| 55 | 'instafeedFollowersCountText', |
| 56 | 'instafeedAccName', |
| 57 | 'instafeedPopup', |
| 58 | 'instafeedPopupFollowBtn', |
| 59 | 'instafeedPopupFollowBtnLabel', |
| 60 | 'instafeedLoadmore', |
| 61 | 'instafeedLoadmoreLabel', |
| 62 | 'instafeedHashtag', |
| 63 | 'instafeedAccountType', |
| 64 | 'instafeedLikesCount', |
| 65 | 'instafeedCommentsCount', |
| 66 | 'instafeedFeedType', |
| 67 | 'slidesShow', |
| 68 | 'carouselSpacing' |
| 69 | ]; |
| 70 | |
| 71 | /** inline {@inheritdoc} */ |
| 72 | protected $httpsSupport = true; |
| 73 | |
| 74 | public function getAllowedParams() |
| 75 | { |
| 76 | return $this->allowedParams; |
| 77 | } |
| 78 | |
| 79 | public function __construct($url, array $config = []) |
| 80 | { |
| 81 | parent::__construct($url, $config); |
| 82 | add_filter('embedpress_render_dynamic_content', [$this, 'fakeDynamicResponse'], 10, 2); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Resolve allowedParams against the provider's config with case-insensitive |
| 87 | * matching. WordPress' shortcode_parse_atts() lowercases attribute names, so |
| 88 | * `[embedpress instaLayout="insta-grid"]` arrives here as `instalayout`. |
| 89 | * Embera's default ProviderAdapter::getParams() uses case-sensitive lookups |
| 90 | * against the camelCase allowedParams list, which drops every lowercased |
| 91 | * shortcode attribute. We rebuild the lookup case-insensitively. |
| 92 | */ |
| 93 | public function getParams() |
| 94 | { |
| 95 | $params = parent::getParams(); |
| 96 | foreach ($this->allowedParams as $allowed) { |
| 97 | if (isset($params[$allowed])) { |
| 98 | continue; |
| 99 | } |
| 100 | foreach ($this->config as $key => $value) { |
| 101 | if (strcasecmp($key, $allowed) === 0) { |
| 102 | $params[$allowed] = $value; |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | return $params; |
| 108 | } |
| 109 | |
| 110 | public function validateUrl(Url $url) |
| 111 | { |
| 112 | return |
| 113 | parent::validateUrl($url) || |
| 114 | (bool) preg_match( |
| 115 | '/^(?:https?:\/\/)?(?:www\.)?instagram\.com\/(?:[a-zA-Z0-9_\.]+\/?|explore\/tags\/[a-zA-Z0-9_\-]+\/?|(?:reel|p)\/[a-zA-Z0-9_-]+\/?)$/', |
| 116 | (string) $url |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | public function validateInstagramFeed($url) |
| 122 | { |
| 123 | return (bool) preg_match( |
| 124 | '/^(?:https?:\/\/)?(?:www\.)?instagram\.com\/(?:[a-zA-Z0-9_\.]+\/?|explore\/tags\/[a-zA-Z0-9_\-]+\/?)$/', |
| 125 | (string) $url |
| 126 | ); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | public function validateReelUrl($url) |
| 131 | { |
| 132 | return (bool) preg_match( |
| 133 | '/^(?:https?:\/\/)?(?:www\.)?instagram\.com\/(?:reel|p)\/[a-zA-Z0-9_-]+\/?$/', |
| 134 | (string) $url |
| 135 | ); |
| 136 | } |
| 137 | function enqueue_instagram_script() |
| 138 | { |
| 139 | static $script_loaded = false; |
| 140 | |
| 141 | if (!$script_loaded) { |
| 142 | echo '<script async src="https://www.instagram.com/embed.js"></script>'; |
| 143 | $script_loaded = true; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | public function get_connected_account_type($userID) |
| 148 | { |
| 149 | $instagram_account_data = get_option('ep_instagram_account_data'); |
| 150 | |
| 151 | if (is_array($instagram_account_data) && !empty($instagram_account_data)) { |
| 152 | foreach ($instagram_account_data as $account) { |
| 153 | if ($account['user_id'] == $userID) { |
| 154 | return $account['account_type']; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | public function getHashTag($url) |
| 163 | { |
| 164 | if (preg_match('/\/explore\/tags\/([a-zA-Z0-9_\-]+)/', parse_url($url, PHP_URL_PATH), $matches)) { |
| 165 | return $matches[1]; |
| 166 | } |
| 167 | return ''; |
| 168 | } |
| 169 | |
| 170 | public function getHashTagId($access_token, $hashtag, $user_id) |
| 171 | { |
| 172 | $transient_key = 'hashtag_id_' . md5($access_token . $hashtag . $user_id); |
| 173 | $transient_data = get_transient($transient_key); |
| 174 | |
| 175 | if (false === $transient_data) { |
| 176 | $api_url = "https://graph.facebook.com/v17.0/ig_hashtag_search?user_id=$user_id&q=$hashtag&access_token=$access_token"; |
| 177 | |
| 178 | // Make a GET request to Instagram's API to retrieve posts |
| 179 | $postsResponse = wp_remote_get($api_url); |
| 180 | |
| 181 | // Check if the posts request was successful |
| 182 | if (is_wp_error($postsResponse)) { |
| 183 | echo 'Error: Unable to retrieve Instagram posts.'; |
| 184 | } else { |
| 185 | $postsBody = wp_remote_retrieve_body($postsResponse); |
| 186 | $hashtagId = json_decode($postsBody, true); |
| 187 | |
| 188 | if (empty($hashtagId['data'])) { |
| 189 | $transient_data = 'Please add Instagram Access Token'; |
| 190 | } elseif (isset($hashtagId['data'][0]['id'])) { |
| 191 | $transient_data = $hashtagId['data'][0]['id']; |
| 192 | } else { |
| 193 | $transient_data = ''; |
| 194 | } |
| 195 | |
| 196 | // Set the transient with a 30-day expiration |
| 197 | set_transient($transient_key, $transient_data, 30 * DAY_IN_SECONDS); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | return $transient_data; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | public function getHashTagPosts($access_token, $hashtag, $user_id) |
| 206 | { |
| 207 | // Check if the data is already cached in a transient |
| 208 | $hashtag_id = $this->getHashTagId($access_token, $hashtag, $user_id); |
| 209 | |
| 210 | |
| 211 | $transient_key = 'hashtag_posts_' . $hashtag_id; |
| 212 | |
| 213 | $cached_posts = get_transient($transient_key); |
| 214 | |
| 215 | if (isset($cached_posts[$hashtag_id])) { |
| 216 | return $cached_posts[$hashtag_id]; |
| 217 | } |
| 218 | |
| 219 | $api_url = "https://graph.facebook.com/$hashtag_id/top_media?user_id=$user_id&fields=id,media_url,media_type,comments_count,like_count,caption,children{media_url,id,media_type},permalink,timestamp&access_token=$access_token"; |
| 220 | |
| 221 | $postsResponse = wp_remote_get($api_url, array('timeout' => 30)); |
| 222 | |
| 223 | if (is_wp_error($postsResponse)) { |
| 224 | echo 'Error: Unable to retrieve Hashtag Instagram posts.'; |
| 225 | } else { |
| 226 | $postsBody = wp_remote_retrieve_body($postsResponse); |
| 227 | $posts = json_decode($postsBody, true); |
| 228 | |
| 229 | if (empty($posts['data'])) { |
| 230 | return 'Please add Instagram Access Token'; |
| 231 | } |
| 232 | |
| 233 | // Update the cached posts array with the new data |
| 234 | $cached_posts[$hashtag_id] = $posts['data']; |
| 235 | |
| 236 | // Store the updated array in the transient |
| 237 | set_transient($transient_key, $cached_posts, HOUR_IN_SECONDS); |
| 238 | |
| 239 | return $cached_posts[$hashtag_id]; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | |
| 244 | public function update_instagram_feed_data($access_token, $connected_account_type, $user_id, $limit = 100) |
| 245 | { |
| 246 | $option_key = 'ep_instagram_feed_data'; |
| 247 | $feed_data = get_option($option_key, array()); |
| 248 | |
| 249 | $feed_userinfo = Helper::getInstagramUserInfo($access_token, $connected_account_type, $user_id, false); |
| 250 | $feed_posts = Helper::getInstagramPosts($access_token, $connected_account_type, $user_id, $limit, false); |
| 251 | |
| 252 | $feed_data[$user_id] = [ |
| 253 | 'feed_userinfo' => $feed_userinfo, |
| 254 | 'feed_posts' => $feed_posts, |
| 255 | ]; |
| 256 | |
| 257 | update_option($option_key, $feed_data); |
| 258 | // } |
| 259 | } |
| 260 | |
| 261 | |
| 262 | |
| 263 | public function getInstaFeedItem($post, $index, $account_type, $hashtag, $profile_picture_url, $params = []) |
| 264 | { |
| 265 | |
| 266 | |
| 267 | $caption = !empty($post['caption']) ? $post['caption'] : ''; |
| 268 | $media_type = !empty($post['media_type']) ? $post['media_type'] : ''; |
| 269 | $media_url = !empty($post['media_url']) ? $post['media_url'] : ''; |
| 270 | $permalink = !empty($post['permalink']) ? $post['permalink'] : ''; |
| 271 | $timestamp = !empty($post['timestamp']) ? $post['timestamp'] : ''; |
| 272 | $username = !empty($post['username']) ? $post['username'] : ''; |
| 273 | |
| 274 | $like_count = !empty($post['like_count']) ? $post['like_count'] : 0; |
| 275 | $comments_count = !empty($post['comments_count']) ? $post['comments_count'] : 0; |
| 276 | |
| 277 | $connected_usersAttributes = 'data-caption="' . htmlspecialchars($caption) . '" ' . |
| 278 | 'data-media-type="' . htmlspecialchars($media_type) . '" ' . |
| 279 | 'data-media-type="' . htmlspecialchars($media_type) . '" ' . |
| 280 | 'data-media-url="' . htmlspecialchars($media_url) . '" ' . |
| 281 | 'data-permalink="' . htmlspecialchars($permalink) . '" ' . |
| 282 | 'data-timestamp="' . htmlspecialchars($timestamp) . '" ' . |
| 283 | 'data-username="' . htmlspecialchars($username) . '" ' . |
| 284 | 'data-like-count="' . htmlspecialchars($like_count) . '" ' . |
| 285 | 'data-comments-count="' . htmlspecialchars($comments_count) . '" '; |
| 286 | |
| 287 | $post['account_type'] = $account_type; |
| 288 | $post['profile_picture_url'] = $profile_picture_url; |
| 289 | $post['show_likes_count'] = isset($params['instafeedLikesCount']) ? $params['instafeedLikesCount'] : false; |
| 290 | $post['show_comments_count'] = isset($params['instafeedCommentsCount']) ? $params['instafeedCommentsCount'] : false; |
| 291 | $post['popup_follow_button'] = isset($params['instafeedPopupFollowBtn']) ? $params['instafeedPopupFollowBtn'] : true; |
| 292 | $post['popup_follow_button_text'] = isset($params['instafeedPopupFollowBtnLabel']) ? $params['instafeedPopupFollowBtnLabel'] : 'Follow'; |
| 293 | |
| 294 | ob_start(); ?> |
| 295 | <div class="insta-gallery-item cg-carousel__slide js-carousel__slide" data-insta-postid="<?php echo esc_attr($post['id']) ?>" data-postindex="<?php echo esc_attr($index + 1); ?>" data-postdata="<?php echo htmlspecialchars(json_encode($post), ENT_QUOTES, 'UTF-8'); ?>" data-media-type="<?php echo esc_attr($media_type); ?>"> |
| 296 | <?php |
| 297 | if (!empty($hashtag) && $media_type == 'CAROUSEL_ALBUM') { |
| 298 | if (isset($post['children']['data'][0]['media_url'])) { |
| 299 | $hashtag_media_url = $post['children']['data'][0]['media_url']; |
| 300 | $hashtag_media_type = $post['children']['data'][0]['media_type']; |
| 301 | |
| 302 | if ($hashtag_media_type == 'VIDEO') { |
| 303 | echo '<video class="insta-gallery-image" src="' . esc_url($hashtag_media_url) . '"></video>'; |
| 304 | } else { |
| 305 | echo ' <img class="insta-gallery-image" src="' . esc_url($hashtag_media_url) . '" alt="' . esc_attr('image') . '">'; |
| 306 | } |
| 307 | } |
| 308 | } else { |
| 309 | if ($media_type == 'VIDEO') { |
| 310 | echo '<video class="insta-gallery-image" src="' . esc_url($media_url) . '"></video>'; |
| 311 | } else { |
| 312 | echo ' <img class="insta-gallery-image" src="' . esc_url($media_url) . '" alt="' . esc_attr('image') . '">'; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | ?> |
| 317 | |
| 318 | <div class="insta-gallery-item-type"> |
| 319 | <div class="insta-gallery-item-type-icon"> |
| 320 | <?php |
| 321 | if ($media_type == 'VIDEO') { |
| 322 | echo Helper::get_insta_video_icon(); |
| 323 | } else if ($media_type == 'CAROUSEL_ALBUM') { |
| 324 | echo Helper::get_insta_image_carousel_icon(); |
| 325 | } else { |
| 326 | echo Helper::get_insta_image_icon(); |
| 327 | } |
| 328 | ?> |
| 329 | </div> |
| 330 | </div> |
| 331 | <div class="insta-gallery-item-info"> |
| 332 | <?php if (apply_filters('embedpress/is_allow_rander', false) && (isset($params['instafeedFeedType']) && $params['instafeedFeedType'] === 'hashtag_type' || (isset($params['instafeedLikesCount']) && $params['instafeedLikesCount'] !== 'false')) && ((!empty($params['instafeedLikesCount']) && $params['instafeedLikesCount'] !== 'false') || (!empty($params['instafeedCommentsCount']) && $params['instafeedCommentsCount'] !== 'false'))) : ?> |
| 333 | <?php do_action('embedpress/instafeed_reaction_count', $params, $like_count, $comments_count); ?> |
| 334 | <?php else : ?> |
| 335 | <div class="insta-gallery-item-permalink"> |
| 336 | <?php echo Helper::get_instagram_icon(); ?> |
| 337 | </div> |
| 338 | <?php endif; ?> |
| 339 | </div> |
| 340 | </div> |
| 341 | |
| 342 | <?php $feed_item = ob_get_clean(); |
| 343 | return $feed_item; |
| 344 | } |
| 345 | |
| 346 | public function getInstagramFeedTemplate($accessToken, $account_type, $userID, $platform = '', $params = []) |
| 347 | { |
| 348 | |
| 349 | if ($platform !== 'gutenberg') { |
| 350 | $params = $this->getParams(); |
| 351 | } |
| 352 | |
| 353 | // Use URL from params if available (for Gutenberg blocks), otherwise use instance URL |
| 354 | $current_url = isset($params['url']) ? $params['url'] : $this->url; |
| 355 | |
| 356 | $hashtag = $this->getHashTag($current_url); |
| 357 | if (!empty($hashtag) && !apply_filters('embedpress/is_allow_rander', false)) { |
| 358 | return sprintf( |
| 359 | esc_html__('Unlock %s support by upgrading to our %s! Upgrade today to unlock a whole new level of functionality and make the most out of your experience with Hashtag.', 'embedpress'), |
| 360 | '<strong>hashtag</strong>', |
| 361 | '<strong>Pro subscription</strong>' |
| 362 | ); |
| 363 | } |
| 364 | |
| 365 | $feed_type = isset($params['instafeedFeedType']) ? $params['instafeedFeedType'] : 'user_account_type'; |
| 366 | |
| 367 | if ($feed_type === 'user_account_type' && !empty($hashtag)) { |
| 368 | return 'Please add valid url for user account'; |
| 369 | } else if ($feed_type === 'hashtag_type' && empty($hashtag)) { |
| 370 | return 'Please add valid url for hashtag feed'; |
| 371 | } else if ($feed_type === 'mixed_type') { |
| 372 | return 'Please add valid url for hashtag feed'; |
| 373 | } else if ($feed_type === 'tagged_type') { |
| 374 | return 'Please add valid url for hashtag feed'; |
| 375 | } |
| 376 | |
| 377 | $styleAttribute = ''; |
| 378 | $classes = ''; |
| 379 | |
| 380 | if (isset($params['instaLayout'])) { |
| 381 | if ($params['instaLayout'] === 'insta-grid') { |
| 382 | |
| 383 | // $classes = 'insta-grid'; |
| 384 | |
| 385 | if (isset($params['instafeedColumns']) && is_numeric($params['instafeedColumns']) && $params['instafeedColumns'] > 0) { |
| 386 | $column = (100 / intval($params['instafeedColumns'])); |
| 387 | $gap = isset($params['instafeedColumnsGap']) ? $params['instafeedColumnsGap'] : 0; |
| 388 | |
| 389 | $styleAttribute = 'style="grid-template-columns: repeat(' . esc_attr($params['instafeedColumns']) . ', minmax(0, 1fr)); gap: ' . esc_attr($gap) . 'px;"'; |
| 390 | } else { |
| 391 | $styleAttribute = 'style="grid-template-columns: repeat(1, minmax(0, 1fr));"'; |
| 392 | } |
| 393 | } else if ($params['instaLayout'] === 'insta-masonry') { |
| 394 | // $classes = ' insta-masonry'; |
| 395 | $styleAttribute = 'style="column-count: ' . esc_attr($params['instafeedColumns']) . '; gap: ' . esc_attr(isset($params['instafeedColumnsGap']) ? $params['instafeedColumnsGap'] : 0) . 'px;"'; |
| 396 | } else if ($params['instaLayout'] === 'insta-carousel') { |
| 397 | $classes = 'cg-carousel__track js-carousel__track'; |
| 398 | $styleAttribute = ''; |
| 399 | if (isset($params['slidesShow'])) { |
| 400 | $column = (100 / intval($params['slidesShow'])); |
| 401 | $space = isset($params['carouselSpacing']) ? $params['carouselSpacing'] : 0; |
| 402 | $styleAttribute = $styleAttribute = 'style="grid-auto-columns: calc(' . esc_attr($column) . '% - ' . esc_attr($space) . 'px); gap: ' . esc_attr($space) . 'px"'; // Or some default style |
| 403 | } |
| 404 | } else { |
| 405 | $styleAttribute = ''; // Or some default style |
| 406 | } |
| 407 | } else { |
| 408 | $styleAttribute = ''; // Or some default style |
| 409 | } |
| 410 | |
| 411 | |
| 412 | $feed_data = get_option('ep_instagram_feed_data'); |
| 413 | |
| 414 | // print_r($feed_data); die; |
| 415 | |
| 416 | if (!empty($feed_data[$userID]['feed_userinfo']['error'])) { |
| 417 | return $feed_data[$userID]['feed_userinfo']['error']['message']; |
| 418 | } |
| 419 | |
| 420 | $profile_info = $feed_data[$userID]['feed_userinfo']; |
| 421 | $insta_posts = $feed_data[$userID]['feed_posts']; |
| 422 | |
| 423 | $hashtag_id = ''; |
| 424 | if (!empty($hashtag)) { |
| 425 | $option_key = 'ep_instagram_hashtag_feed'; |
| 426 | $hashtag_feed = get_option($option_key, array()); |
| 427 | |
| 428 | $hashtag_id = $this->getHashTagId($accessToken, $hashtag, $userID); |
| 429 | $insta_posts = $this->getHashtagPosts($accessToken, $hashtag, $userID); |
| 430 | |
| 431 | $hashtag_feed[$hashtag_id] = $insta_posts; |
| 432 | |
| 433 | update_option($option_key, $hashtag_feed); |
| 434 | } |
| 435 | |
| 436 | // Check and assign each item to separate variables |
| 437 | $id = !empty($profile_info['id']) ? $profile_info['id'] : ''; |
| 438 | $username = !empty($profile_info['username']) ? $profile_info['username'] : ''; |
| 439 | $followers_count = !empty($profile_info['followers_count']) ? $profile_info['followers_count'] : 0; |
| 440 | $media_count = !empty($profile_info['media_count']) ? $profile_info['media_count'] : 0; |
| 441 | $profile_picture_url = !empty($profile_info['profile_picture_url']) ? $profile_info['profile_picture_url'] : ''; |
| 442 | $name = !empty($profile_info['name']) ? $profile_info['name'] : ''; |
| 443 | |
| 444 | |
| 445 | $connected_account_type = $account_type; |
| 446 | |
| 447 | if (strtolower($connected_account_type) === 'business') { |
| 448 | $tkey = md5($accessToken . $id . $connected_account_type . $feed_type); |
| 449 | } else { |
| 450 | $tkey = md5($accessToken . $connected_account_type . $feed_type); |
| 451 | } |
| 452 | |
| 453 | $loadmore_key = $hashtag_id; |
| 454 | if (empty($hashtag) || $hashtag === 'false') : |
| 455 | $loadmore_key = $userID; |
| 456 | endif; |
| 457 | |
| 458 | if (is_array($insta_posts) and !empty($insta_posts)) { |
| 459 | ob_start(); ?> |
| 460 | |
| 461 | <?php |
| 462 | $avater_url = 'http://2.gravatar.com/avatar/b642b4217b34b1e8d3bd915fc65c4452?s=150&d=mm&r=g'; |
| 463 | |
| 464 | |
| 465 | if (!empty($params['instafeedProfileImageUrl']) && $params['instafeedProfileImageUrl'] !== 'true' && $params['instafeedProfileImageUrl'] !== 'false') { |
| 466 | $avater_url = $params['instafeedProfileImageUrl']; |
| 467 | } |
| 468 | else{ |
| 469 | $avater_url = $profile_picture_url; |
| 470 | } |
| 471 | |
| 472 | $feed_data[$id]['feed_userinfo']['profile_picture_url'] = $avater_url; |
| 473 | |
| 474 | update_option('ep_instagram_feed_data', $feed_data); |
| 475 | ?> |
| 476 | |
| 477 | <?php if (empty($hashtag) || $hashtag === 'false') : ?> |
| 478 | <header class="profile-header"> |
| 479 | |
| 480 | <?php if (!empty($params['instafeedProfileImage']) && $params['instafeedProfileImage'] !== 'false') : ?> |
| 481 | <div class="profile-image"> |
| 482 | <img src="<?php echo esc_url($avater_url); ?>" alt="<?php echo esc_attr($name); ?>"> |
| 483 | </div> |
| 484 | <?php endif; ?> |
| 485 | <section class="profile-details"> |
| 486 | <div class="username-section"> |
| 487 | <a class="profile-link" target="__blank" href="<?php echo esc_url('https://instagram.com/' . $username); ?>" role="link" tabindex="0"> |
| 488 | <h2 class="username" dir="auto"><?php echo esc_html($username); ?></h2> |
| 489 | </a> |
| 490 | |
| 491 | <?php if (!empty($params['instafeedFollowBtn']) && $params['instafeedFollowBtn'] !== 'false' && !empty($params['instafeedFollowBtnLabel']) && $params['instafeedFollowBtnLabel'] !== 'false' && $params['instafeedFollowBtnLabel'] !== 'true') : ?> |
| 492 | <div class="edit-profile-button"> |
| 493 | <a class="edit-profile-link" target="__blank" href="<?php echo esc_url('https://instagram.com/' . $username); ?>" role="link" tabindex="0"> |
| 494 | <?php echo esc_html($params['instafeedFollowBtnLabel']); ?> |
| 495 | </a> |
| 496 | </div> |
| 497 | <?php endif; ?> |
| 498 | |
| 499 | </div> |
| 500 | <div class="profile-stats"> |
| 501 | <?php if (!empty($params['instafeedPostsCount']) && $params['instafeedPostsCount'] !== 'false') : ?> |
| 502 | <div class="posts-count"> |
| 503 | <?php if (!empty($params['instafeedPostsCountText']) && $params['instafeedPostsCountText'] !== 'false' && $params['instafeedPostsCountText'] !== 'true') : |
| 504 | $posts_count_text = str_replace(['[count]', '{count}'], '<span class="count">' . $media_count . '</span>', $params['instafeedPostsCountText']); |
| 505 | echo wp_kses_post($posts_count_text); |
| 506 | endif; |
| 507 | ?> |
| 508 | |
| 509 | </div> |
| 510 | <?php endif; ?> |
| 511 | |
| 512 | <?php if (!empty($params['instafeedFollowersCount']) && $params['instafeedFollowersCount'] !== 'false' && (!isset($params['instafeedFollowersCountText']) || $params['instafeedFollowersCountText'] !== 'true')) : ?> |
| 513 | <div class="followers-count"> |
| 514 | <?php if (!empty($params['instafeedFollowersCountText']) && $params['instafeedFollowersCountText'] !== 'false' && $params['instafeedFollowersCountText'] !== 'true') : ?> |
| 515 | <a class="followers-link" target="_blank" href="<?php echo esc_url('https://instagram.com/' . $username . '/followers'); ?>" role="link" tabindex="0"> |
| 516 | <?php |
| 517 | $followers_count_text = str_replace(['[count]', '{count}'], '<span class="count">' . $followers_count . '</span>', $params['instafeedFollowersCountText']); |
| 518 | |
| 519 | echo wp_kses_post($followers_count_text); |
| 520 | ?> |
| 521 | </a> |
| 522 | <?php endif; ?> |
| 523 | </div> |
| 524 | <?php endif; ?> |
| 525 | </div> |
| 526 | <?php if (!empty($params['instafeedAccName']) && $params['instafeedAccName'] !== 'false') : ?> |
| 527 | <div class="bio-section"> |
| 528 | <span class="bio" dir="auto"><?php echo esc_attr($name); ?></span> |
| 529 | </div> |
| 530 | <?php endif; ?> |
| 531 | </section> |
| 532 | </header> |
| 533 | <?php else : ?> |
| 534 | <div class="hashtag-container"> |
| 535 | <div class="embedpress-hashtag-header"> |
| 536 | |
| 537 | <?php if (!empty($params['instafeedProfileImage']) && $params['instafeedProfileImage'] !== 'false') : ?> |
| 538 | |
| 539 | <div class="embedpress-hashtag-header-img"> <a target="_blank" href="<?php echo esc_url("https://www.instagram.com/explore/tags/$hashtag/"); ?>" class="embedpress-href"> <img decoding="async" loading="lazy" class="embedpress-hashtag-round" src="<?php echo esc_url($avater_url); ?>" width="30" height="30"> <span class="embedpress-hashtag-username"><?php echo esc_html("#$hashtag"); ?></span> |
| 540 | </a> |
| 541 | </div> |
| 542 | <?php endif; ?> |
| 543 | |
| 544 | <?php if (!empty($params['instafeedFollowBtn']) && $params['instafeedFollowBtn'] !== 'false' && !empty($params['instafeedFollowBtnLabel']) && $params['instafeedFollowBtnLabel'] !== 'false') : ?> |
| 545 | <div class="insta-followbtn"> |
| 546 | <a target="_new" href="<?php echo esc_url("https://www.instagram.com/explore/tags/$hashtag/"); ?>" type="button" class="btn btn-primary"><?php echo esc_html($params['instafeedFollowBtnLabel']); ?></a> |
| 547 | </div> |
| 548 | <?php endif; ?> |
| 549 | </div> |
| 550 | </div> |
| 551 | <?php endif; ?> |
| 552 | |
| 553 | <?php if (apply_filters('embedpress/is_allow_rander', false) && (!empty($params['instafeedTab']) && $params['instafeedTab'] !== 'false')) : ?> |
| 554 | <?php do_action('embedpress/instafeed_tab_option'); ?> |
| 555 | <?php endif; ?> |
| 556 | |
| 557 | <?php |
| 558 | $params_data = [ |
| 559 | 'show_likes_count' => isset($params['instafeedLikesCount']) ? $params['instafeedLikesCount'] : false, |
| 560 | 'show_comments_count' => isset($params['instafeedCommentsCount']) ? $params['instafeedCommentsCount'] : false, |
| 561 | 'popup_follow_button' => isset($params['instafeedPopupFollowBtn']) ? $params['instafeedPopupFollowBtn'] : true, |
| 562 | 'popup_follow_button_text' => isset($params['instafeedPopupFollowBtnLabel']) ? $params['instafeedPopupFollowBtnLabel'] : 'Follow' |
| 563 | ]; |
| 564 | |
| 565 | $params_data_json = json_encode($params_data); |
| 566 | |
| 567 | |
| 568 | |
| 569 | ?> |
| 570 | |
| 571 | <div class="instagram-container" data-tkey="<?php echo esc_attr($loadmore_key); ?>" data-feed-type="<?php echo esc_attr($feed_type); ?>" data-hashtag="<?php echo esc_attr($hashtag); ?>" data-hashtag-id="<?php echo esc_attr($hashtag_id); ?>" data-connected-acc-type="<?php echo esc_attr($connected_account_type); ?>" data-uid="<?php echo esc_attr($userID); ?>" data-params="<?php echo htmlspecialchars($params_data_json, ENT_QUOTES, 'UTF-8'); ?>"> |
| 572 | <div class="embedpress-insta-container"> |
| 573 | <div class="insta-gallery <?php echo esc_attr($classes); ?>" <?php echo $styleAttribute; ?>> |
| 574 | <?php |
| 575 | $posts_per_page = 12; |
| 576 | |
| 577 | if (!empty($params['instafeedPostsPerPage'])) { |
| 578 | $posts_per_page = $params['instafeedPostsPerPage']; |
| 579 | } |
| 580 | // Set the limit to 5 |
| 581 | $counter = 0; // Initialize a counter variable |
| 582 | |
| 583 | foreach ($insta_posts as $index => $post) { |
| 584 | if ($counter >= $posts_per_page) { |
| 585 | break; // Exit the loop when the counter reaches the limit |
| 586 | } |
| 587 | print_r($this->getInstaFeedItem($post, $index, $connected_account_type, $hashtag, $avater_url, $params)); |
| 588 | |
| 589 | $counter++; // Increment the counter for each processed item |
| 590 | } |
| 591 | ?> |
| 592 | </div> |
| 593 | <div class="cg-carousel__btns hidden"> |
| 594 | <button class="cg-carousel__btn" id="js-carousel__prev-1"><svg width="20" height="30" viewBox="-5 0 23 23" xmlns="http://www.w3.org/2000/svg"> |
| 595 | <path d="M11.24.29.361 10.742l-.06.054a.97.97 0 0 0-.301.642v.124a.97.97 0 0 0 .3.642l.054.044L11.239 22.71a1.061 1.061 0 0 0 1.459 0 .964.964 0 0 0 0-1.402l-10.15-9.746 10.15-9.87a.964.964 0 0 0 0-1.402 1.061 1.061 0 0 0-1.459 0Z" fill="#fff" /> |
| 596 | </svg></button> |
| 597 | |
| 598 | <button class="cg-carousel__btn" id="js-carousel__next-1"><svg width="20" height="30" viewBox="-5 0 23 23" xmlns="http://www.w3.org/2000/svg"> |
| 599 | <path d="m1.76.29 10.879 10.452.06.054a.97.97 0 0 1 .301.642v.124a.97.97 0 0 1-.3.642l-.054.044L1.761 22.71a1.061 1.061 0 0 1-1.459 0 .964.964 0 0 1 0-1.402l10.15-9.746-10.15-9.87a.964.964 0 0 1 0-1.402 1.061 1.061 0 0 1 1.459 0Z" fill="#fff" /> |
| 600 | </svg></button> |
| 601 | </div> |
| 602 | </div> |
| 603 | |
| 604 | <!-- Popup div --> |
| 605 | <?php if (!empty($params['instafeedPopup']) && $params['instafeedPopup'] !== 'false') : ?> |
| 606 | <div class="insta-popup" style="display: none;"> |
| 607 | <div class="popup-wrapper popup-is-opened"> |
| 608 | <div class="popup popup-is-initialized" tabindex="-1"> </div> |
| 609 | <div class="popup-close">✕</div> |
| 610 | </div> |
| 611 | </div> |
| 612 | <?php endif; ?> |
| 613 | |
| 614 | <?php if (!empty($params['instafeedLoadmore']) && $params['instafeedLoadmore'] !== 'false') : ?> |
| 615 | |
| 616 | |
| 617 | <?php if (count($insta_posts) > $posts_per_page && (!isset($params['instafeedLoadmoreLabel']) || ($params['instafeedLoadmoreLabel'] !== 'false' && $params['instafeedLoadmoreLabel'] !== 'true'))) : ?> |
| 618 | <div class="load-more-button-container" data-loadmorekey="<?php echo esc_attr($loadmore_key); ?>" data-loaded-posts="<?php echo esc_attr($posts_per_page); ?>" data-posts-per-page="<?php echo esc_attr($posts_per_page); ?>"> |
| 619 | <button class="insta-load-more-button"> |
| 620 | <?php echo !empty($params['instafeedLoadmoreLabel']) ? esc_html($params['instafeedLoadmoreLabel']) : ''; ?> |
| 621 | </button> |
| 622 | </div> |
| 623 | <?php endif; ?> |
| 624 | <?php endif; ?> |
| 625 | |
| 626 | </div> |
| 627 | |
| 628 | |
| 629 | <?php |
| 630 | $feed_template = ob_get_clean(); |
| 631 | return $feed_template; |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | public function getInstagramUnserName($url) |
| 636 | { |
| 637 | $pattern = '/instagram\.com\/([^\/?]+)/i'; |
| 638 | preg_match($pattern, $url, $matches); |
| 639 | return isset($matches[1]) ? $matches[1] : ''; |
| 640 | } |
| 641 | |
| 642 | |
| 643 | public function getStaticResponse() |
| 644 | { |
| 645 | $url = $this->getUrl(); |
| 646 | |
| 647 | if ($this->validateReelUrl($url)) { |
| 648 | |
| 649 | $params = $this->getParams(); |
| 650 | $width = isset($params['maxwidth']) ? $params['maxwidth'] : 380; |
| 651 | $height = isset($params['maxheight']) ? $params['maxheight'] : 600; |
| 652 | |
| 653 | $clean_url = rtrim($url, '/'); |
| 654 | $src_url = $clean_url . '/embed'; |
| 655 | |
| 656 | $IG_reel = '<iframe width="' . esc_attr($width) . 'px" width="' . esc_attr($height) . 'px" allowtransparency="true" allowfullscreen="true" frameborder="0" height="534" data-instgrm-payload-id="instagram-media-payload-0" scrolling="no" style="background-color: white; border-radius: 3px; border: 1px solid rgb(219, 219, 219); box-shadow: none; display: block; margin: 0px 0px 12px; min-width: 326px; padding: 0px;" class="instagram-media instagram-media-rendered" src="' . esc_url($src_url) . '">Fetching content</iframe>'; |
| 657 | |
| 658 | $insta_feed['html'] = $IG_reel; |
| 659 | $insta_feed['provider_name'] = 'Instagram Feed'; |
| 660 | return $insta_feed; |
| 661 | } |
| 662 | $connected_users = get_option('ep_instagram_account_data'); |
| 663 | |
| 664 | $username = $this->getInstagramUnserName($url) ? $this->getInstagramUnserName($url) : ''; |
| 665 | |
| 666 | if ($this->validateInstagramFeed($url) && $this->getHashTag($url)) { |
| 667 | foreach ($connected_users as $entry) { |
| 668 | if ($entry['account_type'] === 'business') { |
| 669 | $businessUsernames[] = $entry['username']; |
| 670 | $username = $businessUsernames[0]; |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | $access_token = ''; // The access token'; |
| 676 | $account_type = ''; |
| 677 | $userid = ''; |
| 678 | |
| 679 | if ($this->validateInstagramFeed($url) || !empty($username)) { |
| 680 | if (!empty($username)) { |
| 681 | |
| 682 | if (empty($connected_users)) { |
| 683 | $connected_users = []; |
| 684 | } |
| 685 | |
| 686 | // Find the key of the matching username |
| 687 | $index = array_search($username, array_column($connected_users, 'username')); |
| 688 | |
| 689 | if ($index !== false) { |
| 690 | // Matching username found |
| 691 | $access_token = $connected_users[$index]['access_token']; |
| 692 | $userid = $connected_users[$index]['user_id']; |
| 693 | $account_type = $connected_users[$index]['account_type']; |
| 694 | // <a href="' . esc_url($page) . '">here</a> |
| 695 | } else { |
| 696 | // No matching username found |
| 697 | $page = site_url() . "/wp-admin/admin.php?page=embedpress&page_type=instagram"; |
| 698 | $insta_feed['html'] = '<p style="text-align:center;height:100%;display:flex;justify-content:center;align-items:center;margin:0;flex-direction: column;">To enable full Instagram embedding experience, please add your access token by navigating to: <b>Dashboard > EmbedPress > Settings > Sources > Instagram</></p>'; |
| 699 | $insta_feed['provider_name'] = 'Instagram Feed'; |
| 700 | return $insta_feed; |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | $this->update_instagram_feed_data($access_token, $account_type, $userid, $limit = 100); |
| 705 | |
| 706 | $template = $this->getInstagramFeedTemplate($access_token, $account_type, $userid); |
| 707 | if ($template) { |
| 708 | $insta_feed['html'] = $template; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | $insta_feed['provider_name'] = 'Instagram Feed'; |
| 713 | return $insta_feed; |
| 714 | } |
| 715 | |
| 716 | public function fakeDynamicResponse($html, $params) |
| 717 | { |
| 718 | |
| 719 | |
| 720 | $url = $params['url']; |
| 721 | |
| 722 | if ($this->validateReelUrl($url)) { |
| 723 | |
| 724 | $width = isset($params['maxwidth']) ? $params['maxwidth'] : 380; |
| 725 | $height = isset($params['maxheight']) ? $params['maxheight'] : 600; |
| 726 | |
| 727 | $clean_url = rtrim($url, '/'); |
| 728 | $src_url = $clean_url . '/embed'; |
| 729 | |
| 730 | $IG_reel = '<iframe width="' . esc_attr($width) . 'px" width="' . esc_attr($height) . 'px" allowtransparency="true" allowfullscreen="true" frameborder="0" height="534" data-instgrm-payload-id="instagram-media-payload-0" scrolling="no" style="background-color: white; border-radius: 3px; border: 1px solid rgb(219, 219, 219); box-shadow: none; display: block; margin: 0px 0px 12px; min-width: 326px; padding: 0px;" class="instagram-media instagram-media-rendered" src="' . esc_url($src_url) . '">Fetching content</iframe>'; |
| 731 | |
| 732 | $insta_feed['html'] = $IG_reel; |
| 733 | $insta_feed['provider_name'] = 'Instagram Feed'; |
| 734 | return $insta_feed; |
| 735 | } |
| 736 | $connected_users = get_option('ep_instagram_account_data'); |
| 737 | |
| 738 | $username = $this->getInstagramUnserName($url) ? $this->getInstagramUnserName($url) : ''; |
| 739 | |
| 740 | if (is_array($connected_users) && $this->validateInstagramFeed($url) && $this->getHashTag($url)) { |
| 741 | foreach ($connected_users as $entry) { |
| 742 | if ($entry['account_type'] === 'business') { |
| 743 | $businessUsernames[] = $entry['username']; |
| 744 | $username = $businessUsernames[0]; |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | $access_token = ''; // The access token'; |
| 750 | $account_type = ''; |
| 751 | $userid = ''; |
| 752 | |
| 753 | if ($this->validateInstagramFeed($url) || !empty($username)) { |
| 754 | if (!empty($username)) { |
| 755 | |
| 756 | if (empty($connected_users)) { |
| 757 | $connected_users = []; |
| 758 | } |
| 759 | |
| 760 | // Find the key of the matching username |
| 761 | $index = array_search($username, array_column($connected_users, 'username')); |
| 762 | |
| 763 | if ($index !== false) { |
| 764 | // Matching username found |
| 765 | $access_token = $connected_users[$index]['access_token']; |
| 766 | $userid = $connected_users[$index]['user_id']; |
| 767 | $account_type = $connected_users[$index]['account_type']; |
| 768 | // <a href="' . esc_url($page) . '">here</a> |
| 769 | } else { |
| 770 | // No matching username found |
| 771 | $page = site_url() . "/wp-admin/admin.php?page=embedpress&page_type=instagram"; |
| 772 | $insta_feed['html'] = '<p style="text-align:center;height:100%;display:flex;justify-content:center;align-items:center;margin:0;flex-direction: column;">To enable full Instagram embedding experience, please add your access token by navigating to: <b>Dashboard > EmbedPress > Settings > Sources > Instagram</></p>'; |
| 773 | $insta_feed['provider_name'] = 'Instagram Feed'; |
| 774 | return $insta_feed; |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | $this->update_instagram_feed_data($access_token, $account_type, $userid, $limit = 100); |
| 779 | |
| 780 | if ($this->getInstagramFeedTemplate($access_token, $account_type, $userid)) { |
| 781 | $insta_feed['html'] = $this->getInstagramFeedTemplate($access_token, $account_type, $userid, 'gutenberg', $params); |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | $insta_feed['provider_name'] = 'Instagram Feed'; |
| 786 | return $insta_feed; |
| 787 | } |
| 788 | |
| 789 | |
| 790 | |
| 791 | /** inline {@inheritdoc} */ |
| 792 | public function getFakeResponse() |
| 793 | { |
| 794 | return [ |
| 795 | 'type' => 'video', |
| 796 | 'provider_name' => 'Instagram Feed', |
| 797 | 'provider_url' => 'https://instagram.com', |
| 798 | 'title' => 'Unknown title', |
| 799 | 'html' => '', |
| 800 | ]; |
| 801 | } |
| 802 | } |
| 803 |