'setting_id', // Unique id * 'title' => 'My Setting', // Title or label for the setting * 'add_label' => '+ ADD', // Text for the "Add Row" button * 'del_label' => 'Delete' // Text for the "Delete" button * 'description' => 'Description', // Help text description * 'fields' => array( * 'field' => array( * 'type' => 'text' //text, select * 'label' => 'Name' * 'required' => false, * 'options' => array() * ) * ) // The attributes and labels for the fields * ); * * @since 2.0 * @package Simple Admin Pages */ class sapAdminPageSettingInfiniteTable_2_7_4 extends sapAdminPageSetting_2_7_4 { public $has_editor = false; // Whether an editor field is included in the table columns public $add_label = '+ Add Row'; // Label applied to the add row button public $del_label = 'Delete'; // Label applied to the delete row button public $fields = array(); // Settings that can be set for each row in the table public $sanitize_callback = 'sap_sanitize_infinite_table'; /** * Add in the JS requried for rows to be added and the values to be stored * @since 2.0 */ public $scripts = array( 'sap-infinite-table' => array( 'path' => 'js/infinite_table.js', 'dependencies' => array( 'jquery' ), 'version' => SAP_VERSION, 'footer' => true, ), ); /** * Add in the CSS requried for rows to be displayed correctly * @since 2.0 */ public $styles = array( 'sap-infinite-table' => array( 'path' => 'css/infinite_table.css', 'dependencies' => array( ), 'version' => SAP_VERSION, 'media' => 'all', ), ); /** * Display this setting * @since 2.0 */ public function display_setting() { $input_name = $this->get_input_name(); $values = json_decode( html_entity_decode( $this->value ?? '' ) ) ?? []; $this->fields = array_filter( $this->fields ); if ( ! is_array( $values ) ) { $values = $this->get_default_setting(); } $field_ids = implode( ',', array_keys( $this->fields ) ); $this->set_field_class_string(); ?>
print_conditional_data(); ?>>
' data-fieldids=''> $row) { ?> fields as $field_id => $field) { ?> has_editor = true; } ?> fields as $field_id => $field) { ?>
'> '> print_field_conditional_data( $field ); ?>> $field_id ); ?> get_editor_row_preview( $row->$field_id ) ); ?> $field_id ); ?> del_label ); ?>
'> '> Open Editor del_label ); ?>
add_label ); ?>
has_editor ) { ?>
id ) ); ?>'>
id ) ); ?>
display_disabled(); ?>
display_description(); } /** * Recursively print out select options * @since 2.5.3 */ public function print_options( $options, $row = false, $field_id = 0 ) { foreach ( $options as $option_value => $option_name ) { if ( is_array( $option_name ) ) { ?> print_options( $option_name, $row, $field_id ); ?> $field_id ) ) ? $row->$field_id : false; ?> value, used in the main class * * @since 2.6.4 */ public function get_default_setting( $fallback_value = array() ) { return ! empty( $this->default ) ? $this->default : $fallback_value; } /** * Get the preview text for an editor input type * * @since 2.6.14 */ public function get_editor_row_preview( $value, $preview_length = 60 ) { return substr( wp_strip_all_tags( $value ), 0, $preview_length ) . ( strlen( $value ) > $preview_length ? '...' : '' ); } /** * Adds a 'class_string' property to each field * * @since 2.6.15 */ public function set_field_class_string() { foreach ( $this->fields as $field_id => $field ) { $this->fields[ $field_id ]['class_string'] = implode( ',', !empty( $field['classes'] ) ? $field['classes'] : array() ); } } /** * Determines whether a field in a row should be displayed, based on its * conditional conditions, if any. * * @since 2.6.15 */ public function get_conditional_display( $field, $row ) { if ( empty( $field['conditional_on'] ) ) { return; } $conditional_on_value = is_array( $field['conditional_on_value'] ) ? $field['conditional_on_value'] : explode( ',', $field['conditional_on_value'] ); return ! in_array( $row->{$field['conditional_on']}, $conditional_on_value ) ? 'sap-hidden' : ''; } /** * Prints conditional data tags within the input element if necessary * * @since 2.6.15 */ public function print_field_conditional_data( $field ) { if ( empty( $field['conditional_on'] ) ) { return; } $conditional_on_value = is_array( $field['conditional_on_value'] ) ? implode( ',', $field['conditional_on_value'] ) : $field['conditional_on_value']; echo 'data-conditional_on="' . esc_attr( $field['conditional_on'] ) . '"'; echo 'data-conditional_on_value="' . esc_attr( $conditional_on_value ) . '"'; } } if ( ! function_exists( 'sap_sanitize_infinite_table' ) ) { function sap_sanitize_infinite_table( $value ) { $values = json_decode( $value, true ); if ( $values === null) { return null; } array_walk_recursive( $values, function ( &$item_value, $key ) { $item_value = wp_kses_post( $item_value ); }); return json_encode( $values ); } }