-
Notifications
You must be signed in to change notification settings - Fork 1
/
admin-compass-load-test.php
190 lines (170 loc) · 6.72 KB
/
admin-compass-load-test.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
<?php
// File: admin-compass-load-test.php
if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
return;
}
/**
* Generates test content for Admin Compass load testing.
*/
class Admin_Compass_Load_Test {
private $title_words = [
'Innovative', 'Strategic', 'Dynamic', 'Sustainable', 'Efficient',
'Creative', 'Productive', 'Optimized', 'Advanced', 'Integrated',
'Customized', 'Responsive', 'Versatile', 'Streamlined', 'Adaptive',
'Scalable', 'Robust', 'Intuitive', 'Seamless', 'Comprehensive'
];
private $content_words = [
'implement', 'utilize', 'integrate', 'streamline', 'optimize',
'leverage', 'innovate', 'generate', 'cultivate', 'iterate',
'synthesize', 'deploy', 'brand', 'grow', 'target',
'revolutionize', 'transform', 'embrace', 'enable', 'orchestrate',
'conceptualize', 'redefine', 'aggregate', 'architect', 'enhance',
'incentivize', 'morph', 'empower', 'envisioneer', 'monetize',
'harness', 'facilitate', 'seize', 'disintermediate', 'synergize',
'strategize', 'deploy', 'engage', 'maximize', 'benchmark',
'expedite', 'reintermediate', 'whiteboard', 'visualize', 'repurpose',
'innovate', 'scale', 'unleash', 'drive', 'extend',
'engineer', 'revolutionize', 'generate', 'exploit', 'transition',
'e-enable', 'iterate', 'cultivate', 'matrix', 'productize',
'redefine', 'recontextualize', 'transform', 'embrace', 'enable',
'orchestrate', 'leverage', 'reinvent', 'aggregate', 'architect',
'enhance', 'incentivize', 'morph', 'empower', 'envisioneer',
'monetize', 'harness', 'facilitate', 'seize', 'disintermediate',
'synergize', 'strategize', 'deploy', 'brand', 'grow',
'target', 'syndicate', 'synthesize', 'deliver', 'mesh'
];
/**
* Generates a specified number of posts, pages, and products.
*
* ## OPTIONS
*
* <type>
* : The type of content to generate (post, page, product, or all).
*
* <count>
* : Number of items to generate.
*
* ## EXAMPLES
*
* wp admin-compass generate post 100
* wp admin-compass generate page 50
* wp admin-compass generate product 200
* wp admin-compass generate all 100
*
* @when after_wp_load
*/
public function generate( $args, $assoc_args ) {
list( $type, $count ) = $args;
$count = intval($count);
switch ( $type ) {
case 'post':
$this->generate_posts( $count );
break;
case 'page':
$this->generate_pages( $count );
break;
case 'product':
$this->generate_products( $count );
break;
case 'all':
$this->generate_posts( $count );
$this->generate_pages( $count );
$this->generate_products( $count );
break;
default:
WP_CLI::error( "Invalid content type. Use 'post', 'page', 'product', or 'all'." );
}
}
private function generate_posts( $count ) {
for ( $i = 0; $i < $count; $i++ ) {
$title = $this->generate_title('Post');
$content = $this->generate_content();
$post_id = wp_insert_post( array(
'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 {
WP_CLI::success( "Created post: $title" );
}
}
}
private function generate_pages( $count ) {
for ( $i = 0; $i < $count; $i++ ) {
$title = $this->generate_title('Page');
$content = $this->generate_content();
$page_id = wp_insert_post( array(
'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 {
WP_CLI::success( "Created page: $title" );
}
}
}
private function generate_products( $count ) {
if ( ! class_exists( 'WooCommerce' ) ) {
WP_CLI::error( "WooCommerce is not active. Cannot generate products." );
return;
}
for ( $i = 0; $i < $count; $i++ ) {
$title = $this->generate_title('Product');
$description = $this->generate_content();
$product = new WC_Product_Simple();
$product->set_name( $title );
$product->set_description( $description );
$product->set_short_description( substr($description, 0, 100) . '...' );
$product->set_status( "publish" );
$product->set_price( rand(10, 100) );
$product->set_regular_price( rand(10, 100) );
$product_id = $product->save();
if ( ! $product_id ) {
WP_CLI::warning( "Failed to create product: $title" );
} else {
WP_CLI::success( "Created product: $title" );
}
}
}
private function generate_title($prefix) {
$word_count = rand(3, 5);
$words = array_rand($this->title_words, $word_count);
if (!is_array($words)) {
$words = [$words];
}
$title_words = array_map(function($index) {
return $this->title_words[$index];
}, $words);
return $prefix . ': ' . implode(' ', $title_words) . ' ' . uniqid();
}
private function generate_content() {
$paragraph_count = rand(3, 7);
$content = '';
for ($i = 0; $i < $paragraph_count; $i++) {
$sentence_count = rand(3, 8);
$paragraph = '';
for ($j = 0; $j < $sentence_count; $j++) {
$word_count = rand(8, 15);
$words = array_rand($this->content_words, $word_count);
if (!is_array($words)) {
$words = [$words];
}
$sentence_words = array_map(function($index) {
return $this->content_words[$index];
}, $words);
$paragraph .= ucfirst(implode(' ', $sentence_words)) . '. ';
}
$content .= $paragraph . "\n\n";
}
return rtrim($content);
}
}
WP_CLI::add_command( 'admin-compass', 'Admin_Compass_Load_Test' );