| @@ -9,8 +9,10 @@ | ||
| 9 | 9 | declare(strict_types=1); |
| 10 | 10 | |
| 11 | 11 | namespace Parsely; |
| 12 | 12 | |
| 13 | +use Parsely\Content_Helper\Suggestion_Defaults; | |
| 14 | + | |
| 13 | 15 | /** |
| 14 | 16 | * Class implementing user/role permissions functionality. |
| 15 | 17 | * |
| 16 | 18 | * @since 3.16.0 |
| @@ -44,14 +46,15 @@ | ||
| 44 | 46 | } |
| 45 | 47 | |
| 46 | 48 | /** |
| 47 | 49 | * Returns whether the current user has the permission to access the |
| 48 | - * specified Content Helper feature. | |
| 50 | + * specified Content Intelligence feature. | |
| 49 | 51 | * |
| 50 | 52 | * @since 3.16.0 |
| 53 | + * @since 3.24.2 Post-specific checks always require the `edit_post` capability. | |
| 51 | 54 | * |
| 52 | 55 | * @param string $feature_name The feature's name. |
| 53 | - * @param Parsely_Options_Content_Helper $pch_options The Content Helper options. | |
| 56 | + * @param Parsely_Options_Content_Helper $pch_options The Content Intelligence options. | |
| 54 | 57 | * @param int|false $post_id The post ID, if the check is for a specific post. |
| 55 | 58 | * @return bool Whether the current user can access the specified feature. |
| 56 | 59 | */ |
| 57 | 60 | public static function current_user_can_use_pch_feature( |
| @@ -58,8 +61,14 @@ | ||
| 58 | 61 | string $feature_name, |
| 59 | 62 | $pch_options, |
| 60 | 63 | $post_id = false |
| 61 | 64 | ): bool { |
| 65 | + // Post-specific checks always require edit access, whatever the filter | |
| 66 | + // below returns. | |
| 67 | + if ( (int) $post_id > 0 && ! current_user_can( 'edit_post', (int) $post_id ) ) { | |
| 68 | + return false; | |
| 69 | + } | |
| 70 | + | |
| 62 | 71 | if ( isset( $pch_options[ $feature_name ] ) ) { |
| 63 | 72 | /** |
| 64 | 73 | * The feature's options. |
| 65 | 74 | * |
| @@ -69,8 +78,44 @@ | ||
| 69 | 78 | } else { |
| 70 | 79 | return false; |
| 71 | 80 | } |
| 72 | 81 | |
| 82 | + $current_user = wp_get_current_user(); | |
| 83 | + $user_roles = $current_user->roles; | |
| 84 | + | |
| 85 | + /** | |
| 86 | + * Filters whether the current user can use the specified Content | |
| 87 | + * Intelligence feature. | |
| 88 | + * | |
| 89 | + * This filter can be used to override the default permissions check. | |
| 90 | + * It cannot grant access to a post that the user cannot edit. | |
| 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 | + * Is null when the filter isn't being used. | |
| 96 | + * @param string $feature_name The feature's name. | |
| 97 | + * @param \WP_User $current_user The current user object. | |
| 98 | + * @param int|false $post_id The post ID, if the check is for a specific post. | |
| 99 | + */ | |
| 100 | + $filtered_current_user_can_use_pch_feature = apply_filters( | |
| 101 | + 'wp_parsely_current_user_can_use_pch_feature', | |
| 102 | + null, | |
| 103 | + $feature_name, | |
| 104 | + $current_user, | |
| 105 | + $post_id | |
| 106 | + ); | |
| 107 | + | |
| 108 | + // When $wp_parsely_current_user_can_use_pch_feature's value is set, | |
| 109 | + // return it without further processing. | |
| 110 | + if ( null !== $filtered_current_user_can_use_pch_feature ) { | |
| 111 | + if ( true === $filtered_current_user_can_use_pch_feature ) { | |
| 112 | + return true; | |
| 113 | + } | |
| 114 | + | |
| 115 | + return false; | |
| 116 | + } | |
| 117 | + | |
| 73 | 118 | // All AI features are disabled. |
| 74 | 119 | if ( true !== $pch_options['ai_features_enabled'] ) { |
| 75 | 120 | return false; |
| 76 | 121 | } |
| @@ -79,37 +124,38 @@ | ||
| 79 | 124 | if ( true !== $feature_options['enabled'] ) { |
| 80 | 125 | return false; |
| 81 | 126 | } |
| 82 | 127 | |
| 128 | + // If the user is a super admin, they can access the feature. | |
| 129 | + if ( is_multisite() && is_super_admin( $current_user->ID ) ) { | |
| 130 | + return true; | |
| 131 | + } | |
| 132 | + | |
| 83 | 133 | // Current user's role is not yet set. |
| 84 | - $current_user = wp_get_current_user(); | |
| 85 | - if ( 0 === count( $current_user->roles ) ) { | |
| 134 | + if ( 0 === count( $user_roles ) ) { | |
| 86 | 135 | return false; |
| 87 | 136 | } |
| 88 | 137 | |
| 89 | - // Check that the user's role has the capability to edit posts. | |
| 90 | - $current_user_role = $current_user->roles[0]; | |
| 91 | - $valid_roles = array_keys( self::get_user_roles_with_edit_posts_cap() ); | |
| 92 | - if ( ! in_array( $current_user_role, $valid_roles, true ) ) { | |
| 138 | + // Get the roles with the capability to edit posts. | |
| 139 | + $valid_roles = array_keys( self::get_user_roles_with_edit_posts_cap() ); | |
| 140 | + | |
| 141 | + // Check that at least one of the user's roles has the capability to edit posts. | |
| 142 | + if ( 0 === count( array_intersect( $user_roles, $valid_roles ) ) ) { | |
| 93 | 143 | return false; |
| 94 | 144 | } |
| 95 | 145 | |
| 96 | - // Check that the user's role has access to the specific feature/post. | |
| 146 | + // Check that at least one of the user's roles has access to the specific feature/post. | |
| 97 | 147 | $allowed_roles = $feature_options['allowed_user_roles']; |
| 98 | - if ( in_array( $current_user_role, $allowed_roles, true ) ) { | |
| 99 | - if ( (int) $post_id > 0 ) { | |
| 100 | - return current_user_can( 'edit_post', $post_id ); | |
| 101 | - } | |
| 102 | - | |
| 103 | - return true; | |
| 148 | + if ( 0 === count( array_intersect( $user_roles, $allowed_roles ) ) ) { | |
| 149 | + return false; | |
| 104 | 150 | } |
| 105 | 151 | |
| 106 | - return false; | |
| 152 | + return true; | |
| 107 | 153 | } |
| 108 | 154 | |
| 109 | 155 | /** |
| 110 | - * Returns a JSON-encoded string with the Content Helper permissions for the | |
| 111 | - * current user. | |
| 156 | + * Returns a JSON-encoded string with the Content Intelligence permissions | |
| 157 | + * for the current user. | |
| 112 | 158 | * |
| 113 | 159 | * @since 3.16.0 |
| 114 | 160 | * |
| 115 | 161 | * @param Parsely_Options_Content_Helper $pch_options The options to check against. |
| @@ -117,10 +163,12 @@ | ||
| 117 | 163 | */ |
| 118 | 164 | public static function get_pch_permissions_json( $pch_options ): string { |
| 119 | 165 | $permissions = array(); |
| 120 | 166 | $features = array( |
| 121 | - 'SmartLinking' => 'smart_linking', | |
| 122 | - 'TitleSuggestions' => 'title_suggestions', | |
| 167 | + 'SmartLinking' => 'smart_linking', | |
| 168 | + 'TitleSuggestions' => 'title_suggestions', | |
| 169 | + 'ExcerptSuggestions' => 'excerpt_suggestions', | |
| 170 | + 'TrafficBoost' => 'traffic_boost', | |
| 123 | 171 | ); |
| 124 | 172 | |
| 125 | 173 | foreach ( $features as $key => $value ) { |
| 126 | 174 | $permissions[ $key ] = self::current_user_can_use_pch_feature( |
| @@ -135,9 +183,9 @@ | ||
| 135 | 183 | return is_string( $result ) ? $result : '{}'; |
| 136 | 184 | } |
| 137 | 185 | |
| 138 | 186 | /** |
| 139 | - * Builds and returns a permissions settings array for the Content Helper, | |
| 187 | + * Builds and returns a permissions settings array for Content Intelligence, | |
| 140 | 188 | * based on the passed values. |
| 141 | 189 | * |
| 142 | 190 | * @since 3.16.0 |
| 143 | 191 | * |
| @@ -157,10 +205,19 @@ | ||
| 157 | 205 | ), |
| 158 | 206 | 'title_suggestions' => array( |
| 159 | 207 | 'enabled' => $enabled, |
| 160 | 208 | 'allowed_user_roles' => $allowed_user_roles, |
| 209 | + 'default_tone' => Suggestion_Defaults::DEFAULT_TONE, | |
| 210 | + 'default_persona' => Suggestion_Defaults::DEFAULT_PERSONA, | |
| 161 | 211 | ), |
| 162 | 212 | 'excerpt_suggestions' => array( |
| 213 | + 'enabled' => $enabled, | |
| 214 | + 'allowed_user_roles' => $allowed_user_roles, | |
| 215 | + 'default_length' => Suggestion_Defaults::DEFAULT_LENGTH, | |
| 216 | + 'default_tone' => Suggestion_Defaults::DEFAULT_TONE, | |
| 217 | + 'default_persona' => Suggestion_Defaults::DEFAULT_PERSONA, | |
| 218 | + ), | |
| 219 | + 'traffic_boost' => array( | |
| 163 | 220 | 'enabled' => $enabled, |
| 164 | 221 | 'allowed_user_roles' => $allowed_user_roles, |
| 165 | 222 | ), |
| 166 | 223 | ); |