| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class that is responsible for generating a CSV export of campaigns. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Export_Campaigns |
| 6 |
* @author Eric Daams |
| 7 |
* @copyright Copyright (c) 2019, Studio 164a |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.6.0 |
| 10 |
* @version 1.6.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Export_Campaigns' ) ) : |
| 18 |
|
| 19 |
/* Include Charitable_Export base class. */ |
| 20 |
if ( ! class_exists( 'Charitable_Export' ) ) { |
| 21 |
require_once( charitable()->get_path( 'includes' ) . 'abstracts/abstract-class-charitable-export.php' ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Charitable_Export_Campaigns |
| 26 |
* |
| 27 |
* @since 1.6.0 |
| 28 |
*/ |
| 29 |
class Charitable_Export_Campaigns extends Charitable_Export { |
| 30 |
|
| 31 |
/* The type of export. */ |
| 32 |
const EXPORT_TYPE = 'campaigns'; |
| 33 |
|
| 34 |
/** |
| 35 |
* Default arguments. |
| 36 |
* |
| 37 |
* @since 1.6.0 |
| 38 |
* |
| 39 |
* @var mixed[] |
| 40 |
*/ |
| 41 |
protected $defaults; |
| 42 |
|
| 43 |
/** |
| 44 |
* List of donation statuses. |
| 45 |
* |
| 46 |
* @since 1.6.0 |
| 47 |
* |
| 48 |
* @var string[] |
| 49 |
*/ |
| 50 |
protected $statuses; |
| 51 |
|
| 52 |
/** |
| 53 |
* Exportable campaign fields. |
| 54 |
* |
| 55 |
* @since 1.6.0 |
| 56 |
* |
| 57 |
* @var array |
| 58 |
*/ |
| 59 |
protected $fields; |
| 60 |
|
| 61 |
/** |
| 62 |
* Set Charitable_Campaign objects. |
| 63 |
* |
| 64 |
* @since 1.6.0 |
| 65 |
* |
| 66 |
* @var Charitable_Campaign[] |
| 67 |
*/ |
| 68 |
protected $campaigns = array(); |
| 69 |
|
| 70 |
/** |
| 71 |
* Set Charitable_User objects. |
| 72 |
* |
| 73 |
* @since 1.6.0 |
| 74 |
* |
| 75 |
* @var Charitable_User[] |
| 76 |
*/ |
| 77 |
protected $creators = array(); |
| 78 |
|
| 79 |
/** |
| 80 |
* Create class object. |
| 81 |
* |
| 82 |
* @since 1.6.0 |
| 83 |
* |
| 84 |
* @param mixed[] $args Arguments for the report. |
| 85 |
*/ |
| 86 |
public function __construct( $args ) { |
| 87 |
$this->defaults = array( |
| 88 |
'start_date' => '', |
| 89 |
'end_date' => '', |
| 90 |
'status' => '', |
| 91 |
); |
| 92 |
|
| 93 |
$this->fields = array_map( array( $this, 'get_field_label' ), charitable()->campaign_fields()->get_export_fields() ); |
| 94 |
|
| 95 |
add_filter( 'charitable_export_data_key_value', array( $this, 'set_custom_field_data' ), 10, 3 ); |
| 96 |
|
| 97 |
parent::__construct( $args ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Filter the cell values. |
| 102 |
* |
| 103 |
* @since 1.6.0 |
| 104 |
* |
| 105 |
* @param mixed $value The value to set. |
| 106 |
* @param string $key The key to set. |
| 107 |
* @param int $campaign_id The campaign ID. |
| 108 |
* @return mixed |
| 109 |
*/ |
| 110 |
public function set_custom_field_data( $value, $key, $campaign_id ) { |
| 111 |
if ( array_key_exists( $key, $this->fields ) ) { |
| 112 |
$value = $this->get_campaign( $campaign_id )->get( $key ); |
| 113 |
} |
| 114 |
|
| 115 |
return $value; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Return the CSV column headers. |
| 120 |
* |
| 121 |
* The columns are set as a key=>label array, where the key is used to retrieve the data for that column. |
| 122 |
* |
| 123 |
* @since 1.6.0 |
| 124 |
* |
| 125 |
* @return string[] |
| 126 |
*/ |
| 127 |
protected function get_csv_columns() { |
| 128 |
$default_columns = array( |
| 129 |
'ID' => __( 'Campaign ID', 'charitable' ), |
| 130 |
'post_title' => __( 'Title', 'charitable' ), |
| 131 |
'post_date' => __( 'Date Created', 'charitable' ), |
| 132 |
'end_date' => __( 'End Date', 'charitable' ), |
| 133 |
'goal' => __( 'Goal', 'charitable' ), |
| 134 |
'donated_amount' => __( 'Amount Donated', 'charitable' ), |
| 135 |
'percent_donated_raw' => __( 'Percentage of Goal Raised', 'charitable' ), |
| 136 |
'status' => __( 'Status', 'charitable' ), |
| 137 |
'campaign_creator_name' => __( 'Campaign Creator', 'charitable' ), |
| 138 |
'campaign_creator_id' => __( 'Campaign Creator ID', 'charitable' ), |
| 139 |
'campaign_creator_email' => __( 'Campaign Creator Email', 'charitable' ), |
| 140 |
); |
| 141 |
|
| 142 |
/** |
| 143 |
* Filter the list of columns in the export. |
| 144 |
* |
| 145 |
* The recommended way to add or remove columns to the campaign export |
| 146 |
* is through the Campaign Fields API. This filter is provided as a way |
| 147 |
* to change export column headers without changing the label for the |
| 148 |
* Campaign Field. |
| 149 |
* |
| 150 |
* @since 1.6.0 |
| 151 |
* |
| 152 |
* @param array $columns List of columns. |
| 153 |
* @param array $args Export args. |
| 154 |
*/ |
| 155 |
$filtered = apply_filters( 'charitable_export_campaigns_columns', $default_columns, $this->args ); |
| 156 |
|
| 157 |
return $this->get_merged_fields( $default_columns, $this->fields, array(), $filtered ); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Return a Campaign object for the given ID. |
| 162 |
* |
| 163 |
* @since 1.6.0 |
| 164 |
* |
| 165 |
* @param int $campaign_id The campaign ID. |
| 166 |
* @return Charitable_Campaign |
| 167 |
*/ |
| 168 |
protected function get_campaign( $campaign_id ) { |
| 169 |
if ( ! array_key_exists( $campaign_id, $this->campaigns ) ) { |
| 170 |
$this->campaigns[ $campaign_id ] = charitable_get_campaign( $campaign_id ); |
| 171 |
} |
| 172 |
|
| 173 |
return $this->campaigns[ $campaign_id ]; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Return a User object for a campaign creator. |
| 178 |
* |
| 179 |
* @since 1.6.0 |
| 180 |
* |
| 181 |
* @param int $campaign_id The campaign ID. |
| 182 |
* @return Charitable_User |
| 183 |
*/ |
| 184 |
public function get_campaign_creator( $campaign_id ) { |
| 185 |
$creator = $this->get_campaign( $campaign_id )->get_campaign_creator(); |
| 186 |
|
| 187 |
if ( ! array_key_exists( $creator, $this->creators ) ) { |
| 188 |
$this->creators[ $creator ] = charitable_get_user( $creator ); |
| 189 |
} |
| 190 |
|
| 191 |
return $this->creators[ $creator ]; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Get the data to be exported. |
| 196 |
* |
| 197 |
* @since 1.6.0 |
| 198 |
* |
| 199 |
* @return array |
| 200 |
*/ |
| 201 |
protected function get_data() { |
| 202 |
$query_args = array( |
| 203 |
'fields' => 'ids', |
| 204 |
'posts_per_page' => -1, |
| 205 |
); |
| 206 |
|
| 207 |
if ( strlen( $this->args['start_date'] ) || strlen( $this->args['end_date'] ) ) { |
| 208 |
$date_query = array( |
| 209 |
'inclusive' => true, |
| 210 |
); |
| 211 |
|
| 212 |
if ( strlen( $this->args['start_date'] ) ) { |
| 213 |
$date_query['after'] = charitable_sanitize_date( $this->args['start_date'], 'Y-m-d' ); |
| 214 |
} |
| 215 |
|
| 216 |
if ( strlen( $this->args['end_date'] ) ) { |
| 217 |
$date_query['before'] = charitable_sanitize_date( $this->args['end_date'], 'Y-m-d' ); |
| 218 |
} |
| 219 |
|
| 220 |
$query_args['date_query'] = $date_query; |
| 221 |
} |
| 222 |
|
| 223 |
if ( ! empty( $this->args['status'] ) ) { |
| 224 |
switch ( $this->args['status'] ) { |
| 225 |
case 'active': |
| 226 |
$query_args['post_status'] = 'publish'; |
| 227 |
$query_args['meta_query'] = array( |
| 228 |
'relation' => 'OR', |
| 229 |
array( |
| 230 |
'key' => '_campaign_end_date', |
| 231 |
'value' => date( 'Y-m-d H:i:s' ), |
| 232 |
'compare' => '>', |
| 233 |
'type' => 'datetime', |
| 234 |
), |
| 235 |
array( |
| 236 |
'key' => '_campaign_end_date', |
| 237 |
'value' => 0, |
| 238 |
'compare' => '=', |
| 239 |
), |
| 240 |
); |
| 241 |
break; |
| 242 |
|
| 243 |
case 'finish': |
| 244 |
$query_args['post_status'] = 'publish'; |
| 245 |
$query_args['meta_query'] = array( |
| 246 |
array( |
| 247 |
'key' => '_campaign_end_date', |
| 248 |
'value' => date( 'Y-m-d H:i:s' ), |
| 249 |
'compare' => '<=', |
| 250 |
'type' => 'datetime', |
| 251 |
), |
| 252 |
); |
| 253 |
break; |
| 254 |
|
| 255 |
default: |
| 256 |
$query_args['post_status'] = $this->args['status']; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Filter the campaigns export query arguments. |
| 262 |
* |
| 263 |
* @since 1.6.0 |
| 264 |
* |
| 265 |
* @param array $query_args The query arguments. |
| 266 |
* @param array $args The export arguments. |
| 267 |
*/ |
| 268 |
$query_args = apply_filters( 'charitable_export_campaigns_query_args', $query_args, $this->args ); |
| 269 |
|
| 270 |
return Charitable_Campaigns::query( $query_args )->posts; |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* Receives a row of data and maps it to the keys defined in the columns. |
| 275 |
* |
| 276 |
* @since 1.6.0 |
| 277 |
* |
| 278 |
* @param int $data The campaign ID. |
| 279 |
* @return mixed |
| 280 |
*/ |
| 281 |
protected function map_data( $data ) { |
| 282 |
$row = array(); |
| 283 |
|
| 284 |
foreach ( $this->columns as $key => $label ) { |
| 285 |
$value = isset( $data[ $key ] ) ? $data[ $key ] : ''; |
| 286 |
$value = apply_filters( 'charitable_export_data_key_value', $value, $key, $data ); |
| 287 |
$row[] = $value; |
| 288 |
} |
| 289 |
|
| 290 |
return $row; |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Return the field label for a registered Campaign Field. |
| 295 |
* |
| 296 |
* @since 1.6.0 |
| 297 |
* |
| 298 |
* @param Charitable_Campaign_Field $field Instance of `Charitable_Campaign_Field`. |
| 299 |
* @return string |
| 300 |
*/ |
| 301 |
protected function get_field_label( Charitable_Campaign_Field $field ) { |
| 302 |
return $field->label; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
endif; |
| 307 |
|