From 14f962dd3944cf0a2f7c55e1b89b7b19f73ba1c9 Mon Sep 17 00:00:00 2001 From: Champ Camba Date: Wed, 8 Jan 2020 17:51:23 +0800 Subject: [PATCH] Add field notice --- assets/css/um-styles.css | 46 +++++++++++++++++ includes/core/class-fields.php | 94 ++++++++++++++++++++++++++++++++-- includes/core/class-form.php | 46 +++++++++++++++++ 3 files changed, 182 insertions(+), 4 deletions(-) diff --git a/assets/css/um-styles.css b/assets/css/um-styles.css index 2485a183..66facbcc 100644 --- a/assets/css/um-styles.css +++ b/assets/css/um-styles.css @@ -259,6 +259,52 @@ p.um-notice.warning a { margin: 12px 0 0 0; } +/* + - Notices +*/ +.um-field-notice { + width: auto; + max-width: 100%; + background: #497BC7; + -moz-border-radius: 3px; + -webkit-border-radius: 3px; + border-radius: 3px; + color: #fff; + box-sizing: border-box; + position: relative; + padding: 12px; + font-size: 14px; + line-height: 20px !important; + margin: 5px 0 0 0; +} + +.um-field-notice a{color: #fff !important;text-decoration: underline !important} + +.um-field-notice .um-field-arrow { + top: -17px; + left: 10px; + position: absolute; + z-index: 1; + color: #497BC7 !important; + font-size: 28px; + line-height: 1em !important; +} + +.um-notice-block { + width: auto; + max-width: 100%; + background: #497BC7; + -moz-border-radius: 3px; + -webkit-border-radius: 3px; + border-radius: 3px; + color: #fff; + box-sizing: border-box; + position: relative; + padding: 12px; + font-size: 14px; + line-height: 1em !important; + margin: 12px 0 0 0; +} /* - Help tooltips */ diff --git a/includes/core/class-fields.php b/includes/core/class-fields.php index a9dbec72..e95b2b00 100644 --- a/includes/core/class-fields.php +++ b/includes/core/class-fields.php @@ -388,6 +388,35 @@ if ( ! class_exists( 'um\core\Fields' ) ) { } + /** + * Print field notice + * + * @param string $text + * @param bool $force_show + * + * @return string + */ + function field_notice( $text, $force_show = false ) { + if ( $force_show ) { + $output = '
' . $text . '
'; + return $output; + } + + + if ( isset( $this->set_id ) && UM()->form()->processing == $this->set_id ) { + $output = '
' . $text . '
'; + } else { + $output = ''; + } + + if ( ! UM()->form()->processing ) { + $output = '
' . $text . '
'; + } + + return $output; + } + + /** * Checks if field has a server-side error * @@ -399,6 +428,17 @@ if ( ! class_exists( 'um\core\Fields' ) ) { return UM()->form()->has_error( $key ); } + /** + * Checks if field has a notice + * + * @param string $key + * + * @return boolean + */ + function is_notice( $key ) { + return UM()->form()->has_notice( $key ); + } + /** * Returns field error @@ -411,6 +451,17 @@ if ( ! class_exists( 'um\core\Fields' ) ) { return UM()->form()->errors[ $key ]; } + /** + * Returns field notices + * + * @param string $key + * + * @return string + */ + function show_notice( $key ) { + return UM()->form()->notices[ $key ]; + } + /** * Display field label @@ -2026,6 +2077,8 @@ if ( ! class_exists( 'um\core\Fields' ) ) { * ?> */ $field_id = apply_filters( 'um_completeness_field_id', $field_id, $data, $args ); + + /* Begin by field type */ switch ( $type ) { @@ -2054,6 +2107,7 @@ if ( ! class_exists( 'um\core\Fields' ) ) { * } * ?> */ + $output .= apply_filters( "um_edit_field_{$mode}_{$type}", $output, $data ); break; @@ -2090,9 +2144,11 @@ if ( ! class_exists( 'um\core\Fields' ) ) { if ( $this->is_error( $key ) ) { $output .= $this->field_error( $this->show_error( $key ) ); + }else if ( $this->is_notice( $key ) ) { + $output .= $this->field_notice( $this->show_notice( $key ) ); } - $output .= ''; + $output .= ''; break; /* Text */ @@ -2125,6 +2181,8 @@ if ( ! class_exists( 'um\core\Fields' ) ) { if ( $this->is_error( $key ) ) { $output .= $this->field_error( $this->show_error( $key ) ); + }else if ( $this->is_notice( $key ) ) { + $output .= $this->field_notice( $this->show_notice( $key ) ); } $output .= ''; @@ -2161,6 +2219,8 @@ if ( ! class_exists( 'um\core\Fields' ) ) { if ( $this->is_error( $key ) ) { $output .= $this->field_error( $this->show_error( $key ) ); + }else if ( $this->is_notice( $key ) ) { + $output .= $this->field_notice( $this->show_notice( $key ) ); } $output .= ''; @@ -2195,6 +2255,8 @@ if ( ! class_exists( 'um\core\Fields' ) ) { if ( $this->is_error( $key ) ) { $output .= $this->field_error( $this->show_error( $key ) ); + }else if ( $this->is_notice( $key ) ) { + $output .= $this->field_notice( $this->show_notice( $key ) ); } $output .= ''; @@ -2224,6 +2286,8 @@ if ( ! class_exists( 'um\core\Fields' ) ) { if ( $this->is_error( $key ) ) { $output .= $this->field_error( $this->show_error( $key ) ); + }else if ( $this->is_notice( $key ) ) { + $output .= $this->field_notice( $this->show_notice( $key ) ); } $output .= ''; @@ -2258,6 +2322,8 @@ if ( ! class_exists( 'um\core\Fields' ) ) { if ( $this->is_error( $key ) ) { $output .= $this->field_error( $this->show_error( $key ) ); + }else if ( $this->is_notice( $key ) ) { + $output .= $this->field_notice( $this->show_notice( $key ) ); } $output .= ''; @@ -2285,6 +2351,8 @@ if ( ! class_exists( 'um\core\Fields' ) ) { if ( $this->is_error( $key ) ) { $output .= $this->field_error( $this->show_error( $key ) ); + }else if ( $this->is_notice( $key ) ) { + $output .= $this->field_notice( $this->show_notice( $key ) ); } $output .= ''; @@ -2318,6 +2386,8 @@ if ( ! class_exists( 'um\core\Fields' ) ) { if ( $this->is_error( $key ) ) { $output .= $this->field_error( $this->show_error( $key ) ); + }else if ( $this->is_notice( $key ) ) { + $output .= $this->field_notice( $this->show_notice( $key ) ); } $output .= ''; @@ -2346,6 +2416,8 @@ if ( ! class_exists( 'um\core\Fields' ) ) { if ( $this->is_error( $key ) ) { $output .= $this->field_error( $this->show_error( $key ) ); + }else if ( $this->is_notice( $key ) ) { + $output .= $this->field_notice( $this->show_notice( $key ) ); } $output .= ''; @@ -2374,6 +2446,8 @@ if ( ! class_exists( 'um\core\Fields' ) ) { if ( $this->is_error( $key ) ) { $output .= $this->field_error( $this->show_error( $key ) ); + }else if ( $this->is_notice( $key ) ) { + $output .= $this->field_notice( $this->show_notice( $key ) ); } $output .= ''; @@ -2454,6 +2528,8 @@ if ( ! class_exists( 'um\core\Fields' ) ) { if ( $this->is_error( $key ) ) { $output .= $this->field_error( $this->show_error( $key ) ); + }else if ( $this->is_notice( $key ) ) { + $output .= $this->field_notice( $this->show_notice( $key ) ); } $output .= ''; @@ -2555,8 +2631,10 @@ if ( ! class_exists( 'um\core\Fields' ) ) { $output .= ''; $output .= ''; /* end */ - if ($this->is_error( $key )) { + if ( $this->is_error( $key ) ) { $output .= $this->field_error( $this->show_error( $key ) ); + }else if ( $this->is_notice( $key ) ) { + $output .= $this->field_notice( $this->show_notice( $key ) ); } $output .= ''; @@ -2654,6 +2732,8 @@ if ( ! class_exists( 'um\core\Fields' ) ) { /* end */ if ( $this->is_error( $key ) ) { $output .= $this->field_error( $this->show_error( $key ) ); + }else if ( $this->is_notice( $key ) ) { + $output .= $this->field_notice( $this->show_notice( $key ) ); } $output .= ''; @@ -3142,6 +3222,8 @@ if ( ! class_exists( 'um\core\Fields' ) ) { if ( $this->is_error( $key ) ) { $output .= $this->field_error( $this->show_error( $key ) ); + }else if ( $this->is_notice( $key ) ) { + $output .= $this->field_notice( $this->show_notice( $key ) ); } $output .= ''; @@ -3278,8 +3360,10 @@ if ( ! class_exists( 'um\core\Fields' ) ) { $output .= ''; - if ( $this->is_error( $form_key ) ) { - $output .= $this->field_error( $this->show_error( $form_key ) ); + if ( $this->is_error( $key ) ) { + $output .= $this->field_error( $this->show_error( $key ) ); + }else if ( $this->is_notice( $key ) ) { + $output .= $this->field_notice( $this->show_notice( $key ) ); } $output .= ''; @@ -3428,6 +3512,8 @@ if ( ! class_exists( 'um\core\Fields' ) ) { if ( $this->is_error( $key ) ) { $output .= $this->field_error( $this->show_error( $key ) ); + }else if ( $this->is_notice( $key ) ) { + $output .= $this->field_notice( $this->show_notice( $key ) ); } $output .= ''; diff --git a/includes/core/class-form.php b/includes/core/class-form.php index b7f60ca4..1ba5e98d 100644 --- a/includes/core/class-form.php +++ b/includes/core/class-form.php @@ -232,6 +232,40 @@ if ( ! class_exists( 'um\core\Form' ) ) { } } + /** + * Appends field notices + * @param string $key + * @param string $notice + */ + function add_notice( $key, $notice ) { + if ( ! isset( $this->notices[ $key ] ) ){ + /** + * UM hook + * + * @type filter + * @title um_submit_form_notice + * @description Change notice text on submit form + * @input_vars + * [{"var":"$notice","type":"string","desc":"notice String"}, + * {"var":"$key","type":"string","desc":"notice Key"}] + * @change_log + * ["Since: 2.0"] + * @usage + * + * @example + * + */ + $notice = apply_filters( 'um_submit_form_notice', $notice, $key ); + $this->notices[ $key ] = $notice; + } + } + /** * If a form has errors @@ -245,6 +279,18 @@ if ( ! class_exists( 'um\core\Form' ) ) { return false; } + /** + * If a form has notices/info + * @param string $key + * @return boolean + */ + function has_notice( $key ) { + if ( isset( $this->notices[ $key ] ) ) { + return true; + } + return false; + } + /** * Declare all fields