From 56bd8a1b53eb032f3f98759c80510488f801dd3d Mon Sep 17 00:00:00 2001 From: Timo Haapala Date: Mon, 20 Dec 2021 16:20:23 +0200 Subject: [PATCH 1/2] Settings for adding URLs to sitemap for Cookiebot scanner --- CHANGELOG.MD | 4 + .../Fields/Settings/SitemapSettingsTab.php | 94 +++++++++++++++++++ lib/ACF/SettingsGroup.php | 3 + lib/Cookiebot.php | 31 ++++++ 4 files changed, 132 insertions(+) create mode 100644 lib/ACF/Fields/Settings/SitemapSettingsTab.php diff --git a/CHANGELOG.MD b/CHANGELOG.MD index d8f1094f..62de816c 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Added + +- TMS-579: Added Site settings tab for adding URLs to sitemap to make Cookiebot scan sites that are in subfolders. + ### Changed - TMS-573: Escape search query #315 diff --git a/lib/ACF/Fields/Settings/SitemapSettingsTab.php b/lib/ACF/Fields/Settings/SitemapSettingsTab.php new file mode 100644 index 00000000..65cdd9f3 --- /dev/null +++ b/lib/ACF/Fields/Settings/SitemapSettingsTab.php @@ -0,0 +1,94 @@ + 'Sivukartta', + 'enable_sibling_navigation' => [ + 'title' => 'Sivukartan osoitteet', + 'instructions' => 'Syötä osoitteet, jotka lisätään sivukarttaan.', + ], + 'sitemap_links' => [ + 'title' => 'Sivukartan linkit', + 'instructions' => 'Linkit jotka lisätään sivukarttaan Cookiebottia varten.', + 'button_label' => 'Lisää linkki', + ], + 'sitemap_link' => [ + 'title' => 'Linkki', + 'instructions' => '', + ], + ]; + + /** + * The constructor for tab. + * + * @param string $label Label. + * @param null $key Key. + * @param null $name Name. + */ + public function __construct( $label = '', $key = null, $name = null ) { // phpcs:ignore + $label = $this->strings['tab']; + + parent::__construct( $label ); + + $this->sub_fields( $key ); + } + + /** + * Register sub fields. + * + * @param string $key Field tab key. + */ + public function sub_fields( $key ) { + $strings = $this->strings; + + try { + $sitemap_links_field = ( new Field\Repeater( $strings['sitemap_links']['title'] ) ) + ->set_key( "${key}sitemap_links" ) + ->set_name( 'sitemap_links' ) + ->set_button_label( $strings['sitemap_links']['button_label'] ) + ->set_instructions( $strings['sitemap_links']['instructions'] ); + + $sitemap_link_field = ( new Field\Link( $strings['sitemap_link']['title'] ) ) + ->set_key( "${key}_sitemap_link" ) + ->set_name( 'sitemap_link' ) + ->set_instructions( $strings['sitemap_link']['instructions'] ); + + $sitemap_links_field->add_field( $sitemap_link_field ); + + $this->add_fields( [ + $sitemap_links_field, + ] ); + } + catch ( Exception $e ) { + ( new Logger() )->error( $e->getMessage(), $e->getTrace() ); + } + } +} diff --git a/lib/ACF/SettingsGroup.php b/lib/ACF/SettingsGroup.php index da25355e..81665c4a 100644 --- a/lib/ACF/SettingsGroup.php +++ b/lib/ACF/SettingsGroup.php @@ -21,9 +21,11 @@ use TMS\Theme\Base\ACF\Fields\Settings\PageSettingsTab; use TMS\Theme\Base\ACF\Fields\Settings\SocialMediaSettingsTab; use TMS\Theme\Base\ACF\Fields\Settings\ThemeColorTab; +use TMS\Theme\Base\ACF\Fields\Settings\SitemapSettingsTab; use TMS\Theme\Base\Logger; use TMS\Theme\Base\PostType; + /** * Class SettingsGroup * @@ -82,6 +84,7 @@ protected function register_fields() : void { new ExceptionNoticeSettingsTab( '', $field_group->get_key() ), new BlogArticleSettingsTab( '', $field_group->get_key() ), new ContactsSettingsTab( '', $field_group->get_key() ), + new SitemapSettingsTab( '', $field_group->get_key() ), ], $field_group->get_key() ) diff --git a/lib/Cookiebot.php b/lib/Cookiebot.php index 45dc05ec..b84a844d 100644 --- a/lib/Cookiebot.php +++ b/lib/Cookiebot.php @@ -6,6 +6,7 @@ namespace TMS\Theme\Base; use TMS\Theme\Base\Interfaces\Controller; +use TMS\Theme\Base\Settings; /** * Class Cookiebot @@ -22,6 +23,10 @@ public function hooks() : void { \Closure::fromCallable( [ $this, 'add_data_attribute' ] ), 10, 2 ); + add_filter( 'the_seo_framework_sitemap_additional_urls', + \Closure::fromCallable( [ $this, 'add_cb_urls_to_sitemap' ] ), + 10, 2 ); + } /** @@ -53,4 +58,30 @@ private function add_data_attribute( $tag, $handle ) { $tag = str_replace( '>', ' data-cookieconsent="ignore">', $tag ); return $tag; } + + + /** + * Add custom URLs to sitemap for Cookiebot's scanner + * + * @param string $custom_urls URL array to add. + * @return array Custom urls array for Cookiebot scanner. + */ + private function add_cb_urls_to_sitemap( $custom_urls = [] ) { + + $columns = Settings::get_setting( 'sitemap_links' ); + + if ( empty( $columns ) ) { + return null; + } + + foreach ( $columns as $col ) { + if ( ! empty( $col['sitemap_link']['url'] ) ) { + $custom_urls [] = $col['sitemap_link']['url']; + } + } + + return $custom_urls; + } + + } From 0e3b58d6cc36d4bd77f54c0607d03b4705b551c9 Mon Sep 17 00:00:00 2001 From: Timo Haapala Date: Mon, 20 Dec 2021 18:04:41 +0200 Subject: [PATCH 2/2] fixed linter warnings for SitemapSettingsTab.php --- lib/ACF/Fields/Settings/SitemapSettingsTab.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ACF/Fields/Settings/SitemapSettingsTab.php b/lib/ACF/Fields/Settings/SitemapSettingsTab.php index 65cdd9f3..f9a7cbe7 100644 --- a/lib/ACF/Fields/Settings/SitemapSettingsTab.php +++ b/lib/ACF/Fields/Settings/SitemapSettingsTab.php @@ -35,12 +35,12 @@ class SitemapSettingsTab extends Tab { 'title' => 'Sivukartan osoitteet', 'instructions' => 'Syötä osoitteet, jotka lisätään sivukarttaan.', ], - 'sitemap_links' => [ + 'sitemap_links' => [ 'title' => 'Sivukartan linkit', 'instructions' => 'Linkit jotka lisätään sivukarttaan Cookiebottia varten.', 'button_label' => 'Lisää linkki', ], - 'sitemap_link' => [ + 'sitemap_link' => [ 'title' => 'Linkki', 'instructions' => '', ],