Database
Database operations class.
Namespace: WebberZone\Top_Ten
Since: 4.2.0
Source: includes/class-database.php line 17
Methods
__construct()
Constructor.
Line: 50
public function __construct( );
get_table() static
Get the table name for overall or daily counts.
Since: 4.2.0
Line: 62
static public function get_table( $daily = ... ): string;
| Type | Name | Description |
|---|---|---|
bool |
$daily |
Whether to get the daily table. |
Returns: string — Table name.
get_count() static
Get count for a specific post.
Since: 4.2.0
Line: 83
static public function get_count( $post_id, $blog_id = ..., $daily = ..., $date_range = ... ): int;
| Type | Name | Description |
|---|---|---|
int |
$post_id |
Post ID. |
int |
$blog_id |
Blog ID (optional, defaults to current blog). |
bool |
$daily |
Whether to get daily count. |
array |
$date_range |
Date range array for daily counts [‘from_date’, ‘to_date’]. |
Returns: int — Post count.
get_sitewide_count() static
Get the count for a site-wide request context.
Site-wide contexts are resolved by the Pro-only fixed-ID map and then addressed by their reserved numeric ID in the shared count tables.
Since: 4.5.0
Line: 124
static public function get_sitewide_count( $context, $blog_id = ..., $daily = ..., $date_range = ... ): int;
| Type | Name | Description |
|---|---|---|
string |
$context |
Site-wide context key. |
int|null |
$blog_id |
Blog ID (optional, defaults to current blog). |
bool |
$daily |
Whether to get the daily count. |
array |
$date_range |
Date range array for daily counts. |
Returns: int — Site-wide count.
update_count() static
Update count for a post.
Since: 4.2.0
Line: 152
static public function update_count( $post_id, $blog_id = ..., $daily = ... ): int|false;
| Type | Name | Description |
|---|---|---|
int |
$post_id |
Post ID. |
int |
$blog_id |
Blog ID (optional, defaults to current blog). |
bool |
$daily |
Whether to update daily count. |
Returns: int|false — Number of rows affected or false on error.
set_count() static
Set count for a post to a specific value.
Since: 4.2.0
Line: 172
static public function set_count( $post_id, $count, $blog_id = ..., $daily = ... ): int|false;
| Type | Name | Description |
|---|---|---|
int |
$post_id |
Post ID. |
int |
$count |
Count value to set. |
int |
$blog_id |
Blog ID (optional, defaults to current blog). |
bool |
$daily |
Whether to update daily count. |
Returns: int|false — Number of rows affected or false on error.
delete_counts() static
Delete counts based on criteria.
Since: 4.2.0
Line: 238
static public function delete_counts( $args = ... ): int|false;
| Type | Name | Description |
|---|---|---|
array |
$args |
{ |
Returns: int|false — Number of rows deleted or false on error.
get_table_statistics() static
Get estimated table statistics including entry count and size.
Since: 4.2.0
Line: 306
static public function get_table_statistics( ): array;
Returns: array — Array of table statistics with entry count and size.
clear_table_statistics_cache() static
Clear the table statistics cache.
Since: 4.2.0
Line: 471
static public function clear_table_statistics_cache( );
clear_table_installation_cache() static
Invalidate the persistent and request-level table installation caches.
Since: 4.5.0
Line: 486
static public function clear_table_installation_cache( );
get_table_installation_status() static
Get the installation status of the four required Top 10 tables.
The status is persisted in a network option so normal admin requests do not need to enumerate the database tables. Explicit diagnostic requests can bypass the cache by setting $force to true.
Since: 4.5.0
Line: 507
static public function get_table_installation_status( $force = ... ): array<string,bool>;
| Type | Name | Description |
|---|---|---|
bool |
$force |
Whether to perform a live check. |
Returns: array<string,bool> — Table names mapped to their installation status.
is_table_installed() static
Check if a table exists.
Since: 4.2.0
Line: 562
static public function is_table_installed( $table, $force = ... ): bool;
| Type | Name | Description |
|---|---|---|
string |
$table |
Table name to check. |
bool |
$force |
Whether to perform a live check. |
Returns: bool — True if table exists, false otherwise.
get_counts_with_posts() static
Get counts with post information (JOIN with wp_posts).
Since: 4.2.0
Line: 605
static public function get_counts_with_posts( $args = ... ): array;
| Type | Name | Description |
|---|---|---|
array |
$args |
{ |
Returns: array — Array of results with post and count information.
bulk_upsert() static
Bulk upsert counts for import operations.
Since: 4.2.0
Line: 673
static public function bulk_upsert( $data, $daily = ... ): int|false;
| Type | Name | Description |
|---|---|---|
array |
$data |
Array of data to insert. Each element should be an array with postnumber, cntaccess, blog_id keys. |
bool |
$daily |
Whether this is for daily table (includes dp_date). |
Returns: int|false — Number of rows affected or false on error.
get_total_count() static
Get total count for all posts.
Since: 4.2.0
Line: 715
static public function get_total_count( $blog_id = ..., $daily = ..., $from_date = ..., $to_date = ... ): int;
| Type | Name | Description |
|---|---|---|
int |
$blog_id |
Blog ID (optional, defaults to current blog). |
bool |
$daily |
Whether to get daily total. |
string |
$from_date |
From date for daily counts. |
string |
$to_date |
To date for daily counts. |
Returns: int — Total count.
get_popular_posts() static
Get popular posts with caching support.
Since: 4.2.0
Line: 746
static public function get_popular_posts( $args = ... ): array;
| Type | Name | Description |
|---|---|---|
array |
$args |
Query arguments (same format as Top_Ten_Core_Query). |
Returns: array — Array of post IDs.
are_tables_installed() static
Check if the Top Ten tables exist.
Since: 4.2.0
Line: 772
static public function are_tables_installed( ): bool;
Returns: bool — True if both tables exist, false otherwise.
create_full_table_sql() static
Create table SQL for the main top_ten table.
Since: 4.2.0
Line: 786
static public function create_full_table_sql( ): string;
Returns: string — SQL to create the main table.
create_daily_table_sql() static
Create table SQL for the daily top_ten_daily table.
Since: 4.2.0
Line: 813
static public function create_daily_table_sql( ): string;
Returns: string — SQL to create the daily table.
get_funnel_table() static
Get the name of the visits funnel table (hot buffer, drained every 5 minutes).
Since: 4.3.0
Line: 840
static public function get_funnel_table( ): string;
Returns: string — Table name.
create_funnel_table_sql() static
SQL to create the visits funnel table.
Since: 4.3.0
Line: 852
static public function create_funnel_table_sql( ): string;
Returns: string — CREATE TABLE SQL.
get_log_table() static
Get the name of the visits log table (cold archive, pruned by maintenance cron).
Since: 4.3.0
Line: 879
static public function get_log_table( ): string;
Returns: string — Table name.
create_log_table_sql() static
SQL to create the visits log table.
Since: 4.3.0
Line: 891
static public function create_log_table_sql( ): string;
Returns: string — CREATE TABLE SQL.
record_view() static
Record a single visit using the configured tracking method.
Funnel tracking (default) appends the visit to the funnel table which is drained into the count tables by the aggregation cron. Legacy tracking writes directly to the count tables on every visit (pre-4.3 behaviour) and does not populate the visits log.
Since: 4.3.3
Line: 927
static public function record_view( $post_id, $blog_id, $activate_counter = ..., $source = ... ): int|false;
| Type | Name | Description |
|---|---|---|
int |
$post_id |
Post ID. |
int |
$blog_id |
Blog ID. |
int |
$activate_counter |
Counter flag: 1 = overall, 10 = daily, 11 = both. |
int |
$source |
Traffic source: 0 = web, 1 = feed. Only stored by funnel tracking. |
Returns: int|false — Rows inserted/updated or false on error.
update_counts_direct() static
Write a single visit directly to the overall and daily count tables.
This is the legacy (pre-4.3) tracking method: an immediate upsert per view, bypassing the funnel table and the aggregation cron. The visits log is not populated by this method.
Since: 4.3.3
Line: 949
static public function update_counts_direct( $post_id, $blog_id, $activate_counter = ... ): int|false;
| Type | Name | Description |
|---|---|---|
int |
$post_id |
Post ID. |
int |
$blog_id |
Blog ID. |
int |
$activate_counter |
Counter flag: 1 = overall, 10 = daily, 11 = both. |
Returns: int|false — Rows inserted/updated or false on error.
append_to_funnel() static
Append a single visit to the funnel table.
Since: 4.3.0
Line: 1007
static public function append_to_funnel( $post_id, $blog_id, $activate_counter = ..., $source = ... ): int|false;
| Type | Name | Description |
|---|---|---|
int |
$post_id |
Post ID. |
int |
$blog_id |
Blog ID. |
int |
$activate_counter |
Counter flag: 1 = overall, 10 = daily, 11 = both. |
int |
$source |
Traffic source: 0 = web, 1 = feed. |
Returns: int|false — Rows inserted or false on error.
aggregate_visit_log() static
Drain the funnel into the log and count tables, then empty the funnel.
Each of the four steps (copy to log, aggregate to daily, aggregate to overall, delete from funnel) commits independently rather than inside one app-level transaction. wpdb silently reconnects and retries a query if the DB connection drops mid-request, which would otherwise void an in-flight transaction and let later steps (e.g. the funnel delete) commit on a fresh connection while earlier ones were rolled back — losing visits with no error. Without a wrapping transaction, a crash between steps can at worst cause one batch to be re-aggregated (a bounded, self-correcting over-count), never a silent loss.
Since: 4.3.0
Line: 1048
static public function aggregate_visit_log( $batch_size = ..., $blog_id = ... ): true|false|int|\WP_Error;
| Type | Name | Description |
|---|---|---|
int |
$batch_size |
Maximum funnel rows to process per run. |
int|null |
$blog_id |
Optional blog ID. When set, only that site’s buffered visits are processed. |
Returns: true|false|int|\WP_Error — True if rows processed, false if lock not acquired, 0 if funnel empty, WP_Error on DB failure.
recreate_table() static
Recreate a table.
This method recreates a table by creating a backup, dropping the original table, and then creating a new table with the original name and inserting the data from the backup.
Since: 4.2.0
Line: 1183
static public function recreate_table( $table_name, $create_table_sql, $backup = ..., $fields = ..., $group_by_fields = ... ): bool|\WP_Error;
| Type | Name | Description |
|---|---|---|
string |
$table_name |
The name of the table to recreate. |
string |
$create_table_sql |
The SQL statement to create the new table. |
bool |
$backup |
Whether to backup the table or not. |
array |
$fields |
The fields to include in the temporary table and on duplicate key code. |
array |
$group_by_fields |
The fields to group by in the temporary table. |
Returns: bool|\WP_Error — True if recreated, error message if failed.
recreate_overall_table() static
Recreate overall table.
Since: 4.2.0
Line: 1244
static public function recreate_overall_table( $backup = ... ): bool|\WP_Error;
| Type | Name | Description |
|---|---|---|
bool |
$backup |
Whether to backup the table or not. |
Returns: bool|\WP_Error — True if recreated, error message if failed.
recreate_daily_table() static
Recreate daily table.
Since: 4.2.0
Line: 1262
static public function recreate_daily_table( $backup = ... ): bool|\WP_Error;
| Type | Name | Description |
|---|---|---|
bool |
$backup |
Whether to backup the table or not. |
Returns: bool|\WP_Error — True if recreated, error message if failed.
recreate_funnel_table() static
Recreate visits funnel table.
Since: 4.3.0
Line: 1282
static public function recreate_funnel_table( $backup = ... ): bool|\WP_Error;
| Type | Name | Description |
|---|---|---|
bool |
$backup |
Whether to create a permanent backup table before recreating. |
Returns: bool|\WP_Error — True if recreated, error message if failed.
recreate_log_table() static
Recreate visits log table.
Since: 4.3.0
Line: 1330
static public function recreate_log_table( $backup = ... ): bool|\WP_Error;
| Type | Name | Description |
|---|---|---|
bool |
$backup |
Whether to create a permanent backup table before recreating. |
Returns: bool|\WP_Error — True if recreated, error message if failed.
truncate_table() static
Truncate a table.
Since: 4.2.0
Line: 1377
static public function truncate_table( $table_name ): bool;
| Type | Name | Description |
|---|---|---|
string |
$table_name |
Table name to truncate. |
Returns: bool — True on success, false on failure.
count_deletable_daily_rows() static
Count rows in the daily table that would be pruned up to a given date.
Since: 4.3.0
Line: 1393
static public function count_deletable_daily_rows( string $to_date ): int;
| Type | Name | Description |
|---|---|---|
string |
$to_date |
Rows with dp_date at or before this value are counted. |
Returns: int — Row count.
get_daily_rollup_stats() static
Get the daily-table row counts before and after a rollup.
The projected row count groups rows by post, blog, and calendar date.
Since: 4.5.0
Line: 1411
static public function get_daily_rollup_stats( string $before_date, $blog_id = ... ): array|\WP_Error;
| Type | Name | Description |
|---|---|---|
string |
$before_date |
Rows before this date are included. |
int|null |
$blog_id |
Blog ID. Defaults to the current blog. |
Returns: array|\WP_Error — Rollup statistics or an error.
rollup_daily() static
Roll up hourly daily rows older than a date into one midnight row per post.
Each calendar date is processed in its own transaction so an interrupted operation can safely resume on the next date. The overall count table is never modified.
Since: 4.5.0
Line: 1483
static public function rollup_daily( string $before_date, $blog_id = ... ): array|\WP_Error;
| Type | Name | Description |
|---|---|---|
string |
$before_date |
Rows before this date are rolled up. |
int|null |
$blog_id |
Blog ID. Defaults to the current blog. |
Returns: array|\WP_Error — Rollup statistics or an error.
count_deletable_log_rows() static
Count rows in the visits log table older than a given datetime.
Since: 4.3.0
Line: 1668
static public function count_deletable_log_rows( string $before_datetime ): int;
| Type | Name | Description |
|---|---|---|
string |
$before_datetime |
Rows with visited_at before this value are counted. |
Returns: int — Row count.
prune_log_table() static
Delete rows from the visits log table older than a given datetime.
Since: 4.3.0
Line: 1684
static public function prune_log_table( string $before_datetime, int $batch_size = ... ): int|false;
| Type | Name | Description |
|---|---|---|
string |
$before_datetime |
Rows with visited_at before this value are deleted. |
int |
$batch_size |
Maximum rows to delete per call. |
Returns: int|false — Rows deleted, or false on failure.
count_funnel_rows() static
Count rows in the visits funnel table.
Since: 4.3.0
Line: 1698
static public function count_funnel_rows( ): int;
Returns: int — Row count.
count_orphan_counts() static
Count orphaned rows in a count table (rows with no matching post).
Only inspects rows belonging to the current blog so that posts on other sites in a multisite network are not falsely reported as orphans.
Since: 4.3.0
Line: 1716
static public function count_orphan_counts( string $table_name ): int;
| Type | Name | Description |
|---|---|---|
string |
$table_name |
Count table to inspect. |
Returns: int — Row count.
delete_orphan_counts() static
Delete orphaned rows from a count table (rows with no matching post).
Only deletes rows belonging to the current blog so that posts on other sites in a multisite network are not falsely treated as orphans.
Since: 4.3.0
Line: 1755
static public function delete_orphan_counts( string $table_name, int $batch_size = ... ): int|false;
| Type | Name | Description |
|---|---|---|
string |
$table_name |
Count table to clean. |
int |
$batch_size |
Maximum rows to delete per call. |
Returns: int|false — Rows deleted, or false on failure.