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

558 lines 15.4 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 *
181 * @return array
182 */
183 public function get_view_meta_boxes() {
184 global $post;
185
186 $meta_boxes = array(
187 'donation-overview' => array(
188 'title' => __( 'Donation Overview', 'charitable' ),
189 'context' => 'normal',
190 'priority' => 'high',
191 'view' => 'metaboxes/donation/donation-overview',
192 ),
193 'donation-actions' => array(
194 'title' => __( 'Donation Actions', 'charitable' ),
195 'context' => 'side',
196 'priority' => 'high',
197 'view' => 'metaboxes/actions',
198 'actions' => charitable_get_donation_actions(),
199 ),
200 'donation-details' => array(
201 'title' => __( 'Donation Details', 'charitable' ),
202 'context' => 'side',
203 'priority' => 'high',
204 'view' => 'metaboxes/donation/donation-details',
205 ),
206 'donation-log' => array(
207 'title' => __( 'Donation Log', 'charitable' ),
208 'context' => 'normal',
209 'priority' => 'low',
210 'view' => 'metaboxes/donation/donation-log',
211 ),
212 );
213
214 /* Get rid of the donation actions meta box if it doesn't apply to this donation. */
215 if ( ! charitable_get_donation_actions()->has_available_actions( $post->ID ) ) {
216 unset( $meta_boxes['donation-actions'] );
217 }
218
219 /**
220 * Filter the meta boxes to be displayed on a donation overview page.
221 *
222 * @since 1.5.0
223 *
224 * @param array $meta_boxes The array of meta boxes and their details.
225 */
226 return apply_filters( 'charitable_donation_view_meta_boxes', $meta_boxes );
227 }
228
229 /**
230 * Create donation actions instance, with some initial defaults.
231 *
232 * @since 1.6.0
233 *
234 * @return void
235 */
236 public function register_donation_actions() {
237 $donation_actions = charitable_get_donation_actions();
238
239 foreach ( charitable_get_valid_donation_statuses() as $status => $label ) {
240 $donation_actions->register(
241 'change_status_to_' . $status,
242 array(
243 'label' => $label,
244 'callback' => array( $this, 'change_donation_status' ),
245 'button_text' => __( 'Update Status', 'charitable' ),
246 'active_callback' => array( $this, 'can_change_donation_status' ),
247 'success_message' => 13,
248 'failed_message' => 14,
249 'fields' => array( $this, 'get_donation_status_change_fields' ),
250 ),
251 __( 'Change Status', 'charitable' )
252 );
253 }
254 }
255
256 /**
257 * Check whether a particular status change works for the given donation.
258 *
259 * @since 1.6.0
260 *
261 * @param int $object_id The donation's ID.
262 * @param array $args Mixed action arguments.
263 * @return boolean
264 */
265 public function can_change_donation_status( $object_id, $args = array() ) {
266 $donation = charitable_get_donation( $object_id );
267
268 return $donation && $args['action_args']['label'] !== $donation->get_status_label();
269 }
270
271 /**
272 * Change a donation's status.
273 *
274 * @since 1.6.0
275 *
276 * @param boolean $success Whether the action has been run.
277 * @param int $object_id The donation's ID.
278 * @param array $args Mixed action arguments.
279 * @param string $action The action id.
280 * @return boolean Whether the status was changed.
281 */
282 public function change_donation_status( $success, $object_id, $args, $action ) {
283 $donation = charitable_get_donation( $object_id );
284
285 if ( ! $donation ) {
286 return false;
287 }
288
289 $status = str_replace( 'change_status_to_', '', $action );
290 $success = $donation->update_status( $status );
291
292 if ( array_key_exists( 'gateway_refund', $_POST ) && $_POST['gateway_refund'] ) {
293 $gateway = $donation->get_gateway();
294
295 /**
296 * Perform a refund in the donation's gateway.
297 *
298 * @since 1.6.0
299 *
300 * @param int $donation_id The donation's ID.
301 */
302 do_action( 'charitable_process_refund_' . $gateway, $object_id );
303 }
304
305 return 0 !== $success;
306 }
307
308 /**
309 * Additional fields to display for a status change.
310 *
311 * @since 1.6.0
312 *
313 * @param int $object_id The donation's ID.
314 * @param array $action Mixed action arguments.
315 * @return void
316 */
317 public function get_donation_status_change_fields( $object_id, $action ) {
318 $statuses = charitable_get_valid_donation_statuses();
319
320 if ( $statuses['charitable-refunded'] != $action['label'] ) {
321 return;
322 }
323
324 $donation = charitable_get_donation( $object_id );
325
326 if ( ! $donation || ! $donation->is_refundable_in_gateway() ) {
327 return;
328 }
329
330 /* translators: %s: gateway name */
331 printf( '%s' . __( 'Refund in %s automatically.', 'charitable' ),
332 '<input type="checkbox" name="gateway_refund" value="1" />',
333 $donation->get_gateway_object()->get_name()
334 );
335 }
336
337 /**
338 * Save meta for the donation.
339 *
340 * @since 1.5.0
341 *
342 * @param int $donation_id
343 * @param WP_Post $post
344 * @return void
345 */
346 public function save_donation( $donation_id, WP_Post $post ) {
347 if ( ! $this->meta_box_helper->user_can_save( $donation_id ) ) {
348 return;
349 }
350
351 $this->maybe_save_form_submission( $donation_id );
352
353 /* Handle any fired actions */
354 if ( ! empty( $_POST['charitable_donation_action'] ) ) {
355 charitable_get_donation_actions()->do_action( sanitize_text_field( $_POST['charitable_donation_action'] ), $donation_id );
356 }
357
358 /**
359 * Hook for plugins to do something else with the posted data.
360 *
361 * @since 1.0.0
362 *
363 * @param int $donation_id The donation ID.
364 * @param WP_Post $post Instance of `WP_Post`.
365 */
366 do_action( 'charitable_donation_save', $donation_id, $post );
367 }
368
369 /**
370 * Save a donation after the admin donation form has been submitted.
371 *
372 * @since 1.5.0
373 *
374 * @param int $donation_id The donation ID.
375 * @return boolean True if this was a form submission. False otherwise.
376 */
377 public function maybe_save_form_submission( $donation_id ) {
378 if ( ! $this->is_admin_donation_save() || did_action( 'charitable_before_save_donation' ) ) {
379 return false;
380 }
381
382 $form = new Charitable_Admin_Donation_Form( charitable_get_donation( $donation_id ) );
383
384 if ( ! $form->validate_submission() ) {
385 wp_safe_redirect( admin_url( 'post-new.php?post_type=donation&show_form=1' ) );
386 exit();
387 }
388
389 charitable_create_donation( $form->get_donation_values() );
390
391 update_post_meta( $donation_id, '_donation_manually_edited', true );
392
393 return true;
394 }
395
396 /**
397 * Change messages when a post type is updated.
398 *
399 * @since 1.5.0
400 *
401 * @param array $messages The post messages.
402 * @return array
403 */
404 public function post_messages( $messages ) {
405 global $post, $post_ID;
406
407 $messages[ Charitable::DONATION_POST_TYPE ] = array(
408 0 => '', // Unused. Messages start at index 1.
409 1 => sprintf(
410 /* translators: %s: link */
411 __( 'Donation updated. <a href="%s">View Donation</a>', 'charitable' ),
412 esc_url( get_permalink( $post_ID ) )
413 ),
414 2 => __( 'Custom field updated.', 'charitable' ),
415 3 => __( 'Custom field deleted.', 'charitable' ),
416 4 => __( 'Donation updated.', 'charitable' ),
417 5 => isset( $_GET['revision'] )
418 ? sprintf(
419 /* translators: %s: revision title */
420 __( 'Donation restored to revision from %s', 'charitable' ),
421 wp_post_revision_title( (int) $_GET['revision'], false )
422 )
423 : false,
424 6 => sprintf(
425 /* translators: %s: link */
426 __( 'Donation published. <a href="%s">View Donation</a>', 'charitable' ),
427 esc_url( get_permalink( $post_ID ) )
428 ),
429 7 => __( 'Donation saved.', 'charitable' ),
430 8 => sprintf(
431 /* translators: %s: link */
432 __( 'Donation submitted. <a target="_blank" href="%s">Preview Donation</a>', 'charitable' ),
433 esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) )
434 ),
435 9 => sprintf(
436 /* translators: %1$s: date and time; %2$s: link */
437 __( 'Donation scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview Donation</a>', 'charitable' ),
438 date_i18n( __( 'M j, Y @ G:i', 'charitable' ), strtotime( $post->post_date ) ),
439 esc_url( get_permalink( $post_ID ) )
440 ),
441 10 => sprintf(
442 /* translators: %s: link */
443 __( 'Donation draft updated. <a target="_blank" href="%s">Preview Donation</a>', 'charitable' ),
444 esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) )
445 ),
446 11 => __( 'Email resent.', 'charitable' ),
447 12 => __( 'Email could not be resent.', 'charitable' ),
448 13 => __( 'Donation status changed.', 'charitable' ),
449 14 => __( 'Donation status could not be changed.', 'charitable' ),
450 );
451
452 return $messages;
453 }
454
455 /**
456 * Prevent email from sending if we are saving the donation via the admin.
457 *
458 * @since 1.6.15
459 *
460 * @param boolean $send_email Whether to send the email.
461 * @param Charitable_Donation $donation The donation object.
462 * @return boolean
463 */
464 public function maybe_block_new_donation_email( $send_email, Charitable_Donation $donation ) {
465 if ( ! $send_email ) {
466 return $send_email;
467 }
468
469 /* Don't block sending it from donation actions. */
470 if ( $this->is_donation_action() ) {
471 return $send_email;
472 }
473
474 /* If we're not saving the donation, send the email. */
475 if ( ! $this->is_admin_donation_save() ) {
476 return $send_email;
477 }
478
479 /* If this isn't the `charitable_after_save_donation` hook, don't send the email. */
480 if ( ! doing_action( 'charitable_after_save_donation' ) ) {
481 return false;
482 }
483
484 /* If this isn't a manually created donation, send the email. */
485 if ( 'manual' !== $donation->get_gateway() ) {
486 return $send_email;
487 }
488
489 /* Do not send admin notifications for manually created donations. */
490 return false;
491 }
492
493 /**
494 * Prevent email from sending if we are saving the donation via the admin.
495 *
496 * @since 1.6.15
497 *
498 * @param boolean $send_email Whether to send the email.
499 * @param Charitable_Donation $donation The donation object.
500 * @return boolean
501 */
502 public function maybe_block_donation_receipt_email( $send_email, Charitable_Donation $donation ) {
503 if ( ! $send_email ) {
504 return $send_email;
505 }
506
507 /* Don't block sending it from donation actions. */
508 if ( $this->is_donation_action() ) {
509 return $send_email;
510 }
511
512 /* If we're not saving the donation, send the email. */
513 if ( ! $this->is_admin_donation_save() ) {
514 return $send_email;
515 }
516
517 /* If this isn't the `charitable_after_save_donation` hook, don't send the email. */
518 if ( ! doing_action( 'charitable_after_save_donation' ) ) {
519 return false;
520 }
521
522 /* If this isn't a manually created donation, send the email. */
523 if ( 'manual' !== $donation->get_gateway() ) {
524 return $send_email;
525 }
526
527 /**
528 * Finally, if we're saving a manually created donation, only
529 * send the email if that option was checked in the admin.
530 */
531 return array_key_exists( 'send_donation_receipt', $_POST ) && $_POST['send_donation_receipt'];
532 }
533
534 /**
535 * Checks whether we are saving the admin donation form.
536 *
537 * @since 1.6.15
538 *
539 * @return boolean
540 */
541 public function is_admin_donation_save() {
542 return array_key_exists( 'charitable_action', $_POST );
543 }
544
545 /**
546 * Checks whether we are doing a donation action.
547 *
548 * @since 1.6.15
549 *
550 * @return boolean
551 */
552 public function is_donation_action() {
553 return array_key_exists( 'charitable_donation_action', $_POST );
554 }
555 }
556
557 endif;
558