-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathacf-base.php
More file actions
354 lines (308 loc) · 7.71 KB
/
acf-base.php
File metadata and controls
354 lines (308 loc) · 7.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php
class acf_field_svg_icon extends acf_field {
/**
* Defaults for the svg.
*
* @var array
*/
public $defaults = array();
public $cache_key = 'acf_svg_icon_files';
public function __construct() {
// vars
$this->name = 'svg_icon';
$this->label = __( 'SVG Icon selector', 'acf-svg-icon' );
$this->category = __( 'Basic', 'acf' );
$this->defaults = array(
'allow_clear' => 0,
);
// do not delete!
parent::__construct();
// Hooks !
add_action( 'add_attachment', array( $this, 'save_post_attachment' ) );
add_action( 'edit_attachment', array( $this, 'save_post_attachment' ) );
}
/**
*
* Create the HTML interface for your field
*
* @param $field - an array holding all the field's data
*
* @type action
* @since 3.6
* @date 23/01/13
*/
public function render_field( $field ) {
?>
<input class="widefat acf-svg-icon-<?php echo esc_attr( $field['type'] ); ?>"
value="<?php echo esc_attr( $field['value'] ); ?>" name="<?php echo esc_attr( $field['name'] ); ?>"
data-placeholder="<?php esc_attr_e( 'Select an icon', 'acf-svg-icon' ); ?>"
data-allow-clear="<?php echo esc_attr( $field['allow_clear'] ) ?>"/>
<?php
}
/**
* render_field_settings()
*
* Create extra options for your field. This is rendered when editing a field.
* The value of $field['name'] can be used (like bellow) to save extra data to the $field
*
* @type action
*
* @param $field - an array holding all the field's data
*
* @since 3.6
* @date 23/01/13
*
*/
public function render_field_settings( $field ) {
// allow clear.
acf_render_field_setting( $field, array(
'label' => __( 'Display clear button?', 'acf-svg-icon' ),
'instructions' => __( 'Whether or not a clear button is displayed when the select box has a selection.', 'acf-svg-icon' ),
'name' => 'allow_clear',
'type' => 'true_false',
'ui' => 1,
) );
}
/**
* Get the SVG filepath from theme.
*
* @return array
* @author Nicolas JUEN
*/
private function get_svg_files_path() {
$custom_svg_path_icons = apply_filters( 'acf_svg_icon_filepath', array() );
return array_map( function ( $val ) {
return [
'type' => 'custom',
'file' => $val,
];
}, (array) $custom_svg_path_icons );
}
/**
* Merge WP Medias SVG and custom SVG files
*
* @return array
* @since 2.0.0
*
*/
public function get_all_svg_files() {
// First try to load files list from the cache.
$files = get_transient( $this->cache_key );
if ( ! empty( $files ) ) {
return $files;
}
/**
* Get WP Media SVGs
*
* @since 2.0.0
*/
$media_svg_files = $this->get_medias_svg();
/**
* The path to the svg file.
*
* @since 1.0.0
*/
$custom_svg_files = $this->get_svg_files_path();
$files = array_merge( $media_svg_files, $custom_svg_files );
// Cache 24 hours.
set_transient( $this->cache_key, $files, HOUR_IN_SECONDS * 24 );
return $files;
}
/**
* Extract icons from svg file.
*
* @return array|bool
* @since 1.0.0
*
*/
public function parse_svg() {
$files = $this->get_all_svg_files();
if ( empty( $files ) ) {
return false;
}
/**
* Get the allowed tags to parse icon's ids
*
* @param string $allowed_tags : Passed directly to strip_tags
*
* @return string
* @since 2.0.1
*
* @author david-treblig
*/
$allowed_tags = apply_filters( 'acf_svg_icon_svg_parse_tags', '<symbol><g>' );
$out = array();
foreach ( $files as $file ) {
if ( ! is_file( $file['file'] ) ) {
continue;
}
if ( 'media' === $file['type'] ) {
$pathinfo = pathinfo( $file['file'] );
$out[] = array(
'id' => $file['id'],
'text' => self::get_nice_display_text( $pathinfo['filename'], false ),
'url' => $file['file_url'],
'disabled' => false,
);
} else {
// If not extract them from the CSS file.
$contents = file_get_contents( $file['file'] );
preg_match_all( '/id="(\S+)"/m', strip_tags( $contents, $allowed_tags ), $svg );
foreach ( $svg[1] as $id ) {
$id = sanitize_title( $id );
$out[] = array(
'id' => $id,
'text' => self::get_nice_display_text( $id ),
'disabled' => false,
);
}
}
}
return apply_filters( 'acf_svg_icon_parsed_svg', $out, $files );
}
/**
* Get WP Medias SVGs
*
* @return array
* @since 2.0.0
*
*/
public function get_medias_svg() {
$args = array(
'post_type' => 'attachment',
'posts_per_page' => '-1',
'post_status' => 'inherit',
'post_mime_type' => 'image/svg+xml',
);
/**
* Filter WP Query get attachments args
*
* @param array $args
*
* @since 2.0.0
*
*/
$args = apply_filters( 'acf_svg_icon_wp_medias_svg_args', $args );
$attachments = new WP_Query( $args );
if ( empty( $attachments->posts ) ) {
return array();
}
$svg = array();
foreach ( $attachments->posts as $attachment ) {
$svg[] = [
'type' => 'media',
'id' => $attachment->ID,
'file' => get_attached_file( $attachment->ID ),
'file_url' => wp_get_attachment_url( $attachment->ID ),
];
}
return $svg;
}
/**
* Format the icon id to get his nicename for display purpose
*
* @param $id
* @param bool $delete_suffix
*
* @return string
* @since 1.2.0
*/
public static function get_nice_display_text( $id, $delete_suffix = true ) {
// Split up the string based on the '-' carac
$ex = explode( '-', $id );
if ( empty( $ex ) ) {
return $id;
}
// Delete the first value, as it has no real value for the icon name.
if ( $delete_suffix ) {
unset( $ex[0] );
}
// Remix values into one with spaces
$text = implode( ' ', $ex );
// Add uppercase to the first word
return ucfirst( $text );
}
/**
* Display the css based on the vars given for dynamic fonts url.
*
* @since 1.0.0
*/
public function display_svg() {
/**
* The svg's files URLs
*
* @param array $font_urls the default svg file url
*
* @since 1.0.0
*
*/
$files = $this->get_all_svg_files();
if ( empty( $files ) ) {
return;
}
foreach ( $files as $file ) {
if ( ! is_file( $file['file'] ) ) {
continue;
}
$svg = file_get_contents( $file['file'] );
if ( true === strpos( $svg, 'style="' ) ) {
$svg = str_replace( 'style="', 'style="display:none; ', $svg );
} else {
$svg = str_replace( '<svg ', '<svg style="display:none;" ', $svg );
}
echo $svg;
}
}
/**
* Enqueue assets for the SVG icon field in admin
*
* @since 1.0.0
*/
public function input_admin_enqueue_scripts() {
// Min version ?
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG === true ? '' : '.min';
wp_localize_script( 'acf-input-svg-icon', 'svg_icon_format_data', $this->parse_svg() );
wp_register_style( 'acf-input-svg-icon', ACF_SVG_ICON_URL . 'assets/css/style' . $suffix . '.css', array( 'select2' ), ACF_SVG_ICON_VER );
wp_enqueue_script( 'acf-input-svg-icon' );
wp_enqueue_style( 'acf-input-svg-icon' );
}
/**
* Display SVG style in head.
*
* @since 1.0.0
*/
public function input_admin_footer() {
$this->display_svg();
}
/**
* Flush cache on new SVG added to medias
*
* @param $post_ID
*
* @since 2.0.0
*
*/
public function save_post_attachment( $post_ID ) {
$mime_type = get_post_mime_type( $post_ID );
if ( 'image/svg+xml' !== $mime_type ) {
return;
}
delete_transient( $this->cache_key );
}
/**
* TODO: Pas compris l'intérêt de ce filtre ici
*
* @param $value
* @param $post_id
* @param $field
*
* @return mixed
*/
public function format_value( $value, $post_id, $field ) {
if ( ! is_int( $value ) ) {
return $value;
}
//$file = get_attached_file( $value );
return $value;
}
}