Skip to content

Commit

Permalink
Merge pull request #324 from devgeniem/TMS-579
Browse files Browse the repository at this point in the history
TMS-579: Add tab to site settings for adding urls to sitemap for Cookiebot
  • Loading branch information
tim0haapala authored Dec 20, 2021
2 parents ca73dfb + 0e3b58d commit 962be41
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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-494: Fix breadcrumb trail for articles and blogs #322
Expand Down
94 changes: 94 additions & 0 deletions lib/ACF/Fields/Settings/SitemapSettingsTab.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* Copyright (c) 2021. Geniem Oy
*/

namespace TMS\Theme\Base\ACF\Fields\Settings;

use Geniem\ACF\Exception;
use Geniem\ACF\Field;
use Geniem\ACF\Field\Tab;
use TMS\Theme\Base\Logger;

/**
* Class PageSettingsTab
*
* @package TMS\Theme\Base\ACF\Tab
*/
class SitemapSettingsTab extends Tab {

/**
* Where should the tab switcher be located
*
* @var string
*/
protected $placement = 'left';

/**
* Tab strings.
*
* @var array
*/
protected $strings = [
'tab' => '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() );
}
}
}
3 changes: 3 additions & 0 deletions lib/ACF/SettingsGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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()
)
Expand Down
31 changes: 31 additions & 0 deletions lib/Cookiebot.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace TMS\Theme\Base;

use TMS\Theme\Base\Interfaces\Controller;
use TMS\Theme\Base\Settings;

/**
* Class Cookiebot
Expand All @@ -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 );

}

/**
Expand Down Expand Up @@ -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;
}


}

0 comments on commit 962be41

Please sign in to comment.