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 +86 -37 2.0.4 → 3.4 View file →
@@ -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,14 +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,
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.
57 62 );
58 63
59 64 /**
60 65 * Instance of WP_Option class for Plugin Options.
@@ -59,19 +64,17 @@
59 64 /**
60 65 * Instance of WP_Option class for Plugin Options.
61 66 *
62 67 * @since 1.0.0
63 - * @var TablePress_WP_Option
64 68 */
65 - protected $plugin_options;
69 + protected \TablePress_WP_Option $plugin_options;
66 70
67 71 /**
68 72 * Instance of WP_User_Option class for User Options.
69 73 *
70 74 * @since 1.0.0
71 - * @var TablePress_WP_User_Option
72 75 */
73 - protected $user_options;
76 + protected \TablePress_WP_User_Option $user_options;
74 77
75 78 /**
76 79 * Init Options Model by creating the object instances for the Plugin and User Options.
77 80 *
@@ -83,8 +86,11 @@
83 86 $params = array(
84 87 'option_name' => 'tablepress_plugin_options',
85 88 'default_value' => $this->default_plugin_options,
86 89 );
90 + if ( strtotime( '2026-10-10' ) >= strtotime( 'today' ) ) {
91 + $params['default_value']['message_plugin_update'] = true;
92 + }
87 93 $this->plugin_options = TablePress::load_class( 'TablePress_WP_Option', 'class-wp_option.php', 'classes', $params );
88 94
89 95 $params = array(
90 96 'option_name' => 'tablepress_user_options',
@@ -100,12 +106,12 @@
100 106 * Update a single option or an array of options with new values.
101 107 *
102 108 * @since 1.0.0
103 109 *
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).
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).
106 112 */
107 - 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 {
108 114 // Allow saving of single options that are not in an array.
109 115 if ( ! is_array( $new_options ) ) {
110 116 $new_options = array( $new_options => $single_value );
111 117 }
@@ -111,14 +117,17 @@
111 117 }
112 118
113 119 $plugin_options = $this->plugin_options->get();
114 120 $user_options = $this->user_options->get();
121 +
122 + $old_options = array_merge( $plugin_options, $user_options );
123 +
115 124 foreach ( $new_options as $name => $value ) {
116 125 if ( isset( $this->default_plugin_options[ $name ] ) ) {
117 126 $plugin_options[ $name ] = $value;
118 127 } elseif ( isset( $this->default_user_options[ $name ] ) ) {
119 128 $user_options[ $name ] = $value;
120 - } else {
129 + } else { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedElse
121 130 // No valid Plugin or User Option -> discard the name/value pair.
122 131 }
123 132 }
124 133
@@ -123,8 +132,17 @@
123 132 }
124 133
125 134 $this->plugin_options->update( $plugin_options );
126 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 );
127 145 }
128 146
129 147 /**
130 148 * Get the value of a single option, or an array with all options.
@@ -134,9 +152,9 @@
134 152 * @param string|false $name Optional. Name of a single option to get, or false for all options.
135 153 * @param mixed $default_value Optional. Default value, if the option $name does not exist.
136 154 * @return mixed Value of the retrieved option $name, or $default_value if it does not exist, or array of all options.
137 155 */
138 - 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 */ {
139 157 if ( false === $name ) {
140 158 return array_merge( $this->plugin_options->get(), $this->user_options->get() );
141 159 }
142 160
@@ -155,11 +173,11 @@
155 173 * Get all Plugin Options (only used in Debug).
156 174 *
157 175 * @since 1.0.0
158 176 *
159 - * @return array Array of all Plugin Options.
177 + * @return array<string, mixed> Array of all Plugin Options.
160 178 */
161 - public function _debug_get_plugin_options() {
179 + public function _debug_get_plugin_options(): array {
162 180 return $this->plugin_options->get();
163 181 }
164 182
165 183 /**
@@ -166,11 +184,11 @@
166 184 * Get all User Options (only used in Debug).
167 185 *
168 186 * @since 1.0.0
169 187 *
170 - * @return array Array of all User Options.
188 + * @return array<string, mixed> Array of all User Options.
171 189 */
172 - public function _debug_get_user_options() {
190 + public function _debug_get_user_options(): array {
173 191 return $this->user_options->get();
174 192 }
175 193
176 194 /**
@@ -178,9 +196,9 @@
178 196 * remove (no longer) existing options, e.g. after a plugin update.
179 197 *
180 198 * @since 1.0.0
181 199 */
182 - public function merge_plugin_options_defaults() {
200 + public function merge_plugin_options_defaults(): void {
183 201 $plugin_options = $this->plugin_options->get();
184 202 // Remove old (i.e. no longer existing) Plugin Options.
185 203 $plugin_options = array_intersect_key( $plugin_options, $this->default_plugin_options );
186 204 // Merge current into new Plugin Options.
@@ -194,9 +212,9 @@
194 212 * remove (no longer) existing options, e.g. after a plugin update.
195 213 *
196 214 * @since 1.0.0
197 215 */
198 - public function merge_user_options_defaults() {
216 + public function merge_user_options_defaults(): void {
199 217 $user_options = $this->user_options->get();
200 218 // Remove old (i.e. no longer existing) User Options.
201 219 $user_options = array_intersect_key( $user_options, $this->default_user_options );
202 220 // Merge current into new User Options.
@@ -205,13 +223,13 @@
205 223 $this->user_options->update( $user_options );
206 224 }
207 225
208 226 /**
209 - * Add default capabilities to "Administrator", "Editor", and "Author" user roles.
227 + * Adds the TablePress default capabilities to the "Administrator", "Editor", and "Author" user roles.
210 228 *
211 229 * @since 1.0.0
212 230 */
213 - public function add_access_capabilities() {
231 + public function add_access_capabilities(): void {
214 232 // Capabilities for all roles.
215 233 $roles = array( 'administrator', 'editor', 'author' );
216 234 foreach ( $roles as $role ) {
217 235 $role = get_role( $role );
@@ -238,9 +256,14 @@
238 256 // Capabilities for single roles.
239 257 $role = get_role( 'administrator' );
240 258 if ( ! empty( $role ) ) {
241 259 $role->add_cap( 'tablepress_edit_options' );
260 + $role->add_cap( 'tablepress_import_tables_url' );
242 261 }
262 + $role = get_role( 'editor' );
263 + if ( ! empty( $role ) ) {
264 + $role->add_cap( 'tablepress_import_tables_url' );
265 + }
243 266
244 267 // Refresh current set of capabilities of the user, to be able to directly use the new caps.
245 268 $user = wp_get_current_user();
246 269 $user->get_role_caps();
@@ -246,8 +269,27 @@
246 269 $user->get_role_caps();
247 270 }
248 271
249 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 + /**
250 292 * Remove all TablePress capabilities from all roles.
251 293 *
252 294 * @since 1.1.0
253 295 *
@@ -253,13 +295,13 @@
253 295 *
254 296 * @global WP_Roles $wp_roles WordPress User Roles abstraction object.
255 297 * @see add_access_capabilities()
256 298 */
257 - public function remove_access_capabilities() {
299 + public function remove_access_capabilities(): void {
258 300 global $wp_roles;
259 301
260 302 if ( ! isset( $wp_roles ) ) {
261 - $wp_roles = new WP_Roles();
303 + $wp_roles = new WP_Roles(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
262 304 }
263 305
264 306 foreach ( $wp_roles->roles as $role => $details ) {
265 307 $role = $wp_roles->get_role( $role );
@@ -289,15 +331,22 @@
289 331 * Map TablePress meta capabilities to primitive capabilities.
290 332 *
291 333 * @since 1.0.0
292 334 *
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.
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.
298 340 */
299 - 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 +
300 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 ) ) {
301 350 return $caps;
302 351 }
303 352
@@ -337,12 +386,12 @@
337 386 * @since 1.0.0
338 387 *
339 388 * @see map_meta_cap()
340 389 *
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.
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.
345 394 */
346 395 return apply_filters( 'tablepress_map_meta_caps', $caps, $cap, $user_id, $args );
347 396 }
348 397
@@ -350,9 +399,9 @@
350 399 * Delete the WP_Option and the user option of the model.
351 400 *
352 401 * @since 1.0.0
353 402 */
354 - public function destroy() {
403 + public function destroy(): void {
355 404 $this->plugin_options->delete();
356 405 $this->user_options->delete_for_all_users();
357 406 }
358 407