WP_Attachment.php
3.79 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
<?php
namespace FakerPress\Provider;
use Faker\Provider\Base;
class WP_Attachment extends Base {
public function post_type() {
return 'attachment';
}
protected static $default = [
'ping_status' => [ 'closed', 'open' ],
'comment_status' => [ 'closed', 'open' ],
];
/**
* Hold the default width and height for the diff providers.
*
* @since 0.1.0
* @since 0.5.0 now it's a public static var.
*
* @var array
*/
public static $type_defaults = [
'placeholdit' => [
'width' => [ 200, 640 ],
'height' => 1.25,
],
'lorempixel' => [
'width' => [ 200, 640 ],
'height' => 1.25,
],
'lorempicsum' => [
'width' => [ 1024, 1440 ],
'height' => 1.5,
],
];
public function attachment_url( $type = 'lorempicsum', $args = [] ) {
$url = '';
// Check if defaults exists
if ( ! isset( self::$type_defaults[ $type ] ) ){
return $url;
}
$args = wp_parse_args( $args, self::$type_defaults[ $type ] );
if ( 'placeholdit' === $type ){
$url = call_user_func_array( [ $this->generator, 'placeholdit' ], (array) $args );
} elseif ( 'lorempicsum' === $type ){
$url = call_user_func_array( [ $this->generator, 'lorempicsum' ], (array) $args );
} elseif ( 'lorempixel' === $type ){
$url = call_user_func_array( [ $this->generator, 'lorempixel' ], (array) $args );
}
return $url;
}
public function post_title( $qty_words = 5 ) {
$title = $this->generator->sentence( $qty_words );
$title = substr( $title, 0, strlen( $title ) - 1 );
return $title;
}
public function post_content( $html = true, $args = [] ) {
if ( true === $html ){
$content = implode( "\n", $this->generator->html_elements( $args ) );
} else {
$content = implode( "\r\n\r\n", $this->generator->paragraphs( $this->generator->randomDigitNotNull() ) );
}
return $content;
}
public function post_author( $haystack = [] ) {
if ( empty( $haystack ) ){
$haystack = get_users(
[
'blog_id' => get_current_blog_id(),
'count_total' => false,
'fields' => 'ID', // When you pass only one field it returns an array of the values
]
);
}
return $this->generator->randomElement( (array) $haystack );
}
public function post_status() {
return 'inherit';
}
public function post_parent( $haystack = [], $rate = 70 ) {
return $this->generator->numberBetween( 0, 100 ) < absint( $rate ) ? 0 : $this->generator->randomElement( (array) $haystack );
}
public function ping_status( $haystack = [] ) {
if ( empty( $haystack ) ){
$haystack = static::$default['ping_status'];
}
return $this->generator->randomElement( (array) $haystack );
}
public function comment_status( $haystack = [] ) {
if ( empty( $haystack ) ){
$haystack = static::$default['comment_status'];
}
return $this->generator->randomElement( (array) $haystack );
}
public function menu_order( $haystack = [] ) {
if ( empty( $haystack ) ){
return 0;
}
return $this->generator->randomElement( (array) $haystack );
}
public function post_password( $generator = null, $args = [] ) {
if ( is_null( $generator ) ){
return '';
}
return call_user_func_array( $generator, $args );
}
public function post_date( $min = 'now', $max = null ) {
// Unfortunatelly there is not such solution to this problem, we need to try and catch with DateTime
try {
$min = new \Carbon\Carbon( $min );
} catch ( \Exception $e ) {
return null;
}
if ( ! is_null( $max ) ){
// Unfortunatelly there is not such solution to this problem, we need to try and catch with DateTime
try {
$max = new \Carbon\Carbon( $max );
} catch ( \Exception $e ) {
return null;
}
}
if ( ! is_null( $max ) ) {
$selected = $this->generator->dateTimeBetween( (string) $min, (string) $max )->format( 'Y-m-d H:i:s' );
} else {
$selected = (string) $min;
}
return $selected;
}
}