| 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) 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_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 |
* 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->donation = $email->get_donation(); |
| 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()->donation_fields()->get_email_tag_fields(); |
| 87 |
$fields = array_map( array( $this, 'parse_donation_field' ), $fields ); |
| 88 |
$fields = array_combine( wp_list_pluck( $fields, 'tag' ), $fields ); |
| 89 |
|
| 90 |
/** |
| 91 |
* Filter the donation email fields. |
| 92 |
* |
| 93 |
* @since 1.5.0 |
| 94 |
* |
| 95 |
* @param array $fields The default set of fields. |
| 96 |
* @param Charitable_Donation|null $donation When an email is being sent, this will be an instance of `Charitable_Donation`. Otherwise it's null. |
| 97 |
* @param Charitable_Email $email Instance of `Charitable_Email`. |
| 98 |
*/ |
| 99 |
return apply_filters( 'charitable_email_donation_fields', $fields, $this->donation, $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 donation object set. |
| 115 |
* |
| 116 |
* @since 1.5.0 |
| 117 |
* |
| 118 |
* @return boolean |
| 119 |
*/ |
| 120 |
public function has_valid_donation() { |
| 121 |
return ! is_null( $this->donation ) && is_a( $this->donation, 'Charitable_Donation' ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Return the campaigns donated to. |
| 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_campaigns( $value, $args ) { |
| 134 |
$linked = array_key_exists( 'with_links', $args ) ? $args['with_links'] : false; |
| 135 |
return $this->donation->get_campaigns_donated_to( $linked ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Return the value for a particular field that is registered as a Charitable_Donation_Field. |
| 140 |
* |
| 141 |
* @since 1.5.0 |
| 142 |
* @version 1.8.2 - added filter. |
| 143 |
* |
| 144 |
* @param string $value The content to display in place of the shortcode. |
| 145 |
* @param array $args Optional set of arguments. |
| 146 |
* @return string |
| 147 |
*/ |
| 148 |
public function get_value_from_donation_field( $value, $args ) { |
| 149 |
if ( ! array_key_exists( $args['show'], $this->fields ) || ! array_key_exists( 'field', $this->fields[ $args['show'] ] ) ) { |
| 150 |
return ''; |
| 151 |
} |
| 152 |
|
| 153 |
$field_id = ! empty( $this->fields ) && ! empty( $this->fields[ $args['show'] ]['field'] ) ? $this->fields[ $args['show'] ]['field'] : ''; |
| 154 |
$return_value = $this->donation->get( $this->fields[ $args['show'] ]['field'] ); |
| 155 |
|
| 156 |
return apply_filters( 'charitable_email_fields_donation_field_value', $return_value, $value, $args, $field_id ); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Parse a donation field, returning just the email tag parameters. |
| 161 |
* |
| 162 |
* @since 1.5.0 |
| 163 |
* |
| 164 |
* @param Charitable_Donation_Field $field A Charitable_Donation_Field instance. |
| 165 |
* @return array |
| 166 |
*/ |
| 167 |
private function parse_donation_field( Charitable_Donation_Field $field ) { |
| 168 |
$tag_settings = $field->email_tag; |
| 169 |
$tag_settings['field'] = $field->field; |
| 170 |
|
| 171 |
if ( method_exists( $this, 'get_' . $field->field ) ) { |
| 172 |
$tag_settings['callback'] = array( $this, 'get_' . $field->field ); |
| 173 |
} else { |
| 174 |
$tag_settings['callback'] = array( $this, 'get_value_from_donation_field' ); |
| 175 |
} |
| 176 |
|
| 177 |
return $tag_settings; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
endif; |
| 182 |
|