]
61 | * : The type of generator to use. Passing aioseo generators posts with aioseo meta.
62 | * ---
63 | * default: default
64 | * ---
65 | *
66 | * ## EXAMPLES
67 | *
68 | * wp faker core content
69 | *
70 | * @when after_wp_load
71 | *
72 | * @param string[] $args The command line arguments.
73 | * @param array $assoc_args The associative command line arguments.
74 | */
75 | public function content( $args, $assoc_args ) {
76 | $generator = Generator_Factory::get_core_generator( $assoc_args['type'] );
77 |
78 | $author_ids = generate_with_progress(
79 | 'author',
80 | (int) $assoc_args['authors'],
81 | function() use ( $generator ) {
82 | return $generator->generate_user();
83 | }
84 | );
85 | $category_ids = generate_with_progress(
86 | 'category',
87 | (int) $assoc_args['categories'],
88 | function( $i, $ids ) use ( $generator ) {
89 | return $generator->generate_term( 'category', $ids );
90 | }
91 | );
92 |
93 | $tag_ids = generate_with_progress(
94 | 'tag',
95 | (int) $assoc_args['tags'],
96 | function() use ( $generator ) {
97 | return $generator->generate_term( 'post_tag' );
98 | }
99 | );
100 |
101 | $attachment_keyword = $assoc_args['attachment-keyword'];
102 | $attachment_ids = generate_with_progress(
103 | 'attachment',
104 | (int) $assoc_args['attachments'],
105 | function( $i ) use ( $generator, $attachment_keyword ) {
106 | return $generator->generate_attachment( 640, 480, $attachment_keyword, "$attachment_keyword$i.jpg" );
107 | }
108 | );
109 |
110 | generate_with_progress(
111 | 'post',
112 | (int) $assoc_args['posts'],
113 | function() use ( $generator, $author_ids, $attachment_ids, $category_ids, $tag_ids ) {
114 | return $generator->generate_post( 'post', $author_ids, $attachment_ids, [], $category_ids, $tag_ids );
115 | }
116 | );
117 |
118 | generate_with_progress(
119 | 'page',
120 | (int) $assoc_args['pages'],
121 | function( $i, $ids ) use ( $generator, $author_ids, $attachment_ids, $category_ids, $tag_ids ) {
122 | return $generator->generate_post( 'page', $author_ids, $attachment_ids, $ids, $category_ids, $tag_ids );
123 | }
124 | );
125 | }
126 | }
127 |
128 | if ( class_exists( WP_CLI::class ) ) {
129 | WP_CLI::add_command( 'faker core', Core::class );
130 | }
131 |
--------------------------------------------------------------------------------
/src/factories/generator-factory.php:
--------------------------------------------------------------------------------
1 | generate_aioseo_fields( $post_id );
65 | $this->insert_data_into_aioseo_table( $aioseo_fields );
66 | $this->insert_aioseo_data_into_post_meta_table( $post_id, $aioseo_fields );
67 |
68 | return $post_id;
69 | }
70 |
71 | /**
72 | * Generates random All-in-One SEO data in the form of an associative array
73 | * mapping AiOSEO field name to value.
74 | *
75 | * @param int $post_id The ID of the post to generate data for.
76 | *
77 | * @return array Generated AiOSEO data.
78 | */
79 | private function generate_aioseo_fields( $post_id ) {
80 | $replace_vars = \array_merge(
81 | self::REPLACE_VARS,
82 | $this->get_custom_field_replace_vars( $post_id )
83 | );
84 |
85 | return [
86 | // "id" => 0, // Auto increment
87 | "post_id" => $post_id,
88 | "title" => $this->generate_aioseo_text_with_replace_vars( $replace_vars ),
89 | "description" => $this->generate_aioseo_text_with_replace_vars( $replace_vars ),
90 | "og_title" => $this->generate_aioseo_text_with_replace_vars( $replace_vars ),
91 | "og_description" => $this->generate_aioseo_text_with_replace_vars( $replace_vars ),
92 | "twitter_title" => $this->generate_aioseo_text_with_replace_vars( $replace_vars ),
93 | "twitter_description" => $this->generate_aioseo_text_with_replace_vars( $replace_vars ),
94 | "keywords" => null, // We don't offer keywords functionality.
95 | /**
96 | * {
97 | * "focus":{
98 | * "keyphrase":"Test",
99 | * "score": 0, // not important
100 | * "analysis": "" // not important
101 | * },
102 | * "additional":[
103 | * {
104 | * "keyphrase": "Additional",
105 | * "score": 0 // not important
106 | * "analysis": "" // not important
107 | * }
108 | * ]
109 | * }
110 | */
111 | "keyphrases" => null, // JSON, see above.
112 | "page_analysis" => null, // JSON, Not important, since we can't use a different scoring mechanism.
113 | "canonical_url" => null, // string
114 | /**
115 | * See link for object types.
116 | *
117 | * https://github.com/awesomemotive/all-in-one-seo-pack/blob/4a4a5224a2ce3c87d2f6c154a487ea013119c551/src/vue/plugins/constants.js#L1158
118 | */
119 | "og_object_type" => "default", // string
120 | /**
121 | * See link for image types.
122 | *
123 | * https://github.com/awesomemotive/all-in-one-seo-pack/blob/4a4a5224a2ce3c87d2f6c154a487ea013119c551/src/vue/mixins/Image.js#L53
124 | *
125 | * Special remarks:
126 | * - "custom" / Image from Custom Field (Saved in og_image_custom_fields)
127 | * - "custom_image" / Custom Image (Saved in og_image_custom_url)
128 | */
129 | "og_image_type" => "default", // string
130 | "og_image_custom_url" => null, // string, value used as og:image when "custom_image" is selected as og_image_type
131 | "og_image_custom_fields" => null, // string, key of the custom field to be used as og:image when og_image_type "custom" is selected
132 | "og_custom_image_width" => 0, // int
133 | "og_custom_image_height" => 0, // int
134 | "og_video" => null, // ?
135 | "og_custom_url" => null, // ?
136 | "og_article_section" => "", // string, Output in a "article:section" metatag.
137 | /**
138 | * [
139 | * {
140 | * "label":"tag1",
141 | * "value":"tag1"
142 | * },
143 | * {
144 | * "label":"tag2",
145 | * "value":"tag2"
146 | * },
147 | * ]
148 | */
149 | "og_article_tags" => "", // JSON, each tag's value is output in a separate "article:tags" metatag. See above.
150 | "twitter_use_og" => 0, // Boolean, corresponds to the "Use Data from Facebook Tab" toggle on the twitter tab.
151 | /**
152 | * - "default" / Default (Set under Social Networks)
153 | * - "summary" / Summary
154 | * - "summary_large_image" / Summary with Large Image
155 | */
156 | "twitter_card" => "default",
157 | "twitter_image_type" => "default", // Same options as og_image_type, see above.
158 | "twitter_image_custom_url" => null, // string, value used as twitter:image when "custom_image" is selected as twitter_image_type
159 | "twitter_image_custom_fields" => null, // string, key of the custom field to be used as twitter:image when twitter_image_type "custom" is selected
160 | "schema_type" => "default", // string
161 | /**
162 | * {
163 | * "article":{
164 | * "articleType":"BlogPosting"
165 | * },
166 | * "course":{
167 | * "name":"",
168 | * "description":"",
169 | * "provider":""
170 | * },
171 | * "faq":{
172 | * "pages":[
173 | *
174 | * ]
175 | * },
176 | * "product":{
177 | * "reviews":[
178 | *
179 | * ]
180 | * },
181 | * "recipe":{
182 | * "ingredients":[
183 | *
184 | * ],
185 | * "instructions":[
186 | *
187 | * ],
188 | * "keywords":[
189 | *
190 | * ]
191 | * },
192 | * "software":{
193 | * "reviews":[
194 | *
195 | * ],
196 | * "operatingSystems":[
197 | *
198 | * ]
199 | * },
200 | * "webPage":{
201 | * "webPageType":"WebPage"
202 | * }
203 | * }
204 | */
205 | "schema_type_options" => null, // JSON, See above.
206 | "pillar_content" => 0, // Boolean
207 | "robots_default" => 1, // Boolean, whether or not to use default robots settings.
208 | "robots_noindex" => 0, // Boolean
209 | "robots_noarchive" => 0, // Boolean
210 | "robots_nosnippet" => 0, // Boolean
211 | "robots_nofollow" => 0, // Boolean
212 | "robots_noimageindex" => 0, // Boolean
213 | "robots_noodp" => 0, // Boolean
214 | "robots_notranslate" => 0, // Boolean
215 | "robots_max_snippet" => -1, // int
216 | "robots_max_videopreview" => -1, // int
217 | "robots_max_imagepreview" => "large", // string
218 | "tabs" => null, // JSON
219 | "images" => null, // JSON, Array of images with metadata in the post.
220 | "priority" => "default", // string
221 | "image_scan_date" => null, // Date string
222 | "frequency" => "default", // string
223 | "videos" => null, // JSON, Array of videos with metadata in the post.
224 | "video_scan_date" => null, // Date string
225 | // "created" => null, // Date string, defaults to current date
226 | // "updated" => null, // Date string, defaults to current date
227 | ];
228 | }
229 |
230 | /**
231 | * Inserts the given generated All-in-One SEO data into their
232 | * own purposefully built database table.
233 | *
234 | * @param array $aioseo_data The generated AiOSEO data.
235 | *
236 | * @return void
237 | */
238 | private function insert_data_into_aioseo_table( $aioseo_data ) {
239 | global $wpdb;
240 |
241 | $wpdb->query(
242 | $wpdb->prepare(
243 | "
244 | INSERT INTO {$wpdb->prefix}aioseo_posts
245 | (" . \implode( ",", \array_keys( $aioseo_data ) ) . ")
246 | VALUES
247 | (" . \implode( ', ', \array_fill( 0, count( $aioseo_data ), '%s' ) ) . ")
248 | ",
249 | \array_values( $aioseo_data )
250 | )
251 | );
252 | }
253 |
254 | /**
255 | * Inserts the given All-in-One SEO data into the post meta table.
256 | *
257 | * @param int $post_id The ID of the post to insert the data for.
258 | * @param array $aioseo_data The AiOSEO data to insert.
259 | *
260 | * @return void
261 | */
262 | private function insert_aioseo_data_into_post_meta_table( $post_id, $aioseo_data ) {
263 | foreach ( self::META_KEYS as $meta_key ) {
264 | $db_key = str_replace( "_aioseo_", "", $meta_key );
265 |
266 | \add_post_meta( $post_id, $meta_key, $aioseo_data[ $db_key ] );
267 | }
268 | }
269 |
270 | /**
271 | * Creates a list of AIOSEO replacement variables for the post's custom fields.
272 | *
273 | * @param int $post_id The post id.
274 | *
275 | * @return string[] List of AIOSEO replacement variables for custom fields.
276 | */
277 | private function get_custom_field_replace_vars( $post_id ) {
278 | $custom_field_keys = \array_filter(
279 | \get_post_custom_keys( $post_id ),
280 | static function( $key ) {
281 | return \strpos( $key, self::$custom_field_prefix ) === 0;
282 | }
283 | );
284 |
285 | return \array_map(
286 | static function( $key ) {
287 | return '#custom_field-' . $key;
288 | },
289 | $custom_field_keys
290 | );
291 | }
292 |
293 | /**
294 | * Generates a random title or description using AIOSEO replacement variables.
295 | *
296 | * @param array $replace_vars Array of replacevars to use.
297 | *
298 | * @return string The generated replacevar string.
299 | */
300 | private function generate_aioseo_text_with_replace_vars( $replace_vars ) {
301 | $items = \array_merge( $this->faker->words( 12 ), $replace_vars );
302 | return \implode( ' ', $this->faker->randomElements( $items, $this->faker->numberBetween( 2, 5 ) ) );
303 | }
304 | }
305 |
--------------------------------------------------------------------------------
/src/generators/core-generator.php:
--------------------------------------------------------------------------------
1 | faker = $faker;
31 | }
32 |
33 | /**
34 | * Generates a user.
35 | *
36 | * @param string $role The role of the user.
37 | *
38 | * @return int The user id.
39 | *
40 | * @throws Exception If the user could not be saved.
41 | */
42 | public function generate_user( $role = 'author' ) {
43 | $author_id = \wp_insert_user(
44 | [
45 | 'user_login' => $this->faker->unique()->userName,
46 | 'user_pass' => $this->faker->password,
47 | 'user_url' => $this->faker->url,
48 | 'user_email' => $this->faker->email,
49 | 'first_name' => $this->faker->firstName,
50 | 'last_name' => $this->faker->lastName,
51 | 'description' => $this->faker->paragraph,
52 | 'user_registered' => $this->faker->dateTimeThisCentury->format( 'Y-m-d H:i:s' ),
53 | 'role' => $role,
54 | ]
55 | );
56 |
57 | if ( is_wp_error( $author_id ) ) {
58 | throw new Exception( $author_id->get_error_message() );
59 | }
60 |
61 | return $author_id;
62 | }
63 |
64 | /**
65 | * Generates an attachment.
66 | *
67 | * @param int $width They width of the image.
68 | * @param int $height The height of the image.
69 | * @param string $keyword The keyword to search loremflicker.com for.
70 | * @param string $file_name The file name to save the image as.
71 | *
72 | * @return int The attachment id.
73 | *
74 | * @throws Exception If the image could not be downloaded or saved.
75 | */
76 | public function generate_attachment( $width, $height, $keyword, $file_name ) {
77 | $file_array = [];
78 | $file_array['name'] = $file_name;
79 |
80 | // Download file to temp location.
81 | $file_array['tmp_name'] = download_url( "https://loremflickr.com/$width/$height/$keyword" );
82 | if ( is_wp_error( $file_array['tmp_name'] ) ) {
83 | throw new Exception( $file_array['tmp_name']->get_error_message() );
84 | }
85 |
86 | // Do the validation and storage stuff.
87 | $attachment_id = media_handle_sideload( $file_array, 0, null );
88 |
89 | if ( is_wp_error( $attachment_id ) ) {
90 | throw new Exception( $attachment_id->get_error_message() );
91 | }
92 |
93 | return $attachment_id;
94 | }
95 |
96 | /**
97 | * Generates a term
98 | *
99 | * @param string $taxonomy The taxonomy of the term.
100 | * @param array $parent_ids Optional. The list of possible parent ids. 50% a random one will be assigned.
101 | *
102 | * @return int The term id.
103 | *
104 | * @throws Exception If the term could not be saved.
105 | */
106 | public function generate_term( $taxonomy, $parent_ids = [] ) {
107 | $category = \wp_insert_term(
108 | $this->faker->unique()->catchPhrase,
109 | $taxonomy,
110 | [
111 | 'description' => $this->faker->paragraph,
112 | 'parent' => ( $this->faker->boolean && count( $parent_ids ) > 0 ) ? $this->faker->randomElement( $parent_ids ) : null,
113 | ]
114 | );
115 |
116 | if ( \is_wp_error( $category ) ) {
117 | throw new Exception( $category->get_error_message() );
118 | }
119 |
120 | return $category['term_id'];
121 | }
122 |
123 | /**
124 | * Generates a post.
125 | *
126 | * @param string $post_type The post type.
127 | * @param int[] $author_ids The possible author ids.
128 | * @param int[] $attachment_ids The possible attachment ids.
129 | * @param int[] $parent_ids The possible parent ids.
130 | * @param int[] $category_ids The possible category ids.
131 | * @param int[] $tag_ids The possible tag ids.
132 | *
133 | * @return int The post id.
134 | *
135 | * @throws Exception If the post could not be saved.
136 | */
137 | public function generate_post( $post_type, $author_ids, $attachment_ids, $parent_ids, $category_ids, $tag_ids ) {
138 | $date = $this->faker->dateTimeThisYear();
139 | $post_id = \wp_insert_post(
140 | [
141 | 'post_author' => $this->faker->randomElement( $author_ids ),
142 | 'post_date' => $date->format( 'Y-m-d H:i:s' ),
143 | 'post_content' => $this->generate_post_content( $attachment_ids ),
144 | 'post_title' => $this->faker->catchPhrase,
145 | 'post_type' => $post_type,
146 | 'post_status' => 'publish',
147 | 'post_parent' => ( $this->faker->boolean( 25 ) && count( $parent_ids ) > 0 ) ? $this->faker->randomElement( $parent_ids ) : null,
148 | 'post_modified' => ( ( $this->faker->boolean ) ? $this->faker->dateTimeBetween( $date ) : $date )->format( 'Y-m-d H:i:s' ),
149 | 'post_category' => $this->faker->randomElements( $category_ids, $this->faker->numberBetween( 1, 2 ) ),
150 | 'tags_input' => $this->faker->randomElements( $tag_ids, $this->faker->numberBetween( 0, 4 ) ),
151 | 'meta_input' => $this->generate_custom_fields(),
152 | ]
153 | );
154 |
155 | if ( \is_wp_error( $post_id ) ) {
156 | throw new Exception( $post_id->get_error_message() );
157 | }
158 |
159 | \set_post_thumbnail( $post_id, $this->faker->randomElement( $attachment_ids ) );
160 |
161 | return $post_id;
162 | }
163 |
164 | /**
165 | * Generates post content.
166 | *
167 | * @param int[] $attachment_ids The possible attachment ids.
168 | *
169 | * @return string The post content.
170 | */
171 | public function generate_post_content( $attachment_ids ) {
172 | $blocks = [];
173 | $block_count = $this->faker->numberBetween( 8, 12 );
174 |
175 | for ( $i = 0; $i < $block_count; $i++ ) {
176 | if ( $this->faker->boolean( 90 ) ) {
177 | $blocks[] = "\n" . $this->faker->paragraph . "
\n";
178 | continue;
179 | }
180 |
181 | $attachment_id = $this->faker->randomElement( $attachment_ids );
182 | $attachment_url = wp_get_attachment_url( $attachment_id );
183 | $blocks[] = "\n
\n";
184 | }
185 |
186 | return implode( "\n", $blocks );
187 | }
188 |
189 | /**
190 | * Generates custom fields with random words as a value.
191 | *
192 | * @return string[] The array of generate custom fields.
193 | */
194 | public function generate_custom_fields() {
195 | if ( count( self::$custom_field_keys ) === 0 ) {
196 | for ( $i = 0; $i < 10; $i++ ) {
197 | self::$custom_field_keys[] = self::$custom_field_prefix . $this->faker->word;
198 | }
199 | }
200 |
201 | $custom_fields = [];
202 | foreach ( self::$custom_field_keys as $key ) {
203 | $custom_fields[ $key ] = $this->faker->word;
204 | }
205 |
206 | return $custom_fields;
207 | }
208 | }
209 |
--------------------------------------------------------------------------------
/src/generators/woocommerce-generator.php:
--------------------------------------------------------------------------------
1 | faker = $faker;
28 | $this->core_generator = $core_generator;
29 | }
30 |
31 | /**
32 | * Generates a review
33 | *
34 | * @param int $product_id The product id.
35 | *
36 | * @return int The review id.
37 | *
38 | * @throws Exception If the review could not be saved.
39 | */
40 | public function generate_review( $product_id ) {
41 | $controller = new \WC_REST_Product_Reviews_Controller();
42 |
43 | $params = [
44 | 'product_id' => $product_id,
45 | 'review' => $this->faker->paragraph,
46 | 'reviewer' => $this->faker->name,
47 | 'reviewer_email' => $this->faker->email,
48 | 'rating' => $this->faker->numberBetween( 0, 5 ),
49 | 'verified' => $this->faker->boolean,
50 | ];
51 | $request = new \WP_REST_Request( 'POST' );
52 | $request->set_body_params( $params );
53 |
54 | $response = $controller->create_item( $request );
55 |
56 | if ( \is_wp_error( $response ) ) {
57 | throw new Exception( $response->get_error_message() );
58 | }
59 |
60 | return $response->data['id'];
61 | }
62 |
63 | /**
64 | * Generates a product category
65 | *
66 | * @param int[] $attachment_ids The possible attachment ids.
67 | *
68 | * @return int The product category id.
69 | *
70 | * @throws Exception If the product category could not be saved.
71 | */
72 | public function generate_category( $attachment_ids ) {
73 | $controller = new \WC_REST_Product_Categories_Controller();
74 |
75 | $params = [
76 | 'name' => $this->faker->unique()->catchPhrase,
77 | 'description' => $this->core_generator->generate_post_content( $attachment_ids ),
78 | 'image' => [ 'id' => $this->faker->randomElement( $attachment_ids ) ],
79 | ];
80 | $request = new \WP_REST_Request( 'POST' );
81 | $request->set_body_params( $params );
82 |
83 | $response = $controller->create_item( $request );
84 |
85 | if ( \is_wp_error( $response ) ) {
86 | throw new Exception( $response->get_error_message() );
87 | }
88 |
89 | return $response->data['id'];
90 | }
91 |
92 | /**
93 | * Generates a product
94 | *
95 | * @param int[] $attachment_ids The possible attachment ids.
96 | * @param int[] $category_ids The possible category ids.
97 | * @param int[] $brand_ids The possible brand ids.
98 | *
99 | * @return int The product id.
100 | *
101 | * @throws Exception If the product could not be saved.
102 | */
103 | public function generate_product( $attachment_ids, $category_ids, $brand_ids ) {
104 | $controller = new \WC_REST_Products_Controller();
105 |
106 | $params = [
107 | 'name' => $this->faker->unique()->catchPhrase,
108 | 'description' => $this->core_generator->generate_post_content( $attachment_ids ),
109 | 'status' => 'publish',
110 | 'type' => 'Simple',
111 | 'featured' => $this->faker->boolean( 10 ),
112 | 'sku' => $this->faker->numerify( '######' ),
113 | 'regular_price' => $this->faker->numberBetween( 10, 100 ),
114 | 'images' => [],
115 | 'categories' => [],
116 | ];
117 |
118 | $number_of_images = $this->faker->numberBetween( 1, 3 );
119 | for ( $i = 0; $i < $number_of_images; $i++ ) {
120 | $params['images'][] = [ 'id' => $this->faker->randomElement( $attachment_ids ) ];
121 | }
122 |
123 | $number_of_categories = $this->faker->numberBetween( 1, 2 );
124 | for ( $i = 0; $i < $number_of_categories; $i++ ) {
125 | $params['categories'][] = [ 'id' => $this->faker->randomElement( $category_ids ) ];
126 | }
127 |
128 | $number_of_brands = $this->faker->numberBetween( 1, 2 );
129 | $params['brands'] = $this->faker->randomElements( $brand_ids, $number_of_brands );
130 |
131 | $request = new \WP_REST_Request( 'POST' );
132 | $request->set_body_params( $params );
133 | $response = $controller->create_item( $request );
134 |
135 | if ( \is_wp_error( $response ) ) {
136 | throw new Exception( $response->get_error_message() );
137 | }
138 |
139 | return $response->data['id'];
140 | }
141 | }
142 |
--------------------------------------------------------------------------------
/src/helper-functions.php:
--------------------------------------------------------------------------------
1 | getMessage() );
27 | }
28 | $progress->tick();
29 | }
30 | $progress->finish();
31 |
32 | return $ids;
33 | }
34 |
--------------------------------------------------------------------------------
/src/woocommerce.php:
--------------------------------------------------------------------------------
1 | ]
21 | * : The number of attachments to generate.
22 | * ---
23 | * default: 10
24 | * ---
25 | *
26 | * [--attachment-keyword=]
27 | * : The keyword used to generate attachments.
28 | * ---
29 | * default: jewelry
30 | * ---
31 | *
32 | * [--categories=]
33 | * : The number of categories to generate.
34 | * ---
35 | * default: 25
36 | * ---
37 | *
38 | * [--brands=]
39 | * : The number of brands to generate.
40 | * ---
41 | * default: 25
42 | * ---
43 | *
44 | * [--products=]
45 | * : The number of products to generate.
46 | * ---
47 | * default: 300
48 | * ---
49 | *
50 | * [--min-reviews=]
51 | * : The minimum number of reviews to generate.
52 | * ---
53 | * default: 3
54 | * ---
55 | *
56 | * [--max-reviews=]
57 | * : The maximum number of reviews to generate.
58 | * ---
59 | * default: 8
60 | * ---
61 | *
62 | * ## EXAMPLES
63 | *
64 | * wp faker woocommerce products
65 | *
66 | * @when after_wp_load
67 | *
68 | * @param string[] $args The command line arguments.
69 | * @param array $assoc_args The associative command line arguments.
70 | */
71 | public function products( $args, $assoc_args ) {
72 | $faker = Factory::create();
73 | $core_generator = new Core_Generator( $faker );
74 | $woo_generator = new WooCommerce_Generator( $faker, $core_generator );
75 |
76 | $attachment_keyword = $assoc_args['attachment-keyword'];
77 | $attachment_ids = generate_with_progress(
78 | 'attachment',
79 | (int) $assoc_args['attachments'],
80 | function( $i ) use ( $core_generator, $attachment_keyword ) {
81 | return $core_generator->generate_attachment( 640, 480, $attachment_keyword, "$attachment_keyword$i.jpg" );
82 | }
83 | );
84 |
85 | $category_ids = generate_with_progress(
86 | 'product category',
87 | (int) $assoc_args['categories'],
88 | function() use ( $woo_generator, $attachment_ids ) {
89 | return $woo_generator->generate_category( $attachment_ids );
90 | }
91 | );
92 |
93 | $brand_ids = [];
94 | if ( \taxonomy_exists( 'pwb-brand' ) ) {
95 | $brand_ids = generate_with_progress(
96 | 'brand',
97 | (int) $assoc_args['brands'],
98 | function() use ( $core_generator, $faker, $attachment_ids ) {
99 | $id = $core_generator->generate_term( 'pwb-brand' );
100 | \update_term_meta( $id, 'pwb_brand_image', $faker->randomElement( $attachment_ids ) );
101 | return $id;
102 | }
103 | );
104 | }
105 |
106 | $product_ids = generate_with_progress(
107 | 'product',
108 | (int) $assoc_args['products'],
109 | function() use ( $woo_generator, $attachment_ids, $category_ids, $brand_ids ) {
110 | return $woo_generator->generate_product( $attachment_ids, $category_ids, $brand_ids );
111 | }
112 | );
113 |
114 | add_filter( 'wp_is_comment_flood', '__return_false', PHP_INT_MAX );
115 | add_filter( 'pre_comment_approved', '__return_true' );
116 | generate_with_progress(
117 | 'reviews',
118 | (int) $assoc_args['products'],
119 | function( $i ) use ( $woo_generator, $faker, $product_ids, $assoc_args ) {
120 | $review_ids = [];
121 | $number_of_reviews = $faker->numberBetween( $assoc_args['min-reviews'], $assoc_args['max-reviews'] );
122 | for ( $i = 0; $i < $number_of_reviews; $i++ ) {
123 | $review_ids[] = $woo_generator->generate_review( $product_ids[ $i ] );
124 | }
125 | return $review_ids;
126 | }
127 | );
128 | remove_filter( 'wp_is_comment_flood', '__return_false', PHP_INT_MAX );
129 | remove_filter( 'pre_comment_approved', '__return_true' );
130 | }
131 | }
132 |
133 | if ( class_exists( WP_CLI::class ) ) {
134 | WP_CLI::add_command( 'faker woocommerce', WooCommerce::class );
135 | }
136 |
--------------------------------------------------------------------------------
/wp-cli-faker.php:
--------------------------------------------------------------------------------
1 |