WP_Post.php
6.44 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
<?php
namespace FakerPress\Provider;
use Faker\Provider\Base;
use FakerPress;
class WP_Post extends Base {
protected static $default = [
'ping_status' => [ 'closed', 'open' ],
'comment_status' => [ 'closed', 'open' ],
];
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_type( $haystack = [] ) {
if ( empty( $haystack ) ) {
// Later on we will remove the Attachment rule
$haystack = array_diff( get_post_types( [ 'public' => true, 'show_ui' => true ], 'names' ), [ 'attachment' ] );
}
return $this->generator->randomElement( (array) $haystack );
}
public function post_status( $haystack = [ 'draft', 'publish', 'private' ] ) {
if ( empty( $haystack ) ) {
$haystack = array_values( get_post_stati() );
}
return $this->generator->randomElement( (array) $haystack );
}
public function post_date( $interval = 'now' ) {
$format = 'Y-m-d H:i:s';
$interval = (array) $interval;
// Unfortunatelly there is not such solution to this problem, we need to try and catch with DateTime
try {
$min = new \Carbon\Carbon( array_shift( $interval ) );
} catch ( \Exception $e ) {
$min = new \Carbon\Carbon( 'today' );
$min = $min->startOfDay();
}
if ( ! empty( $interval ) ) {
// Unfortunatelly there is not such solution to this problem, we need to try and catch with DateTime
try {
$max = new \Carbon\Carbon( array_shift( $interval ) );
} 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 );
return $selected;
}
public function post_content( $html = true, $args = [] ) {
$defaults = [
'qty' => [ 5, 15 ],
];
$args = wp_parse_args( $args, $defaults );
if ( true === $html ) {
$content = implode( "\n", $this->generator->html_elements( $args ) );
} else {
$content = implode( "\r\n\r\n", $this->generator->paragraphs( FakerPress\Utils::instance()->get_qty_from_range( $args['qty'] ) ) );
}
return $content;
}
/**
* Configures a Excerpt for the Post
*
* @since 0.4.9
*
* @param array|int $qty How many words we should generate (range with Array)
* @param boolean $html Should use HTML or not (currently not used)
* @param integer $weight Percentage of times where we will setup a Excerpt
*
* @return string
*/
public function post_excerpt( $qty = [ 25, 75 ], $html = false, $weight = 60 ) {
$words = FakerPress\Utils::instance()->get_qty_from_range( $qty );
$paragraphs = $this->generator->randomElement( [ 1, 1, 1, 1, 1, 2, 2, 2, 3, 4 ] );
for ( $i = 0; $i < $paragraphs; $i++ ) {
$excerpt[ $i ] = $this->generator->sentence( $words );
}
$excerpt = implode( "\n\n", $excerpt );
return $this->generator->optional( $weight, '' )->randomElement( (array) $excerpt );
}
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_parent( $haystack = [], $weight = 70 ) {
return $this->generator->optional( $weight, 0 )->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 tax_input( $config = null ) {
$output = [];
if ( is_null( $config ) ) {
return $output;
}
// The percentage of change in which the terms will be applied
$rates = apply_filters( 'fakerpress/provider/WP_Post/tax_input.rates', [
'category' => 50,
'post_tag' => 45,
'__default' => 35,
] );
// The amount of terms that might have, provide a number for exact and [ int, int ] to range
$ranges = apply_filters( 'fakerpress/provider/WP_Post/tax_input.ranges', [
'category' => [ 1, 3 ],
'post_tag' => [ 0, 15 ],
'__default' => [ 0, 3 ],
] );
foreach ( $config as $settings ) {
$settings = (object) $settings;
if ( ! empty( $settings->taxonomies ) && is_string( $settings->taxonomies ) ) {
$settings->taxonomies = explode( ',', $settings->taxonomies );
}
$settings->taxonomies = array_filter( (array) $settings->taxonomies );
if ( ! empty( $settings->terms ) && is_string( $settings->terms ) ) {
$settings->terms = explode( ',', $settings->terms );
}
$settings->terms = array_filter( (array) $settings->terms );
foreach ( $settings->taxonomies as $taxonomy ) {
if ( empty( $settings->terms ) ) {
$terms = get_terms( $taxonomy, [ 'fields' => 'ids', 'hide_empty' => false ] );
} else {
$terms = $settings->terms;
}
// Get all the term ids
$terms = array_filter( array_map( 'absint', $terms ) );
if ( ! isset( $settings->qty ) ) {
$qty = FakerPress\Utils::instance()->get_qty_from_range( ( isset( $ranges[ $taxonomy ] ) ? $ranges[ $taxonomy ] : $ranges['__default'] ), $terms );
} else {
$qty = (int) FakerPress\Utils::instance()->get_qty_from_range( $settings->qty, $terms );
}
if ( ! isset( $settings->rate ) ) {
$rate = isset( $rates[ $taxonomy ] ) ? $rates[ $taxonomy ] : $rates['__default'];
} else {
$rate = (int) $settings->rate;
}
// Select the elements based on qty
$output[ $taxonomy ] = $this->generator->optional( (int) $rate, null )->randomElements( $terms, (int) $qty );
}
}
return $output;
}
}