← All changes
|
integrations/charitable/includes/triggers/user-donation.php
+175
-0
5.7.8
→
6.0.3
View file →
| @@ -1,0 +1,175 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * User Donation | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\Charitable\Triggers\User_Donation | |
| 6 | + * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.0.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +class AutomatorWP_Charitable_User_Donation extends AutomatorWP_Integration_Trigger { | |
| 13 | + | |
| 14 | + public $integration = 'charitable'; | |
| 15 | + public $trigger = 'charitable_user_donation'; | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * Register the trigger | |
| 19 | + * | |
| 20 | + * @since 1.0.0 | |
| 21 | + */ | |
| 22 | + public function register() { | |
| 23 | + | |
| 24 | + automatorwp_register_trigger( $this->trigger, array( | |
| 25 | + 'integration' => $this->integration, | |
| 26 | + 'label' => __( 'User makes a donation', 'automatorwp' ), | |
| 27 | + 'select_option' => __( 'User <strong>makes</strong> a donation', 'automatorwp' ), | |
| 28 | + /* %1$s: Number of times. */ | |
| 29 | + 'edit_label' => sprintf( __( 'User makes a donation %1$s time(s)', 'automatorwp' ), '{times}' ), | |
| 30 | + 'log_label' => __( 'User makes a donation', 'automatorwp' ), | |
| 31 | + 'action' => 'charitable_donation_save', | |
| 32 | + 'function' => array( $this, 'listener' ), | |
| 33 | + 'priority' => 10, | |
| 34 | + 'accepted_args' => 2, | |
| 35 | + 'options' => array( | |
| 36 | + 'times' => automatorwp_utilities_times_option(), | |
| 37 | + ), | |
| 38 | + 'tags' => array_merge( | |
| 39 | + automatorwp_charitable_get_donation_tags(), | |
| 40 | + automatorwp_utilities_times_tag() | |
| 41 | + ) | |
| 42 | + ) ); | |
| 43 | + | |
| 44 | + } | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * Trigger listener | |
| 48 | + * | |
| 49 | + * @since 1.0.0 | |
| 50 | + * | |
| 51 | + * @param int $donation_id The donation ID. | |
| 52 | + * @param WP_Post $post Instance of `WP_Post`. | |
| 53 | + */ | |
| 54 | + public function listener( $donation_id, $post ) { | |
| 55 | + | |
| 56 | + $donation = charitable_get_donation( $donation_id ); | |
| 57 | + | |
| 58 | + $donor = $donation->get_donor_data(); | |
| 59 | + | |
| 60 | + $user = get_user_by( 'email', $donor['email'] ); | |
| 61 | + | |
| 62 | + // Bail if no user | |
| 63 | + if ( empty( $user ) ) { | |
| 64 | + return; | |
| 65 | + } | |
| 66 | + | |
| 67 | + $user_id = $user->ID; | |
| 68 | + | |
| 69 | + // Get campaigns. | |
| 70 | + $campaigns = $donation->get_campaign_donations(); | |
| 71 | + | |
| 72 | + // Bail no campaigns. | |
| 73 | + if ( empty( $campaigns ) ) { | |
| 74 | + return; | |
| 75 | + } | |
| 76 | + | |
| 77 | + foreach ( $campaigns as $campaign ) { | |
| 78 | + $campaign_id = $campaign->campaign_id; | |
| 79 | + } | |
| 80 | + | |
| 81 | + $campaign = charitable_get_campaign( $campaign_id ); | |
| 82 | + | |
| 83 | + // Bail if not approved status | |
| 84 | + if ( ! charitable_is_approved_status( get_post_status( $donation_id ) ) ) { | |
| 85 | + return false; | |
| 86 | + } | |
| 87 | + | |
| 88 | + $old_status = ! empty( $_POST['original_post_status'] ) ? $_POST['original_post_status'] : ''; | |
| 89 | + | |
| 90 | + // Bail if same status | |
| 91 | + if ( $old_status === $post->post_status ) { | |
| 92 | + return; | |
| 93 | + } | |
| 94 | + | |
| 95 | + // Trigger the become an affiliate | |
| 96 | + automatorwp_trigger_event( array( | |
| 97 | + 'trigger' => $this->trigger, | |
| 98 | + 'user_id' => $user_id, | |
| 99 | + 'donation_id' => $donation_id, | |
| 100 | + 'donation_title' => get_the_title( $donation_id ), | |
| 101 | + 'donation_amount' => $donation->get_amount_formatted(), | |
| 102 | + 'donation_status' => $donation->get_status_label(), | |
| 103 | + 'donation_payment_method' => $donation->get_gateway_label(), | |
| 104 | + 'donor_id' => $user_id, | |
| 105 | + 'donor_first_name' => $donor['first_name'], | |
| 106 | + 'donor_last_name' => $donor['last_name'], | |
| 107 | + 'donor_country' => $donor['country'], | |
| 108 | + 'donor_phone' => $donor['phone'], | |
| 109 | + 'campaign_id' => $campaign_id, | |
| 110 | + 'campaign_title' => $campaign->post_title, | |
| 111 | + 'campaign_goal' => charitable_format_money( $campaign->get_goal() ), | |
| 112 | + 'campaign_min_donation' => charitable_format_money( charitable_get_minimum_donation_amount( $campaign_id ) ), | |
| 113 | + 'campaign_end_date' => $campaign->get_end_date(), | |
| 114 | + | |
| 115 | + ) ); | |
| 116 | + | |
| 117 | + } | |
| 118 | + | |
| 119 | + /** | |
| 120 | + * Register the required hooks | |
| 121 | + * | |
| 122 | + * @since 1.0.0 | |
| 123 | + */ | |
| 124 | + public function hooks() { | |
| 125 | + | |
| 126 | + // Log meta data | |
| 127 | + add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 ); | |
| 128 | + | |
| 129 | + parent::hooks(); | |
| 130 | + } | |
| 131 | + | |
| 132 | + /** | |
| 133 | + * Trigger custom log meta | |
| 134 | + * | |
| 135 | + * @since 1.0.0 | |
| 136 | + * | |
| 137 | + * @param array $log_meta Log meta data | |
| 138 | + * @param stdClass $trigger The trigger object | |
| 139 | + * @param int $user_id The user ID | |
| 140 | + * @param array $event Event information | |
| 141 | + * @param array $trigger_options The trigger's stored options | |
| 142 | + * @param stdClass $automation The trigger's automation object | |
| 143 | + * | |
| 144 | + * @return array | |
| 145 | + */ | |
| 146 | + function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) { | |
| 147 | + | |
| 148 | + // Bail if action type don't match this action | |
| 149 | + if( $trigger->type !== $this->trigger ) { | |
| 150 | + return $log_meta; | |
| 151 | + } | |
| 152 | + | |
| 153 | + $log_meta['donation_id'] = ( isset( $event['donation_id'] ) ? $event['donation_id'] : '' ); | |
| 154 | + $log_meta['donation_title'] = ( isset( $event['donation_title'] ) ? $event['donation_title'] : '' ); | |
| 155 | + $log_meta['donation_amount'] = ( isset( $event['donation_amount'] ) ? $event['donation_amount'] : 0.00 ); | |
| 156 | + $log_meta['donation_status'] = ( isset( $event['donation_status'] ) ? $event['donation_status'] : '' ); | |
| 157 | + $log_meta['donation_payment_method'] = ( isset( $event['donation_payment_method'] ) ? $event['donation_payment_method'] : '' ); | |
| 158 | + $log_meta['donor_id'] = ( isset( $event['donor_id'] ) ? $event['donor_id'] : '' ); | |
| 159 | + $log_meta['donor_first_name'] = ( isset( $event['donor_first_name'] ) ? $event['donor_first_name'] : '' ); | |
| 160 | + $log_meta['donor_last_name'] = ( isset( $event['donor_last_name'] ) ? $event['donor_last_name'] : '' ); | |
| 161 | + $log_meta['donor_country'] = ( isset( $event['donor_country'] ) ? $event['donor_country'] : '' ); | |
| 162 | + $log_meta['donor_phone'] = ( isset( $event['donor_phone'] ) ? $event['donor_phone'] : '' ); | |
| 163 | + $log_meta['campaign_id'] = ( isset( $event['campaign_id'] ) ? $event['campaign_id'] : '' ); | |
| 164 | + $log_meta['campaign_title'] = ( isset( $event['campaign_title'] ) ? $event['campaign_title'] : '' ); | |
| 165 | + $log_meta['campaign_goal'] = ( isset( $event['campaign_goal'] ) ? $event['campaign_goal'] : '' ); | |
| 166 | + $log_meta['campaign_min_donation'] = ( isset( $event['campaign_min_donation'] ) ? $event['campaign_min_donation'] : '' ); | |
| 167 | + $log_meta['campaign_end_date'] = ( isset( $event['campaign_end_date'] ) ? $event['campaign_end_date'] : '' ); | |
| 168 | + | |
| 169 | + return $log_meta; | |
| 170 | + | |
| 171 | + } | |
| 172 | + | |
| 173 | +} | |
| 174 | + | |
| 175 | +new AutomatorWP_Charitable_User_Donation(); | |