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 / Post.php

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

238 lines 5.7 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\Admin;
6 use FakerPress\Plugin;
7 use FakerPress\ThirdParty\Faker;
8 use FakerPress;
9 use function FakerPress\make;
10 use function FakerPress\get_request_var;
11 use function FakerPress\is_truthy;
12 use function FakerPress\get;
13
14 class Post 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_Post::class;
28
29 /**
30 * @inheritDoc
31 */
32 public static function get_slug(): string {
33 return 'posts';
34 }
35
36 /**
37 * @inheritDoc
38 */
39 public function hook(): void {
40
41 }
42
43 /**
44 * @inheritDoc
45 */
46 public static function fetch( array $args = [] ): array {
47 $defaults = [
48 'post_type' => 'any',
49 'post_status' => 'any',
50 'nopaging' => true,
51 'posts_per_page' => - 1,
52 'fields' => 'ids',
53 'meta_query' => [
54 [
55 'key' => static::get_flag(),
56 'value' => true,
57 'type' => 'BINARY',
58 ],
59 ],
60 ];
61
62 $args = wp_parse_args( $args, $defaults );
63 $query_posts = new \WP_Query( $args );
64
65 return array_map( 'absint', $query_posts->posts );
66 }
67
68 /**
69 * @inheritDoc
70 */
71 public static function delete( $item ) {
72 if ( is_array( $item ) ) {
73 $deleted = [];
74
75 foreach ( $item as $id ) {
76 $id = $id instanceof \WP_Post ? $id->ID : $id;
77
78 if ( ! is_numeric( $id ) ) {
79 continue;
80 }
81
82 $deleted[ $id ] = static::delete( $id );
83 }
84
85 return $deleted;
86 }
87
88 if ( is_numeric( $item ) ) {
89 $item = \WP_Post::get_instance( $item );
90 }
91
92 if ( ! $item instanceof \WP_Post ) {
93 return false;
94 }
95
96 $flag = (bool) get_post_meta( $item->ID, static::get_flag(), true );
97
98 if ( true !== $flag ) {
99 return false;
100 }
101
102 return wp_delete_post( $item->ID, true );
103 }
104
105 /**
106 * @inheritDoc
107 */
108 public function filter_save_response( $response, array $data, Abstract_Module $module ) {
109 $post_id = wp_insert_post( $data );
110
111 if ( ! is_numeric( $post_id ) ) {
112 return false;
113 }
114
115 // Flag the Object as FakerPress
116 update_post_meta( $post_id, static::get_flag(), 1 );
117
118 return $post_id;
119 }
120
121 /**
122 * @since 0.6.4
123 *
124 * @throws \Exception
125 *
126 * @param $request
127 * @param $qty
128 *
129 * @return array|string
130 */
131 public function parse_request( $qty, $request = [] ) {
132 if ( is_null( $qty ) ) {
133 $qty = get_request_var( [ Plugin::$slug, 'qty' ] );
134 $min = absint( $qty['min'] );
135 $max = max( absint( isset( $qty['max'] ) ? $qty['max'] : 0 ), $min );
136 $qty = $this->get_faker()->numberBetween( $min, $max );
137 }
138
139 if ( 0 === $qty ) {
140 return esc_attr__( 'Zero is not a good number of posts to fake...', 'fakerpress' );
141 }
142
143 // Fetch Comment Status
144 $comment_status = get( $request, 'comment_status' );
145 $comment_status = array_map( 'trim', explode( ',', $comment_status ) );
146
147 // Fetch Post Author
148 $post_author = get( $request, 'author' );
149 $post_author = array_map( 'trim', explode( ',', $post_author ) );
150 $post_author = array_intersect( get_users( [ 'fields' => 'ID' ] ), $post_author );
151
152 // Fetch the dates
153 $date = [
154 get( $request, [ 'interval_date', 'min' ] ),
155 get( $request, [ 'interval_date', 'max' ] ),
156 ];
157
158 // Fetch Post Types
159 $post_types = get( $request, 'post_types' );
160 $post_types = array_map( 'trim', explode( ',', $post_types ) );
161 $post_types = array_intersect( get_post_types( [ 'public' => true ] ), $post_types );
162
163 // Fetch Post Content
164 $post_content_size = get( $request, 'content_size', [ 5, 15 ] );
165 $post_content_use_html = ( (int) get( $request, 'use_html', 0 ) ) === 1;
166 $post_content_html_tags = array_map( 'trim', explode( ',', get( $request, 'html_tags' ) ) );
167
168 // Fetch Post Excerpt.
169 $post_excerpt_size = get( $request, 'excerpt_size', [ 1, 3 ] );
170
171 // Fetch and clean Post Parents
172 $post_parents = get( $request, 'post_parent' );
173 $post_parents = array_map( 'trim', explode( ',', $post_parents ) );
174
175 $images_origin = array_map( 'trim', explode( ',', get( $request, 'images_origin' ) ) );
176
177 // Fetch Taxonomies
178 $taxonomies_configuration = get( $request, 'taxonomy' );
179
180 // Fetch Metas It will be parsed later!
181 $metas = get( $request, 'meta', [] );
182
183 $results = [];
184
185 for ( $i = 0; $i < $qty; $i ++ ) {
186 $this->set( 'post_title' );
187 $this->set( 'post_status', 'publish' );
188 $this->set( 'post_date', $date );
189 $this->set( 'post_parent', $post_parents );
190 $this->set(
191 'post_content',
192 $post_content_use_html,
193 [
194 'qty' => $post_content_size,
195 'elements' => $post_content_html_tags,
196 'sources' => $images_origin,
197 ]
198 );
199 $this->set( 'post_excerpt', $post_excerpt_size );
200 $this->set( 'post_author', $post_author );
201 $this->set( 'post_type', $post_types );
202 $this->set( 'comment_status', $comment_status );
203 $this->set( 'ping_status' );
204 $this->set( 'tax_input', $taxonomies_configuration );
205
206 $generated = $this->generate();
207 $post_id = $generated->save();
208
209 if ( $post_id && is_numeric( $post_id ) ) {
210 foreach ( $metas as $meta_index => $meta ) {
211 if ( ! isset( $meta['type'], $meta['name'] ) ) {
212 continue;
213 }
214
215 $type = get( $meta, 'type' );
216 $name = get( $meta, 'name' );
217 unset( $meta['type'], $meta['name'] );
218
219 if ( isset( $meta['weight'] ) ) {
220 $meta['weight'] = absint( $meta['weight'] );
221 $meta['weight'] = $meta['weight'] > 0 ? $meta['weight'] : 100;
222 } else {
223 $meta['weight'] = 100;
224 }
225
226 make( Meta::class )->object( $post_id )->with( $type, $name, $meta )->generate()->save();
227 }
228 }
229
230 $results[] = $post_id;
231 }
232
233 $results = array_filter( (array) $results, 'absint' );
234
235 return $results;
236 }
237 }
238