|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * CMB2 Multicheck by Post Type |
| 5 | + * |
| 6 | + * @package CMB2 Default Tags field/metabox |
| 7 | + * @author Daniele Mte90 Scasciafratte |
| 8 | + */ |
| 9 | + |
| 10 | +if ( !defined( 'ABSPATH' ) ) exit; |
| 11 | + |
| 12 | +if ( ! class_exists( 'CMB2_Field_Textarea_With_Checkbox' ) ) { |
| 13 | + /** |
| 14 | + * Class CMB2_Field_Textarea_With_Checkbox |
| 15 | + */ |
| 16 | + class CMB2_Field_Textarea_With_Checkbox { |
| 17 | + |
| 18 | + /** |
| 19 | + * Current version number |
| 20 | + */ |
| 21 | + const VERSION = '1.0.0'; |
| 22 | + /** |
| 23 | + * Initialize the plugin |
| 24 | + */ |
| 25 | + public function __construct() { |
| 26 | + add_action( 'cmb2_render_textarea_with_checkbox', [$this, 'render_textarea_with_checkbox'], 10, 5 ); |
| 27 | + add_filter( 'cmb2_sanitize_textarea_with_checkbox', [$this, 'sanitize_textarea_with_checkbox'], 10, 5 ); |
| 28 | + add_filter( 'cmb2_types_esc_textarea_with_checkbox', [$this, 'escape_textarea_with_checkbox'], 10, 4 ); |
| 29 | + } |
| 30 | + |
| 31 | + public function render_textarea_with_checkbox( $field, $field_escaped_value, $field_object_id, $field_object_type, $field_type_object ) { |
| 32 | + // the properties of the fields. |
| 33 | + $field_escaped_value = wp_parse_args( $field_escaped_value, [ |
| 34 | + 'text'=> '', |
| 35 | + 'status'=> '', |
| 36 | + ] ); |
| 37 | + $checked = false; |
| 38 | + if ( ! empty( $field_escaped_value['status'] ) ) { |
| 39 | + $checked = true; |
| 40 | + } |
| 41 | + ?> |
| 42 | + <div style="overflow: hidden;"> |
| 43 | + <?= $field_type_object->textarea( [ |
| 44 | + 'name' => $field_type_object->_name( '[text]' ), |
| 45 | + 'id' => $field_type_object->_id( '_text' ), |
| 46 | + 'value' => $field_escaped_value['text'], |
| 47 | + ] ); ?> |
| 48 | + </div> |
| 49 | + <div style="overflow: hidden"> |
| 50 | + <p><label for="<?= $field_type_object->_id( '_status' ); ?>"><?= $field_type_object->field->args('title_checkbox'); ?></label></p> |
| 51 | + <?= $field_type_object->checkbox( [ |
| 52 | + 'type' => 'checkbox', |
| 53 | + 'name' => $field_type_object->_name( '[status]' ), |
| 54 | + 'id' => $field_type_object->_id( '_status' ), |
| 55 | + ], $checked ); ?> |
| 56 | + </div> |
| 57 | + <?php |
| 58 | + echo $field_type_object->_desc( true ); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Sanitize Field. |
| 63 | + */ |
| 64 | + public static function sanitize_textarea_with_checkbox( $check, $meta_value, $object_id, $field_args, $sanitize_object ) { |
| 65 | + if ( !is_array( $meta_value ) || !( array_key_exists('repeatable', $field_args ) && $field_args['repeatable'] == TRUE ) ) { |
| 66 | + return $check; |
| 67 | + } |
| 68 | + |
| 69 | + $new_values = array(); |
| 70 | + foreach ( $meta_value as $key => $val ) { |
| 71 | + if( !empty( $meta_value[$key]['text'] ) ) { |
| 72 | + $new_values[$key] = array_filter( array_map( 'sanitize_text_field', $val ) ); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return array_filter( array_values( $new_values ) ); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Escape Field. |
| 81 | + */ |
| 82 | + public static function escape_textarea_with_checkbox( $check, $meta_value, $field_args, $field_object ) { |
| 83 | + if ( !is_array( $meta_value ) || ! $field_args['repeatable'] ) { |
| 84 | + return $check; |
| 85 | + } |
| 86 | + |
| 87 | + $new_values = array(); |
| 88 | + foreach ( $meta_value as $key => $val ) { |
| 89 | + if( !empty( $meta_value[$key]['text'] ) ) { |
| 90 | + $new_values[$key] = array_filter( array_map( 'esc_attr', $val ) ); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return array_filter( array_values( $new_values ) ); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + $cmb2_field_textarea_with_checkbox = new CMB2_Field_Textarea_With_Checkbox(); |
| 99 | +} |
0 commit comments