Skip to content

Commit 8eed28c

Browse files
committed
Fixes for sqllite
1 parent fb33b80 commit 8eed28c

4 files changed

Lines changed: 90 additions & 112 deletions

File tree

.playground/blueprint-github.json

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,40 @@
11
{
2-
"$schema": "https://playground.wordpress.net/blueprint-schema.json",
3-
"meta": {
4-
"title": "WebberZone Top 10 — GitHub Version",
5-
"description": "Try WebberZone Top 10 from GitHub master branch with pre-imported posts and seeded view counts.",
6-
"author": "webberzone"
7-
},
8-
"landingPage": "/popular-posts/",
9-
"preferredVersions": {
10-
"php": "8.3",
11-
"wp": "latest"
12-
},
13-
"features": {
14-
"networking": true
15-
},
16-
"login": true,
17-
"steps": [
18-
{
19-
"step": "installPlugin",
20-
"pluginZipFile": {
21-
"resource": "url",
22-
"url": "https://github.com/WebberZone/top-10/archive/refs/heads/master.zip"
23-
},
24-
"options": {
25-
"activate": true
26-
}
27-
},
28-
{
29-
"step": "importWxr",
30-
"file": {
31-
"resource": "url",
32-
"url": "https://raw.githubusercontent.com/WebberZone/top-10/master/.playground/demo-content.xml"
33-
}
34-
},
35-
{
36-
"step": "runPHP",
37-
"code": "<?php\nrequire '/wordpress/wp-load.php';\n\n// Get current settings or use defaults\n$settings = function_exists('tptn_get_settings') ? tptn_get_settings() : (array) get_option('tptn_settings', array());\n\n// Merge with demo settings\n$settings = array_merge($settings, array(\n\t'add_to' => 'single,page,home',\n\t'limit' => 10,\n\t'disp_list_count' => true,\n\t'show_excerpt' => true,\n\t'excerpt_length' => 15,\n\t'show_date' => true,\n\t'title_length' => 60,\n\t'thumb_default_show' => true\n));\n\nupdate_option('tptn_settings', $settings);\necho 'Settings updated.';\n"
38-
},
39-
{
40-
"step": "runPHP",
41-
"code": "<?php\nrequire '/wordpress/wp-load.php';\nglobal $wpdb;\n\n$funnel_table = $wpdb->base_prefix . 'top_ten_visits_funnel';\n$daily_table = $wpdb->base_prefix . 'top_ten_daily';\n$full_table = $wpdb->base_prefix . 'top_ten';\n$blog_id = get_current_blog_id();\n\n// Get published posts\n$posts = get_posts(array(\n\t'post_type' => array('post', 'page'),\n\t'post_status' => 'publish',\n\t'posts_per_page' => -1,\n\t'fields' => 'ids',\n\t'orderby' => 'date',\n\t'order' => 'DESC'\n));\n\nif (empty($posts)) {\n\techo 'No posts found.';\n\treturn;\n}\n\n// Seed funnel table with visit data\nforeach ($posts as $index => $post_id) {\n\t$base_count = max(25, 1200 - ($index * 30));\n\t$random_add = mt_rand(0, 150);\n\t$total_count = $base_count + $random_add;\n\t\n\t// Add multiple visits to funnel (simulating real traffic)\n\tfor ($i = 0; $i < $total_count; $i++) {\n\t\t$wpdb->insert($funnel_table, array(\n\t\t\t'postnumber' => $post_id,\n\t\t\t'blog_id' => $blog_id,\n\t\t\t'visited_at' => current_time('mysql'),\n\t\t\t'activate_counter' => 11, // Both daily and total\n\t\t\t'source' => 0 // Web traffic\n\t\t), array('%d', '%d', '%s', '%d', '%d'));\n\t}\n}\n\necho 'Seeded ' . count($posts) . ' posts with funnel data.';\n"
42-
},
43-
{
44-
"step": "runPHP",
45-
"code": "<?php\nrequire '/wordpress/wp-load.php';\n\n// Create Popular Posts page\n$page_id = wp_insert_post(array(\n\t'post_title' => 'Popular Posts',\n\t'post_name' => 'popular-posts',\n\t'post_status' => 'publish',\n\t'post_type' => 'page',\n\t'post_content' => '<!-- wp:shortcode -->[tptn_list heading=\"0\" limit=\"10\"]<!-- /wp:shortcode -->'\n));\n\n// Set pretty permalinks\nupdate_option('permalink_structure', '/%postname%/');\nflush_rewrite_rules(false);\n\necho 'Created Popular Posts page (ID: ' . $page_id . ') and set permalinks.';\n"
46-
}
47-
]
48-
}
2+
"$schema": "https://playground.wordpress.net/blueprint-schema.json",
3+
"meta": {
4+
"title": "WebberZone Top 10 — GitHub Version",
5+
"description": "Try WebberZone Top 10 from GitHub master branch with pre-imported posts and seeded view counts.",
6+
"author": "webberzone"
7+
},
8+
"landingPage": "/popular-posts/",
9+
"preferredVersions": {
10+
"php": "8.3",
11+
"wp": "latest"
12+
},
13+
"features": {
14+
"networking": true
15+
},
16+
"login": true,
17+
"steps": [
18+
{
19+
"step": "installPlugin",
20+
"pluginZipFile": {
21+
"resource": "url",
22+
"url": "https://github.com/WebberZone/top-10/archive/refs/heads/master.zip"
23+
},
24+
"options": {
25+
"activate": true
26+
}
27+
},
28+
{
29+
"step": "importWxr",
30+
"file": {
31+
"resource": "url",
32+
"url": "https://raw.githubusercontent.com/WebberZone/top-10/master/.playground/demo-content.xml"
33+
}
34+
},
35+
{
36+
"step": "runPHP",
37+
"code": "<?php\nrequire '/wordpress/wp-load.php';\n/**\n * Top 10 — WordPress Playground demo seeder.\n *\n * Assumes WordPress is already loaded (the blueprint requires wp-load.php\n * before requiring this file). Run once, after the demo content has been\n * imported and the plugin activated. Safe to re-run: count tables are\n * cleared before reseeding and the demo page is upserted by slug.\n *\n * @package WebberZone\\Top_Ten\n */\n\nglobal $wpdb;\n\n/*\n * ---------------------------------------------------------------------------\n * 1. Settings — start from the plugin defaults, then override a few keys so\n * the demo shows counts, excerpts and dates out of the box.\n * ---------------------------------------------------------------------------\n */\n$settings = (array) get_option( 'tptn_settings', array() );\n\n$settings = array_merge(\n\t$settings,\n\tarray(\n\t\t'add_to' => 'single,page,home',\n\t\t'limit' => 10,\n\t\t'disp_list_count' => true,\n\t\t'show_excerpt' => true,\n\t\t'excerpt_length' => 15,\n\t\t'show_date' => true,\n\t\t'title_length' => 60,\n\t\t'thumb_default_show' => true,\n\t)\n);\n\nupdate_option( 'tptn_settings', $settings );\n\n/*\n * ---------------------------------------------------------------------------\n * 2. View counts — seed the total and daily tables so the popular-posts\n * queries return data immediately (a fresh import has zero views).\n * ---------------------------------------------------------------------------\n */\n$total_table = $wpdb->base_prefix . 'top_ten';\n$daily_table = $wpdb->base_prefix . 'top_ten_daily';\n$blog_id = get_current_blog_id();\n\n$post_ids = get_posts(\n\tarray(\n\t\t'post_type' => array( 'post', 'page' ),\n\t\t'post_status' => 'publish',\n\t\t'posts_per_page' => -1,\n\t\t'fields' => 'ids',\n\t\t'orderby' => 'date',\n\t\t'order' => 'DESC',\n\t)\n);\n\nif ( empty( $post_ids ) ) {\n\treturn;\n}\n\n// Clear any prior demo rows (DELETE — SQLite under Playground has no TRUNCATE).\n$wpdb->query( \"DELETE FROM {$total_table}\" ); // phpcs:ignore WordPress.DB\n$wpdb->query( \"DELETE FROM {$daily_table}\" ); // phpcs:ignore WordPress.DB\n\nmt_srand( 42 ); // Deterministic counts so the demo looks the same every spin-up.\n\nforeach ( $post_ids as $i => $post_id ) {\n\t// Stagger totals so the ranking looks intentional, with a little jitter.\n\t$base = max( 25, 1200 - ( $i * 30 ) );\n\t$total = $base + mt_rand( 0, 150 );\n\n\t$wpdb->insert(\n\t\t$total_table,\n\t\tarray(\n\t\t\t'postnumber' => $post_id,\n\t\t\t'cntaccess' => $total,\n\t\t\t'blog_id' => $blog_id,\n\t\t),\n\t\tarray( '%d', '%d', '%d' )\n\t);\n\n\t// Spread part of the total across the last 7 days for the daily / range views.\n\tfor ( $d = 0; $d < 7; $d++ ) {\n\t\t$wpdb->insert(\n\t\t\t$daily_table,\n\t\t\tarray(\n\t\t\t\t'postnumber' => $post_id,\n\t\t\t\t'cntaccess' => mt_rand( 1, max( 2, (int) ( $total / 20 ) ) ),\n\t\t\t\t'dp_date' => gmdate( 'Y-m-d H:i:s', strtotime( \"-{$d} days\" ) ),\n\t\t\t\t'blog_id' => $blog_id,\n\t\t\t),\n\t\t\tarray( '%d', '%d', '%s', '%d' )\n\t\t);\n\t}\n}\n\n/*\n * ---------------------------------------------------------------------------\n * 3. A front-end demo page that lists popular posts via the shortcode, so the\n * blueprint can land somewhere that visibly shows the plugin working.\n * ---------------------------------------------------------------------------\n */\n$existing = get_page_by_path( 'popular-posts' );\n$page_args = array(\n\t'post_title' => 'Popular Posts',\n\t'post_name' => 'popular-posts',\n\t'post_status' => 'publish',\n\t'post_type' => 'page',\n\t'post_content' => \"<!-- wp:shortcode -->\\n[tptn_list heading=\\\"0\\\" limit=\\\"10\\\"]\\n<!-- /wp:shortcode -->\",\n);\nif ( $existing ) {\n\t$page_args['ID'] = $existing->ID;\n}\nwp_insert_post( $page_args );\n\n// Pretty permalinks + flush so /popular-posts/ resolves for the landing page.\nupdate_option( 'permalink_structure', '/%postname%/' );\nflush_rewrite_rules( false );\n"
38+
}
39+
]
40+
}

.playground/blueprint.json

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,40 @@
11
{
2-
"$schema": "https://playground.wordpress.net/blueprint-schema.json",
3-
"meta": {
4-
"title": "WebberZone Top 10 — Demo",
5-
"description": "Try WebberZone Top 10 with pre-imported posts and seeded view counts.",
6-
"author": "webberzone"
7-
},
8-
"landingPage": "/popular-posts/",
9-
"preferredVersions": {
10-
"php": "8.3",
11-
"wp": "latest"
12-
},
13-
"features": {
14-
"networking": true
15-
},
16-
"login": true,
17-
"steps": [
18-
{
19-
"step": "installPlugin",
20-
"pluginData": {
21-
"resource": "wordpress.org/plugins",
22-
"slug": "top-10"
23-
},
24-
"options": {
25-
"activate": true
26-
}
27-
},
28-
{
29-
"step": "importWxr",
30-
"file": {
31-
"resource": "url",
32-
"url": "https://raw.githubusercontent.com/WebberZone/top-10/master/.playground/demo-content.xml"
33-
}
34-
},
35-
{
36-
"step": "runPHP",
37-
"code": "<?php\nrequire '/wordpress/wp-load.php';\n\n// Get current settings or use defaults\n$settings = function_exists('tptn_get_settings') ? tptn_get_settings() : (array) get_option('tptn_settings', array());\n\n// Merge with demo settings\n$settings = array_merge($settings, array(\n\t'add_to' => 'single,page,home',\n\t'limit' => 10,\n\t'disp_list_count' => true,\n\t'show_excerpt' => true,\n\t'excerpt_length' => 15,\n\t'show_date' => true,\n\t'title_length' => 60,\n\t'thumb_default_show' => true\n));\n\nupdate_option('tptn_settings', $settings);\necho 'Settings updated.';\n"
38-
},
39-
{
40-
"step": "runPHP",
41-
"code": "<?php\nrequire '/wordpress/wp-load.php';\nglobal $wpdb;\n\n$funnel_table = $wpdb->base_prefix . 'top_ten_visits_funnel';\n$daily_table = $wpdb->base_prefix . 'top_ten_daily';\n$full_table = $wpdb->base_prefix . 'top_ten';\n$blog_id = get_current_blog_id();\n\n// Get published posts\n$posts = get_posts(array(\n\t'post_type' => array('post', 'page'),\n\t'post_status' => 'publish',\n\t'posts_per_page' => -1,\n\t'fields' => 'ids',\n\t'orderby' => 'date',\n\t'order' => 'DESC'\n));\n\nif (empty($posts)) {\n\techo 'No posts found.';\n\treturn;\n}\n\n// Seed funnel table with visit data\nforeach ($posts as $index => $post_id) {\n\t$base_count = max(25, 1200 - ($index * 30));\n\t$random_add = mt_rand(0, 150);\n\t$total_count = $base_count + $random_add;\n\t\n\t// Add multiple visits to funnel (simulating real traffic)\n\tfor ($i = 0; $i < $total_count; $i++) {\n\t\t$wpdb->insert($funnel_table, array(\n\t\t\t'postnumber' => $post_id,\n\t\t\t'blog_id' => $blog_id,\n\t\t\t'visited_at' => current_time('mysql'),\n\t\t\t'activate_counter' => 11, // Both daily and total\n\t\t\t'source' => 0 // Web traffic\n\t\t), array('%d', '%d', '%s', '%d', '%d'));\n\t}\n}\n\necho 'Seeded ' . count($posts) . ' posts with funnel data.';\n"
42-
},
43-
{
44-
"step": "runPHP",
45-
"code": "<?php\nrequire '/wordpress/wp-load.php';\n\n// Create Popular Posts page\n$page_id = wp_insert_post(array(\n\t'post_title' => 'Popular Posts',\n\t'post_name' => 'popular-posts',\n\t'post_status' => 'publish',\n\t'post_type' => 'page',\n\t'post_content' => '<!-- wp:shortcode -->[tptn_list heading=\"0\" limit=\"10\"]<!-- /wp:shortcode -->'\n));\n\n// Set pretty permalinks\nupdate_option('permalink_structure', '/%postname%/');\nflush_rewrite_rules(false);\n\necho 'Created Popular Posts page (ID: ' . $page_id . ') and set permalinks.';\n"
46-
}
47-
]
48-
}
2+
"$schema": "https://playground.wordpress.net/blueprint-schema.json",
3+
"meta": {
4+
"title": "WebberZone Top 10 — Demo",
5+
"description": "Try WebberZone Top 10 with pre-imported posts and seeded view counts.",
6+
"author": "webberzone"
7+
},
8+
"landingPage": "/popular-posts/",
9+
"preferredVersions": {
10+
"php": "8.3",
11+
"wp": "latest"
12+
},
13+
"features": {
14+
"networking": true
15+
},
16+
"login": true,
17+
"steps": [
18+
{
19+
"step": "installPlugin",
20+
"pluginData": {
21+
"resource": "wordpress.org/plugins",
22+
"slug": "top-10"
23+
},
24+
"options": {
25+
"activate": true
26+
}
27+
},
28+
{
29+
"step": "importWxr",
30+
"file": {
31+
"resource": "url",
32+
"url": "https://raw.githubusercontent.com/WebberZone/top-10/master/.playground/demo-content.xml"
33+
}
34+
},
35+
{
36+
"step": "runPHP",
37+
"code": "<?php\nrequire '/wordpress/wp-load.php';\n/**\n * Top 10 — WordPress Playground demo seeder.\n *\n * Assumes WordPress is already loaded (the blueprint requires wp-load.php\n * before requiring this file). Run once, after the demo content has been\n * imported and the plugin activated. Safe to re-run: count tables are\n * cleared before reseeding and the demo page is upserted by slug.\n *\n * @package WebberZone\\Top_Ten\n */\n\nglobal $wpdb;\n\n/*\n * ---------------------------------------------------------------------------\n * 1. Settings — start from the plugin defaults, then override a few keys so\n * the demo shows counts, excerpts and dates out of the box.\n * ---------------------------------------------------------------------------\n */\n$settings = (array) get_option( 'tptn_settings', array() );\n\n$settings = array_merge(\n\t$settings,\n\tarray(\n\t\t'add_to' => 'single,page,home',\n\t\t'limit' => 10,\n\t\t'disp_list_count' => true,\n\t\t'show_excerpt' => true,\n\t\t'excerpt_length' => 15,\n\t\t'show_date' => true,\n\t\t'title_length' => 60,\n\t\t'thumb_default_show' => true,\n\t)\n);\n\nupdate_option( 'tptn_settings', $settings );\n\n/*\n * ---------------------------------------------------------------------------\n * 2. View counts — seed the total and daily tables so the popular-posts\n * queries return data immediately (a fresh import has zero views).\n * ---------------------------------------------------------------------------\n */\n$total_table = $wpdb->base_prefix . 'top_ten';\n$daily_table = $wpdb->base_prefix . 'top_ten_daily';\n$blog_id = get_current_blog_id();\n\n$post_ids = get_posts(\n\tarray(\n\t\t'post_type' => array( 'post', 'page' ),\n\t\t'post_status' => 'publish',\n\t\t'posts_per_page' => -1,\n\t\t'fields' => 'ids',\n\t\t'orderby' => 'date',\n\t\t'order' => 'DESC',\n\t)\n);\n\nif ( empty( $post_ids ) ) {\n\treturn;\n}\n\n// Clear any prior demo rows (DELETE — SQLite under Playground has no TRUNCATE).\n$wpdb->query( \"DELETE FROM {$total_table}\" ); // phpcs:ignore WordPress.DB\n$wpdb->query( \"DELETE FROM {$daily_table}\" ); // phpcs:ignore WordPress.DB\n\nmt_srand( 42 ); // Deterministic counts so the demo looks the same every spin-up.\n\nforeach ( $post_ids as $i => $post_id ) {\n\t// Stagger totals so the ranking looks intentional, with a little jitter.\n\t$base = max( 25, 1200 - ( $i * 30 ) );\n\t$total = $base + mt_rand( 0, 150 );\n\n\t$wpdb->insert(\n\t\t$total_table,\n\t\tarray(\n\t\t\t'postnumber' => $post_id,\n\t\t\t'cntaccess' => $total,\n\t\t\t'blog_id' => $blog_id,\n\t\t),\n\t\tarray( '%d', '%d', '%d' )\n\t);\n\n\t// Spread part of the total across the last 7 days for the daily / range views.\n\tfor ( $d = 0; $d < 7; $d++ ) {\n\t\t$wpdb->insert(\n\t\t\t$daily_table,\n\t\t\tarray(\n\t\t\t\t'postnumber' => $post_id,\n\t\t\t\t'cntaccess' => mt_rand( 1, max( 2, (int) ( $total / 20 ) ) ),\n\t\t\t\t'dp_date' => gmdate( 'Y-m-d H:i:s', strtotime( \"-{$d} days\" ) ),\n\t\t\t\t'blog_id' => $blog_id,\n\t\t\t),\n\t\t\tarray( '%d', '%d', '%s', '%d' )\n\t\t);\n\t}\n}\n\n/*\n * ---------------------------------------------------------------------------\n * 3. A front-end demo page that lists popular posts via the shortcode, so the\n * blueprint can land somewhere that visibly shows the plugin working.\n * ---------------------------------------------------------------------------\n */\n$existing = get_page_by_path( 'popular-posts' );\n$page_args = array(\n\t'post_title' => 'Popular Posts',\n\t'post_name' => 'popular-posts',\n\t'post_status' => 'publish',\n\t'post_type' => 'page',\n\t'post_content' => \"<!-- wp:shortcode -->\\n[tptn_list heading=\\\"0\\\" limit=\\\"10\\\"]\\n<!-- /wp:shortcode -->\",\n);\nif ( $existing ) {\n\t$page_args['ID'] = $existing->ID;\n}\nwp_insert_post( $page_args );\n\n// Pretty permalinks + flush so /popular-posts/ resolves for the landing page.\nupdate_option( 'permalink_structure', '/%postname%/' );\nflush_rewrite_rules( false );\n"
38+
}
39+
]
40+
}

.playground/seed.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
* the demo shows counts, excerpts and dates out of the box.
2323
* ---------------------------------------------------------------------------
2424
*/
25-
$settings = function_exists( 'tptn_get_settings' )
26-
? tptn_get_settings()
27-
: (array) get_option( 'tptn_settings', array() );
25+
$settings = (array) get_option( 'tptn_settings', array() );
2826

2927
$settings = array_merge(
3028
$settings,

includes/class-database.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -785,14 +785,12 @@ public static function aggregate_visit_log( $batch_size = 10000 ) {
785785
$r = $wpdb->query(
786786
$wpdb->prepare(
787787
"INSERT INTO {$daily_table} (postnumber, cntaccess, dp_date, blog_id)
788-
SELECT * FROM (
789-
SELECT postnumber, COUNT(*) AS cntaccess,
790-
DATE_FORMAT(visited_at, '%%Y-%%m-%%d %%H:00:00') AS dp_date, blog_id
791-
FROM {$funnel_table}
792-
WHERE id <= %d AND activate_counter IN (10, 11)
793-
GROUP BY postnumber, DATE_FORMAT(visited_at, '%%Y-%%m-%%d %%H:00:00'), blog_id
794-
) AS new_row
795-
ON DUPLICATE KEY UPDATE cntaccess = {$daily_table}.cntaccess + new_row.cntaccess",
788+
SELECT postnumber, COUNT(*) AS cntaccess,
789+
DATE_FORMAT(visited_at, '%%Y-%%m-%%d %%H:00:00') AS dp_date, blog_id
790+
FROM {$funnel_table}
791+
WHERE id <= %d AND activate_counter IN (10, 11)
792+
GROUP BY postnumber, DATE_FORMAT(visited_at, '%%Y-%%m-%%d %%H:00:00'), blog_id
793+
ON DUPLICATE KEY UPDATE cntaccess = {$daily_table}.cntaccess + VALUES(cntaccess)",
796794
$max_id
797795
)
798796
);
@@ -806,13 +804,11 @@ public static function aggregate_visit_log( $batch_size = 10000 ) {
806804
$r = $wpdb->query(
807805
$wpdb->prepare(
808806
"INSERT INTO {$full_table} (postnumber, cntaccess, blog_id)
809-
SELECT * FROM (
810-
SELECT postnumber, COUNT(*) AS cntaccess, blog_id
811-
FROM {$funnel_table}
812-
WHERE id <= %d AND activate_counter IN (1, 11)
813-
GROUP BY postnumber, blog_id
814-
) AS new_row
815-
ON DUPLICATE KEY UPDATE cntaccess = {$full_table}.cntaccess + new_row.cntaccess",
807+
SELECT postnumber, COUNT(*) AS cntaccess, blog_id
808+
FROM {$funnel_table}
809+
WHERE id <= %d AND activate_counter IN (1, 11)
810+
GROUP BY postnumber, blog_id
811+
ON DUPLICATE KEY UPDATE cntaccess = {$full_table}.cntaccess + VALUES(cntaccess)",
816812
$max_id
817813
)
818814
);

0 commit comments

Comments
 (0)