From d1ba8da114f8a6e5a3668b507bfdbd08fa870093 Mon Sep 17 00:00:00 2001 From: Christopher Kanitz Date: Thu, 31 Oct 2024 13:40:05 +0100 Subject: [PATCH] upd/svg/add-filter/svg-icon-library-icons (#190) * upd/svg/add-filter/svg-icon-library-icons * update the SVG component README --- plugin/inc/SVG/README.md | 49 ++++++++++++++++++++++++++++++++++++++++ plugin/inc/SVG/SVG.php | 8 ++++--- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/plugin/inc/SVG/README.md b/plugin/inc/SVG/README.md index 293f3de..291cd21 100644 --- a/plugin/inc/SVG/README.md +++ b/plugin/inc/SVG/README.md @@ -9,6 +9,55 @@ current plugin. The component first looks into the theme folder to find the file and then in the plugin folder. +## Filters + +### `lhagentur_svg_icon_library_icons` +Filters the array of ``s passed to the ``. + +#### Parameters +* `(array) $icons` - An array of ``s. + +#### Returns +* `(array) $icons` - The updated / created array of ``s + +#### Example +```PHP +use WpMunich\basics\plugin\SVG\Icon; + +# ... +add_filter( 'lhagentur_svg_icons_library_icons', 'my_svg_icons_library_icons' ); + +# ... +function my_svg_icons_library_icons( array $icons ): array { + // If there's some case in which no icons should be registered. + if (some_other_condition) { + return $icons; + } + + $img_path = 'my/img/path/'; + $my_icons = array( + new Icon( + $icon_path . 'icons/my-icon.svg', + 'my-icon--slug', + __( 'My Icon Label', 'textdomain' ) + ), + # ... + ); + + // If you want to add icons. + if ( some_condition ) { + return array_merge( + $icons, + $my_icons + ); + } + + // If you want to override icons. + return $my_icons; +} +``` + + ## Functions ### get_svg( (sting) $path, (array) $arguments ) diff --git a/plugin/inc/SVG/SVG.php b/plugin/inc/SVG/SVG.php index 46fcf4c..fced531 100644 --- a/plugin/inc/SVG/SVG.php +++ b/plugin/inc/SVG/SVG.php @@ -65,9 +65,8 @@ protected function must_run() {} public function get_icon_library() { if ( ! $this->icon_library instanceof Icon_Library ) { $base_path = plugin()->get_plugin_path(); - - $this->icon_library = new Icon_Library(); - $this->icon_library->set_icons( + $icons = apply_filters( + 'lhagentur_svg_icon_library_icons', array( new Icon( $base_path . 'img/icons/slashes.svg', @@ -86,6 +85,9 @@ public function get_icon_library() { ), ) ); + + $this->icon_library = new Icon_Library(); + $this->icon_library->set_icons( $icons ); } return $this->icon_library; }