PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
← All changes | models/model-options.php +93 -42 1.9.2 → 3.4 View file →
@@ -7,13 +7,16 @@
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 /**
15 17 * Options Model class
18 + *
16 19 * @package TablePress
17 20 * @subpackage Models
18 21 * @author Tobias Bäthge
19 22 * @since 1.0.0
@@ -23,11 +26,12 @@
23 26 /**
24 27 * Default Plugin Options.
25 28 *
26 29 * @since 1.0.0
27 - * @var array
30 + * @since 2.0.0 Added the "modules" option.
31 + * @var array<string, mixed>
28 32 */
29 - protected $default_plugin_options = array(
33 + protected array $default_plugin_options = array(
30 34 'plugin_options_db_version' => 0,
31 35 'table_scheme_db_version' => 0,
32 36 'prev_tablepress_version' => '0',
33 37 'tablepress_version' => TablePress::version,
@@ -38,8 +42,9 @@
38 42 'use_custom_css_file' => true,
39 43 'custom_css' => '',
40 44 'custom_css_minified' => '',
41 45 'custom_css_version' => 0,
46 + 'modules' => false,
42 47 );
43 48
44 49 /**
45 50 * Default User Options.
@@ -44,14 +49,17 @@
44 49 /**
45 50 * Default User Options.
46 51 *
47 52 * @since 1.0.0
48 - * @var array
53 + * @var array<string, mixed>
49 54 */
50 - protected $default_user_options = array(
51 - 'user_options_db_version' => TablePress::db_version, // to prevent saving on first load
52 - 'admin_menu_parent_page' => 'middle',
53 - 'message_first_visit' => true,
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.
54 62 );
55 63
56 64 /**
57 65 * Instance of WP_Option class for Plugin Options.
@@ -56,19 +64,17 @@
56 64 /**
57 65 * Instance of WP_Option class for Plugin Options.
58 66 *
59 67 * @since 1.0.0
60 - * @var TablePress_WP_Option
61 68 */
62 - protected $plugin_options;
69 + protected \TablePress_WP_Option $plugin_options;
63 70
64 71 /**
65 72 * Instance of WP_User_Option class for User Options.
66 73 *
67 74 * @since 1.0.0
68 - * @var TablePress_WP_User_Option
69 75 */
70 - protected $user_options;
76 + protected \TablePress_WP_User_Option $user_options;
71 77
72 78 /**
73 79 * Init Options Model by creating the object instances for the Plugin and User Options.
74 80 *
@@ -80,8 +86,11 @@
80 86 $params = array(
81 87 'option_name' => 'tablepress_plugin_options',
82 88 'default_value' => $this->default_plugin_options,
83 89 );
90 + if ( strtotime( '2026-10-10' ) >= strtotime( 'today' ) ) {
91 + $params['default_value']['message_plugin_update'] = true;
92 + }
84 93 $this->plugin_options = TablePress::load_class( 'TablePress_WP_Option', 'class-wp_option.php', 'classes', $params );
85 94
86 95 $params = array(
87 96 'option_name' => 'tablepress_user_options',
@@ -97,12 +106,12 @@
97 106 * Update a single option or an array of options with new values.
98 107 *
99 108 * @since 1.0.0
100 109 *
101 - * @param array|string $new_options Array of new options (name => value) or name of a single option.
102 - * @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).
103 112 */
104 - 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 {
105 114 // Allow saving of single options that are not in an array.
106 115 if ( ! is_array( $new_options ) ) {
107 116 $new_options = array( $new_options => $single_value );
108 117 }
@@ -108,14 +117,17 @@
108 117 }
109 118
110 119 $plugin_options = $this->plugin_options->get();
111 120 $user_options = $this->user_options->get();
121 +
122 + $old_options = array_merge( $plugin_options, $user_options );
123 +
112 124 foreach ( $new_options as $name => $value ) {
113 125 if ( isset( $this->default_plugin_options[ $name ] ) ) {
114 126 $plugin_options[ $name ] = $value;
115 127 } elseif ( isset( $this->default_user_options[ $name ] ) ) {
116 128 $user_options[ $name ] = $value;
117 - } else {
129 + } else { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedElse
118 130 // No valid Plugin or User Option -> discard the name/value pair.
119 131 }
120 132 }
121 133
@@ -120,8 +132,17 @@
120 132 }
121 133
122 134 $this->plugin_options->update( $plugin_options );
123 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 );
124 145 }
125 146
126 147 /**
127 148 * Get the value of a single option, or an array with all options.
@@ -127,13 +148,13 @@
127 148 * Get the value of a single option, or an array with all options.
128 149 *
129 150 * @since 1.0.0
130 151 *
131 - * @param string|bool $name Optional. Name of a single option to get, or false for all options.
132 - * @param mixed $default_value Optional. Default value, if the option $name does not exist.
152 + * @param string|false $name Optional. Name of a single option to get, or false for all options.
153 + * @param mixed $default_value Optional. Default value, if the option $name does not exist.
133 154 * @return mixed Value of the retrieved option $name, or $default_value if it does not exist, or array of all options.
134 155 */
135 - 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 */ {
136 157 if ( false === $name ) {
137 158 return array_merge( $this->plugin_options->get(), $this->user_options->get() );
138 159 }
139 160
@@ -152,11 +173,11 @@
152 173 * Get all Plugin Options (only used in Debug).
153 174 *
154 175 * @since 1.0.0
155 176 *
156 - * @return array Array of all Plugin Options.
177 + * @return array<string, mixed> Array of all Plugin Options.
157 178 */
158 - public function _debug_get_plugin_options() {
179 + public function _debug_get_plugin_options(): array {
159 180 return $this->plugin_options->get();
160 181 }
161 182
162 183 /**
@@ -163,11 +184,11 @@
163 184 * Get all User Options (only used in Debug).
164 185 *
165 186 * @since 1.0.0
166 187 *
167 - * @return array Array of all User Options.
188 + * @return array<string, mixed> Array of all User Options.
168 189 */
169 - public function _debug_get_user_options() {
190 + public function _debug_get_user_options(): array {
170 191 return $this->user_options->get();
171 192 }
172 193
173 194 /**
@@ -175,9 +196,9 @@
175 196 * remove (no longer) existing options, e.g. after a plugin update.
176 197 *
177 198 * @since 1.0.0
178 199 */
179 - public function merge_plugin_options_defaults() {
200 + public function merge_plugin_options_defaults(): void {
180 201 $plugin_options = $this->plugin_options->get();
181 202 // Remove old (i.e. no longer existing) Plugin Options.
182 203 $plugin_options = array_intersect_key( $plugin_options, $this->default_plugin_options );
183 204 // Merge current into new Plugin Options.
@@ -191,9 +212,9 @@
191 212 * remove (no longer) existing options, e.g. after a plugin update.
192 213 *
193 214 * @since 1.0.0
194 215 */
195 - public function merge_user_options_defaults() {
216 + public function merge_user_options_defaults(): void {
196 217 $user_options = $this->user_options->get();
197 218 // Remove old (i.e. no longer existing) User Options.
198 219 $user_options = array_intersect_key( $user_options, $this->default_user_options );
199 220 // Merge current into new User Options.
@@ -202,13 +223,13 @@
202 223 $this->user_options->update( $user_options );
203 224 }
204 225
205 226 /**
206 - * Add default capabilities to "Administrator", "Editor", and "Author" user roles.
227 + * Adds the TablePress default capabilities to the "Administrator", "Editor", and "Author" user roles.
207 228 *
208 229 * @since 1.0.0
209 230 */
210 - public function add_access_capabilities() {
231 + public function add_access_capabilities(): void {
211 232 // Capabilities for all roles.
212 233 $roles = array( 'administrator', 'editor', 'author' );
213 234 foreach ( $roles as $role ) {
214 235 $role = get_role( $role );
@@ -234,11 +255,15 @@
234 255
235 256 // Capabilities for single roles.
236 257 $role = get_role( 'administrator' );
237 258 if ( ! empty( $role ) ) {
238 - $role->add_cap( 'tablepress_import_tables_wptr' );
239 259 $role->add_cap( 'tablepress_edit_options' );
260 + $role->add_cap( 'tablepress_import_tables_url' );
240 261 }
262 + $role = get_role( 'editor' );
263 + if ( ! empty( $role ) ) {
264 + $role->add_cap( 'tablepress_import_tables_url' );
265 + }
241 266
242 267 // Refresh current set of capabilities of the user, to be able to directly use the new caps.
243 268 $user = wp_get_current_user();
244 269 $user->get_role_caps();
@@ -244,8 +269,27 @@
244 269 $user->get_role_caps();
245 270 }
246 271
247 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 + /**
248 292 * Remove all TablePress capabilities from all roles.
249 293 *
250 294 * @since 1.1.0
251 295 *
@@ -251,13 +295,13 @@
251 295 *
252 296 * @global WP_Roles $wp_roles WordPress User Roles abstraction object.
253 297 * @see add_access_capabilities()
254 298 */
255 - public function remove_access_capabilities() {
299 + public function remove_access_capabilities(): void {
256 300 global $wp_roles;
257 301
258 302 if ( ! isset( $wp_roles ) ) {
259 - $wp_roles = new WP_Roles();
303 + $wp_roles = new WP_Roles(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
260 304 }
261 305
262 306 foreach ( $wp_roles->roles as $role => $details ) {
263 307 $role = $wp_roles->get_role( $role );
@@ -273,9 +317,9 @@
273 317 $role->remove_cap( 'tablepress_import_tables' );
274 318 $role->remove_cap( 'tablepress_export_tables' );
275 319 $role->remove_cap( 'tablepress_access_options_screen' );
276 320 $role->remove_cap( 'tablepress_access_about_screen' );
277 - $role->remove_cap( 'tablepress_import_tables_wptr' );
321 + $role->remove_cap( 'tablepress_import_tables_wptr' ); // No longer used since 1.10, but might still be in the database.
278 322 $role->remove_cap( 'tablepress_edit_options' );
279 323 }
280 324
281 325 // Refresh current set of capabilities of the user, to be able to directly use the new caps.
@@ -287,15 +331,22 @@
287 331 * Map TablePress meta capabilities to primitive capabilities.
288 332 *
289 333 * @since 1.0.0
290 334 *
291 - * @param array $caps Current set of primitive caps.
292 - * @param string $cap Meta cap that is to be checked/mapped.
293 - * @param int $user_id User ID for which meta cap is to be checked.
294 - * @param array $args Arguments for the check, here e.g. the table ID.
295 - * @return array $caps 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.
296 340 */
297 - 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 +
298 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 ) ) {
299 350 return $caps;
300 351 }
301 352
@@ -329,18 +380,18 @@
329 380 break;
330 381 }
331 382
332 383 /**
333 - * Filter a user's TablePress capabilities.
384 + * Filters a user's TablePress capabilities.
334 385 *
335 386 * @since 1.0.0
336 387 *
337 388 * @see map_meta_cap()
338 389 *
339 - * @param array $caps The user's current TablePress capabilities.
340 - * @param string $cap Capability name.
341 - * @param int $user_id The user ID.
342 - * @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.
343 394 */
344 395 return apply_filters( 'tablepress_map_meta_caps', $caps, $cap, $user_id, $args );
345 396 }
346 397
@@ -348,9 +399,9 @@
348 399 * Delete the WP_Option and the user option of the model.
349 400 *
350 401 * @since 1.0.0
351 402 */
352 - public function destroy() {
403 + public function destroy(): void {
353 404 $this->plugin_options->delete();
354 405 $this->user_options->delete_for_all_users();
355 406 }
356 407