Skip to content

Commit

Permalink
Sitemap condition and no-index
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiorodenas committed Oct 26, 2023
1 parent aecb314 commit 7144757
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
3 changes: 3 additions & 0 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,7 @@

(new Extend\Event())
->subscribe(Listeners\SettingsListener::class),

(new Extend\Frontend('forum'))
->content(Listeners\NoIndexListener::class),
];
49 changes: 49 additions & 0 deletions src/Listeners/NoIndexListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of fof/sitemap.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/

namespace FoF\Sitemap\Listeners;

use Flarum\Frontend\Document;
use Flarum\Settings\SettingsRepositoryInterface;
use Psr\Http\Message\ServerRequestInterface;
use Flarum\Extension\ExtensionManager;
use Illuminate\Support\Arr;

class NoIndexListener
{
public function __construct(protected ExtensionManager $extensionManager)
{}

public function __invoke(Document $document, ServerRequestInterface $request)
{
$noIndexTags = [38, 33, 35, 36]; // Add tags here so their discussions include the no-index

if( ! $this->extensionManager->isEnabled('flarum-tags')){
return;
}

$type = Arr::get($document->getForumApiDocument(), 'data.type');

if($type != 'discussions'){
return;
}

$tags = Arr::get($document->getForumApiDocument(), 'data.relationships.tags.data');

foreach($tags as $tag){
if(in_array($tag['id'], $noIndexTags)){
$document->head[] = '<meta name="robots" content="noindex">';
return;
}
}
}
}
6 changes: 6 additions & 0 deletions src/Resources/Discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public function query(): Builder
{
$query = Model::whereVisibleTo(new Guest());

if(static::$extensionManager->isEnabled('flarum-tags')){
$query->whereDoesntHave('tags', function($query){
$query->whereIn('id', [38, 33, 35, 36]); // Add tags here so their discussions aren't included in sitemap
});
}

if (static::$settings->get('fof-sitemap.riskyPerformanceImprovements')) {
// Limiting the number of columns to fetch improves query time
// This is a risky optimization because of 2 reasons:
Expand Down

0 comments on commit 7144757

Please sign in to comment.