PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.9
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.9
1.8.13.1 1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 All 212 releases
charitable / includes / ajax / charitable-ajax-functions.php

charitable-ajax-functions.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.7.0.9, at includes/ajax/charitable-ajax-functions.php

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