| 1 |
<?php |
| 2 |
/** |
| 3 |
* This class is responsible for setting up and general maintenance of acitivity logs. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Admin_Campaign_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 |
|
| 18 |
|
| 19 |
if ( ! class_exists( 'Charitable_Admin_Campaign_Activities' ) ) : |
| 20 |
|
| 21 |
/** |
| 22 |
* Charitable_Admin_Campaign_Activities |
| 23 |
* |
| 24 |
* @since 1.8.1 |
| 25 |
*/ |
| 26 |
final class Charitable_Admin_Campaign_Activities { |
| 27 |
|
| 28 |
/** |
| 29 |
* The name of our database table |
| 30 |
* |
| 31 |
* @since 1.0.0 |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
public $table_name; |
| 36 |
|
| 37 |
/** |
| 38 |
* The single instance of this class. |
| 39 |
* |
| 40 |
* @var Charitable_Admin_Campaign_Activities|null |
| 41 |
*/ |
| 42 |
private static $instance = null; |
| 43 |
|
| 44 |
|
| 45 |
/** |
| 46 |
* Create class object. |
| 47 |
* |
| 48 |
* @since 1.8.1 |
| 49 |
*/ |
| 50 |
private function __construct() { |
| 51 |
|
| 52 |
global $wpdb; |
| 53 |
|
| 54 |
$this->table_name = $wpdb->prefix . 'charitable_campaign_activities'; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Add a campaign activity after a post status transition. |
| 59 |
* |
| 60 |
* @since 1.8.1 |
| 61 |
* @param string $new_status The new status. Example: "publish". |
| 62 |
* @param string $old_status The old status. Example: "draft". |
| 63 |
* @param object $post The post object. |
| 64 |
*/ |
| 65 |
public function add_campaign_activity_transition_post_status( $new_status = false, $old_status = false, $post = null ) { |
| 66 |
|
| 67 |
// For doing stuff when moving in and out (!) of published status only. |
| 68 |
if ( empty( $post->ID ) || $old_status === $new_status || $old_status !== 'publish' && $new_status !== 'publish' ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
// For now, limit to just when a campaign is pubhlished. |
| 73 |
if ( 'publish' !== $new_status ) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
$campaign_id = $post->ID; |
| 78 |
$action = 'update'; |
| 79 |
$data = (array) $post; |
| 80 |
|
| 81 |
$this->add_campaign_activity( $campaign_id, $data, $action, $new_status ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Add a campaign activity. |
| 86 |
* |
| 87 |
* @since 1.8.1 |
| 88 |
* @param int $campaign_id The campaign ID. |
| 89 |
* @param array $data The campaign data. |
| 90 |
* @param string $primary_action The primary action to write to the db table. |
| 91 |
* @param string $secondary_action The secondary action to write to the db table. |
| 92 |
*/ |
| 93 |
public function add_campaign_activity( $campaign_id = false, $data = array(), $primary_action = '', $secondary_action = '' ) { // phpcs:ignore |
| 94 |
|
| 95 |
if ( false === $campaign_id || empty( $data ) ) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
if ( ! class_exists( 'Charitable_Campaign_Activities_DB' ) ) { |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
$charitable_campaign_activities_db = Charitable_Campaign_Activities_DB::get_instance(); |
| 104 |
|
| 105 |
// check to see if db tables exist. |
| 106 |
if ( ! $charitable_campaign_activities_db->table_exists() ) { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
$log_data = array(); |
| 111 |
|
| 112 |
// Filter the primary action. |
| 113 |
$primary_action = esc_attr( $primary_action ); |
| 114 |
|
| 115 |
// If we aren't passed a secondary action, attempt to add one from a $_POST value. |
| 116 |
$secondary_action = '' === $secondary_action && ! empty( $_POST['charitable_action'] ) ? $this->get_activity_slug_from_action( esc_attr( $_POST['charitable_action'] ) ) : $secondary_action; // phpcs:ignore |
| 117 |
$secondary_action = false === $secondary_action && ! empty( $_POST['action'] ) ? $this->get_activity_slug_from_action( esc_attr( $_POST['action'] ) ) : $secondary_action; // phpcs:ignore |
| 118 |
|
| 119 |
$log_data[] = array( |
| 120 |
'campaign_id' => intval( $campaign_id ), |
| 121 |
'type' => 'campaign', |
| 122 |
'primary_action' => $primary_action, |
| 123 |
'secondary_action' => $secondary_action, |
| 124 |
'created_by' => get_current_user_id(), |
| 125 |
'date_recorded' => current_time( 'mysql' ), |
| 126 |
); |
| 127 |
|
| 128 |
$activity = new Charitable_Admin_Activities(); |
| 129 |
|
| 130 |
foreach ( $log_data as $log ) { |
| 131 |
$activity_id = $activity->insert( $log, $this->table_name ); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Add a campaign status activity via Charitable_Admin_Actions do_action(). |
| 137 |
* Empty/placeholder function that is already deprecated in favor of add_campaign_activity_transition_post_status(). |
| 138 |
* |
| 139 |
* @since 1.8.1 |
| 140 |
* |
| 141 |
* @param bool $success Whether the previous passed in action was successful. |
| 142 |
* @param int $campaign_id The campaign ID. |
| 143 |
* @param array $args The campaign data or args. This can be empty. |
| 144 |
* @param string $action The action. Example: "change_status_to_charitable-pending". |
| 145 |
* @return void |
| 146 |
*/ |
| 147 |
public function add_campaign_status_activity( $success = false, $campaign_id = false, $args = array(), $action = '' ) { |
| 148 |
|
| 149 |
if ( false === $campaign_id || '' === $action ) { |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
if ( strpos( $action, 'change_status_to_charitable-' ) !== 0 ) { |
| 154 |
return; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Get a slug for the activity type. Often this is the same as the action but not always, and is filterable for addons. |
| 160 |
* |
| 161 |
* @since 1.8.1 |
| 162 |
* |
| 163 |
* @param string $action The action/slug. |
| 164 |
* |
| 165 |
* @return string |
| 166 |
*/ |
| 167 |
public function get_activity_slug_from_action( $action = false ) { |
| 168 |
|
| 169 |
if ( false === $action ) { |
| 170 |
return false; |
| 171 |
} |
| 172 |
|
| 173 |
switch ( $action ) { |
| 174 |
case 'add_campaign': |
| 175 |
$action_slug = 'add_campaign'; |
| 176 |
break; |
| 177 |
case 'add_campaign': |
| 178 |
$action_slug = 'update_campaign'; |
| 179 |
break; |
| 180 |
default: |
| 181 |
$action_slug = sanitize_text_field( $action ); |
| 182 |
break; |
| 183 |
} |
| 184 |
|
| 185 |
return apply_filters( 'charitable_reports_action_slug', $action_slug ); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Returns and/or create the single instance of this class. |
| 190 |
* |
| 191 |
* @since 1.8.1 |
| 192 |
* |
| 193 |
* @return Charitable_Admin_Campaign_Activities |
| 194 |
*/ |
| 195 |
public static function get_instance() { |
| 196 |
if ( is_null( self::$instance ) ) { |
| 197 |
self::$instance = new self(); |
| 198 |
} |
| 199 |
|
| 200 |
return self::$instance; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
endif; |
| 205 |
|