-
Notifications
You must be signed in to change notification settings - Fork 1
/
admin-compass-demo-setup.php
227 lines (189 loc) · 7.68 KB
/
admin-compass-demo-setup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
// File: admin-compass-demo-setup.php
if (!defined('WP_CLI') || !WP_CLI) {
return;
}
/**
* Manages demo content for Admin Compass plugin testing.
*/
class Admin_Compass_Demo_Setup {
private function get_unsplash_image($query = 'nature', $width = 800, $height = 600) {
$url = "https://source.unsplash.com/random/{$width}x{$height}/?{$query}";
$response = wp_remote_get($url);
if (is_wp_error($response)) {
return false;
}
return wp_remote_retrieve_header($response, 'location');
}
/**
* Cleans existing content and sets up a demo environment for Admin Compass.
*
* ## OPTIONS
*
* [--posts=<number>]
* : Number of demo posts to create.
* ---
* default: 10
* ---
*
* [--pages=<number>]
* : Number of demo pages to create.
* ---
* default: 5
* ---
*
* ## EXAMPLES
*
* wp admin-compass setup_demo
* wp admin-compass setup_demo --posts=15 --pages=7
*
* @when after_wp_load
*/
public function setup_demo($args, $assoc_args) {
$post_count = $assoc_args['posts'] ?? 10;
$page_count = $assoc_args['pages'] ?? 5;
$media_count = $assoc_args['media'] ?? 20;
WP_CLI::log('Starting demo environment setup for Admin Compass...');
// Clean existing content
$this->clean_existing_content();
// Create demo posts
$this->create_demo_posts($post_count);
// Create demo pages
$this->create_demo_pages($page_count);
// Create demo media
$this->create_demo_media($media_count);
// Update options for demo
$this->update_options();
WP_CLI::success('Demo environment setup complete!');
}
private function clean_existing_content() {
WP_CLI::log('Cleaning existing content...');
// Remove all posts
$posts = get_posts(['numberposts' => -1]);
foreach ($posts as $post) {
wp_delete_post($post->ID, true);
}
// Remove all pages
$pages = get_pages(['number' => -1]);
foreach ($pages as $page) {
wp_delete_post($page->ID, true);
}
WP_CLI::log('Existing content removed.');
}
private function create_demo_posts($count) {
WP_CLI::log("Creating $count demo posts...");
$post_titles = [
'Getting Started with WordPress Plugin Development',
'Understanding WordPress Hooks and Filters',
'Best Practices for Secure Plugin Development',
'Creating Custom Post Types in WordPress',
'Integrating with the WordPress REST API',
'Optimizing Your Plugin for Performance',
'Internationalization and Localization in WordPress Plugins',
'Debugging Techniques for WordPress Plugins',
'Creating Settings Pages for Your Plugin',
'Leveraging WordPress Transients for Better Performance',
'Unit Testing Your WordPress Plugin',
'Creating Custom Widgets in WordPress',
'Using Composer in WordPress Plugin Development',
'Integrating JavaScript and CSS in WordPress Plugins',
'Creating Custom Gutenberg Blocks',
];
for ($i = 0; $i < $count; $i++) {
$title = $post_titles[$i % count($post_titles)];
$content = "This is a demo post about $title. It demonstrates the search functionality of the Admin Compass plugin.";
// Fetch an image from Unsplash
$image_url = $this->get_unsplash_image('technology', 800, 600);
if ($image_url) {
// Download the image and add it to the media library
$upload = media_sideload_image($image_url, 0, $title, 'id');
if (!is_wp_error($upload)) {
$content .= "\n\n" . wp_get_attachment_image($upload, 'large');
}
}
$post_id = wp_insert_post([
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'post',
]);
if (is_wp_error($post_id)) {
WP_CLI::warning("Failed to create post: $title");
} else {
if ($upload && !is_wp_error($upload)) {
set_post_thumbnail($post_id, $upload);
}
WP_CLI::log("Created post: $title");
}
}
}
private function create_demo_pages($count) {
WP_CLI::log("Creating $count demo pages...");
$page_titles = [
'About Our Plugin Development Blog',
'WordPress Plugin Development Resources',
'Contact Us for Custom Plugin Development',
'Our Plugin Development Process',
'Frequently Asked Questions about Plugin Development',
'WordPress Plugin Development Services',
'Plugin Development Case Studies',
];
for ($i = 0; $i < $count; $i++) {
$title = $page_titles[$i % count($page_titles)];
$content = "This is a demo page about $title. It showcases the Admin Compass plugin's ability to search pages.";
// Fetch an image from Unsplash
$image_url = $this->get_unsplash_image('office', 1200, 800);
if ($image_url) {
// Download the image and add it to the media library
$upload = media_sideload_image($image_url, 0, $title, 'id');
if (!is_wp_error($upload)) {
$content .= "\n\n" . wp_get_attachment_image($upload, 'large');
}
}
$page_id = wp_insert_post([
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page',
]);
if (is_wp_error($page_id)) {
WP_CLI::warning("Failed to create page: $title");
} else {
if ($upload && !is_wp_error($upload)) {
set_post_thumbnail($page_id, $upload);
}
WP_CLI::log("Created page: $title");
}
}
}
private function create_demo_media($count) {
WP_CLI::log("Creating $count demo media items...");
$queries = ['technology', 'nature', 'business', 'food', 'travel'];
for ($i = 0; $i < $count; $i++) {
$query = $queries[$i % count($queries)];
$title = ucfirst($query) . " Image " . ($i + 1);
$image_url = $this->get_unsplash_image($query, 1200, 800);
if ($image_url) {
$upload = media_sideload_image($image_url, 0, $title, 'id');
if (!is_wp_error($upload)) {
$attachment_url = wp_get_attachment_url($upload);
WP_CLI::log("Created media item: $title ($attachment_url)");
} else {
WP_CLI::warning("Failed to create media item: $title");
}
} else {
WP_CLI::warning("Failed to fetch image for: $title");
}
}
}
private function update_options() {
WP_CLI::log('Updating WordPress options...');
update_option('blogname', 'WordPress Plugin Development Blog');
update_option('blogdescription', 'Insights and tutorials on creating powerful WordPress plugins');
update_option('admin_email', 'admin@example.com');
WP_CLI::log('WordPress options updated.');
}
}
WP_CLI::add_command('admin-compass', 'Admin_Compass_Demo_Setup');