array( $this, 'render_callback' ), ) ); getwid_maybe_add_option( 'getwid_instagram_token', '', true ); } public function get_label() { return __( 'Instagram', 'getwid' ); } public function check_instagram_token() { check_ajax_referer( 'getwid_nonce_check_instagram_token', 'nonce' ); $response = (bool) get_option( 'getwid_instagram_token', '' ); wp_send_json_success( $response ); } public function render_callback( $attributes, $content ) { $attributes = wp_parse_args( $attributes, array( 'photoCount' => 6, 'gridColumns' => 3, 'spacing' => 'default', ) ); $encryption = new StringEncryption(); $access_token = $encryption->decrypt( get_option( 'getwid_instagram_token', '' ) ); if ( empty( $access_token ) ) { if ( current_user_can( 'manage_options' ) ) { return '

' . sprintf( // translators: %s is a link. __( 'Instagram Access Token is not set. Connect Instagram Account.', 'getwid' ), esc_url( getwid()->settingsPage()->getTabUrl( 'general' ) ) ) . '

'; } return ''; } $instagram_media = get_transient( 'getwid_instagram_response_data' ); if ( false === $instagram_media ) { $api_uri = 'https://graph.instagram.com/me/media?fields=id,media_type,media_url,permalink,caption,thumbnail_url&access_token=' . $access_token . '&limit=100'; $response = wp_remote_get( $api_uri, array( 'timeout' => 15 ) ); if ( is_wp_error( $response ) ) { if ( current_user_can( 'manage_options' ) ) { return '

' . esc_html( $response->get_error_message() ) . '

'; } return ''; } $instagram_media = json_decode( wp_remote_retrieve_body( $response ), false ); if ( JSON_ERROR_NONE === json_last_error() ) { if ( isset( $instagram_media->data ) ) { $expiration = intval( get_option( 'getwid_instagram_cache_timeout', 30 ) ); set_transient( 'getwid_instagram_response_data', $instagram_media, $expiration * MINUTE_IN_SECONDS ); } elseif ( current_user_can( 'manage_options' ) ) { return '

' . esc_html( $instagram_media->error->message ) . '

'; } else { return ''; } } else { return __( 'Error in json_decode.', 'getwid' ); } } $class = 'wp-block-getwid-instagram'; $block_name = 'wp-block-getwid-instagram'; if ( isset( $attributes['align'] ) ) { $class .= ' align' . $attributes['align']; } $wrapper_class = 'wp-block-getwid-instagram__wrapper'; $wrapper_class .= ' has-' . $attributes['gridColumns'] . '-columns'; if ( isset( $attributes['spacing'] ) && 'default' !== $attributes['spacing'] ) { $class .= ' has-spacing-' . $attributes['spacing']; } if ( isset( $attributes['className'] ) ) { $class .= ' ' . $attributes['className']; } ob_start(); ?>
data as $value ) { if ( $counter <= $attributes['photoCount'] ) { $extra_attr = array( 'block_name' => $block_name, 'post' => $value, ); getwid_get_template_part( 'instagram/post', $attributes, false, $extra_attr ); } ++$counter; } ?>
blocksManager()->addBlock( new Instagram() );