PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.9.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.9.1
4.4.9.1 4.4.9 4.4.8 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 All 141 releases
← All changes | inc/Services/UserService.php +72 -0 4.4.0 → 4.4.9.1 View file →
@@ -2,12 +2,14 @@
2 2
3 3 namespace LearnPress\Services;
4 4
5 5 use Exception;
6 +use LearnPress\Helpers\Config;
6 7 use LearnPress\Helpers\Singleton;
7 8 use LearnPress\Models\UserModel;
8 9 use Throwable;
9 10 use WP_Error;
11 +use WP_Role;
10 12 use WP_User;
11 13
12 14
13 15 /**
@@ -166,6 +168,76 @@
166 168 return false;
167 169 }
168 170
169 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.1
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 + $capabilities = $role_data['capabilities'] ?? [];
187 + $prefix_caps = $role_data['prefix_capabilities'] ?? [];
188 + $role = get_role( $role_key );
189 + if ( $role_key !== UserModel::ROLE_ADMINISTRATOR
190 + && ! $role instanceof WP_Role ) {
191 + add_role( $role_key, $role_label );
192 + $role = get_role( $role_key );
193 + }
194 +
195 + if ( $role instanceof WP_Role ) {
196 + // For prefix + item_type
197 + if ( ! empty( $item_type ) ) {
198 + foreach ( $prefix_caps as $prefix_cap ) {
199 + // Example: "publish_lp_courses", "edit_lp_courses",...
200 + $cap = "{$prefix_cap}_{$item_type}s";
201 + if ( ! $role->has_cap( $cap ) ) {
202 + $role->add_cap( $cap );
203 + }
204 + }
205 + } else {
206 + foreach ( $capabilities as $cap ) {
207 + if ( ! $role->has_cap( $cap ) ) {
208 + $role->add_cap( $cap );
209 + }
210 + }
211 + }
212 + }
213 + }
214 + }
215 +
216 + /**
217 + * Create or get CSRF token to verify
218 + *
219 + * @return mixed|string|null
220 + * @since 4.4.6
221 + * @version 1.0.0
222 + */
223 + public static function csrf_token() {
224 + static $token = null;
225 + $cookie_name = 'lp_csrf_token';
226 +
227 + if ( null !== $token ) {
228 + return $token;
229 + }
230 +
231 + if ( ! empty( $_COOKIE[ $cookie_name ] ) ) {
232 + $token = sanitize_text_field( wp_unslash( $_COOKIE[ $cookie_name ] ) );
233 + return $token;
234 + }
235 +
236 + $token = wp_generate_password( 20, false );
237 + if ( ! headers_sent() ) {
238 + learn_press_setcookie( $cookie_name, $token, time() + DAY_IN_SECONDS );
239 + }
240 +
241 + return $token;
170 242 }
171 243 }