Skip to content

Commit 2257b8b

Browse files
committed
Merge branch 'acf-5-6-compat'
2 parents 3e22ff5 + 5a30312 commit 2257b8b

16 files changed

+311
-162
lines changed

.gitignore

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ local.properties
2020
nbproject/
2121
node_modules/*
2222
premium/node_modules/*
23+
package-lock.json
2324

2425
############
2526
## OSes

README.md

100644100755
Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,51 @@
11
# Advanced Custom Fields: SVG #
22

3-
## Description ##
3+
This enhance [Advanced Custom Field](https://www.advancedcustomfields.com/pro/) plugin by adding a custom field.
44

5-
This enhance ACF with a field icons. Add a field type selector to include your great font icon and returns class icon.
6-
One activated you'll be able to use a custom ACF field type which is an icon selector
5+
This ACF field is a select2 field in order to include your great fonts. It will allow you to select icons and then return the corresponding class icon.
76

8-
## Important to know ##
7+
## Compatibility
98

10-
In case you want to include this small plugin to your project running composer you can add this line to your composer.json :
9+
This ACF field type is compatible with:
10+
* ACF 5.0.0 and up, that means the pro version.
11+
* ACF 4 (not supported).
1112

12-
```json
13-
"repositories": [
14-
{
15-
"type": "vcs",
16-
"url": "https://github.com/BeAPI/acf-svg-icon"
17-
}
18-
]
19-
```
13+
## Installation
2014

21-
then run the command :
15+
### via Composer
2216

23-
```shell
24-
composer require bea/acf-svg-icon
25-
```
17+
1. Add a line to your repositories array: `{ "type": "git", "url": "https://github.com/strickdj/acf-field-address" }`
18+
2. Add a line to your require block: `"strickdj/acf-address": "dev-master"`
19+
3. Run: `composer update
20+
21+
### Manual
22+
23+
1. Copy the plugin folder into your plugins folder.
24+
2. Activate the plugin via the plugins admin page.
25+
3. Create a new field via ACF and select the SVG Icon selector.
2626

27-
## Tips ##
27+
## How to ##
2828

2929
### Using this with your own svg file in your own theme ###
3030

3131
```php
3232
add_filter( 'acf_svg_icon_filepath', 'bea_svg_icon_filepath' );
3333
function bea_svg_icon_filepath( $filepath ) {
34-
if ( is_file( get_stylesheet_directory() . '/assets/icons/icons.svg' ) ) {
35-
$filepath = get_stylesheet_directory() . '/assets/icons/icons.svg';
36-
37-
}
38-
39-
return $filepath;
34+
if ( is_file( get_stylesheet_directory() . '/assets/icons/icons.svg' ) ) {
35+
$filepath = get_stylesheet_directory() . '/assets/icons/icons.svg';
36+
}
37+
return $filepath;
4038
}
4139
```
4240

4341
## Changelog ##
4442

45-
### 1.0.1
46-
* 11 May 2017
43+
### 1.2.0 - 27 July 2017
44+
* Add compatibility for ACF 5.6.0 and more versions
45+
* Still keep compatibility for ACF 5.6.0 and lower versions
46+
* Add some custom CSS for a more beautiful admin UI
47+
* Now displaying the icon name, not anymore like a slug
48+
* Improve readme
49+
50+
### 1.0.1 - 11 May 2017
4751
* Initial

acf-svg-icon.php

100644100755
Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php
22
/*
33
Plugin Name: Advanced Custom Fields: SVG Icon
4-
Version: 1.0.1
4+
Version: 1.2.0
55
Plugin URI: http://www.beapi.fr
6-
Description: Add svg icon selector
6+
Description: Add an ACF SVG icon selector.
77
Author: BE API Technical team
8-
Author URI: http://www.beapi.fr
8+
Author URI: https://www.beapi.fr
99
Domain Path: languages
1010
Text Domain: acf-svg-icon
1111
@@ -32,7 +32,7 @@
3232
die();
3333
}
3434

35-
define( 'ACF_SVG_ICON_VER', '1.0.1' );
35+
define( 'ACF_SVG_ICON_VER', '1.2.0' );
3636
define( 'ACF_SVG_ICON_URL', plugin_dir_url( __FILE__ ) );
3737
define( 'ACF_SVG_ICON_DIR', plugin_dir_path( __FILE__ ) );
3838

@@ -59,21 +59,30 @@ function __construct() {
5959
* @since 1.0.0
6060
*/
6161
public static function load_translation() {
62-
load_plugin_textdomain(
63-
'acf-svg-icon',
64-
false,
65-
dirname( plugin_basename( __FILE__ ) ) . '/languages'
66-
);
62+
load_plugin_textdomain( 'acf-svg-icon', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
6763
}
6864

6965
/**
70-
* Register SVG icon field for ACF v5.
66+
* Register SVG icon field for ACF v5 or v5.6 depending on ACF version.
7167
*
7268
* @since 1.0.0
7369
*/
7470
public static function register_field_v5() {
75-
include_once( ACF_SVG_ICON_DIR . 'fields/field-v5.php' );
76-
new acf_field_svg_icon();
71+
// Check ACF version to load the 5.6+ field or the 5+ field version
72+
$acf = new acf();
73+
$version = version_compare( $acf->version, '5.6.O', '>=' ) ? 56 : 5;
74+
75+
// Include the corresponding files
76+
include_once( sprintf( '%sfields/acf-base.php', ACF_SVG_ICON_DIR ) );
77+
include_once( sprintf( '%sfields/acf-%s.php', ACF_SVG_ICON_DIR, $version ) );
78+
79+
/**
80+
* Instantiate the corresponding class
81+
* @see acf_field_svg_icon_56
82+
* @see acf_field_svg_icon_5
83+
*/
84+
$klass = sprintf( 'acf_field_svg_icon_%s', $version );
85+
new $klass();
7786
}
7887
}
7988

assets/css/style.css

100644100755
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22
width:40px;
33
height:40px;
44
margin-right: 10px;
5+
vertical-align: middle;
56
}
7+
68
.acf_svg__icon.small {
79
width: 25px;
8-
height: 25px;
10+
height: 19px;
911
margin-right: 10px;
1012
margin-top: 3px;
1113
float: left;
14+
size: 2px;
15+
}
1216

17+
.acf_svg__span {
18+
font-size: 1.2em;
1319
}

assets/css/style.min.css

100644100755
Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/js/input-5.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
(function ($) {
2+
function initialize_field($field) {
3+
var input = $field.find('.acf-input input');
4+
var allowClear = $(input).attr('data-allow-clear') || 0;
5+
var opts = {
6+
dropdownCssClass: "bigdrop widefat",
7+
dropdownAutoWidth: true,
8+
formatResult: svg_icon_format,
9+
formatSelection: svg_icon_format_small,
10+
data: {results: svg_icon_format_data},
11+
allowClear: 1 == allowClear
12+
};
13+
14+
input.select2(opts);
15+
16+
/**
17+
* Format the content in select 2 for the selected item
18+
*
19+
* @param css
20+
* @returns {string}
21+
*/
22+
function svg_icon_format(css) {
23+
return '<svg class="acf_svg__icon icon ' + css.id + '" aria-hidden="true" role="img"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#' + css.id + '"></use> </svg>' + css.text;
24+
}
25+
26+
/**
27+
* Format the content in select 2 for the dropdown list
28+
*
29+
* @param css
30+
* @returns {string}
31+
*/
32+
function svg_icon_format_small(css) {
33+
return '<svg class="acf_svg__icon small icon ' + css.id + '" aria-hidden="true" role="img"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#' + css.id + '"></use> </svg>' + css.text;
34+
}
35+
36+
}
37+
38+
/*
39+
* ready append (ACF5)
40+
*
41+
* These are 2 events which are fired during the page load
42+
* ready = on page load similar to jQuery(document).ready()
43+
* append = on new DOM elements appended via repeater field
44+
*
45+
* @type event
46+
* @date 20/07/13
47+
*
48+
* @param jQueryel (jQuery selection) the jQuery element which contains the ACF fields
49+
* @return n/a
50+
*/
51+
acf.add_action('ready append', function (jQueryel) {
52+
// search jQueryel for fields of type 'FIELD_NAME'
53+
acf.get_fields({type: 'svg_icon'}, jQueryel).each(function () {
54+
initialize_field($(this));
55+
});
56+
});
57+
})(jQuery);
File renamed without changes.

assets/js/input-56.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
(function ($) {
2+
function initialize_field($field) {
3+
var input = $field.find('.acf-input input');
4+
var allowClear = $(input).attr('data-allow-clear') || 0;
5+
var opts = {
6+
dropdownCssClass: "bigdrop widefat",
7+
dropdownAutoWidth: true,
8+
templateResult: bea_svg_format,
9+
templateSelection: bea_svg_format_small,
10+
data: svg_icon_format_data,
11+
allowClear: 1 == allowClear,
12+
};
13+
14+
input.select2(opts);
15+
16+
/**
17+
* Format the content in select 2 for the selected item
18+
*
19+
* @param css
20+
* @returns {string}
21+
*/
22+
function bea_svg_format(css) {
23+
if (!css.id) { return css.text; }
24+
return $('<span class="acf_svg__span"><svg class="acf_svg__icon icon ' + css.id + '" aria-hidden="true" role="img"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#' + css.id + '"></use></svg>' + css.text + '</span>');
25+
};
26+
27+
/**
28+
* Format the content in select 2 for the dropdown list
29+
*
30+
* @param css
31+
* @returns {string}
32+
*/
33+
function bea_svg_format_small(css) {
34+
if (!css.id) { return css.text; }
35+
return $('<span class="acf_svg__span"><svg class="acf_svg__icon small icon ' + css.id + '" aria-hidden="true" role="img"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#' + css.id + '"></use></svg>' + css.text + '</span>');
36+
};
37+
}
38+
39+
/*
40+
* ready append (ACF5)
41+
*
42+
* These are 2 events which are fired during the page load
43+
* ready = on page load similar to jQuery(document).ready()
44+
* append = on new DOM elements appended via repeater field
45+
*
46+
* @type event
47+
* @date 20/07/13
48+
*
49+
* @param jQueryel (jQuery selection) the jQuery element which contains the ACF fields
50+
* @return n/a
51+
*/
52+
acf.add_action('ready append', function (jQueryel) {
53+
// search jQueryel for fields of type 'FIELD_NAME'
54+
acf.get_fields({type: 'svg_icon'}, jQueryel).each(function () {
55+
initialize_field($(this));
56+
});
57+
});
58+
})(jQuery);

assets/js/input-56.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/js/input.js

Lines changed: 0 additions & 58 deletions
This file was deleted.

composer.json

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bea/acf-svg-icon",
3-
"description": "Add ACF SVG selector",
3+
"description": "Add ACF field for selecting SVG icons.",
44
"minimum-stability": "stable",
55
"license": "GPL-2.0",
66
"authors": [

fields/acf-5.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php class acf_field_svg_icon_5 extends acf_field_svg_icon {
2+
3+
function __construct() {
4+
// do not delete!
5+
parent::__construct();
6+
}
7+
8+
/**
9+
* Enqueue assets for the SVG icon field in admin
10+
*
11+
* @since 1.0.0
12+
*/
13+
function input_admin_enqueue_scripts() {
14+
// The suffix.
15+
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG === true ? '' : '.min';
16+
17+
// Scripts.
18+
wp_register_script( 'acf-input-svg-icon', ACF_SVG_ICON_URL . 'assets/js/input-5' . $suffix . '.js', array(
19+
'jquery',
20+
'select2',
21+
'acf-input'
22+
), ACF_SVG_ICON_VER );
23+
24+
// Enqueuing.
25+
wp_enqueue_script( 'acf-input-svg-icon' );
26+
27+
parent::input_admin_enqueue_scripts();
28+
}
29+
}

0 commit comments

Comments
 (0)