summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Parecki <aaron@parecki.com>2016-04-21 19:34:22 -0700
committerAaron Parecki <aaron@parecki.com>2016-04-21 19:34:22 -0700
commit3f61541094fb0c6836a6b3afaea58d7bead9ade9 (patch)
tree1083c8b0cc77aa4b7ee0015963d386f2a777f12a
parentbd97d362c7702329e453b7c10586900ee06b728e (diff)
accept new json q=syndicate-to response
closes #36
-rw-r--r--lib/helpers.php41
-rw-r--r--views/new-post.php10
-rw-r--r--views/partials/syndication-js.php3
3 files changed, 25 insertions, 29 deletions
diff --git a/lib/helpers.php b/lib/helpers.php
index a860dc0..f0c226b 100644
--- a/lib/helpers.php
+++ b/lib/helpers.php
@@ -175,13 +175,14 @@ function micropub_get($endpoint, $params, $access_token) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Authorization: Bearer ' . $access_token
+ 'Authorization: Bearer ' . $access_token,
+ 'Accept: application/json'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$data = array();
if($response) {
- parse_str($response, $data);
+ $data = json_decode($response, true);
}
$error = curl_error($ch);
return array(
@@ -198,35 +199,23 @@ function get_syndication_targets(&$user) {
$r = micropub_get($user->micropub_endpoint, array('q'=>'syndicate-to'), $user->micropub_access_token);
if($r['data'] && array_key_exists('syndicate-to', $r['data'])) {
if(is_array($r['data']['syndicate-to'])) {
- $targetURLs = $r['data']['syndicate-to'];
- } elseif(is_string($r['data']['syndicate-to'])) {
- // support comma separated as a fallback
- $targetURLs = preg_split('/, ?/', $r['data']['syndicate-to']);
+ $data = $r['data']['syndicate-to'];
} else {
- $targetURLs = array();
+ $data = array();
}
- foreach($targetURLs as $t) {
- // If the syndication target doesn't have a scheme, add http
- if(!preg_match('/^http/', $t))
- $t2 = 'http://' . $t;
- else
- $t2 = $t;
-
- // Parse the target expecting it to be a URL
- $url = parse_url($t2);
-
- // If there's a host, and the host contains a . then we can assume there's a favicon
- // parse_url will parse strings like http://twitter into an array with a host of twitter, which is not resolvable
- if($url && array_key_exists('host', $url) && strpos($url['host'], '.') !== false) {
- $targets[] = array(
- 'target' => $t,
- 'favicon' => 'http://' . $url['host'] . '/favicon.ico'
- );
+ foreach($data as $t) {
+ if(array_key_exists('service', $t) && array_key_exists('photo', $t['service'])) {
+ $icon = $t['service']['photo'];
} else {
+ $icon = false;
+ }
+
+ if(array_key_exists('uid', $t) && array_key_exists('name', $t)) {
$targets[] = array(
- 'target' => $t,
- 'favicon' => false
+ 'target' => $t['name'],
+ 'uid' => $t['uid'],
+ 'favicon' => $icon
);
}
}
diff --git a/views/new-post.php b/views/new-post.php
index be5dafb..2c8c9f9 100644
--- a/views/new-post.php
+++ b/views/new-post.php
@@ -38,7 +38,12 @@
if($this->syndication_targets) {
echo '<ul>';
foreach($this->syndication_targets as $syn) {
- echo '<li><button data-syndication="'.$syn['target'].'" class="btn btn-default btn-block"><img src="'.$syn['favicon'].'" width="16" height="16"> '.$syn['target'].'</button></li>';
+ echo '<li>'
+ . '<button data-syndicate-to="'.(isset($syn['uid']) ? $syn['uid'] : $syn['target']).'" class="btn btn-default btn-block">'
+ . ($syn['favicon'] ? '<img src="'.$syn['favicon'].'" width="16" height="16"> ' : '')
+ . $syn['target']
+ . '</button>'
+ . '</li>';
}
echo '</ul>';
} else {
@@ -181,9 +186,10 @@ $(function(){
$("#btn_post").click(function(){
+ // Collect all the syndication buttons that are pressed
var syndications = [];
$("#syndication-container button.btn-info").each(function(i,btn){
- syndications.push($(btn).data('syndication'));
+ syndications.push($(btn).data('syndicate-to'));
});
var category = csv_to_array($("#note_category").val());
diff --git a/views/partials/syndication-js.php b/views/partials/syndication-js.php
index bd657f3..088cb43 100644
--- a/views/partials/syndication-js.php
+++ b/views/partials/syndication-js.php
@@ -5,8 +5,9 @@ function reload_syndications() {
$("#syndication-container").html('<ul></ul>');
for(var i in data.targets) {
var target = data.targets[i].target;
+ var uid = data.targets[i].uid;
var favicon = data.targets[i].favicon;
- $("#syndication-container ul").append('<li><button data-syndication="'+target+'" class="btn btn-default btn-block"><img src="'+favicon+'" width="16" height="16"> '+target+'</button></li>');
+ $("#syndication-container ul").append('<li><button data-syndicate-to="'+(uid ? uid : target)+'" class="btn btn-default btn-block">'+(favicon ? '<img src="'+favicon+'" width="16" height="16"> ':'')+target+'</button></li>');
}
bind_syndication_buttons();
} else {