Skip to content

Commit

Permalink
Merge branch 'release/1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Dec 2, 2020
2 parents e8d1d62 + 8e9c856 commit 4e473bd
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 25 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,16 @@
# v1.1.0
## 12/02/2020

1. [](#new)
* Require Grav 1.7
* Code cleanup
* Pass `phpstan` tests
* Added `select` method that allows you to perform custom SELECT queries
* Remove `admin.css` as it's no longer needed
1. [](#bugfix)
* Fixed default `limit` for `getAll` from `0` to `-1` in order to actually return all items
* Fix CLI commands to use new format

# v1.0.1
## 12/09/2018

Expand Down
2 changes: 2 additions & 0 deletions README.md
@@ -1,5 +1,7 @@
# Views Plugin

[![PHPStan](https://img.shields.io/badge/PHPStan-enabled-brightgreen.svg?style=flat)](https://github.com/phpstan/phpstan)

The **Views** Plugin is for [Grav CMS](http://github.com/getgrav/grav) version 1.6+. This is a simple views count tracking plugin. You can use it several ways, but by default it will automatically track all site page requests and use the **page route** as the identifying key. There is no limiting, tracking, or refresh detection, it simply tracks the number of times a page has been loaded.

## Installation
Expand Down
8 changes: 5 additions & 3 deletions blueprints.yaml
@@ -1,19 +1,21 @@
name: Views
version: 1.0.1
type: plugin
slug: views
version: 1.1.0
description: Simple View tracking and reporting
icon: eye
author:
name: Trilby Media
email: hello@trilby.media
homepage: https://github.com/trilbymedia/grav-plugin-views
demo: http://demo.yoursite.com
keywords: grav, plugin, etc
keywords: grav, plugin, views
bugs: https://github.com/trilbymedia/grav-plugin-views/issues
docs: https://github.com/trilbymedia/grav-plugin-views/blob/develop/README.md
license: MIT

dependencies:
- { name: grav, version: '>=1.6.0-beta.6' }
- { name: grav, version: '>=1.7.0-rc.13' }
- { name: database, version: '>=1.0.0' }

form:
Expand Down
37 changes: 30 additions & 7 deletions classes/Views.php
@@ -1,4 +1,12 @@
<?php

/**
* @package Grav\Plugin\Views
*
* @copyright Copyright (C) 2014 - 2019 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/

namespace Grav\Plugin\Views;

use Grav\Common\Filesystem\Folder;
Expand Down Expand Up @@ -108,12 +116,12 @@ public function get($id, $type = null)
{
$query = "SELECT count FROM {$this->table_total_views} WHERE id = :id";

if (!is_null($type)) {
if (null !== $type) {
$query .= ' AND type = :type';
}

$statement = $this->db->prepare($query);
if (!is_null($type)) {
if (null !== $type) {
$statement->bindValue(':type', $type, PDO::PARAM_STR);
}
$statement->bindValue(':id', $id, PDO::PARAM_STR);
Expand All @@ -124,23 +132,23 @@ public function get($id, $type = null)
return $results['count'] ?? 0;
}

public function getAll($type = null, $limit = 0, $order = 'ASC')
public function getAll($type = null, $limit = -1, $order = 'ASC')
{
$order = strtoupper($order) === 'ASC' ? 'ASC' : 'DESC';
$offset = 0;

$query = "SELECT id, count, type FROM {$this->table_total_views} ";

if (!is_null($type)) {
$query .= "WHERE type = :type ";
if (null !== $type) {
$query .= 'WHERE type = :type ';
}

$query .= "ORDER BY count {$order}, type LIMIT :limit OFFSET :offset";


$statement = $this->db->prepare($query);

if (!is_null($type)) {
if (null !== $type) {
$statement->bindValue(':type', $type, PDO::PARAM_STR);
}
$statement->bindValue(':limit', $limit, PDO::PARAM_INT);
Expand All @@ -150,6 +158,19 @@ public function getAll($type = null, $limit = 0, $order = 'ASC')
return $statement->fetchAll();
}

public function select($query = null, $multi = false)
{
if (null === $query) {
return false;
}

$query = 'SELECT ' . $query;
$statement = $this->db->prepare($query);
$statement->execute();

return $multi ? $statement->fetchAll() : $statement->fetch();
}

public function createTables()
{
$commands = [
Expand All @@ -167,7 +188,9 @@ protected function supportOnConflict()
static $bool;

if ($bool === null) {
$bool = version_compare($this->db->query('SELECT sqlite_version()')->fetch()[0], '3.24', '>=');
$query = $this->db->query('SELECT sqlite_version()');
$version = $query ? $query->fetch()[0] ?? 0 : 0;
$bool = version_compare($version, '3.24', '>=');
}

return $bool;
Expand Down
7 changes: 4 additions & 3 deletions cli/LsCommand.php
@@ -1,15 +1,16 @@
<?php

/**
* @package Grav\Plugin\Views
*
* @copyright Copyright (C) 2014 - 2017 Trilby Media, LLC. All rights reserved.
* @copyright Copyright (C) 2014 - 2019 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/

namespace Grav\Plugin\Console;

use Grav\Console\ConsoleCommand;
use Grav\Common\Grav;
use Grav\Plugin\Database\Database;
use Grav\Plugin\Views\Views;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -79,7 +80,7 @@ protected function serve()
$io = new SymfonyStyle($this->input, $this->output);

// Initialize Plugins
$grav->fireEvent('onPluginsInitialized');
$this->initializePlugins();

$slug = $this->input->getArgument('slug');
$limit = $this->input->getOption('limit');
Expand Down
7 changes: 4 additions & 3 deletions cli/SetCommand.php
@@ -1,15 +1,16 @@
<?php

/**
* @package Grav\Plugin\Views
*
* @copyright Copyright (C) 2014 - 2017 Trilby Media, LLC. All rights reserved.
* @copyright Copyright (C) 2014 - 2019 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/

namespace Grav\Plugin\Console;

use Grav\Console\ConsoleCommand;
use Grav\Common\Grav;
use Grav\Plugin\Database\Database;
use Grav\Plugin\Views\Views;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Style\SymfonyStyle;
Expand Down Expand Up @@ -66,7 +67,7 @@ protected function serve()
$io = new SymfonyStyle($this->input, $this->output);

// Initialize Plugins
$grav->fireEvent('onPluginsInitialized');
$this->initializePlugins();

$slug = $this->input->getArgument('slug');
$count = $this->input->getArgument('count');
Expand Down
26 changes: 24 additions & 2 deletions composer.json
@@ -1,8 +1,30 @@
{
"name": "getgrav/grav-plugin-views",
"type": "grav-plugin",
"description": "Simple views count tracking plugin for Grav CMS",
"keywords": ["grav", "plugin", "views"],
"homepage": "https://github.com/trilbymedia/grav-plugin-views",
"license": "MIT",
"authors": [
{
"name": "Trilby Media",
"email": "hello@trilby.media",
"homepage": "https://trilby.media/",
"role": "Developer"
}
],
"support": {
"issues": "https://github.com/trilbymedia/grav-plugin-views/issues",
"irc": "https://chat.getgrav.org",
"forum": "https://getgrav.org/forum",
"docs": "https://github.com/trilbymedia/grav-plugin-views/blob/master/README.md"
},
"autoload": {
"psr-4": {
"Grav\\Plugin\\Views\\": "classes/"
}
"Grav\\Plugin\\Views\\": "classes/",
"Grav\\Plugin\\Console\\": "cli/"
},
"classmap": ["views.php"]
},
"require": {
"php": ">=7.1.3",
Expand Down
23 changes: 23 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions css/admin.css

This file was deleted.

2 changes: 1 addition & 1 deletion vendor/composer/ClassLoader.php
Expand Up @@ -377,7 +377,7 @@ private function findFileWithExtension($class, $ext)
$subPath = $class;
while (false !== $lastPos = strrpos($subPath, '\\')) {
$subPath = substr($subPath, 0, $lastPos);
$search = $subPath . '\\';
$search = $subPath.'\\';
if (isset($this->prefixDirsPsr4[$search])) {
$pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1);
foreach ($this->prefixDirsPsr4[$search] as $dir) {
Expand Down
1 change: 1 addition & 0 deletions vendor/composer/autoload_classmap.php
Expand Up @@ -6,4 +6,5 @@
$baseDir = dirname($vendorDir);

return array(
'Grav\\Plugin\\ViewsPlugin' => $baseDir . '/views.php',
);
1 change: 1 addition & 0 deletions vendor/composer/autoload_psr4.php
Expand Up @@ -7,4 +7,5 @@

return array(
'Grav\\Plugin\\Views\\' => array($baseDir . '/classes'),
'Grav\\Plugin\\Console\\' => array($baseDir . '/cli'),
);
10 changes: 10 additions & 0 deletions vendor/composer/autoload_static.php
Expand Up @@ -10,6 +10,7 @@ class ComposerStaticInitf7887527659a2ee20565693aa7585160
'G' =>
array (
'Grav\\Plugin\\Views\\' => 18,
'Grav\\Plugin\\Console\\' => 20,
),
);

Expand All @@ -18,13 +19,22 @@ class ComposerStaticInitf7887527659a2ee20565693aa7585160
array (
0 => __DIR__ . '/../..' . '/classes',
),
'Grav\\Plugin\\Console\\' =>
array (
0 => __DIR__ . '/../..' . '/cli',
),
);

public static $classMap = array (
'Grav\\Plugin\\ViewsPlugin' => __DIR__ . '/../..' . '/views.php',
);

public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInitf7887527659a2ee20565693aa7585160::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitf7887527659a2ee20565693aa7585160::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitf7887527659a2ee20565693aa7585160::$classMap;

}, null, ClassLoader::class);
}
Expand Down
17 changes: 14 additions & 3 deletions views.php
@@ -1,8 +1,18 @@
<?php

/**
* @package Grav\Plugin\Views
*
* @copyright Copyright (C) 2014 - 2019 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/

namespace Grav\Plugin;

use Composer\Autoload\ClassLoader;
use Grav\Common\Config\Config;
use Grav\Common\Plugin;
use Grav\Common\Uri;
use Grav\Plugin\Views\Views;
use RocketTheme\Toolbox\Event\Event;

Expand Down Expand Up @@ -53,7 +63,10 @@ public function autoload()
public function register()
{
$this->grav['views'] = function ($c) {
return new Views($c['config']->get('plugins.views'));
/** @var Config $config */
$config = $c['config'];

return new Views($config->get('plugins.views'));
};
}

Expand Down Expand Up @@ -102,8 +115,6 @@ public function onAdminGenerateReports(Event $e)
];

$reports['Grav Views'] = $this->grav['twig']->processTemplate('reports/views-report.html.twig', $data);

$this->grav['assets']->addCss('plugins://views/css/admin.css');
}

/**
Expand Down

0 comments on commit 4e473bd

Please sign in to comment.