PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.7
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.7
4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 4.2.1 All 138 releases
learnpress / inc / Services / UserService.php

UserService.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.7, at inc/Services/UserService.php

234 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace LearnPress\Services;
4
5 use Exception;
6 use LearnPress\Helpers\Config;
7 use LearnPress\Helpers\Singleton;
8 use LearnPress\Models\UserModel;
9 use Throwable;
10 use WP_Error;
11 use WP_Role;
12 use WP_User;
13
14
15 /**
16 * Class UserService
17 *
18 * Handle logic business for user.
19 *
20 * @since 4.3.4
21 * @version 1.0.0
22 */
23 class UserService {
24 use Singleton;
25
26 public function init() {}
27
28 /**
29 * Check if pretty slug exists of another user.
30 *
31 * @param string $slug
32 *
33 * @return false|UserModel
34 * @throws Exception
35 * @version 1.0.1
36 * @since 4.3.4
37 */
38 /*public function get_user_by_pretty_slug( string $slug ) {
39 if ( '' === $slug ) {
40 return false;
41 }
42
43 $lp_user_db = UserDB::getInstance();
44 $filter = new UserFilter();
45 $filter->only_fields = [ 'u.ID' ];
46 $lp_user_db->get_query_single_row( $filter );
47 $filter->join[] = "INNER JOIN {$lp_user_db->wpdb->usermeta} AS um ON um.user_id = u.ID";
48 $filter->where[] = $lp_user_db->wpdb->prepare( 'AND um.meta_key = %s', UserModel::META_KEY_USER_SLUG );
49 $filter->where[] = $lp_user_db->wpdb->prepare( 'AND um.meta_value = %s', $slug );
50 $query = $lp_user_db->get_users( $filter );
51
52 $user_id = (int) $lp_user_db->wpdb->get_var( $query );
53
54 return UserModel::find( $user_id, true );
55 }*/
56
57 /**
58 * Generate pretty slug for all users who don't have it yet.
59 *
60 * @return array [ 'processed' => int, 'generated' => int, 'skipped' => int, 'failed' => int ]
61 * @since 4.3.4
62 * @version 1.0.0
63 */
64 public function generate_users_pretty_slug(): array {
65 $user_ids = get_users(
66 [
67 'fields' => 'ids',
68 'number' => - 1,
69 ]
70 );
71
72 $result = [
73 'processed' => 0,
74 'generated' => 0,
75 'skipped' => 0,
76 'failed' => 0,
77 ];
78
79 foreach ( $user_ids as $user_id ) {
80 $user_id = (int) $user_id;
81 ++ $result['processed'];
82
83 $userModel = UserModel::find( $user_id, true );
84 if ( ! $userModel instanceof UserModel ) {
85 ++ $result['failed'];
86 continue;
87 }
88
89 if ( '' !== $userModel->get_slug_link() ) {
90 ++ $result['skipped'];
91 continue;
92 }
93
94 $generated = $this->generate_pretty_slug( $userModel );
95 if ( is_wp_error( $generated ) ) {
96 ++ $result['failed'];
97 } else {
98 ++ $result['generated'];
99 }
100 }
101
102 return $result;
103 }
104
105 /**
106 * Create a unique pretty slug for user.
107 *
108 * If the user already has a pretty slug, it will return the existing one without generating a new one.
109 * The slug is generated based on the user's first name and last name.
110 * If empty user's first name and last name, it will use the username with uniqid() to generate a slug.
111 *
112 * @return string|WP_Error
113 * @since 4.3.4
114 * @version 1.0.1
115 */
116 public function generate_pretty_slug( UserModel $userModel ) {
117 $user_slug_new = '';
118
119 try {
120 // Check if pretty slug already exists, if exists, return it without generating a new one.
121 $existing_slug = $userModel->get_slug_link();
122 if ( ! empty( $existing_slug ) ) {
123 return $existing_slug;
124 }
125
126 // Generate pretty slug based on first name and last name.
127 $first_name = $userModel->get_meta_value_by_key( 'first_name', '' );
128 $last_name = $userModel->get_meta_value_by_key( 'last_name', '' );
129 $base_source = trim( "{$first_name} {$last_name}" );
130 $base_slug = sanitize_title( $base_source );
131
132 if ( empty( $base_slug ) ) {
133 // Shuffle username with uniqid to make it more unique and less guessable, get first 10 characters to make slug shorter.
134 $base_slug = substr( str_shuffle( sanitize_title( $userModel->user_login . uniqid() ) ), 0, 10 );
135 } else {
136 $base_slug = $base_slug . substr( str_shuffle( uniqid() ), 0, 3 );
137 }
138
139 // Check slug exists.
140 $userModelFind = UserService::instance()->get_user_by_slug_link( $base_slug );
141 if ( ! $userModelFind ) {
142 $userModel->user_nicename = $base_slug;
143 $userModel->save();
144 } else {
145 // Regenerate slug by adding random string at the end of base slug until it is unique.
146 $user_slug_new = $this->generate_pretty_slug( $userModel );
147 }
148 } catch ( Throwable $e ) {
149 return new WP_Error( 'lp_user_slug_generation_failed', $e->getMessage() );
150 }
151
152 return $user_slug_new;
153 }
154
155 /**
156 * Detected user by slug link.
157 *
158 * @param string $slug_link
159 *
160 * @return false|UserModel
161 * @since 4.3.6
162 * @version 1.0.0
163 */
164 public function get_user_by_slug_link( string $slug_link ) {
165 // Get from column `user_nicename` in table `wp_users`.
166 $wp_user = get_user_by( 'slug', $slug_link );
167 if ( ! $wp_user instanceof WP_User ) {
168 return false;
169 }
170
171 return new UserModel( $wp_user->data );
172 }
173
174 /**
175 * Set capabilities for roles.
176 *
177 * @param string $item_type Ex: "lp_course", "lp_lesson", "lp_assignment", "lp_h5p"
178 * @return void
179 * @since 4.4.2
180 * @version 1.0.0
181 */
182 public function add_capabilities_for_roles( string $item_type ) {
183 $role_capabilities = Config::instance()->get( 'user-roles-caps' );
184 foreach ( $role_capabilities as $role_key => $role_data ) {
185 $role_label = $role_data['label'] ?? '';
186 $prefix_caps = $role_data['prefix_capabilities'] ?? [];
187 $role = get_role( $role_key );
188 if ( $role_key !== UserModel::ROLE_ADMINISTRATOR
189 && ! $role instanceof WP_Role ) {
190 add_role( $role_key, $role_label );
191 $role = get_role( $role_key );
192 }
193
194 if ( $role instanceof WP_Role ) {
195 foreach ( $prefix_caps as $prefix_cap ) {
196 // Example: "publish_lp_courses", "edit_lp_courses",...
197 $cap = "{$prefix_cap}_{$item_type}s";
198 if ( ! $role->has_cap( $cap ) ) {
199 $role->add_cap( $cap );
200 }
201 }
202 }
203 }
204 }
205
206 /**
207 * Create or get CSRF token to verify
208 *
209 * @return mixed|string|null
210 * @since 4.4.6
211 * @version 1.0.0
212 */
213 public static function csrf_token() {
214 static $token = null;
215 $cookie_name = 'lp_csrf_token';
216
217 if ( null !== $token ) {
218 return $token;
219 }
220
221 if ( ! empty( $_COOKIE[ $cookie_name ] ) ) {
222 $token = sanitize_text_field( wp_unslash( $_COOKIE[ $cookie_name ] ) );
223 return $token;
224 }
225
226 $token = wp_generate_password( 20, false );
227 if ( ! headers_sent() ) {
228 learn_press_setcookie( $cookie_name, $token, time() + DAY_IN_SECONDS );
229 }
230
231 return $token;
232 }
233 }
234