Add like and bookmark buttons
authorChristian Weiske <cweiske@cweiske.de>
Tue, 30 May 2017 21:39:10 +0000 (23:39 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Tue, 30 May 2017 21:39:10 +0000 (23:39 +0200)
commentform.phtml
init.php

index 636fa51252a7094eeac86f2f27aecbf7fa2214b9..d1ffd8275ed3229c9cd7d1404cc79c09e9452134 100644 (file)
   <?php print_hidden('plugin', 'micropub'); ?>
   <?php print_hidden('method', 'action'); ?>
   <?php print_hidden('mode', 'post'); ?>
+  <input name="action" value="" id="mpaction"
+         dojoType="dijit.form.TextBox" style="display: none"/>
 
-  <?php print_hidden('replyTo', htmlspecialchars($article['link'])); ?>
+  <?php print_hidden('postUrl', htmlspecialchars($article['link'])); ?>
+
+  <div style="text-align: right">
+   <button type="submit" name="action"
+     onclick="dijit.byId('mpaction').set('value',this.value);"
+     value="bookmark">🔖 Bookmark</button>
+   <button type="submit" name="action"
+     onclick="dijit.byId('mpaction').set('value',this.value);"
+     value="like">♥ Like</button>
+  </div>
+
+  <textarea name="content" rows="4" cols="60"
+            style="box-sizing: border-box; width:100%; height: auto"
+            dojoType="dijit.form.SimpleTextarea"
+   ></textarea><br/>
 
   <?php if (count($accounts) == 1) { ?>
    <?php print_hidden('me', htmlspecialchars(reset($accounts))); ?>
    <br/>
   <?php } ?>
 
-  <textarea name="content" rows="4" cols="60"
-            style="width:95%; height: auto"
-            dojoType="dijit.form.SimpleTextarea"
-   ></textarea><br/>
-  <button type="submit">Post comment</button>
+  <button type="submit" name="action"
+    onclick="dijit.byId('mpaction').set('value',this.value);"
+    value="comment">Post comment</button>
  </form>
 
  <div class="reply" style="text-align: right">
index 5b26c226820075474a8fb7d149c537acd6cf3e59..745d432424a246172747c2af63687408f268b4b1 100644 (file)
--- a/init.php
+++ b/init.php
@@ -185,29 +185,45 @@ class Micropub extends Plugin implements IHandler
         }
     }
 
+    /**
+     * Post a comment, like or bookmark via micropub
+     */
     protected function postAction()
     {
-        if (!isset($_POST['me'])) {
-            return $this->errorOut('"me" parameter missing');
+        $action = 'comment';
+        if (isset($_POST['action'])) {
+            $action = trim($_POST['action']);
         }
-        $me = trim($_POST['me']);
-
-        if (!isset($_POST['replyTo'])) {
-            return $this->errorOut('"replyTo" parameter missing');
+        if (array_search($action, ['bookmark', 'comment', 'like']) === false) {
+            return $this->errorOut('"action" parameter invalid');
         }
-        $replyTo = trim($_POST['replyTo']);
 
-        if (!isset($_POST['content'])) {
-            return $this->errorOut('"content" parameter missing');
+        if (!isset($_POST['me'])) {
+            return $this->errorOut('"me" parameter missing');
         }
-        $content = trim($_POST['content']);
-
+        $me = trim($_POST['me']);
         $accounts = PluginHost::getInstance()->get($this, 'accounts', []);
         if (!isset($accounts[$me])) {
             return $this->errorOut('"me" parameter invalid');
         }
         $account = $accounts[$me];
 
+        if (!isset($_POST['postUrl'])) {
+            return $this->errorOut('"postUrl" parameter missing');
+        }
+        $postUrl = trim($_POST['postUrl']);
+
+        if ($action == 'comment') {
+            if (!isset($_POST['content'])) {
+                return $this->errorOut('"content" parameter missing');
+            }
+            $content = trim($_POST['content']);
+            if (!strlen($_POST['content'])) {
+                return $this->errorOut('"content" is empty');
+            }
+        }
+
+
         $links = $this->getLinks($me);
         if (!count($links)) {
             return $this->errorOut('No links found');
@@ -216,20 +232,30 @@ class Micropub extends Plugin implements IHandler
             return $this->errorOut('No micropub endpoint found');
         }
 
+        $parameters = [
+            'access_token' => $account['access_token'],
+            'h'            => 'entry',
+        ];
+
+        if ($action == 'bookmark') {
+            $parameters['bookmark-of'] = $postUrl;
+
+        } else if ($action == 'comment') {
+            $parameters['in-reply-to'] = $postUrl;
+            $parameters['content']     = $content;
+
+        } else if ($action == 'like') {
+            $parameters['like-of'] = $postUrl;
+        }
+
+
         /* unfortunately fetch_file_contents() does not return headers
            so we have to bring our own way to POST data */
         $opts = [
             'http' => [
                 'method'  => 'POST',
                 'header'  => 'Content-type: application/x-www-form-urlencoded',
-                'content' => http_build_query(
-                    [
-                        'access_token' => $account['access_token'],
-                        'h'            => 'entry',
-                        'in-reply-to'  => $replyTo,
-                        'content'      => $content,
-                    ]
-                ),
+                'content' => http_build_query($parameters),
                 'ignore_errors' => true,
             ]
         ];