feat(core): initialize DummyLab plugin boilerplate with generator and purge functionality

This commit is contained in:
idtemankampus-creator
2026-09-17 22:31:38 +07:00
commit fc6e5083d3
2 changed files with 192 additions and 0 deletions
+2
View File
@@ -0,0 +1,2 @@
vendor/
*.log
+190
View File
@@ -0,0 +1,190 @@
<?php
/**
* Plugin Name: DummyLab Dummy Post & Content Generator
* Plugin URI: https://wordpress.org/plugins/dummylab-wp/
* Description: Generates safe dummy posts for development & layout testing with one-click cleanup feature.
* Version: 1.0.0
* Author: A. Budi Kusuma
* Text Domain: dummylab
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly.
}
class DummyLab {
private static $instance = null;
private $meta_key = '_is_dummylab_post';
public static function get_instance() {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct() {
add_action('admin_menu', [$this, 'add_admin_menu']);
add_action('admin_post_dummylab_generate', [$this, 'handle_generate']);
add_action('admin_post_dummylab_purge', [$this, 'handle_purge']);
}
public function add_admin_menu() {
add_management_page(
'DummyLab Generator',
'DummyLab',
'manage_options',
'dummylab',
[$this, 'render_admin_page']
);
}
public function render_admin_page() {
if (!current_user_can('manage_options')) {
return;
}
// Count existing dummy posts
$query = new WP_Query([
'post_type' => 'post',
'posts_per_page' => -1,
'meta_key' => $this->meta_key,
'meta_value' => '1',
'fields' => 'ids',
]);
$dummy_count = $query->found_posts;
?>
<div class="wrap">
<h1>🧪 DummyLab Post Generator</h1>
<p>Generate dummy posts safely for design testing and layout development.</p>
<hr />
<?php if (isset($_GET['status'])): ?>
<div class="notice notice-success is-dismissible">
<p>
<?php
if ($_GET['status'] === 'generated') {
echo esc_html(intval($_GET['count'])) . ' dummy posts generated successfully!';
} elseif ($_GET['status'] === 'purged') {
echo esc_html(intval($_GET['count'])) . ' dummy posts deleted successfully!';
}
?>
</p>
</div>
<?php endif; ?>
<div style="display: flex; gap: 20px; margin-top: 20px;">
<!-- Generate Form -->
<div class="card" style="max-width: 400px; width: 100%;">
<h2>Generate Dummy Posts</h2>
<form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
<?php wp_nonce_field('dummylab_generate_action', 'dummylab_nonce'); ?>
<input type="hidden" name="action" value="dummylab_generate">
<p>
<label for="post_count"><strong>Number of Posts:</strong></label><br>
<input type="number" id="post_count" name="post_count" value="5" min="1" max="50" class="regular-text" required>
</p>
<p>
<label for="post_status"><strong>Post Status:</strong></label><br>
<select name="post_status" id="post_status">
<option value="publish">Publish</option>
<option value="draft">Draft</option>
</select>
</p>
<submit_button>
<?php submit_button('Generate Posts', 'primary', 'submit', false); ?>
</form>
</div>
<!-- Purge / Cleanup Form -->
<div class="card" style="max-width: 400px; width: 100%; border-left: 4px solid #dc3232;">
<h2>Cleanup / Purge Data</h2>
<p>Currently active dummy posts: <strong><?php echo esc_html($dummy_count); ?></strong></p>
<form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
<?php wp_nonce_field('dummylab_purge_action', 'dummylab_nonce'); ?>
<input type="hidden" name="action" value="dummylab_purge">
<p class="description">This action will permanently delete all posts created by DummyLab.</p>
<?php submit_button('Delete All Dummy Posts', 'delete', 'submit', false, ['disabled' => ($dummy_count === 0)]); ?>
</form>
</div>
</div>
</div>
<?php
}
public function handle_generate() {
if (!current_user_can('manage_options') || !check_admin_referer('dummylab_generate_action', 'dummylab_nonce')) {
wp_die('Unauthorized request.');
}
$count = isset($_POST['post_count']) ? min(50, max(1, intval($_POST['post_count']))) : 5;
$status = isset($_POST['post_status']) && in_array($_POST['post_status'], ['publish', 'draft']) ? $_POST['post_status'] : 'publish';
$titles = [
'Exploring the Future of Web Architecture',
'10 Essential Tips for Responsive Design',
'Understanding High-Performance Server Configurations',
'A Deep Dive into Modern CMS Solutions',
'Optimizing Database Queries for Scalability',
'Building Secure Application Infrastructure'
];
$paragraphs = [
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.',
'Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
'Curabitur pretium tincidunt lacus. Nulla gravida orci a odio. Nullam varius, turpis et commodo pharetra, est eros bibendum elit, nec luctus magna felis sollicitudin mauris.'
];
for ($i = 0; $i < $count; $i++) {
$random_title = $titles[array_rand($titles)] . ' #' . rand(100, 999);
$random_content = '<p>' . $paragraphs[array_rand($paragraphs)] . '</p><p>' . $paragraphs[array_rand($paragraphs)] . '</p>';
$post_id = wp_insert_post([
'post_title' => $random_title,
'post_content' => $random_content,
'post_status' => $status,
'post_type' => 'post',
'post_author' => get_current_user_id(),
]);
if ($post_id && !is_wp_error($post_id)) {
update_post_meta($post_id, $this->meta_key, '1');
}
}
wp_redirect(admin_url('tools.php?page=dummylab&status=generated&count=' . $count));
exit;
}
public function handle_purge() {
if (!current_user_can('manage_options') || !check_admin_referer('dummylab_purge_action', 'dummylab_nonce')) {
wp_die('Unauthorized request.');
}
$query = new WP_Query([
'post_type' => 'post',
'posts_per_page' => -1,
'meta_key' => $this->meta_key,
'meta_value' => '1',
'fields' => 'ids',
]);
$purged_count = 0;
if (!empty($query->posts)) {
foreach ($query->posts as $post_id) {
if (wp_delete_post($post_id, true)) {
$purged_count++;
}
}
}
wp_redirect(admin_url('tools.php?page=dummylab&status=purged&count=' . $purged_count));
exit;
}
}
DummyLab::get_instance();