PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 2.0.0
Subscriptions for WooCommerce with Stripe Recurring Payments v2.0.0
2.0.0 1.11.2 1.11.1 1.11.0 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.6 1.9.5 trunk 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 All 61 releases
subscription / includes / Admin / SettingsHelper.php

SettingsHelper.php in Subscriptions for WooCommerce with Stripe Recurring Payments 2.0.0, at includes/Admin/SettingsHelper.php

896 lines 31.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Settings Helper File
4 *
5 * @package SpringDevs\Subscription\Admin
6 */
7
8 namespace SpringDevs\Subscription\Admin;
9
10 /**
11 * Settings Helper Class
12 *
13 * @package SpringDevs\Subscription\Admin
14 */
15 class SettingsHelper {
16 /**
17 * Singleton instance
18 *
19 * @var SettingsHelper|null
20 */
21 private static $instance = null;
22
23 /**
24 * Get singleton instance
25 *
26 * @return SettingsHelper
27 */
28 public static function get_instance() {
29 if ( null === self::$instance ) {
30 self::$instance = new self();
31 }
32 return self::$instance;
33 }
34
35 /**
36 * Initialize the class.
37 */
38 private function __construct() {
39 add_filter( 'process_subscrpt_settings_fields', [ $this, 'process_settings_fields' ], 100, 1 );
40 }
41
42 /**
43 * Process settings fields.
44 *
45 * @param array $fields Settings fields.
46 * @return array Processed settings fields.
47 */
48 public function process_settings_fields( $fields ) {
49 // Group settings fields.
50 $fields = $this->group_settings_fields( $fields );
51
52 // Sort fields by priority (groups & fields).
53 $fields = $this->sort_settings_fields( $fields );
54
55 return $fields;
56 }
57
58 /**
59 * Group settings fields.
60 *
61 * @param array $fields Settings fields.
62 * @return array Processed settings fields.
63 */
64 public function group_settings_fields( $fields ) {
65 $tmp_fields = [];
66 foreach ( $fields as $field ) {
67 $field_group = $field['group'] ?? 'main';
68
69 if ( $field['type'] === 'heading' ) {
70 $group_priority = $field['priority'] ?? 0;
71 $tmp_fields[ $field_group ]['priority'] = $group_priority;
72 $field['priority'] = -1;
73 }
74
75 $tmp_fields[ $field_group ]['fields'][] = $field;
76 }
77 return $tmp_fields;
78 }
79
80 /**
81 * Sort settings fields.
82 *
83 * @param array $fields Settings fields.
84 * @return array Processed settings fields.
85 */
86 public function sort_settings_fields( $fields ) {
87 // Sort groups by priority.
88 uasort(
89 $fields,
90 function ( $a, $b ) {
91 $priority_a = $a['priority'] ?? 0;
92 $priority_b = $b['priority'] ?? 0;
93 return $priority_a <=> $priority_b;
94 }
95 );
96
97 // Sort fields within each group by priority.
98 foreach ( $fields as $group_key => $group_data ) {
99 uasort(
100 $group_data['fields'],
101 function ( $a, $b ) {
102 $priority_a = $a['priority'] ?? 0;
103 $priority_b = $b['priority'] ?? 0;
104 return $priority_a <=> $priority_b;
105 }
106 );
107 $fields[ $group_key ]['fields'] = $group_data['fields'];
108 }
109 return $fields;
110 }
111
112 /**
113 * Label a settings group for its tab.
114 *
115 * The label is the group's `heading` field, which is also what the panel
116 * shows, so a tab and its panel can never disagree. An add-on that adds a
117 * group without a heading still gets a usable tab rather than a blank one:
118 * `live_qr_settings` reads as "Live Qr Settings", which is wrong-ish but
119 * findable, and the fix is for that add-on to add a heading.
120 *
121 * `main` is the exception. It is what a field with no `group` falls back to,
122 * so it holds whatever nobody placed rather than anything named "Main". No
123 * field ships in it — this plugin has no settings that are merely general —
124 * and it only becomes a tab when something lands there uninvited.
125 *
126 * @param string $group_id Group key.
127 * @param array $group Group data: `fields`, `priority`.
128 * @return string Unescaped label.
129 */
130 public static function group_label( $group_id, array $group ) {
131 foreach ( $group['fields'] ?? array() as $field ) {
132 if ( 'heading' === ( $field['type'] ?? '' ) && ! empty( $field['field_data']['title'] ) ) {
133 return $field['field_data']['title'];
134 }
135 }
136
137 if ( 'main' === $group_id ) {
138 return __( 'General', 'subscription' );
139 }
140
141 return ucwords( str_replace( array( '_', '-' ), ' ', (string) $group_id ) );
142 }
143
144 /**
145 * Whether every field in a group is locked behind Pro.
146 *
147 * Drives the "Pro" marker on the tab, so the whole panel does not have to be
148 * opened to find out that none of it can be changed yet.
149 *
150 * @param array $group Group data.
151 * @return bool
152 */
153 public static function group_is_pro_locked( array $group ) {
154 $has_field = false;
155
156 foreach ( $group['fields'] ?? array() as $field ) {
157 if ( 'heading' === ( $field['type'] ?? '' ) ) {
158 continue;
159 }
160 $has_field = true;
161 if ( empty( $field['field_data']['pro_locked'] ) ) {
162 return false;
163 }
164 }
165
166 return $has_field;
167 }
168
169 /**
170 * The settings sections, in display order.
171 *
172 * One level, named for the job a merchant came to do rather than for the
173 * plugin's internals. Each section is one rail item and one panel; the
174 * groups inside it stack, so nothing is ever two clicks deep.
175 *
176 * There is deliberately no "All settings" entry. It duplicated every panel
177 * on one page, which made the rail beside it look like decoration and gave
178 * every setting two addresses.
179 *
180 * @return array<string,string> Section key => label.
181 */
182 public static function categories() {
183 return array(
184 'renewals' => __( 'Renewals', 'subscription' ),
185 'payments' => __( 'Payments', 'subscription' ),
186 'switching' => __( 'Switching & Upgrades', 'subscription' ),
187 'customers' => __( 'Customers', 'subscription' ),
188 'advanced' => __( 'Advanced', 'subscription' ),
189 );
190 }
191
192 /**
193 * Which section a settings group belongs to.
194 *
195 * Groups are merged rather than mapped one-to-one: a section holding a
196 * single option is a wasted click, so `health_queue` sits with the other
197 * plumbing in Advanced, and everything the customer meets — the role they
198 * are given, checking out as a guest, what their subscription's quick view
199 * shows — is in Customers.
200 *
201 * Unmapped groups, including any an add-on registers without knowing
202 * sections exist, fall into `advanced`, so a new group is always reachable.
203 *
204 * @param string $group_id Group key.
205 * @return string Section key.
206 */
207 public static function group_category( $group_id ) {
208 $map = array(
209 'renewals' => 'renewals',
210 'payment_gateways' => 'payments',
211 'payment_failure' => 'payments',
212 'grace_period' => 'payments',
213 'switching' => 'switching',
214 'role_based_settings' => 'customers',
215 'guest_checkout' => 'customers',
216 'live_qr_settings' => 'customers',
217 'api_settings' => 'advanced',
218 'health_queue' => 'advanced',
219 );
220
221 return $map[ $group_id ] ?? 'advanced';
222 }
223
224 /**
225 * Group keys bucketed by section, each list in the order the groups already
226 * sort in.
227 *
228 * Empty sections are dropped. Most of them are filled by Pro, and free
229 * alone would otherwise show rail items that open onto nothing.
230 *
231 * @param array $settings_fields Grouped, sorted settings fields.
232 * @return array<string,string[]> Section key => ordered group keys.
233 */
234 public static function category_groups( array $settings_fields ) {
235 $out = array();
236 foreach ( array_keys( self::categories() ) as $cat ) {
237 $out[ $cat ] = array();
238 }
239
240 foreach ( array_keys( $settings_fields ) as $group_id ) {
241 $cat = self::group_category( $group_id );
242 $out[ $cat ][] = $group_id;
243 }
244
245 return array_filter(
246 $out,
247 function ( $group_ids ) {
248 return ! empty( $group_ids );
249 }
250 );
251 }
252
253 /**
254 * Whether every group in a section is locked behind Pro.
255 *
256 * Drives the "Pro" marker on the rail item, so a section none of which can
257 * be changed yet says so before it is opened.
258 *
259 * @param string[] $group_ids Group keys in the section.
260 * @param array $settings_fields Grouped settings fields.
261 * @return bool
262 */
263 public static function category_is_pro_locked( array $group_ids, array $settings_fields ) {
264 if ( empty( $group_ids ) ) {
265 return false;
266 }
267
268 foreach ( $group_ids as $group_id ) {
269 if ( ! self::group_is_pro_locked( $settings_fields[ $group_id ] ?? array() ) ) {
270 return false;
271 }
272 }
273
274 return true;
275 }
276
277 /**
278 * Render specified settings field.
279 *
280 * @param string $field Field type.
281 * @param array $args Field arguments.
282 * @param bool $should_print Whether to print the field or return as HTML string.
283 */
284 public static function render_settings_field( $field, $args, $should_print = true ) {
285 if ( empty( $field ) ) {
286 $field = 'input'; // Default field type.
287 subscrpt_write_debug_log( "[SettingsHelper] Field type not specified. Defaulting to 'input'." );
288 }
289
290 switch ( $field ) {
291 case 'heading':
292 return self::render_heading( $args, $should_print );
293 case 'switch':
294 case 'toggle':
295 return self::render_switch_field( $args, $should_print );
296 case 'select':
297 return self::render_select_field( $args, $should_print );
298 case 'multi_select':
299 return self::render_multiselect_field( $args, $should_print );
300 case 'join':
301 return self::render_joined_field( $args, $should_print );
302 case 'editlist':
303 return self::render_editlist_field( $args, $should_print );
304 case 'input':
305 default:
306 return self::render_input_field( $args, $should_print );
307 }
308 }
309
310 /**
311 * Pro badge markup, shown beside settings that require WPSubscription Pro.
312 *
313 * @return string Pre-escaped badge HTML.
314 */
315 public static function pro_badge_html() {
316 return '<span class="subscrpt-pro-badge" title="' . esc_attr__( 'WPSubscription Pro required', 'subscription' ) . '">' . esc_html__( 'Pro', 'subscription' ) . '</span>';
317 }
318
319
320 /**
321 * Text Element HTML.
322 *
323 * @param array $args Same as 'render_text_field'.
324 * @param bool $join_item Whether to return element for 'join' container or not.
325 */
326 public static function inp_element( $args = [], $join_item = false ) {
327 $id = $args['id'];
328 $value = $args['value'] ?? '';
329 $placeholder = $args['placeholder'] ?? '';
330 $type = $args['type'] ?? 'text';
331
332 $disabled_attr = isset( $args['disabled'] ) && $args['disabled'] ? 'disabled' : '';
333
334 $style_attr = '';
335 if ( isset( $args['style'] ) ) {
336 $style_attr = $args['style'];
337 }
338
339 $other_attrs_html = '';
340 foreach ( ( $args['attributes'] ?? [] ) as $attr_key => $attr_value ) {
341 $other_attrs_html .= sprintf( ' %s="%s" ', esc_attr( $attr_key ), esc_attr( $attr_value ) );
342 }
343
344 ob_start();
345 ?>
346 <input
347 id="<?php echo esc_attr( $id ); ?>"
348 name="<?php echo esc_attr( $id ); ?>"
349 class="wpsubs-input"
350 <?php
351 if ( $style_attr ) :
352 ?>
353 style="<?php echo esc_attr( $style_attr ); ?>"<?php endif; ?>
354 type="<?php echo esc_attr( $type ); ?>"
355 placeholder="<?php echo esc_attr( $placeholder ); ?>"
356 value="<?php echo esc_attr( $value ); ?>"
357 <?php echo esc_attr( $disabled_attr ); ?>
358 <?php echo wp_kses_post( $other_attrs_html ); ?>
359 />
360 <?php
361 return ob_get_clean();
362 }
363
364 /**
365 * Select Element HTML.
366 *
367 * @param array $args Same as 'render_select_field'.
368 * @param bool $join_item Whether to return element for 'join' container or not.
369 */
370 public static function select_element( $args = [], $join_item = false ) {
371 $id = $args['id'];
372
373 // Enhanced / multiselect → wpsubs-tag-select (pill input with filter).
374 if ( isset( $args['enhanced'] ) && $args['enhanced'] ) {
375 $multiple = isset( $args['attributes']['multiple'] ) && $args['attributes']['multiple'];
376 $adv_options = array();
377 foreach ( ( $args['options'] ?? [] ) as $opt_value => $opt_label ) {
378 $adv_options[] = array(
379 'value' => (string) $opt_value,
380 'label' => $opt_label,
381 );
382 }
383
384 ob_start();
385 wpsubs_render_tag_select(
386 array(
387 'name' => $id,
388 'value' => $args['selected'] ?? ( $multiple ? array() : '' ),
389 'options' => $adv_options,
390 'multiple' => $multiple,
391 )
392 );
393 return ob_get_clean();
394 }
395
396 // Regular select → wpsubs-adv-select (button-based custom dropdown).
397 $selected = (string) ( $args['selected'] ?? '' );
398 $adv_options = array();
399 foreach ( ( $args['options'] ?? [] ) as $opt_value => $opt_label ) {
400 $adv_options[] = array(
401 'value' => (string) $opt_value,
402 'label' => $opt_label,
403 'disabled' => isset( $args['disabled'] ) && ( is_array( $args['disabled'] )
404 ? in_array( $opt_value, $args['disabled'], true )
405 : $args['disabled'] === $opt_value ),
406 );
407 }
408
409 ob_start();
410 wpsubs_render_adv_select(
411 array(
412 'name' => $id,
413 'value' => $selected,
414 'options' => $adv_options,
415 'align' => 'left',
416 'class' => $args['class'] ?? '',
417 )
418 );
419 return ob_get_clean();
420 }
421
422 /**
423 * Render Field Heading.
424 *
425 * - Args:
426 * - title (string) - Field title.
427 * - description (string) - Field description (optional).
428 *
429 * @param array $args Field arguments.
430 * @param bool $should_print Whether to print the field or return as HTML string.
431 */
432 public static function render_heading( $args = [], $should_print = true ) {
433 $title = $args['title'] ?? '';
434 $description = $args['description'] ?? '';
435
436 ob_start();
437 ?>
438 <div class="wpsubs-settings-heading">
439 <h3 class="wpsubs-settings-heading__title"><?php echo esc_html( $title ); ?></h3>
440 <?php if ( ! empty( $description ) ) : ?>
441 <p class="wpsubs-settings-heading__desc"><?php echo wp_kses_post( $description ); ?></p>
442 <?php endif; ?>
443 </div>
444 <?php
445 $html_content = ob_get_clean();
446
447 // Output not escaped intentionally. Breaks the HTML structure when escaped.
448 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
449 return $should_print ? print( $html_content ) : $html_content;
450 }
451
452 /**
453 * Render Text field.
454 *
455 * - Args:
456 * - id (string) - Field ID.
457 * - title (string) - Field title.
458 * - description (string) - Field description (optional).
459 * - value (string) - Default value.
460 * - placeholder (string) - Default placeholder.
461 * - disabled (bool) - Disabled status.
462 * - type (string) - Input type [text, email, number, date, time, etc.].
463 *
464 * @param array $args Field arguments.
465 * @param bool $should_print Whether to print the field or return as HTML string.
466 */
467 public static function render_input_field( $args = [], $should_print = true ) {
468 $title = $args['title'] ?? '';
469 $description = $args['description'] ?? '';
470
471 // Return error if ID is not provided.
472 if ( empty( $args['id'] ?? '' ) ) {
473 $field_hint = empty( $title ) ? 'Error' : $title;
474 $no_id_msg = '<p><strong>' . $field_hint . ':</strong> ' . __( 'Field ID is required.', 'subscription' ) . '</p>';
475 return $should_print ? print wp_kses_post( $no_id_msg ) : $no_id_msg;
476 }
477
478 // Input HTML.
479 $text_el_html = self::inp_element( $args );
480
481 ob_start();
482 ?>
483 <div class="wpsubs-settings-field<?php echo ! empty( $args['pro_locked'] ) ? ' wpsubs-settings-field--locked' : ''; ?>">
484 <div class="wpsubs-settings-field__label">
485 <?php
486 if ( ! empty( $args['pro_locked'] ) ) {
487 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Pre-escaped badge markup.
488 echo self::pro_badge_html();
489 }
490 ?>
491 <?php echo esc_html( $title ); ?>
492 </div>
493 <div class="wpsubs-settings-field__control">
494 <?php
495 // Output intentionally not escaped as element is already escaped during generation & re-escaping breaks the HTML structure.
496 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
497 echo $text_el_html;
498 ?>
499 <?php if ( ! empty( $description ) ) : ?>
500 <p class="wpsubs-settings-field__hint"><?php echo wp_kses_post( $description ); ?></p>
501 <?php endif; ?>
502 </div>
503 </div>
504 <?php
505 $html_content = ob_get_clean();
506
507 // Output not escaped intentionally. Breaks the HTML structure when escaped.
508 // All form elements inside $html_content are pre-escaped during generation (esc_attr, esc_html).
509 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
510 return $should_print ? print( $html_content ) : $html_content;
511 }
512
513 /**
514 * Render Switch field.
515 *
516 * - Args:
517 * - id (string) - Field ID.
518 * - title (string) - Field title.
519 * - label (string) - Checkbox label.
520 * - description (string) - Field description (optional).
521 * - value (string) - Checked value.
522 * - checked (bool) - Checked status.
523 * - disabled (bool) - Disabled status.
524 *
525 * @param array $args Field arguments.
526 * @param bool $should_print Whether to print the field or return as HTML string.
527 */
528 public static function render_switch_field( $args = [], $should_print = true ) {
529 $id = $args['id'];
530 $title = $args['title'] ?? '';
531 $label = $args['label'] ?? '';
532 $description = $args['description'] ?? '';
533 $value = $args['value'] ?? '0';
534
535 // Return error if ID is not provided.
536 if ( empty( $args['id'] ?? '' ) ) {
537 $field_hint = empty( $title ) ? 'Error' : $title;
538 $no_id_msg = '<p><strong>' . $field_hint . ':</strong> ' . __( 'Field ID is required.', 'subscription' ) . '</p>';
539 return $should_print ? print wp_kses_post( $no_id_msg ) : $no_id_msg;
540 }
541
542 $description_html = '';
543 if ( ! empty( $description ) ) {
544 $description_html = sprintf(
545 '<p class="wpsubs-settings-field__hint">%s</p>',
546 wp_kses_post( $description )
547 );
548 }
549
550 $style_attr = '';
551 if ( isset( $args['style'] ) ) {
552 $style_attr .= ' ' . $args['style'];
553 }
554
555 $other_attrs_html = '';
556 foreach ( ( $args['attributes'] ?? [] ) as $attr_key => $attr_value ) {
557 $other_attrs_html .= sprintf( ' %s="%s" ', esc_attr( $attr_key ), esc_attr( $attr_value ) );
558 }
559
560 $checked_attr = isset( $args['checked'] ) && (bool) $args['checked'] ? 'checked' : '';
561 $disabled_attr = isset( $args['disabled'] ) && (bool) $args['disabled'] ? 'disabled' : '';
562
563 ob_start();
564 ?>
565 <div class="wpsubs-settings-field<?php echo ! empty( $args['pro_locked'] ) ? ' wpsubs-settings-field--locked' : ''; ?>">
566 <div class="wpsubs-settings-field__label">
567 <?php
568 if ( ! empty( $args['pro_locked'] ) ) {
569 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Pre-escaped badge markup.
570 echo self::pro_badge_html();
571 }
572 ?>
573 <?php echo esc_html( $title ); ?>
574 </div>
575 <div class="wpsubs-settings-field__control">
576 <label class="wpsubs-settings-toggle-label" for="<?php echo esc_attr( $id ); ?>">
577 <input
578 id="<?php echo esc_attr( $id ); ?>"
579 name="<?php echo esc_attr( $id ); ?>"
580 class="wpsubs-toggle"
581 type="checkbox"
582 value="<?php echo esc_attr( $value ); ?>"
583 <?php echo esc_attr( $checked_attr ); ?>
584 <?php echo esc_attr( $disabled_attr ); ?>
585 <?php
586 // Output intentionally not escaped as element is already escaped during generation & re-escaping breaks the HTML structure.
587 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
588 echo $other_attrs_html;
589 ?>
590 />
591 <span class="wpsubs-toggle-ui" aria-hidden="true"></span>
592 <?php if ( ! empty( $label ) ) : ?>
593 <span class="wpsubs-settings-toggle-label__text"><?php echo esc_html( $label ); ?></span>
594 <?php endif; ?>
595 </label>
596 <?php echo wp_kses_post( $description_html ); ?>
597 </div>
598 </div>
599 <?php
600 $html_content = ob_get_clean();
601
602 // Output not escaped intentionally. Breaks the HTML structure when escaped.
603 // All form elements inside $html_content are pre-escaped during generation (esc_attr, esc_html).
604 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
605 return $should_print ? print( $html_content ) : $html_content;
606 }
607
608 /**
609 * Render Select field.
610 *
611 * - Args:
612 * - id (string) - Field ID.
613 * - title (string) - Field title.
614 * - description (string) - Field description (optional).
615 * - options (array) - Field options [value => label].
616 * - selected (string) - Selected option value.
617 * - disabled (string|array) - Disabled option value(s).
618 *
619 * @param array $args Field arguments.
620 * @param bool $should_print Whether to print the field or return as HTML string.
621 */
622 public static function render_select_field( $args = [], $should_print = true ) {
623 $title = $args['title'] ?? '';
624 $description = $args['description'] ?? '';
625
626 // Return error if ID is not provided.
627 if ( empty( $args['id'] ?? '' ) ) {
628 $field_hint = empty( $title ) ? 'Error' : $title;
629 $no_id_msg = '<p><strong>' . $field_hint . ':</strong> ' . __( 'Field ID is required.', 'subscription' ) . '</p>';
630 return $should_print ? print wp_kses_post( $no_id_msg ) : $no_id_msg;
631 }
632
633 // Select HTML.
634 $select_el_html = self::select_element( $args );
635
636 ob_start();
637 ?>
638 <div class="wpsubs-settings-field<?php echo ! empty( $args['pro_locked'] ) ? ' wpsubs-settings-field--locked' : ''; ?>">
639 <div class="wpsubs-settings-field__label">
640 <?php
641 if ( ! empty( $args['pro_locked'] ) ) {
642 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Pre-escaped badge markup.
643 echo self::pro_badge_html();
644 }
645 ?>
646 <?php echo esc_html( $title ); ?>
647 </div>
648 <div class="wpsubs-settings-field__control">
649 <?php
650 // Output intentionally not escaped as element is already escaped during generation & re-escaping breaks the HTML structure.
651 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
652 echo $select_el_html;
653 ?>
654 <?php if ( ! empty( $description ) ) : ?>
655 <p class="wpsubs-settings-field__hint"><?php echo wp_kses_post( $description ); ?></p>
656 <?php endif; ?>
657 </div>
658 </div>
659 <?php
660 $html_content = ob_get_clean();
661
662 // Output not escaped intentionally. Breaks the HTML structure when escaped.
663 // All form elements inside $html_content are pre-escaped during generation (esc_attr, esc_html).
664 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
665 return $should_print ? print( $html_content ) : $html_content;
666 }
667
668 /**
669 * Render Multiselect field.
670 * Just a wrapper over 'render_select_field' with multiple attribute.
671 *
672 * @param array $args Field arguments.
673 * @param bool $should_print Whether to print the field or return as HTML string.
674 */
675 public static function render_multiselect_field( $args = [], $should_print = true ) {
676 $default_multiselect_args = [
677 'attributes' => [
678 'multiple' => 'multiple',
679 ],
680 'enhanced' => true,
681 ];
682
683 $args = wp_parse_args( $args, $default_multiselect_args );
684
685 return self::render_select_field( $args, $should_print );
686 }
687
688 /**
689 * Render Joined field with multiple elements.
690 *
691 * - Args:
692 * - title (string) - Field title.
693 * - description (string) - Field description (optional).
694 * - vertical (bool) - Whether to show items vertically or not.
695 * - elements ([...string]) - Array of HTML elements to join.
696 *
697 * @param array $args Field arguments.
698 * @param bool $should_print Whether to print the field or return as HTML string.
699 */
700 public static function render_joined_field( $args = [], $should_print = true ) {
701 $title = $args['title'] ?? '';
702 $description = $args['description'] ?? '';
703
704 $vertical_style = ( $args['vertical'] ?? false ) ? 'flex-direction:column;' : '';
705
706 ob_start();
707 ?>
708 <div class="wpsubs-settings-field<?php echo ! empty( $args['pro_locked'] ) ? ' wpsubs-settings-field--locked' : ''; ?>">
709 <div class="wpsubs-settings-field__label">
710 <?php
711 if ( ! empty( $args['pro_locked'] ) ) {
712 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Pre-escaped badge markup.
713 echo self::pro_badge_html();
714 }
715 ?>
716 <?php echo esc_html( $title ); ?>
717 </div>
718 <div class="wpsubs-settings-field__control">
719 <div class="wpsubs-input-group"
720 <?php
721 if ( $vertical_style ) :
722 ?>
723 style="<?php echo esc_attr( $vertical_style ); ?>"<?php endif; ?>>
724 <?php
725 foreach ( ( $args['elements'] ?? [] ) as $element_html ) {
726 // Output intentionally not escaped as element is already escaped during generation & re-escaping breaks the HTML structure.
727 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
728 echo $element_html;
729 }
730 ?>
731 </div>
732 <?php if ( ! empty( $description ) ) : ?>
733 <p class="wpsubs-settings-field__hint"><?php echo wp_kses_post( $description ); ?></p>
734 <?php endif; ?>
735 </div>
736 </div>
737 <?php
738 $html_content = ob_get_clean();
739
740 // Output not escaped intentionally. Breaks the HTML structure when escaped.
741 // All form elements inside $html_content are pre-escaped during generation (esc_attr, esc_html).
742 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
743 return $should_print ? print( $html_content ) : $html_content;
744 }
745
746 /**
747 * Render an editable, reorderable list field (the `wpsubs-editlist` component).
748 *
749 * Generic and reusable: a sortable list of text items with per-row remove/move
750 * controls and an inline input + add button. The ordered list is serialized as
751 * JSON (`[{ key, label }]`) into a hidden input so it submits with the form;
752 * behaviour is wired by `WPSubsEditList` (admin-components/editlist.js). All user-facing
753 * strings are overridable so the field carries no feature-specific text.
754 *
755 * When `modal` is true the list lives inside a `wpsubs-modal` (via the
756 * `WPSubsModal` component) and the settings row only shows a trigger button with
757 * a live item count.
758 *
759 * - Args:
760 * - id (string) - Field ID / option key.
761 * - title (string)
762 * - description (string)
763 * - value (array) - Ordered list of { key, label } entries.
764 * - add_placeholder (string) - Inline input placeholder.
765 * - add_label (string) - Add button accessible label.
766 * - empty_text (string) - Message shown when the list is empty.
767 * - modal (bool) - Present the list inside a modal (default false).
768 * - button_label (string) - Modal trigger button text (modal mode).
769 * - modal_title (string) - Modal header title (modal mode; defaults to title).
770 * - pro_locked (bool)
771 *
772 * @param array $args Field arguments.
773 * @param bool $should_print Whether to print the field or return as HTML string.
774 */
775 public static function render_editlist_field( $args = [], $should_print = true ) {
776 $id = $args['id'] ?? '';
777 $title = $args['title'] ?? '';
778 $description = $args['description'] ?? '';
779 $items = is_array( $args['value'] ?? null ) ? $args['value'] : [];
780 $locked = ! empty( $args['pro_locked'] );
781 $modal = ! empty( $args['modal'] );
782
783 $add_placeholder = $args['add_placeholder'] ?? __( 'Add an item…', 'subscription' );
784 $add_label = $args['add_label'] ?? __( 'Add item', 'subscription' );
785 $empty_text = $args['empty_text'] ?? __( 'No items yet. Add one below.', 'subscription' );
786 $button_label = $args['button_label'] ?? __( 'Manage list', 'subscription' );
787 $modal_title = $args['modal_title'] ?? ( '' !== $title ? $title : __( 'Edit list', 'subscription' ) );
788
789 if ( empty( $id ) ) {
790 $field_hint = empty( $title ) ? 'Error' : $title;
791 $no_id_msg = '<p><strong>' . $field_hint . ':</strong> ' . __( 'Field ID is required.', 'subscription' ) . '</p>';
792 return $should_print ? print wp_kses_post( $no_id_msg ) : $no_id_msg;
793 }
794
795 $body = self::editlist_body_html( $items, $add_placeholder, $add_label, $empty_text, $locked );
796
797 ob_start();
798 ?>
799 <div class="wpsubs-settings-field<?php echo $locked ? ' wpsubs-settings-field--locked' : ''; ?>">
800 <div class="wpsubs-settings-field__label">
801 <?php
802 if ( $locked ) {
803 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Pre-escaped badge markup.
804 echo self::pro_badge_html();
805 }
806 ?>
807 <?php echo esc_html( $title ); ?>
808 </div>
809 <div class="wpsubs-settings-field__control">
810 <div class="wpsubs-editlist<?php echo $locked ? ' wpsubs-editlist--locked' : ''; ?>">
811 <input type="hidden" id="<?php echo esc_attr( $id ); ?>" name="<?php echo esc_attr( $id ); ?>" value="<?php echo esc_attr( wp_json_encode( array_values( $items ) ) ); ?>" />
812 <?php if ( $modal ) : ?>
813 <button type="button" class="wpsubs-btn wpsubs-btn--outline wpsubs-editlist__trigger" data-wpsubs-modal-open="<?php echo esc_attr( $id . '_modal' ); ?>"<?php echo $locked ? ' disabled' : ''; ?>>
814 <?php echo esc_html( $button_label ); ?>
815 <span class="wpsubs-editlist__count"><?php echo esc_html( (string) count( $items ) ); ?></span>
816 </button>
817 <?php
818 wpsubs_render_modal(
819 [
820 'id' => $id . '_modal',
821 'title' => $modal_title,
822 'body' => $body,
823 'class' => 'wpsubs-modal--editlist',
824 'footer' => '<button type="button" class="wpsubs-btn wpsubs-btn--primary" data-wpsubs-modal-close>' . esc_html__( 'Done', 'subscription' ) . '</button>',
825 ]
826 );
827 ?>
828 <?php else : ?>
829 <?php
830 // Body markup is pre-escaped during generation.
831 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
832 echo $body;
833 ?>
834 <?php endif; ?>
835 </div>
836 <?php if ( ! empty( $description ) ) : ?>
837 <p class="wpsubs-settings-field__hint"><?php echo wp_kses_post( $description ); ?></p>
838 <?php endif; ?>
839 </div>
840 </div>
841 <?php
842 $html_content = ob_get_clean();
843
844 // Output not escaped intentionally. Breaks the HTML structure when escaped.
845 // All form elements inside $html_content are pre-escaped during generation (esc_attr, esc_html).
846 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
847 return $should_print ? print( $html_content ) : $html_content;
848 }
849
850 /**
851 * Build the inner markup of an edit list (items + empty message + add row).
852 *
853 * Shared by inline and modal presentations of {@see self::render_editlist_field}.
854 *
855 * @param array $items Ordered list of { key, label } entries.
856 * @param string $add_placeholder Inline input placeholder.
857 * @param string $add_label Add button accessible label.
858 * @param string $empty_text Message shown when the list is empty.
859 * @param bool $locked Whether the controls are disabled.
860 * @return string Pre-escaped HTML.
861 */
862 private static function editlist_body_html( array $items, $add_placeholder, $add_label, $empty_text, $locked ) {
863 ob_start();
864 ?>
865 <ul class="wpsubs-editlist__items">
866 <?php foreach ( $items as $item ) : ?>
867 <?php
868 $item_key = isset( $item['key'] ) ? (string) $item['key'] : '';
869 $item_label = isset( $item['label'] ) ? (string) $item['label'] : '';
870 if ( '' === $item_label ) {
871 continue;
872 }
873 ?>
874 <li class="wpsubs-editlist__item" data-key="<?php echo esc_attr( $item_key ); ?>">
875 <span class="wpsubs-editlist__handle" aria-hidden="true">&#8942;&#8942;</span>
876 <span class="wpsubs-editlist__label"><?php echo esc_html( $item_label ); ?></span>
877 <span class="wpsubs-editlist__actions">
878 <button type="button" class="wpsubs-editlist__btn" data-editlist-up aria-label="<?php esc_attr_e( 'Move up', 'subscription' ); ?>"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><polyline points="18 15 12 9 6 15"/></svg></button>
879 <button type="button" class="wpsubs-editlist__btn" data-editlist-down aria-label="<?php esc_attr_e( 'Move down', 'subscription' ); ?>"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><polyline points="6 9 12 15 18 9"/></svg></button>
880 <button type="button" class="wpsubs-editlist__btn wpsubs-editlist__btn--danger" data-editlist-remove aria-label="<?php esc_attr_e( 'Remove', 'subscription' ); ?>"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 6h18"/><path d="M19 6l-1 14a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 6"/><path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg></button>
881 </span>
882 </li>
883 <?php endforeach; ?>
884 </ul>
885 <p class="wpsubs-editlist__empty"<?php echo empty( $items ) ? '' : ' hidden'; ?>><?php echo esc_html( $empty_text ); ?></p>
886 <div class="wpsubs-editlist__add">
887 <input type="text" class="wpsubs-input wpsubs-editlist__input" placeholder="<?php echo esc_attr( $add_placeholder ); ?>"<?php echo $locked ? ' disabled' : ''; ?> />
888 <button type="button" class="wpsubs-editlist__add-btn" data-editlist-add aria-label="<?php echo esc_attr( $add_label ); ?>"<?php echo $locked ? ' disabled' : ''; ?>>
889 <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M20 6 9 17l-5-5"/></svg>
890 </button>
891 </div>
892 <?php
893 return ob_get_clean();
894 }
895 }
896