| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Light Views Counter |
| 4 |
* Plugin URI: https://themeruby.com/light-views-counter |
| 5 |
* Description: Lightweight and fast post view counter with smart tracking, built for high-traffic sites and large post databases. |
| 6 |
* Tags: views, counter, post views, popular posts |
| 7 |
* Author: ThemeRuby |
| 8 |
* License: GPLv3 |
| 9 |
* License URI: https://www.gnu.org/licenses/gpl-3.0.html |
| 10 |
* Version: 1.3.0 |
| 11 |
* Requires at least: 6.0 |
| 12 |
* Requires PHP: 7.4 |
| 13 |
* Author URI: https://themeruby.com/ |
| 14 |
* Text Domain: light-views-counter |
| 15 |
* Domain Path: /languages |
| 16 |
* |
| 17 |
* @package light-views-counter |
| 18 |
* |
| 19 |
* This program is free software; you can redistribute it and/or modify |
| 20 |
* it under the terms of the GNU General Public License as published by |
| 21 |
* the Free Software Foundation, either version 3 of the License, or any later version. |
| 22 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 23 |
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 24 |
*/ |
| 25 |
|
| 26 |
// Exit if accessed directly. |
| 27 |
if ( ! defined( 'ABSPATH' ) ) { |
| 28 |
exit; |
| 29 |
} |
| 30 |
|
| 31 |
// ============================================================================ |
| 32 |
// CONSTANTS |
| 33 |
// ============================================================================ |
| 34 |
|
| 35 |
if ( ! defined( 'LIGHTVC_VERSION' ) ) { |
| 36 |
define( 'LIGHTVC_VERSION', '1.3.0' ); |
| 37 |
} |
| 38 |
|
| 39 |
if ( ! defined( 'LIGHTVC_PLUGIN_DIR' ) ) { |
| 40 |
define( 'LIGHTVC_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); |
| 41 |
} |
| 42 |
|
| 43 |
if ( ! defined( 'LIGHTVC_PLUGIN_URL' ) ) { |
| 44 |
define( 'LIGHTVC_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); |
| 45 |
} |
| 46 |
|
| 47 |
if ( ! defined( 'LIGHTVC_PLUGIN_BASENAME' ) ) { |
| 48 |
define( 'LIGHTVC_PLUGIN_BASENAME', plugin_basename( __FILE__ ) ); |
| 49 |
} |
| 50 |
|
| 51 |
// ============================================================================ |
| 52 |
// PLUGIN ACTIVATION & DEACTIVATION |
| 53 |
// ============================================================================ |
| 54 |
|
| 55 |
/** |
| 56 |
* Plugin activation handler. |
| 57 |
* |
| 58 |
* Creates database table, sets default options, and schedules events. |
| 59 |
* |
| 60 |
* @since 1.0.0 |
| 61 |
*/ |
| 62 |
function lightvc_activate() { |
| 63 |
// Load required files for activation. |
| 64 |
require_once LIGHTVC_PLUGIN_DIR . 'includes/class-lvc-database.php'; |
| 65 |
require_once LIGHTVC_PLUGIN_DIR . 'includes/class-lvc-cache.php'; |
| 66 |
|
| 67 |
// Create custom database table with indexes. |
| 68 |
LIGHTVC_Database::create_table(); |
| 69 |
|
| 70 |
// Set default plugin options (only if not already set). |
| 71 |
$default_options = [ |
| 72 |
'lightvc_scroll_threshold' => 50, // 50% scroll threshold |
| 73 |
'lightvc_time_window' => 1800, // 1800 seconds (30 minutes) duplicate prevention |
| 74 |
'lightvc_cache_duration' => 300, // 5 minutes cache |
| 75 |
'lightvc_enable_caching' => 1, // Caching enabled |
| 76 |
'lightvc_fast_mode' => 1, // sendBeacon API enabled |
| 77 |
'lightvc_show_views_on_content' => 0, // Auto-display disabled by default |
| 78 |
'lightvc_load_css_in_header' => 0, // Load CSS disabled by default (on-demand only) |
| 79 |
'lightvc_enable_get_endpoint' => 0, // GET endpoint disabled by default for privacy |
| 80 |
'lightvc_supported_post_types' => [ 'post' ], // Track 'post' type by default |
| 81 |
'lightvc_query_method' => 'subquery', // Subquery method for ordering (default) |
| 82 |
'lightvc_exclude_bots' => 1, // Exclude bots enabled by default |
| 83 |
'lightvc_uninstall_data' => 0, // Delete data on uninstall disabled by default |
| 84 |
]; |
| 85 |
|
| 86 |
foreach ( $default_options as $option => $value ) { |
| 87 |
add_option( $option, $value ); |
| 88 |
} |
| 89 |
|
| 90 |
// Schedule cache warmup event for performance. |
| 91 |
LIGHTVC_Cache::schedule_warmup(); |
| 92 |
|
| 93 |
// Clear WordPress object cache |
| 94 |
wp_cache_flush(); |
| 95 |
} |
| 96 |
|
| 97 |
register_activation_hook( __FILE__, 'lightvc_activate' ); |
| 98 |
|
| 99 |
/** |
| 100 |
* Plugin deactivation handler. |
| 101 |
* |
| 102 |
* Cleans up scheduled events and caches. |
| 103 |
* |
| 104 |
* @since 1.0.0 |
| 105 |
*/ |
| 106 |
function lightvc_deactivate() { |
| 107 |
// Load cache class for cleanup. |
| 108 |
require_once LIGHTVC_PLUGIN_DIR . 'includes/class-lvc-cache.php'; |
| 109 |
|
| 110 |
// Unschedule cache warmup event. |
| 111 |
LIGHTVC_Cache::unschedule_warmup(); |
| 112 |
|
| 113 |
// Clear WordPress object cache |
| 114 |
wp_cache_flush(); |
| 115 |
} |
| 116 |
|
| 117 |
register_deactivation_hook( __FILE__, 'lightvc_deactivate' ); |
| 118 |
|
| 119 |
// ============================================================================ |
| 120 |
// INITIALIZATION |
| 121 |
// ============================================================================ |
| 122 |
|
| 123 |
/** |
| 124 |
* Initialize plugin core functionality. |
| 125 |
* |
| 126 |
* Loads all plugin classes, registers hooks, and initializes components. |
| 127 |
* This runs on 'plugins_loaded' hook with priority 10. |
| 128 |
* |
| 129 |
* @since 1.0.0 |
| 130 |
*/ |
| 131 |
function lightvc_init() { |
| 132 |
// Load core classes. |
| 133 |
require_once LIGHTVC_PLUGIN_DIR . 'includes/class-lvc-database.php'; |
| 134 |
require_once LIGHTVC_PLUGIN_DIR . 'includes/class-lvc-cache.php'; |
| 135 |
require_once LIGHTVC_PLUGIN_DIR . 'includes/class-lvc-counter.php'; |
| 136 |
require_once LIGHTVC_PLUGIN_DIR . 'includes/class-lvc-api.php'; |
| 137 |
require_once LIGHTVC_PLUGIN_DIR . 'includes/class-lvc-query.php'; |
| 138 |
require_once LIGHTVC_PLUGIN_DIR . 'includes/class-lvc-display.php'; |
| 139 |
require_once LIGHTVC_PLUGIN_DIR . 'includes/class-lvc-shortcode.php'; |
| 140 |
|
| 141 |
// Initialize core components. |
| 142 |
LIGHTVC_Counter::init(); |
| 143 |
LIGHTVC_Base_API::init(); |
| 144 |
LIGHTVC_Query::init(); |
| 145 |
LIGHTVC_Display::init(); |
| 146 |
LIGHTVC_Shortcode::init(); |
| 147 |
|
| 148 |
// Load WordPress widget. |
| 149 |
require_once LIGHTVC_PLUGIN_DIR . 'widgets/class-lvc-popular-posts-widget.php'; |
| 150 |
|
| 151 |
// Load admin interface (only in admin area). |
| 152 |
if ( is_admin() ) { |
| 153 |
require_once LIGHTVC_PLUGIN_DIR . 'admin/class-lvc-admin.php'; |
| 154 |
require_once LIGHTVC_PLUGIN_DIR . 'admin/class-lvc-meta.php'; |
| 155 |
require_once LIGHTVC_PLUGIN_DIR . 'admin/class-lvc-admin-stats.php'; |
| 156 |
require_once LIGHTVC_PLUGIN_DIR . 'admin/class-lvc-links.php'; |
| 157 |
|
| 158 |
LIGHTVC_Admin::init(); |
| 159 |
LIGHTVC_Metaboxes::init(); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
add_action( 'plugins_loaded', 'lightvc_init', 200 ); |
| 164 |
|
| 165 |
// ============================================================================ |
| 166 |
// PUBLIC API FUNCTIONS |
| 167 |
// ============================================================================ |
| 168 |
|
| 169 |
/** |
| 170 |
* Get post views count. |
| 171 |
* |
| 172 |
* This is the main public API function for retrieving view counts. |
| 173 |
* Uses caching when enabled for better performance. |
| 174 |
* |
| 175 |
* @param int $post_id Post ID to get views for. |
| 176 |
* |
| 177 |
* @return int View count (0 if post doesn't exist). |
| 178 |
* @since 1.0.0 |
| 179 |
* |
| 180 |
*/ |
| 181 |
function lightvc_get_post_views( $post_id ) { |
| 182 |
|
| 183 |
$post_id = absint( $post_id ); |
| 184 |
|
| 185 |
if ( ! $post_id ) { |
| 186 |
$post_id = get_the_ID(); |
| 187 |
} |
| 188 |
|
| 189 |
// Validate post ID. |
| 190 |
if ( ! $post_id ) { |
| 191 |
return 0; |
| 192 |
} |
| 193 |
|
| 194 |
// Get view count from database. |
| 195 |
$count = LIGHTVC_Database::get_views( $post_id ); |
| 196 |
|
| 197 |
/** |
| 198 |
* Filters the post views count before returning. |
| 199 |
* |
| 200 |
* Allows developers to modify the view count for custom logic. |
| 201 |
* |
| 202 |
* @param int $count The view count. |
| 203 |
* @param int $post_id The post ID. |
| 204 |
* |
| 205 |
* @since 1.0.0 |
| 206 |
* |
| 207 |
*/ |
| 208 |
return apply_filters( 'lightvc_post_views_count', $count, $post_id ); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Get popular posts based on view count. |
| 213 |
* |
| 214 |
* Returns an array of post objects ordered by views, with optional |
| 215 |
* date range filtering and post type selection. |
| 216 |
* |
| 217 |
* @param array $args { |
| 218 |
* Optional. Query arguments. |
| 219 |
* |
| 220 |
* @type int $limit Number of posts to return. Default 10. |
| 221 |
* @type string|array $post_type Post type(s) to query. Default 'post'. |
| 222 |
* @type int $date_range Days to filter (0 = all time). Default 0. |
| 223 |
* @type int $offset Query offset for pagination. Default 0. |
| 224 |
* } |
| 225 |
* @return array Array of post objects with ID, post_title, post_date, and views properties. |
| 226 |
* @since 1.0.0 |
| 227 |
* |
| 228 |
*/ |
| 229 |
function lightvc_get_popular_posts( $args = [] ) { |
| 230 |
// Set defaults. |
| 231 |
$defaults = [ |
| 232 |
'limit' => 10, |
| 233 |
'post_type' => 'post', |
| 234 |
'date_range' => 0, // 0 = all time, 7 = last 7 days, etc. |
| 235 |
'offset' => 0, |
| 236 |
]; |
| 237 |
|
| 238 |
// Merge with provided arguments. |
| 239 |
$args = wp_parse_args( $args, $defaults ); |
| 240 |
|
| 241 |
// Get popular posts from database. |
| 242 |
return LIGHTVC_Database::get_popular_posts( $args ); |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Check if Foxiz Core plugin is active. |
| 247 |
* |
| 248 |
* Helper function for checking Foxiz Core status across different contexts. |
| 249 |
* Checks multiple methods to ensure reliable detection. |
| 250 |
* |
| 251 |
* @return bool True if Foxiz Core is active, false otherwise. |
| 252 |
* @since 1.0.0 |
| 253 |
* |
| 254 |
*/ |
| 255 |
function lightvc_is_foxiz_core_active() { |
| 256 |
return class_exists( 'Foxiz_Core' ) || |
| 257 |
defined( 'FOXIZ_CORE_PATH' ) || |
| 258 |
( function_exists( 'is_plugin_active' ) && is_plugin_active( 'foxiz-core/foxiz-core.php' ) ); |
| 259 |
} |
| 260 |
|