-
-
Save w3guy/787aa69e66cae763edd6de643eae20bc to your computer and use it in GitHub Desktop.
EDD Refund Column - Adds a “Request Refund” column to the Easy Digital Downloads purchase history table
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: EDD Refund Column | |
* Description: Adds a “Request Refund” column to the Easy Digital Downloads purchase history table that links to a refund page with the Order ID. | |
* Author: Your Name | |
* Version: 1.3.0 | |
* Requires at least: 6.0 | |
* Requires PHP: 7.4 | |
* License: GPL-2.0-or-later | |
* | |
* @package EDDRefundColumn | |
*/ | |
defined( 'ABSPATH' ) || exit; | |
// === CONFIGURATION CONSTANTS === // | |
define( 'EDD_REFUND_COLUMN_PERIOD_DAYS', 30 ); // Number of days after purchase to allow refund requests. | |
define( 'EDD_REFUND_COLUMN_PAGE_ID', 123 ); // The WordPress page ID of your refund policy page. | |
define( 'EDD_REFUND_COLUMN_QUERY_ARG', 'order_id' ); // The query parameter for passing the order ID. | |
/** | |
* Bootstraps the plugin if Easy Digital Downloads is active. | |
* | |
* @since 1.0.0 | |
* @return void | |
*/ | |
function gk_edd_refund_column_maybe_bootstrap() { | |
if ( ! function_exists( 'EDD' ) ) { | |
return; | |
} | |
add_action( 'edd_purchase_history_header_after', 'gk_edd_refund_column_add_header' ); | |
add_action( 'edd_order_history_row_end', 'gk_edd_refund_column_add_cell', 10, 1 ); | |
} | |
add_action( 'plugins_loaded', 'gk_edd_refund_column_maybe_bootstrap' ); | |
/** | |
* Outputs the “Request Refund” header cell. | |
* | |
* Hook: edd_purchase_history_header_after | |
* | |
* @since 1.0.0 | |
* @return void | |
*/ | |
function gk_edd_refund_column_add_header() { | |
?> | |
<th class="edd_purchase_refund"> | |
<?php echo esc_html__( 'Request Refund', 'edd-refund-column' ); ?> | |
</th> | |
<?php | |
} | |
/** | |
* Whether the provided EDD order is eligible for refund link display. | |
* | |
* Conditions: | |
* - Status is exactly 'complete' or 'renewal' | |
* - Order was created within the configured refund period | |
* | |
* @since 1.1.0 | |
* | |
* @param \EDD\Order $order EDD order object. | |
* @return bool True if eligible; false otherwise. | |
*/ | |
function gk_edd_refund_column_is_eligible( $order ) { | |
if ( empty( $order ) || empty( $order->id ) ) { | |
return false; | |
} | |
// Must be a completed or renewal order. | |
if ( ! in_array( $order->status, [ 'complete', 'renewal' ], true ) ) { | |
return false; | |
} | |
// Convert creation date to timestamp in the site's timezone. | |
$order_ts = 0; | |
try { | |
$order_ts = EDD()->utils->date( $order->date_created, null, true )->getTimestamp(); | |
} catch ( Exception $e ) { | |
$order_ts = is_string( $order->date_created ) ? strtotime( $order->date_created ) : 0; | |
} | |
if ( ! $order_ts ) { | |
return false; | |
} | |
$current_ts = current_time( 'timestamp' ); | |
$period_seconds = (int) EDD_REFUND_COLUMN_PERIOD_DAYS * DAY_IN_SECONDS; | |
return ( $order_ts >= ( $current_ts - $period_seconds ) ); | |
} | |
/** | |
* Outputs the “Request Refund” table cell. | |
* | |
* @since 1.0.0 | |
* | |
* @param \EDD\Order $order EDD order object for the current row. | |
* @return void | |
*/ | |
function gk_edd_refund_column_add_cell( $order ) { | |
$cell_open = '<td class="edd_purchase_refund">'; | |
$cell_close = '</td>'; | |
if ( ! gk_edd_refund_column_is_eligible( $order ) ) { | |
echo $cell_open . '—' . $cell_close; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
return; | |
} | |
$refund_url = add_query_arg( | |
EDD_REFUND_COLUMN_QUERY_ARG, | |
rawurlencode( (string) $order->id ), | |
get_permalink( EDD_REFUND_COLUMN_PAGE_ID ) | |
); | |
printf( | |
'%1$s<a href="%2$s">%3$s</a>%4$s', | |
$cell_open, | |
esc_url( $refund_url ), | |
esc_html__( 'Request Refund', 'edd-refund-column' ), | |
$cell_close | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment