Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shortcode for total # of visitors (e.g. a site counter) #120

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions koko-analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@
$shortcode = new Shortcode_Most_Viewed_Posts();
$shortcode->init();

require __DIR__ . '/src/class-shortcode-site-counter.php';
$site_counter_shortcode = new ShortCode_Site_Counter();
$site_counter_shortcode->init();

require __DIR__ . '/src/class-pruner.php';
$pruner = new Pruner();
$pruner->init();

Expand Down
81 changes: 81 additions & 0 deletions src/class-shortcode-site-counter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* @package koko-analytics
* @license GPL-3.0+
* @author Anil Kulkarni
* Adds support for a shortcode to display the number of times a page or a site has been viewed
* For example, to show the last 30 day stats in the footer
* <?php
* echo do_shortcode('[koko_analytics_site_counter days=30]');
* ?>
*
* Options:
* days: How many previous days to count. Defaults to -1 which means show views for all time
* use_pageviews: If you want to counte pageviews instead of visitors, set this to True
* always_show_global_stats: By default, the value of the counter will depend on the page.
* If you're viewing a single page, it shows the stats only for that page, else the whole site.
* Setting always_show_global_stats to True means the stats will always be for the whole site
*/

namespace KokoAnalytics;

class ShortCode_Site_Counter {
const SHORTCODE = 'koko_analytics_site_counter';

public function init() {
add_shortcode( self::SHORTCODE, array( $this, 'content' ) );
}

public function content( $args ) {
$default_args = array(
'days' => -1,
'use_pageviews' => false,
'always_show_global_stats' => false,
);
$args = shortcode_atts( $default_args, $args, self::SHORTCODE );
$days = $args['days'];
# Since the column value is directly in the sql query, hard code it
# to prevent SQL injection attacks.
$column = $args['use_pageviews'] === true ? 'pageviews' : 'visitors';

if ( ! is_single() || $args['always_show_global_stats'] === true ) {
$count = $this->get_site_views( $days, $column );
} else {
$count = $this->get_single_page_views( $days, $column );
}

$html = sprintf( PHP_EOL . ' <span class="koko-analytics-post-count">%s</span>', $count );
return $html;
}

private function get_start_date( $days ) {
$timezone = get_option( 'timezone_string', 'UTC' );
$datetime = new \DateTime( 'now', new \DateTimeZone( $timezone ) );
$datetime->modify( sprintf( '-%d days', $days ) );
$start_date = $datetime->format( 'Y-m-d' );
return $start_date;
}

private function get_site_views( $days, $column ) {
global $wpdb;
if ( $days == -1 ) {
$sql = "SELECT SUM({$column}) FROM {$wpdb->prefix}koko_analytics_site_stats";
} else {
$start_date = $this->get_start_date( $days );
$sql = $wpdb->prepare( "SELECT SUM({$column}) FROM {$wpdb->prefix}koko_analytics_site_stats s WHERE s.date >= %s", array( $start_date ) );
}
return (int) ( $wpdb->get_var( $sql ) ?? 0 );
}

private function get_single_page_views( $days, $column ) {
global $wpdb;
if ( $days == -1 ) {
$sql = $wpdb->prepare( "SELECT SUM({$column}) FROM {$wpdb->prefix}koko_analytics_post_stats s WHERE s.id = %d", array( get_the_ID() ) );
} else {
$start_date = $this->get_start_date( $days );
$sql = $wpdb->prepare( "SELECT SUM({$column}) FROM {$wpdb->prefix}koko_analytics_post_stats s WHERE s.id = %d AND s.date >= %s", array( get_the_ID(), $start_date ) );
}
return (int) ( $wpdb->get_var( $sql ) ?? 0 );

}
}
14 changes: 14 additions & 0 deletions tests/ShortcodeSiteCounterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);

use KokoAnalytics\ShortCode_Site_Counter;
use PHPUnit\Framework\TestCase;

final class ShortcodeSiteCounterTest extends TestCase
{
public function testCanInstantiate() : void
{
$i = new ShortCode_Site_Counter();
self::assertTrue($i instanceof ShortCode_Site_Counter);
}
}
Loading