Skip to content

Commit

Permalink
#6 : handle to mix svg (media) and custom sprites (mutliple svg)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximeCulea committed Oct 31, 2018
1 parent 937867f commit 1889c02
Showing 1 changed file with 66 additions and 40 deletions.
106 changes: 66 additions & 40 deletions fields/acf-base.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

class acf_field_svg_icon extends acf_field {
<?php class acf_field_svg_icon extends acf_field {

/**
* Defaults for the svg.
Expand All @@ -9,6 +7,8 @@ class acf_field_svg_icon extends acf_field {
*/
public $defaults = array();

public $cache_key = 'acf_svg_icon_files';

function __construct() {
// vars
$this->name = 'svg_icon';
Expand All @@ -22,7 +22,7 @@ function __construct() {
parent::__construct();

// Hooks !
$this->add_action( 'save_post_attachment', array( $this, 'save_post_attachment' ) );
add_action( 'save_post_attachment', array( $this, 'save_post_attachment' ) );
}

/**
Expand All @@ -38,11 +38,7 @@ function __construct() {
function render_field( $field ) {
// create Field HTML
?>
<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 _e( 'Select an icon', 'acf-svg-icon' ); ?>"
data-allow-clear="<?php echo esc_attr( $field['allow_clear'] ) ?>"/>
<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 _e( 'Select an icon', 'acf-svg-icon' ); ?>" data-allow-clear="<?php echo esc_attr( $field['allow_clear'] ) ?>" />
<?php
}

Expand Down Expand Up @@ -77,7 +73,14 @@ function render_field_settings( $field ) {
* @author Nicolas JUEN
*/
private function get_svg_files_path() {
return apply_filters( 'acf_svg_icon_filepath', array() );
$custom_svg_path_icons = apply_filters( 'acf_svg_icon_filepath', array() );

return array_map( function ( $val ) {
return [
'file' => $val,
'type' => 'custom',
];
}, $custom_svg_path_icons );
}

/**
Expand All @@ -88,10 +91,8 @@ private function get_svg_files_path() {
* @return array
*/
public function get_all_svg_files() {

// First try to load files from the cache.
$cache_key = 'acf_svg_icon_files';
$files = wp_cache_get( $cache_key );
$files = wp_cache_get( $this->cache_key );
if ( ! empty( $files ) ) {
return $files;
}
Expand All @@ -113,7 +114,7 @@ public function get_all_svg_files() {
$files = array_merge( $media_svg_files, $custom_svg_files );

// Cache 24 hours.
wp_cache_set( $cache_key, $files, '', HOUR_IN_SECONDS * 24 );
wp_cache_set( $this->cache_key, $files, '', HOUR_IN_SECONDS * 24 );

return $files;
}
Expand All @@ -126,30 +127,38 @@ public function get_all_svg_files() {
* @return array|bool
*/
public function parse_svg() {

$files = $this->get_all_svg_files();
if ( empty( $files ) ) {
return false;
}

$out = array();

foreach ( $files as $file ) {
if ( ! is_file( $file ) ) {
if ( ! is_file( $file['file'] ) ) {
continue;
}

// If not extract them from the CSS file.
$contents = file_get_contents( $file );
preg_match_all( '#id="(\S+)"#', $contents, $svg );
array_shift( $svg );

foreach ( $svg[0] as $id ) {
$out[] = array(
'id' => $id,
'text' => self::get_nice_display_text( $id ),
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', $contents, $svg );

foreach ( $svg[1] as $id ) {
$id = sanitize_title( $id );
$out[] = array(
'id' => $id,
'text' => self::get_nice_display_text( $id ),
'disabled' => false,
);
}
}
}

Expand Down Expand Up @@ -187,7 +196,12 @@ public function get_medias_svg() {

$svg = array();
foreach ( $attachments->posts as $attachment ) {
$svg[] = get_attached_file( $attachment->ID );
$svg[] = [
'type' => 'media',
'id' => $attachment->ID,
'file' => get_attached_file( $attachment->ID ),
'file_url' => wp_get_attachment_url( $attachment->ID ),
];
}

return $svg;
Expand All @@ -202,15 +216,17 @@ public function get_medias_svg() {
*
* @return string
*/
public static function get_nice_display_text( $id ) {
public static function get_nice_display_text( $id, $delete_suffixe = 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.
unset( $ex[0] );
if ( $delete_suffixe ) {
unset( $ex[0] );
}

// Remix values into one with spaces
$text = implode( ' ', $ex );
Expand All @@ -237,12 +253,13 @@ public function display_svg() {
if ( empty( $files ) ) {
return;
}

foreach ( $files as $file ) {
if ( ! is_file( $file ) ) {
if ( ! is_file( $file['file'] ) ) {
continue;
}
ob_start();
include_once( $file );
include_once( $file['file'] );
$svg = ob_get_clean();

if ( true === strpos( $svg, 'style="' ) ) {
Expand Down Expand Up @@ -284,20 +301,29 @@ public function input_admin_footer() {
}

/**
* Flush cache on new SVG added to medias
*
* @since 2.0.0
*
* Flush cache on new SVG added to medias
*
* @since 2.0.0
*
* @param $post_ID
*
* @return bool
*/
public function save_post_attachment( $post_ID ) {
$mime_type = get_post_mime_type( $post_ID );
if ( 'image/svg+xml' != $mime_type ) {
return false;
}
if ( 'image/svg+xml' != $mime_type ) {
return false;
}

wp_cache_flush();
}
wp_cache_delete( $this->cache_key );
}

function format_value( $value, $post_id, $field ) {
if ( ! is_int( $value ) ) {
return $value;
}

$file = get_attached_file( $value );
echo ob_start( $file );
}
}

0 comments on commit 1889c02

Please sign in to comment.