From bb5f203b29cfe274de4a4efa629806033d78ce11 Mon Sep 17 00:00:00 2001 From: tuankiet2605 Date: Fri, 8 Dec 2023 02:56:08 +0000 Subject: [PATCH 1/4] =?UTF-8?q?Th=C3=AAm=20field=20type=20icon=20v=C3=A0o?= =?UTF-8?q?=20elementor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/GroupField.php | 16 +++++++++++++++- src/Templates/display_field-icon.php | 8 ++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 src/Templates/display_field-icon.php diff --git a/src/GroupField.php b/src/GroupField.php index dd17708..7678070 100644 --- a/src/GroupField.php +++ b/src/GroupField.php @@ -279,7 +279,18 @@ public function get_value_dynamic_tag( $post_type, $field_id, $template_id = nul $value_field = $this->get_value_nested_group( $value_field, $sub_fields, true ); if ( false !== is_int( key( $value_field ) ) ) { - $value_field = array_shift( $value_field ); + $value_field = array_shift( $value_field ); + $group_settings = rwmb_get_field_settings( 'group_icon' ); + if ( $group_settings['type'] === 'group' && array_column( $group_settings['fields'], 'type', 'id' )[ $sub_fields[1] ] === 'icon' ) { + $key = array_search( $sub_fields[1], array_column( $group_settings['fields'], 'id' ) ); + $icons = array_column( \RWMB_Icon_Field::get_icons( $group_settings['fields'][ $key ] ), 'svg', 'value' ); + if ( ! empty( $icons[ $value_field ] ) ) { + $value_field = $icons[ $value_field ]; + } else { + \RWMB_Icon_Field::enqueue_icon_font_style( $group_settings['fields'][ $key ] ); + $value_field = ''; + } + } } if ( is_array( $value_field ) ) { @@ -758,6 +769,9 @@ public function display_field( $data, $field = [], $return = false ) { case 'user': $file_type = 'user'; break; + case 'icon': + $file_type = 'icon'; + break; default: $file_type = 'text'; break; diff --git a/src/Templates/display_field-icon.php b/src/Templates/display_field-icon.php new file mode 100644 index 0000000..d74a6de --- /dev/null +++ b/src/Templates/display_field-icon.php @@ -0,0 +1,8 @@ +'; +} \ No newline at end of file From 966537b8236cc2093aed965f1565601199ff25bc Mon Sep 17 00:00:00 2001 From: tuankiet2605 Date: Tue, 12 Dec 2023 03:24:23 +0000 Subject: [PATCH 2/4] Fix icon field --- src/GroupField.php | 26 +++++++++++++++++++------- src/Templates/display_field-icon.php | 27 +++++++++++++++++++++------ 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/src/GroupField.php b/src/GroupField.php index 7678070..484721f 100644 --- a/src/GroupField.php +++ b/src/GroupField.php @@ -280,14 +280,26 @@ public function get_value_dynamic_tag( $post_type, $field_id, $template_id = nul if ( false !== is_int( key( $value_field ) ) ) { $value_field = array_shift( $value_field ); - $group_settings = rwmb_get_field_settings( 'group_icon' ); + $group_settings = rwmb_get_field_settings( $sub_fields[0] ); if ( $group_settings['type'] === 'group' && array_column( $group_settings['fields'], 'type', 'id' )[ $sub_fields[1] ] === 'icon' ) { - $key = array_search( $sub_fields[1], array_column( $group_settings['fields'], 'id' ) ); - $icons = array_column( \RWMB_Icon_Field::get_icons( $group_settings['fields'][ $key ] ), 'svg', 'value' ); - if ( ! empty( $icons[ $value_field ] ) ) { - $value_field = $icons[ $value_field ]; + $key = array_search( $sub_fields[1], array_column( $group_settings['fields'], 'id' ) ); + $icons = array_column( $group_settings['fields'][ $key ]['options'], 'label', 'value' ); + $pos = strpos( $icons[ $value_field ], '' ); + if ( ! empty( $icons[ $value_field ] ) && $pos !== false ) { + $value_field = substr( $icons[ $value_field ], 0, $pos + strlen( '' ) ); + } elseif ( is_callable( $group_settings['fields'][ $key ]['icon_css'] ) && ! is_admin() ) { + $group_settings['fields'][ $key ]['icon_css'](); + $value_field = ''; } else { - \RWMB_Icon_Field::enqueue_icon_font_style( $group_settings['fields'][ $key ] ); + if ( ! is_admin() ) { + $handle = md5( $group_settings['fields'][ $key ]['icon_css'] ); + wp_enqueue_style( $handle, $group_settings['fields'][ $key ]['icon_css'], [], RWMB_VER ); + } else { + add_action( 'elementor/preview/enqueue_styles', function() use ( $group_settings, $key ) { + $handle = md5( $group_settings['fields'][ $key ]['icon_css'] ); + wp_enqueue_style( $handle, $group_settings['fields'][ $key ]['icon_css'], [], RWMB_VER ); + } ); + } $value_field = ''; } } @@ -538,7 +550,7 @@ public function render_nested_group( $data_groups, $data_column ) { $clone = false; if ( isset( $data_column['id'] ) ) { $data_groups = [ $data_column['id'] => $data_groups ]; - $clone = true; + $clone = true; } foreach ( $data_groups as $data_group ) { diff --git a/src/Templates/display_field-icon.php b/src/Templates/display_field-icon.php index d74a6de..ba0bddb 100644 --- a/src/Templates/display_field-icon.php +++ b/src/Templates/display_field-icon.php @@ -1,8 +1,23 @@ '; +$icons = array_column( $field['options'], 'label', 'value' ); +if ( $icons[ $data ] ) { + $pos = strpos( $icons[ $data ], '' ); + if ( $pos !== false ) { + $icons[ $data ] = substr( $icons[ $data ], 0, $pos + strlen( '' ) ); + echo $icons[ $data ]; + } elseif ( $field['icon_css'] && is_string( $field['icon_css'] ) ) { + if ( ! is_admin() ) { + $handle = md5( $field['icon_css'] ); + wp_enqueue_style( $handle, $field['icon_css'], [], RWMB_VER ); + } else { + add_action( 'elementor/preview/enqueue_styles', function() use ( $field ) { + $handle = md5( $field['icon_css'] ); + wp_enqueue_style( $handle, $field['icon_css'], [], RWMB_VER ); + } ); + } + } elseif ( is_callable( $field['icon_css'] ) && ! is_admin() ) { + $field['icon_css'](); + } else { + echo ''; + } } \ No newline at end of file From 6fc9b01efafb5fe38f447b8f00f9f7f64e87ae87 Mon Sep 17 00:00:00 2001 From: tuankiet2605 Date: Tue, 12 Dec 2023 09:08:18 +0000 Subject: [PATCH 3/4] Fix display icon with enqueue script for fontend --- src/GroupField.php | 63 ++++++++++++++++++---------- src/Templates/display_field-icon.php | 23 +--------- 2 files changed, 41 insertions(+), 45 deletions(-) diff --git a/src/GroupField.php b/src/GroupField.php index 484721f..690dee1 100644 --- a/src/GroupField.php +++ b/src/GroupField.php @@ -281,27 +281,10 @@ public function get_value_dynamic_tag( $post_type, $field_id, $template_id = nul if ( false !== is_int( key( $value_field ) ) ) { $value_field = array_shift( $value_field ); $group_settings = rwmb_get_field_settings( $sub_fields[0] ); + // Case group contain icon if ( $group_settings['type'] === 'group' && array_column( $group_settings['fields'], 'type', 'id' )[ $sub_fields[1] ] === 'icon' ) { - $key = array_search( $sub_fields[1], array_column( $group_settings['fields'], 'id' ) ); - $icons = array_column( $group_settings['fields'][ $key ]['options'], 'label', 'value' ); - $pos = strpos( $icons[ $value_field ], '' ); - if ( ! empty( $icons[ $value_field ] ) && $pos !== false ) { - $value_field = substr( $icons[ $value_field ], 0, $pos + strlen( '' ) ); - } elseif ( is_callable( $group_settings['fields'][ $key ]['icon_css'] ) && ! is_admin() ) { - $group_settings['fields'][ $key ]['icon_css'](); - $value_field = ''; - } else { - if ( ! is_admin() ) { - $handle = md5( $group_settings['fields'][ $key ]['icon_css'] ); - wp_enqueue_style( $handle, $group_settings['fields'][ $key ]['icon_css'], [], RWMB_VER ); - } else { - add_action( 'elementor/preview/enqueue_styles', function() use ( $group_settings, $key ) { - $handle = md5( $group_settings['fields'][ $key ]['icon_css'] ); - wp_enqueue_style( $handle, $group_settings['fields'][ $key ]['icon_css'], [], RWMB_VER ); - } ); - } - $value_field = ''; - } + $key = array_search( $sub_fields[1], array_column( $group_settings['fields'], 'id' ) ); + $value_field = self::display_icon( $value_field, $group_settings['fields'][ $key ] ); } } @@ -645,11 +628,11 @@ public function display_data_template( $template_id, $data_groups, $data_column, 'width="' . $img->getAttribute( 'width' ) . '"', 'height' => 'height="' . $img->getAttribute( 'height' ) . '"', 'class="' . $img->getAttribute( 'class' ) . '"', - ], [ + ], [ $search_data['width'], $search_data['height'], $search_data['class'], - ], $value ); + ], $value ); } $content = str_replace( $search_data['html'], $value, $content ); @@ -743,12 +726,13 @@ public function display_data_widget( $data_groups, $data_column, $options = [ echo $options['loop_header']; foreach ( $data_group as $key => $value ) { + $data_sub_column = []; if ( is_array( $value ) && ! empty( $value ) ) { $data_sub_column = isset( $data_column[ $key ]['fields'] ) ? array_combine( array_column( $data_column[ $key ]['fields'], 'id' ), $data_column[ $key ]['fields'] ) : $data_column[ $key ]; } ob_start(); - isset( $data_sub_column ) ? $this->render_nested_group( $value, $data_sub_column ) : $this->display_field( $value, $data_column[ $key ] ); + count( $data_sub_column ) > 0 ? $this->render_nested_group( $value, $data_sub_column ) : $this->display_field( $value, $data_column[ $key ] ); $content = ob_get_contents(); ob_end_clean(); @@ -796,4 +780,37 @@ public function display_field( $data, $field = [], $return = false ) { } } + public static function display_icon( $data, $field ) { + $icons = array_column( $field['options'], 'label', 'value' ); + + if ( ! $icons[ $data ] ) { + return ''; + } + + // Case using svg + $str_svg = strstr( $icons[ $data ], '', true ); + if ( $str_svg !== false ) { + return $str_svg . ''; + } + + // Case using font with icon_css as string + if ( $field['icon_css'] && is_string( $field['icon_css'] ) ) { + // Frontend + if ( ! is_admin() ) { + $handle = md5( $field['icon_css'] ); + wp_enqueue_style( $handle, $field['icon_css'], [], RWMB_VER ); + } + return ''; + } + + // Case using font with icon_css as function + if ( $field['icon_css'] && is_callable( $field['icon_css'] ) ) { + // Frontend + if ( ! is_admin() ) { + $field['icon_css'](); + } + return ''; + } + + } } diff --git a/src/Templates/display_field-icon.php b/src/Templates/display_field-icon.php index ba0bddb..4391227 100644 --- a/src/Templates/display_field-icon.php +++ b/src/Templates/display_field-icon.php @@ -1,23 +1,2 @@ ' ); - if ( $pos !== false ) { - $icons[ $data ] = substr( $icons[ $data ], 0, $pos + strlen( '' ) ); - echo $icons[ $data ]; - } elseif ( $field['icon_css'] && is_string( $field['icon_css'] ) ) { - if ( ! is_admin() ) { - $handle = md5( $field['icon_css'] ); - wp_enqueue_style( $handle, $field['icon_css'], [], RWMB_VER ); - } else { - add_action( 'elementor/preview/enqueue_styles', function() use ( $field ) { - $handle = md5( $field['icon_css'] ); - wp_enqueue_style( $handle, $field['icon_css'], [], RWMB_VER ); - } ); - } - } elseif ( is_callable( $field['icon_css'] ) && ! is_admin() ) { - $field['icon_css'](); - } else { - echo ''; - } -} \ No newline at end of file +echo MBEI\GroupField::display_icon( $data, $field ); \ No newline at end of file From bf78098102f0f981db64a38cb8fb1fd49e9459d7 Mon Sep 17 00:00:00 2001 From: tuankiet2605 Date: Tue, 12 Dec 2023 09:28:46 +0000 Subject: [PATCH 4/4] Fix --- src/GroupField.php | 4 ++-- src/Templates/display_field-icon.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/GroupField.php b/src/GroupField.php index 690dee1..ee6ab3c 100644 --- a/src/GroupField.php +++ b/src/GroupField.php @@ -284,7 +284,7 @@ public function get_value_dynamic_tag( $post_type, $field_id, $template_id = nul // Case group contain icon if ( $group_settings['type'] === 'group' && array_column( $group_settings['fields'], 'type', 'id' )[ $sub_fields[1] ] === 'icon' ) { $key = array_search( $sub_fields[1], array_column( $group_settings['fields'], 'id' ) ); - $value_field = self::display_icon( $value_field, $group_settings['fields'][ $key ] ); + $value_field = $this->display_icon( $value_field, $group_settings['fields'][ $key ] ); } } @@ -780,7 +780,7 @@ public function display_field( $data, $field = [], $return = false ) { } } - public static function display_icon( $data, $field ) { + private function display_icon( $data, $field ) { $icons = array_column( $field['options'], 'label', 'value' ); if ( ! $icons[ $data ] ) { diff --git a/src/Templates/display_field-icon.php b/src/Templates/display_field-icon.php index 4391227..6502916 100644 --- a/src/Templates/display_field-icon.php +++ b/src/Templates/display_field-icon.php @@ -1,2 +1,2 @@ display_icon( $data, $field );