| 1 |
<?php |
| 2 |
/** |
| 3 |
* Register and retrieve donation fields. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Donation_Field_Registry |
| 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_Donation_Field_Registry' ) ) : |
| 19 |
|
| 20 |
/** |
| 21 |
* Charitable_Donation_Field_Registry |
| 22 |
* |
| 23 |
* @since 1.5.0 |
| 24 |
*/ |
| 25 |
class Charitable_Donation_Field_Registry extends Charitable_Field_Registry implements Charitable_Field_Registry_Interface { |
| 26 |
|
| 27 |
/** |
| 28 |
* Admin form fields. |
| 29 |
* |
| 30 |
* @since 1.5.0 |
| 31 |
* |
| 32 |
* @var array |
| 33 |
*/ |
| 34 |
protected $admin_form_fields; |
| 35 |
|
| 36 |
/** |
| 37 |
* Public donation form fields. |
| 38 |
* |
| 39 |
* @since 1.6.0 |
| 40 |
* |
| 41 |
* @var array |
| 42 |
*/ |
| 43 |
protected $donation_form_fields; |
| 44 |
|
| 45 |
/** |
| 46 |
* Instantiate registry. |
| 47 |
* |
| 48 |
* @since 1.6.0 |
| 49 |
* |
| 50 |
* @return void |
| 51 |
*/ |
| 52 |
public function __construct() { |
| 53 |
parent::__construct(); |
| 54 |
|
| 55 |
$this->sections = $this->get_core_sections(); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Returns the core sections. |
| 60 |
* |
| 61 |
* @since 1.6.0 |
| 62 |
* |
| 63 |
* @return array |
| 64 |
*/ |
| 65 |
private function get_core_sections() { |
| 66 |
/** |
| 67 |
* Filter the default donation sections. |
| 68 |
* |
| 69 |
* @since 1.6.0 |
| 70 |
* |
| 71 |
* @param array $sections The full array of sections for all forms, including defaults. |
| 72 |
*/ |
| 73 |
return apply_filters( |
| 74 |
'charitable_default_donation_sections', |
| 75 |
array( |
| 76 |
'defaults' => array( |
| 77 |
'public' => 'user', |
| 78 |
'admin' => 'user', |
| 79 |
), |
| 80 |
'public' => array( |
| 81 |
'user' => __( 'Your Details', 'charitable' ), |
| 82 |
), |
| 83 |
'admin' => array( |
| 84 |
'user' => '', |
| 85 |
'meta' => '', |
| 86 |
), |
| 87 |
) |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Return the donation form fields. |
| 93 |
* |
| 94 |
* @since 1.5.0 |
| 95 |
* @since 1.6.0 Added $section parameter. |
| 96 |
* |
| 97 |
* @param string $section Optional. If set, will only return the fields within this section. |
| 98 |
* @return array |
| 99 |
*/ |
| 100 |
public function get_donation_form_fields( $section = '' ) { |
| 101 |
if ( ! isset( $this->donation_form_fields ) ) { |
| 102 |
$this->donation_form_fields = array_filter( $this->fields, array( new Charitable_Field_Filter( 'donation_form' ), 'is_not_false' ) ); |
| 103 |
} |
| 104 |
|
| 105 |
if ( empty( $section ) ) { |
| 106 |
return $this->donation_form_fields; |
| 107 |
} |
| 108 |
|
| 109 |
$fields = array(); |
| 110 |
|
| 111 |
foreach ( $this->donation_form_fields as $key => $field ) { |
| 112 |
if ( $section != $field->donation_form['section'] ) { |
| 113 |
continue; |
| 114 |
} |
| 115 |
|
| 116 |
$fields[ $key ] = $field; |
| 117 |
} |
| 118 |
|
| 119 |
return $fields; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Return the donation form fields. |
| 124 |
* |
| 125 |
* @since 1.5.0 |
| 126 |
* |
| 127 |
* @param string $section Optional. If set, this will only return fields within this section. |
| 128 |
* @return array |
| 129 |
*/ |
| 130 |
public function get_admin_form_fields( $section = '' ) { |
| 131 |
if ( ! isset( $this->admin_form_fields ) ) { |
| 132 |
$this->admin_form_fields = array_filter( $this->fields, array( new Charitable_Field_Filter( 'admin_form' ), 'is_not_false' ) ); |
| 133 |
} |
| 134 |
|
| 135 |
if ( empty( $section ) ) { |
| 136 |
return $this->admin_form_fields; |
| 137 |
} |
| 138 |
|
| 139 |
$fields = array(); |
| 140 |
|
| 141 |
foreach ( $this->admin_form_fields as $key => $field ) { |
| 142 |
if ( $section != $field->admin_form['section'] ) { |
| 143 |
continue; |
| 144 |
} |
| 145 |
|
| 146 |
$fields[ $key ] = $field; |
| 147 |
} |
| 148 |
|
| 149 |
return $fields; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Return the donation form fields. |
| 154 |
* |
| 155 |
* @since 1.5.0 |
| 156 |
* |
| 157 |
* @return array |
| 158 |
*/ |
| 159 |
public function get_email_tag_fields() { |
| 160 |
return array_filter( $this->fields, array( new Charitable_Field_Filter( 'email_tag' ), 'is_not_false' ) ); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Return the fields to be included in the export. |
| 165 |
* |
| 166 |
* @since 1.5.0 |
| 167 |
* |
| 168 |
* @return array |
| 169 |
*/ |
| 170 |
public function get_export_fields() { |
| 171 |
return array_filter( $this->fields, array( new Charitable_Field_Filter( 'show_in_export' ), 'is_true' ) ); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Return the fields to be included in the donation meta. |
| 176 |
* |
| 177 |
* @since 1.5.0 |
| 178 |
* |
| 179 |
* @return array |
| 180 |
*/ |
| 181 |
public function get_meta_fields() { |
| 182 |
return array_filter( $this->fields, array( new Charitable_Field_Filter( 'show_in_meta' ), 'is_true' ) ); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Return all fields for a particular data type. |
| 187 |
* |
| 188 |
* @since 1.6.0 |
| 189 |
* |
| 190 |
* @param string $data_type The data type. |
| 191 |
* @return Charitable_Donation_Field[] |
| 192 |
*/ |
| 193 |
public function get_data_type_fields( $data_type ) { |
| 194 |
return array_filter( $this->fields, array( new Charitable_Field_Filter( 'data_type', $data_type ), 'is_equal_to' ) ); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Set the default form section. |
| 199 |
* |
| 200 |
* @since 1.5.0 |
| 201 |
* |
| 202 |
* @param string $section Section to register. |
| 203 |
* @param string $form Which form we're registering the section in. |
| 204 |
* @return void |
| 205 |
*/ |
| 206 |
public function set_default_section( $section, $form = 'public' ) { |
| 207 |
$this->sections['defaults'][ $form ] = $section; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Register a field. |
| 212 |
* |
| 213 |
* @since 1.5.0 |
| 214 |
* |
| 215 |
* @param Charitable_Field_Interface $field Instance of `Charitable_Field_Interface`. |
| 216 |
* @return boolean |
| 217 |
*/ |
| 218 |
public function register_field( Charitable_Field_Interface $field ) { |
| 219 |
if ( ! is_a( $field, 'Charitable_Donation_Field' ) ) { |
| 220 |
return false; |
| 221 |
} |
| 222 |
|
| 223 |
$field->value_callback = $this->get_field_value_callback( $field ); |
| 224 |
$field->donation_form = $this->get_field_donation_form( $field ); |
| 225 |
$field->admin_form = $this->get_field_admin_form( $field ); |
| 226 |
$this->fields[ $field->field ] = $field; |
| 227 |
|
| 228 |
return true; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Return a callback for the field. |
| 233 |
* |
| 234 |
* @since 1.5.0 |
| 235 |
* |
| 236 |
* @param Charitable_Donation_Field $field Instance of `Charitable_Donation_Field`. |
| 237 |
* @return false|string|callable Returns a callable function or false if none is set and |
| 238 |
* we don't have a default one for the data type. |
| 239 |
*/ |
| 240 |
public function get_field_value_callback( $field ) { |
| 241 |
if ( isset( $field->value_callback ) ) { |
| 242 |
return $field->value_callback; |
| 243 |
} |
| 244 |
|
| 245 |
switch ( $field->data_type ) { |
| 246 |
case 'user': |
| 247 |
return 'charitable_get_donor_meta_value'; |
| 248 |
|
| 249 |
case 'meta': |
| 250 |
return 'charitable_get_donation_meta_value'; |
| 251 |
|
| 252 |
default: |
| 253 |
return false; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Return a parsed array of settings for the field, or false if it should not appear |
| 259 |
* in the donation form. |
| 260 |
* |
| 261 |
* @since 1.5.0 |
| 262 |
* |
| 263 |
* @param Charitable_Donation_Field $field Instance of `Charitable_Donation_Field`. |
| 264 |
* @return array|false |
| 265 |
*/ |
| 266 |
protected function get_field_donation_form( Charitable_Donation_Field $field ) { |
| 267 |
$settings = $field->donation_form; |
| 268 |
|
| 269 |
if ( false === $settings ) { |
| 270 |
return $settings; |
| 271 |
} |
| 272 |
|
| 273 |
if ( ! array_key_exists( 'section', $settings ) ) { |
| 274 |
$settings['section'] = $this->get_default_section( 'public' ); |
| 275 |
} |
| 276 |
|
| 277 |
return $this->parse_form_settings( $settings, $field ); |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Return a parsed array of settings for the field, or false if it should not appear |
| 282 |
* in the donation form. |
| 283 |
* |
| 284 |
* @since 1.5.0 |
| 285 |
* |
| 286 |
* @param Charitable_Donation_Field $field Instance of `Charitable_Donation_Field`. |
| 287 |
* @return array |
| 288 |
*/ |
| 289 |
protected function get_field_admin_form( Charitable_Donation_Field $field ) { |
| 290 |
$settings = $field->admin_form; |
| 291 |
|
| 292 |
if ( false === $settings ) { |
| 293 |
return $settings; |
| 294 |
} |
| 295 |
|
| 296 |
/* If the value is true, we use the same args as for the donation_form setting. */ |
| 297 |
if ( true === $settings ) { |
| 298 |
return $field->donation_form; |
| 299 |
} |
| 300 |
|
| 301 |
if ( is_array( $field->donation_form ) ) { |
| 302 |
$settings = array_merge( $field->donation_form, $settings ); |
| 303 |
} |
| 304 |
|
| 305 |
if ( ! array_key_exists( 'section', $settings ) ) { |
| 306 |
$settings['section'] = $this->get_default_section( 'admin' ); |
| 307 |
} |
| 308 |
|
| 309 |
return $this->parse_form_settings( $settings, $field ); |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* Return an array containing the keys of the form properties. |
| 314 |
* |
| 315 |
* @since 1.6.0 |
| 316 |
* |
| 317 |
* @return array |
| 318 |
*/ |
| 319 |
protected function set_forms() { |
| 320 |
return array( |
| 321 |
'donation_form', |
| 322 |
'admin_form', |
| 323 |
); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
endif; |
| 328 |
|