| @@ -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,30 +78,22 @@ | ||
| 69 | 78 | } else { |
| 70 | 79 | return false; |
| 71 | 80 | } |
| 72 | 81 | |
| 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 | 82 | $current_user = wp_get_current_user(); |
| 84 | 83 | $user_roles = $current_user->roles; |
| 85 | 84 | |
| 86 | 85 | /** |
| 87 | - * Filters whether the current user can use the specified Content Helper | |
| 88 | - * feature. | |
| 86 | + * Filters whether the current user can use the specified Content | |
| 87 | + * Intelligence feature. | |
| 89 | 88 | * |
| 90 | 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 | 91 | * |
| 92 | 92 | * @since 3.16.2 |
| 93 | 93 | * |
| 94 | - * @param bool $current_user_can_use_pch_feature Whether the current user can use the feature. | |
| 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. | |
| 95 | 96 | * @param string $feature_name The feature's name. |
| 96 | 97 | * @param \WP_User $current_user The current user object. |
| 97 | 98 | * @param int|false $post_id The post ID, if the check is for a specific post. |
| 98 | 99 | */ |
| @@ -97,15 +98,36 @@ | ||
| 97 | 98 | * @param int|false $post_id The post ID, if the check is for a specific post. |
| 98 | 99 | */ |
| 99 | 100 | $filtered_current_user_can_use_pch_feature = apply_filters( |
| 100 | 101 | 'wp_parsely_current_user_can_use_pch_feature', |
| 101 | - false, | |
| 102 | + null, | |
| 102 | 103 | $feature_name, |
| 103 | 104 | $current_user, |
| 104 | 105 | $post_id |
| 105 | 106 | ); |
| 106 | 107 | |
| 107 | - if ( true === $filtered_current_user_can_use_pch_feature ) { | |
| 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 | + | |
| 118 | + // All AI features are disabled. | |
| 119 | + if ( true !== $pch_options['ai_features_enabled'] ) { | |
| 120 | + return false; | |
| 121 | + } | |
| 122 | + | |
| 123 | + // The specific AI feature is disabled. | |
| 124 | + if ( true !== $feature_options['enabled'] ) { | |
| 125 | + return false; | |
| 126 | + } | |
| 127 | + | |
| 128 | + // If the user is a super admin, they can access the feature. | |
| 129 | + if ( is_multisite() && is_super_admin( $current_user->ID ) ) { | |
| 108 | 130 | return true; |
| 109 | 131 | } |
| 110 | 132 | |
| 111 | 133 | // Current user's role is not yet set. |
| @@ -126,19 +148,14 @@ | ||
| 126 | 148 | if ( 0 === count( array_intersect( $user_roles, $allowed_roles ) ) ) { |
| 127 | 149 | return false; |
| 128 | 150 | } |
| 129 | 151 | |
| 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 | 152 | return true; |
| 136 | 153 | } |
| 137 | 154 | |
| 138 | 155 | /** |
| 139 | - * Returns a JSON-encoded string with the Content Helper permissions for the | |
| 140 | - * current user. | |
| 156 | + * Returns a JSON-encoded string with the Content Intelligence permissions | |
| 157 | + * for the current user. | |
| 141 | 158 | * |
| 142 | 159 | * @since 3.16.0 |
| 143 | 160 | * |
| 144 | 161 | * @param Parsely_Options_Content_Helper $pch_options The options to check against. |
| @@ -146,10 +163,12 @@ | ||
| 146 | 163 | */ |
| 147 | 164 | public static function get_pch_permissions_json( $pch_options ): string { |
| 148 | 165 | $permissions = array(); |
| 149 | 166 | $features = array( |
| 150 | - 'SmartLinking' => 'smart_linking', | |
| 151 | - 'TitleSuggestions' => 'title_suggestions', | |
| 167 | + 'SmartLinking' => 'smart_linking', | |
| 168 | + 'TitleSuggestions' => 'title_suggestions', | |
| 169 | + 'ExcerptSuggestions' => 'excerpt_suggestions', | |
| 170 | + 'TrafficBoost' => 'traffic_boost', | |
| 152 | 171 | ); |
| 153 | 172 | |
| 154 | 173 | foreach ( $features as $key => $value ) { |
| 155 | 174 | $permissions[ $key ] = self::current_user_can_use_pch_feature( |
| @@ -164,9 +183,9 @@ | ||
| 164 | 183 | return is_string( $result ) ? $result : '{}'; |
| 165 | 184 | } |
| 166 | 185 | |
| 167 | 186 | /** |
| 168 | - * Builds and returns a permissions settings array for the Content Helper, | |
| 187 | + * Builds and returns a permissions settings array for Content Intelligence, | |
| 169 | 188 | * based on the passed values. |
| 170 | 189 | * |
| 171 | 190 | * @since 3.16.0 |
| 172 | 191 | * |
| @@ -186,10 +205,19 @@ | ||
| 186 | 205 | ), |
| 187 | 206 | 'title_suggestions' => array( |
| 188 | 207 | 'enabled' => $enabled, |
| 189 | 208 | 'allowed_user_roles' => $allowed_user_roles, |
| 209 | + 'default_tone' => Suggestion_Defaults::DEFAULT_TONE, | |
| 210 | + 'default_persona' => Suggestion_Defaults::DEFAULT_PERSONA, | |
| 190 | 211 | ), |
| 191 | 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( | |
| 192 | 220 | 'enabled' => $enabled, |
| 193 | 221 | 'allowed_user_roles' => $allowed_user_roles, |
| 194 | 222 | ), |
| 195 | 223 | ); |