name = 'font-awesome';
$this->label = __( 'Font Awesome Icon', 'acf-font-awesome' );
$this->category = 'Content';
$this->settings = $settings;
$this->defaults = array(
'enqueue_fa' => 0,
'allow_null' => 0,
'show_preview' => 1,
'save_format' => 'element',
'default_value' => '',
'fa_live_preview' => '',
'choices' => array()
);
parent::__construct();
if ( apply_filters( 'ACFFA_always_enqueue_fa', false ) ) {
add_action( 'wp_enqueue_scripts', array( $this, 'frontend_enqueue_scripts' ) );
} else {
add_filter('acf/load_field', array( $this, 'maybe_enqueue_font_awesome' ) );
}
}
private function get_icons( $format = 'list' )
{
if ( ! $this->icons ) {
$this->icons = apply_filters( 'ACFFA_get_icons', array() );
}
return $this->icons[ $format ];
}
private function get_fa_url()
{
return apply_filters( 'ACFFA_get_fa_url', '' );
}
public function create_options( $field )
{
$field = array_merge( $this->defaults, $field );
$key = $field['name'];
?>
|
|
|
|
|
'select',
'name' => 'fields[' . $key . '][default_value]',
'value' => $field['default_value'],
'class' => 'chosen-fontawesome fontawesome-create',
'choices' => $field['choices']
));
?>
|
|
|
'radio',
'name' => 'fields['.$key.'][save_format]',
'value' => $field['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'),
),
'layout' => 'vertical',
));
?>
|
|
|
'radio',
'name' => 'fields['.$key.'][allow_null]',
'value' => $field['allow_null'],
'choices' => array(
1 => __( 'Yes', 'acf-font-awesome' ),
0 => __( 'No', 'acf-font-awesome' ),
),
'layout' => 'horizontal',
));
?>
|
|
|
'radio',
'name' => 'fields['.$key.'][show_preview]',
'value' => $field['show_preview'],
'choices' => array(
1 => __( 'Yes', 'acf-font-awesome' ),
0 => __( 'No', 'acf-font-awesome' ),
),
'layout' => 'horizontal',
));
?>
|
|
|
'radio',
'name' => 'fields['.$key.'][enqueue_fa]',
'value' => $field['enqueue_fa'],
'choices' => array(
1 => __( 'Yes', 'acf-font-awesome' ),
0 => __( 'No', 'acf-font-awesome' ),
),
'layout' => 'horizontal',
));
?>
|
enqueue_admin_scripts( array( 'acf-input' ) );
}
public function field_group_admin_enqueue_scripts()
{
$this->enqueue_admin_scripts( array( 'acf-field-group' ) );
}
private function enqueue_admin_scripts( $dependencies = array() )
{
$url = $this->settings['url'];
$version = $this->settings['version'];
if ( apply_filters( 'ACFFA_load_chosen', true ) ) {
wp_enqueue_script( 'chosen', "{$url}assets/inc/chosen/chosen.jquery.min.js", array('jquery'), '1.7.0' );
wp_enqueue_style( 'chosen', "{$url}assets/inc/chosen/chosen.min.css", '', '1.7.0' );
}
wp_register_script( 'acf-input-font-awesome', "{$url}assets/js/input-v4.js", $dependencies, $version );
wp_enqueue_script( 'acf-input-font-awesome' );
wp_localize_script( 'acf-input-font-awesome', 'ACFFA', array(
'chosen' => apply_filters( 'ACFFA_load_chosen', true ),
'nonce' => wp_create_nonce( 'ACFFA_nonce' ),
'is_rtl' => is_rtl(),
'no_results' => __( 'Cannot find icon', 'acf-font-awesome' ) . ' : ',
'major_version' => ACFFA_MAJOR_VERSION
));
wp_register_style( 'acf-input-font-awesome', "{$url}assets/css/input.css", $dependencies, $version );
wp_enqueue_style( 'acf-input-font-awesome' );
if ( apply_filters( 'ACFFA_admin_enqueue_fa', true ) ) {
wp_register_style( 'acf-input-font-awesome_library', $this->get_fa_url(), $dependencies );
wp_enqueue_style( 'acf-input-font-awesome_library' );
}
}
public 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;
}
public function frontend_enqueue_scripts()
{
wp_register_style( 'font-awesome', $this->get_fa_url() );
wp_enqueue_style('font-awesome');
}
public function format_value_for_api( $value, $post_id, $field )
{
if ( 'null' == $value ) {
return false;
}
if ( empty( $value ) ) {
return $value;
}
if ( ! $this->icons ) {
$this->get_icons();
}
if ( version_compare( ACFFA_MAJOR_VERSION, 5, '<' ) ) {
$icon = isset( $this->icons['details'][ $value ] ) ? $this->icons['details'][ $value ] : false;
} else {
$prefix = substr( $value, 0, 3 );
$icon = isset( $this->icons['details'][ $prefix ][ $value ] ) ? $this->icons['details'][ $prefix ][ $value ] : false;
}
if ( $icon ) {
switch ( $field['save_format'] ) {
case 'element':
if ( version_compare( ACFFA_MAJOR_VERSION, 5, '<' ) ) {
$value = '';
} else {
$value = '';
}
break;
case 'unicode':
$value = $icon['unicode'];
break;
case 'object':
$object_data = array(
'element' => '',
'class' => $value,
'hex' => $icon['hex'],
'unicode' => $icon['unicode']
);
if ( version_compare( ACFFA_MAJOR_VERSION, 5, '>=' ) ) {
$object_data['prefix'] = $prefix;
}
$value = ( object ) $object_data;
break;
}
}
return $value;
}
}
new acf_field_font_awesome( $this->settings );
endif;