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

198 lines 5.3 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 /**
87 * Filters whether the current user can use the specified Content Helper
88 * feature.
89 *
90 * This filter can be used to override the default permissions check.
91 *
92 * @since 3.16.2
93 *
94 * @param bool $current_user_can_use_pch_feature Whether the current user can use the feature.
95 * @param string $feature_name The feature's name.
96 * @param \WP_User $current_user The current user object.
97 * @param int|false $post_id The post ID, if the check is for a specific post.
98 */
99 $filtered_current_user_can_use_pch_feature = apply_filters(
100 'wp_parsely_current_user_can_use_pch_feature',
101 false,
102 $feature_name,
103 $current_user,
104 $post_id
105 );
106
107 if ( true === $filtered_current_user_can_use_pch_feature ) {
108 return true;
109 }
110
111 // Current user's role is not yet set.
112 if ( 0 === count( $user_roles ) ) {
113 return false;
114 }
115
116 // Get the roles with the capability to edit posts.
117 $valid_roles = array_keys( self::get_user_roles_with_edit_posts_cap() );
118
119 // Check that at least one of the user's roles has the capability to edit posts.
120 if ( 0 === count( array_intersect( $user_roles, $valid_roles ) ) ) {
121 return false;
122 }
123
124 // Check that at least one of the user's roles has access to the specific feature/post.
125 $allowed_roles = $feature_options['allowed_user_roles'];
126 if ( 0 === count( array_intersect( $user_roles, $allowed_roles ) ) ) {
127 return false;
128 }
129
130 // Check if the user can edit the post.
131 if ( (int) $post_id > 0 ) {
132 return current_user_can( 'edit_post', $post_id );
133 }
134
135 return true;
136 }
137
138 /**
139 * Returns a JSON-encoded string with the Content Helper permissions for the
140 * current user.
141 *
142 * @since 3.16.0
143 *
144 * @param Parsely_Options_Content_Helper $pch_options The options to check against.
145 * @return string The JSON-encoded permissions string.
146 */
147 public static function get_pch_permissions_json( $pch_options ): string {
148 $permissions = array();
149 $features = array(
150 'SmartLinking' => 'smart_linking',
151 'TitleSuggestions' => 'title_suggestions',
152 );
153
154 foreach ( $features as $key => $value ) {
155 $permissions[ $key ] = self::current_user_can_use_pch_feature(
156 $value,
157 $pch_options,
158 get_the_ID()
159 );
160 }
161
162 $result = wp_json_encode( $permissions );
163
164 return is_string( $result ) ? $result : '{}';
165 }
166
167 /**
168 * Builds and returns a permissions settings array for the Content Helper,
169 * based on the passed values.
170 *
171 * @since 3.16.0
172 *
173 * @param bool $enabled Whether to enable the features.
174 * @param array<int, string> $allowed_user_roles The allowed user roles.
175 * @return Parsely_Options_Content_Helper The resulting permissions settings.
176 */
177 public static function build_pch_permissions_settings_array(
178 bool $enabled,
179 array $allowed_user_roles
180 ) {
181 return array(
182 'ai_features_enabled' => $enabled,
183 'smart_linking' => array(
184 'enabled' => $enabled,
185 'allowed_user_roles' => $allowed_user_roles,
186 ),
187 'title_suggestions' => array(
188 'enabled' => $enabled,
189 'allowed_user_roles' => $allowed_user_roles,
190 ),
191 'excerpt_suggestions' => array(
192 'enabled' => $enabled,
193 'allowed_user_roles' => $allowed_user_roles,
194 ),
195 );
196 }
197 }
198