Last active
September 22, 2022 13:27
-
-
Save rohmann/4bc926fadb11669d89edd0da96dec87a to your computer and use it in GitHub Desktop.
Pro / Cornerstone Custom Loopers Example
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 | |
// NOTE: Unfortunately, this code no longer works because Google has shut down | |
// their V3 API for Sheets. We used that at the time because it didn't require | |
// additional setup, nor creating an API key unlike the V4 API. | |
// This is the new URL syntax, and it requires an API Key | |
// https://sheets.googleapis.com/v4/spreadsheets/SHEET_ID/values/NAME_OF_SHEET_TAB?alt=json&key=API_KEY | |
// It isn't compatible with the code below, so you sill still need to write custom PHP to reformat the JSON | |
// ---------------------------------------------------------------------------------------------------- | |
// Google V3 API Acccess (no longer available) | |
// https://spreadsheets.google.com/feeds/cells/*****/1/public/values?alt=json | |
// Replace ***** with the ID of your google sheet which you can copy from the address bar | |
add_filter( 'cs_looper_custom_shuffle', function( $result, $args ) { | |
shuffle( $args ); | |
return $args; | |
}, 10, 2); | |
add_filter( 'cs_looper_custom_post_query', function( $result, $args ) { | |
return get_posts( $args ); | |
}, 10, 2); | |
add_filter( 'cs_looper_custom_http', function( $result, $args ) { | |
$url = isset( $args['url'] ) ? $args['url'] : null; | |
if ( $url ) { | |
$cache_key = 'custom_http_cache_' . md5( $url); | |
$cached_data = get_transient( $cache_key ); | |
if ( $cached_data === false ) { | |
$request = wp_remote_get( $url ); | |
if ( ! is_wp_error( $request ) ) { | |
$response = json_decode( wp_remote_retrieve_body( $request ), true ); | |
if ( is_array( $response ) ) { | |
$result = $response; | |
$ttl = max( 1, isset( $args['ttl'] ) ? $args['ttl'] : 1 ); | |
set_transient( $cache_key, $result, $ttl * MINUTE_IN_SECONDS ); | |
} | |
} | |
} else { | |
$result = $cached_data; | |
} | |
} | |
return $result; | |
}, 10, 2); | |
function map_google_sheet( $input, $args ) { | |
$row_width = max(1, isset( $args['row_width'] ) ? $args['row_width'] : 1); | |
$cells = array_map( function( $cell ) { | |
return $cell['content']['$t']; | |
}, $input['feed']['entry']); | |
$rows = []; | |
$keys = array_splice( $cells, 0, $row_width ); | |
while (count($cells) > 0) { | |
$row = []; | |
foreach ($keys as $key) { | |
$row[sanitize_title( $key )] = array_shift( $cells ); | |
} | |
$rows[] = $row; | |
} | |
return $rows; | |
} | |
add_filter( 'cs_looper_custom_http', function( $result, $args ) { | |
if ( isset( $args['transform'] ) ) { | |
switch ( $args['transform'] ) { | |
case 'map_google_sheet': | |
$result = map_google_sheet( $result, $args ); | |
break; | |
} | |
} | |
return $result; | |
}, 20, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment