WP_Meta.php
11.5 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<?php
namespace FakerPress\Provider;
use Faker\Provider\Base;
use FakerPress;
use FakerPress\Utils;
class WP_Meta extends Base {
public $meta_object = [
'name' => 'post',
'id' => 0,
];
public function set_meta_object( $name, $id ) {
$this->meta_object = (object) $this->meta_object;
$this->meta_object->name = $name;
$this->meta_object->id = $id;
}
private function meta_parse_qty( $qty, $elements = null ) {
$_qty = array_filter( (array) $qty );
$min = reset( $_qty );
$qty = (int) ( is_array( $qty ) && count( $_qty ) > 1 ? call_user_func_array( [ $this->generator, 'numberBetween' ], $qty ) : reset( $_qty ) );
if ( $qty < $min ) {
$qty = $min;
}
if ( is_array( $elements ) && $qty > count( $elements ) ) {
$qty = count( $elements );
}
return $qty;
}
private function meta_parse_separator( $separator ) {
$separator = stripcslashes( $separator );
$search = [
'\n',
'\r',
'\t',
];
$replace = [
"\n",
"\r",
"\t",
];
$separator = str_replace( $search, $replace, $separator );
return $separator;
}
public function meta_type_numbers( $number = [ 0, 9 ], $weight = 50 ) {
$number = ( is_array( $number ) ? call_user_func_array( [ $this->generator, 'numberBetween' ], $number ) : $number );
return $this->generator->optional( (int) $weight, null )->randomElement( (array) $number );
}
public function meta_type_elements( $elements = '', $qty = 1, $separator = ',', $weight = 50 ) {
$separator = $this->meta_parse_separator( $separator );
$elements = explode( ',', $elements );
$qty = $this->meta_parse_qty( $qty, $elements );
$value = $this->generator->optional( (int) $weight, null )->randomElements( (array) $elements, $qty );
if ( is_null( $value ) ) {
return $value;
}
return implode( $separator, $value );
}
public function meta_type_letter( $weight = 50 ) {
return $this->generator->optional( (int) $weight, null )->randomLetter();
}
public function meta_type_words( $qty = 8, $weight = 50 ) {
$qty = $this->meta_parse_qty( $qty );
$sentence = $this->generator->optional( (int) $weight, '' )->sentence( $qty );
return Utils::instance()->remove_sentence_period( $sentence );
}
public function meta_type_text( $type = 'sentences', $qty = 3, $separator = "\r\n\r\n", $weight = 50 ) {
$separator = $this->meta_parse_separator( $separator );
$qty = $this->meta_parse_qty( $qty );
if ( 'sentences' === $type ) {
$value = $this->generator->optional( (int) $weight, null )->sentences( $qty );
} else {
$value = $this->generator->optional( (int) $weight, null )->paragraphs( $qty );
}
if ( is_null( $value ) ) {
return $value;
}
return implode( $separator, $value );
}
public function meta_type_html( $elements, $qty = 6, $weight = 50 ) {
$qty = $this->meta_parse_qty( $qty );
$elements = explode( ',', $elements );
$value = $this->generator->optional( (int) $weight, null )->html_elements( [
'elements' => $elements,
'qty' => $qty,
] );
if ( is_null( $value ) ) {
return $value;
}
return implode( "\n", $value );
}
public function meta_type_wp_query( $query, $weight = 50 ) {
$args = wp_parse_args( $query, [] );
$args['fields'] = 'ids';
// Make easier for Attachment Queries
if ( isset( $args['post_type'] ) && ! isset( $args['post_status'] ) && in_array( 'attachment', (array) $args['post_type'] ) ) {
$args['post_status'] = 'any';
}
$query = new \WP_Query( $args );
if ( ! $query->have_posts() ) {
return null;
}
$value = $this->generator->optional( (int) $weight, null )->randomElement( (array) $query->posts );
return $value;
}
public function meta_type_attachment( $type, $providers, $weight = 50, $width = [], $height = [] ) {
$providers = array_map( 'esc_attr', array_map( 'trim', explode( ',', $providers ) ) );
$provider = $this->generator->randomElement( $providers );
$attachment = FakerPress\Module\Attachment::instance();
$arguments = [];
// Specially for Meta we do the Randomization here
if ( ! empty( $width ) && $this->meta_parse_qty( $width ) ) {
$arguments['width'] = $this->meta_parse_qty( $width );
}
// Specially for Meta we do the Randomization here
if ( ! empty( $height ) && $this->meta_parse_qty( $height ) ) {
$arguments['height'] = $this->meta_parse_qty( $height );
}
// Generate the Attachment
$attachment->set( 'attachment_url', $provider, $arguments );
// If it's meta for a post we need to mark the attachment as child of that post
if ( 'post' === $this->meta_object->name ) {
$attachment->set( 'post_parent', $this->meta_object->id, 1 );
}
// Actually save the Attachment and get it's ID
$value = $attachment->generate()->save();
// If there was an error, just bail now
if ( ! $value ) {
return null;
}
// If asked URL, change to URL
if ( 'url' === $type ) {
$value = wp_get_attachment_url( $value );
}
// Apply Weight
$value = $this->generator->optional( (int) $weight, null )->randomElement( (array) $value );
return $value;
}
public function meta_type_lexify( $template, $weight = 50 ) {
$value = $this->generator->optional( (int) $weight, null )->bothify( (string) $template );
return $value;
}
public function meta_type_asciify( $template, $weight = 50 ) {
$value = $this->generator->optional( (int) $weight, null )->asciify( (string) $template );
return $value;
}
public function meta_type_regexify( $template, $weight = 50 ) {
$value = $this->generator->optional( (int) $weight, null )->regexify( (string) $template );
return $value;
}
public function meta_type_timezone( $weight = 50 ) {
$value = $this->generator->optional( (int) $weight, null )->timezone;
return $value;
}
public function meta_type_company( $template, $weight = 50 ) {
$template = explode( '|', $template );
$tags = [
'suffix',
'company',
'bs',
'catch_phrase',
];
$text = [];
foreach ( $template as $key => $tag ) {
preg_match( '|^\{\% *([^\ ]*) *\%\}$|i', $tag, $_parsed );
if ( ! empty( $_parsed ) ) {
list( $element, $term ) = $_parsed;
switch ( $term ) {
case 'suffix':
$text[] = $this->generator->companySuffix;
break;
case 'company':
$text[] = $this->generator->company;
break;
case 'bs':
$text[] = $this->generator->bs;
break;
case 'catch_phrase':
$text[] = $this->generator->catchPhrase;
break;
}
} else {
$text[] = ( empty( $tag ) ? ' ' : $tag );
}
}
$value = $this->generator->optional( (int) $weight, null )->randomElement( (array) implode( '', $text ) );
return $value;
}
public function meta_type_person( $template, $gender = 'female', $weight = 50 ) {
$template = explode( '|', $template );
$tags = [
'title',
'first_name',
'last_name',
'suffix',
];
$text = [];
foreach ( $template as $key => $tag ) {
preg_match( '|^\{\% *([^\ ]*) *\%\}$|i', $tag, $_parsed );
if ( ! empty( $_parsed ) ) {
list( $element, $term ) = $_parsed;
switch ( $term ) {
case 'title':
$text[] = $this->generator->title( $gender );
break;
case 'first_name':
$text[] = $this->generator->firstName( $gender );
break;
case 'last_name':
$text[] = $this->generator->lastName;
break;
case 'suffix':
$text[] = $this->generator->suffix;
break;
}
} else {
$text[] = ( empty( $tag ) ? ' ' : $tag );
}
}
$value = $this->generator->optional( (int) $weight, null )->randomElement( (array) implode( '', $text ) );
return $value;
}
public function meta_type_geo( $template, $weight = 50 ) {
$template = explode( '|', $template );
$tags = [
'country',
'country_code',
'country_abbr',
'city_prefix',
'city_suffix',
'city',
'state',
'state_abbr',
'address',
'secondary_address',
'building_number',
'street_name',
'street_address',
'postalcode',
'latitude',
'longitude',
];
$text = [];
foreach ( $template as $key => $tag ) {
preg_match( '|^\{\% *([^\ ]*) *\%\}$|i', $tag, $_parsed );
if ( ! empty( $_parsed ) ) {
list( $element, $term ) = $_parsed;
switch ( $term ) {
case 'country':
$text[] = $this->generator->country;
break;
case 'country_code':
$text[] = Utils::get_country_alpha_code( $this->generator->country, 2 );
break;
case 'country_abbr':
$text[] = Utils::get_country_alpha_code( $this->generator->country, 3 );
break;
case 'city_prefix':
$text[] = $this->generator->cityPrefix;
break;
case 'city_suffix':
$text[] = $this->generator->citySuffix;
break;
case 'city':
$text[] = $this->generator->city;
break;
case 'state':
$text[] = $this->generator->state;
break;
case 'state_abbr':
$text[] = $this->generator->stateAbbr;
break;
case 'address':
$text[] = $this->generator->address;
break;
case 'secondary_address':
$text[] = $this->generator->secondaryAddress;
break;
case 'building_number':
$text[] = $this->generator->buildingNumber;
break;
case 'street_name':
$text[] = $this->generator->streetName;
break;
case 'street_address':
$text[] = $this->generator->streetAddress;
break;
case 'postalcode':
$text[] = $this->generator->postcode;
break;
case 'latitude':
$text[] = $this->generator->latitude;
break;
case 'longitude':
$text[] = $this->generator->longitude;
break;
}
} else {
$text[] = ( empty( $tag ) ? ' ' : $tag );
}
}
$value = $this->generator->optional( (int) $weight, null )->randomElement( (array) implode( '', $text ) );
return $value;
}
public function meta_type_date( $interval, $format = 'Y-m-d H:i:s', $weight = 50 ) {
$interval = (array) $interval;
// Unfortunately there is not such solution to this problem, we need to try and catch with DateTime
try {
$min = new \Carbon\Carbon( $interval['min'] );
} catch ( \Exception $e ) {
$min = new \Carbon\Carbon( 'today' );
$min = $min->startOfDay();
}
if ( ! empty( $interval ) ) {
// Unfortunately there is not such solution to this problem, we need to try and catch with DateTime
try {
$max = new \Carbon\Carbon( $interval['max'] );
} catch ( \Exception $e ) {}
}
if ( ! isset( $max ) ) {
$max = new \Carbon\Carbon( 'now' );
}
// If max has no Time set it to the end of the day
$max_has_time = array_filter( [ $max->hour, $max->minute, $max->second ] );
$max_has_time = ! empty( $max_has_time );
if ( ! $max_has_time ) {
$max = $max->endOfDay();
}
$selected = $this->generator->dateTimeBetween( (string) $min, (string) $max )->format( $format );
$value = $this->generator->optional( (int) $weight, null )->randomElement( (array) $selected );
return $value;
}
public function meta_type_ip( $weight = 50 ) {
$value = $this->generator->optional( (int) $weight, null )->ipv4;
return $value;
}
public function meta_type_domain( $weight = 50 ) {
$value = $this->generator->optional( (int) $weight, null )->domainName;
return $value;
}
public function meta_type_email( $weight = 50 ) {
$value = $this->generator->optional( (int) $weight, null )->email;
return $value;
}
public function meta_type_user_agent( $weight = 50 ) {
$value = $this->generator->optional( (int) $weight, null )->userAgent;
return $value;
}
public function meta_type_raw( $weight = 100, $value = null, $default = null ) {
if ( (int) $weight >= $this->generator->numberBetween( 0, 100 ) ) {
return $value;
} else {
return $default;
}
}
}