Skip to content

Commit

Permalink
feat: implement Sitemapable (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Oct 29, 2023
1 parent def6be3 commit 8a7c8aa
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 95 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

## Maintainer Contact

* Will Rossiter (Nickname: wrossiter, willr) <[email protected]>
- Will Rossiter (Nickname: wrossiter, willr) <[email protected]>

## Installation

> composer require "wilr/silverstripe-googlesitemaps"
If you're using Silverstripe 5 then version 3 or `dev-main` will work.
If you're using Silverstripe 5 then version `3` or `dev-main` will work.

For Silverstripe 4 use the `2.x` branch line.

Expand All @@ -34,4 +34,4 @@ See docs/en for more information about configuring the module.

## Troubleshooting

* Flush this route to ensure the changes take effect (e.g http://yoursite.com/sitemap.xml?flush=1)
- Flush this route to ensure the changes take effect (e.g http://yoursite.com/sitemap.xml?flush=1)
10 changes: 10 additions & 0 deletions _config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use SilverStripe\Core\ClassInfo;
use Wilr\GoogleSitemaps\GoogleSitemap;

if (0 === strpos(ltrim($_SERVER['REQUEST_URI'], '/'), 'sitemap')) {
foreach (ClassInfo::implementorsOf(Sitemapable::class) as $className) {
GoogleSitemap::register_dataobject($className);
}
}
103 changes: 55 additions & 48 deletions docs/en/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,42 +30,32 @@ manually, including requesting to have the page excluded from the sitemap.
Most module configuration is done via the SilverStripe Config API. Create a new
config file `mysite/_config/googlesitemaps.yml` with the following outline:

---
Name: customgooglesitemaps
After: googlesitemaps
---
Wilr\GoogleSitemaps\GoogleSitemap:
enabled: true
objects_per_sitemap: 1000
google_notification_enabled: false
use_show_in_search: true
---
Name: customgooglesitemaps
After: googlesitemaps
---
Wilr\GoogleSitemaps\GoogleSitemap:

enabled: true
objects_per_sitemap: 1000
google_notification_enabled: false
use_show_in_search: true

You can now alter any of those properties to set your needs. A popular option
is to turn on automatic pinging so that Google is notified of any updates to
your page. You can set this in the file we created in the last paragraph by
editing the `google_notification_enabled` option to true

---
Name: customgooglesitemaps
After: googlesitemaps
---
Wilr\GoogleSitemaps\GoogleSitemap:
enabled: true
objects_per_sitemap: 1000
google_notification_enabled: true
use_show_in_search: true

### Bing Ping Support

To ping Bing whenever your sitemap is updated, set `bing_notification_enabled`

---
Name: customgooglesitemaps
After: googlesitemaps
---
Wilr\GoogleSitemaps\GoogleSitemap:
enabled: true
bing_notification_enabled: true

enabled: true
objects_per_sitemap: 1000
google_notification_enabled: true
use_show_in_search: true

### Including DataObjects

Expand All @@ -76,49 +66,48 @@ database as DataObject subclasses.
To include a DataObject instance in the Sitemap it requires that your subclass
defines two functions:

* AbsoluteLink() function which returns the URL for this DataObject
* canView() function which returns a boolean value.
- AbsoluteLink() function which returns the URL for this DataObject
- canView() function which returns a boolean value.

The following is a barebones example of a DataObject called 'MyDataObject'. It
assumes that you have a controller called 'MyController' which has a show method
to show the DataObject by its ID.

<?php
<?php

use SilverStripe\ORM\DataObject;
use SilverStripe\Control\Director;

class MyDataObject extends DataObject {
class MyDataObject extends DataObject {

function canView($member = null) {
return true;
}
function canView($member = null) {
return true;
}

function AbsoluteLink() {
return Director::absoluteURL($this->Link());
}

function Link() {
return 'MyController/show/'. $this->ID;
}
}
function AbsoluteLink() {
return Director::absoluteURL($this->Link());
}

function Link() {
return 'MyController/show/'. $this->ID;
}
}

After those methods have been defined on your DataObject you now need to tell
the Google Sitemaps module that it should be listed in the sitemap.xml file. To
do that, include the following in your _config.php file.
do that, include the following in your \_config.php file.

use Wilr\GoogleSitemaps\GoogleSitemap;

GoogleSitemap::register_dataobject('MyDataObject');
GoogleSitemap::register_dataobject('MyDataObject');

If you need to change the frequency of the indexing, you can pass the change
frequency (daily, weekly, monthly) as a second parameter to register_dataobject(), So
instead of the previous code you would write:

use Wilr\GoogleSitemaps\GoogleSitemap;

GoogleSitemap::register_dataobject('MyDataObject', 'daily');
GoogleSitemap::register_dataobject('MyDataObject', 'daily');

See the following blog post for more information:

Expand All @@ -133,8 +122,26 @@ urls to include.

use Wilr\GoogleSitemaps\GoogleSitemap;

GoogleSitemap::register_routes(array(
'/my-custom-controller/',
'/Security/',
'/Security/login/'
));
GoogleSitemap::register_routes(array(
'/my-custom-controller/',
'/Security/',
'/Security/login/'
));

### Sitemapable

For automatic registration of a DataObject subclass, implement the `Sitemapable`
extension

```
<?php
class MyDataObject extends DataObject implements Sitemapable
{
public function AbsoluteLink()
{
// ..
}
}
```
5 changes: 3 additions & 2 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="SilverStripe">
<description>CodeSniffer ruleset for SilverStripe coding conventions.</description>

<file>src</file>
<file>tests</file>
<!-- base rules are PSR-2 -->
<rule ref="PSR2" >
<rule ref="PSR2">
<!-- Current exclusions -->
<exclude name="PSR1.Methods.CamelCapsMethodName" />
</rule>
Expand Down
3 changes: 1 addition & 2 deletions src/Control/GoogleSitemapController.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ public function sitemap()
}
}

if (
GoogleSitemap::enabled()
if (GoogleSitemap::enabled()
&& $class
&& ($page > 0)
&& ($class == SiteTree::class || $class == 'GoogleSitemapRoute' || GoogleSitemap::is_registered($class))
Expand Down
3 changes: 1 addition & 2 deletions src/GoogleSitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ class GoogleSitemap
* @var array
*/
private static $search_indexes = [
'google' => 'http://www.google.com/webmasters/sitemaps/ping?sitemap=',
'bing' => 'http://www.bing.com/ping?sitemap=',
'google' => 'http://www.google.com/webmasters/sitemaps/ping?sitemap='
];

/**
Expand Down
14 changes: 14 additions & 0 deletions src/Sitemapable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php


namespace Wilr\GoogleSitemaps;

interface Sitemapable
{
/**
* Return the absolute URL for this object
*
* @return string
*/
public function AbsoluteLink();
}
Loading

0 comments on commit 8a7c8aa

Please sign in to comment.