From ad311481d1160f89dcc55022fd3ed73984295ad5 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Fri, 3 Feb 2017 16:45:58 -0700 Subject: [PATCH] initial commit --- CHANGELOG.md | 5 +++ LICENSE | 21 ++++++++++ README.md | 53 +++++++++++++++++++++++++- blueprints.yaml | 34 +++++++++++++++++ social-counters.php | 91 ++++++++++++++++++++++++++++++++++++++++++++ social-counters.yaml | 7 ++++ 6 files changed, 210 insertions(+), 1 deletion(-) create mode 100644 CHANGELOG.md create mode 100644 LICENSE create mode 100644 blueprints.yaml create mode 100644 social-counters.php create mode 100644 social-counters.yaml diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..10cc541 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +# v0.1.0 +## 02/03/2017 + +1. [](#new) + * ChangeLog started... diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..90a7c01 --- /dev/null +++ b/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. diff --git a/README.md b/README.md index ff9b0ff..eb83315 100644 --- a/README.md +++ b/README.md @@ -1 +1,52 @@ -# grav-plugin-social-counters \ No newline at end of file +# 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 }} +``` \ No newline at end of file diff --git a/blueprints.yaml b/blueprints.yaml new file mode 100644 index 0000000..89f4293 --- /dev/null +++ b/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 diff --git a/social-counters.php b/social-counters.php new file mode 100644 index 0000000..bb5febc --- /dev/null +++ b/social-counters.php @@ -0,0 +1,91 @@ + ['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]; + + } +} diff --git a/social-counters.yaml b/social-counters.yaml new file mode 100644 index 0000000..c3fd37f --- /dev/null +++ b/social-counters.yaml @@ -0,0 +1,7 @@ +enabled: true +cache_timeout: 3600 +github: + user: getgrav + repo: grav +twitter: + user: getgrav \ No newline at end of file