Created
February 8, 2019 16:35
-
-
Save taciara/653fc07dd2b3f07e0bf2f23222dcb72f to your computer and use it in GitHub Desktop.
Integração Instagram
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php PRECISO QUE TENHA:FOTO/NUMERO DE CURTIDA / NUMERO DE COMENTARIO / DESCRICAO ?> | |
<?php | |
//SHORTCODE INSTAGRAM | |
add_filter( 'wpis_default_shortcode_attributes', 'custom_wpis_instagram_access_token' ); | |
function custom_wpis_instagram_access_token ( $atts ) { | |
$atts['token'] = '2195092261.1677ed0.f7e6b044cdc54b859d0f9413e0e404c6'; | |
return $atts; | |
} | |
add_shortcode( 'instagram_photos', 'wpis_instagram_photos_callback' ); | |
function wpis_instagram_photos_callback ( $atts ) { | |
$transient_key = 'wpis_output_cache'; | |
$base_url = 'https://api.instagram.com/v1'; | |
$defaults = apply_filters( 'wpis_default_shortcode_attributes', array( | |
'endpoint' => '/users/self/media/recent/', | |
'token' => '', | |
'cache' => 30 * MINUTE_IN_SECONDS, | |
'limit' => false, | |
'class' => '' | |
) ); | |
$atts = shortcode_atts( $defaults, $atts, 'instagram_photos' ); | |
$args = array( | |
'access_token' => $atts['token'], | |
'count' => $atts['limit'] | |
); | |
$url = esc_url_raw( add_query_arg( $args, $base_url . $atts['endpoint'] ) ); | |
if ( ! empty( $atts['cache'] ) ) { | |
$last_url = get_transient( 'wpis_last_url' ); | |
if ( ( defined('WP_DEBUG') && WP_DEBUG ) || $last_url != $url ) { | |
$atts['cache'] = false; | |
} | |
} | |
set_transient( 'wpis_last_url', $url, 12 * HOUR_IN_SECONDS ); | |
$response = wp_safe_remote_get( $url, array( 'timeout' => 30 ) ); | |
$output = ''; | |
if ( is_wp_error( $response ) ) { | |
$output .= '<pre class="wpis_error">' . $response->get_error_message() . '</pre>'; | |
} else { | |
$json = json_decode( $response['body'], true ); | |
if ( $json['meta']['code'] >= 400 ) { | |
$output .= '<pre class="wpis_error">' . $json['meta']['error_message'] . '</pre>'; | |
} else { | |
$cache = false; | |
if ( empty( $atts['cache'] ) ) { | |
delete_transient( 'wpis_output_cache' ); | |
} else { | |
$cache = get_transient( 'wpis_output_cache' ); | |
} | |
$output .= '<div class="wpis_photos ' . $atts['class'] . '">'; | |
if ( $cache == false ) { | |
$data = $json['data']; | |
foreach( $data as $photo ) { | |
$output .= '<div class="wpis_image item">'; | |
$output .= '<a rel="nofollow" href="' . $photo['link'] . '" target="_blank">'; | |
$output .= '<img '; | |
$output .= 'src="' . $photo['images']['standard_resolution']['url'] . '" '; | |
$output .= 'width="' . $photo['images']['standard_resolution']['width'] . '" '; | |
$output .= 'height="' . $photo['images']['standard_resolution']['height'] . '" '; | |
if ( ! empty( $photo['caption'] ) ) { | |
$output .= 'alt="' . $photo['caption']['text'] . '" '; | |
} | |
$output .= '></a></div>'; | |
} | |
if ( ! empty( $atts['cache'] ) ) { | |
set_transient( 'wpis_output_cache', $output, $atts['cache'] ); | |
} | |
} else { | |
$output .= $cache; | |
} | |
$output .= '</div>'; | |
} | |
} | |
return $output; | |
} | |
//SHORTCODE INSTAGRAM | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment