PluginProbe
Parse.ly / 3.24.0
Parse.ly v3.24.0
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.24.0, at src/class-permissions.php

223 lines 6.4 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 use Parsely\Content_Helper\Suggestion_Defaults;
14
15 /**
16 * Class implementing user/role permissions functionality.
17 *
18 * @since 3.16.0
19 *
20 * @phpstan-import-type Parsely_Options_Content_Helper from Parsely
21 * @phpstan-import-type Parsely_Options_Content_Helper_Feature from Parsely
22 */
23 class Permissions {
24 /**
25 * Returns all user roles (custom roles included) that have the edit_posts
26 * capability.
27 *
28 * The return value is an associative array with role keys and names, e.g.
29 * `array( 'administrator' => 'Administrator', 'editor' => 'Editor' )`.
30 *
31 * @since 3.16.0
32 *
33 * @return array<string, string> The user roles having the edit_posts capability.
34 */
35 public static function get_user_roles_with_edit_posts_cap(): array {
36 $result = array();
37 $roles = wp_roles()->roles;
38
39 foreach ( $roles as $key => $role ) {
40 if ( isset( $role['capabilities']['edit_posts'] ) ) {
41 $result[ $key ] = $role['name'];
42 }
43 }
44
45 return $result;
46 }
47
48 /**
49 * Returns whether the current user has the permission to access the
50 * specified Content Intelligence feature.
51 *
52 * @since 3.16.0
53 *
54 * @param string $feature_name The feature's name.
55 * @param Parsely_Options_Content_Helper $pch_options The Content Intelligence options.
56 * @param int|false $post_id The post ID, if the check is for a specific post.
57 * @return bool Whether the current user can access the specified feature.
58 */
59 public static function current_user_can_use_pch_feature(
60 string $feature_name,
61 $pch_options,
62 $post_id = false
63 ): bool {
64 if ( isset( $pch_options[ $feature_name ] ) ) {
65 /**
66 * The feature's options.
67 *
68 * @var Parsely_Options_Content_Helper_Feature $feature_options
69 */
70 $feature_options = $pch_options[ $feature_name ];
71 } else {
72 return false;
73 }
74
75 $current_user = wp_get_current_user();
76 $user_roles = $current_user->roles;
77
78 /**
79 * Filters whether the current user can use the specified Content
80 * Intelligence feature.
81 *
82 * This filter can be used to override the default permissions check.
83 *
84 * @since 3.16.2
85 *
86 * @param ?bool $current_user_can_use_pch_feature Whether the current user can use the feature.
87 * Is null when the filter isn't being used.
88 * @param string $feature_name The feature's name.
89 * @param \WP_User $current_user The current user object.
90 * @param int|false $post_id The post ID, if the check is for a specific post.
91 */
92 $filtered_current_user_can_use_pch_feature = apply_filters(
93 'wp_parsely_current_user_can_use_pch_feature',
94 null,
95 $feature_name,
96 $current_user,
97 $post_id
98 );
99
100 // When $wp_parsely_current_user_can_use_pch_feature's value is set,
101 // return it without further processing.
102 if ( null !== $filtered_current_user_can_use_pch_feature ) {
103 if ( true === $filtered_current_user_can_use_pch_feature ) {
104 return true;
105 }
106
107 return false;
108 }
109
110 // All AI features are disabled.
111 if ( true !== $pch_options['ai_features_enabled'] ) {
112 return false;
113 }
114
115 // The specific AI feature is disabled.
116 if ( true !== $feature_options['enabled'] ) {
117 return false;
118 }
119
120 // If the user is a super admin, they can access the feature.
121 if ( is_multisite() && is_super_admin( $current_user->ID ) ) {
122 return true;
123 }
124
125 // Current user's role is not yet set.
126 if ( 0 === count( $user_roles ) ) {
127 return false;
128 }
129
130 // Get the roles with the capability to edit posts.
131 $valid_roles = array_keys( self::get_user_roles_with_edit_posts_cap() );
132
133 // Check that at least one of the user's roles has the capability to edit posts.
134 if ( 0 === count( array_intersect( $user_roles, $valid_roles ) ) ) {
135 return false;
136 }
137
138 // Check that at least one of the user's roles has access to the specific feature/post.
139 $allowed_roles = $feature_options['allowed_user_roles'];
140 if ( 0 === count( array_intersect( $user_roles, $allowed_roles ) ) ) {
141 return false;
142 }
143
144 // Check if the user can edit the post.
145 if ( (int) $post_id > 0 ) {
146 return current_user_can( 'edit_post', $post_id );
147 }
148
149 return true;
150 }
151
152 /**
153 * Returns a JSON-encoded string with the Content Intelligence permissions
154 * for the current user.
155 *
156 * @since 3.16.0
157 *
158 * @param Parsely_Options_Content_Helper $pch_options The options to check against.
159 * @return string The JSON-encoded permissions string.
160 */
161 public static function get_pch_permissions_json( $pch_options ): string {
162 $permissions = array();
163 $features = array(
164 'SmartLinking' => 'smart_linking',
165 'TitleSuggestions' => 'title_suggestions',
166 'ExcerptSuggestions' => 'excerpt_suggestions',
167 'TrafficBoost' => 'traffic_boost',
168 );
169
170 foreach ( $features as $key => $value ) {
171 $permissions[ $key ] = self::current_user_can_use_pch_feature(
172 $value,
173 $pch_options,
174 get_the_ID()
175 );
176 }
177
178 $result = wp_json_encode( $permissions );
179
180 return is_string( $result ) ? $result : '{}';
181 }
182
183 /**
184 * Builds and returns a permissions settings array for Content Intelligence,
185 * based on the passed values.
186 *
187 * @since 3.16.0
188 *
189 * @param bool $enabled Whether to enable the features.
190 * @param array<int, string> $allowed_user_roles The allowed user roles.
191 * @return Parsely_Options_Content_Helper The resulting permissions settings.
192 */
193 public static function build_pch_permissions_settings_array(
194 bool $enabled,
195 array $allowed_user_roles
196 ) {
197 return array(
198 'ai_features_enabled' => $enabled,
199 'smart_linking' => array(
200 'enabled' => $enabled,
201 'allowed_user_roles' => $allowed_user_roles,
202 ),
203 'title_suggestions' => array(
204 'enabled' => $enabled,
205 'allowed_user_roles' => $allowed_user_roles,
206 'default_tone' => Suggestion_Defaults::DEFAULT_TONE,
207 'default_persona' => Suggestion_Defaults::DEFAULT_PERSONA,
208 ),
209 'excerpt_suggestions' => array(
210 'enabled' => $enabled,
211 'allowed_user_roles' => $allowed_user_roles,
212 'default_length' => Suggestion_Defaults::DEFAULT_LENGTH,
213 'default_tone' => Suggestion_Defaults::DEFAULT_TONE,
214 'default_persona' => Suggestion_Defaults::DEFAULT_PERSONA,
215 ),
216 'traffic_boost' => array(
217 'enabled' => $enabled,
218 'allowed_user_roles' => $allowed_user_roles,
219 ),
220 );
221 }
222 }
223