Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thêm field type icon vào elementor #34

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 48 additions & 5 deletions src/GroupField.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,13 @@ 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( $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' ) );
$value_field = $this->display_icon( $value_field, $group_settings['fields'][ $key ] );
}
}

if ( is_array( $value_field ) ) {
Expand Down Expand Up @@ -527,7 +533,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 ) {
Expand Down Expand Up @@ -622,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 );
Expand Down Expand Up @@ -720,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();

Expand Down Expand Up @@ -758,6 +765,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;
Expand All @@ -770,4 +780,37 @@ public function display_field( $data, $field = [], $return = false ) {
}
}

private function display_icon( $data, $field ) {
$icons = array_column( $field['options'], 'label', 'value' );

if ( ! $icons[ $data ] ) {
return '';
}

// Case using svg
$str_svg = strstr( $icons[ $data ], '</svg>', true );
if ( $str_svg !== false ) {
return $str_svg . '</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 '<span class="' . $data . '"></span>';
}

// 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 '<span class="' . $data . '"></span>';
}

}
}
2 changes: 2 additions & 0 deletions src/Templates/display_field-icon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
echo $this->display_icon( $data, $field );