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',
);
$this->prefix = 'icon-';
parent::__construct();
}
/**
* Create settings for this fields in ACF
*/
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 type
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 (HTML)',
'icon_class' => 'Icon class',
'svg' => 'SVG element'
)
));
}
/**
* Render HTML interface for ACF
*/
function render_field($field){
// list of icons
$icon_array = $this->viivue_get_icomoon_json(viivue_array_key_exists('selection_json_path', $field));
// selected value
$value = esc_attr(viivue_array_key_exists('value', $field));
$name = esc_attr(viivue_array_key_exists('name', $field));
$input_html = '';
// output HTML
echo $this->viivue_icomoon_select_html($icon_array, $value, $input_html);
}
/**
* HTML for ACF and VC
*/
function viivue_icomoon_select_html($icon_array, $value, $input_html, $vc_element = false): string{
$class = $vc_element ? 'vc-element' : 'acf';
$html = '';
// value must begin with prefix (support legacy version)
if(substr($value, 0, strlen($this->prefix)) !== $this->prefix){
$value = $this->prefix . $value;
}
// wrapper
$html .= '
';
// json data
$html .= "
";
// hidden input
$html .= '
';
$html .= $input_html;
$html .= '
';
// custom field
$html .= '
';
$html .= '
';
$html .= '
';
$html .= '';
$html .= '{{selected.icon_class}}';
$html .= 'Click to select icon';
$html .= '
';
$html .= '
';
$html .= '
';
$html .= '
'; // end custom field
// popup
$html .= '
';
$html .= '
'; // end wrapper
return $html;
}
/**
* Get array of icons from json
*/
function viivue_get_icomoon_json($json_path = ''): array{
$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'];
$this->prefix = $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)
$attr = 'width="' . $icon_size . '"';
$attr .= 'height="' . $icon_size . '"';
$attr .= 'viewBox="0 0 1024 1024"';
$attr .= 'preserveAspectRatio="xMidYMid meet"';
$icon_svg = '';
$icon_array[] = array(
'name' => $icon_name,
'icon_class' => $prefix . $icon_name,
'svg' => $icon_svg,
);
}
}
}
}
return $icon_array;
}
/**
* Add assets, inherit from acf_field class
*/
function input_admin_enqueue_scripts(){
// easy popup
wp_register_style('viivue-acf-field-icomoon-easy-popup', ACFICOMOON_ASSETS_URL . "css/easy-popup.min.css", false, '0.0.2');
wp_register_script('viivue-acf-field-icomoon-easy-popup', ACFICOMOON_ASSETS_URL . "js/easy-popup.min.js", false, '0.0.2');
// vue
wp_register_script('viivue-acf-field-icomoon-helper', ACFICOMOON_ASSETS_URL . "js/helper.js", false, ACFICOMOON_VERSION);
wp_register_script('viivue-acf-field-icomoon-vue', ACFICOMOON_ASSETS_URL . "js/vue.global.min.js", false, '3.2.31');
wp_register_script('viivue-acf-field-icomoon-vue-dom', ACFICOMOON_ASSETS_URL . "js/class-acf-icomoon-dom.js", false, ACFICOMOON_VERSION);
wp_register_script('viivue-acf-field-icomoon-vue-app', ACFICOMOON_ASSETS_URL . "js/acf-icomoon-app.js", array(
'viivue-acf-field-icomoon-helper',
'viivue-acf-field-icomoon-easy-popup',
'viivue-acf-field-icomoon-vue',
'viivue-acf-field-icomoon-vue-dom'
), ACFICOMOON_VERSION);
wp_enqueue_script('viivue-acf-field-icomoon-vue-app');
// CSS
wp_register_style('viivue-acf-field-icomoon', ACFICOMOON_ASSETS_URL . "css/acf-icomoon.css", array(
'acf-input',
'viivue-acf-field-icomoon-easy-popup'
), ACFICOMOON_VERSION);
wp_enqueue_style('viivue-acf-field-icomoon');
}
/**
* 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);
$icon = array();
foreach($choices as $object){
if(viivue_array_key_exists('icon_class', $object) === $value){
$icon = $object;
break;
}
}
// return icon class
if($display_type == 'icon_class'){
return viivue_array_key_exists('icon_class', $icon);
}
// return svg
if($display_type == 'svg'){
return viivue_array_key_exists('svg', $icon);
}
// return icon html
return '';
}
}
// initialize
new ViiVue_ACF_Field_Icomoon();
}