Skip to content

Commit

Permalink
Merge pull request #17 from BeAPI/issue/16
Browse files Browse the repository at this point in the history
Issue/16
  • Loading branch information
asadowski10 authored Oct 28, 2019
2 parents 2ec22f4 + 6792840 commit f0fc1f5
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 66 deletions.
16 changes: 8 additions & 8 deletions acf-svg-icon.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/*
Plugin Name: Advanced Custom Fields: SVG Icon
Version: 2.0.3
Version: 2.0.4
Plugin URI: http://www.beapi.fr
Description: Add an ACF SVG icon selector.
Author: BE API Technical team
Expand Down Expand Up @@ -32,7 +32,7 @@
die();
}

define( 'ACF_SVG_ICON_VER', '2.0.3' );
define( 'ACF_SVG_ICON_VER', '2.0.4' );
define( 'ACF_SVG_ICON_URL', plugin_dir_url( __FILE__ ) );
define( 'ACF_SVG_ICON_DIR', plugin_dir_path( __FILE__ ) );

Expand All @@ -45,8 +45,7 @@ class acf_field_svg_icon_plugin {
*
* @since 1.0.0
*/
function __construct() {

public function __construct() {
add_action( 'init', array( __CLASS__, 'load_translation' ), 1 );

// Register ACF fields
Expand All @@ -64,15 +63,15 @@ public static function load_translation() {

/**
* Register SVG icon field for ACF v5 or v5.6 depending on ACF version.
* @param $major
*
* @since 1.0.0
*/
public static function register_field_v5() {
$version = version_compare( acf_get_setting('version'), '5.6.O', '>=' ) ? 56 : 5;
$version = version_compare( acf_get_setting( 'version' ), '5.6.O', '>=' ) ? 56 : 5;

// Include the corresponding files
include_once( sprintf( '%sfields/acf-base.php', ACF_SVG_ICON_DIR ) );
include_once( sprintf( '%sfields/acf-%s.php', ACF_SVG_ICON_DIR, $version ) );
include_once sprintf( '%sfields/acf-base.php', ACF_SVG_ICON_DIR );
include_once sprintf( '%sfields/acf-%s.php', ACF_SVG_ICON_DIR, $version );

/**
* Instantiate the corresponding class
Expand All @@ -92,5 +91,6 @@ public static function register_field_v5() {
function acf_field_svg_icon() {
new acf_field_svg_icon_plugin();
}

add_action( 'plugins_loaded', 'acf_field_svg_icon' );

17 changes: 8 additions & 9 deletions fields/acf-5.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php class acf_field_svg_icon_5 extends acf_field_svg_icon {
<?php

function __construct() {
class acf_field_svg_icon_5 extends acf_field_svg_icon {

public function __construct() {
// do not delete!
parent::__construct();
}
Expand All @@ -10,20 +12,17 @@ function __construct() {
*
* @since 1.0.0
*/
function input_admin_enqueue_scripts() {
// The suffix.
public function input_admin_enqueue_scripts() {
// Min version ?
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG === true ? '' : '.min';

// Scripts.
wp_register_script( 'acf-input-svg-icon', ACF_SVG_ICON_URL . 'assets/js/input-5' . $suffix . '.js', array(
'jquery',
'select2',
'acf-input'
'acf-input',
), ACF_SVG_ICON_VER );

// Enqueuing.
wp_enqueue_script( 'acf-input-svg-icon' );

parent::input_admin_enqueue_scripts();
}
}
}
15 changes: 7 additions & 8 deletions fields/acf-56.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php class acf_field_svg_icon_56 extends acf_field_svg_icon {
<?php

function __construct() {
class acf_field_svg_icon_56 extends acf_field_svg_icon {

public function __construct() {
// do not delete!
parent::__construct();
}
Expand All @@ -10,16 +12,13 @@ function __construct() {
*
* @since 1.0.0
*/
function input_admin_enqueue_scripts() {
// The suffix
public function input_admin_enqueue_scripts() {
// Min version ?
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG === true ? '' : '.min';

// Scripts
wp_register_script( 'acf-input-svg-icon', ACF_SVG_ICON_URL . 'assets/js/input-56' . $suffix . '.js', array( 'select2' ), ACF_SVG_ICON_VER );

// Enqueuing
wp_enqueue_script( 'acf-input-svg-icon' );

parent::input_admin_enqueue_scripts();
}
}
}
93 changes: 52 additions & 41 deletions fields/acf-base.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?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,7 +11,7 @@

public $cache_key = 'acf_svg_icon_files';

function __construct() {
public function __construct() {
// vars
$this->name = 'svg_icon';
$this->label = __( 'SVG Icon selector', 'acf-svg-icon' );
Expand All @@ -35,10 +37,12 @@ function __construct() {
* @since 3.6
* @date 23/01/13
*/
function render_field( $field ) {
// create Field HTML
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 _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 esc_attr_e( 'Select an icon', 'acf-svg-icon' ); ?>"
data-allow-clear="<?php echo esc_attr( $field['allow_clear'] ) ?>"/>
<?php
}

Expand All @@ -49,13 +53,14 @@ function render_field( $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
*
* @param $field - an array holding all the field's data
*/
function render_field_settings( $field ) {

public function render_field_settings( $field ) {
// allow clear.
acf_render_field_setting( $field, array(
'label' => __( 'Display clear button?', 'acf-svg-icon' ),
Expand Down Expand Up @@ -86,13 +91,13 @@ private function get_svg_files_path() {
/**
* Merge WP Medias SVG and custom SVG files
*
* @return array
* @since 2.0.0
*
* @return array
*/
public function get_all_svg_files() {
// First try to load files from the cache.
$files = wp_cache_get( $this->cache_key );
// First try to load files list from the cache.
$files = get_transient( $this->cache_key );
if ( ! empty( $files ) ) {
return $files;
}
Expand All @@ -114,17 +119,17 @@ public function get_all_svg_files() {
$files = array_merge( $media_svg_files, $custom_svg_files );

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

return $files;
}

/**
* Extract icons from svg file.
*
* @return array|bool
* @since 1.0.0
*
* @return array|bool
*/
public function parse_svg() {
$files = $this->get_all_svg_files();
Expand All @@ -137,10 +142,10 @@ public function parse_svg() {
*
* @param string $allowed_tags : Passed directly to strip_tags
*
* @author david-treblig
* @return string
* @since 2.0.1
*
* @return string
* @author david-treblig
*/
$allowed_tags = apply_filters( 'acf_svg_icon_svg_parse_tags', '<symbol><g>' );

Expand Down Expand Up @@ -180,9 +185,9 @@ public function parse_svg() {
/**
* Get WP Medias SVGs
*
* @return array
* @since 2.0.0
*
* @return array
*/
public function get_medias_svg() {
$args = array(
Expand All @@ -195,9 +200,10 @@ public function get_medias_svg() {
/**
* Filter WP Query get attachments args
*
* @param array $args
*
* @since 2.0.0
*
* @param array $args
*/
$args = apply_filters( 'acf_svg_icon_wp_medias_svg_args', $args );

Expand All @@ -223,28 +229,28 @@ public function get_medias_svg() {
* Format the icon id to get his nicename for display purpose
*
* @param $id
*
* @since 1.2.0
* @param bool $delete_suffix
*
* @return string
* @since 1.2.0
*/
public static function get_nice_display_text( $id, $delete_suffixe = true ) {
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_suffixe ) {
if ( $delete_suffix ) {
unset( $ex[0] );
}

// Remix values into one with spaces
$text = implode( ' ', $ex );

// Add uppercase to the first word
return Ucfirst( $text );
return ucfirst( $text );
}

/**
Expand All @@ -253,13 +259,13 @@ public static function get_nice_display_text( $id, $delete_suffixe = true ) {
* @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
*
* @param array $font_urls the default svg file url
*/
$files = $this->get_all_svg_files();
if ( empty( $files ) ) {
Expand All @@ -270,9 +276,8 @@ public function display_svg() {
if ( ! is_file( $file['file'] ) ) {
continue;
}
ob_start();
include_once( $file['file'] );
$svg = ob_get_clean();

$svg = file_get_contents( $file['file'] );

if ( true === strpos( $svg, 'style="' ) ) {
$svg = str_replace( 'style="', 'style="display:none; ', $svg );
Expand All @@ -289,16 +294,13 @@ public function display_svg() {
*
* @since 1.0.0
*/
function input_admin_enqueue_scripts() {
// The suffix
public function input_admin_enqueue_scripts() {
// Min version ?
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG === true ? '' : '.min';

// Localizing the data
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 );

// Enqueuing
wp_enqueue_script( 'acf-input-svg-icon' );
wp_enqueue_style( 'acf-input-svg-icon' );
}
Expand All @@ -315,27 +317,36 @@ public function input_admin_footer() {
/**
* Flush cache on new SVG added to medias
*
* @since 2.0.0
*
* @param $post_ID
*
* @return bool
* @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 false;
if ( 'image/svg+xml' !== $mime_type ) {
return;
}

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

function format_value( $value, $post_id, $field ) {
/**
* 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 );
echo ob_start( $file );
//$file = get_attached_file( $value );
return $value;
}

}

0 comments on commit f0fc1f5

Please sign in to comment.