Last active
October 16, 2015 11:36
-
-
Save 0v3rth3d4wn/4f2ee73a2c64141390a7 to your computer and use it in GitHub Desktop.
Enable Editing of the Entry Payment Details
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
{ | |
"name": "wptailor/gravityforms-edit-entry-payment-details", | |
"description": "Enable the display of the payment details panel on the entry detail page and allow editing of those details for entries which were not processed by a payment add-on.", | |
"type": "wordpress-plugin", | |
"license": "GPL3+", | |
"authors": [ | |
{ | |
"name": "Ivo Karadzhov", | |
"email": "[email protected]" | |
} | |
], | |
"require": {} | |
} |
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 | |
/* | |
Plugin Name: RW GF Edit Payment Details | |
Description: Enable the display of the payment details panel on the entry detail page and allow editing of those details for entries which were not processed by a payment add-on. | |
Plugin URI: https://gist.github.com/4f2ee73a2c64141390a7.git | |
Author: WPTailor | |
Author URI: http://wptailor.com/ | |
Version: 0.1 | |
License: GPL2 | |
*/ | |
defined('ABSPATH') or exit; | |
class RW_GF_Edit_Payment_Details { | |
public function __construct( $form_ids = array() ) { | |
if ( ! empty( $form_ids ) ) { | |
$this->_form_ids = $form_ids; | |
if( did_action( 'gform_loaded' ) ) { | |
$this->init(); | |
} else { | |
add_action( 'gform_loaded', array( $this, 'init' ) ); | |
} | |
} | |
} | |
function init() { | |
if ( ! property_exists( 'GFForms', 'version' ) || ! version_compare( GFForms::$version, '1.9', '>=' ) ) { | |
return; | |
} | |
//add actions to allow the payment details to be modified | |
add_action( 'gform_payment_status', array( $this, 'admin_edit_payment_status' ), 10, 3 ); | |
add_action( 'gform_payment_date', array( $this, 'admin_edit_payment_date' ), 10, 3 ); | |
add_action( 'gform_payment_transaction_id', array( $this, 'admin_edit_payment_transaction_id' ), 10, 3 ); | |
add_action( 'gform_payment_amount', array( $this, 'admin_edit_payment_amount' ), 10, 3 ); | |
add_action( 'gform_after_update_entry', array( $this, 'admin_update_payment' ), 10, 2 ); | |
add_action( 'gform_entries_first_column_actions', array( $this, 'maybe_set_payment_status' ), 10, 4 ); | |
add_filter( 'gform_enable_entry_info_payment_details', '__return_false' ); | |
} | |
public function maybe_set_payment_status( $form_id, $field_id, $value, $entry ) { | |
// payment details panel won't be displayed unless payment_status has a value | |
// only set the payment status when the entry is listed on the entries list page | |
// only set the payment status if its not already set and if it is the form we want | |
if ( rgempty( 'payment_status', $entry ) && in_array( $form_id, $this->_form_ids ) ) { | |
GFAPI::update_entry_property( $entry['id'], 'payment_status', 'Processing' ); | |
} | |
} | |
public function admin_edit_payment_status( $payment_status, $form, $entry ) { | |
if ( $this->payment_details_editing_disabled( $entry ) ) { | |
return $payment_status; | |
} | |
//create drop down for payment status | |
$payment_string = '<select id="payment_status" name="payment_status">'; | |
$payment_string .= '<option value="' . $payment_status . '" selected>' . $payment_status . '</option>'; | |
$payment_string .= '<option value="Paid">Paid</option>'; | |
$payment_string .= '</select>'; | |
return $payment_string; | |
} | |
public function admin_edit_payment_date( $payment_date, $form, $entry ) { | |
if ( $this->payment_details_editing_disabled( $entry ) ) { | |
return $payment_date; | |
} | |
$payment_date = $entry['payment_date']; | |
if ( empty( $payment_date ) ) { | |
$payment_date = gmdate( 'y-m-d H:i:s' ); | |
} | |
$input = '<input type="text" id="payment_date" name="payment_date" value="' . $payment_date . '">'; | |
return $input; | |
} | |
public function admin_edit_payment_transaction_id( $transaction_id, $form, $entry ) { | |
if ( $this->payment_details_editing_disabled( $entry ) ) { | |
return $transaction_id; | |
} | |
$input = '<input type="text" id="custom_transaction_id" name="custom_transaction_id" value="' . $transaction_id . '">'; | |
return $input; | |
} | |
public function admin_edit_payment_amount( $payment_amount, $form, $entry ) { | |
if ( $this->payment_details_editing_disabled( $entry ) ) { | |
return $payment_amount; | |
} | |
if ( empty( $payment_amount ) ) { | |
$payment_amount = GFCommon::get_order_total( $form, $entry ); | |
} | |
$input = '<input type="text" id="payment_amount" name="payment_amount" class="gform_currency" value="' . $payment_amount . '">'; | |
return $input; | |
} | |
public function admin_update_payment( $form, $entry_id ) { | |
check_admin_referer( 'gforms_save_entry', 'gforms_save_entry' ); | |
//update payment information in admin, need to use this function so the lead data is updated before displayed in the sidebar info section | |
$entry = GFFormsModel::get_lead( $entry_id ); | |
error_log_all('PRE ENTRY'); | |
error_log_all($entry); | |
if ( $this->payment_details_editing_disabled( $entry, 'update' ) ) { | |
return; | |
} | |
//get payment fields to update | |
$payment_status = rgpost( 'payment_status' ); | |
//when updating, payment status may not be editable, if no value in post, set to lead payment status | |
if ( empty( $payment_status ) ) { | |
$payment_status = $entry['payment_status']; | |
} | |
$payment_amount = GFCommon::to_number( rgpost( 'payment_amount' ) ); | |
$payment_transaction = rgpost( 'custom_transaction_id' ); | |
$payment_date = rgpost( 'payment_date' ); | |
if ( empty( $payment_date ) ) { | |
$payment_date = gmdate( 'y-m-d H:i:s' ); | |
} else { | |
//format date entered by user | |
$payment_date = date( 'Y-m-d H:i:s', strtotime( $payment_date ) ); | |
} | |
global $current_user; | |
$user_id = 0; | |
$user_name = 'System'; | |
if ( $current_user && $user_data = get_userdata( $current_user->ID ) ) { | |
$user_id = $current_user->ID; | |
$user_name = $user_data->display_name; | |
} | |
$entry['payment_status'] = $payment_status; | |
$entry['payment_amount'] = $payment_amount; | |
$entry['payment_date'] = $payment_date; | |
$entry['transaction_id'] = $payment_transaction; | |
//update lead, add a note | |
GFAPI::update_entry( $entry ); | |
GFFormsModel::add_note( $entry['id'], $user_id, $user_name, sprintf( __( 'Payment information was manually updated. Status: %s. Amount: %s. Transaction Id: %s. Date: %s', 'gravityformspaypal' ), $entry['payment_status'], GFCommon::to_money( $entry['payment_amount'], $entry['currency'] ), $payment_transaction, $entry['payment_date'] ) ); | |
do_action( 'gform_post_payment_completed_rw', $entry ); | |
} | |
public function payment_details_editing_disabled( $entry, $action = 'edit' ) { | |
$form_action = strtolower( rgpost( 'save' ) ); | |
$gateway = gform_get_meta( $entry['id'], 'payment_gateway' ); | |
// error_log_all( 'Gateway' ); | |
// error_log_all( ! empty( $gateway ) ); | |
// error_log_all( $gateway ); | |
// error_log_all( 'Form Action' ); | |
// error_log_all( $form_action <> $action ); | |
// error_log_all( 'Payment Status' ); | |
// error_log_all( rgar( $entry, 'payment_status' ) != 'Processing' ); | |
// error_log_all( 'Transaction Type' ); | |
// error_log_all( ! rgempty( 'transaction_type', $entry ) ); | |
return ! empty( $gateway ) || $form_action <> $action || rgar( $entry, 'payment_status' ) != 'Processing' || ! rgempty( 'transaction_type', $entry ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment