| @@ -25,11 +25,11 @@ | ||
| 25 | 25 | * Default Plugin Options. |
| 26 | 26 | * |
| 27 | 27 | * @since 1.0.0 |
| 28 | 28 | * @since 2.0.0 Added the "modules" option. |
| 29 | - * @var array | |
| 29 | + * @var array<string, mixed> | |
| 30 | 30 | */ |
| 31 | - protected $default_plugin_options = array( | |
| 31 | + protected array $default_plugin_options = array( | |
| 32 | 32 | 'plugin_options_db_version' => 0, |
| 33 | 33 | 'table_scheme_db_version' => 0, |
| 34 | 34 | 'prev_tablepress_version' => '0', |
| 35 | 35 | 'tablepress_version' => TablePress::version, |
| @@ -47,14 +47,17 @@ | ||
| 47 | 47 | /** |
| 48 | 48 | * Default User Options. |
| 49 | 49 | * |
| 50 | 50 | * @since 1.0.0 |
| 51 | - * @var array | |
| 51 | + * @var array<string, mixed> | |
| 52 | 52 | */ |
| 53 | - protected $default_user_options = array( | |
| 54 | - 'user_options_db_version' => TablePress::db_version, // To prevent saving on first load. | |
| 55 | - 'admin_menu_parent_page' => 'middle', | |
| 56 | - 'message_first_visit' => true, | |
| 53 | + protected array $default_user_options = array( | |
| 54 | + 'user_options_db_version' => TablePress::db_version, // To prevent saving on first load. | |
| 55 | + 'admin_menu_parent_page' => 'middle', | |
| 56 | + 'message_first_visit' => true, | |
| 57 | + 'message_superseded_extensions' => true, | |
| 58 | + 'table_editor_column_width' => '170', // Default column width of 170px of the table editor on the "Edit" screen. | |
| 59 | + 'table_editor_line_clamp' => '5', // Maximum number of lines per cell in the table editor on the "Edit" screen. | |
| 57 | 60 | ); |
| 58 | 61 | |
| 59 | 62 | /** |
| 60 | 63 | * Instance of WP_Option class for Plugin Options. |
| @@ -59,19 +62,17 @@ | ||
| 59 | 62 | /** |
| 60 | 63 | * Instance of WP_Option class for Plugin Options. |
| 61 | 64 | * |
| 62 | 65 | * @since 1.0.0 |
| 63 | - * @var TablePress_WP_Option | |
| 64 | 66 | */ |
| 65 | - protected $plugin_options; | |
| 67 | + protected \TablePress_WP_Option $plugin_options; | |
| 66 | 68 | |
| 67 | 69 | /** |
| 68 | 70 | * Instance of WP_User_Option class for User Options. |
| 69 | 71 | * |
| 70 | 72 | * @since 1.0.0 |
| 71 | - * @var TablePress_WP_User_Option | |
| 72 | 73 | */ |
| 73 | - protected $user_options; | |
| 74 | + protected \TablePress_WP_User_Option $user_options; | |
| 74 | 75 | |
| 75 | 76 | /** |
| 76 | 77 | * Init Options Model by creating the object instances for the Plugin and User Options. |
| 77 | 78 | * |
| @@ -100,12 +101,12 @@ | ||
| 100 | 101 | * Update a single option or an array of options with new values. |
| 101 | 102 | * |
| 102 | 103 | * @since 1.0.0 |
| 103 | 104 | * |
| 104 | - * @param array|string $new_options Array of new options (name => value) or name of a single option. | |
| 105 | - * @param mixed $single_value Optional. New value for a single option (only if $new_options is not an array). | |
| 105 | + * @param array<string, mixed>|string $new_options Array of new options (name => value) or name of a single option. | |
| 106 | + * @param mixed $single_value Optional. New value for a single option (only if $new_options is not an array). | |
| 106 | 107 | */ |
| 107 | - public function update( $new_options, $single_value = null ) { | |
| 108 | + public function update( /* array|string */ $new_options, /* string|bool|int|float|null */ $single_value = null ): void { | |
| 108 | 109 | // Allow saving of single options that are not in an array. |
| 109 | 110 | if ( ! is_array( $new_options ) ) { |
| 110 | 111 | $new_options = array( $new_options => $single_value ); |
| 111 | 112 | } |
| @@ -116,9 +117,9 @@ | ||
| 116 | 117 | if ( isset( $this->default_plugin_options[ $name ] ) ) { |
| 117 | 118 | $plugin_options[ $name ] = $value; |
| 118 | 119 | } elseif ( isset( $this->default_user_options[ $name ] ) ) { |
| 119 | 120 | $user_options[ $name ] = $value; |
| 120 | - } else { | |
| 121 | + } else { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedElse | |
| 121 | 122 | // No valid Plugin or User Option -> discard the name/value pair. |
| 122 | 123 | } |
| 123 | 124 | } |
| 124 | 125 | |
| @@ -134,9 +135,9 @@ | ||
| 134 | 135 | * @param string|false $name Optional. Name of a single option to get, or false for all options. |
| 135 | 136 | * @param mixed $default_value Optional. Default value, if the option $name does not exist. |
| 136 | 137 | * @return mixed Value of the retrieved option $name, or $default_value if it does not exist, or array of all options. |
| 137 | 138 | */ |
| 138 | - public function get( $name = false, $default_value = null ) { | |
| 139 | + public function get( /* string|false */ $name = false, /* string|bool|int|float|null */ $default_value = null ) /* : string|bool|int|float|array|null */ { | |
| 139 | 140 | if ( false === $name ) { |
| 140 | 141 | return array_merge( $this->plugin_options->get(), $this->user_options->get() ); |
| 141 | 142 | } |
| 142 | 143 | |
| @@ -155,11 +156,11 @@ | ||
| 155 | 156 | * Get all Plugin Options (only used in Debug). |
| 156 | 157 | * |
| 157 | 158 | * @since 1.0.0 |
| 158 | 159 | * |
| 159 | - * @return array Array of all Plugin Options. | |
| 160 | + * @return array<string, mixed> Array of all Plugin Options. | |
| 160 | 161 | */ |
| 161 | - public function _debug_get_plugin_options() { | |
| 162 | + public function _debug_get_plugin_options(): array { | |
| 162 | 163 | return $this->plugin_options->get(); |
| 163 | 164 | } |
| 164 | 165 | |
| 165 | 166 | /** |
| @@ -166,11 +167,11 @@ | ||
| 166 | 167 | * Get all User Options (only used in Debug). |
| 167 | 168 | * |
| 168 | 169 | * @since 1.0.0 |
| 169 | 170 | * |
| 170 | - * @return array Array of all User Options. | |
| 171 | + * @return array<string, mixed> Array of all User Options. | |
| 171 | 172 | */ |
| 172 | - public function _debug_get_user_options() { | |
| 173 | + public function _debug_get_user_options(): array { | |
| 173 | 174 | return $this->user_options->get(); |
| 174 | 175 | } |
| 175 | 176 | |
| 176 | 177 | /** |
| @@ -178,9 +179,9 @@ | ||
| 178 | 179 | * remove (no longer) existing options, e.g. after a plugin update. |
| 179 | 180 | * |
| 180 | 181 | * @since 1.0.0 |
| 181 | 182 | */ |
| 182 | - public function merge_plugin_options_defaults() { | |
| 183 | + public function merge_plugin_options_defaults(): void { | |
| 183 | 184 | $plugin_options = $this->plugin_options->get(); |
| 184 | 185 | // Remove old (i.e. no longer existing) Plugin Options. |
| 185 | 186 | $plugin_options = array_intersect_key( $plugin_options, $this->default_plugin_options ); |
| 186 | 187 | // Merge current into new Plugin Options. |
| @@ -194,9 +195,9 @@ | ||
| 194 | 195 | * remove (no longer) existing options, e.g. after a plugin update. |
| 195 | 196 | * |
| 196 | 197 | * @since 1.0.0 |
| 197 | 198 | */ |
| 198 | - public function merge_user_options_defaults() { | |
| 199 | + public function merge_user_options_defaults(): void { | |
| 199 | 200 | $user_options = $this->user_options->get(); |
| 200 | 201 | // Remove old (i.e. no longer existing) User Options. |
| 201 | 202 | $user_options = array_intersect_key( $user_options, $this->default_user_options ); |
| 202 | 203 | // Merge current into new User Options. |
| @@ -205,13 +206,13 @@ | ||
| 205 | 206 | $this->user_options->update( $user_options ); |
| 206 | 207 | } |
| 207 | 208 | |
| 208 | 209 | /** |
| 209 | - * Add default capabilities to "Administrator", "Editor", and "Author" user roles. | |
| 210 | + * Adds the TablePress default capabilities to the "Administrator", "Editor", and "Author" user roles. | |
| 210 | 211 | * |
| 211 | 212 | * @since 1.0.0 |
| 212 | 213 | */ |
| 213 | - public function add_access_capabilities() { | |
| 214 | + public function add_access_capabilities(): void { | |
| 214 | 215 | // Capabilities for all roles. |
| 215 | 216 | $roles = array( 'administrator', 'editor', 'author' ); |
| 216 | 217 | foreach ( $roles as $role ) { |
| 217 | 218 | $role = get_role( $role ); |
| @@ -238,9 +239,14 @@ | ||
| 238 | 239 | // Capabilities for single roles. |
| 239 | 240 | $role = get_role( 'administrator' ); |
| 240 | 241 | if ( ! empty( $role ) ) { |
| 241 | 242 | $role->add_cap( 'tablepress_edit_options' ); |
| 243 | + $role->add_cap( 'tablepress_import_tables_url' ); | |
| 242 | 244 | } |
| 245 | + $role = get_role( 'editor' ); | |
| 246 | + if ( ! empty( $role ) ) { | |
| 247 | + $role->add_cap( 'tablepress_import_tables_url' ); | |
| 248 | + } | |
| 243 | 249 | |
| 244 | 250 | // Refresh current set of capabilities of the user, to be able to directly use the new caps. |
| 245 | 251 | $user = wp_get_current_user(); |
| 246 | 252 | $user->get_role_caps(); |
| @@ -246,8 +252,27 @@ | ||
| 246 | 252 | $user->get_role_caps(); |
| 247 | 253 | } |
| 248 | 254 | |
| 249 | 255 | /** |
| 256 | + * Adds a custom "Import from URL" access capability to the "Editor" and "Administrator" user roles. | |
| 257 | + * | |
| 258 | + * @since 2.3.2 | |
| 259 | + */ | |
| 260 | + public function add_access_capabilities_tp232(): void { | |
| 261 | + $roles = array( 'administrator', 'editor' ); | |
| 262 | + foreach ( $roles as $role ) { | |
| 263 | + $role = get_role( $role ); | |
| 264 | + if ( ! empty( $role ) ) { | |
| 265 | + $role->add_cap( 'tablepress_import_tables_url' ); | |
| 266 | + } | |
| 267 | + } | |
| 268 | + | |
| 269 | + // Refresh current set of capabilities of the user, to be able to directly use the new caps. | |
| 270 | + $user = wp_get_current_user(); | |
| 271 | + $user->get_role_caps(); | |
| 272 | + } | |
| 273 | + | |
| 274 | + /** | |
| 250 | 275 | * Remove all TablePress capabilities from all roles. |
| 251 | 276 | * |
| 252 | 277 | * @since 1.1.0 |
| 253 | 278 | * |
| @@ -253,13 +278,13 @@ | ||
| 253 | 278 | * |
| 254 | 279 | * @global WP_Roles $wp_roles WordPress User Roles abstraction object. |
| 255 | 280 | * @see add_access_capabilities() |
| 256 | 281 | */ |
| 257 | - public function remove_access_capabilities() { | |
| 282 | + public function remove_access_capabilities(): void { | |
| 258 | 283 | global $wp_roles; |
| 259 | 284 | |
| 260 | 285 | if ( ! isset( $wp_roles ) ) { |
| 261 | - $wp_roles = new WP_Roles(); | |
| 286 | + $wp_roles = new WP_Roles(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited | |
| 262 | 287 | } |
| 263 | 288 | |
| 264 | 289 | foreach ( $wp_roles->roles as $role => $details ) { |
| 265 | 290 | $role = $wp_roles->get_role( $role ); |
| @@ -289,15 +314,22 @@ | ||
| 289 | 314 | * Map TablePress meta capabilities to primitive capabilities. |
| 290 | 315 | * |
| 291 | 316 | * @since 1.0.0 |
| 292 | 317 | * |
| 293 | - * @param array $caps Current set of primitive caps. | |
| 294 | - * @param string $cap Meta cap that is to be checked/mapped. | |
| 295 | - * @param int $user_id User ID for which meta cap is to be checked. | |
| 296 | - * @param array $args Arguments for the check, here e.g. the table ID. | |
| 297 | - * @return array Modified set of primitive caps. | |
| 318 | + * @param string[] $caps Current set of primitive caps. | |
| 319 | + * @param string $cap Meta cap that is to be checked/mapped. | |
| 320 | + * @param int $user_id User ID for which meta cap is to be checked. | |
| 321 | + * @param mixed[] $args Arguments for the check, here e.g. the table ID. | |
| 322 | + * @return string[] Modified set of primitive caps. | |
| 298 | 323 | */ |
| 299 | - public function map_tablepress_meta_caps( array $caps, $cap, $user_id, array $args ) { | |
| 324 | + public function map_tablepress_meta_caps( array $caps, /* string */ $cap, int $user_id, array $args ): array { | |
| 325 | + // Don't use a type hint for the `$cap` argument as many WordPress plugins seem to be passing `null` to capability check or admin menu functions. | |
| 326 | + | |
| 327 | + // Protect against other plugins not supplying a string for the `$cap` argument. | |
| 328 | + if ( ! is_string( $cap ) ) { // @phpstan-ignore function.alreadyNarrowedType (The `is_string()` check is needed as the input is coming from a filter hook.) | |
| 329 | + return $caps; | |
| 330 | + } | |
| 331 | + | |
| 300 | 332 | if ( ! in_array( $cap, array( 'tablepress_edit_table', 'tablepress_edit_table_id', 'tablepress_copy_table', 'tablepress_delete_table', 'tablepress_export_table', 'tablepress_preview_table' ), true ) ) { |
| 301 | 333 | return $caps; |
| 302 | 334 | } |
| 303 | 335 | |
| @@ -337,12 +369,12 @@ | ||
| 337 | 369 | * @since 1.0.0 |
| 338 | 370 | * |
| 339 | 371 | * @see map_meta_cap() |
| 340 | 372 | * |
| 341 | - * @param array $caps The user's current TablePress capabilities. | |
| 342 | - * @param string $cap Capability name. | |
| 343 | - * @param int $user_id The user ID. | |
| 344 | - * @param array $args Adds the context to the cap, typically the table ID. | |
| 373 | + * @param string[] $caps The user's current TablePress capabilities. | |
| 374 | + * @param string $cap Capability name. | |
| 375 | + * @param int $user_id The user ID. | |
| 376 | + * @param mixed[] $args Adds the context to the cap, typically the table ID. | |
| 345 | 377 | */ |
| 346 | 378 | return apply_filters( 'tablepress_map_meta_caps', $caps, $cap, $user_id, $args ); |
| 347 | 379 | } |
| 348 | 380 | |
| @@ -350,9 +382,9 @@ | ||
| 350 | 382 | * Delete the WP_Option and the user option of the model. |
| 351 | 383 | * |
| 352 | 384 | * @since 1.0.0 |
| 353 | 385 | */ |
| 354 | - public function destroy() { | |
| 386 | + public function destroy(): void { | |
| 355 | 387 | $this->plugin_options->delete(); |
| 356 | 388 | $this->user_options->delete_for_all_users(); |
| 357 | 389 | } |
| 358 | 390 | |