Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spivurno/a537b7f217d70cb4004cc4fae1493db4 to your computer and use it in GitHub Desktop.
Save spivurno/a537b7f217d70cb4004cc4fae1493db4 to your computer and use it in GitHub Desktop.
Gravity Perks // GP Notification Scheduler // Credit Card Expiration
<?php
/**
* Gravity Perks // GP Notification Scheduler // Credit Card Expiration
*
* Send notifications based on a credit card expiration date.
*
* @version 0.1
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link http://gravitywiz.com/
*
* Plugin Name: GP Notification Scheduler - Credit Card Expiration
* Plugin URI: http://gravitywiz.com/
* Description: Send notifications based on a credit card expiration date.
* Author: Gravity Wiz
* Version: 0.1
* Author URI: http://gravitywiz.com
*/
class GPNS_CC_Expiration {
public function __construct( $args = array() ) {
// set our default arguments, parse against the provided arguments, and store for use throughout the class
$this->_args = wp_parse_args( $args, array(
'form_id' => false,
'date_field_id' => false
) );
// do version check in the init to make sure if GF is going to be loaded, it is already loaded
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
// make sure we're running the required minimum version of Gravity Forms
if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) ) {
return;
}
add_action( 'gform_pre_submission', array( $this, 'populate_date_field' ) );
}
public function populate_date_field( $form ) {
if( ! $this->is_applicable_form( $form ) ) {
return;
}
$cc_field = GFFormsModel::get_fields_by_type( $form, array( 'creditcard' ) );
if( empty( $cc_field ) ) {
return;
}
$date_field = GFFormsModel::get_field( $form, $this->_args['date_field_id'] );
$value = rgpost( "input_{$cc_field[0]->id}_2" ); // Format: array( 1, 2018 ).
$timestamp = strtotime( sprintf( '%d/%d/1', $value[1], $value[0] ) );
$_POST[ "input_{$this->_args['date_field_id']}" ] = $this->format_by_date_field( $timestamp, $date_field );
}
public function format_by_date_field( $timestamp, $field ) {
list( $format, $divider ) = $field['dateFormat'] ? array_pad( explode( '_', $field['dateFormat' ] ), 2, 'slash' ) : array( 'mdy', 'slash' );
$dividers = array( 'slash' => '/', 'dot' => '.', 'dash' => '-' );
$format = str_replace( 'y', 'Y', $format );
$divider = $dividers[ $divider ];
$format = implode( $divider, str_split( $format ) );
return date( $format, $timestamp );
}
public function is_applicable_form( $form ) {
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
return empty( $this->_args['form_id'] ) || $form_id == $this->_args['form_id'];
}
}
# Configuration
new GPNS_CC_Expiration( array(
'form_id' => 1787,
'date_field_id' => 3
) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment