| 1 |
<?php |
| 2 |
/** |
| 3 |
* This class is responsible for setting up and general maintenance of acitivity logs. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Admin_Donation_Activities |
| 6 |
* @author David Bisset |
| 7 |
* @copyright Copyright (c) 2023, WP Charitable LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.8.1 |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Admin_Donation_Activities' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Charitable_Admin_Donation_Activities |
| 21 |
* |
| 22 |
* @since 1.8.1 |
| 23 |
*/ |
| 24 |
final class Charitable_Admin_Donation_Activities { |
| 25 |
|
| 26 |
/** |
| 27 |
* The name of our database table |
| 28 |
* |
| 29 |
* @since 1.0.0 |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
public $table_name; |
| 34 |
|
| 35 |
/** |
| 36 |
* The single instance of this class. |
| 37 |
* |
| 38 |
* @var Charitable_Admin_Donation_Activities|null |
| 39 |
*/ |
| 40 |
private static $instance = null; |
| 41 |
|
| 42 |
|
| 43 |
/** |
| 44 |
* Create class object. |
| 45 |
* |
| 46 |
* @since 1.8.1 |
| 47 |
*/ |
| 48 |
private function __construct() { |
| 49 |
|
| 50 |
global $wpdb; |
| 51 |
|
| 52 |
$this->table_name = $wpdb->prefix . 'charitable_donation_activities'; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Add a donation activity. |
| 57 |
* |
| 58 |
* @since 1.8.1 |
| 59 |
* |
| 60 |
* @param int $donation_id The donation ID. |
| 61 |
* @param object $processor Charitable_Donation_Processor Object. |
| 62 |
* @param array $data The donation data. |
| 63 |
* @return void |
| 64 |
*/ |
| 65 |
public function add_donation_activity( $donation_id = false, $processor = false, $data = array() ) { // phpcs:ignore |
| 66 |
|
| 67 |
if ( false === $donation_id ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
if ( ! class_exists( 'Charitable_Donation_Activities_DB' ) ) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
$charitable_donation_activities_db = Charitable_Donation_Activities_DB::get_instance(); |
| 76 |
|
| 77 |
// check to see if db tables exist. |
| 78 |
if ( ! $charitable_donation_activities_db->table_exists() ) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
$log_data = array(); |
| 83 |
|
| 84 |
$donor_id = false !== $processor && method_exists( $processor, 'get_donor_id' ) ? $processor->get_donor_id() : false; |
| 85 |
$primary_action = false !== $processor && method_exists( $processor, 'get_donation_data_value' ) && $processor->get_donation_data_value( 'status' ) ? $processor->get_donation_data_value( 'status' ) : get_post_status( $donation_id ); |
| 86 |
$secondary_action = ! empty( $_POST['charitable_action'] ) ? $this->get_activity_slug_from_action( sanitize_text_field( $_POST['charitable_action'] ) ) : false; // phpcs:ignore |
| 87 |
$secondary_action = false === $secondary_action && ! empty( $_POST['action'] ) ? $this->get_activity_slug_from_action( sanitize_text_field( $_POST['action'] ) ) : $secondary_action; // phpcs:ignore |
| 88 |
|
| 89 |
foreach ( $processor->get_donation_data_value( 'campaigns' ) as $campaign_donation ) { |
| 90 |
|
| 91 |
$log_data[] = array( |
| 92 |
'donation_id' => intval( $donation_id ), |
| 93 |
'campaign_id' => ! empty( $campaign_donation['campaign_id'] ) ? $campaign_donation['campaign_id'] : 0, |
| 94 |
'donor_id' => $donor_id, |
| 95 |
'type' => 'donation', |
| 96 |
'primary_action' => $primary_action, |
| 97 |
'secondary_action' => $secondary_action, |
| 98 |
'amount' => ! empty( $campaign_donation['amount'] ) ? $campaign_donation['amount'] : false, |
| 99 |
'created_by' => get_current_user_id(), |
| 100 |
'date_recorded' => current_time( 'mysql' ), |
| 101 |
); |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
$activity = new Charitable_Admin_Activities(); |
| 106 |
|
| 107 |
foreach ( $log_data as $log ) { |
| 108 |
$activity_id = $activity->insert( $log, $this->table_name ); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Add a donation status activity via Charitable_Admin_Actions do_action(). |
| 114 |
* |
| 115 |
* @since 1.8.1 |
| 116 |
* |
| 117 |
* @param int $success The success of the action (the publish change), which is being passed from a hook. Not used in this function. |
| 118 |
* @param int $donation_id The donation ID. |
| 119 |
* @param array $args The donation data or args. This can be empty. |
| 120 |
* @param string $action The action. Example: "change_status_to_charitable-pending". |
| 121 |
* |
| 122 |
* @return int |
| 123 |
*/ |
| 124 |
public function add_donation_status_activity( $success = false, $donation_id = false, $args = array(), $action = '' ) { |
| 125 |
|
| 126 |
if ( false === $donation_id || '' === $action ) { |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
if ( strpos( $action, 'change_status_to_charitable-' ) !== 0 ) { |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
$amount = charitable_get_table( 'campaign_donations' )->get_donation_amount( $donation_id ); |
| 135 |
$donor_id = charitable_get_donation( $donation_id )->get_donor_id(); |
| 136 |
$campaign_ids = charitable_get_table( 'campaign_donations' )->get_campaigns_for_donation( $donation_id ); |
| 137 |
$campaign_id = ! empty( $campaign_ids[0] ) ? $campaign_ids[0] : false; |
| 138 |
|
| 139 |
$primary_action = str_replace( 'change_status_to_', '', $action ); |
| 140 |
$secondary_action = 'change_status'; |
| 141 |
|
| 142 |
$log_data = array( |
| 143 |
'donation_id' => intval( $donation_id ), |
| 144 |
'campaign_id' => $campaign_id, |
| 145 |
'donor_id' => $donor_id, |
| 146 |
'type' => 'donation', |
| 147 |
'primary_action' => $primary_action, |
| 148 |
'secondary_action' => $secondary_action, |
| 149 |
'amount' => $amount, |
| 150 |
'created_by' => get_current_user_id(), |
| 151 |
'date_recorded' => current_time( 'mysql' ), |
| 152 |
); |
| 153 |
|
| 154 |
$activity = new Charitable_Admin_Activities(); |
| 155 |
$activity_id = $activity->insert( $log_data, $this->table_name ); |
| 156 |
|
| 157 |
return $activity_id; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Get a slug for the activity type. Often this is the same as the action but not always, and is filterable for addons. |
| 162 |
* |
| 163 |
* @since 1.8.1 |
| 164 |
* |
| 165 |
* @param string $action The action/slug. |
| 166 |
* |
| 167 |
* @return string |
| 168 |
*/ |
| 169 |
public function get_activity_slug_from_action( $action = false ) { |
| 170 |
|
| 171 |
if ( false === $action ) { |
| 172 |
return false; |
| 173 |
} |
| 174 |
|
| 175 |
switch ( $action ) { |
| 176 |
case 'add_donation': |
| 177 |
$action_slug = 'add_donation'; |
| 178 |
break; |
| 179 |
case 'update_donation': |
| 180 |
$action_slug = 'update_donation'; |
| 181 |
break; |
| 182 |
default: |
| 183 |
$action_slug = sanitize_text_field( $action ); |
| 184 |
break; |
| 185 |
} |
| 186 |
|
| 187 |
return apply_filters( 'charitable_reports_action_slug', $action_slug ); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Returns and/or create the single instance of this class. |
| 192 |
* |
| 193 |
* @since 1.8.1 |
| 194 |
* |
| 195 |
* @return Charitable_Admin_Donation_Activities |
| 196 |
*/ |
| 197 |
public static function get_instance() { |
| 198 |
if ( is_null( self::$instance ) ) { |
| 199 |
self::$instance = new self(); |
| 200 |
} |
| 201 |
|
| 202 |
return self::$instance; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
endif; |
| 207 |
|