Skip to content

Instantly share code, notes, and snippets.

@bastianallgeier
Created July 17, 2012 13:15
Show Gist options
  • Save bastianallgeier/3129327 to your computer and use it in GitHub Desktop.
Save bastianallgeier/3129327 to your computer and use it in GitHub Desktop.

Simple Kirby Like Counter

Installation

  1. Add like.php to your site/templates folder
  2. Create a new invisible like folder in content/blog
  3. Place a like.txt in content/blog It's enough to put title: Like Updater or something like that as content in there.

Usage

Go to http://yourdomain.com/blog/like. This will invoke the Like Counter Updater. Since no parameter has been passed, you will get back an error.

{"status":"error","msg":"The post could not be found"}

Pass an UID of a blog article like this:

http://yourdomain.com/blog/like/post:my-awesome-post

If the article has been found, the like counter will increase and add a Likes field to your article's text file. On each reuquest, the like counter will increase again.

The response will look like:

{"status":"success","msg":"The likes have been updated","likes":5}

Ajax

Here's a simple example how to call the like counter via ajax (jquery):


$.getJSON('/blog/like/post:my-awesome-article', function(r) {

  if(r == undefined || r.status == 'error') {
    return alert('Likes could not be updated');
  } else {
    return alert('Likes have been updated. New Likes count: ' + r.likes);
  }

});

<?php
// get the post UID from the URL
$uid = param('post');
if(!$uid) die(a::json(array(
'status' => 'error',
'msg' => 'The post could not be found',
)));
// get the full post object
$post = $pages->find('blog/' . $uid);
if($post->uid() != $uid) die(a::json(array(
'status' => 'error',
'msg' => 'The post could not be found',
)));
// get current likes count
$likes = intval((string)$post->likes());
$likes++;
// get all variables from the content text file
$variables = $post->content()->variables();
// add the updated likes counter
$variables['likes'] = $likes;
// set the filename
$file = $post->root() . '/' . $post->intendedTemplate() . '.txt';
// overwrite the file
$write = write($file, $variables);
// react on errors
if(error($write)) die(a::json($write));
die(a::json(array(
'status' => 'success',
'msg' => 'The likes have been updated',
'likes' => $likes
)));
// writer function
function write($file, $values) {
if(file_exists($file) && !is_writable($file)) return array(
'status' => 'error',
'msg' => 'The file could not be written'
);
$break = false;
$result = "\xEF\xBB\xBF";
$keys = array();
foreach($values AS $k => $v) {
$k = str::urlify($k);
$k = str::ucfirst(str_replace('-', '_', $k));
if(in_array($k, $keys) || empty($k)) continue;
$keys[] = $k;
$result .= $break . $k . ': ' . trim($v);
$break = "\n\n----\n\n";
}
$write = f::write($file, $result);
if(!$write || !file_exists($file)) return array(
'status' => 'error',
'msg' => 'The file could not be written'
);
return array(
'status' => 'success',
'msg' => 'The file has been written'
);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment