WP_Comment.php
4.45 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
<?php
namespace FakerPress\Provider;
use Faker\Provider\Base;
use FakerPress\Utils;
class WP_Comment extends Base {
public function comment_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( Utils::instance()->get_qty_from_range( $args['qty'] ) ) );
}
return $content;
}
public function user_id( $users = [] ) {
// We only need to query if there is no users passed
if ( is_array( $users ) && empty( $users ) ) {
$users = get_users(
[
'blog_id' => $GLOBALS['blog_id'],
'count_total' => false,
'fields' => 'ID', // When you pass only one field it returns an array of the values
]
);
}
// Cast $users as an array and always return an absolute integer
$user_id = absint( $this->generator->randomElement( (array) $users ) );
return $user_id;
}
public function comment_author( $comment_author = null ) {
// Lacks the method to random a bunch of elements
return $this->generator->name();
}
public function comment_parent( $comment_parent = null ) {
// Lacks the method to random a bunch of elements
return absint( $comment_parent );
}
/**
* Generate a random comment type with the values given
* Converts 'default' into an empty string for default post comments
*
* @since 0.4.8
*
* @param array|string $comment_type Possible comment types to pick from
*
* @return string
*/
public function comment_type( $comment_type = null ) {
// Fetch a Random element from the possible comment types
$comment_type = $this->generator->randomElement( (array) $comment_type );
if ( 'default' === $comment_type || is_null( $comment_type ) ) {
$comment_type = '';
}
return $comment_type;
}
public function comment_author_IP( $ip = null ) {
if ( is_null( $ip ) ) {
$ip = $this->generator->ipv4;
}
return $ip;
}
public function comment_agent( $user_agent = null ) {
if ( is_null( $user_agent ) ) {
$user_agent = $this->generator->userAgent;
}
return $user_agent;
}
public function comment_approved( $comment_approved = 1 ) {
return $comment_approved;
}
/**
* Generates a Post ID for the Comment
*
* @since 0.1.0
* @since 0.4.8 Argument `$args` to allow custom Post Types
*
* @param array|int $comment_post_ID Which ids you want to use
* @param array $args WP_Query args for Searching these Posts
*
* @return int
*/
public function comment_post_ID( $comment_post_ID = null, $args = [] ) {
if ( is_null( $comment_post_ID ) ) {
// We should be able to pass these arguments
$defaults = [
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true,
];
// Apply the defaults
$args = wp_parse_args( $args, $defaults );
// We need only the IDs here
$args['fields'] = 'ids';
$query = new \WP_Query( $args );
if ( $query->found_posts ) {
$comment_post_ID = absint( $this->generator->randomElement( $query->posts, 1 ) );
}
// We need to check if there is no posts, should we include the comment anyways?
}
return $comment_post_ID;
}
public function comment_author_email( $author_email = null ) {
if ( is_null( $author_email ) ) {
$author_email = $this->generator->safeEmail;
$author_email = substr( $author_email, 0, strlen( $author_email ) - 1 );
}
return $author_email;
}
public function comment_author_url( $author_url = null ) {
if ( is_null( $author_url ) ) {
$author_url = $this->generator->url;
$author_url = substr( $author_url, 0, strlen( $author_url ) - 1 );
}
return $author_url;
}
public function comment_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;
}
}