From bbe1d49ba1ed40342247bc0c01617f05c2d218e6 Mon Sep 17 00:00:00 2001 From: lipemat Date: Tue, 18 Dec 2018 14:21:15 -0500 Subject: [PATCH 1/2] introduce true_false field for CMB2 --- src/CMB2/Field/True_False.php | 185 ++++++++++++++++++++++++++++++++++ src/CMB2/Field_Type.php | 22 +++- 2 files changed, 205 insertions(+), 2 deletions(-) create mode 100644 src/CMB2/Field/True_False.php diff --git a/src/CMB2/Field/True_False.php b/src/CMB2/Field/True_False.php new file mode 100644 index 00000000..07883073 --- /dev/null +++ b/src/CMB2/Field/True_False.php @@ -0,0 +1,185 @@ + 'checkbox', + 'class' => 'cmb2-option cmb2-list', + 'value' => 'on', + 'desc' => '', + ]; + + $meta_value = $this->field->escaped_value(); + + $is_checked = $this->is_checked ?? ! empty( $meta_value ); + + if ( $is_checked ) { + $defaults['checked'] = 'checked'; + } + + return $this->rendered( + sprintf( + '%s ', + $this->render_toggle_field( $defaults ), + $this->_id(), + $this->_desc() + ) + ); + } + + /** + * Generates a CSS only driven toggle on/off field. + * + * @param array $args + * + * @return string + */ + protected function render_toggle_field( array $args ) : string { + ob_start(); + $args['class'] .= ' checkbox-toggle-checkbox'; + ?> +
+ parse_args( 'checkbox', $args ) ); //phpcs:ignore ?> + +
+ styles(); + + return ob_get_clean(); + } + + /** + * One-time loaded CSS to style the field + * + * @props Proto.IO which gave me the inspiration + * + * @link https://proto.io/freebies/onoff/ + * + * @return void + */ + protected function styles() : void { + static $displayed = false; + if ( $displayed ) { + return; + } + $displayed = true; + + ?> + + set( [ 'type' => $this->title ] ); } + /** + * True false switch like checkbox + * + * Custom to WP-Libs + * + * @since 1.17.0 + * + * @return Field + */ + public function true_false() : Field { + return $this->set( [ + 'type' => $this->checkbox, + 'render_class' => True_False::class, + ]); + } + /** * Standard text field (large). @@ -585,9 +602,10 @@ public function checkbox( string $layout = 'block' ) : Field { $_args = [ 'type' => $this->checkbox, ]; - if( 'block' !== $layout ) { - $_args['render_row_cb' ] = [ Checkbox::in(), 'render_field_callback' ]; + if ( 'block' !== $layout ) { + $_args['render_row_cb'] = [ Checkbox::in(), 'render_field_callback' ]; } + return $this->set( $_args ); } From 6605019c242f066ed788cbc51c1a54de35403805 Mon Sep 17 00:00:00 2001 From: lipemat Date: Tue, 18 Dec 2018 14:21:36 -0500 Subject: [PATCH 2/2] bump to 1.17.0 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a9421a1a..fb7fe833 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # WordPress Libs WordPress library which supports a core plugin and theme. -**Version [1.16.0](https://github.com/lipemat/wordpress-lipe-libs/releases/tag/1.16.0)** +**Version [1.17.0](https://github.com/lipemat/wordpress-lipe-libs/releases/tag/1.17.0)** ### Requirements 1. PHP Version 7.1.3+ (Recommended 7.2.0+)