]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/Disqus/DisqusPlugin.php
Merge branch 'testing' of gitorious.org:statusnet/mainline into testing
[quix0rs-gnu-social.git] / plugins / Disqus / DisqusPlugin.php
index ec5857dd7453945c90064cd01689cb7e48ec45dc..add089e77f5a9aeef2aba1be8ac38f693614e5ce 100644 (file)
@@ -52,16 +52,19 @@ if (!defined('STATUSNET')) {
  * );
  *
  * If you only want to allow commenting on a specific user's notices or
- * a specific set of user's notices, use the "nicknames" array, e.g.:
+ * a specific set of users' notices initialize the plugin with the "restricted"
+ * parameter and grant the "richedit" role to those users. E.g.:
  *
  * addPlugin(
  *     'Disqus', array(
- *         'shortname' => 'YOURSHORTNAME',
- *         'divStyle'  => 'width:675px; padding-top:10px; position:relative; float:left;',
- *         'nicknames' => array('spock', 'kirk', 'bones')
+ *         'shortname'  => 'YOURSHORTNAME',
+ *         'divStyle'   => 'width:675px; padding-top:10px; position:relative; float:left;',
+ *         'restricted' => true
  *     )
  * );
  *
+ * $ php userrole.php -s#### -nusername -rrichedit
+ *
  *
  * NOTE: the 'divStyle' in an optional parameter that passes in some
  * inline CSS when creating the Disqus widget. It's a shortcut to make
@@ -85,7 +88,11 @@ class DisqusPlugin extends Plugin
 {
     public $shortname; // Required 'shortname' for actually triggering Disqus
     public $divStyle;  // Optional CSS chunk for the main <div>
-    public $nicknames; // Optional array of nicks to restrict commenting to (default on for all users)
+
+    // By default, Disqus commenting will be available to all users.
+    // With restricted on, only users who have been granted the
+    // "richedit" role get it.
+    public $restricted = false;
 
     /**
      * Add a Disqus commenting section to the end of an individual
@@ -99,7 +106,7 @@ class DisqusPlugin extends Plugin
 
             $profile = Profile::staticGet('id', $action->notice->profile_id);
 
-            if ($this->hasCommenting($profile)) {
+            if ($this->isAllowedRichEdit($profile)) {
 
                 $attrs = array();
                 $attrs['id'] = 'disqus_thread';
@@ -132,6 +139,7 @@ ENDOFSCRIPT;
                 $action->elementStart('div', $attrs);
                 $action->elementStart('noscript');
 
+                // TRANS: User notification that JavaScript is required for Disqus comment display.
                 $noScriptMsg = sprintf(_m("Please enable JavaScript to view the [comments powered by Disqus](http://disqus.com/?ref_noscript=%s)."), $this->shortname);
                 $output = common_markup_to_html($noScriptMsg);
                 $action->raw($output);
@@ -139,6 +147,7 @@ ENDOFSCRIPT;
                 $action->elementEnd('noscript');
 
                 $action->elementStart('a', array('href' => 'http://disqus.com', 'class' => 'dsq-brlink'));
+                // TRANS: This message is followed by a Disqus logo. Alt text is "Disqus".
                 $action->raw(_m('Comments powered by '));
                 $action->element('span', array('class' => 'logo-disqus'), 'Disqus');
                 $action->elementEnd('a');
@@ -169,68 +178,53 @@ ENDOFSCRIPT;
     }
 
     /**
-     * Override the default Notice display to add Disqus comments link
+     * Tack on a Disqus comments link to the notice options stanza
      * (the link displays the total number of comments for each notice)
      *
      * @param NoticeListItem $noticeListItem
      *
-     * @return boolean override
      */
-    function onStartShowNoticeItem($noticeListItem)
+    function onEndShowNoticeInfo($noticeListItem)
     {
         // Don't enable commenting for remote notices
         if (empty($noticeListItem->notice->is_local)) {
-            return true;
+            return;
         }
 
         $profile = Profile::staticGet('id', $noticeListItem->notice->profile_id);
 
-        if ($this->hasCommenting($profile)) {
-
-            // @todo Refactor individual notice display to have it's own event hooks
-
-            $noticeListItem->showNotice();
-            $noticeListItem->showNoticeInfo();
-
+        if ($this->isAllowedRichEdit($profile)) {
             $noticeUrl = $noticeListItem->notice->bestUrl();
             $noticeUrl .= '#disqus_thread';
 
             $noticeListItem->out->element(
-                'a', array('href' => $noticeUrl, 'class' => 'disqus_count'), 'Comments'
+                'a',
+                array('href' => $noticeUrl, 'class' => 'disqus_count'),
+                // TRANS: Plugin supplied feature for Disqus comments to notices.
+                _m('Comments')
             );
-
-            $noticeListItem->showNoticeOptions();
-            Event::handle('EndShowNoticeItem', array($noticeListItem));
-
-            return false;
-        } else {
-            return true;
         }
     }
 
     /**
-     * Helper to check whether commenting should be enabled
-     * for a given notice
+     * Does the current user have permission to use the Disqus plugin?
+     * Always true unless the plugin's "restricted" setting is on, in which
+     * case it's limited to users with the "richedit" role.
      *
-     * Assumes commenting should be enabled, unless the
-     * nicknames array is populated
+     * @fixme make that more sanely configurable :)
      *
      * @param Profile $profile the profile to check
      *
-     * @return boolean true if yes
+     * @return boolean
      */
-    private function hasCommenting($profile)
+    private function isAllowedRichEdit($profile)
     {
-        if (!empty($this->nicknames)) {
-            foreach ($this->nicknames as $nickname) {
-                if ($profile->nickname == $nickname) {
-                    return true;
-                }
-            }
-            return false;
+        if ($this->restricted) {
+            $user = User::staticGet($profile->id);
+            return !empty($user) && $user->hasRole('richedit');
+        } else {
+            return true;
         }
-
-        return true;
     }
 
     /**
@@ -247,6 +241,7 @@ ENDOFSCRIPT;
                             'author' => 'Zach Copley',
                             'homepage' => 'http://status.net/wiki/Plugin:Disqus',
                             'rawdescription' =>
+                            // TRANS: Plugin description.
                             _m('Use <a href="http://disqus.com/">Disqus</a>'.
                                ' to add commenting to notice pages.'));
         return true;