Last active
November 30, 2023 15:55
-
-
Save geekontheroad/a1418bafef82bfa8b8b262597f53c842 to your computer and use it in GitHub Desktop.
This file contains 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 | |
/** | |
* Snippet For Gravity Perks Google Sheets | |
* | |
* Description: Create a daily spreadsheet for your feed. The spreadsheet will be created after the first entry of that day. | |
* | |
* Instructions: Copy this code to your functions.php and enter your form id and feed ids at the bottom in the configuration section | |
* | |
* @author Johan d'Hollander | |
* @link https://geekontheroad.com | |
*/ | |
class GOTRGPGS_Daily_Spreadsheets { | |
private $_args = array(); | |
public function __construct($args = array()) { | |
// set arguments and store for use throughout the class | |
$this->_args = wp_parse_args($args, array( | |
"form_id" => 0, | |
"feed_ids" => array(), | |
)); | |
add_action('init', array($this, 'init')); | |
} | |
public function init() { | |
add_filter( "gform_gp-google-sheets_pre_process_feeds_" . $this->_args["form_id"], array($this , "maybe_create_new_spreadsheet") , 10 , 3 ); | |
} | |
/** | |
* Loop through all GPGS feeds and maybe create a new spreadsheet for that feed if needed | |
* | |
* @param array $feeds an array of all feeds | |
* @param array $entry current entry for which $feeds is processed | |
* @param array $form current form | |
* | |
*/ | |
public function maybe_create_new_spreadsheet($feeds, $entry, $form) { | |
$form_id = rgar($form , "id"); | |
foreach($feeds as $key => $feed) { | |
$feed_id = rgar($feed , "id"); | |
// only proceed for eligible feeds | |
if( in_array($feed_id , $this->_args["feed_ids"]) && $this->check_if_spreadsheet_created($form_id , $feed_id) == false ) { | |
$feed_name = rgars($feed , "meta/feed_name"); | |
$account_email = rgars($feed , "meta/google_sheet_account"); | |
if( $feed_name && $account_email ) { | |
// create a new spreadsheet for this feed | |
$new_spreadsheet = $this->create_new_spreadsheet( $feed_name , $account_email); | |
if(!$new_spreadsheet) { | |
gp_google_sheets()->log_debug(__METHOD__ . " Error while creating a new spreadsheet." ); | |
continue; | |
} | |
// get the current meta | |
$meta = $feed['meta']; | |
// new spreadsheet url | |
$meta['google_sheet_url'] = $new_spreadsheet->get_url(); | |
// @David. I'm not sure about this. suppose this has to do with creating new sheets? GP seems to not add an Id here but instead the string add. | |
$meta["google_spreadsheet_id"] = "add"; // could possibly also be $new_spreadsheet->get_id(); | |
$meta["google_sheet_id"] = 0; // ensure this is set to the first sheet in the spreadsheet to avoid fatal error | |
$meta["google_sheet_account"] = $account_email; // This seems to fix the account email disappearing from the meta after updating the feed | |
// update the current feeds. This will be returned for processing of the current active feed | |
$feeds[$key]["meta"] = $meta; | |
// update the feed | |
$update = GFAPI::update_feed($feed_id , $meta , $form_id ); | |
// proceed with mapping of the new spreadsheet if the update was success | |
if( is_wp_error($update) ) { | |
gp_google_sheets()->log_debug(__METHOD__ . " Updating feed " . $feed_id . " failed. " . $update->get_error_message() ); | |
continue; | |
} else { | |
// updating feed success so now lets update the column mapping | |
// @David. Is this the proper way of handling this ? I want to ensure future compatibility | |
gp_google_sheets()->update_sheet_after_field_map_change($feed_id , $form_id , $meta , gp_google_sheets() ); | |
// set a transient for today | |
set_transient("gotrgpgs_sheet_created_" . $form_id . "_" . $feed_id . "_" . $this->get_today_date(), true, DAY_IN_SECONDS); | |
} | |
} | |
} | |
// DEVELOPMENT PURPOSES trigger this feed now to get around the sheduler. Remove this in production | |
$feed = GFAPI::get_feed( $feed_id ); // this will always return 1 feed | |
gp_google_sheets()->process_feed( $feed, $entry, $form ); | |
} | |
return $feeds; | |
} | |
/** | |
* check if a transient for todays spreadsheet already exists | |
* | |
* @return bool If the transient does not exist, WP will return false and otherwise the transient will have true as value | |
**/ | |
public function check_if_spreadsheet_created( $form_id , $feed_id ) { | |
return get_transient("gotrgpgs_sheet_created_" . $form_id . "_" . $feed_id . "_" . $this->get_today_date()); | |
} | |
/** | |
* Create a new spreadsheet in google | |
* | |
* @param string $feed_name The current feed name | |
* @param string $account email The current account email | |
* | |
* @return Spreadsheet|Bool $new_spreadsheet A new Spreadsheet instance or false if the GPGS classes do not exist | |
*/ | |
public function create_new_spreadsheet( $feed_name , $account_email) { | |
if (class_exists("\GP_Google_Sheets\Spreadsheets\Spreadsheet") && class_exists("\GP_Google_Sheets\Accounts\Google_Account")) { | |
// get instance of the GPGS spreadsheet class | |
$spreadsheet = new \GP_Google_Sheets\Spreadsheets\Spreadsheet(); | |
// get instance of the GPGS google account class | |
$google_account = '\GP_Google_Sheets\Accounts\Google_Account'; | |
$account = $google_account::from_email( $account_email ); | |
return $spreadsheet->create( $feed_name . "_" . $this->get_today_date() , $account ); | |
} | |
return false; | |
} | |
/** | |
* Get the todays date in format Y-m-d | |
* @return string $date of today | |
*/ | |
public function get_today_date() { | |
return date('Y-m-d'); | |
} | |
} | |
// configuration | |
new GOTRGPGS_Daily_Spreadsheets( array( | |
"form_id" => 20, | |
"feed_ids" => array("7") // array of all the feed ids to apply this to. | |
) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment