Database
Database operations class.
Namespace: WebberZone\Top_Ten
Since: 4.2.0
Source: includes/class-database.php line 17
Methods
__construct()
Constructor.
Line: 22
public function __construct( );
get_table() static
Get the table name for overall or daily counts.
Since: 4.2.0
Line: 34
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: 55
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.
update_count() static
Update count for a post.
Since: 4.2.0
Line: 93
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: 113
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: 169
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 table statistics including entry count and size.
Since: 4.2.0
Line: 230
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: 331
static public function clear_table_statistics_cache( );
is_table_installed() static
Check if a table exists.
Since: 4.2.0
Line: 343
static public function is_table_installed( $table ): bool;
| Type | Name | Description |
|---|---|---|
string | $table | Table name to 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: 379
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: 447
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: 489
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: 520
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: 546
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: 558
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: 583
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: 610
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: 622
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: 649
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: 661
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: 697
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: 719
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: 775
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.
All four operations (copy to log, aggregate to daily, aggregate to overall, delete from funnel) run inside one transaction. A failure rolls back cleanly and the next run retries the same rows with no double-counting risk.
Since: 4.3.0
Line: 804
static public function aggregate_visit_log( $batch_size = ... ): true|false|int|\WP_Error;
| Type | Name | Description |
|---|---|---|
int | $batch_size | Maximum funnel rows to process per run. |
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: 950
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: 1011
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: 1029
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: 1049
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: 1097
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: 1144
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: 1160
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.
count_deletable_log_rows() static
Count rows in the visits log table older than a given datetime.
Since: 4.3.0
Line: 1175
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: 1191
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: 1205
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: 1223
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: 1250
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.