PluginProbe
FakerPress / 0.8.0
FakerPress v0.8.0
0.9.2 trunk 0.1.0 0.1.1 0.1.2 0.1.3 0.5.2 0.5.3 0.6.1 0.6.2 0.6.3 0.6.4 0.6.5 0.6.6 0.7.0 0.7.1 0.7.2 0.8.0 0.9.0 0.9.1
fakerpress / src / FakerPress / Provider / WP_Meta.php

WP_Meta.php in FakerPress 0.8.0, at src/FakerPress/Provider/WP_Meta.php

475 lines 12.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FakerPress\Provider;
4
5 use FakerPress\ThirdParty\Faker\Provider\Base;
6 use FakerPress;
7 use FakerPress\Utils;
8 use function FakerPress\make;
9
10 class WP_Meta extends Base {
11 public $meta_object = [
12 'name' => 'post',
13 'id' => 0,
14 ];
15
16 public function set_meta_object( $name, $id ) {
17 $this->meta_object = (object) $this->meta_object;
18
19 $this->meta_object->name = $name;
20 $this->meta_object->id = $id;
21 }
22
23 /**
24 * Given a number or an array of numbers, return a random number between them, and take into account the weight.
25 *
26 * @since 0.1.0
27 *
28 * @param string[]|int[]|int|string $number Range of numbers or a single number.
29 * @param string|int $weight Weight of the number.
30 *
31 * @return int
32 */
33 private function meta_parse_qty( $qty, $elements = null ) {
34 $qty = array_values( (array) $qty );
35 $_qty = array_filter( $qty );
36 $min = reset( $_qty );
37
38 $qty = (int) ( is_array( $qty ) && count( $_qty ) > 1 ? call_user_func_array( [ $this->generator, 'numberBetween' ], $qty ) : reset( $_qty ) );
39 if ( $qty < $min ) {
40 $qty = $min;
41 }
42
43 if ( is_array( $elements ) && $qty > count( $elements ) ) {
44 $qty = count( $elements );
45 }
46
47 return $qty;
48 }
49
50 private function meta_parse_separator( $separator ) {
51 $separator = stripcslashes( $separator );
52
53 $search = [
54 '\n',
55 '\r',
56 '\t',
57 ];
58 $replace = [
59 "\n",
60 "\r",
61 "\t",
62 ];
63 $separator = str_replace( $search, $replace, $separator );
64
65 return $separator;
66 }
67
68 /**
69 * Given a number or an array of numbers, return a random number between them, and take into account the weight.
70 *
71 * @since 1.0.0
72 *
73 * @param string[]|int[]|int|string $number Range of numbers or a single number.
74 * @param string|int $weight Weight of the number.
75 *
76 * @return string|int
77 */
78 public function meta_type_numbers( $number = [ 0, 9 ], $weight = 50 ) {
79 $number = array_values( array_map( 'absint', (array) $number ) );
80
81 // If the number is an array, then we assume it's a range.
82 if ( count( $number ) > 1 ) {
83 $number = $this->generator->numberBetween( ...$number );
84 }
85
86 return $this->generator->optional( $weight / 100, null )->randomElement( (array) $number );
87 }
88
89 public function meta_type_elements( $elements = '', $qty = 1, $separator = ',', $weight = 50 ) {
90 $separator = $this->meta_parse_separator( $separator );
91
92 $elements = explode( ',', $elements );
93 $qty = $this->meta_parse_qty( $qty, $elements );
94
95 $value = $this->generator->optional( $weight / 100, null )->randomElements( (array) $elements, $qty );
96 if ( is_null( $value ) ) {
97 return $value;
98 }
99
100 return implode( $separator, $value );
101 }
102
103 public function meta_type_letter( $weight = 50 ) {
104 return $this->generator->optional( $weight / 100, null )->randomLetter();
105 }
106
107 public function meta_type_words( $qty = 8, $weight = 50 ) {
108 $qty = $this->meta_parse_qty( $qty );
109 $sentence = $this->generator->optional( $weight / 100, '' )->sentence( $qty );
110
111 return make( Utils::class )->remove_sentence_period( $sentence );
112 }
113
114 public function meta_type_text( $type = 'sentences', $qty = 3, $separator = "\r\n\r\n", $weight = 50 ) {
115 $separator = $this->meta_parse_separator( $separator );
116 $qty = $this->meta_parse_qty( $qty );
117
118 if ( 'sentences' === $type ) {
119 $value = $this->generator->optional( $weight / 100, null )->sentences( $qty );
120 } else {
121 $value = $this->generator->optional( $weight / 100, null )->paragraphs( $qty );
122 }
123
124 if ( is_null( $value ) ) {
125 return $value;
126 }
127
128 return implode( $separator, $value );
129 }
130
131 public function meta_type_html( $elements, $qty = 6, $weight = 50 ) {
132 $qty = $this->meta_parse_qty( $qty );
133 $elements = explode( ',', $elements );
134
135 $value = $this->generator->optional( $weight / 100, null )->html_elements( [
136 'elements' => $elements,
137 'qty' => $qty,
138 ] );
139
140 if ( is_null( $value ) ) {
141 return $value;
142 }
143
144 return implode( "\n", $value );
145 }
146
147 public function meta_type_wp_query( $query, $weight = 50 ) {
148 $args = wp_parse_args( $query, [] );
149 $args['fields'] = 'ids';
150
151 // Make easier for Attachment Queries
152 if ( isset( $args['post_type'] ) && ! isset( $args['post_status'] ) && in_array( 'attachment', (array) $args['post_type'] ) ) {
153 $args['post_status'] = 'any';
154 }
155
156 $query = new \WP_Query( $args );
157
158 if ( ! $query->have_posts() ) {
159 return null;
160 }
161
162 $value = $this->generator->optional( $weight / 100, null )->randomElement( (array) $query->posts );
163
164 return $value;
165 }
166
167 public function meta_type_attachment( $type, $providers, $weight = 50, $width = [], $height = [] ) {
168 $providers = array_map( 'esc_attr', array_map( 'trim', explode( ',', $providers ) ) );
169
170 $provider = $this->generator->randomElement( $providers );
171
172 $attachment = make( FakerPress\Module\Attachment::class );
173
174 $arguments = [];
175
176 // Specially for Meta we do the Randomization here
177 if ( ! empty( $width ) && $this->meta_parse_qty( $width ) ) {
178 $arguments['width'] = $this->meta_parse_qty( $width );
179 }
180
181 // Specially for Meta we do the Randomization here
182 if ( ! empty( $height ) && $this->meta_parse_qty( $height ) ) {
183 $arguments['height'] = $this->meta_parse_qty( $height );
184 }
185
186 // Generate the Attachment
187 $attachment->set( 'attachment_url', $provider, $arguments );
188
189 // If it's meta for a post we need to mark the attachment as child of that post
190 if ( 'post' === $this->meta_object->name ) {
191 $attachment->set( 'post_parent', $this->meta_object->id, 1 );
192 }
193
194 // Actually save the Attachment and get it's ID
195 $value = $attachment->generate()->save();
196
197 // If there was an error, just bail now
198 if ( ! $value ) {
199 return null;
200 }
201
202 // If asked URL, change to URL
203 if ( 'url' === $type ) {
204 $value = wp_get_attachment_url( $value );
205 }
206
207 // Apply Weight
208 $value = $this->generator->optional( $weight / 100, null )->randomElement( (array) $value );
209
210 return $value;
211 }
212
213 public function meta_type_lexify( $template, $weight = 50 ) {
214 $value = $this->generator->optional( $weight / 100, null )->bothify( (string) $template );
215
216 return $value;
217 }
218
219 public function meta_type_asciify( $template, $weight = 50 ) {
220 $value = $this->generator->optional( $weight / 100, null )->asciify( (string) $template );
221
222 return $value;
223 }
224
225 public function meta_type_regexify( $template, $weight = 50 ) {
226 $value = $this->generator->optional( $weight / 100, null )->regexify( (string) $template );
227
228 return $value;
229 }
230
231 public function meta_type_timezone( $weight = 50 ) {
232 $value = $this->generator->optional( $weight / 100, null )->timezone;
233
234 return $value;
235 }
236
237 public function meta_type_company( $template, $weight = 50 ) {
238 $template = explode( '|', $template );
239
240 $tags = [
241 'suffix',
242 'company',
243 'bs',
244 'catch_phrase',
245 ];
246
247 $text = [];
248
249 foreach ( $template as $key => $tag ) {
250 preg_match( '|^\{\% *([^\ ]*) *\%\}$|i', $tag, $_parsed );
251 if ( ! empty( $_parsed ) ) {
252 [ $element, $term ] = $_parsed;
253 switch ( $term ) {
254 case 'suffix':
255 $text[] = $this->generator->companySuffix;
256 break;
257 case 'company':
258 $text[] = $this->generator->company;
259 break;
260 case 'bs':
261 $text[] = $this->generator->bs;
262 break;
263 case 'catch_phrase':
264 $text[] = $this->generator->catchPhrase;
265 break;
266 }
267 } else {
268 $text[] = ( empty( $tag ) ? ' ' : $tag );
269 }
270 }
271
272 $value = $this->generator->optional( $weight / 100, null )->randomElement( (array) implode( '', $text ) );
273
274 return $value;
275 }
276
277 public function meta_type_person( $template, $gender = 'female', $weight = 50 ) {
278 $template = explode( '|', $template );
279
280 $tags = [
281 'title',
282 'first_name',
283 'last_name',
284 'suffix',
285 ];
286
287 $text = [];
288
289 foreach ( $template as $key => $tag ) {
290 preg_match( '|^\{\% *([^\ ]*) *\%\}$|i', $tag, $_parsed );
291 if ( ! empty( $_parsed ) ) {
292 [ $element, $term ] = $_parsed;
293 switch ( $term ) {
294 case 'title':
295 $text[] = $this->generator->title( $gender );
296 break;
297 case 'first_name':
298 $text[] = $this->generator->firstName( $gender );
299 break;
300 case 'last_name':
301 $text[] = $this->generator->lastName;
302 break;
303 case 'suffix':
304 $text[] = $this->generator->suffix;
305 break;
306 }
307 } else {
308 $text[] = ( empty( $tag ) ? ' ' : $tag );
309 }
310 }
311
312 $value = $this->generator->optional( $weight / 100, null )->randomElement( (array) implode( '', $text ) );
313
314 return $value;
315 }
316
317 public function meta_type_geo( $template, $weight = 50 ) {
318 $template = explode( '|', $template );
319 $tags = [
320 'country',
321 'country_code',
322 'country_abbr',
323 'city_prefix',
324 'city_suffix',
325 'city',
326 'state',
327 'state_abbr',
328 'address',
329 'secondary_address',
330 'building_number',
331 'street_name',
332 'street_address',
333 'postalcode',
334 'latitude',
335 'longitude',
336 ];
337
338 $text = [];
339
340 foreach ( $template as $key => $tag ) {
341 preg_match( '|^\{\% *([^\ ]*) *\%\}$|i', $tag, $_parsed );
342 if ( ! empty( $_parsed ) ) {
343 [ $element, $term ] = $_parsed;
344 switch ( $term ) {
345 case 'country':
346 $text[] = $this->generator->country;
347 break;
348 case 'country_code':
349 $text[] = Utils::get_country_alpha_code( $this->generator->country, 2 );
350 break;
351 case 'country_abbr':
352 $text[] = Utils::get_country_alpha_code( $this->generator->country, 3 );
353 break;
354 case 'city_prefix':
355 $text[] = $this->generator->cityPrefix;
356 break;
357 case 'city_suffix':
358 $text[] = $this->generator->citySuffix;
359 break;
360 case 'city':
361 $text[] = $this->generator->city;
362 break;
363 case 'state':
364 $text[] = $this->generator->state;
365 break;
366 case 'state_abbr':
367 $text[] = $this->generator->stateAbbr;
368 break;
369 case 'address':
370 $text[] = $this->generator->address;
371 break;
372 case 'secondary_address':
373 $text[] = $this->generator->secondaryAddress;
374 break;
375 case 'building_number':
376 $text[] = $this->generator->buildingNumber;
377 break;
378 case 'street_name':
379 $text[] = $this->generator->streetName;
380 break;
381 case 'street_address':
382 $text[] = $this->generator->streetAddress;
383 break;
384 case 'postalcode':
385 $text[] = $this->generator->postcode;
386 break;
387 case 'latitude':
388 $text[] = $this->generator->latitude;
389 break;
390 case 'longitude':
391 $text[] = $this->generator->longitude;
392 break;
393 }
394 } else {
395 $text[] = ( empty( $tag ) ? ' ' : $tag );
396 }
397 }
398
399 $value = $this->generator->optional( $weight / 100, null )->randomElement( (array) implode( '', $text ) );
400
401 return $value;
402 }
403
404 public function meta_type_date( $interval, $format = 'Y-m-d H:i:s', $weight = 50 ) {
405 $interval = (array) $interval;
406
407 // Unfortunately there is not such solution to this problem, we need to try and catch with DateTime
408 try {
409 $min = new Chronos( $interval['min'] );
410 } catch ( \Exception $e ) {
411 $min = new Chronos( 'today' );
412 $min = $min->startOfDay();
413 }
414
415 if ( ! empty( $interval ) ) {
416 // Unfortunately there is not such solution to this problem, we need to try and catch with DateTime
417 try {
418 $max = new Chronos( $interval['max'] );
419 } catch ( \Exception $e ) {
420 }
421 }
422
423 if ( ! isset( $max ) ) {
424 $max = new Chronos( 'now' );
425 }
426
427 // If max has no Time set it to the end of the day
428 $max_has_time = array_filter( [ $max->hour, $max->minute, $max->second ] );
429 $max_has_time = ! empty( $max_has_time );
430 if ( ! $max_has_time ) {
431 $max = $max->endOfDay();
432 }
433
434 $selected = $this->generator->dateTimeBetween( (string) $min, (string) $max )->format( $format );
435
436 $value = $this->generator->optional( $weight / 100, null )->randomElement( (array) $selected );
437
438 return $value;
439 }
440
441 public function meta_type_ip( $weight = 50 ) {
442 $value = $this->generator->optional( $weight / 100, null )->ipv4;
443
444 return $value;
445 }
446
447 public function meta_type_domain( $weight = 50 ) {
448 $value = $this->generator->optional( $weight / 100, null )->domainName;
449
450 return $value;
451 }
452
453 public function meta_type_email( $weight = 50 ) {
454 $value = $this->generator->optional( $weight / 100, null )->email;
455
456 return $value;
457 }
458
459 public function meta_type_user_agent( $weight = 50 ) {
460 $value = $this->generator->optional( $weight / 100, null )->userAgent;
461
462 return $value;
463 }
464
465 public function meta_type_raw( $weight = 100, $value = null, $default = null ) {
466 if ( (int) $weight >= $this->generator->numberBetween( 0, 100 ) ) {
467 return $value;
468 } else {
469 return $default;
470 }
471
472 }
473
474 }
475