| @@ -7,8 +7,10 @@ | ||
| 7 | 7 | * @author Tobias Bäthge |
| 8 | 8 | * @since 1.0.0 |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | +declare(strict_types=1); | |
| 12 | + | |
| 11 | 13 | // Prohibit direct script loading. |
| 12 | 14 | defined( 'ABSPATH' ) || die( 'No direct script access allowed!' ); |
| 13 | 15 | |
| 14 | 16 | /** |
| @@ -25,11 +27,11 @@ | ||
| 25 | 27 | * Default Plugin Options. |
| 26 | 28 | * |
| 27 | 29 | * @since 1.0.0 |
| 28 | 30 | * @since 2.0.0 Added the "modules" option. |
| 29 | - * @var array | |
| 31 | + * @var array<string, mixed> | |
| 30 | 32 | */ |
| 31 | - protected $default_plugin_options = array( | |
| 33 | + protected array $default_plugin_options = array( | |
| 32 | 34 | 'plugin_options_db_version' => 0, |
| 33 | 35 | 'table_scheme_db_version' => 0, |
| 34 | 36 | 'prev_tablepress_version' => '0', |
| 35 | 37 | 'tablepress_version' => TablePress::version, |
| @@ -47,16 +49,17 @@ | ||
| 47 | 49 | /** |
| 48 | 50 | * Default User Options. |
| 49 | 51 | * |
| 50 | 52 | * @since 1.0.0 |
| 51 | - * @var array | |
| 53 | + * @var array<string, mixed> | |
| 52 | 54 | */ |
| 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, | |
| 57 | - 'table_editor_column_width' => '170', // Default column width of 170px of the table editor on the "Edit" screen. | |
| 58 | - 'table_editor_line_clamp' => '5', // Maximum number of lines per cell in the table editor on the "Edit" screen. | |
| 55 | + protected array $default_user_options = array( | |
| 56 | + 'user_options_db_version' => TablePress::db_version, // To prevent saving on first load. | |
| 57 | + 'admin_menu_parent_page' => 'middle', | |
| 58 | + 'message_first_visit' => true, | |
| 59 | + 'message_superseded_extensions' => true, | |
| 60 | + 'table_editor_column_width' => '170', // Default column width of 170px of the table editor on the "Edit" screen. | |
| 61 | + 'table_editor_line_clamp' => '5', // Maximum number of lines per cell in the table editor on the "Edit" screen. | |
| 59 | 62 | ); |
| 60 | 63 | |
| 61 | 64 | /** |
| 62 | 65 | * Instance of WP_Option class for Plugin Options. |
| @@ -61,19 +64,17 @@ | ||
| 61 | 64 | /** |
| 62 | 65 | * Instance of WP_Option class for Plugin Options. |
| 63 | 66 | * |
| 64 | 67 | * @since 1.0.0 |
| 65 | - * @var TablePress_WP_Option | |
| 66 | 68 | */ |
| 67 | - protected $plugin_options; | |
| 69 | + protected \TablePress_WP_Option $plugin_options; | |
| 68 | 70 | |
| 69 | 71 | /** |
| 70 | 72 | * Instance of WP_User_Option class for User Options. |
| 71 | 73 | * |
| 72 | 74 | * @since 1.0.0 |
| 73 | - * @var TablePress_WP_User_Option | |
| 74 | 75 | */ |
| 75 | - protected $user_options; | |
| 76 | + protected \TablePress_WP_User_Option $user_options; | |
| 76 | 77 | |
| 77 | 78 | /** |
| 78 | 79 | * Init Options Model by creating the object instances for the Plugin and User Options. |
| 79 | 80 | * |
| @@ -85,8 +86,11 @@ | ||
| 85 | 86 | $params = array( |
| 86 | 87 | 'option_name' => 'tablepress_plugin_options', |
| 87 | 88 | 'default_value' => $this->default_plugin_options, |
| 88 | 89 | ); |
| 90 | + if ( strtotime( '2026-10-10' ) >= strtotime( 'today' ) ) { | |
| 91 | + $params['default_value']['message_plugin_update'] = true; | |
| 92 | + } | |
| 89 | 93 | $this->plugin_options = TablePress::load_class( 'TablePress_WP_Option', 'class-wp_option.php', 'classes', $params ); |
| 90 | 94 | |
| 91 | 95 | $params = array( |
| 92 | 96 | 'option_name' => 'tablepress_user_options', |
| @@ -102,12 +106,12 @@ | ||
| 102 | 106 | * Update a single option or an array of options with new values. |
| 103 | 107 | * |
| 104 | 108 | * @since 1.0.0 |
| 105 | 109 | * |
| 106 | - * @param array|string $new_options Array of new options (name => value) or name of a single option. | |
| 107 | - * @param mixed $single_value Optional. New value for a single option (only if $new_options is not an array). | |
| 110 | + * @param array<string, mixed>|string $new_options Array of new options (name => value) or name of a single option. | |
| 111 | + * @param mixed $single_value Optional. New value for a single option (only if $new_options is not an array). | |
| 108 | 112 | */ |
| 109 | - public function update( $new_options, $single_value = null ) { | |
| 113 | + public function update( /* array|string */ $new_options, /* string|bool|int|float|null */ $single_value = null ): void { | |
| 110 | 114 | // Allow saving of single options that are not in an array. |
| 111 | 115 | if ( ! is_array( $new_options ) ) { |
| 112 | 116 | $new_options = array( $new_options => $single_value ); |
| 113 | 117 | } |
| @@ -113,14 +117,17 @@ | ||
| 113 | 117 | } |
| 114 | 118 | |
| 115 | 119 | $plugin_options = $this->plugin_options->get(); |
| 116 | 120 | $user_options = $this->user_options->get(); |
| 121 | + | |
| 122 | + $old_options = array_merge( $plugin_options, $user_options ); | |
| 123 | + | |
| 117 | 124 | foreach ( $new_options as $name => $value ) { |
| 118 | 125 | if ( isset( $this->default_plugin_options[ $name ] ) ) { |
| 119 | 126 | $plugin_options[ $name ] = $value; |
| 120 | 127 | } elseif ( isset( $this->default_user_options[ $name ] ) ) { |
| 121 | 128 | $user_options[ $name ] = $value; |
| 122 | - } else { | |
| 129 | + } else { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedElse | |
| 123 | 130 | // No valid Plugin or User Option -> discard the name/value pair. |
| 124 | 131 | } |
| 125 | 132 | } |
| 126 | 133 | |
| @@ -125,8 +132,17 @@ | ||
| 125 | 132 | } |
| 126 | 133 | |
| 127 | 134 | $this->plugin_options->update( $plugin_options ); |
| 128 | 135 | $this->user_options->update( $user_options ); |
| 136 | + | |
| 137 | + /** | |
| 138 | + * Fires after the options have been saved. | |
| 139 | + * | |
| 140 | + * @since 3.1.0 | |
| 141 | + * | |
| 142 | + * @param array<string, mixed> $old_options Array of all options before the update. | |
| 143 | + */ | |
| 144 | + do_action( 'tablepress_event_updated_options', $old_options ); | |
| 129 | 145 | } |
| 130 | 146 | |
| 131 | 147 | /** |
| 132 | 148 | * Get the value of a single option, or an array with all options. |
| @@ -136,9 +152,9 @@ | ||
| 136 | 152 | * @param string|false $name Optional. Name of a single option to get, or false for all options. |
| 137 | 153 | * @param mixed $default_value Optional. Default value, if the option $name does not exist. |
| 138 | 154 | * @return mixed Value of the retrieved option $name, or $default_value if it does not exist, or array of all options. |
| 139 | 155 | */ |
| 140 | - public function get( $name = false, $default_value = null ) { | |
| 156 | + public function get( /* string|false */ $name = false, /* string|bool|int|float|null */ $default_value = null ) /* : string|bool|int|float|array|null */ { | |
| 141 | 157 | if ( false === $name ) { |
| 142 | 158 | return array_merge( $this->plugin_options->get(), $this->user_options->get() ); |
| 143 | 159 | } |
| 144 | 160 | |
| @@ -157,11 +173,11 @@ | ||
| 157 | 173 | * Get all Plugin Options (only used in Debug). |
| 158 | 174 | * |
| 159 | 175 | * @since 1.0.0 |
| 160 | 176 | * |
| 161 | - * @return array Array of all Plugin Options. | |
| 177 | + * @return array<string, mixed> Array of all Plugin Options. | |
| 162 | 178 | */ |
| 163 | - public function _debug_get_plugin_options() { | |
| 179 | + public function _debug_get_plugin_options(): array { | |
| 164 | 180 | return $this->plugin_options->get(); |
| 165 | 181 | } |
| 166 | 182 | |
| 167 | 183 | /** |
| @@ -168,11 +184,11 @@ | ||
| 168 | 184 | * Get all User Options (only used in Debug). |
| 169 | 185 | * |
| 170 | 186 | * @since 1.0.0 |
| 171 | 187 | * |
| 172 | - * @return array Array of all User Options. | |
| 188 | + * @return array<string, mixed> Array of all User Options. | |
| 173 | 189 | */ |
| 174 | - public function _debug_get_user_options() { | |
| 190 | + public function _debug_get_user_options(): array { | |
| 175 | 191 | return $this->user_options->get(); |
| 176 | 192 | } |
| 177 | 193 | |
| 178 | 194 | /** |
| @@ -180,9 +196,9 @@ | ||
| 180 | 196 | * remove (no longer) existing options, e.g. after a plugin update. |
| 181 | 197 | * |
| 182 | 198 | * @since 1.0.0 |
| 183 | 199 | */ |
| 184 | - public function merge_plugin_options_defaults() { | |
| 200 | + public function merge_plugin_options_defaults(): void { | |
| 185 | 201 | $plugin_options = $this->plugin_options->get(); |
| 186 | 202 | // Remove old (i.e. no longer existing) Plugin Options. |
| 187 | 203 | $plugin_options = array_intersect_key( $plugin_options, $this->default_plugin_options ); |
| 188 | 204 | // Merge current into new Plugin Options. |
| @@ -196,9 +212,9 @@ | ||
| 196 | 212 | * remove (no longer) existing options, e.g. after a plugin update. |
| 197 | 213 | * |
| 198 | 214 | * @since 1.0.0 |
| 199 | 215 | */ |
| 200 | - public function merge_user_options_defaults() { | |
| 216 | + public function merge_user_options_defaults(): void { | |
| 201 | 217 | $user_options = $this->user_options->get(); |
| 202 | 218 | // Remove old (i.e. no longer existing) User Options. |
| 203 | 219 | $user_options = array_intersect_key( $user_options, $this->default_user_options ); |
| 204 | 220 | // Merge current into new User Options. |
| @@ -207,13 +223,13 @@ | ||
| 207 | 223 | $this->user_options->update( $user_options ); |
| 208 | 224 | } |
| 209 | 225 | |
| 210 | 226 | /** |
| 211 | - * Add default capabilities to "Administrator", "Editor", and "Author" user roles. | |
| 227 | + * Adds the TablePress default capabilities to the "Administrator", "Editor", and "Author" user roles. | |
| 212 | 228 | * |
| 213 | 229 | * @since 1.0.0 |
| 214 | 230 | */ |
| 215 | - public function add_access_capabilities() { | |
| 231 | + public function add_access_capabilities(): void { | |
| 216 | 232 | // Capabilities for all roles. |
| 217 | 233 | $roles = array( 'administrator', 'editor', 'author' ); |
| 218 | 234 | foreach ( $roles as $role ) { |
| 219 | 235 | $role = get_role( $role ); |
| @@ -240,9 +256,14 @@ | ||
| 240 | 256 | // Capabilities for single roles. |
| 241 | 257 | $role = get_role( 'administrator' ); |
| 242 | 258 | if ( ! empty( $role ) ) { |
| 243 | 259 | $role->add_cap( 'tablepress_edit_options' ); |
| 260 | + $role->add_cap( 'tablepress_import_tables_url' ); | |
| 244 | 261 | } |
| 262 | + $role = get_role( 'editor' ); | |
| 263 | + if ( ! empty( $role ) ) { | |
| 264 | + $role->add_cap( 'tablepress_import_tables_url' ); | |
| 265 | + } | |
| 245 | 266 | |
| 246 | 267 | // Refresh current set of capabilities of the user, to be able to directly use the new caps. |
| 247 | 268 | $user = wp_get_current_user(); |
| 248 | 269 | $user->get_role_caps(); |
| @@ -248,8 +269,27 @@ | ||
| 248 | 269 | $user->get_role_caps(); |
| 249 | 270 | } |
| 250 | 271 | |
| 251 | 272 | /** |
| 273 | + * Adds a custom "Import from URL" access capability to the "Editor" and "Administrator" user roles. | |
| 274 | + * | |
| 275 | + * @since 2.3.2 | |
| 276 | + */ | |
| 277 | + public function add_access_capabilities_tp232(): void { | |
| 278 | + $roles = array( 'administrator', 'editor' ); | |
| 279 | + foreach ( $roles as $role ) { | |
| 280 | + $role = get_role( $role ); | |
| 281 | + if ( ! empty( $role ) ) { | |
| 282 | + $role->add_cap( 'tablepress_import_tables_url' ); | |
| 283 | + } | |
| 284 | + } | |
| 285 | + | |
| 286 | + // Refresh current set of capabilities of the user, to be able to directly use the new caps. | |
| 287 | + $user = wp_get_current_user(); | |
| 288 | + $user->get_role_caps(); | |
| 289 | + } | |
| 290 | + | |
| 291 | + /** | |
| 252 | 292 | * Remove all TablePress capabilities from all roles. |
| 253 | 293 | * |
| 254 | 294 | * @since 1.1.0 |
| 255 | 295 | * |
| @@ -255,13 +295,13 @@ | ||
| 255 | 295 | * |
| 256 | 296 | * @global WP_Roles $wp_roles WordPress User Roles abstraction object. |
| 257 | 297 | * @see add_access_capabilities() |
| 258 | 298 | */ |
| 259 | - public function remove_access_capabilities() { | |
| 299 | + public function remove_access_capabilities(): void { | |
| 260 | 300 | global $wp_roles; |
| 261 | 301 | |
| 262 | 302 | if ( ! isset( $wp_roles ) ) { |
| 263 | - $wp_roles = new WP_Roles(); | |
| 303 | + $wp_roles = new WP_Roles(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited | |
| 264 | 304 | } |
| 265 | 305 | |
| 266 | 306 | foreach ( $wp_roles->roles as $role => $details ) { |
| 267 | 307 | $role = $wp_roles->get_role( $role ); |
| @@ -291,15 +331,22 @@ | ||
| 291 | 331 | * Map TablePress meta capabilities to primitive capabilities. |
| 292 | 332 | * |
| 293 | 333 | * @since 1.0.0 |
| 294 | 334 | * |
| 295 | - * @param array $caps Current set of primitive caps. | |
| 296 | - * @param string $cap Meta cap that is to be checked/mapped. | |
| 297 | - * @param int $user_id User ID for which meta cap is to be checked. | |
| 298 | - * @param array $args Arguments for the check, here e.g. the table ID. | |
| 299 | - * @return array Modified set of primitive caps. | |
| 335 | + * @param string[] $caps Current set of primitive caps. | |
| 336 | + * @param string $cap Meta cap that is to be checked/mapped. | |
| 337 | + * @param int $user_id User ID for which meta cap is to be checked. | |
| 338 | + * @param mixed[] $args Arguments for the check, here e.g. the table ID. | |
| 339 | + * @return string[] Modified set of primitive caps. | |
| 300 | 340 | */ |
| 301 | - public function map_tablepress_meta_caps( array $caps, $cap, $user_id, array $args ) { | |
| 341 | + public function map_tablepress_meta_caps( array $caps, /* string */ $cap, int $user_id, array $args ): array { | |
| 342 | + // 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. | |
| 343 | + | |
| 344 | + // Protect against other plugins not supplying a string for the `$cap` argument. | |
| 345 | + if ( ! is_string( $cap ) ) { // @phpstan-ignore function.alreadyNarrowedType (The `is_string()` check is needed as the input is coming from a filter hook.) | |
| 346 | + return $caps; | |
| 347 | + } | |
| 348 | + | |
| 302 | 349 | 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 ) ) { |
| 303 | 350 | return $caps; |
| 304 | 351 | } |
| 305 | 352 | |
| @@ -339,12 +386,12 @@ | ||
| 339 | 386 | * @since 1.0.0 |
| 340 | 387 | * |
| 341 | 388 | * @see map_meta_cap() |
| 342 | 389 | * |
| 343 | - * @param array $caps The user's current TablePress capabilities. | |
| 344 | - * @param string $cap Capability name. | |
| 345 | - * @param int $user_id The user ID. | |
| 346 | - * @param array $args Adds the context to the cap, typically the table ID. | |
| 390 | + * @param string[] $caps The user's current TablePress capabilities. | |
| 391 | + * @param string $cap Capability name. | |
| 392 | + * @param int $user_id The user ID. | |
| 393 | + * @param mixed[] $args Adds the context to the cap, typically the table ID. | |
| 347 | 394 | */ |
| 348 | 395 | return apply_filters( 'tablepress_map_meta_caps', $caps, $cap, $user_id, $args ); |
| 349 | 396 | } |
| 350 | 397 | |
| @@ -352,9 +399,9 @@ | ||
| 352 | 399 | * Delete the WP_Option and the user option of the model. |
| 353 | 400 | * |
| 354 | 401 | * @since 1.0.0 |
| 355 | 402 | */ |
| 356 | - public function destroy() { | |
| 403 | + public function destroy(): void { | |
| 357 | 404 | $this->plugin_options->delete(); |
| 358 | 405 | $this->user_options->delete_for_all_users(); |
| 359 | 406 | } |
| 360 | 407 | |