| 1 |
<?php |
| 2 |
/** |
| 3 |
* This class is responsible for adding the Charitable admin pages. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Export_Items |
| 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.7.0.7 |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Export_Items' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Charitable_Export_Items |
| 21 |
* |
| 22 |
* @since 1.7.0.7 |
| 23 |
*/ |
| 24 |
final class Charitable_Export_Items { |
| 25 |
|
| 26 |
/** |
| 27 |
* The single instance of this class. |
| 28 |
* |
| 29 |
* @var Charitable_Export_Items|null |
| 30 |
*/ |
| 31 |
private static $instance = null; |
| 32 |
|
| 33 |
|
| 34 |
/** |
| 35 |
* Create class object. |
| 36 |
* |
| 37 |
* @since 1.7.0.7 |
| 38 |
*/ |
| 39 |
private function __construct() { |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Returns and/or create the single instance of this class. |
| 44 |
* |
| 45 |
* @since 1.7.0.7 |
| 46 |
* |
| 47 |
* @return Charitable_Export_Items |
| 48 |
*/ |
| 49 |
public static function get_instance() { |
| 50 |
if ( is_null( self::$instance ) ) { |
| 51 |
self::$instance = new self(); |
| 52 |
} |
| 53 |
|
| 54 |
return self::$instance; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* This forces the charitable menu to be open for category and tag pages. |
| 59 |
* |
| 60 |
* @since 1.7.0.7 |
| 61 |
* |
| 62 |
* @return string |
| 63 |
*/ |
| 64 |
public function admin_accept_export_campaign_request() { |
| 65 |
|
| 66 |
global $wpdb; |
| 67 |
|
| 68 |
if ( ! is_admin() || empty( $_POST ) ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
// Array ( [charitable_nonce] => 56a1cf267c [_wp_http_referer] => /wp-admin/admin.php?page=charitable-settings&tab=tools [charitable_settings] => Array ( [export] => Array ( [export_campaign] => 5 ) ) ) good |
| 73 |
|
| 74 |
if ( ! isset( $_POST['charitable_nonce'] ) || ! wp_verify_nonce( $_POST['charitable_nonce'], 'export_campaign' ) ) { // phpcs:ignore |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
if ( ! isset( $_POST['charitable_settings']['export'] ) ) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
$export_args = $_POST['charitable_settings']['export']; // phpcs:ignore |
| 83 |
|
| 84 |
if ( ! isset( $export_args['export_campaign'] ) || intval( $export_args['export_campaign'] ) === 0 ) { |
| 85 |
if ( charitable_is_debug() ) { |
| 86 |
error_log( 'admin_accept_export_campaign_request: ' ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 87 |
error_log( print_r( $export_args, true ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log,WordPress.PHP.DevelopmentFunctions.error_log_print_r |
| 88 |
} |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
// Ignore the user aborting the action. |
| 93 |
ignore_user_abort( true ); |
| 94 |
|
| 95 |
// Grab the proper data. |
| 96 |
$campaign_id = ( isset( $export_args['export_campaign'] ) ) ? absint( $export_args['export_campaign'] ) : false; |
| 97 |
$campaign_post = get_post( $campaign_id ); |
| 98 |
|
| 99 |
// Metadata rows. |
| 100 |
$meta_sql = "SELECT * FROM $wpdb->postmeta WHERE meta_key LIKE '_campaign_%' AND post_id = " . intval( $campaign_id ); |
| 101 |
$meta_raw = $wpdb->get_results( $wpdb->prepare( $meta_sql ), OBJECT_K ); // phpcs:ignore |
| 102 |
|
| 103 |
// author / campaign_creator. |
| 104 |
$author_data = array(); |
| 105 |
$author_meta = ( $campaign_post->post_author !== 0 ) ? get_userdata( $campaign_post->post_author ) : array(); |
| 106 |
if ( $author_meta ) { |
| 107 |
$author_data = $author_meta->data; |
| 108 |
unset( $author_data->user_pass ); |
| 109 |
} |
| 110 |
|
| 111 |
// categories and tags. |
| 112 |
$categories = get_the_terms( $campaign_id, 'campaign_category' ); |
| 113 |
$tags = get_the_terms( $campaign_id, 'campaign_tag' ); |
| 114 |
|
| 115 |
// feature image. |
| 116 |
$thumbnail = get_the_post_thumbnail_url( $campaign_id, 'full' ); |
| 117 |
|
| 118 |
// Wrapper. |
| 119 |
$data = array( |
| 120 |
'post' => $campaign_post, |
| 121 |
'meta' => $meta_raw, |
| 122 |
'campaign_creator' => $author_data, |
| 123 |
'campaign_category' => $categories, |
| 124 |
'campaign_tag' => $tags, |
| 125 |
'thumbnail' => $thumbnail, |
| 126 |
); |
| 127 |
|
| 128 |
// Allow Addons to add to the Campaign export. |
| 129 |
$data = apply_filters( 'charitable_export_campaign_data', $data, $campaign_id ); |
| 130 |
|
| 131 |
// Set the proper headers. |
| 132 |
nocache_headers(); |
| 133 |
header( 'Content-Type: application/json; charset=utf-8' ); |
| 134 |
header( 'Content-Disposition: attachment; filename=charitable-campaign-' . $campaign_id . '-' . gmdate( 'm-d-Y' ) . '.json' ); |
| 135 |
header( 'Expires: 0' ); |
| 136 |
|
| 137 |
// Make the settings downloadable to a JSON file and die. |
| 138 |
die( wp_json_encode( $data ) ); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* This forces the charitable menu to be open for category and tag pages. |
| 143 |
* |
| 144 |
* @since 1.7.0.7 |
| 145 |
* |
| 146 |
* @return string |
| 147 |
*/ |
| 148 |
public function admin_accept_export_donations_request() { |
| 149 |
|
| 150 |
global $wpdb; |
| 151 |
|
| 152 |
if ( ! is_admin() || empty( $_POST ) ) { |
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
if ( ! isset( $_POST['charitable_nonce'] ) || ! wp_verify_nonce( $_POST['charitable_nonce'], 'export_donations_from_campaign' ) ) { // phpcs:ignore |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
if ( ! isset( $_POST['charitable_settings']['export'] ) ) { |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
$export_args = $_POST['charitable_settings']['export']; // phpcs:ignore |
| 165 |
|
| 166 |
if ( ! isset( $export_args['export_donations'] ) || intval( $export_args['export_donations'] ) === 0 ) { |
| 167 |
if ( charitable_is_debug() ) { |
| 168 |
error_log( 'admin_accept_export_donations_request: ' ); // phpcs:ignore |
| 169 |
error_log( print_r( $export_args, true ) ); // phpcs:ignore |
| 170 |
} |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
// Ignore the user aborting the action. |
| 175 |
ignore_user_abort( true ); |
| 176 |
|
| 177 |
// Grab the proper data. |
| 178 |
$campaign_id = ( isset( $export_args['export_donations'] ) ) ? absint( $export_args['export_donations'] ) : false; |
| 179 |
$campaign_post = get_post( $campaign_id ); |
| 180 |
|
| 181 |
// Metadata rows. |
| 182 |
$meta_sql = 'SELECT * FROM ' . $wpdb->prefix . 'charitable_campaign_donations WHERE campaign_id = ' . $campaign_id; |
| 183 |
$meta_campaign_donations = $wpdb->get_results( $meta_sql, OBJECT_K ); // phpcs:ignore |
| 184 |
|
| 185 |
if ( empty( $meta_campaign_donations ) ) { |
| 186 |
return; |
| 187 |
} |
| 188 |
|
| 189 |
$campaigns = array(); |
| 190 |
$donations = array(); |
| 191 |
foreach ( $meta_campaign_donations as $index => $campaign_donation ) { |
| 192 |
if ( ! array_key_exists( $campaign_donation->campaign_id, $campaigns ) ) { |
| 193 |
$campaigns[ $campaign_donation->campaign_id ] = array(); |
| 194 |
$campaigns[ $campaign_donation->campaign_id ]['donation_posts'] = array(); |
| 195 |
} |
| 196 |
$donation_post = get_post( $campaign_donation->donation_id ); |
| 197 |
if ( $donation_post ) : |
| 198 |
$campaigns[ $campaign_donation->campaign_id ]['donation_posts'][ $campaign_donation->donation_id ]['post'] = $donation_post; |
| 199 |
$meta_sql = "SELECT * FROM $wpdb->postmeta WHERE post_id = " . $campaign_donation->donation_id; |
| 200 |
$meta_raw = $wpdb->get_results( $meta_sql, ARRAY_A ); // phpcs:ignore |
| 201 |
|
| 202 |
// if 'meta_key' of '_donation_log' exists, then we need to dejson the value and remove any html, then rejson it. |
| 203 |
foreach ( $meta_raw as $meta_key => $meta_value ) { |
| 204 |
if ( ! empty( $meta_value['meta_key'] ) && $meta_value['meta_key'] === '_donation_log' ) { |
| 205 |
|
| 206 |
if ( empty( $meta_value['meta_value'] ) ) { |
| 207 |
continue; |
| 208 |
} |
| 209 |
$donation_log = maybe_unserialize( $meta_value['meta_value'] ); |
| 210 |
if ( ! empty( $donation_log ) ) { |
| 211 |
// this is an array. |
| 212 |
foreach ( $donation_log as $index => $log ) { |
| 213 |
$donation_log[ $index ] = array_map( 'strip_tags', $log ); |
| 214 |
} |
| 215 |
$donation_log = serialize( $donation_log ); |
| 216 |
$meta_raw[ $meta_key ]['meta_value'] = $donation_log; // phpcs:ignore |
| 217 |
} |
| 218 |
|
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
$campaigns[ $campaign_donation->campaign_id ]['donation_posts'][ $campaign_donation->donation_id ]['meta'] = $meta_raw; |
| 223 |
|
| 224 |
$author_data = false; |
| 225 |
$author_meta = ( $donation_post->post_author !== 0 ) ? get_userdata( $donation_post->post_author ) : false; |
| 226 |
|
| 227 |
if ( $author_meta ) { |
| 228 |
$author_data = $author_meta->data; |
| 229 |
unset( $author_data->user_pass ); |
| 230 |
} |
| 231 |
|
| 232 |
$campaigns[ $campaign_donation->campaign_id ]['donation_posts'][ $campaign_donation->donation_id ]['author'] = $author_data; |
| 233 |
endif; |
| 234 |
$campaigns[ $campaign_donation->campaign_id ]['campaign'] = get_post( $campaign_donation->campaign_id ); |
| 235 |
$campaigns[ $campaign_donation->campaign_id ]['charitable_campaign_donations'] = $meta_campaign_donations; |
| 236 |
} |
| 237 |
|
| 238 |
// Wrapper. |
| 239 |
$data = array( 'campaigns' => $campaigns ); |
| 240 |
|
| 241 |
// Allow Addons to add to the Campaign export. |
| 242 |
$data = apply_filters( 'charitable_export_campaign_donations_data', $data, $campaign_id ); |
| 243 |
|
| 244 |
// Set the proper headers. |
| 245 |
nocache_headers(); |
| 246 |
header( 'Content-Type: application/json; charset=utf-8' ); |
| 247 |
header( 'Content-Disposition: attachment; filename=charitable-donations-' . $campaign_id . '-' . gmdate( 'm-d-Y' ) . '.json' ); |
| 248 |
header( 'Expires: 0' ); |
| 249 |
|
| 250 |
// Make the settings downloadable to a JSON file and die. |
| 251 |
die( wp_json_encode( $data ) ); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
endif; |
| 256 |
|