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

V5 #23

Merged
merged 9 commits into from
Nov 26, 2024
Merged

V5 #23

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
24 changes: 24 additions & 0 deletions .github/workflows/fix-php-code-style-issues.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Fix PHP code style issues

on:
push:
paths:
- '**.php'

jobs:
php-code-styling:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}

- name: Fix PHP code style issues
uses: aglipanci/laravel-pint-action@2.3.0

- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Fix styling
45 changes: 45 additions & 0 deletions .github/workflows/pest-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Pest Tests

on:
workflow_dispatch:
push:
paths:
- "**.php"
- "composer.lock"
pull_request:
paths:
- "**.php"
- "composer.lock"

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
tools: composer:v2
coverage: xdebug

- name: Cache composer dependencies
uses: actions/cache@v4
with:
path: vendor
key: composer-${{ hashFiles('composer.lock') }}

- name: Install
run: composer kirby

- name: Test & publish code coverage
uses: paambaati/codeclimate-action@v8.0.0
with:
coverageCommand: vendor/bin/pest --ci
coverageLocations: ${{github.workspace}}/tests/clover.xml:clover
env:
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}

34 changes: 34 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: PHPStan

on:
push:
paths:
- '**.php'
- 'phpstan.neon.dist'

jobs:
phpstan:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
tools: composer:v2
coverage: none

- name: Cache composer dependencies
uses: actions/cache@v4
with:
path: vendor
key: composer-${{ hashFiles('composer.lock') }}

- name: Install
run: composer kirby

- name: Run PHPStan
run: vendor/bin/phpstan --error-format=github
52 changes: 0 additions & 52 deletions .github/workflows/test.yml

This file was deleted.

45 changes: 28 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,48 @@
# Kirby Robots.txt

![Release](https://flat.badgen.net/packagist/v/bnomei/kirby3-robots-txt?color=ae81ff)
![Downloads](https://flat.badgen.net/packagist/dt/bnomei/kirby3-robots-txt?color=272822)
[![Coverage](https://flat.badgen.net/codeclimate/coverage/bnomei/kirby3-robots-txt)](https://codeclimate.com/github/bnomei/kirby3-robots-txt)
[![Maintainability](https://flat.badgen.net/codeclimate/maintainability/bnomei/kirby3-robots-txt)](https://codeclimate.com/github/bnomei/kirby3-robots-txt)
[![Discord](https://flat.badgen.net/badge/discord/bnomei?color=7289da)](https://discordapp.com/users/bnomei)


Manage a virtual [robots.txt](https://developers.google.com/search/reference/robots_txt) from the Kirby config file.
[![Kirby 5](https://flat.badgen.net/badge/Kirby/5?color=ECC748)](https://getkirby.com)
![PHP 8.2](https://flat.badgen.net/badge/PHP/8.2?color=4E5B93&icon=php&label)
![Release](https://flat.badgen.net/packagist/v/bnomei/kirby3-robots-txt?color=ae81ff&icon=github&label)
![Downloads](https://flat.badgen.net/packagist/dt/bnomei/kirby3-robots-txt?color=272822&icon=github&label)
[![Coverage](https://flat.badgen.net/codeclimate/coverage/bnomei/kirby3-robots-txt?icon=codeclimate&label)](https://codeclimate.com/github/bnomei/kirby3-robots-txt)
[![Maintainability](https://flat.badgen.net/codeclimate/maintainability/bnomei/kirby3-robots-txt?icon=codeclimate&label)](https://codeclimate.com/github/bnomei/kirby3-robots-txt/issues)
[![Discord](https://flat.badgen.net/badge/discord/bnomei?color=7289da&icon=discord&label)](https://discordapp.com/users/bnomei)
[![Buymecoffee](https://flat.badgen.net/badge/icon/donate?icon=buymeacoffee&color=FF813F&label)](https://www.buymeacoffee.com/bnomei)

Installing the plugin is enough to get a virtual [robots.txt](https://developers.google.com/search/reference/robots_txt) file.

## Installation

- unzip [master.zip](https://github.com/bnomei/kirby3-robots-txt/archive/master.zip) as folder `site/plugins/kirby3-robots-txt` or
- `git submodule add https://github.com/bnomei/kirby3-robots-txt.git site/plugins/kirby3-robots-txt` or
- `composer require bnomei/kirby3-robots-txt`

## Staging Server? Debug Mode = Disallow all
## Zero-Configuration

The plugin will work out of the box without the need to configure anything. It will generate a virtual `robots.txt` file with the following content.

```txt
User-agent: *
Disallow: /kirby/
Disallow: /site/
Disallow: /cdn-cgi/
Allow: /media/
```

Setting the global Kirby `debug` configuration to `true` will prevent all indexing for every user agent. This is particularly useful on staging servers, but you may also want to consider it for XML sitemaps and RSS feeds, among other things.
## Active debug mode will block indexing by search engines

> [!WARNING]
> This means if you have Kirby's debug mode enabled in production, all search engines will be blocked from indexing your site!
> [!WARNING]
> Setting the global Kirby `debug` configuration to `true` will prevent all indexing for every user agent (all Search Engines).

## Adding Sitemap Link to Robots.txt
## Link to Sitemap.xml

This plugin will add the sitemap link **automatically** to the virtual `robots.txt` file for most available SEO plugins. In that case, you can skip setting the `bnomei.robots-txt.sitemap` config value to `sitemap.xml`.
This plugin will add the link to your `sitemap.xml` **automatically** for most available SEO plugins. In that case, you can skip setting the `bnomei.robots-txt.sitemap` config value to `sitemap.xml`.

## Setup
## Optional Manual Configuration

The plugin generates automatic defaults for the starterkit. You do not have to enter them in the config file. But if you would, it would look like this.
The plugin generates automatic defaults optimized for the official [Kirby Starterkit](https://github.com/getkirby/starterkit). You do not have to enter any of them in the config file. But if you would, they would look like this.

**defaults for starterkit**
**defaults for Kirby Starterkit**
```php
<?php
return [
Expand Down
102 changes: 38 additions & 64 deletions classes/Robotstxt.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,10 @@

final class Robotstxt
{
/**
* @var string[]
*/
private $txt;

/**
* @var array
*/
private $options;

/**
* Robotstxt constructor.
*/
public function __construct(array $options = [])
{
$this->txt = [];

public function __construct(
private array $options = [],
private array $txt = [],
) {
$defaults = [
'debug' => option('debug'),
'content' => option('bnomei.robots-txt.content'),
Expand All @@ -39,9 +26,14 @@ public function __construct(array $options = [])
}
}

$this->addContent(A::get($this->options, 'content'));
$this->addGroups(A::get($this->options, 'groups'));
$this->addSitemap(A::get($this->options, 'sitemap'));
$this->addContent($this->option('content'))
->addGroups($this->option('groups'))
->addSitemap($this->option('sitemap'));
}

public function option(string $key): mixed
{
return A::get($this->options, $key);
}

public function toArray(): ?array
Expand All @@ -54,78 +46,60 @@ public function toTxt(): ?string
return count($this->txt) ? implode(PHP_EOL, $this->txt).PHP_EOL : null;
}

/**
* @param null $content
*/
private function addContent($content = null): Robotstxt
private function addContent(mixed $content = null): self
{
if (! $content) {
if (empty($content)) {
return $this;
}
$this->txt[] = (string) $content;
if (is_string($content)) {
$this->txt[] = $content;
}

return $this;
}

/**
* @param null $groups
*/
private function addGroups($groups = null): Robotstxt
private function addGroups(mixed $groups = null): self
{
if (! $groups) {
if (empty($groups)) {
return $this;
}
if (A::get($this->options, 'debug')) {
if ($this->option('debug')) {
$groups = ['*' => ['disallow' => ['/']]];
}
if (! is_array($groups) && ! is_string($groups) && is_callable($groups)) {
$groups = $groups();
}
if (is_array($groups)) {
foreach ($groups as $useragent => $group) {
$this->txt[] = 'user-agent: '.$useragent;
if (! is_array($group)) {
continue;
}
foreach ($group as $field => $values) {
if (! is_array($values)) {
continue;
}
foreach ($values as $value) {
$this->txt[] = $field.': '.$value;
$this->txt[] = implode('', [$field, ': ', $value]);
}
}
}
} else {
$this->txt[] = (string) $groups;
} elseif (is_string($groups)) {
$this->txt[] = $groups;
}

return $this;
}

private function hasSitemapFromKnownPlugin(): bool
{
if (option('omz13.xmlsitemap.disable') === false) {
return true;
}
if (option('fabianmichael.meta.sitemap') === true) {
return true;
}
if (option('tobimori.seo.robots.active') === false) {
return true;
}
if (option('johannschopplich.helpers.sitemap.enable') === true && option('johannschopplich.helpers.robots.enable') === false) {
return true;
}
if (option('kirbyzone.sitemapper.customMap') instanceof \Closure) {
return true;
}
$feedPlugin = kirby()->plugin('bnomei/feed');
if ($feedPlugin && option('bnomei.feed.sitemap.enable') === true && version_compare($feedPlugin->version(), '1.4.0', '>=')) {
return true;
}

return false;
return count(array_filter([
option('omz13.xmlsitemap.disable') === false,
option('fabianmichael.meta.sitemap') === true,
option('tobimori.seo.robots.active') === false,
option('johannschopplich.helpers.sitemap.enable') === true && option('johannschopplich.helpers.robots.enable') === false,
option('bnomei.feed.sitemap.enable') === true,
])) > 0;
}

/**
* @param null $sitemap
*/
private function addSitemap($sitemap = null): Robotstxt
private function addSitemap(mixed $sitemap = null): self
{
// @codeCoverageIgnoreStart
if ($this->hasSitemapFromKnownPlugin()) {
Expand All @@ -135,7 +109,7 @@ private function addSitemap($sitemap = null): Robotstxt
}
// @codeCoverageIgnoreEnd

if (! $sitemap) {
if (! is_string($sitemap)) {
return $this;
}

Expand Down
Loading