*/
class ACFFA_Admin
{
private $version;
public function init()
{
$this->version = 'v' . ACFFA_MAJOR_VERSION;
add_action( 'admin_notices', array( $this, 'show_upgrade_notice' ) );
add_action( 'admin_notices', array( $this, 'maybe_notify_cdn_error' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_filter( 'plugin_action_links', array( $this, 'add_settings_link' ), 10, 2 );
add_action( 'admin_menu', array( $this, 'add_settings_page' ), 100 );
add_action( 'admin_init', array( $this, 'register_settings' ) );
add_filter( 'pre_update_option_acffa_settings', array( $this, 'intercept_icon_set_save' ), 10, 2 );
add_filter( 'pre_update_option_acffa_settings', array( $this, 'maybe_refresh_icons' ), 20, 2 );
add_action( 'wp_ajax_ACFFA_delete_icon_set', array( $this, 'ajax_remove_icon_set' ) );
}
public function show_upgrade_notice()
{
$acffa_settings = get_option( 'acffa_settings' );
if ( ! isset( $acffa_settings['show_upgrade_notice'] ) ) {
return;
}
?>
FontAwesome Settings page to change FontAwesome icon version, or to create custom icon sets.', 'acf-font-awesome' ), admin_url( '/edit.php?post_type=acf-field-group&page=fontawesome-settings' ) ); ?>
__( 'Search List', 'acf-font-awesome' ),
'confirm_delete' => __( 'Are you sure you want to delete this icon set?', 'acf-font-awesome' ),
'delete_fail' => __( 'There was an error while trying to delete the icon set, please refresh the page and try again.', 'acf-font-awesome' )
));
}
public function add_settings_link( $links, $file )
{
if ( $file != ACFFA_BASENAME ) {
return $links;
}
array_unshift( $links, '' . esc_html__( 'Settings', 'acf-font-awesome' ) . '' );
return $links;
}
public function add_settings_page()
{
add_submenu_page(
'edit.php?post_type=acf-field-group',
'FontAwesome Settings',
'FontAwesome Settings',
'manage_options',
'fontawesome-settings',
array( $this, 'fontawesome_settings' )
);
}
public function fontawesome_settings()
{
$errors = get_settings_errors( 'acffa_messages' );
if ( isset( $_GET['settings-updated'] ) && ! $errors ) {
add_settings_error( 'acffa_messages', 'acffa_message', __( 'Settings Saved', 'acf-font-awesome' ), 'updated' );
}
settings_errors( 'acffa_messages' );
?>
array( $this, 'sanitize_new_icon_set' )
)
);
add_settings_section(
'acffa_section_developers',
__( 'Major Version', 'acf-font-awesome' ),
array( $this, 'acffa_section_developers_cb' ),
'acffa'
);
add_settings_field(
'acffa_major_version',
__( 'Version', 'acf-font-awesome' ),
array( $this, 'acffa_major_version_cb' ),
'acffa',
'acffa_section_developers',
array(
'label_for' => 'acffa_major_version',
'class' => 'acffa_row'
)
);
add_settings_field(
'acffa_pro_cdn',
__( 'Enable Pro Icons?', 'acf-font-awesome' ),
array( $this, 'acffa_pro_cdn_cb' ),
'acffa',
'acffa_section_developers',
array(
'label_for' => 'acffa_pro_cdn',
'class' => 'acffa_row pro_icons'
)
);
add_settings_field(
'acffa_plugin_version',
'Plugin Version',
array( $this, 'acffa_plugin_version_cb' ),
'acffa',
'acffa_section_developers',
array(
'label_for' => 'acffa_plugin_version',
'class' => 'acffa_row hidden'
)
);
add_settings_section(
'acffa_section_icon_set_builder',
__( 'Icon Set Builder', 'acf-font-awesome' ),
array( $this, 'acffa_section_icon_set_builder_cb' ),
'acffa'
);
add_settings_field(
'acffa_new_icon_set_label',
__( 'New Icon Set Label', 'acf-font-awesome' ),
array( $this, 'acffa_new_icon_set_label_cb' ),
'acffa',
'acffa_section_icon_set_builder',
array(
'label_for' => 'acffa_new_icon_set_label',
'class' => 'acffa_row custom-icon-set'
)
);
add_settings_field(
'acffa_new_icon_set',
__( 'New Icon Set', 'acf-font-awesome' ),
array( $this, 'acffa_new_icon_set_cb' ),
'acffa',
'acffa_section_icon_set_builder',
array(
'label_for' => 'acffa_new_icon_set',
'class' => 'acffa_row custom-icon-set'
)
);
add_settings_field(
'acffa_existing_icon_sets',
__( 'Existing Icon Sets', 'acf-font-awesome' ),
array( $this, 'acffa_existing_icon_sets_cb' ),
'acffa',
'acffa_section_icon_set_builder',
array(
'label_for' => 'acffa_existing_icon_sets',
'class' => 'acffa_row custom-icon-set'
)
);
}
public function sanitize_new_icon_set( $data )
{
if ( isset( $data['acffa_new_icon_set_label'] ) || ! empty( $data['acffa_new_icon_set_label'] ) ) {
$data['acffa_new_icon_set_label'] = sanitize_text_field( $data['acffa_new_icon_set_label'] );
} else {
$data['acffa_new_icon_set_label'] = false;
}
if ( isset( $data['acffa_new_icon_set'] ) || ! empty( $data['acffa_new_icon_set'] ) ) {
$data['acffa_new_icon_set'] = array_map(
'sanitize_text_field',
wp_unslash( $data['acffa_new_icon_set'] )
);
} else {
$data['acffa_new_icon_set'] = false;
}
if ( $data['acffa_new_icon_set_label'] && ! $data['acffa_new_icon_set'] ) {
add_settings_error( 'acffa_messages', 'missing_label', __( 'Please select at least one icon when adding a new custom icon set.', 'acf-font-awesome' ), 'error' );
} else if ( $data['acffa_new_icon_set'] && ! $data['acffa_new_icon_set_label'] ) {
add_settings_error( 'acffa_messages', 'missing_icons', __( 'Label is required when adding a new custom icon set.', 'acf-font-awesome' ), 'error' );
}
return $data;
}
public function acffa_section_developers_cb( $args )
{
?>
/>
version ] ) && ! empty( $custom_icon_sets_list[ $this->version ] ) ) {
?>
version ] as $icon_set_name => $icon_set_label ) {
$icon_set = get_option( $icon_set_name );
if ( ! $icon_set ) {
$this->remove_icon_set( $custom_icon_sets_list, $icon_set_name, true );
}
?>
-
( | | )
=' ) ) {
foreach ( $icon_set as $prefix => $icons ) {
?>
-
$label ) {
echo '- ' . $label . '
';
}
?>
$label ) {
?>
-
' . $label . '
';
?>
save_new_icon_set( $label, $icons );
}
return $new_value;
}
public function maybe_refresh_icons( $new_value, $old_value )
{
unset( $new_value['acffa_new_icon_set_label'] );
unset( $new_value['acffa_new_icon_set'] );
$refresh_icons = false;
if ( $new_value['acffa_major_version'] !== $old_value['acffa_major_version'] ) {
$refresh_icons = true;
}
if ( ! $refresh_icons ) {
if ( ( isset( $new_value['acffa_pro_cdn'] ) && ! isset( $old_value['acffa_pro_cdn'] ) )
|| ( ! isset( $new_value['acffa_pro_cdn'] ) && isset( $old_value['acffa_pro_cdn'] ) )
) {
$refresh_icons = true;
}
}
if ( $refresh_icons ) {
do_action( 'ACFFA_refresh_latest_icons' );
}
return $new_value;
}
private function save_new_icon_set( $label, $icons )
{
$new_icon_set = array();
$fa_icons = apply_filters( 'ACFFA_get_icons', array() );
if ( version_compare( ACFFA_MAJOR_VERSION, 5, '>=' ) ) {
foreach( $icons as $icon ) {
$prefix = substr( $icon, 0, 3 );
if ( isset( $fa_icons['list'][ $prefix ][ $icon ] ) ) {
if ( ! isset( $new_icon_set[ $prefix ] ) ) {
$new_icon_set[ $prefix ] = array();
}
$new_icon_set[ $prefix ][ $icon ] = $fa_icons['list'][ $prefix ][ $icon ];
}
}
} else {
foreach( $icons as $icon ) {
if ( isset( $fa_icons['list'][ $icon ] ) ) {
$new_icon_set[ $icon ] = $fa_icons['list'][ $icon ];
}
}
}
if ( ! empty( $new_icon_set ) ) {
$option_name = 'ACFFA_custom_icon_list_' . $this->version . '_' . sanitize_html_class( $label );
update_option( $option_name, $new_icon_set, false );
$this->update_custom_icon_sets_list( $option_name, $label );
}
}
private function update_custom_icon_sets_list( $option_name, $label )
{
$icon_sets_list = get_option( 'ACFFA_custom_icon_sets_list' );
if ( ! $icon_sets_list ) {
$icon_sets_list = array();
}
if ( ! isset( $icon_sets_list[ $this->version ] ) ) {
$icon_sets_list[ $this->version ] = array();
}
if ( ! isset( $icon_sets_list[ $this->version ][ 'ACFFA_custom_icon_list_' . $option_name ] ) ) {
$icon_sets_list[ $this->version ][ $option_name ] = $label;
}
update_option( 'ACFFA_custom_icon_sets_list', $icon_sets_list, false );
}
private function remove_icon_set( $custom_icon_sets_list = false, $icon_set_name, $list_only = false )
{
if ( ! $custom_icon_sets_list ) {
$custom_icon_sets_list = get_option( 'ACFFA_custom_icon_sets_list' );
}
if ( ! isset( $custom_icon_sets_list[ $this->version ][ $icon_set_name ] ) ) {
return;
}
unset( $custom_icon_sets_list[ $this->version ][ $icon_set_name ] );
update_option( 'ACFFA_custom_icon_sets_list', $custom_icon_sets_list );
if ( ! $list_only ) {
delete_option( $icon_set_name );
}
}
public function ajax_remove_icon_set()
{
$valid_nonce = check_ajax_referer( 'acffa_delete_set_' . $_POST['icon_set_name'], 'nonce', false );
if ( ! $valid_nonce ) {
wp_die( 'fail' );
}
$this->remove_icon_set( false, $_POST['icon_set_name'] );
wp_die( 'success' );
}
}
add_action( 'acf/init', array( new ACFFA_Admin, 'init' ), 10 );