Skip to content

Commit 6e7f2be

Browse files
authored
Merge pull request #94 from Mte90/textarea-with-title
Textarea with checkbox
2 parents e16b52c + 416fb68 commit 6e7f2be

File tree

3 files changed

+115
-5
lines changed

3 files changed

+115
-5
lines changed

custom-field-types/multicheck_posttype-field_type.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<?php
2-
//By Daniele Mte90 Scasciafratte
3-
//render multicheck-posttype
2+
/**
3+
* CMB2 Multicheck by Post Type
4+
*
5+
* @package CMB2 Default Tags field/metabox
6+
* @author Daniele Mte90 Scasciafratte
7+
*/
8+
49
add_action( 'cmb2_render_multicheck_posttype', 'ds_cmb_render_multicheck_posttype', 10, 5 );
510

611
function ds_cmb_render_multicheck_posttype( $field, $escaped_value, $object_id, $object_type, $field_type_object ) {
@@ -10,6 +15,7 @@ function ds_cmb_render_multicheck_posttype( $field, $escaped_value, $object_id,
1015
}
1116

1217
$cpts = get_post_types();
18+
// To disable the avalaibility of post types
1319
unset( $cpts[ 'nav_menu_item' ] );
1420
unset( $cpts[ 'revision' ] );
1521
$cpts = apply_filters( 'multicheck_posttype_' . $field->args[ '_id' ], $cpts );

custom-field-types/multicheck_title-field_type.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
<?php
2-
//By Daniele Mte90 Scasciafratte
3-
//Is multicheck but with section title
42

3+
/**
4+
* CMB2 Multicheck by Title
5+
*
6+
* @package CMB2 Default Tags field/metabox
7+
* @author Daniele Mte90 Scasciafratte
8+
*/
9+
510
/*
6-
//How to use
11+
// Example
712
$array[ 'id_of_the_key' ] = __( 'ID of the key' );
813
$fields[ 'Title of the section' ] = $array;
914
$cmb->add_field( array(
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)