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.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 1.7.0.9 All 211 releases
charitable / includes / fields / class-charitable-campaign-field-registry.php

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

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