From 2153737da7b5d93dc7c38fbcd52aba10508b1d4f Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Tue, 18 Nov 2008 22:31:26 +0000 Subject: [PATCH] ENHANCEMENT Added SiteTreeDecorator ENHANCEMENT Addings hooks in SiteTree for onBeforeUnpublish and onAfterUnpublish ENHANCEMENT Removed GoogleSitemap references in SiteTree (moving to decorator implementation) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@66168 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/SiteTree.php | 23 +++++++++----- core/model/SiteTreeDecorator.php | 31 +++++++++++++++++++ integration/GoogleSitemap.php | 52 +++++++++++++++++++++++++++----- 3 files changed, 92 insertions(+), 14 deletions(-) create mode 100644 core/model/SiteTreeDecorator.php diff --git a/core/model/SiteTree.php b/core/model/SiteTree.php index cdda24cfc..c825a3b9f 100644 --- a/core/model/SiteTree.php +++ b/core/model/SiteTree.php @@ -555,7 +555,7 @@ class SiteTree extends DataObject { * - canEdit() is not granted * - There are no classes defined in {@link $allowed_children} * - * @uses DataObjectDecorator->canAddChildren() + * @uses SiteTreeDecorator->canAddChildren() * @uses canEdit() * @uses $allowed_children * @@ -644,6 +644,7 @@ class SiteTree extends DataObject { * - any descendant page returns FALSE for canDelete() * * @uses canDelete() + * @uses DataObjectDecorator->canDelete() * @uses canEdit() * * @param Member $member @@ -686,6 +687,7 @@ class SiteTree extends DataObject { * Use {@link canAddChildren()} to control behaviour of creating children under this page. * * @uses $can_create + * @uses DataObjectDecorator->canCreate() * * @param Member $member * @return boolean True if the current user can create pages on this class. @@ -721,6 +723,7 @@ class SiteTree extends DataObject { * * @uses canView() * @uses EditorGroups() + * @uses DataObjectDecorator->canEdit() * * @param Member $member * @return boolean True if the current user can edit this page. @@ -768,7 +771,7 @@ class SiteTree extends DataObject { * - canPublish() on any decorator returns FALSE * - canEdit() returns FALSE * - * @uses DataObjectDecorator->canPublish() + * @uses SiteTreeDecorator->canPublish() * * @param Member $member * @return boolean True if the current user can publish this page. @@ -1361,7 +1364,10 @@ class SiteTree extends DataObject { } /** - * Publish this page + * Publish this page. + * + * @uses SiteTreeDecorator->onBeforePublish() + * @uses SiteTreeDecorator->onAfterPublish() */ function doPublish() { $original = Versioned::get_one_by_stage("SiteTree", "Live", "`SiteTree`.`ID` = $this->ID"); @@ -1376,8 +1382,6 @@ class SiteTree extends DataObject { //$this->PublishedByID = Member::currentUser()->ID; $this->write(); $this->publish("Stage", "Live"); - - GoogleSitemap::ping(); // Fix the sort order for this page's siblings DB::query("UPDATE SiteTree_Live @@ -1391,8 +1395,13 @@ class SiteTree extends DataObject { /** * Unpublish this page - remove it from the live site + * + * @uses SiteTreeDecorator->onBeforeUnpublish() + * @uses SiteTreeDecorator->onAfterUnpublish() */ function doUnpublish() { + $this->extend('onBeforeUnpublish'); + // Call delete on a cloned object so that this one doesn't lose its ID $this->flushCache(); $clone = DataObject::get_by_id("SiteTree", $this->ID); @@ -1400,8 +1409,8 @@ class SiteTree extends DataObject { $this->Status = "Unpublished"; $this->write(); - - GoogleSitemap::ping(); + + $this->extend('onAfterUnpublish'); } /** diff --git a/core/model/SiteTreeDecorator.php b/core/model/SiteTreeDecorator.php new file mode 100644 index 000000000..c2989ac35 --- /dev/null +++ b/core/model/SiteTreeDecorator.php @@ -0,0 +1,31 @@ + \ No newline at end of file diff --git a/integration/GoogleSitemap.php b/integration/GoogleSitemap.php index 3230905b5..e56b717fa 100755 --- a/integration/GoogleSitemap.php +++ b/integration/GoogleSitemap.php @@ -1,9 +1,21 @@ + * + * @see http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=34609 + * * @package sapphire * @subpackage misc */ @@ -22,7 +34,7 @@ class GoogleSitemap extends Controller { /** * @var boolean */ - protected static $pings = true; + protected static $google_notification_enabled = false; public function Items() { $this->Pages = Versioned::get_by_stage('SiteTree', 'Live'); @@ -70,13 +82,21 @@ class GoogleSitemap extends Controller { } /** + * Notifies Google about changes to your sitemap. + * Triggered automatically on every publish/unpublish of a page. + * This behaviour is disabled by default, enable with: + * GoogleSitemap::enable_google_notificaton(); + * + * If the site is in "dev-mode", no ping will be sent regardless wether + * the Google notification is enabled. + * * @return string Response text */ static function ping() { if(!self::$enabled) return false; //Don't ping if the site has disabled it, or if the site is in dev mode - if(!GoogleSitemap::$pings || Director::isDev()) + if(!GoogleSitemap::$google_notification_enabled || Director::isDev()) return; $location = urlencode(Director::absoluteBaseURL() . '/sitemap.xml'); @@ -87,15 +107,18 @@ class GoogleSitemap extends Controller { return $response; } + /** + * Enable pings to google.com whenever sitemap changes. + */ public static function enable_google_notification() { - self::$pings = true; + self::$google_notification_enabled = true; } /** * Disables pings to google when the sitemap changes. */ public static function disable_google_notification() { - self::$pings = false; + self::$google_notification_enabled = false; } function index($url) { @@ -120,4 +143,19 @@ class GoogleSitemap extends Controller { self::$enabled = false; } } -?> + +/** + * @package sapphire + * @subpackage misc + */ +class GoogleSitemapDecorator extends SiteTreeDecorator { + function onAfterPublish() { + GoogleSiteMap::ping(); + } + function onAfterUnpublish() { + GoogleSiteMap::ping(); + } +} + +Object::add_extension('SiteTree', 'GoogleSitemapDecorator'); +?> \ No newline at end of file