| 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) 2022, 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 |
|
| 44 |
/** |
| 45 |
* Returns and/or create the single instance of this class. |
| 46 |
* |
| 47 |
* @since 1.7.0.7 |
| 48 |
* |
| 49 |
* @return Charitable_Export_Items |
| 50 |
*/ |
| 51 |
public static function get_instance() { |
| 52 |
if ( is_null( self::$instance ) ) { |
| 53 |
self::$instance = new self(); |
| 54 |
} |
| 55 |
|
| 56 |
return self::$instance; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* This forces the charitable menu to be open for category and tag pages. |
| 61 |
* |
| 62 |
* @since 1.7.0.7 |
| 63 |
* |
| 64 |
* @return string |
| 65 |
*/ |
| 66 |
public function admin_accept_export_campaign_request() { |
| 67 |
|
| 68 |
global $wpdb; |
| 69 |
|
| 70 |
if ( ! is_admin() || empty( $_POST ) ) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
// 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 |
| 75 |
|
| 76 |
if (! isset( $_POST['charitable_nonce'] ) || ! wp_verify_nonce( $_POST['charitable_nonce'], 'export_campaign' ) ) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
if ( ! isset( $_POST['charitable_settings']['tools'] ) ) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
$export_args = $_POST['charitable_settings']['tools']; |
| 85 |
|
| 86 |
if ( ! isset( $export_args['export_campaign'] ) || intval( $export_args['export_campaign'] ) === 0 ) { |
| 87 |
if ( defined( 'CHARITABLE_DEBUG' ) && CHARITABLE_DEBUG ) { |
| 88 |
error_log ('admin_accept_export_campaign_request: '); |
| 89 |
error_log ( print_r( $export_args, true ) ); |
| 90 |
} |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
// Ignore the user aborting the action. |
| 95 |
ignore_user_abort( true ); |
| 96 |
|
| 97 |
// Grab the proper data. |
| 98 |
$campaign_id = ( isset( $export_args['export_campaign'] ) ) ? absint( $export_args['export_campaign'] ) : false; |
| 99 |
$campaign_post = get_post( $campaign_id ); |
| 100 |
|
| 101 |
// Metadata rows |
| 102 |
$meta_sql = "SELECT * FROM $wpdb->postmeta WHERE meta_key LIKE '_campaign_%' AND post_id = " . $campaign_id; |
| 103 |
$meta_raw = $wpdb->get_results( $wpdb->prepare( $meta_sql ), OBJECT_K ); |
| 104 |
|
| 105 |
// author / campaign_creator |
| 106 |
$author_data = array(); |
| 107 |
$author_meta = ( $campaign_post->post_author !== 0 ) ? get_userdata( $campaign_post->post_author ) : array(); |
| 108 |
if ( $author_meta ) { |
| 109 |
$author_data = $author_meta->data; |
| 110 |
unset( $author_data->user_pass ); |
| 111 |
} |
| 112 |
|
| 113 |
// categories and tags |
| 114 |
$categories = get_the_terms( $campaign_id, 'campaign_category' ); |
| 115 |
$tags = get_the_terms( $campaign_id, 'campaign_tag' ); |
| 116 |
|
| 117 |
// feature image |
| 118 |
$thumbnail = get_the_post_thumbnail_url( $campaign_id, 'full' ); |
| 119 |
|
| 120 |
// Wrapper |
| 121 |
$data = array( 'post' => $campaign_post, 'meta' => $meta_raw, 'campaign_creator' => $author_data, 'campaign_category' => $categories, 'campaign_tag' => $tags, 'thumbnail' => $thumbnail ); |
| 122 |
|
| 123 |
// Allow Addons to add to the Campaign export. |
| 124 |
$data = apply_filters( 'charitable_export_campaign_data', $data, $campaign_id ); |
| 125 |
|
| 126 |
// Set the proper headers. |
| 127 |
nocache_headers(); |
| 128 |
header( 'Content-Type: application/json; charset=utf-8' ); |
| 129 |
header( 'Content-Disposition: attachment; filename=charitable-campaign-' . $campaign_id . '-' . gmdate( 'm-d-Y' ) . '.json' ); |
| 130 |
header( 'Expires: 0' ); |
| 131 |
|
| 132 |
// Make the settings downloadable to a JSON file and die. |
| 133 |
die( wp_json_encode( $data ) ); |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* This forces the charitable menu to be open for category and tag pages. |
| 139 |
* |
| 140 |
* @since 1.7.0.7 |
| 141 |
* |
| 142 |
* @return string |
| 143 |
*/ |
| 144 |
public function admin_accept_export_donations_request() { |
| 145 |
|
| 146 |
global $wpdb; |
| 147 |
|
| 148 |
if ( ! is_admin() || empty( $_POST ) ) { |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
// 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 |
| 153 |
|
| 154 |
if ( ! isset( $_POST['charitable_nonce'] ) || ! wp_verify_nonce( $_POST['charitable_nonce'], 'export_donations_from_campaign' ) ) { |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
if ( ! isset( $_POST['charitable_settings']['tools'] ) ) { |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
$export_args = $_POST['charitable_settings']['tools']; |
| 163 |
|
| 164 |
if ( ! isset( $export_args['export_donations'] ) || intval( $export_args['export_donations'] ) === 0 ) { |
| 165 |
if ( defined( 'CHARITABLE_DEBUG' ) && CHARITABLE_DEBUG ) { |
| 166 |
error_log ('admin_accept_export_donations_request: '); |
| 167 |
error_log ( print_r( $export_args, true ) ); |
| 168 |
} |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
// Ignore the user aborting the action. |
| 173 |
ignore_user_abort( true ); |
| 174 |
|
| 175 |
// Grab the proper data. |
| 176 |
$campaign_id = ( isset( $export_args['export_donations'] ) ) ? absint( $export_args['export_donations'] ) : false; |
| 177 |
$campaign_post = get_post( $campaign_id ); |
| 178 |
|
| 179 |
// Metadata rows |
| 180 |
$meta_sql = "SELECT * FROM " . $wpdb->prefix . "charitable_campaign_donations WHERE campaign_id = " . $campaign_id; |
| 181 |
$meta_campaign_donations = $wpdb->get_results( $meta_sql, OBJECT_K ); |
| 182 |
|
| 183 |
if ( empty( $meta_campaign_donations ) ) { |
| 184 |
return; |
| 185 |
} |
| 186 |
|
| 187 |
$campaigns = array(); |
| 188 |
$donations = array(); |
| 189 |
foreach ( $meta_campaign_donations as $index => $campaign_donation ) { |
| 190 |
if ( ! array_key_exists( $campaign_donation->campaign_id, $campaigns ) ) { |
| 191 |
$campaigns[ $campaign_donation->campaign_id ] = array(); |
| 192 |
$campaigns[ $campaign_donation->campaign_id ]['donation_posts'] = array(); |
| 193 |
} |
| 194 |
$donation_post = get_post( $campaign_donation->donation_id ); |
| 195 |
if ( $donation_post ) : |
| 196 |
$campaigns[ $campaign_donation->campaign_id ]['donation_posts'][ $campaign_donation->donation_id ]['post'] = $donation_post; |
| 197 |
$meta_sql = "SELECT * FROM $wpdb->postmeta WHERE post_id = " . $campaign_donation->donation_id; |
| 198 |
$meta_raw = $wpdb->get_results( $meta_sql, ARRAY_A ); |
| 199 |
$campaigns[ $campaign_donation->campaign_id ]['donation_posts'][ $campaign_donation->donation_id ]['meta'] = $meta_raw; |
| 200 |
|
| 201 |
$author_data = false; |
| 202 |
$author_meta = ( $donation_post->post_author !== 0 ) ? get_userdata( $donation_post->post_author ) : false; |
| 203 |
|
| 204 |
if ( $author_meta ) { |
| 205 |
$author_data = $author_meta->data; |
| 206 |
unset( $author_data->user_pass ); |
| 207 |
} |
| 208 |
|
| 209 |
$campaigns[ $campaign_donation->campaign_id ]['donation_posts'][ $campaign_donation->donation_id ]['author'] = $author_data; |
| 210 |
endif; |
| 211 |
$campaigns[ $campaign_donation->campaign_id ]['campaign'] = get_post( $campaign_donation->campaign_id ); |
| 212 |
$campaigns[ $campaign_donation->campaign_id ]['charitable_campaign_donations'] = $meta_campaign_donations; |
| 213 |
} |
| 214 |
|
| 215 |
// Wrapper |
| 216 |
$data = array( 'campaigns' => $campaigns ); |
| 217 |
|
| 218 |
// Allow Addons to add to the Campaign export. |
| 219 |
$data = apply_filters( 'charitable_export_campaign_donations_data', $data, $campaign_id ); |
| 220 |
|
| 221 |
// Set the proper headers. |
| 222 |
nocache_headers(); |
| 223 |
header( 'Content-Type: application/json; charset=utf-8' ); |
| 224 |
header( 'Content-Disposition: attachment; filename=charitable-donations-' . $campaign_id . '-' . gmdate( 'm-d-Y' ) . '.json' ); |
| 225 |
header( 'Expires: 0' ); |
| 226 |
|
| 227 |
// Make the settings downloadable to a JSON file and die. |
| 228 |
die( wp_json_encode( $data ) ); |
| 229 |
|
| 230 |
} |
| 231 |
|
| 232 |
} |
| 233 |
|
| 234 |
endif; |
| 235 |
|