LLMS_Roles

LifterLMS Custom Roles and Capabilities.


Source Source

File: includes/class.llms.roles.php

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
class LLMS_Roles {
 
    /**
     * The capability name to manage earned engagament.
     *
     * @since 6.0.0
     *
     * @var string
     */
    const MANAGE_EARNED_ENGAGEMENT_CAP = 'manage_earned_engagement';
 
    /**
     * Retrieve an array of all capabilities for a role
     *
     * @since 3.13.0
     *
     * @param string $role Name of the role.
     * @return array
     */
    private static function get_all_caps( $role ) {
 
        $caps         = array();
        $caps['core'] = self::get_core_caps( $role );
        $caps['wp']   = self::get_wp_caps( $role );
        $caps         = array_merge( $caps, self::get_post_type_caps( $role ) );
 
        return apply_filters( 'llms_get_all_' . $role . '_caps', $caps );
 
    }
 
    /**
     * Get an array of registered core lifterlms caps.
     *
     * @since 3.13.0
     * @since 3.14.0 Add the `lifterlms_instructor` capability.
     * @since 3.34.0 Added capabilities for student management.
     * @since 4.21.2 Added the `view_grades` capability.
     * @since 6.0.0 Added `manage_earned_engagement` capability.
     *
     *
     * @return string[]
     */
    public static function get_all_core_caps() {
 
        /**
         * Filters the list of available LifterLMS core user capabilities
         *
         * @since 3.13.0
         *
         * @param string[] $capabilities List of LifterLMS user capabilities.
         */
        return apply_filters(
            'llms_get_all_core_caps',
            array(
                'lifterlms_instructor',
                'manage_lifterlms',
                self::MANAGE_EARNED_ENGAGEMENT_CAP,
                'view_lifterlms_reports',
                'view_others_lifterlms_reports',
                'enroll',
                'unenroll',
                'create_students',
                'view_grades',
                'view_students',
                'view_others_students',
                'edit_students',
                'edit_others_students',
                'delete_students',
                'delete_others_students',
            )
        );
    }
 
    /**
     * Retrieve the LifterLMS core capabilities for a give role
     *
     * @since 3.13.0
     * @since 3.34.0 Added student management capabilities.
     * @since 4.21.2 Added 'view_grades' to the list of instructor/assistant caps which are not automatically available.
     * @since 6.0.0 Added `manage_earned_engagement` to the list of instructor/assistant caps which are not automatically available.
     *
     * @param string $role Name of the role.
     * @return string[]
     */
    private static function get_core_caps( $role ) {
 
        $all_caps = array_fill_keys( array_values( self::get_all_core_caps() ), true );
 
        switch ( $role ) {
 
            case 'instructor':
            case 'instructors_assistant':
                $caps = $all_caps;
                unset(
                    $caps['enroll'],
                    $caps['unenroll'],
                    $caps['manage_lifterlms'],
                    $caps[ self::MANAGE_EARNED_ENGAGEMENT_CAP ],
                    $caps['view_others_lifterlms_reports'],
                    $caps['create_students'],
                    $caps['view_others_students'],
                    $caps['edit_students'],
                    $caps['edit_others_students'],
                    $caps['delete_students'],
                    $caps['delete_others_students'],
                    $caps['view_grades']
                );
                break;
 
            case 'administrator':
            case 'lms_manager':
                $caps = $all_caps;
                break;
 
            default:
                $caps = array();
 
        }
 
        /**
         * Filters the LifterLMS capabilities added to a LifterLMS user role.
         *
         * The dynamic portion of this hook `$role` refers to the user's role name.
         *
         * @since 4.21.2
         *
         * @param string[] $caps     List of capabilities provided to the role.
         * @param string[] $all_caps Full list of all LifterLMS user capabilities.
         */
        return apply_filters( "llms_get_{$role}_core_caps", $caps, $all_caps );
 
    }
 
    /**
     * Retrieve the post type specific capabilities for a give role.
     *
     * @since 3.13.0
     * @since 4.21.2 Use strict comparisons for `in_array()`.
     *
     * @param string $role Name of the role
     * @return array
     */
    private static function get_post_type_caps( $role ) {
 
        $caps = array();
 
        // Students get nothing.
        if ( 'student' !== $role ) {
 
            $post_types = array(
                'course'          => 'course',
                'lesson'          => 'lesson',
                'llms_quiz'       => array( 'quiz', 'quizzes' ),
                'llms_question'   => 'question',
                'llms_membership' => 'membership',
            );
            foreach ( $post_types as $post_type => $names ) {
 
                $post_caps = LLMS_Post_Types::get_post_type_caps( $names );
 
                // Filter the caps down for these roles.
                if ( in_array( $role, array( 'instructor', 'instructors_assistant' ), true ) ) {
 
                    $allowed = array(
                        'instructor'            => array(
                            'delete_posts',
                            'delete_published_posts',
                            'edit_post',
                            'edit_posts',
                            'edit_published_posts',
                            'publish_posts',
                            'create_posts',
                        ),
                        'instructors_assistant' => array(
                            'edit_post',
                            'edit_posts',
                            'edit_published_posts',
                        ),
                    );
 
                    foreach ( $post_caps as $post_cap => $cpt_cap ) {
 
                        if ( ! in_array( $post_cap, $allowed[ $role ], true ) ) {
                            unset( $post_caps[ $post_cap ] );
                        }
                    }
                }
 
                $caps[ $post_type ] = array_fill_keys( array_values( $post_caps ), true );
 
            }
 
            $taxes = array(
                'course_cat'        => 'course_cat',
                'course_difficulty' => array( 'course_difficulty', 'course_difficulties' ),
                'course_tag'        => 'course_tag',
                'course_track'      => 'course_track',
                'membership_cat'    => 'membership_cat',
                'membership_tag'    => 'membership_tag',
            );
            foreach ( $taxes as $tax => $names ) {
 
                $tax_caps = LLMS_Post_Types::get_tax_caps( $names );
 
                // Filter the caps down for these roles.
                if ( in_array( $role, array( 'instructor', 'instructors_assistant' ), true ) ) {
 
                    $allowed = array(
                        'assign_terms',
                    );
 
                    foreach ( $tax_caps as $tax_cap => $ct_cap ) {
 
                        if ( ! in_array( $tax_cap, $allowed, true ) ) {
                            unset( $tax_caps[ $tax_cap ] );
                        }
                    }
                }
 
                $caps[ $tax ] = array_fill_keys( array_values( $tax_caps ), true );
 
            }
        }
 
        return apply_filters( 'llms_get_' . $role . '_post_type_caps', $caps );
 
    }
 
    /**
     * Retrieve the core WP capabilities for a give role
     *
     * @since 3.13.0
     * @since 3.34.0 Add the `list_users` capability to instructors.
     *
     * @param string $role Name of the role.
     * @return array
     */
    private static function get_wp_caps( $role ) {
 
        $caps = array(
            'read' => true,
        );
 
        switch ( $role ) {
 
            case 'instructor':
                $add = array(
                    'create_users'  => true,
                    'edit_users'    => true,
                    'promote_users' => true,
                    'list_users'    => true,
 
                    'read'          => true,
                    'upload_files'  => true,
 
                    /**
                     * See WP Core issue(s)
                     *
                     * @link https://core.trac.wordpress.org/ticket/22895
                     * @link https://core.trac.wordpress.org/ticket/16808
                     */
                    'edit_posts'    => true,
                );
 
                break;
 
            case 'instructors_assistant':
                $add = array(
                    'read'         => true,
                    'upload_files' => true,
 
                    /**
                     * See WP Core issue(s)
                     *
                     * @link https://core.trac.wordpress.org/ticket/22895
                     * @link https://core.trac.wordpress.org/ticket/16808
                     */
                    'edit_posts'   => true,
                );
 
                break;
 
            case 'lms_manager':
                $add = array(
                    'read_private_pages'     => true,
                    'read_private_posts'     => true,
                    'edit_posts'             => true,
                    'edit_pages'             => true,
                    'edit_published_posts'   => true,
                    'edit_published_pages'   => true,
                    'edit_private_pages'     => true,
                    'edit_private_posts'     => true,
                    'edit_others_posts'      => true,
                    'edit_others_pages'      => true,
                    'publish_posts'          => true,
                    'publish_pages'          => true,
                    'delete_posts'           => true,
                    'delete_pages'           => true,
                    'delete_private_pages'   => true,
                    'delete_private_posts'   => true,
                    'delete_published_pages' => true,
                    'delete_published_posts' => true,
                    'delete_others_posts'    => true,
                    'delete_others_pages'    => true,
                    'manage_categories'      => true,
                    'manage_links'           => true,
                    'moderate_comments'      => true,
                    'upload_files'           => true,
                    'export'                 => true,
                    'import'                 => true,
 
                    'edit_users'             => true,
                    'create_users'           => true,
                    'list_users'             => true,
                    'promote_users'          => true,
                    'delete_users'           => true,
                );
 
                break;
 
            default:
                $add = array();
 
        }
 
        return apply_filters( 'llms_get_' . $role . '_wp_caps', array_merge( $add, $caps ) );
 
    }
 
    /**
     * Retrieve LifterLMS roles and role names
     *
     * @since 3.13.0
     *
     * @return array
     */
    public static function get_roles() {
 
        return apply_filters(
            'llms_get_roles',
            array(
                'lms_manager'           => __( 'LMS Manager', 'lifterlms' ),
                'instructor'            => __( 'Instructor', 'lifterlms' ),
                'instructors_assistant' => __( 'Instructor\'s Assistant', 'lifterlms' ),
                'student'               => __( 'Student', 'lifterlms' ),
            )
        );
 
    }
 
    /**
     * Install custom roles and related capabilities
     *
     * Called from LLMS_Install during installation and upgrades.
     *
     * @since 3.13.0
     *
     * @return void
     */
    public static function install() {
 
        global $wp_roles;
 
        if ( ! class_exists( 'WP_Roles' ) ) {
            return;
        }
 
        $roles                  = self::get_roles();
        $roles['administrator'] = __( 'Administrator', 'lifterlms' );
 
        $wp_roles = wp_roles();
 
        foreach ( $roles as $role => $name ) {
 
            $role_obj = $wp_roles->get_role( $role );
 
            if ( ! $role_obj ) {
                $role_obj = $wp_roles->add_role( $role, $name );
            }
 
            self::update_caps( $role_obj, 'add' );
 
        }
 
    }
 
    /**
     * Uninstall custom roles and remove custom caps from default WP roles
     *
     * @since 3.13.0
     *
     * @return void
     */
    public static function remove_roles() {
 
        if ( ! class_exists( 'WP_Roles' ) ) {
            return;
        }
 
        $wp_roles = wp_roles();
 
        // Delete all our custom roles.
        foreach ( array_keys( self::get_roles() ) as $role ) {
            $wp_roles->remove_role( $role );
        }
 
        // Remove custom caps from the WP core admin role.
        self::update_caps( $wp_roles->get_role( 'administrator' ), 'remove', array( 'wp' ) );
 
    }
 
    /**
     * Update the capabilities for a given role
     *
     * @since 3.13.0
     * @since 4.5.1 Added `$exclude_group` parameter that allows excluding groups of caps from the update.
     *
     * @param WP_Role  $role           Role object.
     * @param string   $type           Update type [add|remove].
     * @param string[] $exclude_groups Array of groups to exclude.
     * @return void
     */
    private static function update_caps( $role, $type = 'add', $exclude_groups = array() ) {
 
        $role_caps = self::get_all_caps( $role->name );
        $role_caps = empty( $exclude_groups ) ? $role_caps : array_diff_key( $role_caps, array_flip( $exclude_groups ) );
 
        foreach ( $role_caps as $group => $caps ) {
 
            foreach ( array_keys( $caps ) as $cap ) {
 
                if ( 'add' === $type ) {
                    $role->add_cap( $cap );
                } elseif ( 'remove' === $type ) {
                    $role->remove_cap( $cap );
                }
            }
        }
 
    }
 
    /**
     * Returns an array of role names.
     *
     * LLMS roles and WP core roles are translated.
     *
     * @since 5.6.0
     *
     * @return array
     */
    public static function get_all_role_names() {
 
        $all_roles = wp_roles()->roles;
 
        return array_merge(
            array_combine(
                array_keys( $all_roles ),
                array_map(
                    'translate_user_role', // Translates WP Core roles.
                    array_column( $all_roles, 'name' )
                )
            ),
            self::get_roles() // So our roles are translated as well.
        );
 
    }
 
}

Top ↑

Methods Methods


Top ↑

Changelog Changelog

Changelog
Version Description
3.13.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.