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

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

206 lines 4.8 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 function FakerPress\get_request_var;
6 use function FakerPress\get;
7 use function FakerPress\make;
8 use FakerPress\Plugin;
9 use FakerPress\ThirdParty\Faker;
10 use FakerPress;
11
12 class User extends Abstract_Module {
13
14 /**
15 * @inheritDoc
16 */
17 protected $dependencies = [
18 FakerPress\ThirdParty\Faker\Provider\Lorem::class,
19 FakerPress\ThirdParty\Faker\Provider\DateTime::class,
20 FakerPress\Provider\HTML::class,
21 ];
22
23 /**
24 * @inheritDoc
25 */
26 protected $provider_class = FakerPress\Provider\WP_User::class;
27
28 /**
29 * @inheritDoc
30 */
31 public static function get_slug(): string {
32 return 'users';
33 }
34
35 /**
36 * @inheritDoc
37 */
38 public function hook(): void {
39
40 }
41
42 /**
43 * @inheritDoc
44 */
45 public static function fetch( array $args = [] ): array {
46 $defaults = [
47 'fields' => 'ID',
48 'meta_query' => [
49 [
50 'key' => static::get_flag(),
51 'value' => true,
52 'type' => 'BINARY',
53 ],
54 ],
55 ];
56 $args = wp_parse_args( $args, $defaults );
57
58 $query_users = new \WP_User_Query( $args );
59
60 return array_map( 'absint', $query_users->results );
61 }
62
63 /**
64 * @inheritDoc
65 */
66 public static function delete( $user ) {
67 if ( is_array( $user ) ) {
68 $deleted = [];
69
70 foreach ( $user as $id ) {
71 $id = $id instanceof \WP_User ? $id->ID : $id;
72
73 if ( ! is_numeric( $id ) ) {
74 continue;
75 }
76
77 $deleted[ $id ] = self::delete( $id );
78 }
79
80 return $deleted;
81 }
82
83 if ( is_numeric( $user ) ) {
84 $user = new \WP_User( $user );
85 }
86
87 if ( ! $user instanceof \WP_User || ! $user->exists() ) {
88 return false;
89 }
90
91 $flag = (bool) get_user_meta( $user->ID, static::get_flag(), true );
92
93 if ( true !== $flag ) {
94 return false;
95 }
96
97 return wp_delete_user( $user->ID, get_current_user_id() );
98 }
99
100 /**
101 * @inheritDoc
102 */
103 public function filter_save_response( $response, array $data, Abstract_Module $module ) {
104 $user_id = wp_insert_user( $data );
105
106 if ( ! is_numeric( $user_id ) ) {
107 return false;
108 }
109
110 // Only set role if needed
111 if ( ! is_null( $data['role'] ) ) {
112 $user = new \WP_User( $user_id );
113
114 // Here we could add in the future the possibility to set multiple roles at once
115 $user->set_role( $data['role'] );
116 }
117
118 // Flag the Object as FakerPress
119 update_user_meta( $user_id, static::get_flag(), 1 );
120
121 return $user_id;
122 }
123
124 /**
125 * @inheritDoc
126 */
127 public function parse_request( $qty, $request = [] ) {
128 if ( is_null( $qty ) ) {
129 $qty = get_request_var( [ Plugin::$slug, 'qty' ] );
130 $min = absint( $qty['min'] );
131 $max = max( absint( isset( $qty['max'] ) ? $qty['max'] : 0 ), $min );
132 $qty = $this->get_faker()->numberBetween( $min, $max );
133 }
134
135 if ( 0 === $qty ) {
136 return esc_attr__( 'Zero is not a good number of users to fake...', 'fakerpress' );
137 }
138
139 $description_size = get( $request, 'description_size', [ 1, 5 ] );
140 $description_use_html = get( $request, 'use_html', 'off' ) === 'on';
141 $description_html_tags = array_map( 'trim', explode( ',', get( $request, 'html_tags' ) ) );
142
143 $roles = array_intersect( array_keys( get_editable_roles() ), array_map( 'trim', explode( ',', get( $request, 'roles' ) ) ) );
144 $metas = get( $request, 'meta', [] );
145
146 $results = [];
147
148 for ( $i = 0; $i < $qty; $i ++ ) {
149 $this->set( 'role', $roles );
150 $this->set(
151 'description',
152 $description_use_html,
153 [
154 'qty' => $description_size,
155 'elements' => $description_html_tags,
156 ]
157 );
158 $this->set( 'user_registered', 'yesterday', 'now' );
159
160 $this->set( [
161 'first_name',
162 'last_name',
163 'user_pass',
164 'user_url',
165 ] );
166
167 $this->generate();
168
169 $username_from_generated_first_last = strtolower( implode( '.', [ $this->get_value( 'first_name' ), $this->get_value( 'last_name' ) ] ) );
170
171 $this->set( 'user_login', $username_from_generated_first_last );
172 $this->set( 'user_nicename', $username_from_generated_first_last );
173 $this->set( 'user_email', $username_from_generated_first_last . '@' . FakerPress\ThirdParty\Faker\Provider\Internet::safeEmailDomain() );
174 $this->set( 'display_name', $this->get_value( 'first_name' ) );
175 $this->set( 'nickname', $username_from_generated_first_last );
176
177 $user_id = $this->generate()->save();
178
179 if ( $user_id && is_numeric( $user_id ) ) {
180 foreach ( $metas as $meta_index => $meta ) {
181 if ( ! isset( $meta['type'], $meta['name'] ) ) {
182 continue;
183 }
184
185 $type = get( $meta, 'type' );
186 $name = get( $meta, 'name' );
187 unset( $meta['type'], $meta['name'] );
188
189 if ( isset( $meta['weight'] ) ) {
190 $meta['weight'] = absint( $meta['weight'] );
191 $meta['weight'] = $meta['weight'] > 0 ? $meta['weight'] : 100;
192 } else {
193 $meta['weight'] = 100;
194 }
195
196 make( Meta::class )->object( $user_id, 'user' )->with( $type, $name, $meta )->generate()->save();
197 }
198 }
199 $results[] = $user_id;
200 }
201 $results = array_filter( $results, 'absint' );
202
203 return $results;
204 }
205 }
206