| 1 |
<?php |
| 2 |
/** |
| 3 |
* Helpers Instagram |
| 4 |
* |
| 5 |
* @package Powerkit |
| 6 |
* @subpackage Modules/Helper |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Template handler |
| 11 |
* |
| 12 |
* @param string $name Specific template. |
| 13 |
* @param array $feed Array of instagram feed. |
| 14 |
* @param array $instagram Array of instagram items. |
| 15 |
* @param array $params Array of params. |
| 16 |
*/ |
| 17 |
function powerkit_instagram_template_handler( $name, $feed, $instagram, $params ) { |
| 18 |
$templates = apply_filters( 'powerkit_instagram_templates', array() ); |
| 19 |
|
| 20 |
$new = isset( $templates['default'] ) ? false : true; |
| 21 |
|
| 22 |
if ( $new && count( $templates ) > 0 ) { |
| 23 |
$first_item = array_shift( $templates ); |
| 24 |
|
| 25 |
if ( function_exists( $first_item['func'] ) ) { |
| 26 |
call_user_func( $first_item['func'], $feed, $instagram, $params ); |
| 27 |
} else { |
| 28 |
call_user_func( 'powerkit_instagram_default_template', $feed, $instagram, $params ); |
| 29 |
} |
| 30 |
} elseif ( isset( $templates[ $name ] ) && function_exists( $templates[ $name ]['func'] ) ) { |
| 31 |
call_user_func( $templates[ $name ]['func'], $feed, $instagram, $params ); |
| 32 |
} else { |
| 33 |
call_user_func( 'powerkit_instagram_default_template', $feed, $instagram, $params ); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Get templates options |
| 39 |
* |
| 40 |
* @return array Items. |
| 41 |
*/ |
| 42 |
function powerkit_instagram_get_templates_options() { |
| 43 |
$options = array(); |
| 44 |
|
| 45 |
$templates = apply_filters( 'powerkit_instagram_templates', array() ); |
| 46 |
|
| 47 |
if ( $templates ) { |
| 48 |
foreach ( $templates as $key => $item ) { |
| 49 |
if ( isset( $item['name'] ) ) { |
| 50 |
$options[ $key ] = $item['name']; |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
return $options; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Get data (from json) of instagram response |
| 60 |
* |
| 61 |
* @param string $ins_request Instagram request. |
| 62 |
* @param array $ins_params Options. |
| 63 |
*/ |
| 64 |
function powerkit_instagram_json_get_feed( $ins_request, $ins_params ) { |
| 65 |
|
| 66 |
$user_data = wp_remote_retrieve_body( $ins_request ); |
| 67 |
|
| 68 |
// Json decode of user. |
| 69 |
$user_data = json_decode( $user_data ); |
| 70 |
|
| 71 |
// Get data of user. |
| 72 |
if ( is_object( $user_data ) ) { |
| 73 |
|
| 74 |
if ( isset( $user_data->error->message ) ) { |
| 75 |
|
| 76 |
return sprintf( '%s <a href="%s">%s</a>', $user_data->error->message . esc_html__( ' Please check your ', 'powerkit' ), powerkit_get_page_url( 'connect&tab=instagram' ), esc_html__( ' Instagram Settings', 'powerkit' ) ); |
| 77 |
|
| 78 |
} elseif ( isset( $user_data->username ) ) { |
| 79 |
|
| 80 |
if ( $ins_params['user_id'] !== $user_data->username ) { |
| 81 |
return esc_html__( 'Please check your Instagram User, you do not have access to this account.', 'powerkit' ); |
| 82 |
} |
| 83 |
} else { |
| 84 |
|
| 85 |
return sprintf( '%s <a href="%s">%s</a>', esc_html__( 'Instagram data is not set. Please check your Instagram User or ', 'powerkit' ), powerkit_get_page_url( 'connect&tab=instagram' ), esc_html__( ' Instagram Settings', 'powerkit' ) ); |
| 86 |
} |
| 87 |
} else { |
| 88 |
return esc_html__( 'Error decoding the instagram json.', 'powerkit' ); |
| 89 |
} |
| 90 |
|
| 91 |
$media_data = wp_remote_retrieve_body( $ins_request['headers']['recent'] ); |
| 92 |
|
| 93 |
// Json decode of media. |
| 94 |
$media_data = json_decode( $media_data ); |
| 95 |
|
| 96 |
// Get data of media. |
| 97 |
if ( is_object( $media_data ) ) { |
| 98 |
|
| 99 |
if ( isset( $media_data->error->message ) ) { |
| 100 |
|
| 101 |
return sprintf( '%s <a href="%s">%s</a>', $media_data->error->message . esc_html__( ' Please check your ', 'powerkit' ), powerkit_get_page_url( 'connect&tab=instagram' ), esc_html__( ' Instagram Settings', 'powerkit' ) ); |
| 102 |
|
| 103 |
} elseif ( isset( $media_data ) ) { |
| 104 |
|
| 105 |
// Images not found. |
| 106 |
if ( ! is_array( $media_data->data ) || count( $media_data->data ) <= 0 ) { |
| 107 |
return esc_html__( 'There are no images in your account.', 'powerkit' ); |
| 108 |
} |
| 109 |
} else { |
| 110 |
|
| 111 |
return sprintf( '%s <a href="%s">%s</a>', esc_html__( 'Instagram data is not set. Please check your Instagram User or ', 'powerkit' ), powerkit_get_page_url( 'connect&tab=instagram' ), esc_html__( ' Instagram Settings', 'powerkit' ) ); |
| 112 |
} |
| 113 |
} else { |
| 114 |
return esc_html__( 'Error decoding the instagram json.', 'powerkit' ); |
| 115 |
} |
| 116 |
|
| 117 |
/* ------------- PARSE DATA -------------- */ |
| 118 |
|
| 119 |
$feed = array(); |
| 120 |
|
| 121 |
// Full name. |
| 122 |
$feed['name'] = null; |
| 123 |
|
| 124 |
if ( isset( $user_data->name ) ) { |
| 125 |
$feed['name'] = $user_data->name; |
| 126 |
} |
| 127 |
|
| 128 |
// Username. |
| 129 |
$feed['username'] = null; |
| 130 |
|
| 131 |
if ( isset( $user_data->username ) ) { |
| 132 |
$feed['username'] = $user_data->username; |
| 133 |
} |
| 134 |
|
| 135 |
// Liked count. |
| 136 |
$feed['following'] = null; |
| 137 |
|
| 138 |
if ( isset( $user_data->following_count ) ) { |
| 139 |
$feed['following'] = (int) $user_data->following_count; |
| 140 |
} |
| 141 |
|
| 142 |
// Liked count. |
| 143 |
$feed['followers'] = null; |
| 144 |
|
| 145 |
if ( isset( $user_data->followers_count ) ) { |
| 146 |
$feed['followers'] = (int) $user_data->followers_count; |
| 147 |
} |
| 148 |
|
| 149 |
// Avatar x1. |
| 150 |
$feed['avatar_1x'] = null; |
| 151 |
|
| 152 |
if ( isset( $user_data->profile_picture_url ) ) { |
| 153 |
$feed['avatar_1x'] = $user_data->profile_picture_url; |
| 154 |
} |
| 155 |
|
| 156 |
// Avatar x2. |
| 157 |
$feed['avatar_2x'] = null; |
| 158 |
|
| 159 |
if ( isset( $user_data->profile_picture_url ) ) { |
| 160 |
$feed['avatar_2x'] = $user_data->profile_picture_url; |
| 161 |
} |
| 162 |
|
| 163 |
// Images. |
| 164 |
$feed['images'] = array(); |
| 165 |
|
| 166 |
foreach ( $media_data->data as $key => $media ) { |
| 167 |
// Thumbnail. |
| 168 |
$feed['images'][ $key ]['sizes']['thumbnail'] = null; |
| 169 |
|
| 170 |
if ( isset( $media->media_url ) ) { |
| 171 |
$feed['images'][ $key ]['sizes']['thumbnail'] = $media->media_url; |
| 172 |
} |
| 173 |
|
| 174 |
// Small. |
| 175 |
$feed['images'][ $key ]['sizes']['small'] = null; |
| 176 |
|
| 177 |
if ( isset( $media->media_url ) ) { |
| 178 |
$feed['images'][ $key ]['sizes']['small'] = $media->media_url; |
| 179 |
} |
| 180 |
|
| 181 |
// Large. |
| 182 |
$feed['images'][ $key ]['sizes']['large'] = null; |
| 183 |
|
| 184 |
if ( isset( $media->media_url ) ) { |
| 185 |
$feed['images'][ $key ]['sizes']['large'] = $media->media_url; |
| 186 |
} |
| 187 |
|
| 188 |
if ( isset( $media->thumbnail_url ) ) { |
| 189 |
$feed['images'][ $key ]['sizes']['thumbnail'] = $media->thumbnail_url; |
| 190 |
$feed['images'][ $key ]['sizes']['small'] = $media->thumbnail_url; |
| 191 |
$feed['images'][ $key ]['sizes']['large'] = $media->thumbnail_url; |
| 192 |
} |
| 193 |
|
| 194 |
// Item link. |
| 195 |
$feed['images'][ $key ]['link'] = null; |
| 196 |
|
| 197 |
if ( isset( $media->permalink ) ) { |
| 198 |
$feed['images'][ $key ]['link'] = $media->permalink; |
| 199 |
} |
| 200 |
|
| 201 |
// Desc. |
| 202 |
$feed['images'][ $key ]['text'] = null; |
| 203 |
|
| 204 |
if ( isset( $media->caption ) ) { |
| 205 |
$text = strtok( $media->caption, "\n" ); |
| 206 |
|
| 207 |
$feed['images'][ $key ]['text'] = strip_tags( $text ); |
| 208 |
} |
| 209 |
|
| 210 |
// Timestamp. |
| 211 |
$feed['images'][ $key ]['timestamp'] = null; |
| 212 |
|
| 213 |
if ( isset( $media->timestamp ) ) { |
| 214 |
$feed['images'][ $key ]['timestamp'] = powerkit_relative_time( $media->timestamp ); |
| 215 |
} |
| 216 |
|
| 217 |
// Comment count. |
| 218 |
$feed['images'][ $key ]['comment_count'] = null; |
| 219 |
|
| 220 |
if ( isset( $media->comments_count ) ) { |
| 221 |
$feed['images'][ $key ]['comment_count'] = (int) $media->comments_count; |
| 222 |
} |
| 223 |
|
| 224 |
// Liked count. |
| 225 |
$feed['images'][ $key ]['liked_count'] = null; |
| 226 |
|
| 227 |
if ( isset( $media->like_count ) ) { |
| 228 |
$feed['images'][ $key ]['liked_count'] = (int) $media->like_count; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
// Number of images. |
| 233 |
if ( isset( $ins_params['number'] ) && intval( $ins_params['number'] ) > 0 ) { |
| 234 |
$feed['images'] = array_slice( $feed['images'], 0, $ins_params['number'], true ); |
| 235 |
} |
| 236 |
|
| 237 |
return $feed; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Get data (from html) of instagram response |
| 242 |
* |
| 243 |
* @param string $ins_request Instagram request. |
| 244 |
* @param array $ins_params Options. |
| 245 |
*/ |
| 246 |
function powerkit_instagram_html_get_feed( $ins_request, $ins_params ) { |
| 247 |
|
| 248 |
$ins_response = wp_remote_retrieve_body( $ins_request ); |
| 249 |
|
| 250 |
$feed = array(); |
| 251 |
|
| 252 |
// Get the serialized data string present in the page script. |
| 253 |
preg_match( '/window\._sharedData = (.*);<\/script>/', $ins_response, $ins_data ); |
| 254 |
|
| 255 |
$ins_data_full = array_shift( $ins_data ); |
| 256 |
$ins_data_json = array_shift( $ins_data ); |
| 257 |
|
| 258 |
if ( ! $ins_data_json ) { |
| 259 |
return sprintf( '%s <a href="%s">%s</a> %s', esc_html__( 'Instagram html data cannot be retrieved. Please try adding the ', 'powerkit' ), powerkit_get_page_url( 'connect&tab=instagram' ), esc_html__( 'Instagram Settings', 'powerkit' ), esc_html__( ' on the settings page.', 'powerkit' ) ); |
| 260 |
} |
| 261 |
|
| 262 |
$instagram_json = json_decode( $ins_data_json, true ); |
| 263 |
|
| 264 |
// Try to decode the json. |
| 265 |
if ( null === $instagram_json && JSON_ERROR_NONE !== json_last_error() ) { |
| 266 |
return sprintf( '%s <a href="%s">%s</a> %s', esc_html__( 'Error decoding the instagram json. Please try adding the ', 'powerkit' ), powerkit_get_page_url( 'connect&tab=instagram' ), esc_html__( 'Instagram Token', 'powerkit' ), esc_html__( ' on the settings page.', 'powerkit' ) ); |
| 267 |
} |
| 268 |
|
| 269 |
// Current instagram data is not set. |
| 270 |
if ( ! isset( $instagram_json['entry_data']['ProfilePage'][0]['graphql']['user'] ) ) { |
| 271 |
return sprintf( '%s <a href="%s">%s</a> %s', esc_html__( 'Instagram data is not set, please check the ID. Please try adding the ', 'powerkit' ), powerkit_get_page_url( 'connect&tab=instagram' ), esc_html__( 'Instagram Settings', 'powerkit' ), esc_html__( ' on the settings page.', 'powerkit' ) ); |
| 272 |
} |
| 273 |
|
| 274 |
$user_data = $instagram_json['entry_data']['ProfilePage'][0]['graphql']['user']; |
| 275 |
|
| 276 |
// Images not found. |
| 277 |
if ( ! isset( $user_data['edge_owner_to_timeline_media']['edges'] ) ) { |
| 278 |
return esc_html__( 'There are no images in your account.', 'powerkit' ); |
| 279 |
} |
| 280 |
|
| 281 |
/* ------------- PARSE DATA -------------- */ |
| 282 |
|
| 283 |
// Full name. |
| 284 |
$feed['name'] = null; |
| 285 |
|
| 286 |
if ( isset( $user_data['full_name'] ) ) { |
| 287 |
$feed['name'] = $user_data['full_name']; |
| 288 |
} |
| 289 |
|
| 290 |
// Username. |
| 291 |
$feed['username'] = null; |
| 292 |
|
| 293 |
if ( isset( $user_data['username'] ) ) { |
| 294 |
$feed['username'] = $user_data['username']; |
| 295 |
} |
| 296 |
|
| 297 |
// Liked count. |
| 298 |
$feed['following'] = null; |
| 299 |
|
| 300 |
if ( isset( $user_data['edge_follow']['count'] ) ) { |
| 301 |
$feed['following'] = (int) $user_data['edge_follow']['count']; |
| 302 |
} |
| 303 |
|
| 304 |
// Liked count. |
| 305 |
$feed['followers'] = null; |
| 306 |
|
| 307 |
if ( isset( $user_data['edge_followed_by']['count'] ) ) { |
| 308 |
$feed['followers'] = (int) $user_data['edge_followed_by']['count']; |
| 309 |
} |
| 310 |
|
| 311 |
// Avatar x1. |
| 312 |
$feed['avatar_1x'] = null; |
| 313 |
|
| 314 |
if ( isset( $user_data['profile_pic_url'] ) ) { |
| 315 |
$feed['avatar_1x'] = $user_data['profile_pic_url']; |
| 316 |
} |
| 317 |
|
| 318 |
// Avatar x2. |
| 319 |
$feed['avatar_2x'] = null; |
| 320 |
|
| 321 |
if ( isset( $user_data['profile_pic_url_hd'] ) ) { |
| 322 |
$feed['avatar_2x'] = $user_data['profile_pic_url_hd']; |
| 323 |
} |
| 324 |
|
| 325 |
// Images. |
| 326 |
$edges = $user_data['edge_owner_to_timeline_media']['edges']; |
| 327 |
|
| 328 |
$feed['images'] = array(); |
| 329 |
|
| 330 |
foreach ( $edges as $key => $edge ) { |
| 331 |
if ( ! isset( $edge['node']['thumbnail_resources'] ) && $edge['node']['thumbnail_resources'] ) { |
| 332 |
continue; |
| 333 |
} |
| 334 |
|
| 335 |
// Resources. |
| 336 |
$resources = $edge['node']['thumbnail_resources']; |
| 337 |
|
| 338 |
// Get src thumbnail. |
| 339 |
foreach ( $resources as $resource ) { |
| 340 |
$feed['images'][ $key ]['sizes']['thumbnail'] = isset( $resource['src'] ) ? $resource['src'] : null; |
| 341 |
if ( isset( $resource['config_width'] ) && 150 === (int) $resource['config_width'] ) { |
| 342 |
break; |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
// Get src small. |
| 347 |
foreach ( $resources as $resource ) { |
| 348 |
$feed['images'][ $key ]['sizes']['small'] = isset( $resource['src'] ) ? $resource['src'] : null; |
| 349 |
if ( isset( $resource['config_width'] ) && 320 === (int) $resource['config_width'] ) { |
| 350 |
break; |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
// Get src large. |
| 355 |
foreach ( $resources as $resource ) { |
| 356 |
$feed['images'][ $key ]['sizes']['large'] = isset( $resource['src'] ) ? $resource['src'] : null; |
| 357 |
if ( isset( $resource['config_width'] ) && 640 === (int) $resource['config_width'] ) { |
| 358 |
break; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
// Item link. |
| 363 |
$feed['images'][ $key ]['link'] = null; |
| 364 |
|
| 365 |
if ( isset( $edge['node']['shortcode'] ) ) { |
| 366 |
$feed['images'][ $key ]['link'] = sprintf( 'https://www.instagram.com/p/%s', $edge['node']['shortcode'] ); |
| 367 |
} |
| 368 |
|
| 369 |
// Desc. |
| 370 |
$feed['images'][ $key ]['text'] = null; |
| 371 |
|
| 372 |
if ( isset( $edge['node']['edge_media_to_caption']['edges'][0]['node']['text'] ) ) { |
| 373 |
$text = strtok( $edge['node']['edge_media_to_caption']['edges'][0]['node']['text'], "\n" ); |
| 374 |
|
| 375 |
$feed['images'][ $key ]['text'] = strip_tags( $text ); |
| 376 |
} |
| 377 |
|
| 378 |
// Timestamp. |
| 379 |
$feed['images'][ $key ]['timestamp'] = null; |
| 380 |
|
| 381 |
if ( isset( $edge['node']['taken_at_timestamp'] ) ) { |
| 382 |
$feed['images'][ $key ]['timestamp'] = powerkit_relative_time( $edge['node']['taken_at_timestamp'] ); |
| 383 |
} |
| 384 |
|
| 385 |
// Comment count. |
| 386 |
$feed['images'][ $key ]['comment_count'] = null; |
| 387 |
|
| 388 |
if ( isset( $edge['node']['edge_media_to_comment']['count'] ) ) { |
| 389 |
$feed['images'][ $key ]['comment_count'] = (int) $edge['node']['edge_media_to_comment']['count']; |
| 390 |
} |
| 391 |
|
| 392 |
// Liked count. |
| 393 |
$feed['images'][ $key ]['liked_count'] = null; |
| 394 |
|
| 395 |
if ( isset( $edge['node']['edge_liked_by']['count'] ) ) { |
| 396 |
$feed['images'][ $key ]['liked_count'] = (int) $edge['node']['edge_liked_by']['count']; |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
// Number of images. |
| 401 |
if ( isset( $ins_params['number'] ) && intval( $ins_params['number'] ) > 0 ) { |
| 402 |
$feed['images'] = array_slice( $feed['images'], 0, $ins_params['number'], true ); |
| 403 |
} |
| 404 |
|
| 405 |
return $feed; |
| 406 |
} |
| 407 |
|
| 408 |
/** |
| 409 |
* Get data of instagram response |
| 410 |
* |
| 411 |
* @param string $ins_request Instagram request. |
| 412 |
* @param array $ins_params Options. |
| 413 |
*/ |
| 414 |
function powerkit_instagram_get_feed( $ins_request, $ins_params ) { |
| 415 |
if ( powerkit_connect( 'instagram_app_access_token' ) ) { |
| 416 |
$feed = powerkit_instagram_json_get_feed( $ins_request, $ins_params ); |
| 417 |
} else { |
| 418 |
$feed = powerkit_instagram_html_get_feed( $ins_request, $ins_params ); |
| 419 |
} |
| 420 |
|
| 421 |
/* Manual Override */ |
| 422 |
|
| 423 |
if ( is_array( $feed ) && get_option( 'powerkit_connect_instagram_custom_name' ) ) { |
| 424 |
$feed['name'] = get_option( 'powerkit_connect_instagram_custom_name' ); |
| 425 |
} |
| 426 |
|
| 427 |
if ( is_array( $feed ) && get_option( 'powerkit_connect_instagram_following' ) ) { |
| 428 |
$feed['following'] = (int) get_option( 'powerkit_connect_instagram_following' ); |
| 429 |
} |
| 430 |
|
| 431 |
if ( is_array( $feed ) && get_option( 'powerkit_connect_instagram_custom_followers' ) ) { |
| 432 |
$feed['followers'] = (int) get_option( 'powerkit_connect_instagram_custom_followers' ); |
| 433 |
} |
| 434 |
|
| 435 |
if ( is_array( $feed ) && get_option( 'powerkit_connect_instagram_custom_avatar' ) ) { |
| 436 |
$feed['avatar_1x'] = get_option( 'powerkit_connect_instagram_custom_avatar' ); |
| 437 |
} |
| 438 |
|
| 439 |
if ( is_array( $feed ) && get_option( 'powerkit_connect_instagram_custom_avatar' ) ) { |
| 440 |
$feed['avatar_2x'] = get_option( 'powerkit_connect_instagram_custom_avatar' ); |
| 441 |
} |
| 442 |
|
| 443 |
return $feed; |
| 444 |
} |
| 445 |
|
| 446 |
/** |
| 447 |
* Get recent photos instagram |
| 448 |
* |
| 449 |
* @param array $params Recent options. |
| 450 |
* @param string $cache_name The cache name. |
| 451 |
*/ |
| 452 |
function powerkit_instagram_get_recent( $params, $cache_name = 'powerkit_instagram_data' ) { |
| 453 |
|
| 454 |
$params = array_merge( array( |
| 455 |
'user_id' => '', |
| 456 |
'header' => false, |
| 457 |
'button' => false, |
| 458 |
'number' => 4, |
| 459 |
'columns' => 1, |
| 460 |
'size' => 'small', |
| 461 |
'target' => '_blank', |
| 462 |
'template' => 'default', |
| 463 |
'cache_time' => 720, |
| 464 |
), (array) $params ); |
| 465 |
|
| 466 |
$cache_time = (int) $params['cache_time']; |
| 467 |
|
| 468 |
// Instagram trans name. |
| 469 |
$trans_name = $cache_name . '_' . md5( maybe_serialize( $params ) . powerkit_connect( 'instagram_app_access_token' ) ); |
| 470 |
|
| 471 |
// Instagram request. |
| 472 |
$ins_request = get_transient( $trans_name ); |
| 473 |
|
| 474 |
if ( false === $ins_request || 0 === $cache_time ) { |
| 475 |
|
| 476 |
Powerkit_Connect::access_token_refresh( 'instagram' ); |
| 477 |
|
| 478 |
if ( powerkit_connect( 'instagram_app_access_token' ) && powerkit_connect( 'instagram_app_user_id' ) ) { |
| 479 |
$access_token = powerkit_connect( 'instagram_app_access_token' ); |
| 480 |
$user_id = powerkit_connect( 'instagram_app_user_id' ); |
| 481 |
|
| 482 |
// Get information about. |
| 483 |
if ( 'business' === powerkit_connect( 'instagram_app_type' ) ) { |
| 484 |
$ins_link = add_query_arg( array( |
| 485 |
'fields' => 'biography,id,username,website,followers_count,media_count,profile_picture_url,name', |
| 486 |
'access_token' => $access_token, |
| 487 |
), 'https://graph.facebook.com/' . $user_id ); |
| 488 |
} else { |
| 489 |
$ins_link = add_query_arg( array( |
| 490 |
'fields' => 'id,username,media_count', |
| 491 |
'access_token' => $access_token, |
| 492 |
), 'https://graph.instagram.com/me' ); |
| 493 |
} |
| 494 |
|
| 495 |
$ins_request = wp_safe_remote_get( $ins_link, |
| 496 |
array( |
| 497 |
'timeout' => 120, |
| 498 |
'httpversion' => '1.1', |
| 499 |
'redirection' => 10, |
| 500 |
'sslverify' => false, |
| 501 |
) |
| 502 |
); |
| 503 |
|
| 504 |
usleep( 100000 ); |
| 505 |
|
| 506 |
// Get the most recent media published. |
| 507 |
if ( 'business' === powerkit_connect( 'instagram_app_type' ) ) { |
| 508 |
$ins_recent_link = add_query_arg( array( |
| 509 |
'fields' => 'media_url,thumbnail_url,caption,id,media_type,timestamp,username,comments_count,like_count,permalink', |
| 510 |
'limit' => min( $params['number'], 200 ), |
| 511 |
'access_token' => $access_token, |
| 512 |
), 'https://graph.facebook.com/' . $user_id . '/media' ); |
| 513 |
} else { |
| 514 |
$ins_recent_link = add_query_arg( array( |
| 515 |
'fields' => 'media_url,thumbnail_url,caption,id,media_type,timestamp,username,comments_count,like_count,permalink', |
| 516 |
'limit' => min( $params['number'], 200 ), |
| 517 |
'access_token' => $access_token, |
| 518 |
), 'https://graph.instagram.com/' . $user_id . '/media' ); |
| 519 |
} |
| 520 |
|
| 521 |
$ins_recent_request = wp_safe_remote_get( $ins_recent_link, |
| 522 |
array( |
| 523 |
'timeout' => 120, |
| 524 |
'httpversion' => '1.1', |
| 525 |
'redirection' => 10, |
| 526 |
'sslverify' => false, |
| 527 |
) |
| 528 |
); |
| 529 |
|
| 530 |
$ins_request['headers']['recent'] = $ins_recent_request; |
| 531 |
} else { |
| 532 |
|
| 533 |
$ins_link = sprintf( 'https://www.instagram.com/%s', $params['user_id'] ); |
| 534 |
|
| 535 |
$ins_request = wp_safe_remote_get( $ins_link, |
| 536 |
array( |
| 537 |
'timeout' => 120, |
| 538 |
'httpversion' => '1.1', |
| 539 |
'redirection' => 10, |
| 540 |
'sslverify' => false, |
| 541 |
) |
| 542 |
); |
| 543 |
} |
| 544 |
|
| 545 |
set_transient( $trans_name, $ins_request, 60 * $cache_time ); |
| 546 |
} |
| 547 |
|
| 548 |
// Instagram response. |
| 549 |
if ( ! is_wp_error( $ins_request ) ) { |
| 550 |
|
| 551 |
$instagram_feed = powerkit_instagram_get_feed( $ins_request, $params ); |
| 552 |
|
| 553 |
if ( is_string( $instagram_feed ) ) { |
| 554 |
|
| 555 |
powerkit_alert_warning( $instagram_feed ); |
| 556 |
|
| 557 |
} else { |
| 558 |
$instagram = array(); |
| 559 |
|
| 560 |
$placeholder_image = apply_filters( 'powerkit_lazyload_instagram_output', false ); |
| 561 |
|
| 562 |
$ins_feed_class = null; |
| 563 |
|
| 564 |
$ins_feed_class .= ' pk-instagram-template-' . $params['template']; |
| 565 |
$ins_feed_class .= ' pk-instagram-size-' . $params['size']; |
| 566 |
$ins_feed_class .= ' pk-instagram-columns-' . $params['columns']; |
| 567 |
?> |
| 568 |
<div class="pk-instagram-feed <?php echo esc_attr( $ins_feed_class ); ?>"> |
| 569 |
<?php |
| 570 |
foreach ( $instagram_feed['images'] as $item ) { |
| 571 |
|
| 572 |
$item = apply_filters( 'powerkit_instagram_item_data', $item ); |
| 573 |
|
| 574 |
$class = 'pk-instagram-image'; |
| 575 |
|
| 576 |
$image_thumbnail = $item['sizes']['thumbnail']; |
| 577 |
$image_small = $item['sizes']['small']; |
| 578 |
$image_large = $item['sizes']['large']; |
| 579 |
|
| 580 |
// Image Resolution. |
| 581 |
if ( 'thumbnail' === $params['size'] ) { |
| 582 |
$user_image = $image_thumbnail; |
| 583 |
} elseif ( 'small' === $params['size'] ) { |
| 584 |
$user_image = $image_small; |
| 585 |
} else { |
| 586 |
$user_image = $image_large; |
| 587 |
} |
| 588 |
|
| 589 |
// Columns. |
| 590 |
if ( 3 === (int) $params['columns'] ) { |
| 591 |
$user_image = $image_thumbnail; |
| 592 |
} elseif ( 2 === (int) $params['columns'] ) { |
| 593 |
$user_image = $image_small; |
| 594 |
} elseif ( 1 === (int) $params['columns'] ) { |
| 595 |
$user_image = $image_small; |
| 596 |
} |
| 597 |
|
| 598 |
// Retina sizes. |
| 599 |
if ( 'auto' === $params['size'] ) { |
| 600 |
if ( 3 === (int) $params['columns'] ) { |
| 601 |
$width = 150; |
| 602 |
} elseif ( 2 === (int) $params['columns'] ) { |
| 603 |
$width = 320; |
| 604 |
} elseif ( 1 === (int) $params['columns'] ) { |
| 605 |
$width = 640; |
| 606 |
} |
| 607 |
} elseif ( 'small' === $params['size'] ) { |
| 608 |
$width = 320; |
| 609 |
} elseif ( 'thumbnail' === $params['size'] ) { |
| 610 |
$width = 150; |
| 611 |
} else { |
| 612 |
$width = 640; |
| 613 |
} |
| 614 |
|
| 615 |
// Placeholder image. |
| 616 |
if ( $placeholder_image ) { |
| 617 |
$class .= ' pk-lazyload'; |
| 618 |
} |
| 619 |
|
| 620 |
// Instagram item. |
| 621 |
$instagram[] = array( |
| 622 |
'class' => $class, |
| 623 |
'description' => $item['text'], |
| 624 |
'link' => $item['link'], |
| 625 |
'user_link' => $item['link'], |
| 626 |
'comments' => $item['comment_count'], |
| 627 |
'likes' => $item['liked_count'], |
| 628 |
'time' => $item['timestamp'], |
| 629 |
'thumbnail' => $image_thumbnail, |
| 630 |
'small' => $image_small, |
| 631 |
'large' => $image_large, |
| 632 |
'user_image' => $placeholder_image ? $placeholder_image : $user_image, |
| 633 |
'sizes' => $placeholder_image ? 'auto' : sprintf( '(max-width: %1$spx) 100vw, %1$spx', $width ), |
| 634 |
'srcset' => sprintf( '%s 150w, %s 320w, %s 640w', $image_thumbnail, $image_small, $image_large ), |
| 635 |
); |
| 636 |
} |
| 637 |
|
| 638 |
ob_start(); |
| 639 |
|
| 640 |
powerkit_instagram_template_handler( $params['template'], $instagram_feed, $instagram, $params ); |
| 641 |
|
| 642 |
$template_html = ob_get_clean(); |
| 643 |
|
| 644 |
// Placeholder adaptation. |
| 645 |
if ( $placeholder_image ) { |
| 646 |
preg_match( '/<div class="pk-instagram-items">.*?<\/div>/msU', $template_html, $template_match ); |
| 647 |
|
| 648 |
if ( $template_match ) { |
| 649 |
$template_items = array_shift( $template_match ); |
| 650 |
|
| 651 |
$output_items = str_replace( 'src=', sprintf( 'data-pk-src="%s" src=', $user_image ), $template_items ); |
| 652 |
$output_items = str_replace( 'srcset=', 'data-pk-srcset=', $output_items ); |
| 653 |
$output_items = str_replace( 'sizes=', 'data-pk-sizes=', $output_items ); |
| 654 |
|
| 655 |
$template_html = str_replace( $template_items, $output_items, $template_html ); |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
// Template Output. |
| 660 |
call_user_func( 'printf', '%s', $template_html ); |
| 661 |
?> |
| 662 |
</div> |
| 663 |
<?php |
| 664 |
} |
| 665 |
} else { |
| 666 |
powerkit_alert_warning( esc_html__( 'This client has not been approved to access this resource.', 'powerkit' ) ); |
| 667 |
} |
| 668 |
} |
| 669 |
|