Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Exclude tags from sitemap (and return no-index header on them) - Google 2023 Helpful Content Update #51

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "fof/sitemap",
"name": "sergiorodenas/flarum-sitemap",
"description": "Generate a sitemap",
"keywords": [
"extension",
Expand Down
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