PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.2
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.2
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.7.0.2, at includes/emails/fields/class-charitable-email-fields-campaign.php

155 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 Campaign class.
4 *
5 * @package Charitable/Classes/Charitable_Email_Fields_Campaign
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.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 * 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->campaign = $email->get_campaign();
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()->campaign_fields()->get_email_tag_fields();
60 $fields = array_map( array( $this, 'parse_campaign_field' ), $fields );
61 $fields = array_combine( wp_list_pluck( $fields, 'tag' ), $fields );
62
63 /**
64 * Filter the campaign email fields.
65 *
66 * @since 1.5.0
67 *
68 * @param array $fields The default set of fields.
69 * @param Charitable_Campaign $campaign Instance of `Charitable_Campaign`.
70 * @param Charitable_Email $email Instance of `Charitable_Email`.
71 */
72 return apply_filters( 'charitable_email_campaign_fields', $fields, $this->campaign, $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 Campaign object set.
88 *
89 * @since 1.5.0
90 *
91 * @return boolean
92 */
93 public function has_valid_campaign() {
94 return ! is_null( $this->campaign ) && is_a( $this->campaign, 'Charitable_Campaign' );
95 }
96
97 /**
98 * Display whether the campaign achieved its goal.
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_goal_achieved_message( $value, $args ) {
107 $args = wp_parse_args( $args, array(
108 'success' => '',
109 'failure' => '',
110 ) );
111
112 return $this->campaign->get_goal_achieved_message( $args['success'], $args['failure'] );
113 }
114
115 /**
116 * Return the value for a particular field that is registered as a Charitable_Campaign_Field.
117 *
118 * @since 1.6.0
119 *
120 * @param string $value The content to display in place of the shortcode.
121 * @param array $args Optional set of arguments.
122 * @return string
123 */
124 public function get_value_from_campaign_field( $value, $args ) {
125 if ( ! array_key_exists( $args['show'], $this->fields ) || ! array_key_exists( 'field', $this->fields[ $args['show'] ] ) ) {
126 return '';
127 }
128
129 return $this->campaign->get( $this->fields[ $args['show'] ]['field'] );
130 }
131
132 /**
133 * Parse a campaign field, returning just the email tag parameters.
134 *
135 * @since 1.6.0
136 *
137 * @param Charitable_Campaign_Field $field A Charitable_Campaign_Field instance.
138 * @return array
139 */
140 private function parse_campaign_field( Charitable_Campaign_Field $field ) {
141 $tag_settings = $field->email_tag;
142 $tag_settings['field'] = $field->field;
143
144 if ( method_exists( $this, 'get_' . $field->field ) ) {
145 $tag_settings['callback'] = array( $this, 'get_' . $field->field );
146 } else {
147 $tag_settings['callback'] = array( $this, 'get_value_from_campaign_field' );
148 }
149
150 return $tag_settings;
151 }
152 }
153
154 endif;
155