| 1 |
<?php |
| 2 |
/** |
| 3 |
* Render a table of WPCasa settings fields. |
| 4 |
* |
| 5 |
* This partial is required from the WPSight_Admin_Settings rendering context. |
| 6 |
* It expects `$settings_options` to contain one normalized field collection |
| 7 |
* and delegates each supported field type to its existing `option-*.php` |
| 8 |
* partial. Invalid field definitions are skipped instead of breaking the |
| 9 |
* complete settings page. |
| 10 |
* |
| 11 |
* @package WPCasa |
| 12 |
* @since 1.5.4 |
| 13 |
*/ |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
$settings_options = isset( $settings_options ) && is_array( $settings_options ) |
| 20 |
? $settings_options |
| 21 |
: array(); |
| 22 |
?> |
| 23 |
|
| 24 |
<table class="form-table"> |
| 25 |
<?php |
| 26 |
foreach ( $settings_options as $option ) { |
| 27 |
// A stable field ID is required for selectors, labels and option views. |
| 28 |
$has_valid_id = is_array( $option ) |
| 29 |
&& isset( $option['id'] ) |
| 30 |
&& is_scalar( $option['id'] ) |
| 31 |
&& '' !== (string) $option['id']; |
| 32 |
|
| 33 |
if ( ! $has_valid_id ) { |
| 34 |
continue; |
| 35 |
} |
| 36 |
|
| 37 |
$option_css = sanitize_html_class( $this->settings_name . '_' . $option['id'] ); |
| 38 |
$option_name = isset( $option['name'] ) && is_scalar( $option['name'] ) |
| 39 |
? stripslashes( (string) $option['name'] ) |
| 40 |
: ''; |
| 41 |
$option_desc = isset( $option['desc'] ) && is_scalar( $option['desc'] ) |
| 42 |
? stripslashes( (string) $option['desc'] ) |
| 43 |
: ''; |
| 44 |
$option_type = isset( $option['type'] ) && is_scalar( $option['type'] ) |
| 45 |
? sanitize_key( $option['type'] ) |
| 46 |
: ''; |
| 47 |
$option_classes = array(); |
| 48 |
$class = ''; |
| 49 |
$compare_class = ''; |
| 50 |
|
| 51 |
// Sanitize every custom class separately before using it in the row. |
| 52 |
if ( isset( $option['class'] ) && is_scalar( $option['class'] ) ) { |
| 53 |
foreach ( preg_split( '/\s+/', (string) $option['class'] ) as $option_class ) { |
| 54 |
$option_class = sanitize_html_class( $option_class ); |
| 55 |
|
| 56 |
if ( '' !== $option_class ) { |
| 57 |
$option_classes[] = $option_class; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
if ( ! empty( $option_classes ) ) { |
| 62 |
$class = ' ' . implode( ' ', $option_classes ); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
// Preserve the existing show_if contract for checkbox dependencies. |
| 67 |
if ( isset( $option['show_if'] ) && is_array( $option['show_if'] ) ) { |
| 68 |
$dependable_option_id = isset( $option['show_if']['id'] ) |
| 69 |
? sanitize_key( $option['show_if']['id'] ) |
| 70 |
: ''; |
| 71 |
$comparable_value = isset( $option['show_if']['value'] ) |
| 72 |
? $option['show_if']['value'] |
| 73 |
: null; |
| 74 |
$comparison_operator = isset( $option['show_if']['compare'] ) |
| 75 |
? $option['show_if']['compare'] |
| 76 |
: '=='; |
| 77 |
|
| 78 |
if ( '' !== $dependable_option_id && '==' === $comparison_operator ) { |
| 79 |
$dependable_option_value = wpsight_get_option( $dependable_option_id ); |
| 80 |
|
| 81 |
// phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual -- Stored settings can be strings or integers. |
| 82 |
$compare_result = $comparable_value == $dependable_option_value; |
| 83 |
|
| 84 |
$compare_class = $compare_result ? '' : ' hidden'; |
| 85 |
|
| 86 |
$dependable_selector = '#setting-' . sanitize_html_class( $this->settings_name . '_' . $dependable_option_id ); |
| 87 |
|
| 88 |
$option_selector = '#setting-' . $option_css; |
| 89 |
?> |
| 90 |
|
| 91 |
<script> |
| 92 |
jQuery( document ).ready( function( $ ) { |
| 93 |
var dependableOptionId = <?php echo wp_json_encode( $dependable_selector ); ?>; |
| 94 |
var optionId = <?php echo wp_json_encode( $option_selector ); ?>; |
| 95 |
|
| 96 |
$( document ).on( 'click', dependableOptionId, function() { |
| 97 |
$( optionId ).closest( 'tr' ).toggleClass( 'hidden', ! $( dependableOptionId ).is( ':checked' ) ); |
| 98 |
} ); |
| 99 |
} ); |
| 100 |
</script> |
| 101 |
|
| 102 |
<?php |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
$option_view = WPSIGHT_PLUGIN_DIR . '/includes/admin/views/option-' . $option_type . '.php'; |
| 107 |
|
| 108 |
// Unknown field types must not be able to include arbitrary files. |
| 109 |
if ( ! file_exists( $option_view ) ) { |
| 110 |
continue; |
| 111 |
} |
| 112 |
?> |
| 113 |
|
| 114 |
<tr valign="top" class="setting-<?php echo esc_attr( $option_css ); ?>-tr<?php echo esc_attr( $class . $compare_class ); ?>"> |
| 115 |
<?php |
| 116 |
if ( in_array( $option_type, array( 'pageheading', 'heading' ), true ) ) { |
| 117 |
require $option_view; |
| 118 |
} else { |
| 119 |
?> |
| 120 |
|
| 121 |
<th scope="row"> |
| 122 |
<label for="setting-<?php echo esc_attr( $option_css ); ?>"><?php echo esc_html( $option_name ); ?></label> |
| 123 |
<p class="description"><?php echo wp_kses( $option_desc, wp_kses_allowed_html( 'post' ) ); ?></p> |
| 124 |
</th> |
| 125 |
<td> |
| 126 |
<div class="wpsight-settings-field-wrap wpsight-settings-field-<?php echo esc_attr( $option_type ); ?>-wrap"> |
| 127 |
<?php require $option_view; ?> |
| 128 |
</div> |
| 129 |
</td> |
| 130 |
|
| 131 |
<?php |
| 132 |
} |
| 133 |
?> |
| 134 |
</tr> |
| 135 |
|
| 136 |
<?php |
| 137 |
} |
| 138 |
?> |
| 139 |
</table> |
| 140 |
|