PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.14
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.14
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 / admin / forms / views / class-charitable-admin-form-view.php

class-charitable-admin-form-view.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.7.0.14, at includes/admin/forms/views/class-charitable-admin-form-view.php

468 lines 10.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Admin form view.
4 *
5 * This is responsible for rendering the output of forms in the WordPress dashboard.
6 *
7 * @package Charitable/Forms/Charitable_Admin_Form_View
8 * @author David Bisset
9 * @copyright Copyright (c) 2022, WP Charitable LLC
10 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
11 * @since 1.5.0
12 * @version 1.6.45
13 */
14
15 // Exit if accessed directly.
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit;
18 }
19
20 if ( ! class_exists( 'Charitable_Admin_Form_View' ) ) :
21
22 /**
23 * Charitable_Admin_Form_View.
24 *
25 * @since 1.5.0
26 */
27 class Charitable_Admin_Form_View implements Charitable_Form_View_Interface {
28
29 /**
30 * The Form we are responsible for rendering.
31 *
32 * @since 1.5.0
33 *
34 * @var Charitable_Form
35 */
36 protected $form;
37
38 /**
39 * Current WP_Post object.
40 *
41 * @since 1.5.0
42 *
43 * @var WP_Post
44 */
45 protected $post;
46
47 /**
48 * Custom keys for the current post.
49 *
50 * @since 1.5.0
51 *
52 * @var array
53 */
54 protected $post_custom_keys;
55
56 /**
57 * Current tabindex.
58 *
59 * @since 1.5.0
60 *
61 * @var int
62 */
63 protected $tabindex;
64
65 /**
66 * Set up view instance.
67 *
68 * @since 1.5.0
69 *
70 * @param Charitable_Form $form Form object.
71 */
72 public function __construct( Charitable_Form $form ) {
73 $this->form = $form;
74 $this->tabindex = 0;
75 }
76
77 /**
78 * Render a form.
79 *
80 * @since 1.5.0
81 *
82 * @return void
83 */
84 public function render() {
85 $this->render_hidden_fields();
86 $this->render_fields();
87 }
88
89 /**
90 * Render a form's hidden fields.
91 *
92 * @since 1.5.0
93 *
94 * @return boolean True if any fields were rendered. False otherwise.
95 */
96 public function render_hidden_fields() {
97 $fields = $this->form->get_hidden_fields();
98
99 if ( ! is_array( $fields ) || empty( $fields ) ) {
100 return false;
101 }
102
103 array_walk( $fields, array( $this, 'render_hidden_field' ) );
104
105 return true;
106 }
107
108 /**
109 * Render notices before the form.
110 *
111 * @since 1.5.0
112 *
113 * @return void
114 */
115 public function render_notices() {
116 }
117
118 /**
119 * Render all of a form's fields.
120 *
121 * @since 1.5.0
122 *
123 * @param array $fields Optional. A set of fields to display. If not set,
124 * we will look for fields in the form's `get_fields()`
125 * method (if it has one).
126 * @return boolean True if any fields were rendered. False otherwsie.
127 */
128 public function render_fields( $fields = array() ) {
129 if ( empty( $fields ) ) {
130 $fields = $this->form->get_fields();
131
132 /* If still missing fields, return false and throw an error. */
133 if ( empty( $fields ) ) {
134 charitable_get_deprecated()->doing_it_wrong(
135 __METHOD__,
136 __( 'There are no fields to render.', 'charitable' ),
137 '1.5.0'
138 );
139 return false;
140 }
141 }
142
143 array_walk( $fields, array( $this, 'render_field' ) );
144 }
145
146 /**
147 * Render a hidden field.
148 *
149 * @since 1.5.0
150 *
151 * @param string $value Field value.
152 * @param string $key Field key.
153 * @return void
154 */
155 public function render_hidden_field( $value, $key ) {
156 printf( '<input type="hidden" name="%s" value="%s" />', esc_attr( $key ), esc_attr( $value ) );
157 }
158
159 /**
160 * Render a specific form field.
161 *
162 * @since 1.5.0
163 *
164 * @param array $field Field definition.
165 * @param string $key Field key.
166 * @param array $args Unused. Mixed array of arguments.
167 * @return boolean False if the field was not rendered. True otherwise.
168 */
169 public function render_field( $field, $key, $args = array() ) {
170 $field['form_view'] = $this;
171 $field['view'] = $this->get_field_view( $field );
172 $field['type'] = $this->get_field_type( $field );
173 $field['key'] = $this->get_field_key( $field, $key );
174 $field['id'] = $this->get_field_id( $field );
175 $field['wrapper_id'] = 'charitable-' . $field['id'] . '-wrap';
176 $field['wrapper_class'] = $this->get_field_class( $field );
177 $field['tabindex'] = array_key_exists( 'tabindex', $field ) ? (int) $field['tabindex'] : $this->tabindex;
178
179 if ( 'checkbox' == $field['type'] ) {
180 $field['checked'] = $this->is_field_checked( $field );
181 } else {
182 $field['value'] = $this->get_field_value( $field );
183 }
184
185 /* Increment the tabindex. */
186 $this->tabindex = $field['tabindex'] + 1;
187
188 return charitable_admin_view( $field['view'], $field );
189 }
190
191 /**
192 * Checks whether a field has all required arguments.
193 *
194 * @since 1.5.0
195 *
196 * @param array $view_args The view args.
197 * @return boolean
198 */
199 public function field_has_required_args( $view_args ) {
200 $required = array( 'view', 'key', 'id', 'wrapper_id', 'type' );
201 $missing = count( array_diff( $required, array_keys( $view_args ) ) );
202
203 if ( $missing > 0 ) {
204 return false;
205 }
206
207 $type = $view_args['type'];
208
209 if ( 'fieldset' == $type ) {
210 return array_key_exists( 'fields', $view_args ) && is_array( $view_args['fields'] ) && count( $view_args['fields'] );
211 }
212
213 if ( $this->field_needs_options( $type ) ) {
214 return array_key_exists( 'options', $view_args );
215 }
216
217 if ( 'checkbox' == $type && ! array_key_exists( 'checked', $view_args ) ) {
218 return false;
219 }
220
221 return array_key_exists( 'value', $view_args );
222 }
223
224 /**
225 * Return the field view based on a field 'type'.
226 *
227 * @since 1.5.0
228 *
229 * @param array $field Field definition.
230 * @return string
231 */
232 protected function get_field_view( $field ) {
233 if ( array_key_exists( 'view', $field ) ) {
234 return $field['view'];
235 }
236
237 if ( $this->use_default_field_template( $field['type'] ) ) {
238 return 'metaboxes/field-types/default';
239 }
240
241 switch ( $field['type'] ) {
242 case 'charitable-fieldset':
243 return 'metaboxes/field-types/fieldset';
244
245 default:
246 return 'metaboxes/field-types/' . $field['type'];
247 }
248 }
249
250 /**
251 * Return the field type.
252 *
253 * @since 1.5.0
254 *
255 * @param array $field Field definition.
256 * @return string
257 */
258 protected function get_field_type( $field ) {
259 if ( array_key_exists( 'type', $field ) ) {
260 return $field['type'];
261 }
262
263 return str_replace( 'metaboxes/field-types/', '', $field['view'] );
264 }
265
266 /**
267 * Return the field wrapper class.
268 *
269 * @since 1.5.7
270 *
271 * @param array $field Field definition.
272 * @return string
273 */
274 protected function get_field_class( $field ) {
275 $classes = array(
276 'charitable-metabox-wrap',
277 'charitable-' . $this->get_field_type( $field ) . '-wrap',
278 );
279
280 if ( array_key_exists( 'wrapper_class', $field ) ) {
281 $classes = array_merge( $classes, $field['wrapper_class'] );
282 }
283
284 return implode( ' ', $classes );
285
286 }
287
288 /**
289 * Return the key for a particular field.
290 *
291 * @since 1.5.0
292 *
293 * @param array $field Field definition.
294 * @param string $default The key of the form field. This is the fallback option.
295 * @return string|void
296 */
297 protected function get_field_key( $field, $default ) {
298 foreach ( array( 'key', 'meta_key' ) as $key ) {
299 if ( array_key_exists( $key, $field ) ) {
300 return $field[ $key ];
301 }
302 }
303
304 return $default;
305 }
306
307 /**
308 * Return the ID for a particular field.
309 *
310 * @since 1.5.3
311 *
312 * @param array $field Field definition.
313 * @return string
314 */
315 protected function get_field_id( $field ) {
316 if ( array_key_exists( 'id', $field ) ) {
317 return $field['id'];
318 }
319
320 return str_replace( '_', '-', trim( preg_replace( '![^a-z0-9]+!i', '-', $field['key'] ), '-' ) );
321 }
322
323 /**
324 * Return the current value of a particular field.
325 *
326 * @since 1.5.0
327 *
328 * @param array $field Field definition.
329 * @return string
330 */
331 protected function get_field_value( $field ) {
332 if ( array_key_exists( 'value', $field ) ) {
333 return $field['value'];
334 }
335
336 return $this->get_current_value( $field );
337 }
338
339 /**
340 * Check whether the field is checked.
341 *
342 * @since 1.5.0
343 *
344 * @param array $field Field definition.
345 * @return boolean
346 */
347 protected function is_field_checked( $field ) {
348 if ( isset( $field['checked'] ) ) {
349 return $field['checked'];
350 }
351
352 $value = $this->get_current_value( $field );
353
354 if ( isset( $field['value'] ) ) {
355 return $field['value'] == $value;
356 }
357
358 return false != $value;
359 }
360
361 /**
362 * Fetches the current value for a field, ignoring the 'value' setting of the field.
363 *
364 * @since 1.5.0
365 *
366 * @param array $field Field definition.
367 * @return mixed
368 */
369 protected function get_current_value( $field ) {
370 if ( empty( $field['key'] ) ) {
371 return;
372 }
373
374 $default = array_key_exists( 'default', $field ) ? $field['default'] : '';
375
376 if ( ! in_array( $field['key'], $this->get_post_custom_keys() ) ) {
377 return $default;
378 }
379
380 return get_post_meta( $this->get_post()->ID, $field['key'], true );
381 }
382
383 /**
384 * Get the post object.
385 *
386 * @since 1.6.45
387 *
388 * @global WP_Post $post
389 *
390 * @return WP_Post|Null
391 */
392 public function get_post() {
393 if ( ! isset( $this->post ) ) {
394 global $post;
395
396 $this->post = $post;
397 }
398
399 return $this->post;
400 }
401
402 /**
403 * Return the custom keys for the current post.
404 *
405 * @since 1.5.0
406 *
407 * @return array
408 */
409 protected function get_post_custom_keys() {
410 if ( ! is_a( $this->get_post(), 'WP_Post' ) ) {
411 return array();
412 }
413
414 if ( ! isset( $this->post_custom_keys ) ) {
415 $this->post_custom_keys = get_post_custom_keys( $this->get_post()->ID );
416
417 if ( ! is_array( $this->post_custom_keys ) ) {
418 $this->post_custom_keys = array();
419 }
420 }
421
422 return $this->post_custom_keys;
423 }
424
425 /**
426 * Whether the given field type can use the default field template.
427 *
428 * @since 1.5.0
429 *
430 * @param string $field_type Type of field.
431 * @return boolean
432 */
433 protected function use_default_field_template( $field_type ) {
434 /**
435 * Filter the list of field types that use the default template.
436 *
437 * @since 1.0.0
438 *
439 * @param string[] $types Field types.
440 */
441 $default_field_types = apply_filters(
442 'charitable_default_template_field_types',
443 array(
444 'text',
445 'email',
446 'password',
447 'date',
448 )
449 );
450
451 return in_array( $field_type, $default_field_types );
452 }
453
454 /**
455 * Whether a field needs an array of options to be set.
456 *
457 * @since 1.5.0
458 *
459 * @param string $field_type The type of field.
460 * @return boolean
461 */
462 protected function field_needs_options( $field_type ) {
463 return in_array( $field_type, array( 'select', 'multi-checkbox', 'radio' ) );
464 }
465 }
466
467 endif;
468