PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.13
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.13
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-campaign.php

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

185 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Email Fields Campaign class.
4 *
5 * @package Charitable/Classes/Charitable_Email_Fields_Campaign
6 * @author David Bisset
7 * @copyright Copyright (c) 2023, WP Charitable LLC
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.5.0
10 * @version 1.8.0
11 */
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 if ( ! class_exists( 'Charitable_Email_Fields_Campaign' ) ) :
19
20 /**
21 * Charitable_Email_Fields class.
22 *
23 * @since 1.5.0
24 */
25 class Charitable_Email_Fields_Campaign implements Charitable_Email_Fields_Interface {
26
27 /**
28 * The Charitable_Campaign object.
29 *
30 * @since 1.5.0
31 *
32 * @var Charitable_Campaign
33 */
34 private $campaign;
35
36 /**
37 * Email.
38 *
39 * @since 1.8.0
40 *
41 * @var string
42 */
43 public $email;
44
45 /**
46 * Preview.
47 *
48 * @since 1.8.0
49 *
50 * @var string
51 */
52 public $preview;
53
54 /**
55 * Fields.
56 *
57 * @since 1.8.0
58 *
59 * @var mixed
60 */
61 public $fields;
62
63 /**
64 * Set up class instance.
65 *
66 * @since 1.5.0
67 *
68 * @param Charitable_Email $email The email object.
69 * @param boolean $preview Whether this is an email preview.
70 */
71 public function __construct( Charitable_Email $email, $preview ) {
72 $this->email = $email;
73 $this->preview = $preview;
74 $this->campaign = $email->get_campaign();
75 $this->fields = $this->init_fields();
76 }
77
78 /**
79 * Get the fields that apply to the current email.
80 *
81 * @since 1.5.0
82 *
83 * @return array
84 */
85 public function init_fields() {
86 $fields = charitable()->campaign_fields()->get_email_tag_fields();
87 $fields = array_map( array( $this, 'parse_campaign_field' ), $fields );
88 $fields = array_combine( wp_list_pluck( $fields, 'tag' ), $fields );
89
90 /**
91 * Filter the campaign email fields.
92 *
93 * @since 1.5.0
94 *
95 * @param array $fields The default set of fields.
96 * @param Charitable_Campaign $campaign Instance of `Charitable_Campaign`.
97 * @param Charitable_Email $email Instance of `Charitable_Email`.
98 */
99 return apply_filters( 'charitable_email_campaign_fields', $fields, $this->campaign, $this->email );
100 }
101
102 /**
103 * Return fields.
104 *
105 * @since 1.5.0
106 *
107 * @return array
108 */
109 public function get_fields() {
110 return $this->fields;
111 }
112
113 /**
114 * Checks whether the email has a valid Campaign object set.
115 *
116 * @since 1.5.0
117 *
118 * @return boolean
119 */
120 public function has_valid_campaign() {
121 return ! is_null( $this->campaign ) && is_a( $this->campaign, 'Charitable_Campaign' );
122 }
123
124 /**
125 * Display whether the campaign achieved its goal.
126 *
127 * @since 1.5.0
128 *
129 * @param string $value The content to display in place of the shortcode.
130 * @param array $args Optional set of arguments.
131 * @return string
132 */
133 public function get_goal_achieved_message( $value, $args ) {
134 $args = wp_parse_args(
135 $args,
136 array(
137 'success' => '',
138 'failure' => '',
139 )
140 );
141
142 return $this->campaign->get_goal_achieved_message( $args['success'], $args['failure'] );
143 }
144
145 /**
146 * Return the value for a particular field that is registered as a Charitable_Campaign_Field.
147 *
148 * @since 1.6.0
149 *
150 * @param string $value The content to display in place of the shortcode.
151 * @param array $args Optional set of arguments.
152 * @return string
153 */
154 public function get_value_from_campaign_field( $value, $args ) {
155 if ( ! array_key_exists( $args['show'], $this->fields ) || ! array_key_exists( 'field', $this->fields[ $args['show'] ] ) ) {
156 return '';
157 }
158
159 return $this->campaign->get( $this->fields[ $args['show'] ]['field'] );
160 }
161
162 /**
163 * Parse a campaign field, returning just the email tag parameters.
164 *
165 * @since 1.6.0
166 *
167 * @param Charitable_Campaign_Field $field A Charitable_Campaign_Field instance.
168 * @return array
169 */
170 private function parse_campaign_field( Charitable_Campaign_Field $field ) {
171 $tag_settings = $field->email_tag;
172 $tag_settings['field'] = $field->field;
173
174 if ( method_exists( $this, 'get_' . $field->field ) ) {
175 $tag_settings['callback'] = array( $this, 'get_' . $field->field );
176 } else {
177 $tag_settings['callback'] = array( $this, 'get_value_from_campaign_field' );
178 }
179
180 return $tag_settings;
181 }
182 }
183
184 endif;
185