name = 'viivue_acf_icomoon';
$this->label = __('Icomoon', 'acf-icomoon');
$this->category = 'content';
$this->defaults = array(
'selection_json_path' => ACFICOMOON_STYLESHEET_DIR . '/assets/fonts/selection.json',
);
parent::__construct();
}
/**
* Create extra settings
*/
function render_field_settings($field){
// Admin field: Selection.json
acf_render_field_setting($field, array(
'label' => __('Selection.json', 'acf-icomoon'),
'instructions' => __('The path to the selection.json file generated by IcoMoon. Default value will be assets/fonts/selection.json in the current theme folder.', 'acf-icomoon'),
'type' => 'text',
'name' => 'selection_json_path'
));
// Admin field: Return value
acf_render_field_setting($field, array(
'label' => __('Return value', 'acf-icomoon'),
'instructions' => __('Specify the returned value on front end', 'acf-icomoon'),
'type' => 'radio',
'name' => 'display_type',
'choices' => array(
'icon' => 'Icon element',
'icon_class' => 'Icon class',
'svg' => 'SVG element'
)
));
}
/*
* Create the HTML interface
*/
function render_field($field){
$choices = $this->viivue_get_icomoon_json(viivue_array_key_exists('selection_json_path', $field));
$value = esc_attr(viivue_array_key_exists('value', $field)); // selected value
$name = esc_attr(viivue_array_key_exists('name', $field));
$input_html = '';
echo $this->viivue_icomoon_select_html($choices, $value, $input_html);
}
/**
* Icomoon select
*/
function viivue_icomoon_select_html($icon_array, $value, $input_html, $vc_element = false){
$count_text = count($icon_array) . ' icons';
$id = uniqid();
$active_icon = array();
// Icon array
$items = '';
foreach($icon_array as $icon){
$return_value = $vc_element ? "icon_class" : "name";
$svg = viivue_array_key_exists('icon_svg', $icon);
$name = viivue_array_key_exists('name', $icon);
$is_selected = $icon[$return_value] == $value;
$class = '';
if($is_selected){
$class = "active";
$active_icon = $icon;
}
$icomoon_value = viivue_array_key_exists($return_value, $icon);
$items .= '
' . $svg . '';
}
$active_icon_svg = viivue_array_key_exists('icon_svg', $active_icon);
$active_icon_name = viivue_array_key_exists('name', $active_icon);
// start field html
$html = '';
return $html;
}
/**
* Get icomoon array
*/
function viivue_get_icomoon_json($json_path = ''){
$icon_size = 32;
$icon_array = array();
if(empty($json_path) || !file_exists($json_path)){
$json_path = ACFICOMOON_STYLESHEET_DIR . '/assets/fonts/selection.json';
}
if($json_path && file_exists($json_path)){
$icomoon_json = json_decode(file_get_contents($json_path), true);
if($icomoon_json){
$prefix = $icomoon_json['preferences']['fontPref']['prefix'];
$icons = viivue_array_key_exists('icons', $icomoon_json);
if($icons){
foreach($icons as $icon){
$icon_obj = viivue_array_key_exists('icon', $icon);
$icon_paths = viivue_array_key_exists('paths', $icon_obj);
$icon_props = viivue_array_key_exists('properties', $icon);
$icon_name = viivue_array_key_exists('name', $icon_props);
// get svg (Format the SVG path)
$icon_svg = '';
$icon_array[$icon_name] = array(
'name' => $icon_name,
'icon_class' => $prefix . $icon_name,
'icon_svg' => $icon_svg,
);
}
}
}
}
return $icon_array;
}
/*
* Add assets, inherit from acf_field class
*/
function input_admin_enqueue_scripts(){
wp_enqueue_style('viivue-acf-field-icomoon', ACFICOMOON_ASSETS_URL . "css/acf-icomoon.css", array('acf-input'), ACFICOMOON_VERSION);
wp_enqueue_script('viivue-acf-field-icomoon', ACFICOMOON_ASSETS_URL . "js/acf-icomoon.js", array('acf-input'), ACFICOMOON_VERSION);
}
/*
* This filter is applied to the $value after it is loaded from the db before it is returned to the template
*/
function format_value($value, $post_id, $field){
if(empty($value)){
return $value;
}
$path = $field['selection_json_path'];
$choices = $this->viivue_get_icomoon_json($path);
$display_type = viivue_array_key_exists('display_type', $field);
if(!array_key_exists($value, $choices)){
return '';
}
$value = viivue_array_key_exists($value, $choices);
if($display_type == 'icon_class'){
return viivue_array_key_exists('icon_class', $value);
}
if($display_type == 'svg'){
return viivue_array_key_exists('icon_svg', $value);
}
return '';
}
}
// initialize
new ViiVue_ACF_Field_Icomoon();
}