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 / 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.9, at includes/admin/donations/class-charitable-donation-meta-boxes.php

568 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 // echo 'made it'; exit;
391
392 $form = new Charitable_Admin_Donation_Form( charitable_get_donation( $donation_id ) );
393
394 if ( ! $form->validate_submission() ) {
395 wp_safe_redirect( admin_url( 'post-new.php?post_type=donation&show_form=1' ) );
396 exit();
397 }
398
399 charitable_create_donation( $form->get_donation_values() );
400
401 update_post_meta( $donation_id, '_donation_manually_edited', true );
402
403 return true;
404 }
405
406 /**
407 * Change messages when a post type is updated.
408 *
409 * @since 1.5.0
410 *
411 * @param array $messages The post messages.
412 * @return array
413 */
414 public function post_messages( $messages ) {
415 global $post, $post_ID;
416
417 $messages[ Charitable::DONATION_POST_TYPE ] = array(
418 0 => '', // Unused. Messages start at index 1.
419 1 => sprintf(
420 /* translators: %s: link */
421 __( 'Donation updated. <a href="%s">View Donation</a>', 'charitable' ),
422 esc_url( get_permalink( $post_ID ) )
423 ),
424 2 => __( 'Custom field updated.', 'charitable' ),
425 3 => __( 'Custom field deleted.', 'charitable' ),
426 4 => __( 'Donation updated.', 'charitable' ),
427 5 => isset( $_GET['revision'] )
428 ? sprintf(
429 /* translators: %s: revision title */
430 __( 'Donation restored to revision from %s', 'charitable' ),
431 wp_post_revision_title( (int) $_GET['revision'], false )
432 )
433 : false,
434 6 => sprintf(
435 /* translators: %s: link */
436 __( 'Donation published. <a href="%s">View Donation</a>', 'charitable' ),
437 esc_url( get_permalink( $post_ID ) )
438 ),
439 7 => __( 'Donation saved.', 'charitable' ),
440 8 => sprintf(
441 /* translators: %s: link */
442 __( 'Donation submitted. <a target="_blank" href="%s">Preview Donation</a>', 'charitable' ),
443 esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) )
444 ),
445 9 => sprintf(
446 /* translators: %1$s: date and time; %2$s: link */
447 __( 'Donation scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview Donation</a>', 'charitable' ),
448 date_i18n( __( 'M j, Y @ G:i', 'charitable' ), strtotime( $post->post_date ) ),
449 esc_url( get_permalink( $post_ID ) )
450 ),
451 10 => sprintf(
452 /* translators: %s: link */
453 __( 'Donation draft updated. <a target="_blank" href="%s">Preview Donation</a>', 'charitable' ),
454 esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) )
455 ),
456 11 => __( 'Email resent.', 'charitable' ),
457 12 => __( 'Email could not be resent.', 'charitable' ),
458 13 => __( 'Donation status changed.', 'charitable' ),
459 14 => __( 'Donation status could not be changed.', 'charitable' ),
460 );
461
462 return $messages;
463 }
464
465 /**
466 * Prevent email from sending if we are saving the donation via the admin.
467 *
468 * @since 1.6.15
469 *
470 * @param boolean $send_email Whether to send the email.
471 * @param Charitable_Donation $donation The donation object.
472 * @return boolean
473 */
474 public function maybe_block_new_donation_email( $send_email, Charitable_Donation $donation ) {
475 if ( ! $send_email ) {
476 return $send_email;
477 }
478
479 /* Don't block sending it from donation actions. */
480 if ( $this->is_donation_action() ) {
481 return $send_email;
482 }
483
484 /* If we're not saving the donation, send the email. */
485 if ( ! $this->is_admin_donation_save() ) {
486 return $send_email;
487 }
488
489 /* If this isn't the `charitable_after_save_donation` hook, don't send the email. */
490 if ( ! doing_action( 'charitable_after_save_donation' ) ) {
491 return false;
492 }
493
494 /* If this isn't a manually created donation, send the email. */
495 if ( 'manual' !== $donation->get_gateway() ) {
496 return $send_email;
497 }
498
499 /* Do not send admin notifications for manually created donations. */
500 return false;
501 }
502
503 /**
504 * Prevent email from sending if we are saving the donation via the admin.
505 *
506 * @since 1.6.15
507 *
508 * @param boolean $send_email Whether to send the email.
509 * @param Charitable_Donation $donation The donation object.
510 * @return boolean
511 */
512 public function maybe_block_donation_receipt_email( $send_email, Charitable_Donation $donation ) {
513 if ( ! $send_email ) {
514 return $send_email;
515 }
516
517 /* Don't block sending it from donation actions. */
518 if ( $this->is_donation_action() ) {
519 return $send_email;
520 }
521
522 /* If we're not saving the donation, send the email. */
523 if ( ! $this->is_admin_donation_save() ) {
524 return $send_email;
525 }
526
527 /* If this isn't the `charitable_after_save_donation` hook, don't send the email. */
528 if ( ! doing_action( 'charitable_after_save_donation' ) ) {
529 return false;
530 }
531
532 /* If this isn't a manually created donation, send the email. */
533 if ( 'manual' !== $donation->get_gateway() ) {
534 return $send_email;
535 }
536
537 /**
538 * Finally, if we're saving a manually created donation, only
539 * send the email if that option was checked in the admin.
540 */
541 return array_key_exists( 'send_donation_receipt', $_POST ) && $_POST['send_donation_receipt'];
542 }
543
544 /**
545 * Checks whether we are saving the admin donation form.
546 *
547 * @since 1.6.15
548 *
549 * @return boolean
550 */
551 public function is_admin_donation_save() {
552 return array_key_exists( 'charitable_action', $_POST );
553 }
554
555 /**
556 * Checks whether we are doing a donation action.
557 *
558 * @since 1.6.15
559 *
560 * @return boolean
561 */
562 public function is_donation_action() {
563 return array_key_exists( 'charitable_donation_action', $_POST );
564 }
565 }
566
567 endif;
568