silverstripe-googlesitemaps/src/Extensions/GoogleSitemapExtension.php

141 lines
3.7 KiB
PHP
Raw Normal View History

<?php
2017-10-19 22:05:18 +02:00
namespace Wilr\GoogleSitemaps\Extensions;
2016-11-02 12:39:25 +01:00
use SilverStripe\Control\Director;
use SilverStripe\ORM\DataExtension;
use SilverStripe\ORM\FieldType\DBDatetime;
2017-10-19 22:05:18 +02:00
use Wilr\GoogleSitemaps\GoogleSitemap;
2016-11-02 12:39:25 +01:00
/**
2017-10-19 22:05:18 +02:00
* Decorate the page object to provide google sitemaps with additional options
* and configuration.
*/
2015-12-17 19:09:19 +01:00
class GoogleSitemapExtension extends DataExtension
{
/**
* @return boolean
*/
public function canIncludeInGoogleSitemap()
{
$can = true;
if ($this->owner->hasMethod('AbsoluteLink')) {
$hostHttp = parse_url(Director::protocolAndHost(), PHP_URL_HOST);
$objHttp = parse_url($this->owner->AbsoluteLink(), PHP_URL_HOST);
if ($objHttp != $hostHttp) {
$can = false;
}
}
2015-12-17 19:09:19 +01:00
if ($can) {
$can = $this->owner->canView();
}
if ($can) {
2018-01-18 05:00:10 +01:00
$can = ($this->owner->getGooglePriority() !== false);
2015-12-17 19:09:19 +01:00
}
2017-10-20 00:16:53 +02:00
if ($can === false) {
return false;
}
// Allow override. In this case, since this can return multiple results, we'll use an "and" based policy.
// That is if any value is false then the current value will be false. Only only if all are true will we
// then return true.
$override = $this->owner->invokeWithExtensions('alterCanIncludeInGoogleSitemap', $can);
2017-10-20 00:16:53 +02:00
if ($override) {
2017-10-20 00:16:53 +02:00
$can = min($override, $can);
}
2015-12-17 19:09:19 +01:00
return $can;
}
/**
* @return void
*/
public function onAfterPublish()
{
GoogleSitemap::ping();
}
/**
* @return void
*/
public function onAfterUnpublish()
{
GoogleSitemap::ping();
}
/**
* The default value of the priority field depends on the depth of the page in
* the site tree, so it must be calculated dynamically.
*
* @return mixed
*/
public function getGooglePriority()
{
$field = $this->owner->hasField('Priority');
2018-01-18 05:00:10 +01:00
if ($field) {
$priority = $this->owner->getField('Priority');
return ($priority < 0) ? false : $priority;
2015-12-17 19:09:19 +01:00
}
return GoogleSitemap::get_priority_for_class($this->owner->class);
}
/**
* Returns a pages change frequency calculated by pages age and number of
* versions. Google expects always, hourly, daily, weekly, monthly, yearly
2015-12-17 19:09:19 +01:00
* or never as values.
*
2015-12-17 19:09:19 +01:00
* @see http://support.google.com/webmasters/bin/answer.py?hl=en&answer=183668&topic=8476&ctx=topic
*
2016-11-02 12:39:25 +01:00
* @return DBDatetime
2015-12-17 19:09:19 +01:00
*/
public function getChangeFrequency()
{
if ($freq = GoogleSitemap::get_frequency_for_class($this->owner->class)) {
return $freq;
}
$date = date('Y-m-d H:i:s');
2016-11-02 12:39:25 +01:00
$created = new DBDatetime();
2015-12-17 19:09:19 +01:00
$created->value = ($this->owner->Created) ? $this->owner->Created : $date;
2016-11-02 12:39:25 +01:00
$now = new DBDatetime();
2015-12-17 19:09:19 +01:00
$now->value = $date;
2015-12-17 19:09:19 +01:00
$versions = ($this->owner->Version) ? $this->owner->Version : 1;
$timediff = $now->format('U') - $created->format('U');
// Check how many revisions have been made over the lifetime of the
// Page for a rough estimate of it's changing frequency.
$period = $timediff / ($versions + 1);
if ($period > 60 * 60 * 24 * 365) {
$freq = 'yearly';
} elseif ($period > 60 * 60 * 24 * 30) {
$freq = 'monthly';
} elseif ($period > 60 * 60 * 24 * 7) {
$freq = 'weekly';
} elseif ($period > 60 * 60 * 24) {
$freq = 'daily';
} elseif ($period > 60 * 60) {
$freq = 'hourly';
} else {
$freq = 'always';
}
return $freq;
}
}