| @@ -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 | * |
| @@ -73,12 +82,13 @@ | ||
| 73 | 82 | $current_user = wp_get_current_user(); |
| 74 | 83 | $user_roles = $current_user->roles; |
| 75 | 84 | |
| 76 | 85 | /** |
| 77 | - * Filters whether the current user can use the specified Content Helper | |
| 78 | - * feature. | |
| 86 | + * Filters whether the current user can use the specified Content | |
| 87 | + * Intelligence feature. | |
| 79 | 88 | * |
| 80 | 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. | |
| 81 | 91 | * |
| 82 | 92 | * @since 3.16.2 |
| 83 | 93 | * |
| 84 | 94 | * @param ?bool $current_user_can_use_pch_feature Whether the current user can use the feature. |
| @@ -114,8 +124,13 @@ | ||
| 114 | 124 | if ( true !== $feature_options['enabled'] ) { |
| 115 | 125 | return false; |
| 116 | 126 | } |
| 117 | 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 | + | |
| 118 | 133 | // Current user's role is not yet set. |
| 119 | 134 | if ( 0 === count( $user_roles ) ) { |
| 120 | 135 | return false; |
| 121 | 136 | } |
| @@ -133,19 +148,14 @@ | ||
| 133 | 148 | if ( 0 === count( array_intersect( $user_roles, $allowed_roles ) ) ) { |
| 134 | 149 | return false; |
| 135 | 150 | } |
| 136 | 151 | |
| 137 | - // Check if the user can edit the post. | |
| 138 | - if ( (int) $post_id > 0 ) { | |
| 139 | - return current_user_can( 'edit_post', $post_id ); | |
| 140 | - } | |
| 141 | - | |
| 142 | 152 | return true; |
| 143 | 153 | } |
| 144 | 154 | |
| 145 | 155 | /** |
| 146 | - * Returns a JSON-encoded string with the Content Helper permissions for the | |
| 147 | - * current user. | |
| 156 | + * Returns a JSON-encoded string with the Content Intelligence permissions | |
| 157 | + * for the current user. | |
| 148 | 158 | * |
| 149 | 159 | * @since 3.16.0 |
| 150 | 160 | * |
| 151 | 161 | * @param Parsely_Options_Content_Helper $pch_options The options to check against. |
| @@ -153,10 +163,12 @@ | ||
| 153 | 163 | */ |
| 154 | 164 | public static function get_pch_permissions_json( $pch_options ): string { |
| 155 | 165 | $permissions = array(); |
| 156 | 166 | $features = array( |
| 157 | - 'SmartLinking' => 'smart_linking', | |
| 158 | - 'TitleSuggestions' => 'title_suggestions', | |
| 167 | + 'SmartLinking' => 'smart_linking', | |
| 168 | + 'TitleSuggestions' => 'title_suggestions', | |
| 169 | + 'ExcerptSuggestions' => 'excerpt_suggestions', | |
| 170 | + 'TrafficBoost' => 'traffic_boost', | |
| 159 | 171 | ); |
| 160 | 172 | |
| 161 | 173 | foreach ( $features as $key => $value ) { |
| 162 | 174 | $permissions[ $key ] = self::current_user_can_use_pch_feature( |
| @@ -171,9 +183,9 @@ | ||
| 171 | 183 | return is_string( $result ) ? $result : '{}'; |
| 172 | 184 | } |
| 173 | 185 | |
| 174 | 186 | /** |
| 175 | - * Builds and returns a permissions settings array for the Content Helper, | |
| 187 | + * Builds and returns a permissions settings array for Content Intelligence, | |
| 176 | 188 | * based on the passed values. |
| 177 | 189 | * |
| 178 | 190 | * @since 3.16.0 |
| 179 | 191 | * |
| @@ -193,10 +205,19 @@ | ||
| 193 | 205 | ), |
| 194 | 206 | 'title_suggestions' => array( |
| 195 | 207 | 'enabled' => $enabled, |
| 196 | 208 | 'allowed_user_roles' => $allowed_user_roles, |
| 209 | + 'default_tone' => Suggestion_Defaults::DEFAULT_TONE, | |
| 210 | + 'default_persona' => Suggestion_Defaults::DEFAULT_PERSONA, | |
| 197 | 211 | ), |
| 198 | 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( | |
| 199 | 220 | 'enabled' => $enabled, |
| 200 | 221 | 'allowed_user_roles' => $allowed_user_roles, |
| 201 | 222 | ), |
| 202 | 223 | ); |