PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.12
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.12
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 / admin / donations / class-charitable-donation-meta-boxes.php

class-charitable-donation-meta-boxes.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.7.0.12, at includes/admin/donations/class-charitable-donation-meta-boxes.php

566 lines 15.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Sets up the donation meta boxes.
4 *
5 * @package Charitable/Classes/Charitable_Donation_Meta_Boxes
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.5.0
10 * @version 1.6.53
11 */
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 if ( ! class_exists( 'Charitable_Donation_Meta_Boxes' ) ) :
18
19 /**
20 * Charitable_Donation_Meta_Boxes class.
21 *
22 * @final
23 * @since 1.5.0
24 */
25 final class Charitable_Donation_Meta_Boxes {
26
27 /**
28 * The single instance of this class.
29 *
30 * @var Charitable_Donation_Meta_Boxes|null
31 */
32 private static $instance = null;
33
34 /**
35 * @var Charitable_Meta_Box_Helper $meta_box_helper
36 */
37 private $meta_box_helper;
38
39 /**
40 * Create object instance.
41 *
42 * @since 1.5.0
43 *
44 * @param Charitable_Meta_Box_Helper $helper The meta box helper class.
45 */
46 public function __construct( Charitable_Meta_Box_Helper $helper ) {
47 $this->meta_box_helper = $helper;
48 }
49
50 /**
51 * Returns and/or create the single instance of this class.
52 *
53 * @since 1.5.0
54 *
55 * @return Charitable_Donation_Meta_Boxes
56 */
57 public static function get_instance() {
58 if ( is_null( self::$instance ) ) {
59 self::$instance = new self(
60 new Charitable_Meta_Box_Helper( 'charitable-donation' )
61 );
62 }
63
64 return self::$instance;
65 }
66
67 /**
68 * Sets up the meta boxes to display on the donation admin page.
69 *
70 * @since 1.5.0
71 *
72 * @return void
73 */
74 public function add_meta_boxes() {
75 foreach ( $this->get_meta_boxes() as $meta_box_id => $meta_box ) {
76 add_meta_box(
77 $meta_box_id,
78 $meta_box['title'],
79 array( $this->meta_box_helper, 'metabox_display' ),
80 Charitable::DONATION_POST_TYPE,
81 $meta_box['context'],
82 $meta_box['priority'],
83 $meta_box
84 );
85 }
86 }
87
88 /**
89 * Remove default meta boxes.
90 *
91 * @since 1.5.0
92 *
93 * @global array $wp_meta_boxes Registered meta boxes in WP.
94 * @return void
95 */
96 public function remove_meta_boxes() {
97 global $wp_meta_boxes;
98
99 $charitable_meta_boxes = $this->get_meta_boxes();
100
101 foreach ( $wp_meta_boxes[ Charitable::DONATION_POST_TYPE ] as $context => $priorities ) {
102 foreach ( $priorities as $priority => $meta_boxes ) {
103 foreach ( $meta_boxes as $meta_box_id => $meta_box ) {
104 if ( ! isset( $charitable_meta_boxes[ $meta_box_id ] ) ) {
105 remove_meta_box( $meta_box_id, Charitable::DONATION_POST_TYPE, $context );
106 }
107 }
108 }
109 }
110 }
111
112 /**
113 * Returns an array of all meta boxes added to the donation post type screen.
114 *
115 * @since 1.5.0
116 *
117 * @return array
118 */
119 private function get_meta_boxes() {
120 $screen = get_current_screen();
121
122 if ( 'donation' == $screen->post_type && ( 'add' == $screen->action || isset( $_GET['show_form'] ) ) ) {
123 $meta_boxes = $this->get_form_meta_box();
124 } else {
125 $meta_boxes = $this->get_view_meta_boxes();
126 }
127
128 /**
129 * Filter the meta boxes to be displayed on a donation overview page.
130 *
131 * @since 1.0.0
132 *
133 * @param array $meta_boxes The array of meta boxes and their details.
134 */
135 return apply_filters( 'charitable_donation_meta_boxes', $meta_boxes );
136 }
137
138 /**
139 * Return the form meta box.
140 *
141 * @since 1.5.0
142 *
143 * @return array
144 */
145 public function get_form_meta_box() {
146 global $post;
147
148 $form = new Charitable_Admin_Donation_Form( charitable_get_donation( $post->ID ) );
149 $meta_boxes = array(
150 'donation-form' => array(
151 'title' => __( 'Donation Form', 'charitable' ),
152 'context' => 'normal',
153 'priority' => 'high',
154 'view' => 'metaboxes/donation/donation-form',
155 'form' => $form,
156 ),
157 'donation-form-meta' => array(
158 'title' => __( 'Additional Details', 'charitable' ),
159 'context' => 'side',
160 'priority' => 'high',
161 'view' => 'metaboxes/donation/donation-form-meta',
162 'form' => $form,
163 ),
164 );
165
166 /**
167 * Filter the meta boxes to be displayed on a donation add/edit page.
168 *
169 * @since 1.0.0
170 *
171 * @param array $meta_boxes The array of meta boxes and their details.
172 */
173 return apply_filters( 'charitable_donation_form_meta_boxes', $meta_boxes );
174 }
175
176 /**
177 * Return the view meta boxes.
178 *
179 * @since 1.5.0
180 * @since 1.7.0.8
181 *
182 * @return array
183 */
184 public function get_view_meta_boxes() {
185 global $post;
186
187 $meta_boxes = array(
188 'donation-overview' => array(
189 'title' => __( 'Donation Overview', 'charitable' ),
190 'context' => 'normal',
191 'priority' => 'high',
192 'view' => 'metaboxes/donation/donation-overview',
193 ),
194 'donation-actions' => array(
195 'title' => __( 'Donation Actions', 'charitable' ),
196 'context' => 'side',
197 'priority' => 'high',
198 'view' => 'metaboxes/actions',
199 'actions' => charitable_get_donation_actions(),
200 ),
201 'donation-details' => array(
202 'title' => __( 'Donation Details', 'charitable' ),
203 'context' => 'side',
204 'priority' => 'high',
205 'view' => 'metaboxes/donation/donation-details',
206 ),
207 'donation-log' => array(
208 'title' => __( 'Donation Log', 'charitable' ),
209 'context' => 'normal',
210 'priority' => 'low',
211 'view' => 'metaboxes/donation/donation-log',
212 ),
213 'donation-donor-history' => array(
214 'title' => __( 'Related Donations <span class="badge beta">Beta</span>', 'charitable' ),
215 'context' => 'side',
216 'priority' => 'low',
217 'view' => 'metaboxes/donation/donation-donor-history',
218 'help' => ''
219 ),
220 );
221
222 /* Get rid of the donation actions meta box if it doesn't apply to this donation. */
223 if ( ! charitable_get_donation_actions()->has_available_actions( $post->ID ) ) {
224 unset( $meta_boxes['donation-actions'] );
225 }
226
227 /**
228 * Filter the meta boxes to be displayed on a donation overview page.
229 *
230 * @since 1.5.0
231 *
232 * @param array $meta_boxes The array of meta boxes and their details.
233 */
234 return apply_filters( 'charitable_donation_view_meta_boxes', $meta_boxes );
235 }
236
237 /**
238 * Create donation actions instance, with some initial defaults.
239 *
240 * @since 1.6.0
241 *
242 * @return void
243 */
244 public function register_donation_actions() {
245 $donation_actions = charitable_get_donation_actions();
246
247 foreach ( charitable_get_valid_donation_statuses() as $status => $label ) {
248 $donation_actions->register(
249 'change_status_to_' . $status,
250 array(
251 'label' => $label,
252 'callback' => array( $this, 'change_donation_status' ),
253 'button_text' => __( 'Update Status', 'charitable' ),
254 'active_callback' => array( $this, 'can_change_donation_status' ),
255 'success_message' => 13,
256 'failed_message' => 14,
257 'fields' => array( $this, 'get_donation_status_change_fields' ),
258 ),
259 __( 'Change Status', 'charitable' )
260 );
261 }
262 }
263
264 /**
265 * Check whether a particular status change works for the given donation.
266 *
267 * @since 1.6.0
268 *
269 * @param int $object_id The donation's ID.
270 * @param array $args Mixed action arguments.
271 * @return boolean
272 */
273 public function can_change_donation_status( $object_id, $args = array() ) {
274 $donation = charitable_get_donation( $object_id );
275
276 return $donation && $args['action_args']['label'] !== $donation->get_status_label();
277 }
278
279 /**
280 * Change a donation's status.
281 *
282 * @since 1.6.0
283 *
284 * @param boolean $success Whether the action has been run.
285 * @param int $object_id The donation's ID.
286 * @param array $args Mixed action arguments.
287 * @param string $action The action id.
288 * @return boolean Whether the status was changed.
289 */
290 public function change_donation_status( $success, $object_id, $args, $action ) {
291 $donation = charitable_get_donation( $object_id );
292
293 if ( ! $donation ) {
294 return false;
295 }
296
297 $status = str_replace( 'change_status_to_', '', $action );
298 $success = $donation->update_status( $status );
299
300 if ( array_key_exists( 'gateway_refund', $_POST ) && $_POST['gateway_refund'] ) {
301 $gateway = $donation->get_gateway();
302
303 /**
304 * Perform a refund in the donation's gateway.
305 *
306 * @since 1.6.0
307 *
308 * @param int $donation_id The donation's ID.
309 */
310 do_action( 'charitable_process_refund_' . $gateway, $object_id );
311 }
312
313 return 0 !== $success;
314 }
315
316 /**
317 * Additional fields to display for a status change.
318 *
319 * @since 1.6.0
320 *
321 * @param int $object_id The donation's ID.
322 * @param array $action Mixed action arguments.
323 * @return void
324 */
325 public function get_donation_status_change_fields( $object_id, $action ) {
326 $statuses = charitable_get_valid_donation_statuses();
327
328 if ( $statuses['charitable-refunded'] != $action['label'] ) {
329 return;
330 }
331
332 $donation = charitable_get_donation( $object_id );
333
334 if ( ! $donation || ! $donation->is_refundable_in_gateway() ) {
335 return;
336 }
337
338 /* translators: %s: gateway name */
339 printf( '%s' . __( 'Refund in %s automatically.', 'charitable' ),
340 '<input type="checkbox" name="gateway_refund" value="1" />',
341 $donation->get_gateway_object()->get_name()
342 );
343 }
344
345 /**
346 * Save meta for the donation.
347 *
348 * @since 1.5.0
349 *
350 * @param int $donation_id
351 * @param WP_Post $post
352 * @return void
353 */
354 public function save_donation( $donation_id, WP_Post $post ) {
355 if ( ! $this->meta_box_helper->user_can_save( $donation_id ) ) {
356 return;
357 }
358
359 $this->maybe_save_form_submission( $donation_id );
360
361 /* Handle any fired actions */
362 if ( ! empty( $_POST['charitable_donation_action'] ) ) {
363 charitable_get_donation_actions()->do_action( sanitize_text_field( $_POST['charitable_donation_action'] ), $donation_id );
364 }
365
366 /**
367 * Hook for plugins to do something else with the posted data.
368 *
369 * @since 1.0.0
370 *
371 * @param int $donation_id The donation ID.
372 * @param WP_Post $post Instance of `WP_Post`.
373 */
374 do_action( 'charitable_donation_save', $donation_id, $post );
375 }
376
377 /**
378 * Save a donation after the admin donation form has been submitted.
379 *
380 * @since 1.5.0
381 *
382 * @param int $donation_id The donation ID.
383 * @return boolean True if this was a form submission. False otherwise.
384 */
385 public function maybe_save_form_submission( $donation_id ) {
386 if ( ! $this->is_admin_donation_save() || did_action( 'charitable_before_save_donation' ) ) {
387 return false;
388 }
389
390 $form = new Charitable_Admin_Donation_Form( charitable_get_donation( $donation_id ) );
391
392 if ( ! $form->validate_submission() ) {
393 wp_safe_redirect( admin_url( 'post-new.php?post_type=donation&show_form=1' ) );
394 exit();
395 }
396
397 charitable_create_donation( $form->get_donation_values() );
398
399 update_post_meta( $donation_id, '_donation_manually_edited', true );
400
401 return true;
402 }
403
404 /**
405 * Change messages when a post type is updated.
406 *
407 * @since 1.5.0
408 *
409 * @param array $messages The post messages.
410 * @return array
411 */
412 public function post_messages( $messages ) {
413 global $post, $post_ID;
414
415 $messages[ Charitable::DONATION_POST_TYPE ] = array(
416 0 => '', // Unused. Messages start at index 1.
417 1 => sprintf(
418 /* translators: %s: link */
419 __( 'Donation updated. <a href="%s">View Donation</a>', 'charitable' ),
420 esc_url( get_permalink( $post_ID ) )
421 ),
422 2 => __( 'Custom field updated.', 'charitable' ),
423 3 => __( 'Custom field deleted.', 'charitable' ),
424 4 => __( 'Donation updated.', 'charitable' ),
425 5 => isset( $_GET['revision'] )
426 ? sprintf(
427 /* translators: %s: revision title */
428 __( 'Donation restored to revision from %s', 'charitable' ),
429 wp_post_revision_title( (int) $_GET['revision'], false )
430 )
431 : false,
432 6 => sprintf(
433 /* translators: %s: link */
434 __( 'Donation published. <a href="%s">View Donation</a>', 'charitable' ),
435 esc_url( get_permalink( $post_ID ) )
436 ),
437 7 => __( 'Donation saved.', 'charitable' ),
438 8 => sprintf(
439 /* translators: %s: link */
440 __( 'Donation submitted. <a target="_blank" href="%s">Preview Donation</a>', 'charitable' ),
441 esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) )
442 ),
443 9 => sprintf(
444 /* translators: %1$s: date and time; %2$s: link */
445 __( 'Donation scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview Donation</a>', 'charitable' ),
446 date_i18n( __( 'M j, Y @ G:i', 'charitable' ), strtotime( $post->post_date ) ),
447 esc_url( get_permalink( $post_ID ) )
448 ),
449 10 => sprintf(
450 /* translators: %s: link */
451 __( 'Donation draft updated. <a target="_blank" href="%s">Preview Donation</a>', 'charitable' ),
452 esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) )
453 ),
454 11 => __( 'Email resent.', 'charitable' ),
455 12 => __( 'Email could not be resent.', 'charitable' ),
456 13 => __( 'Donation status changed.', 'charitable' ),
457 14 => __( 'Donation status could not be changed.', 'charitable' ),
458 );
459
460 return $messages;
461 }
462
463 /**
464 * Prevent email from sending if we are saving the donation via the admin.
465 *
466 * @since 1.6.15
467 *
468 * @param boolean $send_email Whether to send the email.
469 * @param Charitable_Donation $donation The donation object.
470 * @return boolean
471 */
472 public function maybe_block_new_donation_email( $send_email, Charitable_Donation $donation ) {
473 if ( ! $send_email ) {
474 return $send_email;
475 }
476
477 /* Don't block sending it from donation actions. */
478 if ( $this->is_donation_action() ) {
479 return $send_email;
480 }
481
482 /* If we're not saving the donation, send the email. */
483 if ( ! $this->is_admin_donation_save() ) {
484 return $send_email;
485 }
486
487 /* If this isn't the `charitable_after_save_donation` hook, don't send the email. */
488 if ( ! doing_action( 'charitable_after_save_donation' ) ) {
489 return false;
490 }
491
492 /* If this isn't a manually created donation, send the email. */
493 if ( 'manual' !== $donation->get_gateway() ) {
494 return $send_email;
495 }
496
497 /* Do not send admin notifications for manually created donations. */
498 return false;
499 }
500
501 /**
502 * Prevent email from sending if we are saving the donation via the admin.
503 *
504 * @since 1.6.15
505 *
506 * @param boolean $send_email Whether to send the email.
507 * @param Charitable_Donation $donation The donation object.
508 * @return boolean
509 */
510 public function maybe_block_donation_receipt_email( $send_email, Charitable_Donation $donation ) {
511 if ( ! $send_email ) {
512 return $send_email;
513 }
514
515 /* Don't block sending it from donation actions. */
516 if ( $this->is_donation_action() ) {
517 return $send_email;
518 }
519
520 /* If we're not saving the donation, send the email. */
521 if ( ! $this->is_admin_donation_save() ) {
522 return $send_email;
523 }
524
525 /* If this isn't the `charitable_after_save_donation` hook, don't send the email. */
526 if ( ! doing_action( 'charitable_after_save_donation' ) ) {
527 return false;
528 }
529
530 /* If this isn't a manually created donation, send the email. */
531 if ( 'manual' !== $donation->get_gateway() ) {
532 return $send_email;
533 }
534
535 /**
536 * Finally, if we're saving a manually created donation, only
537 * send the email if that option was checked in the admin.
538 */
539 return array_key_exists( 'send_donation_receipt', $_POST ) && $_POST['send_donation_receipt'];
540 }
541
542 /**
543 * Checks whether we are saving the admin donation form.
544 *
545 * @since 1.6.15
546 *
547 * @return boolean
548 */
549 public function is_admin_donation_save() {
550 return array_key_exists( 'charitable_action', $_POST );
551 }
552
553 /**
554 * Checks whether we are doing a donation action.
555 *
556 * @since 1.6.15
557 *
558 * @return boolean
559 */
560 public function is_donation_action() {
561 return array_key_exists( 'charitable_donation_action', $_POST );
562 }
563 }
564
565 endif;
566