Skip to content

Instantly share code, notes, and snippets.

@ideadude
Created November 5, 2023 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ideadude/f0a0a280323731221b232c2c6ee7006e to your computer and use it in GitHub Desktop.
Save ideadude/f0a0a280323731221b232c2c6ee7006e to your computer and use it in GitHub Desktop.
pmpro_patch_for_affiliate_wp
diff --git a/includes/integrations/class-pmp.php b/includes/integrations/class-pmp.php
index 00a1c89..b133e69 100644
--- a/includes/integrations/class-pmp.php
+++ b/includes/integrations/class-pmp.php
@@ -47,8 +47,7 @@ class Affiliate_WP_PMP extends Affiliate_WP_Base {
public function init() {
add_action( 'pmpro_added_order', array( $this, 'add_pending_referral' ), 10 );
- add_action( 'pmpro_updated_order', array( $this, 'mark_referral_complete' ), 10 );
- add_action( 'admin_init', array( $this, 'revoke_referral_on_refund_and_cancel' ), 10);
+ add_action( 'pmpro_updated_order', array( $this, 'update_referral_status' ), 10 );
add_action( 'pmpro_delete_order', array( $this, 'revoke_referral_on_delete' ), 10, 2 );
add_filter( 'affwp_referral_reference_column', array( $this, 'reference_link' ), 10, 2 );
add_filter( 'pmpro_orders_show_affiliate_ids', '__return_true' );
@@ -136,7 +135,7 @@ class Affiliate_WP_PMP extends Affiliate_WP_Base {
affiliate_wp()->referrals->update( $referral_id, array( 'custom' => $order->id ), '', 'referral' );
}
- $this->mark_referral_complete( $order );
+ $this->update_referral_status( $order );
}
@@ -177,77 +176,61 @@ class Affiliate_WP_PMP extends Affiliate_WP_Base {
return $rate;
}
- public function mark_referral_complete( $order ) {
+ public function update_referral_status( $order ) {
- if( 'success' !== strtolower( $order->status ) ) {
- return;
- }
-
- $this->complete_referral( $order->id );
-
- $referral = affwp_get_referral_by( 'reference', $order->id, $this->context );
-
- if ( ! is_wp_error( $referral ) ) {
- $order = new MemberOrder( $order->id );
-
- // Prevent infinite loop
- remove_action( 'pmpro_updated_order', array( $this, 'mark_referral_complete' ), 10 );
-
- $order->affiliate_id = $referral->affiliate_id;
- $amount = html_entity_decode( affwp_currency_filter( affwp_format_amount( $referral->amount ) ), ENT_QUOTES, 'UTF-8' );
- $name = affiliate_wp()->affiliates->get_affiliate_name( $referral->affiliate_id );
- /* translators: 1: Referral ID, 2: Formatted referral amount, 3: Affiliate name, 4: Referral affiliate ID */
- $note = sprintf( __( 'Referral #%1$d for %2$s recorded for %3$s (ID: %4$d).', 'affiliate-wp' ),
- $referral->referral_id,
- $amount,
- $name,
- $referral->affiliate_id
- );
+ if( strtolower( $order->status ) === 'success' ){
- if( empty( $order->notes ) ) {
- $order->notes = $note;
- } else {
- $order->notes = $order->notes . "\n\n" . $note;
+ if ( $this->complete_referral( $order->id ) ) {
+
+ // Add a note about the referral.
+ $referral = affwp_get_referral_by( 'reference', $order->id, $this->context );
+ if ( ! is_wp_error( $referral ) ) {
+ $order = new MemberOrder( $order->id );
+
+ // Prevent infinite loop
+ remove_action( 'pmpro_updated_order', array( $this, 'update_referral_status' ), 10 );
+
+ $order->affiliate_id = $referral->affiliate_id;
+ $amount = html_entity_decode( affwp_currency_filter( affwp_format_amount( $referral->amount ) ), ENT_QUOTES, 'UTF-8' );
+ $name = affiliate_wp()->affiliates->get_affiliate_name( $referral->affiliate_id );
+ /* translators: 1: Referral ID, 2: Formatted referral amount, 3: Affiliate name, 4: Referral affiliate ID */
+ $note = sprintf( __( 'Referral #%1$d for %2$s recorded for %3$s (ID: %4$d).', 'affiliate-wp' ),
+ $referral->referral_id,
+ $amount,
+ $name,
+ $referral->affiliate_id
+ );
+
+ if( empty( $order->notes ) ) {
+ $order->notes = $note;
+ } elseif( strpos( $order->notes, $note ) === false ){
+ //Prevents duplicate notes
+ $order->notes = $order->notes . "\n\n" . $note;
+ }
+
+ $order->saveOrder();
+
+ // Add this hook back.
+ add_action( 'pmpro_updated_order', array( $this, 'update_referral_status' ), 10 );
+ } else {
+ affiliate_wp()->utils->log( 'update_referral_status: The referral could not be found.', $referral );
+ }
+
}
- $order->saveOrder();
- } else {
- affiliate_wp()->utils->log( 'mark_referral_complete: The referral could not be found.', $referral );
- }
- }
-
- public function revoke_referral_on_refund_and_cancel() {
-
- /*
- * PMP does not have hooks for when an order is refunded or voided, so we detect the form submission manually
- */
+ } elseif( strtolower( $order->status ) == 'refunded' ) {
- if( ! isset( $_REQUEST['save'] ) ) {
- return;
- }
-
- if( ! isset( $_REQUEST['order'] ) ) {
- return;
- }
+ if( ! affiliate_wp()->settings->get( 'revoke_on_refund' ) ) {
+ return;
+ }
- if( ! isset( $_REQUEST['status'] ) ) {
- return;
- }
+ $this->reject_referral( absint( $order->id ) );
- if( ! isset( $_REQUEST['membership_id'] ) ) {
- return;
- }
+ } elseif( in_array( strtolower( $order->status ), array( 'token', 'error', 'review', 'pending' ) ) ) {
- if( 'refunded' != $_REQUEST['status'] ) {
- return;
- }
+ $this->reject_referral( absint( $order->id ) );
- if( ! affiliate_wp()->settings->get( 'revoke_on_refund' ) ) {
- return;
}
-
- $this->reject_referral( absint( $_REQUEST['order'] ) );
-
}
public function revoke_referral_on_delete( $order_id = 0, $order ) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment