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_Comment.php

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

180 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace FakerPress\Provider;
3
4 use FakerPress\ThirdParty\Faker\Provider\Base;
5 use FakerPress\ThirdParty\Cake\Chronos\Chronos;
6 use FakerPress\Utils;
7 use function FakerPress\make;
8
9 class WP_Comment extends Base {
10
11 public function comment_content( $html = true, $args = [] ) {
12 $defaults = [
13 'qty' => [ 5, 15 ],
14 ];
15 $args = wp_parse_args( $args, $defaults );
16
17 if ( true === $html ) {
18 $content = implode( "\n", $this->generator->html_elements( $args ) );
19 } else {
20 $content = implode( "\r\n\r\n", $this->generator->paragraphs( make( Utils::class )->get_qty_from_range( $args['qty'] ) ) );
21 }
22
23 return $content;
24 }
25
26 public function user_id( $users = [] ) {
27 // We only need to query if there is no users passed
28 if ( is_array( $users ) && empty( $users ) ) {
29 $users = get_users(
30 [
31 'blog_id' => $GLOBALS['blog_id'],
32 'count_total' => false,
33 'fields' => 'ID', // When you pass only one field it returns an array of the values
34 ]
35 );
36 }
37
38 // Cast $users as an array and always return an absolute integer
39 $user_id = absint( $this->generator->randomElement( (array) $users ) );
40
41 return $user_id;
42 }
43
44 public function comment_author( $comment_author = null ) {
45 // Lacks the method to random a bunch of elements
46 return $this->generator->name();
47 }
48
49 public function comment_parent( $comment_parent = null ) {
50 // Lacks the method to random a bunch of elements
51 return absint( $comment_parent );
52 }
53
54 /**
55 * Generate a random comment type with the values given
56 * Converts 'default' into an empty string for default post comments
57 *
58 * @since 0.4.8
59 *
60 * @param array|string $comment_type Possible comment types to pick from
61 *
62 * @return string
63 */
64 public function comment_type( $comment_type = null ) {
65 // Fetch a Random element from the possible comment types
66 $comment_type = $this->generator->randomElement( (array) $comment_type );
67
68 if ( 'default' === $comment_type || is_null( $comment_type ) ) {
69 $comment_type = '';
70 }
71
72 return $comment_type;
73 }
74
75 public function comment_author_IP( $ip = null ) {
76 if ( is_null( $ip ) ) {
77 $ip = $this->generator->ipv4;
78 }
79
80 return $ip;
81 }
82
83 public function comment_agent( $user_agent = null ) {
84 if ( is_null( $user_agent ) ) {
85 $user_agent = $this->generator->userAgent;
86 }
87
88 return $user_agent;
89 }
90
91 public function comment_approved( $comment_approved = 1 ) {
92
93 return $comment_approved;
94 }
95
96 /**
97 * Generates a Post ID for the Comment
98 *
99 * @since 0.1.0
100 * @since 0.4.8 Argument `$args` to allow custom Post Types
101 *
102 * @param array|int $comment_post_ID Which ids you want to use
103 * @param array $args WP_Query args for Searching these Posts
104 *
105 * @return int
106 */
107 public function comment_post_ID( $comment_post_ID = null, $args = [] ) {
108
109 if ( is_null( $comment_post_ID ) ) {
110 // We should be able to pass these arguments
111 $defaults = [
112 'posts_per_page' => -1,
113 'post_type' => 'post',
114 'post_status' => 'publish',
115 'suppress_filters' => true,
116 ];
117
118 // Apply the defaults
119 $args = wp_parse_args( $args, $defaults );
120
121 // We need only the IDs here
122 $args['fields'] = 'ids';
123
124 $query = new \WP_Query( $args );
125
126 if ( $query->found_posts ) {
127 $comment_post_ID = absint( $this->generator->randomElement( $query->posts, 1 ) );
128 }
129
130 // We need to check if there is no posts, should we include the comment anyways?
131 }
132
133 return $comment_post_ID;
134 }
135
136 public function comment_author_email( $author_email = null ) {
137 if ( is_null( $author_email ) ) {
138 $author_email = $this->generator->safeEmail;
139 $author_email = substr( $author_email, 0, strlen( $author_email ) - 1 );
140 }
141
142 return $author_email;
143 }
144
145 public function comment_author_url( $author_url = null ) {
146 if ( is_null( $author_url ) ) {
147 $author_url = $this->generator->url;
148 $author_url = substr( $author_url, 0, strlen( $author_url ) - 1 );
149 }
150
151 return $author_url;
152 }
153
154 public function comment_date( $min = 'now', $max = null ) {
155 // Unfortunatelly there is not such solution to this problem, we need to try and catch with DateTime
156 try {
157 $min = new Chronos( $min );
158 } catch ( \Exception $e ) {
159 return null;
160 }
161
162 if ( ! is_null( $max ) ) {
163 // Unfortunatelly there is not such solution to this problem, we need to try and catch with DateTime
164 try {
165 $max = new Chronos( $max );
166 } catch ( \Exception $e ) {
167 return null;
168 }
169 }
170
171 if ( ! is_null( $max ) ) {
172 $selected = $this->generator->dateTimeBetween( (string) $min, (string) $max )->format( 'Y-m-d H:i:s' );
173 } else {
174 $selected = (string) $min;
175 }
176
177 return $selected;
178 }
179 }
180