← All changes
|
Libs/adminify-framework/fields/checkbox/checkbox.php
+66
-83
4.2.13
→
3.2.4.1
View file →
| @@ -1,7 +1,6 @@ | ||
| 1 | -<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly. | |
| 2 | - | |
| 3 | -use PXLBSAdminify\Inc\Utils; | |
| 1 | +<?php if ( ! defined( 'ABSPATH' ) ) { | |
| 2 | + die; } // Cannot access directly. | |
| 4 | 3 | /** |
| 5 | 4 | * |
| 6 | 5 | * Field: checkbox |
| 7 | 6 | * |
| @@ -6,98 +5,82 @@ | ||
| 6 | 5 | * Field: checkbox |
| 7 | 6 | * |
| 8 | 7 | * @since 1.0.0 |
| 9 | 8 | * @version 1.0.0 |
| 10 | - * | |
| 11 | 9 | */ |
| 12 | 10 | if ( ! class_exists( 'ADMINIFY_Field_checkbox' ) ) { |
| 13 | - class ADMINIFY_Field_checkbox extends ADMINIFY_Fields { | |
| 11 | + class ADMINIFY_Field_checkbox extends ADMINIFY_Fields { | |
| 14 | 12 | |
| 15 | - public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) { | |
| 16 | - parent::__construct( $field, $value, $unique, $where, $parent ); | |
| 17 | - } | |
| 13 | + public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) { | |
| 14 | + parent::__construct( $field, $value, $unique, $where, $parent ); | |
| 15 | + } | |
| 18 | 16 | |
| 19 | - public function render() { | |
| 17 | + public function render() { | |
| 18 | + $args = wp_parse_args( | |
| 19 | + $this->field, | |
| 20 | + [ | |
| 21 | + 'inline' => false, | |
| 22 | + 'query_args' => [], | |
| 23 | + ] | |
| 24 | + ); | |
| 20 | 25 | |
| 21 | - $args = wp_parse_args( $this->field, array( | |
| 22 | - 'inline' => false, | |
| 23 | - 'query_args' => array(), | |
| 24 | - 'check_all' => false, | |
| 25 | - 'check_all_text' => esc_html__( 'Check/Uncheck All', 'adminify' ), | |
| 26 | - ) ); | |
| 26 | + $inline_class = ( $args['inline'] ) ? 'adminify--inline-list' : ''; | |
| 27 | 27 | |
| 28 | - $inline_class = ( $args['inline'] ) ? ' class="adminify--inline-list"' : ''; | |
| 28 | + echo wp_kses_post( $this->field_before() ); | |
| 29 | 29 | |
| 30 | - echo wp_kses_post( $this->field_before() ); | |
| 30 | + if ( isset( $this->field['options'] ) ) { | |
| 31 | + $value = ( is_array( $this->value ) ) ? $this->value : array_filter( (array) $this->value ); | |
| 32 | + $options = $this->field['options']; | |
| 33 | + $options = ( is_array( $options ) ) ? $options : array_filter( $this->field_data( $options, false, $args['query_args'] ) ); | |
| 31 | 34 | |
| 32 | - if ( isset( $this->field['options'] ) ) { | |
| 35 | + if ( is_array( $options ) && ! empty( $options ) ) { | |
| 36 | + echo '<ul class="' . esc_attr( $inline_class ) . '">'; | |
| 33 | 37 | |
| 34 | - $value = ( is_array( $this->value ) ) ? $this->value : array_filter( (array) $this->value ); | |
| 35 | - $options = $this->field['options']; | |
| 36 | - $options = ( is_array( $options ) ) ? $options : array_filter( $this->field_data( $options, false, $args['query_args'] ) ); | |
| 38 | + foreach ( $options as $option_key => $option_value ) { | |
| 39 | + if ( is_array( $option_value ) && ! empty( $option_value ) ) { | |
| 40 | + echo '<li>'; | |
| 41 | + echo '<ul>'; | |
| 42 | + echo '<li><strong>' . esc_attr( $option_key ) . '</strong></li>'; | |
| 43 | + foreach ( $option_value as $sub_key => $sub_value ) { | |
| 44 | + $checked = ( in_array( $sub_key, $value ) ) ? ' checked' : ''; | |
| 45 | + echo '<li>'; | |
| 46 | + echo '<label>'; | |
| 47 | + echo '<input type="checkbox" name="' . esc_attr( $this->field_name( '[]' ) ) . '" value="' . esc_attr( $sub_key ) . '"' . wp_kses_post( $this->field_attributes() ) . esc_attr( $checked ) . '/>'; | |
| 48 | + echo '<span class="adminify--text">' . esc_attr( $sub_value ) . '</span>'; | |
| 49 | + echo '</label>'; | |
| 50 | + echo '</li>'; | |
| 51 | + } | |
| 52 | + echo '</ul>'; | |
| 53 | + echo '</li>'; | |
| 54 | + } else { | |
| 55 | + $checked = ( in_array( $option_key, $value ) ) ? ' checked' : ''; | |
| 37 | 56 | |
| 38 | - if ( is_array( $options ) && ! empty( $options ) ) { | |
| 57 | + echo '<li>'; | |
| 58 | + echo '<label>'; | |
| 59 | + echo '<input type="checkbox" name="' . esc_attr( $this->field_name( '[]' ) ) . '" value="' . esc_attr( $option_key ) . '"' . wp_kses_post( $this->field_attributes() ) . esc_attr( $checked ) . '/>'; | |
| 60 | + echo '<span class="adminify--text">' . esc_attr( $option_value ) . '</span>'; | |
| 61 | + echo '</label>'; | |
| 62 | + echo '</li>'; | |
| 63 | + } | |
| 64 | + } | |
| 39 | 65 | |
| 40 | - echo '<ul'. $inline_class .'>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- attribute value pre-escaped with esc_attr() where built. | |
| 66 | + echo '</ul>'; | |
| 67 | + } else { | |
| 68 | + if ( ! empty( $this->field['empty_message'] ) ) { | |
| 69 | + echo esc_attr( $this->field['empty_message'] ); | |
| 70 | + } else { | |
| 71 | + echo esc_html__( 'No data available.', 'adminify' ); | |
| 72 | + } | |
| 73 | + } | |
| 74 | + } else { | |
| 75 | + echo '<label class="adminify-checkbox">'; | |
| 76 | + echo '<input type="hidden" name="' . esc_attr( $this->field_name() ) . '" value="' . esc_attr( $this->value ) . '" class="adminify--input"' . wp_kses_post( $this->field_attributes() ) . '/>'; | |
| 77 | + echo '<input type="checkbox" name="_pseudo" class="adminify--checkbox"' . esc_attr( checked( $this->value, 1, false ) ) . wp_kses_post( $this->field_attributes() ) . '/>'; | |
| 78 | + echo ( ! empty( $this->field['label'] ) ) ? '<span class="adminify--text">' . esc_html( $this->field['label'] ) . '</span>' : ''; | |
| 79 | + echo '</label>'; | |
| 80 | + } | |
| 41 | 81 | |
| 42 | - foreach ( $options as $option_key => $option_value ) { | |
| 82 | + echo wp_kses_post( $this->field_after() ); | |
| 83 | + } | |
| 43 | 84 | |
| 44 | - if ( is_array( $option_value ) && ! empty( $option_value ) ) { | |
| 45 | - | |
| 46 | - echo '<li>'; | |
| 47 | - echo '<ul>'; | |
| 48 | - echo '<li><strong>'. esc_attr( $option_key ) .'</strong></li>'; | |
| 49 | - foreach ( $option_value as $sub_key => $sub_value ) { | |
| 50 | - $checked = ( in_array( $sub_key, $value ) ) ? ' checked' : ''; | |
| 51 | - echo '<li>'; | |
| 52 | - echo '<label>'; | |
| 53 | - echo '<input type="checkbox" name="'. esc_attr( $this->field_name( '[]' ) ) .'" value="'. esc_attr( $sub_key ) .'"'. $this->field_attributes() . esc_attr( $checked ) .'/>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- field_attributes() escapes each attribute via esc_attr() | |
| 54 | - echo '<span class="adminify--text">'. Utils::kses_custom( $sub_value ) .'</span>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- kses_custom() is a wp_kses() wrapper; output already escaped. | |
| 55 | - echo '</label>'; | |
| 56 | - echo '</li>'; | |
| 57 | - } | |
| 58 | - echo '</ul>'; | |
| 59 | - echo '</li>'; | |
| 60 | - | |
| 61 | - } else { | |
| 62 | - | |
| 63 | - $checked = ( in_array( $option_key, $value ) ) ? ' checked' : ''; | |
| 64 | - | |
| 65 | - echo '<li>'; | |
| 66 | - echo '<label>'; | |
| 67 | - echo '<input type="checkbox" name="'. esc_attr( $this->field_name( '[]' ) ) .'" value="'. esc_attr( $option_key ) .'"'. $this->field_attributes() . esc_attr( $checked ) .'/>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- field_attributes() escapes each attribute via esc_attr() | |
| 68 | - echo '<span class="adminify--text">'. Utils::kses_custom( $option_value ) .'</span>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- kses_custom() is a wp_kses() wrapper; output already escaped. | |
| 69 | - echo '</label>'; | |
| 70 | - echo '</li>'; | |
| 71 | - | |
| 72 | - } | |
| 73 | - | |
| 74 | - } | |
| 75 | - | |
| 76 | - echo '</ul>'; | |
| 77 | - | |
| 78 | - if ( $args['check_all'] ) { | |
| 79 | - echo '<div class="adminify-checkbox-all">'. esc_html( $args['check_all_text'] ) .'</div>'; | |
| 80 | - } | |
| 81 | - | |
| 82 | - } else { | |
| 83 | - | |
| 84 | - echo ( ! empty( $this->field['empty_message'] ) ) ? esc_attr( $this->field['empty_message'] ) : esc_html__( 'No data available.', 'adminify' ); | |
| 85 | - | |
| 86 | - } | |
| 87 | - | |
| 88 | - } else { | |
| 89 | - | |
| 90 | - echo '<label class="adminify-checkbox">'; | |
| 91 | - echo '<input type="hidden" name="'. esc_attr( $this->field_name() ) .'" value="'. esc_attr( $this->value ) .'" class="adminify--input"'. $this->field_attributes() .'/>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- field_attributes() escapes each attribute via esc_attr() | |
| 92 | - echo '<input type="checkbox" name="_pseudo" class="adminify--checkbox"'. esc_attr( checked( $this->value, 1, false ) ) . $this->field_attributes() .'/>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- field_attributes() escapes each attribute via esc_attr() | |
| 93 | - echo ( ! empty( $this->field['label'] ) ) ? '<span class="adminify--text">'. Utils::kses_custom( $this->field['label'] ) .'</span>' : ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- kses_custom() is a wp_kses() wrapper; output already escaped. | |
| 94 | - echo '</label>'; | |
| 95 | - | |
| 96 | - } | |
| 97 | - | |
| 98 | - echo wp_kses_post( $this->field_after() ); | |
| 99 | - | |
| 100 | - } | |
| 101 | - | |
| 102 | - } | |
| 85 | + } | |
| 103 | 86 | } |