PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.12.2
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.12.2
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.8.12.2, at includes/ajax/charitable-ajax-functions.php

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