Skip to content

Instantly share code, notes, and snippets.

@jpneey
Last active July 7, 2023 09:25
Show Gist options
  • Save jpneey/7ad30a5d3bf0557bdbd889e85e74ac31 to your computer and use it in GitHub Desktop.
Save jpneey/7ad30a5d3bf0557bdbd889e85e74ac31 to your computer and use it in GitHub Desktop.
Fetch Smash balloon feed assets as an array
<?php
/**
* Template usage
* --- ACF ---
* $data = get_sbi_instagram_feed( get_field("feed_id") );
* --- Manual ---
* $data = get_sbi_instagram_feed( 1 );
*/
if ( class_exists( 'SB_Instagram_Feed' ) ) {
// deregister smash balloon assets
add_action( 'wp_enqueue_scripts', 'echo_sbi_optimiser', 11 );
function echo_sbi_optimiser(){
wp_dequeue_style( 'sbi_styles' );
wp_dequeue_script( 'sbi_scripts' );
}
/**
* Helper function to retrieve media as an array for an SBI instagram feed
* @param int $feed_id Feed ID
* @return array An array of fetched media (video & image)
*/
function get_sbi_instagram_feed( $feed_id, $preview_settings = false ) {
$atts = array( "feed" => $feed_id ); // some class requires the feed id to be passed like this
$database_settings = sbi_get_database_settings();
$instagram_feed_settings = new SB_Instagram_Settings( $atts, $database_settings, $preview_settings );
$connected_accounts = $instagram_feed_settings->get_connected_accounts();
if ( $connected_accounts === null ) {
// Feed is empty and stale.
// Early return to prevent script below from emitting warnings
return array();
}
$instagram_feed_settings->set_feed_type_and_terms();
$instagram_feed_settings->set_transient_name();
$transient_name = $instagram_feed_settings->get_transient_name();
$settings = $instagram_feed_settings->get_settings();
$feed_type_and_terms = $instagram_feed_settings->get_feed_type_and_terms();
$instagram_feed = new SB_Instagram_Feed( $transient_name );
$instagram_feed->set_cache( $instagram_feed_settings->get_cache_time_in_seconds(), $settings );
if ( $settings['caching_type'] === 'background' ) {
$instagram_feed->add_report( 'background caching used' );
if ( $instagram_feed->regular_cache_exists() ) {
$instagram_feed->add_report( 'setting posts from cache' );
$instagram_feed->set_post_data_from_cache();
}
if ( $instagram_feed->need_to_start_cron_job() ) {
$instagram_feed->add_report( 'setting up feed for cron cache' );
$to_cache = array(
'atts' => $atts,
'last_requested' => time(),
);
$instagram_feed->set_cron_cache( $to_cache, $instagram_feed_settings->get_cache_time_in_seconds() );
SB_Instagram_Cron_Updater::do_single_feed_cron_update( $instagram_feed_settings, $to_cache, $atts, false );
$instagram_feed->set_cache( $instagram_feed_settings->get_cache_time_in_seconds(), $settings );
$instagram_feed->set_post_data_from_cache();
} elseif ( $instagram_feed->should_update_last_requested() ) {
$instagram_feed->add_report( 'updating last requested' );
$to_cache = array(
'last_requested' => time(),
);
$instagram_feed->set_cron_cache( $to_cache, $instagram_feed_settings->get_cache_time_in_seconds(), $settings['backup_cache_enabled'] );
}
} elseif ( $instagram_feed->regular_cache_exists() ) {
$instagram_feed->add_report( 'page load caching used and regular cache exists' );
$instagram_feed->set_post_data_from_cache();
if ( $instagram_feed->need_posts( $settings['num'] ) && $instagram_feed->can_get_more_posts() ) {
while ( $instagram_feed->need_posts( $settings['num'] ) && $instagram_feed->can_get_more_posts() ) {
$instagram_feed->add_remote_posts( $settings, $feed_type_and_terms, $instagram_feed_settings->get_connected_accounts_in_feed() );
}
$instagram_feed->cache_feed_data( $instagram_feed_settings->get_cache_time_in_seconds(), $settings['backup_cache_enabled'] );
}
} else {
$instagram_feed->add_report( 'no feed cache found' );
while ( $instagram_feed->need_posts( $settings['num'] ) && $instagram_feed->can_get_more_posts() ) {
$instagram_feed->add_remote_posts( $settings, $feed_type_and_terms, $instagram_feed_settings->get_connected_accounts_in_feed() );
}
if ( ! $instagram_feed->should_use_backup() ) {
$instagram_feed->cache_feed_data( $instagram_feed_settings->get_cache_time_in_seconds(), $settings['backup_cache_enabled'] );
}
}
if ( $instagram_feed->should_use_backup() ) {
$instagram_feed->add_report( 'trying to use backup' );
$instagram_feed->maybe_set_post_data_from_backup();
$instagram_feed->maybe_set_header_data_from_backup();
}
return $instagram_feed->get_post_data();
}
}
if ( ! function_exists( 'get_sbi_instagram_feed' ) ) {
function get_sbi_instagram_feed( $feed_id, $preview_settings ) {
// Throw / log error here. This means that sb plugin is not installed / activated
return array();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment