Skip to content

Commit 1b5174d

Browse files
authored
Merge pull request #17 from catalyst/45-fixups
Make 45 branch
2 parents 61abb47 + 4b78b64 commit 1b5174d

File tree

12 files changed

+138
-211
lines changed

12 files changed

+138
-211
lines changed

.travis.yml

Lines changed: 0 additions & 57 deletions
This file was deleted.

README.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
<a href="https://github.com/catalyst/moodle-tool_seo/actions">
2-
<img src="https://github.com/catalyst/moodle-tool_seo/actions/workflows/ci.yml/badge.svg" >
3-
</a>
1+
[![ci](https://github.com/catalyst/moodle-tool_seo/actions/workflows/ci.yml/badge.svg?branch=MOODLE_405_STABLE)](https://github.com/[user]/[plugin]/actions/workflows/ci.yml?branch=[branch])
42

53
# Moodle SEO admin tool
6-
74
This is an admin tool to help manage SEO issues.
85

9-
106
## noindex pages
11-
127
A simple way to manage the list of pages which are public but we do not want to ever appear in search results.
138

9+
# Branches
1410

15-
16-
17-
11+
| Moodle version | Branch | PHP |
12+
|--------------------|-------------------|-----------|
13+
| Moodle 4.5+ | MOODLE_405_STABLE | 8.1 - 8.3 |
14+
| Moodle 4.1 - 4.4 | MOODLE_401_STABLE | 7.4 |

classes/local/hook/after_config.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace tool_seo\local\hook;
18+
19+
/**
20+
* Hook for after_config
21+
*
22+
* @package tool_seo
23+
* @author Matthew Hilton <matthewhilton@catalyst-au.net>
24+
* @copyright 2025 Catalyst IT
25+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26+
*/
27+
class after_config {
28+
/**
29+
* Callback called after_config
30+
* @param \core\hook\after_config $hook hook data (unused)
31+
*/
32+
public static function callback(\core\hook\after_config $hook): void {
33+
\tool_seo\robots::serve();
34+
}
35+
}

classes/local/hook/output/before_standard_head_html_generation.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
namespace tool_seo\local\hook\output;
1818

19+
use tool_seo\local\seo;
20+
1921
/**
2022
* Allows plugins to add any elements to the page <head> html tag
2123
*
@@ -29,13 +31,13 @@ class before_standard_head_html_generation {
2931
/**
3032
* Callback that adds a custom html header tag to each page.
3133
*
32-
* @param \core\hook\output\standard_head_html_prepend $hook
34+
* @param \core\hook\output\before_standard_head_html_generation $hook
3335
*/
3436
public static function callback(\core\hook\output\before_standard_head_html_generation $hook): void {
3537
GLOBAL $PAGE;
3638

3739
// If URL should not be indexed, add the noindex meta tag to page.
38-
if (tool_seo_is_url_indexable($PAGE->url->get_path()) == false) {
40+
if (seo::is_url_indexable($PAGE->url->get_path()) == false) {
3941
$hook->add_html('<meta name="robots" content="noindex, nofollow" />');
4042
}
4143
}

classes/local/seo.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace tool_seo\local;
18+
19+
use dml_exception;
20+
21+
/**
22+
* SEO utils class.
23+
*
24+
* @package tool_seo
25+
* @author Andrew Madden <andrewmadden@catalyst-au.net>
26+
* @copyright 2019 Catalyst IT
27+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28+
*/
29+
class seo {
30+
/**
31+
* Checks whether the current URL is listed in the URLs to not be indexed in admin tools.
32+
*
33+
* @param string $currentpath The path component of the current URL to compare against.
34+
* @return bool Returns true if the URL should be indexable by search engines.
35+
*/
36+
public static function is_url_indexable($currentpath): bool {
37+
38+
// Get the list of URLs to be excluded from the admin settings.
39+
try {
40+
$nonindexableurlstring = get_config('tool_seo', 'nonindexable');
41+
} catch (dml_exception $e) {
42+
return true;
43+
}
44+
45+
$nonindexableurls = array_map('trim', explode(PHP_EOL, $nonindexableurlstring));
46+
47+
$currentpathliteral = preg_quote($currentpath, '/');
48+
49+
foreach ($nonindexableurls as $nonindexableurl) {
50+
// If the url is empty, ignore.
51+
if ($nonindexableurl == '') {
52+
continue;
53+
}
54+
55+
// Checks if a non-indexable url is part of the current path.
56+
if (strpos($currentpath, $nonindexableurl) !== false) {
57+
return false;
58+
}
59+
60+
// Checks if the current path is a slug of the non-indexable url.
61+
if (preg_match("/^.*$currentpathliteral$/", $nonindexableurl)) {
62+
return false;
63+
}
64+
}
65+
66+
return true;
67+
}
68+
}

classes/privacy/provider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class provider implements \core_privacy\local\metadata\null_provider {
4040
*
4141
* @return string
4242
*/
43-
public static function get_reason() : string {
43+
public static function get_reason(): string {
4444
return 'privacy:metadata';
4545
}
4646
}

db/hooks.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,9 @@
3131
'callback' => 'tool_seo\local\hook\output\before_standard_head_html_generation::callback',
3232
'priority' => 0,
3333
],
34+
[
35+
'hook' => \core\hook\after_config::class,
36+
'callback' => 'tool_seo\local\hook\after_config::callback',
37+
'priority' => 0,
38+
],
3439
];

lang/en/tool_seo.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,9 @@
2323
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2424
*/
2525

26-
$string['pluginname'] = 'SEO';
27-
28-
// Settings.
2926
$string['nonindexable'] = 'Include URLs not to be indexed by search engines.';
3027
$string['nonindexable_help'] = 'List of URLs that will not be indexed and profiled by search engines. Enter each URL on a new line.';
28+
$string['pluginname'] = 'SEO';
29+
$string['privacy:metadata'] = 'The SEO plugin does not store any personal data.';
3130
$string['robotstxt'] = 'robots.txt';
3231
$string['robotstxt_help'] = 'The raw robots.txt file can be entered here. For this to work you must have Moodle setup to handle error pages.';
33-
34-
// Privacy.
35-
$string['privacy:metadata'] = 'The SEO plugin does not store any personal data.';
36-

lib.php

Lines changed: 0 additions & 54 deletions
This file was deleted.

locallib.php

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)