name = 'font-awesome'; $this->label = __('Font Awesome Icon'); $this->category = __("Content",'acf'); // Basic, Content, Choice, etc $this->defaults = array( 'enqueue_fa' => 0, 'allow_null' => 0, 'save_format' => 'element', 'default_value' => '', 'fa_live_preview' => '', 'choices' => $this->get_icons() ); $this->l10n = array(); $this->settings = array( 'path' => dirname(__FILE__), 'dir' => $this->helpers_get_dir( __FILE__ ), 'version' => '1.5' ); add_filter('acf/load_field', array( $this, 'maybe_enqueue_font_awesome' ) ); parent::__construct(); } function get_icons() { require_once ( dirname( __FILE__ ) . '/better-font-awesome-library/better-font-awesome-library.php' ); $args = array( 'version' => 'latest', 'minified' => true, 'remove_existing_fa' => false, 'load_styles' => false, 'load_admin_styles' => false, 'load_shortcode' => false, 'load_tinymce_plugin' => false ); $bfa = Better_Font_Awesome_Library::get_instance( $args ); $bfa_icons = $bfa->get_icons(); $bfa_prefix = $bfa->get_prefix() . '-'; $new_icons = array(); $this->stylesheet = $bfa->get_stylesheet_url(); $this->version = $bfa->get_version(); foreach ( $bfa_icons as $hex => $class ) { $unicode = '' . ltrim( $hex, '\\') . ';'; $new_icons[ $bfa_prefix . $class ] = $unicode . ' ' . $bfa_prefix . $class; } $new_icons = array_merge( array( 'null' => '- Select -' ), $new_icons ); return $new_icons; } /* * maybe_enqueue_font_awesome() * * If Enqueue FA is set to true, enqueue it in the footer. We cannot enqueue in the header because wp_head has already been called * */ function maybe_enqueue_font_awesome( $field ) { if( 'font-awesome' == $field['type'] && $field['enqueue_fa'] ) { add_action( 'wp_footer', array( $this, 'frontend_enqueue_scripts' ) ); } return $field; } /* * frontend_enqueue_scripts() * * This action is called in the wp_enqueue_scripts action on the front end. * * $info http://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts * @type action */ function frontend_enqueue_scripts() { wp_register_style('font-awesome', $this->stylesheet, array(), $this->version); wp_enqueue_style( array( 'font-awesome' ) ); } /* * render_field_settings() * * Create extra settings for your field. These are visible when editing a field * * @type action * @since 3.6 * @date 23/01/13 * * @param $field (array) the $field being edited * @return n/a */ function render_field_settings( $field ) { /* * acf_render_field_setting * * This function will create a setting for your field. Simply pass the $field parameter and an array of field settings. * The array of settings does not require a `value` or `prefix`; These settings are found from the $field array. * * More than one setting can be added by copy/paste the above code. * Please note that you must also have a matching $defaults value for the field name (font_size) */ acf_render_field_setting( $field, array( 'label' => __('Live Preview','acf-font-awesome'), 'instructions' => '', 'type' => 'message', 'name' => 'fa_live_preview', 'class' => 'live-preview' )); acf_render_field_setting( $field, array( 'label' => __('Default Icon','acf-font-awesome'), 'instructions' => '', 'type' => 'select', 'name' => 'default_value', 'class' => 'fontawesome', 'choices' => $field['choices'] )); acf_render_field_setting( $field, array( 'label' => __('Return Value','acf-font-awesome'), 'instructions' => __('Specify the returned value on front end','acf-font-awesome'), 'type' => 'radio', 'name' => 'save_format', 'choices' => array( 'element' => __('Icon Element','acf-font-awesome'), 'class' => __('Icon Class','acf-font-awesome'), 'unicode' => __('Icon Unicode','acf-font-awesome'), 'object' => __('Icon Object','acf-font-awesome'), ) )); acf_render_field_setting( $field, array( 'label' => __('Allow Null?','acf-font-awesome'), 'instructions' => '', 'type' => 'radio', 'name' => 'allow_null', 'choices' => array( 1 => __('Yes','acf-font-awesome'), 0 => __('No','acf-font-awesome') ) )); acf_render_field_setting( $field, array( 'label' => __('Enqueue FontAwesome?','acf-font-awesome'), 'instructions' => __('Set to \'Yes\' to enqueue FA in the footer on any pages using this field.','acf-font-awesome'), 'type' => 'radio', 'name' => 'enqueue_fa', 'choices' => array( 1 => __('Yes','acf-font-awesome'), 0 => __('No','acf-font-awesome') ) )); } /* * render_field() * * Create the HTML interface for your field * * @param $field (array) the $field being rendered * * @type action * @since 3.6 * @date 23/01/13 * * @param $field (array) the $field being edited * @return n/a */ function render_field( $field ) { if( 'object' == $field['save_format'] ) $field['value'] = array( $field['value']->class ); // value must be array if( !is_array($field['value']) ) { // perhaps this is a default value with new lines in it? if( strpos($field['value'], "\n") !== false ) { // found multiple lines, explode it $field['value'] = explode("\n", $field['value']); } else { $field['value'] = array( $field['value'] ); } } // trim value $field['value'] = array_map('trim', $field['value']); // html echo '