Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Feb 3, 2017
1 parent 115765d commit ad31148
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,5 @@
# v0.1.0
## 02/03/2017

1. [](#new)
* ChangeLog started...
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Trilby Media

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
53 changes: 52 additions & 1 deletion README.md
@@ -1 +1,52 @@
# grav-plugin-social-counters
# Social Counters Plugin

The **Social Counters** plugin retrieves counts for both **GitHub** stars and **Twitter followers** and makes them available as Twig variables. You can see a [demo here](https://getgrav.org).

## Installation

Installing the Social Counters plugin can be done in one of two ways. The GPM (Grav Package Manager) installation method enables you to quickly and easily install the plugin with a simple terminal command, while the manual method enables you to do so via a zip file.

### GPM Installation (Preferred)

The simplest way to install this plugin is via the [Grav Package Manager (GPM)](http://learn.getgrav.org/advanced/grav-gpm) through your system's terminal (also called the command line). From the root of your Grav install type:

bin/gpm install social-counters

This will install the Social Counters plugin into your `/user/plugins` directory within Grav. Its files can be found under `/your/site/grav/user/plugins/social-counters`.

## Configuration

Before configuring this plugin, you should copy the `user/plugins/social-counters/social-counters.yaml` to `user/config/plugins/social-counters.yaml` and only edit that copy.

Here is the default configuration and an explanation of available options:

```yaml
enabled: true
cache_timeout: 3600
github:
user: getgrav
repo: grav
twitter:
user: getgrav
```

Just update the information for your GitHub and Twitter accounts.

> Note: the cache_timeout of `3600` is a sensible default to ensure you don't need to use any OAUTH tokens to get past any API limits
## Usage

The plugin makes a couple of variables available to be used:


#### GitHub Star Gazers (Stars)

```
{{ social_counters.github.stars }}
```

#### Twitter followers

```
{{ social_counters.twitter.followers }}
```
34 changes: 34 additions & 0 deletions blueprints.yaml
@@ -0,0 +1,34 @@
name: Social Counters
version: 0.1.0
description: Provides counts for social plugins such as GitHub and Twitter
icon: plug
author:
name: Trilby Media
email: devs@trilby.media
homepage: https://github.com/trilbymedia/grav-plugin-social-counters
demo: https://getgrav.org
keywords: grav, plugin, twitter, github
bugs: https://github.com/trilbymedia/grav-plugin-social-counters/issues
docs: https://github.com/trilbymedia/grav-plugin-social-counters/blob/develop/README.md
license: MIT

dependencies:
- { name: github, version: '>=1.0' }

form:
validation: strict
fields:
enabled:
type: toggle
label: Plugin status
highlight: 1
default: 0
options:
1: Enabled
0: Disabled
validate:
type: bool
text_var:
type: text
label: Text Variable
help: Text to add to the top of a page
91 changes: 91 additions & 0 deletions social-counters.php
@@ -0,0 +1,91 @@
<?php
namespace Grav\Plugin;

use Grav\Common\Plugin;
use RocketTheme\Toolbox\Event\Event;

/**
* Class SocialCountersPlugin
* @package Grav\Plugin
*/
class SocialCountersPlugin extends Plugin
{
protected $active = false;

/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}

/**
* Initialize configuration
*/
public function onPluginsInitialized()
{
if ($this->isAdmin()) {
$this->active = false;
return;
}

$this->enable([
'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
]);
}


/**
* Make form accessible from twig.
*/
public function onTwigSiteVariables()
{
require_once $this->grav['locator']->findResource('plugins://github/vendor/autoload.php');

$config = $this->config->get('plugins.social-counters');

$cache = $this->grav['cache'];
$cache_id = md5('social-counters'.$cache->getKey());

$github = $cache->fetch($cache_id . '-github');
$twitter = $cache->fetch($cache_id . '-twitter');

// Github not found in cache, try again
if ($github === false) {

$client = new \Github\Client(
new \Github\HttpClient\CachedHttpClient(array('cache_dir' => CACHE_DIR . '/github'))
);

$repo = $client->api('repo');

try {
$github['stars'] = $repo->show($config['github']['user'], $config['github']['repo'])['stargazers_count'];
$cache->save($cache_id . '-github', $github, $config['cache_timeout']);
} catch(\Exception $e) {
$github['error'] = $e->getMessage();
}
}

// Twitter not found in cache, try again
if ($twitter === false) {

$response = json_decode(file_get_contents("https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=".$config['twitter']['user']));
$followers = $response[0]->followers_count;

if (is_int($followers)) {

$twitter['followers'] = $followers;
$cache->save($cache_id . '-twitter', $twitter, $config['cache_timeout']);
} else {
$twitter['error'] = 'Could not retrieve gitter followers';
}
}

$this->grav['twig']->twig_vars['social_counters'] = ['github' => $github, 'twitter' => $twitter];

}
}
7 changes: 7 additions & 0 deletions social-counters.yaml
@@ -0,0 +1,7 @@
enabled: true
cache_timeout: 3600
github:
user: getgrav
repo: grav
twitter:
user: getgrav

0 comments on commit ad31148

Please sign in to comment.