suretriggers
/
src
/
Integrations
/
SureDonation
/
triggers
/
suredonation-donation-status-changed.php
suredonation-donation-completed.php
1 month ago
suredonation-donation-refunded.php
1 month ago
suredonation-donation-status-changed.php
1 month ago
suredonation-new-donation.php
1 month ago
suredonation-donation-status-changed.php
149 lines
| 1 | <?php |
| 2 | /** |
| 3 | * SureDonationDonationStatusChanged trigger. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category SureDonationDonationStatusChanged |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace SureTriggers\Integrations\SureDonation\Triggers; |
| 15 | |
| 16 | use SureTriggers\Controllers\AutomationController; |
| 17 | use SureTriggers\Controllers\OptionController; |
| 18 | use SureTriggers\Traits\SingletonLoader; |
| 19 | |
| 20 | /** |
| 21 | * SureDonationDonationStatusChanged |
| 22 | * |
| 23 | * @category SureDonationDonationStatusChanged |
| 24 | * @package SureTriggers |
| 25 | * @author BSF <username@example.com> |
| 26 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 27 | * @link https://www.brainstormforce.com/ |
| 28 | * @since 1.0.0 |
| 29 | */ |
| 30 | class SureDonationDonationStatusChanged { |
| 31 | |
| 32 | /** |
| 33 | * Integration type. |
| 34 | * |
| 35 | * @var string |
| 36 | */ |
| 37 | public $integration = 'SureDonation'; |
| 38 | |
| 39 | /** |
| 40 | * Trigger name. |
| 41 | * |
| 42 | * @var string |
| 43 | */ |
| 44 | public $trigger = 'suredonation_donation_status_changed'; |
| 45 | |
| 46 | use SingletonLoader; |
| 47 | |
| 48 | /** |
| 49 | * Constructor |
| 50 | * |
| 51 | * @since 1.0.0 |
| 52 | */ |
| 53 | public function __construct() { |
| 54 | add_filter( 'sure_trigger_register_trigger', [ $this, 'register' ] ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Register a trigger. |
| 59 | * |
| 60 | * @param array $triggers triggers. |
| 61 | * @return array |
| 62 | */ |
| 63 | public function register( $triggers ) { |
| 64 | $triggers[ $this->integration ][ $this->trigger ] = [ |
| 65 | 'label' => __( 'Donation Status Changed', 'suretriggers' ), |
| 66 | 'action' => 'suredonation_donation_status_changed', |
| 67 | 'function' => [ $this, 'trigger_listener' ], |
| 68 | 'priority' => 10, |
| 69 | 'accepted_args' => 4, |
| 70 | ]; |
| 71 | |
| 72 | return $triggers; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Trigger listener. |
| 77 | * |
| 78 | * @param int $donation_id Donation ID. |
| 79 | * @param string $new_status New payment status. |
| 80 | * @param string $old_status Previous payment status. |
| 81 | * @param array<string, mixed> $donation Donation data array. |
| 82 | * @return void |
| 83 | */ |
| 84 | public function trigger_listener( $donation_id, $new_status, $old_status, $donation ) { |
| 85 | if ( empty( $donation_id ) ) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | $trigger_data = OptionController::get_option( 'trigger_data' ); |
| 90 | if ( is_array( $trigger_data ) && isset( $trigger_data[ $this->integration ][ $this->trigger ]['selected_options']['new_status'] ) ) { |
| 91 | $selected_status = $trigger_data[ $this->integration ][ $this->trigger ]['selected_options']['new_status']; |
| 92 | if ( is_array( $selected_status ) ) { |
| 93 | $selected_status = isset( $selected_status['value'] ) ? $selected_status['value'] : ''; |
| 94 | } |
| 95 | if ( ! empty( $selected_status ) && '-1' !== (string) $selected_status && $selected_status !== $new_status ) { |
| 96 | return; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if ( ( ! is_array( $donation ) || empty( $donation ) ) && class_exists( 'SureDonation\Inc\Database\Tables\Donations' ) ) { |
| 101 | $donation = \SureDonation\Inc\Database\Tables\Donations::get( absint( $donation_id ) ); |
| 102 | } |
| 103 | |
| 104 | if ( ! is_array( $donation ) ) { |
| 105 | $donation = []; |
| 106 | } |
| 107 | |
| 108 | $campaign_id = isset( $donation['campaign_id'] ) && is_numeric( $donation['campaign_id'] ) ? absint( $donation['campaign_id'] ) : 0; |
| 109 | $form_id = isset( $donation['form_id'] ) && is_numeric( $donation['form_id'] ) ? absint( $donation['form_id'] ) : 0; |
| 110 | |
| 111 | $context = [ |
| 112 | 'donation_id' => absint( $donation_id ), |
| 113 | 'old_status' => sanitize_text_field( $old_status ), |
| 114 | 'new_status' => sanitize_text_field( $new_status ), |
| 115 | 'campaign_id' => $campaign_id, |
| 116 | 'campaign_title' => $campaign_id ? (string) get_the_title( $campaign_id ) : '', |
| 117 | 'form_id' => $form_id, |
| 118 | 'form_title' => $form_id ? (string) get_the_title( $form_id ) : '', |
| 119 | 'donor_id' => isset( $donation['donor_id'] ) && is_numeric( $donation['donor_id'] ) ? absint( $donation['donor_id'] ) : 0, |
| 120 | 'donor_name' => isset( $donation['donor_name'] ) ? sanitize_text_field( (string) $donation['donor_name'] ) : '', |
| 121 | 'donor_email' => isset( $donation['donor_email'] ) ? sanitize_email( (string) $donation['donor_email'] ) : '', |
| 122 | 'donor_phone' => isset( $donation['donor_phone'] ) ? sanitize_text_field( (string) $donation['donor_phone'] ) : '', |
| 123 | 'amount' => isset( $donation['amount'] ) && is_numeric( $donation['amount'] ) ? (float) $donation['amount'] : 0.0, |
| 124 | 'fees_covered' => isset( $donation['fees_covered'] ) && is_numeric( $donation['fees_covered'] ) ? (float) $donation['fees_covered'] : 0.0, |
| 125 | 'currency' => isset( $donation['currency'] ) ? sanitize_text_field( (string) $donation['currency'] ) : 'USD', |
| 126 | 'payment_status' => sanitize_text_field( $new_status ), |
| 127 | 'payment_mode' => isset( $donation['payment_mode'] ) ? sanitize_text_field( (string) $donation['payment_mode'] ) : '', |
| 128 | 'gateway' => isset( $donation['gateway'] ) ? sanitize_text_field( (string) $donation['gateway'] ) : '', |
| 129 | 'transaction_id' => isset( $donation['transaction_id'] ) ? sanitize_text_field( (string) $donation['transaction_id'] ) : '', |
| 130 | 'donation_type' => isset( $donation['donation_type'] ) ? sanitize_text_field( (string) $donation['donation_type'] ) : 'one-time', |
| 131 | 'is_anonymous' => ! empty( $donation['is_anonymous'] ), |
| 132 | 'donor_comment' => isset( $donation['donor_comment'] ) ? wp_kses_post( (string) $donation['donor_comment'] ) : '', |
| 133 | 'subscription_id' => isset( $donation['subscription_id'] ) ? sanitize_text_field( (string) $donation['subscription_id'] ) : '', |
| 134 | 'subscription_status' => isset( $donation['subscription_status'] ) ? sanitize_text_field( (string) $donation['subscription_status'] ) : '', |
| 135 | 'created_at' => isset( $donation['created_at'] ) ? sanitize_text_field( (string) $donation['created_at'] ) : '', |
| 136 | 'updated_at' => isset( $donation['updated_at'] ) ? sanitize_text_field( (string) $donation['updated_at'] ) : '', |
| 137 | ]; |
| 138 | |
| 139 | AutomationController::sure_trigger_handle_trigger( |
| 140 | [ |
| 141 | 'trigger' => $this->trigger, |
| 142 | 'context' => $context, |
| 143 | ] |
| 144 | ); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | SureDonationDonationStatusChanged::get_instance(); |
| 149 |