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