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 / Module / Comment.php

Comment.php in FakerPress 0.8.0, at src/FakerPress/Module/Comment.php

194 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FakerPress\Module;
4
5 use FakerPress\Plugin;
6 use FakerPress\Utils;
7 use FakerPress\ThirdParty\Faker;
8 use FakerPress;
9 use function FakerPress\make;
10 use function FakerPress\get;
11 use function FakerPress\get_request_var;
12 use function FakerPress\is_truthy;
13
14 class Comment extends Abstract_Module {
15 /**
16 * @inheritDoc
17 */
18 protected $dependencies = [
19 FakerPress\ThirdParty\Faker\Provider\Lorem::class,
20 FakerPress\ThirdParty\Faker\Provider\DateTime::class,
21 FakerPress\Provider\HTML::class,
22 ];
23
24 /**
25 * @inheritDoc
26 */
27 protected $provider_class = FakerPress\Provider\WP_Comment::class;
28
29 /**
30 * @inheritDoc
31 */
32 public static function get_slug(): string {
33 return 'comments';
34 }
35
36 /**
37 * @inheritDoc
38 */
39 public function hook(): void {
40 }
41
42 /**
43 * @inheritDoc
44 */
45 public static function fetch( array $args = [] ): array {
46 $defaults = [
47 'meta_query' => [
48 [
49 'key' => static::get_flag(),
50 'value' => true,
51 'type' => 'BINARY',
52 ],
53 ],
54 ];
55 $comments = [];
56
57 $query_comments = new \WP_Comment_Query;
58 $args = wp_parse_args( $args, $defaults );
59
60 $query_comments = $query_comments->query( $args );
61
62 foreach ( $query_comments as $comment ) {
63 $comments[] = absint( $comment->comment_ID );
64 }
65
66 return $comments;
67 }
68
69 /**
70 * @inheritDoc
71 */
72 public static function delete( $comment ) {
73 if ( is_array( $comment ) ) {
74 $deleted = [];
75
76 foreach ( $comment as $id ) {
77 $id = $id instanceof \WP_Comment ? $id->comment_ID : $id;
78
79 if ( ! is_numeric( $id ) ) {
80 continue;
81 }
82
83 $deleted[ $id ] = static::delete( $id );
84 }
85
86 return $deleted;
87 }
88
89 if ( is_numeric( $comment ) ) {
90 $comment = \WP_Comment::get_instance( $comment );
91 }
92
93 if ( ! $comment instanceof \WP_Comment ) {
94 return false;
95 }
96
97 $flag = (bool) get_comment_meta( $comment->comment_ID, static::get_flag(), true );
98
99 if ( true !== $flag ) {
100 return false;
101 }
102
103 return wp_delete_comment( $comment->comment_ID, true );
104 }
105
106 /**
107 * @inheritDoc
108 */
109 public function filter_save_response( $response, array $data, Abstract_Module $module ) {
110 $comment_id = wp_insert_comment( $data );
111
112 if ( ! is_numeric( $comment_id ) ) {
113 return false;
114 }
115
116 // Flag the Object as FakerPress
117 update_post_meta( $comment_id, static::get_flag(), 1 );
118
119 return $comment_id;
120 }
121
122 public function parse_request( $qty, $request = [] ) {
123 if ( is_null( $qty ) ) {
124 $qty = make( Utils::class )->get_qty_from_range( get_request_var( [ Plugin::$slug, 'qty' ] ) );
125 }
126
127 if ( 0 === $qty ) {
128 return esc_attr__( 'Zero is not a good number of comments to fake...', 'fakerpress' );
129 }
130
131 $comment_content_size = get( $request, 'content_size', [ 1, 5 ] );
132 $comment_content_use_html = is_truthy( get( $request, 'use_html', 'off' ) );
133 $comment_content_html_tags = array_map( 'trim', explode( ',', get( $request, 'html_tags' ) ) );
134 $comment_type = array_map( 'trim', explode( ',', get( $request, 'type' ) ) );
135 $post_types = array_map( 'trim', explode( ',', get( $request, 'post_types' ) ) );
136
137 $min_date = get( $request, [ 'interval_date', 'min' ] );
138 $max_date = get( $request, [ 'interval_date', 'max' ] );
139 $metas = get( $request, 'meta', [] );
140
141 $results = [];
142
143 for ( $i = 0; $i < $qty; $i ++ ) {
144 $this->set( 'comment_date', $min_date, $max_date );
145 $this->set(
146 'comment_content',
147 $comment_content_use_html,
148 [
149 'qty' => $comment_content_size,
150 'elements' => $comment_content_html_tags,
151 ]
152 );
153 $this->set( 'user_id', 0 );
154 $this->set( 'comment_type', $comment_type );
155
156 $this->set( 'comment_author' );
157 $this->set( 'comment_parent' );
158 $this->set( 'comment_author_IP' );
159 $this->set( 'comment_agent' );
160 $this->set( 'comment_approved' );
161 $this->set( 'comment_post_ID', null, [ 'post_type' => $post_types ] );
162 $this->set( 'comment_author_email' );
163 $this->set( 'comment_author_url' );
164
165 $comment_id = $this->generate()->save();
166
167 if ( $comment_id && is_numeric( $comment_id ) ) {
168 foreach ( $metas as $meta_index => $meta ) {
169 if ( ! isset( $meta['type'], $meta['name'] ) ) {
170 continue;
171 }
172
173 $type = get( $meta, 'type' );
174 $name = get( $meta, 'name' );
175 unset( $meta['type'], $meta['name'] );
176
177 if ( isset( $meta['weight'] ) ) {
178 $meta['weight'] = absint( $meta['weight'] );
179 $meta['weight'] = $meta['weight'] > 0 ? $meta['weight'] : 100;
180 } else {
181 $meta['weight'] = 100;
182 }
183
184 make( Meta::class )->object( $comment_id, 'comment' )->with( $type, $name, $meta )->generate()->save();
185 }
186 }
187 $results[] = $comment_id;
188 }
189 $results = array_filter( $results, 'absint' );
190
191 return $results;
192 }
193 }
194