Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Permissions
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 6
110
0.00% covered (danger)
0.00%
0 / 1
 rest_is_authorized_admin
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 is_authorized_admin
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 rest_can_upload_media
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 can_manage_media_library
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 can_edit_posts
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 rest_can_optimize_images
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace NewfoldLabs\WP\Module\Performance;
4
5/**
6 * Permissions and Authorization utility class.
7 */
8final class Permissions {
9
10    /**
11     * WordPress Admin capability string
12     */
13    const ADMIN                = 'manage_options';
14    const UPLOAD_FILES         = 'upload_files';
15    const EDIT_POSTS           = 'edit_posts';
16    const MANAGE_MEDIA_LIBRARY = 'manage_media_library';
17
18    /**
19     * Checks if the REST API caller has admin capabilities.
20     *
21     * @return bool
22     */
23    public static function rest_is_authorized_admin() {
24        return \is_user_logged_in() && \current_user_can( self::ADMIN );
25    }
26
27    /**
28     * Checks if the current user is logged in and is in the wp-admin with admin capabilities.
29     *
30     * @return bool
31     */
32    public static function is_authorized_admin() {
33        return \is_admin() && self::rest_is_authorized_admin();
34    }
35
36    /**
37     * Checks if the current user has media upload permissions.
38     *
39     * @return bool
40     */
41    public static function rest_can_upload_media() {
42        return \is_user_logged_in() && \current_user_can( self::UPLOAD_FILES );
43    }
44
45    /**
46     * Checks if the current user has permissions to manage media.
47     *
48     * @return bool
49     */
50    public static function can_manage_media_library() {
51        return \current_user_can( self::MANAGE_MEDIA_LIBRARY );
52    }
53
54    /**
55     * Checks if the user has permissions to edit posts.
56     *
57     * @return bool
58     */
59    public static function can_edit_posts() {
60        return \current_user_can( self::EDIT_POSTS );
61    }
62
63    /**
64     * Validates permissions for optimizing images through REST API.
65     *
66     * @return bool
67     */
68    public static function rest_can_optimize_images() {
69        return self::rest_can_upload_media() && self::can_manage_media_library();
70    }
71}