From 41811ef7983b711b173dcd56e64726473577ce21 Mon Sep 17 00:00:00 2001 From: Clement Boirie Date: Fri, 16 May 2025 19:26:37 +0200 Subject: [PATCH 1/2] fix cache not flushed when SVG are added or removed from the media library --- fields/acf-base.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/fields/acf-base.php b/fields/acf-base.php index b3fb950..ce991d7 100644 --- a/fields/acf-base.php +++ b/fields/acf-base.php @@ -24,7 +24,9 @@ public function __construct() { parent::__construct(); // Hooks ! - add_action( 'save_post_attachment', array( $this, 'save_post_attachment' ) ); + add_action( 'add_attachment', array( $this, 'flush_cache_for_attachments' ) ); + add_action( 'edit_attachment', array( $this, 'flush_cache_for_attachments' ) ); + add_action( 'delete_attachment', array( $this, 'flush_cache_for_attachments' ) ); } /** @@ -327,14 +329,14 @@ public function input_admin_footer() { } /** - * Flush cache on new SVG added to medias + * Flush cache when an SVG is added, update or removed from the medias * * @param $post_ID * * @since 2.0.0 * */ - public function save_post_attachment( $post_ID ) { + public function flush_cache_for_attachments( $post_ID ) { $mime_type = get_post_mime_type( $post_ID ); if ( 'image/svg+xml' !== $mime_type ) { return; From 7d4c3daf13e5d64760e62363a08619063b769164 Mon Sep 17 00:00:00 2001 From: Clement Boirie Date: Fri, 16 May 2025 19:30:15 +0200 Subject: [PATCH 2/2] add a button on the admin bar to flush the cache --- acf-svg-icon.php | 65 ++++++++++++++++++++++++++++++++ fields/acf-base.php | 14 +++++-- languages/acf-svg-icon-fr_FR.mo | Bin 1170 -> 1717 bytes languages/acf-svg-icon-fr_FR.po | 49 +++++++++++++++++++----- languages/acf-svg-icon.pot | 37 ++++++++++++++---- 5 files changed, 145 insertions(+), 20 deletions(-) diff --git a/acf-svg-icon.php b/acf-svg-icon.php index ec9b17a..6dce394 100755 --- a/acf-svg-icon.php +++ b/acf-svg-icon.php @@ -35,6 +35,7 @@ define( 'ACF_SVG_ICON_VER', '2.1.3' ); define( 'ACF_SVG_ICON_URL', plugin_dir_url( __FILE__ ) ); define( 'ACF_SVG_ICON_DIR', plugin_dir_path( __FILE__ ) ); +define( 'ACF_SVG_ICON_CACHE_KEY', 'acf_svg_icon_files' ); class acf_field_svg_icon_plugin { @@ -50,6 +51,10 @@ public function __construct() { // Register ACF fields add_action( 'acf/include_field_types', array( __CLASS__, 'register_field_v5' ) ); + + // Allow to flush the SVG cached data. + add_action( 'admin_bar_menu', array( __CLASS__, 'add_action_button_in_admin_bar' ), 120 ); + add_action( 'init', array( __CLASS__, 'handle_flush_action' ) ); } /** @@ -82,6 +87,66 @@ public static function register_field_v5() { new $klass(); } } + + /** + * Add a button in the WordPress admin bar to flush the cache for the SVG. + * + * @param \WP_Admin_Bar $admin_bar + * + * @return void + */ + public static function add_action_button_in_admin_bar( $admin_bar ) { + if ( ! current_user_can( 'manage_options' ) ) { + return; + } + + $admin_bar->add_node( + array( + 'id' => 'acf_svg_icon_flush_cache', + 'parent' => null, + 'group' => null, + 'title' => esc_html__( 'Flush ACF SVG Icon cache', 'acf-svg-icon' ), + 'href' => add_query_arg( + [ + 'action' => 'acf_svg_icon_flush_cache', + '_wpnonce' => wp_create_nonce( 'flush_cache' ) + ] + ), + 'meta' => [ + 'title' => esc_html__( "If some SVG are missing or not up to date this could help resolve the issue.", 'acf-svg-icon' ), + ] + ) + ); + } + + /** + * Handle the cache flush action. + * + * @return void + */ + public static function handle_flush_action() { + if ( ! isset( $_GET['action'] ) || 'acf_svg_icon_flush_cache' !== $_GET['action'] ) { + return; + } + + if ( ! current_user_can( 'manage_options' ) ) { + wp_die( __( 'You do not have sufficient permissions to execute this action.', 'acf-svg-icon' ) ); + } + + $nonce = sanitize_text_field( $_GET['_wpnonce'] ); + if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, 'flush_cache' ) ) { + wp_die( __( "Security error. Action couldn't be verified.", 'acf-svg-icon' ) ); + } + + delete_transient( ACF_SVG_ICON_CACHE_KEY ); + + $referer = wp_get_referer(); + if ( ! $referer ) { + $referer = home_url( '/' ); + } + wp_safe_redirect( $referer ); + exit; + } } /** diff --git a/fields/acf-base.php b/fields/acf-base.php index ce991d7..8196283 100644 --- a/fields/acf-base.php +++ b/fields/acf-base.php @@ -9,6 +9,14 @@ class acf_field_svg_icon extends acf_field { */ public $defaults = array(); + /** + * Name of the cache key used to store SVG data after processing. + * + * @deprecated This as been replaced by the constant {@see ACF_SVG_ICON_CACHE_KEY} and + * will be removed in the next version. + * + * @var string + */ public $cache_key = 'acf_svg_icon_files'; public function __construct() { @@ -99,7 +107,7 @@ private function get_svg_files_path() { */ public function get_all_svg_files() { // First try to load files list from the cache. - $files = get_transient( $this->cache_key ); + $files = get_transient( ACF_SVG_ICON_CACHE_KEY ); if ( ! empty( $files ) ) { return $files; } @@ -121,7 +129,7 @@ public function get_all_svg_files() { $files = array_merge( $media_svg_files, $custom_svg_files ); // Cache 24 hours. - set_transient( $this->cache_key, $files, HOUR_IN_SECONDS * 24 ); + set_transient( ACF_SVG_ICON_CACHE_KEY, $files, HOUR_IN_SECONDS * 24 ); return $files; } @@ -342,7 +350,7 @@ public function flush_cache_for_attachments( $post_ID ) { return; } - delete_transient( $this->cache_key ); + delete_transient( ACF_SVG_ICON_CACHE_KEY ); } /** diff --git a/languages/acf-svg-icon-fr_FR.mo b/languages/acf-svg-icon-fr_FR.mo index 594f7c010fae374e6b9a6117ed27a8de39a5bdf6..4624af715ea4fc50185562905d6a062042ce99fa 100644 GIT binary patch delta 870 zcmY+AyK7WI6vk(h_=s@@U!daS2+B5_B>@wHq7Z_c1Og%}fnek0-m}?}yLYbhSfUnd zr$rjGq!2+;X2mjs{ROc};Xh!ZwS`#NSoqD|C^&HLZ{~34`_6ouqwmYzFDFK}1lBRs zdDI5#BiZoOy0NJ$A}O%nbE6BzS9nw!eDPG{`%~dit=5^!A9IRrszUE?-x1c z{TeM*-`vBzqzcvCH&RlC&iBmTZ)ZV4qXVcwNl|) T7%_||6J?P?#15f_ye9qto1)*b delta 328 zcmdnWJBhRYo)F7a1|Z-9Vi_RL0b*Vt-UGxS@BxTrf%qd3D*!P+BLjmdkT!tQZa`WP z$d3Wid_X!6Nc#inS|BY9q|X9r5g`2#NM``?Hy~C4^5dDo=G8N_02v?ymjY>!19k&x zNg#a#NGk&APe3{ZNQ\n" "Language-Team: LANGUAGE \n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" -"POT-Creation-Date: 2023-04-11T15:36:45+00:00\n" +"POT-Creation-Date: 2025-05-16T16:54:00+00:00\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" -"X-Generator: WP-CLI 2.7.1\n" +"X-Generator: WP-CLI 2.11.0\n" "X-Domain: acf-svg-icon\n" #. Plugin Name of the plugin +#: acf-svg-icon.php msgid "Advanced Custom Fields: SVG Icon" msgstr "" #. Plugin URI of the plugin +#: acf-svg-icon.php msgid "http://www.beapi.fr" msgstr "" #. Description of the plugin +#: acf-svg-icon.php msgid "Add an ACF SVG icon selector." msgstr "" #. Author of the plugin +#: acf-svg-icon.php msgid "BE API Technical team" msgstr "" #. Author URI of the plugin +#: acf-svg-icon.php msgid "https://www.beapi.fr" msgstr "" -#: fields/acf-base.php:17 +#: acf-svg-icon.php:108 +msgid "Flush ACF SVG Icon cache" +msgstr "" + +#: acf-svg-icon.php:116 +msgid "If some SVG are missing or not up to date this could help resolve the issue." +msgstr "" + +#: acf-svg-icon.php:133 +msgid "You do not have sufficient permissions to execute this action." +msgstr "" + +#: acf-svg-icon.php:138 +msgid "Security error. Action couldn't be verified." +msgstr "" + +#: fields/acf-base.php:15 msgid "SVG Icon selector" msgstr "" -#: fields/acf-base.php:44 +#: fields/acf-base.php:42 msgid "Select an icon" msgstr "" -#: fields/acf-base.php:66 +#: fields/acf-base.php:64 msgid "Display clear button?" msgstr "" -#: fields/acf-base.php:67 +#: fields/acf-base.php:65 msgid "Whether or not a clear button is displayed when the select box has a selection." msgstr ""