name = 'ionicon';
/*
* label (string) Multiple words, can include spaces, visible when selecting a field type
*/
$this->label = __('Ionicon', 'acf-ionicon');
/*
* category (string) basic | content | choice | relational | jquery | layout | CUSTOM GROUP NAME
*/
$this->category = 'content';
/*
* defaults (array) Array of default settings which are merged into the field object. These are used later in settings
*/
$this->defaults = array(
'default_value' => '',
'default_label' => '',
'show_preview' => 1,
'allow_null' => 0,
);
/*
* settings (array) Store plugin settings (url, path, version) as a reference for later use with assets
*/
$this->settings = $settings;
// do not delete!
parent::__construct();
add_action('wp_ajax_acf/fields/ionicon/query', array($this, 'select2_ajax_request'));
add_filter('acf_field_ionicon_get_icons', array($this, 'get_icons'), 5, 1);
}
/*
* 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' => __('Default Icon', 'acf-ionicon'),
'instructions' => '',
'type' => 'select',
'name' => 'default_value',
'class' => 'select2-ionicon ionicon-create',
'choices' => !empty($field['default_value']) ? [$field['default_value'] => $this->render_select2_ionicon_option($field['default_value'])] : [],
'value' => $field['default_value'],
'placeholder' => 'Choose a default icon (optional)',
'ui' => 1,
'allow_null' => 0,
'ajax' => 1,
'ajax_action' => 'acf/fields/ionicon/query'
));
acf_render_field_setting($field, array(
'label' => __('Show Icon Preview', 'acf-ionicon'),
'instructions' => __('Set to \'Yes\' to include a larger icon preview on any admin pages using this field.', 'acf-ionicon'),
'type' => 'true_false',
'name' => 'show_preview',
'ui' => 1,
));
acf_render_field_setting($field, array(
'label' => __('Allow Null?', 'acf-ionicon'),
'instructions' => '',
'type' => 'true_false',
'name' => 'allow_null',
'ui' => 1,
));
}
public function get_icons($icons = array())
{
$icons = get_option('acf_field_ionicon_icon_data');
if (empty($icons)) {
$remote_get = wp_remote_get($this->manifest_url);
$response_code = (int)($remote_get['response']['code'] ?? 0);
if (!is_wp_error($remote_get) && ($response_code >= 200) && ($response_code < 300)) {
$body = wp_remote_retrieve_body($remote_get);
if (!empty($body)) {
$parsed_body = $body ? json_decode($body, true) : null;
if (is_array($parsed_body) && !empty($parsed_body)) {
$icons = $parsed_body['icons'] ?? null;
if (!empty($icons)) {
update_option('acf_field_ionicon_icon_data', $icons, false);
}
}
}
} else {
update_option('acf_field_ionicon_cdn_error', true);
}
}
if (!empty($icons)) {
return $icons;
} else {
return array();
}
}
public function select2_ajax_request()
{
if (!acf_verify_ajax()) {
die();
}
$response = $this->get_ajax_query([
'field_key' => $_POST['field_key'] ?? null,
's' => $_POST['s'] ?? null
]);
acf_send_ajax_results($response);
}
private function render_select2_ionicon_option(string $name)
{
return "