PluginProbe
Parse.ly / 3.16.1
Parse.ly v3.16.1
3.24.1 3.24.0 3.23.7 3.23.6 3.23.5 3.23.4 3.23.3 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.17.0 3.18.0 3.18.1 3.19.0 3.19.1 3.19.2 3.19.3 3.2.0 3.2.1 3.20.0 3.20.1 3.20.2 3.20.3 All 105 releases
wp-parsely / src / class-permissions.php

class-permissions.php in Parse.ly 3.16.1, at src/class-permissions.php

173 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Permissions Class
4 *
5 * @package Parsely
6 * @since 3.16.0
7 */
8
9 declare(strict_types=1);
10
11 namespace Parsely;
12
13 /**
14 * Class implementing user/role permissions functionality.
15 *
16 * @since 3.16.0
17 *
18 * @phpstan-import-type Parsely_Options_Content_Helper from Parsely
19 * @phpstan-import-type Parsely_Options_Content_Helper_Feature from Parsely
20 */
21 class Permissions {
22 /**
23 * Returns all user roles (custom roles included) that have the edit_posts
24 * capability.
25 *
26 * The return value is an associative array with role keys and names, e.g.
27 * `array( 'administrator' => 'Administrator', 'editor' => 'Editor' )`.
28 *
29 * @since 3.16.0
30 *
31 * @return array<string, string> The user roles having the edit_posts capability.
32 */
33 public static function get_user_roles_with_edit_posts_cap(): array {
34 $result = array();
35 $roles = wp_roles()->roles;
36
37 foreach ( $roles as $key => $role ) {
38 if ( isset( $role['capabilities']['edit_posts'] ) ) {
39 $result[ $key ] = $role['name'];
40 }
41 }
42
43 return $result;
44 }
45
46 /**
47 * Returns whether the current user has the permission to access the
48 * specified Content Helper feature.
49 *
50 * @since 3.16.0
51 *
52 * @param string $feature_name The feature's name.
53 * @param Parsely_Options_Content_Helper $pch_options The Content Helper options.
54 * @param int|false $post_id The post ID, if the check is for a specific post.
55 * @return bool Whether the current user can access the specified feature.
56 */
57 public static function current_user_can_use_pch_feature(
58 string $feature_name,
59 $pch_options,
60 $post_id = false
61 ): bool {
62 if ( isset( $pch_options[ $feature_name ] ) ) {
63 /**
64 * The feature's options.
65 *
66 * @var Parsely_Options_Content_Helper_Feature $feature_options
67 */
68 $feature_options = $pch_options[ $feature_name ];
69 } else {
70 return false;
71 }
72
73 // All AI features are disabled.
74 if ( true !== $pch_options['ai_features_enabled'] ) {
75 return false;
76 }
77
78 // The specific AI feature is disabled.
79 if ( true !== $feature_options['enabled'] ) {
80 return false;
81 }
82
83 $current_user = wp_get_current_user();
84 $user_roles = $current_user->roles;
85
86 // Current user's role is not yet set.
87 if ( 0 === count( $user_roles ) ) {
88 return false;
89 }
90
91 // Get the roles with the capability to edit posts.
92 $valid_roles = array_keys( self::get_user_roles_with_edit_posts_cap() );
93
94 // Check that at least one of the user's roles has the capability to edit posts.
95 if ( 0 === count( array_intersect( $user_roles, $valid_roles ) ) ) {
96 return false;
97 }
98
99 // Check that at least one of the user's roles has access to the specific feature/post.
100 $allowed_roles = $feature_options['allowed_user_roles'];
101 if ( 0 === count( array_intersect( $user_roles, $allowed_roles ) ) ) {
102 return false;
103 }
104
105 // Check if the user can edit the post.
106 if ( (int) $post_id > 0 ) {
107 return current_user_can( 'edit_post', $post_id );
108 }
109
110 return true;
111 }
112
113 /**
114 * Returns a JSON-encoded string with the Content Helper permissions for the
115 * current user.
116 *
117 * @since 3.16.0
118 *
119 * @param Parsely_Options_Content_Helper $pch_options The options to check against.
120 * @return string The JSON-encoded permissions string.
121 */
122 public static function get_pch_permissions_json( $pch_options ): string {
123 $permissions = array();
124 $features = array(
125 'SmartLinking' => 'smart_linking',
126 'TitleSuggestions' => 'title_suggestions',
127 );
128
129 foreach ( $features as $key => $value ) {
130 $permissions[ $key ] = self::current_user_can_use_pch_feature(
131 $value,
132 $pch_options,
133 get_the_ID()
134 );
135 }
136
137 $result = wp_json_encode( $permissions );
138
139 return is_string( $result ) ? $result : '{}';
140 }
141
142 /**
143 * Builds and returns a permissions settings array for the Content Helper,
144 * based on the passed values.
145 *
146 * @since 3.16.0
147 *
148 * @param bool $enabled Whether to enable the features.
149 * @param array<int, string> $allowed_user_roles The allowed user roles.
150 * @return Parsely_Options_Content_Helper The resulting permissions settings.
151 */
152 public static function build_pch_permissions_settings_array(
153 bool $enabled,
154 array $allowed_user_roles
155 ) {
156 return array(
157 'ai_features_enabled' => $enabled,
158 'smart_linking' => array(
159 'enabled' => $enabled,
160 'allowed_user_roles' => $allowed_user_roles,
161 ),
162 'title_suggestions' => array(
163 'enabled' => $enabled,
164 'allowed_user_roles' => $allowed_user_roles,
165 ),
166 'excerpt_suggestions' => array(
167 'enabled' => $enabled,
168 'allowed_user_roles' => $allowed_user_roles,
169 ),
170 );
171 }
172 }
173