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 / emails / fields / class-charitable-email-fields-donation.php

class-charitable-email-fields-donation.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.7.0.9, at includes/emails/fields/class-charitable-email-fields-donation.php

151 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Email Fields Donation class.
4 *
5 * @package Charitable/Classes/Charitable_Email_Fields_Donation
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.5.0
11 */
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 if ( ! class_exists( 'Charitable_Email_Fields_Donation' ) ) :
19
20 /**
21 * Charitable_Email_Fields class.
22 *
23 * @since 1.5.0
24 */
25 class Charitable_Email_Fields_Donation implements Charitable_Email_Fields_Interface {
26
27 /**
28 * The Charitable_Donation object.
29 *
30 * @since 1.5.0
31 *
32 * @var Charitable_Donation
33 */
34 private $donation;
35
36 /**
37 * Set up class instance.
38 *
39 * @since 1.5.0
40 *
41 * @param Charitable_Email $email The email object.
42 * @param boolean $preview Whether this is an email preview.
43 */
44 public function __construct( Charitable_Email $email, $preview ) {
45 $this->email = $email;
46 $this->preview = $preview;
47 $this->donation = $email->get_donation();
48 $this->fields = $this->init_fields();
49 }
50
51 /**
52 * Get the fields that apply to the current email.
53 *
54 * @since 1.5.0
55 *
56 * @return array
57 */
58 public function init_fields() {
59 $fields = charitable()->donation_fields()->get_email_tag_fields();
60 $fields = array_map( array( $this, 'parse_donation_field' ), $fields );
61 $fields = array_combine( wp_list_pluck( $fields, 'tag' ), $fields );
62
63 /**
64 * Filter the donation email fields.
65 *
66 * @since 1.5.0
67 *
68 * @param array $fields The default set of fields.
69 * @param Charitable_Donation|null $donation When an email is being sent, this will be an instance of `Charitable_Donation`. Otherwise it's null.
70 * @param Charitable_Email $email Instance of `Charitable_Email`.
71 */
72 return apply_filters( 'charitable_email_donation_fields', $fields, $this->donation, $this->email );
73 }
74
75 /**
76 * Return fields.
77 *
78 * @since 1.5.0
79 *
80 * @return array
81 */
82 public function get_fields() {
83 return $this->fields;
84 }
85
86 /**
87 * Checks whether the email has a valid donation object set.
88 *
89 * @since 1.5.0
90 *
91 * @return boolean
92 */
93 public function has_valid_donation() {
94 return ! is_null( $this->donation ) && is_a( $this->donation, 'Charitable_Donation' );
95 }
96
97 /**
98 * Return the campaigns donated to.
99 *
100 * @since 1.5.0
101 *
102 * @param string $value The content to display in place of the shortcode.
103 * @param array $args Optional set of arguments.
104 * @return string
105 */
106 public function get_campaigns( $value, $args ) {
107 $linked = array_key_exists( 'with_links', $args ) ? $args['with_links'] : false;
108 return $this->donation->get_campaigns_donated_to( $linked );
109 }
110
111 /**
112 * Return the value for a particular field that is registered as a Charitable_Donation_Field.
113 *
114 * @since 1.5.0
115 *
116 * @param string $value The content to display in place of the shortcode.
117 * @param array $args Optional set of arguments.
118 * @return string
119 */
120 public function get_value_from_donation_field( $value, $args ) {
121 if ( ! array_key_exists( $args['show'], $this->fields ) || ! array_key_exists( 'field', $this->fields[ $args['show'] ] ) ) {
122 return '';
123 }
124
125 return $this->donation->get( $this->fields[ $args['show'] ]['field'] );
126 }
127
128 /**
129 * Parse a donation field, returning just the email tag parameters.
130 *
131 * @since 1.5.0
132 *
133 * @param Charitable_Donation_Field $field A Charitable_Donation_Field instance.
134 * @return array
135 */
136 private function parse_donation_field( Charitable_Donation_Field $field ) {
137 $tag_settings = $field->email_tag;
138 $tag_settings['field'] = $field->field;
139
140 if ( method_exists( $this, 'get_' . $field->field ) ) {
141 $tag_settings['callback'] = array( $this, 'get_' . $field->field );
142 } else {
143 $tag_settings['callback'] = array( $this, 'get_value_from_donation_field' );
144 }
145
146 return $tag_settings;
147 }
148 }
149
150 endif;
151