From c5dde1478311ca19b31471c238d010525f38c2d1 Mon Sep 17 00:00:00 2001 From: ADTC Date: Wed, 26 Apr 2017 13:28:59 +0800 Subject: [PATCH 1/5] Add a unique identifier for blog posts and pages The unique identifier is permanently attached to the post or page and doesn't change during its lifetime. It can be used for many purposes that require identification, such as Disqus comment threads (to avoid thread duplication), redirection of old URLs, etc. Signed-off-by: ADTC --- bl-kernel/abstract/content.class.php | 7 +++++++ bl-kernel/admin/controllers/dashboard.php | 12 ++++++++++++ bl-kernel/boot/init.php | 6 ++++++ bl-kernel/dbpages.class.php | 11 +++++++++++ bl-kernel/dbposts.class.php | 6 ++++++ 5 files changed, 42 insertions(+) diff --git a/bl-kernel/abstract/content.class.php b/bl-kernel/abstract/content.class.php index c0eaf294..35eae21d 100644 --- a/bl-kernel/abstract/content.class.php +++ b/bl-kernel/abstract/content.class.php @@ -136,6 +136,12 @@ class Content { return $this->getField('readMore'); } + // Returns the unique identifier + public function uniqueId() + { + return $this->getField('uniqueId'); + } + // Returns the field key public function key() { @@ -291,6 +297,7 @@ class Content { public function json($returnsArray=false) { $tmp['key'] = $this->key(); + $tmp['uniqueId'] = $this->uniqueId(); $tmp['title'] = $this->title(); $tmp['content'] = $this->content(); // Markdown parsed $tmp['contentRaw'] = $this->contentRaw(); // No Markdown parsed diff --git a/bl-kernel/admin/controllers/dashboard.php b/bl-kernel/admin/controllers/dashboard.php index f08de032..e49d503a 100644 --- a/bl-kernel/admin/controllers/dashboard.php +++ b/bl-kernel/admin/controllers/dashboard.php @@ -32,6 +32,12 @@ function updateBludit() $checksum = md5_file(PATH_POSTS.$key.DS.FILENAME); $dbPosts->setPostDb($key, 'md5file', $checksum); } + + // Unique identifier + if( empty($post['uniqueId']) ) { + $uniqueId = generateUniqueId(); + $dbPosts->setPostDb($key, 'uniqueId', $uniqueId); + } } $dbPosts->save(); @@ -52,6 +58,12 @@ function updateBludit() $checksum = md5_file(PATH_PAGES.$key.DS.FILENAME); $dbPages->setPageDb($key, 'md5file', $checksum); } + + // Unique identifier + if( empty($page['uniqueId']) ) { + $uniqueId = generateUniqueId(); + $dbPages->setPageDb($key, 'uniqueId', $uniqueId); + } } $dbPages->save(); diff --git a/bl-kernel/boot/init.php b/bl-kernel/boot/init.php index 34fd3e5c..2e43734b 100644 --- a/bl-kernel/boot/init.php +++ b/bl-kernel/boot/init.php @@ -273,3 +273,9 @@ $Url->checkFilters( $Site->uriFilters() ); // --- Objects shortcuts --- $L = $Language; + +// Function for generating unique identifiers +function generateUniqueId() +{ + return md5(uniqid('', true)); +} diff --git a/bl-kernel/dbpages.class.php b/bl-kernel/dbpages.class.php index 0a52ce7e..8c827d0f 100644 --- a/bl-kernel/dbpages.class.php +++ b/bl-kernel/dbpages.class.php @@ -7,6 +7,7 @@ class dbPages extends dbJSON private $dbFields = array( 'title'=> array('inFile'=>true, 'value'=>''), 'content'=> array('inFile'=>true, 'value'=>''), + 'uniqueId'=> array('inFile'=>false, 'value'=>''), 'description'=> array('inFile'=>false, 'value'=>''), 'username'=> array('inFile'=>false, 'value'=>''), 'tags'=> array('inFile'=>false, 'value'=>array()), @@ -28,6 +29,11 @@ class dbPages extends dbJSON $dataForFile = array(); // This data will be saved in the file $key = $this->generateKey($args['slug'], $args['parent']); + + // Generate unique identifier + if( empty($args['uniqueId']) ) { + $args['uniqueId'] = generateUniqueId(); + } // The user is always the one loggued. $args['username'] = Session::get('username'); @@ -105,6 +111,11 @@ class dbPages extends dbJSON $dataForFile = array(); $newKey = $this->generateKey($args['slug'], $args['parent'], false, $args['key']); + + // Generate unique identifier + if( empty($args['uniqueId']) ) { + $args['uniqueId'] = generateUniqueId(); + } // The user is always the one loggued. $args['username'] = Session::get('username'); diff --git a/bl-kernel/dbposts.class.php b/bl-kernel/dbposts.class.php index 72bac5cf..40a6dbd9 100644 --- a/bl-kernel/dbposts.class.php +++ b/bl-kernel/dbposts.class.php @@ -5,6 +5,7 @@ class dbPosts extends dbJSON private $dbFields = array( 'title'=> array('inFile'=>true, 'value'=>''), 'content'=> array('inFile'=>true, 'value'=>''), + 'uniqueId'=> array('inFile'=>false, 'value'=>''), 'description'=> array('inFile'=>false, 'value'=>''), 'username'=> array('inFile'=>false, 'value'=>''), 'status'=> array('inFile'=>false, 'value'=>'draft'), // published, draft, scheduled @@ -113,6 +114,11 @@ class dbPosts extends dbJSON // Generate the database key / index $key = $this->generateKey($args['slug']); + + // Generate unique identifier + if( empty($args['uniqueId']) ) { + $args['uniqueId'] = generateUniqueId(); + } // The user is always who is loggued $args['username'] = Session::get('username'); From ad3ee5608f09f673feea31ba32bb19be5328d110 Mon Sep 17 00:00:00 2001 From: ADTC Date: Wed, 26 Apr 2017 13:33:11 +0800 Subject: [PATCH 2/5] Insert unique ID, permalink and title in Disqus JS code This avoids thread splitting, duplication, and other problems in Disqus, as discussed at: https://help.disqus.com/customer/en/portal/articles/2158629 Signed-off-by: ADTC --- bl-plugins/disqus/plugin.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/bl-plugins/disqus/plugin.php b/bl-plugins/disqus/plugin.php index d5861887..9016c50b 100644 --- a/bl-plugins/disqus/plugin.php +++ b/bl-plugins/disqus/plugin.php @@ -98,6 +98,30 @@ class pluginDisqus extends Plugin { public function siteBodyEnd() { + global $Page, $Post, $Url, $posts; + + switch($Url->whereAmI()) + { + case 'post': + $absolutePermalink = $Post->permalink(true); + $uniqueId = $Post->uniqueId(); + $disqusTitle = $Post->title(); + break; + case 'page': + $absolutePermalink = $Page->permalink(true); + $uniqueId = $Page->uniqueId(); + $disqusTitle = $Page->title(); + break; + + default: + // Homepage - use the first post + if(isset($posts[0])) { + $absolutePermalink = $posts[0]->permalink(true); + $uniqueId = $posts[0]->uniqueId(); + $disqusTitle = $posts[0]->title(); + } + } + if( $this->enable ) { $html = ' @@ -105,6 +129,12 @@ class pluginDisqus extends Plugin { var disqus_shortname = "'.$this->getDbField('shortname').'"; + var disqus_config = function () { + this.page.url = "'.$absolutePermalink.'"; + this.page.identifier = "'.$uniqueId.'"; + this.page.title = "'.$disqusTitle.'"; + }; + (function() { var dsq = document.createElement("script"); dsq.type = "text/javascript"; dsq.async = true; dsq.src = "//" + disqus_shortname + ".disqus.com/embed.js"; From 452ff207605a03eb6222130d5bf97fe4be2090a5 Mon Sep 17 00:00:00 2001 From: ADTC Date: Wed, 26 Apr 2017 13:35:46 +0800 Subject: [PATCH 3/5] Fix a mistake in dashboard.php ($post instead of $page) Signed-off-by: ADTC --- bl-kernel/admin/controllers/dashboard.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bl-kernel/admin/controllers/dashboard.php b/bl-kernel/admin/controllers/dashboard.php index e49d503a..11b046a4 100644 --- a/bl-kernel/admin/controllers/dashboard.php +++ b/bl-kernel/admin/controllers/dashboard.php @@ -54,7 +54,7 @@ function updateBludit() } // Checksum - if( empty($post['md5file']) ) { + if( empty($page['md5file']) ) { $checksum = md5_file(PATH_PAGES.$key.DS.FILENAME); $dbPages->setPageDb($key, 'md5file', $checksum); } From 2886fc5f98ef58395f97c8595b4091fdb99033be Mon Sep 17 00:00:00 2001 From: ADTC Date: Wed, 26 Apr 2017 13:37:38 +0800 Subject: [PATCH 4/5] Reverse the defaults in Disqus plugin settings Pages and posts to have commments by default, while home page to not. Signed-off-by: ADTC --- bl-plugins/disqus/plugin.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bl-plugins/disqus/plugin.php b/bl-plugins/disqus/plugin.php index 9016c50b..c0e7bd1a 100644 --- a/bl-plugins/disqus/plugin.php +++ b/bl-plugins/disqus/plugin.php @@ -8,9 +8,9 @@ class pluginDisqus extends Plugin { { $this->dbFields = array( 'shortname'=>'', - 'enablePages'=>0, - 'enablePosts'=>0, - 'enableDefaultHomePage'=>1 + 'enablePages'=>1, + 'enablePosts'=>1, + 'enableDefaultHomePage'=>0 ); } From f29811307f3dcec74fc9e668206eaae73076c266 Mon Sep 17 00:00:00 2001 From: ADTC Date: Wed, 26 Apr 2017 16:45:36 +0800 Subject: [PATCH 5/5] Add unique identifier as hidden field in edit post/page This ensures the ID does not change when the post or page is edited. Signed-off-by: ADTC --- bl-kernel/admin/views/edit-page.php | 6 ++++++ bl-kernel/admin/views/edit-post.php | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/bl-kernel/admin/views/edit-page.php b/bl-kernel/admin/views/edit-page.php index de2429b0..8c78516c 100644 --- a/bl-kernel/admin/views/edit-page.php +++ b/bl-kernel/admin/views/edit-page.php @@ -16,6 +16,12 @@ HTML::formOpen(array('class'=>'uk-form-stacked')); 'value'=>$_Page->key() )); + // Unique identifier + HTML::formInputHidden(array( + 'name'=>'uniqueId', + 'value'=>$_Page->uniqueId() + )); + // LEFT SIDE // -------------------------------------------------------------------- echo '
'; diff --git a/bl-kernel/admin/views/edit-post.php b/bl-kernel/admin/views/edit-post.php index 94e78311..241696a5 100644 --- a/bl-kernel/admin/views/edit-post.php +++ b/bl-kernel/admin/views/edit-post.php @@ -16,6 +16,12 @@ HTML::formOpen(array('class'=>'uk-form-stacked')); 'value'=>$_Post->key() )); + // Unique identifier + HTML::formInputHidden(array( + 'name'=>'uniqueId', + 'value'=>$_Post->uniqueId() + )); + // LEFT SIDE // -------------------------------------------------------------------- echo '
';