+ "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"
0 commit comments