| 1 |
<?php |
| 2 |
/** |
| 3 |
* Charitable AJAX Functions. |
| 4 |
* |
| 5 |
* Functions used with ajax hooks. |
| 6 |
* |
| 7 |
* @package Charitable/Functions/AJAX |
| 8 |
* @version 1.2.3 |
| 9 |
* @author Eric Daams |
| 10 |
* @copyright Copyright (c) 2019, Studio 164a |
| 11 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 12 |
*/ |
| 13 |
|
| 14 |
// Exit if accessed directly. |
| 15 |
if ( ! defined( 'ABSPATH' ) ) { exit; } |
| 16 |
|
| 17 |
if ( ! function_exists( 'charitable_ajax_get_donation_form' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Returns the donation form content for a particular campaign, through AJAX. |
| 21 |
* |
| 22 |
* @since 1.2.3 |
| 23 |
* |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
function charitable_ajax_get_donation_form() { |
| 27 |
if ( ! isset( $_POST['campaign_id'] ) ) { |
| 28 |
wp_send_json_error(); |
| 29 |
} |
| 30 |
|
| 31 |
/* Load the template files. */ |
| 32 |
require_once( charitable()->get_path( 'includes' ) . 'public/charitable-template-functions.php' ); |
| 33 |
require_once( charitable()->get_path( 'includes' ) . 'public/charitable-template-hooks.php' ); |
| 34 |
|
| 35 |
$campaign = new Charitable_Campaign( $_POST['campaign_id'] ); |
| 36 |
|
| 37 |
ob_start(); |
| 38 |
|
| 39 |
$campaign->get_donation_form()->render(); |
| 40 |
|
| 41 |
$output = ob_get_clean(); |
| 42 |
|
| 43 |
wp_send_json_success( $output ); |
| 44 |
|
| 45 |
die(); |
| 46 |
} |
| 47 |
|
| 48 |
endif; |
| 49 |
|
| 50 |
if ( ! function_exists( 'charitable_plupload_image_upload' ) ) : |
| 51 |
|
| 52 |
/** |
| 53 |
* Upload an image via plupload. |
| 54 |
* |
| 55 |
* @return void |
| 56 |
*/ |
| 57 |
function charitable_plupload_image_upload() { |
| 58 |
$post_id = (int) filter_input( INPUT_POST, 'post_id', FILTER_SANITIZE_NUMBER_INT ); |
| 59 |
$field_id = (string) filter_input( INPUT_POST, 'field_id' ); |
| 60 |
|
| 61 |
check_ajax_referer( 'charitable-upload-images-' . $field_id ); |
| 62 |
|
| 63 |
$file = $_FILES['async-upload']; |
| 64 |
$file_attr = wp_handle_upload( $file, array( 'test_form' => false ) ); |
| 65 |
|
| 66 |
if ( isset( $file_attr['error'] ) ) { |
| 67 |
wp_send_json_error( $file_attr ); |
| 68 |
} |
| 69 |
|
| 70 |
$attachment = array( |
| 71 |
'guid' => $file_attr['url'], |
| 72 |
'post_mime_type' => $file_attr['type'], |
| 73 |
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file['name'] ) ), |
| 74 |
'post_content' => '', |
| 75 |
'post_status' => 'inherit', |
| 76 |
); |
| 77 |
|
| 78 |
/** |
| 79 |
* Insert the file as an attachment. |
| 80 |
*/ |
| 81 |
$attachment_id = wp_insert_attachment( $attachment, $file_attr['file'], $post_id ); |
| 82 |
|
| 83 |
if ( is_wp_error( $attachment_id ) ) { |
| 84 |
wp_send_json_error(); |
| 85 |
} |
| 86 |
|
| 87 |
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file_attr['file'] ) ); |
| 88 |
|
| 89 |
$size = (string) filter_input( INPUT_POST, 'size' ); |
| 90 |
$max_uploads = (int) filter_input( INPUT_POST, 'max_uploads', FILTER_SANITIZE_NUMBER_INT ); |
| 91 |
|
| 92 |
if ( ! $size ) { |
| 93 |
$size = 'thumbnail'; |
| 94 |
} |
| 95 |
|
| 96 |
ob_start(); |
| 97 |
|
| 98 |
charitable_template( 'form-fields/picture-preview.php', array( |
| 99 |
'image' => $attachment_id, |
| 100 |
'field' => array( |
| 101 |
'key' => $field_id, |
| 102 |
'size' => $size, |
| 103 |
'max_uploads' => $max_uploads, |
| 104 |
), |
| 105 |
) ); |
| 106 |
|
| 107 |
wp_send_json_success( ob_get_clean() ); |
| 108 |
} |
| 109 |
|
| 110 |
endif; |
| 111 |
|
| 112 |
/** |
| 113 |
* Receives an AJAX request to load session content and returns |
| 114 |
* the output to be loaded. |
| 115 |
* |
| 116 |
* @since 1.5.0 |
| 117 |
* |
| 118 |
* @return void |
| 119 |
*/ |
| 120 |
function charitable_ajax_get_session_content() { |
| 121 |
if ( ! array_key_exists( 'templates', $_POST ) ) { |
| 122 |
wp_send_json_error( __( 'Missing templates in request.', 'charitable' ) ); |
| 123 |
} |
| 124 |
|
| 125 |
$output = array(); |
| 126 |
|
| 127 |
foreach ( $_POST['templates'] as $i => $template_args ) { |
| 128 |
if ( empty( $template_args ) || ! array_key_exists( 'template', $template_args ) ) { |
| 129 |
continue; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Get the output for the session content item. |
| 134 |
* |
| 135 |
* @since 1.5.0 |
| 136 |
* |
| 137 |
* @param false|string $content The content to return, or a false in case of failure. |
| 138 |
* @param array $args Mixed set of arguments. |
| 139 |
*/ |
| 140 |
$output[ $i ] = apply_filters( 'charitable_session_content_' . $template_args['template'], false, $template_args ); |
| 141 |
} |
| 142 |
|
| 143 |
wp_send_json_success( $output ); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Return the donation receipt. |
| 148 |
* |
| 149 |
* @since 1.5.0 |
| 150 |
* |
| 151 |
* @param string|false $content Content to return, or false in case of failure. |
| 152 |
* @param array $args Mixed array of args. |
| 153 |
* @return string|false |
| 154 |
*/ |
| 155 |
function charitable_ajax_get_session_donation_receipt( $content, $args ) { |
| 156 |
if ( ! array_key_exists( 'donation_id', $args ) ) { |
| 157 |
return $content; |
| 158 |
} |
| 159 |
|
| 160 |
$donation = charitable_get_donation( $args['donation_id'] ); |
| 161 |
|
| 162 |
if ( ! $donation ) { |
| 163 |
return $content; |
| 164 |
} |
| 165 |
|
| 166 |
return charitable_template_donation_receipt_output( '', $donation ); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Return the donation form's amount field. |
| 171 |
* |
| 172 |
* @since 1.5.0 |
| 173 |
* |
| 174 |
* @param string|false $content Content to return, or false in case of failure. |
| 175 |
* @param array $args Mixed array of args. |
| 176 |
* @return string|false |
| 177 |
*/ |
| 178 |
function charitable_ajax_get_session_donation_form_amount_field( $content, $args ) { |
| 179 |
if ( ! array_key_exists( 'campaign_id', $args ) ) { |
| 180 |
return $content; |
| 181 |
} |
| 182 |
|
| 183 |
if ( ! array_key_exists( 'form_id', $args ) ) { |
| 184 |
return $content; |
| 185 |
} |
| 186 |
|
| 187 |
ob_start(); |
| 188 |
|
| 189 |
charitable_template( 'donation-form/donation-amount-list.php', |
| 190 |
array( |
| 191 |
'campaign' => charitable_get_campaign( $args['campaign_id'] ), |
| 192 |
'form_id' => $args['form_id'], |
| 193 |
) |
| 194 |
); |
| 195 |
|
| 196 |
return ob_get_clean(); |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Return the donation form's amount field. |
| 201 |
* |
| 202 |
* @since 1.5.0 |
| 203 |
* |
| 204 |
* @param string|false $content Content to return, or false in case of failure. |
| 205 |
* @param array $args Mixed array of args. |
| 206 |
* @return string|false |
| 207 |
*/ |
| 208 |
function charitable_ajax_get_session_donation_form_current_amount_text( $content, $args ) { |
| 209 |
if ( ! array_key_exists( 'campaign_id', $args ) ) { |
| 210 |
return $content; |
| 211 |
} |
| 212 |
|
| 213 |
if ( ! array_key_exists( 'form_id', $args ) ) { |
| 214 |
return $content; |
| 215 |
} |
| 216 |
|
| 217 |
$amount = charitable_get_campaign( $args['campaign_id'] )->get_donation_amount_in_session(); |
| 218 |
|
| 219 |
return charitable_template_donation_form_current_amount_text( $amount, $args['form_id'], $args['campaign_id'] ); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Return the error messages. |
| 224 |
* |
| 225 |
* @since 1.5.0 |
| 226 |
* |
| 227 |
* @param string|false $content Content to return, or false in case of failure. |
| 228 |
* @return string|false |
| 229 |
*/ |
| 230 |
function charitable_ajax_get_session_errors( $content ) { |
| 231 |
$errors = charitable_get_notices()->get_errors(); |
| 232 |
|
| 233 |
if ( empty( $errors ) ) { |
| 234 |
return $content; |
| 235 |
} |
| 236 |
|
| 237 |
ob_start(); |
| 238 |
|
| 239 |
charitable_template( 'form-fields/errors.php', array( |
| 240 |
'errors' => $errors, |
| 241 |
) ); |
| 242 |
|
| 243 |
return ob_get_clean(); |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Return the notices |
| 248 |
* |
| 249 |
* @since 1.5.0 |
| 250 |
* |
| 251 |
* @param string|false $content Content to return, or false in case of failure. |
| 252 |
* @return string|false |
| 253 |
*/ |
| 254 |
function charitable_ajax_get_session_notices( $content ) { |
| 255 |
$notices = charitable_get_notices()->get_notices(); |
| 256 |
|
| 257 |
if ( empty( $notices ) ) { |
| 258 |
return $content; |
| 259 |
} |
| 260 |
|
| 261 |
ob_start(); |
| 262 |
|
| 263 |
charitable_template( 'form-fields/notices.php', array( |
| 264 |
'notices' => $notices, |
| 265 |
) ); |
| 266 |
|
| 267 |
return ob_get_clean(); |
| 268 |
} |
| 269 |
|