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 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 $choices = $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)); // ACF input field display in backend interface $input_html = ''; // output HTML echo $this->viivue_icomoon_select_html($choices, $value, $input_html); } /** * HTML for ACF and VC */ function viivue_icomoon_select_html($icon_array, $value, $input_html, $vc_element = false): string{ $id = uniqid(); $class = $vc_element ? 'vc-element' : 'acf'; // find active icon $active_icon = array(); foreach($icon_array as $icon){ $return_value = $vc_element ? "icon_class" : "name"; $is_selected = $icon[$return_value] == $value; if($is_selected){ $active_icon = $icon; } } $active_icon_svg = viivue_array_key_exists('icon_svg', $active_icon); $active_icon_name = viivue_array_key_exists('name', $active_icon); // wrapper $html = ''; // end wrapper return $html; } /** * Get popup HTML * * @param $icons_array * @param $selected_value * @param $vc_element * * @return string */ function viivue_get_popup_html($icons_array, $selected_value, $vc_element): string{ $html = ''; $count_text = '' . count($icons_array) . ' icons'; // icons HTML $items = ''; foreach($icons_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] == $selected_value; $icomoon_value = viivue_array_key_exists($return_value, $icon); // attributes $attr = 'data-icomoon-value="' . $icomoon_value . '"'; $attr .= 'class="' . ($is_selected ? 'active' : '') . '"'; $attr .= 'title="' . $name . '"'; // HTML $items .= '
  • '; $items .= '' . $svg . ''; $items .= '
  • '; } // HTML $html .= '
    '; $html .= '
    '; $html .= '
    '; $html .= ''; $html .= '' . $count_text . ''; $html .= '
    '; $html .= '
    '; $html .= '
      '; $html .= $items; $html .= '
    '; $html .= '
    '; $html .= '
    '; $html .= '
    '; 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']; $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_svg .= '' . $icon_name . ''; foreach($icon_paths as $path){ $icon_svg .= ''; } $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); // return icon class if($display_type == 'icon_class'){ return viivue_array_key_exists('icon_class', $value); } // return svg if($display_type == 'svg'){ return viivue_array_key_exists('icon_svg', $value); } // return icon html return ''; } } // initialize new ViiVue_ACF_Field_Icomoon(); }