PluginProbe
TablePress – Tables in WordPress made easy / 3.0
TablePress – Tables in WordPress made easy v3.0
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 2.4.4 All 44 releases
← All changes | models/model-options.php +76 -41 1.123.0 View file →
@@ -12,8 +12,9 @@
12 12 defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );
13 13
14 14 /**
15 15 * Options Model class
16 + *
16 17 * @package TablePress
17 18 * @subpackage Models
18 19 * @author Tobias Bäthge
19 20 * @since 1.0.0
@@ -23,11 +24,12 @@
23 24 /**
24 25 * Default Plugin Options.
25 26 *
26 27 * @since 1.0.0
27 - * @var array
28 + * @since 2.0.0 Added the "modules" option.
29 + * @var array<string, mixed>
28 30 */
29 - protected $default_plugin_options = array(
31 + protected array $default_plugin_options = array(
30 32 'plugin_options_db_version' => 0,
31 33 'table_scheme_db_version' => 0,
32 34 'prev_tablepress_version' => '0',
33 35 'tablepress_version' => TablePress::version,
@@ -38,8 +40,9 @@
38 40 'use_custom_css_file' => true,
39 41 'custom_css' => '',
40 42 'custom_css_minified' => '',
41 43 'custom_css_version' => 0,
44 + 'modules' => false,
42 45 );
43 46
44 47 /**
45 48 * Default User Options.
@@ -44,14 +47,17 @@
44 47 /**
45 48 * Default User Options.
46 49 *
47 50 * @since 1.0.0
48 - * @var array
51 + * @var array<string, mixed>
49 52 */
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,
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.
54 60 );
55 61
56 62 /**
57 63 * Instance of WP_Option class for Plugin Options.
@@ -56,19 +62,17 @@
56 62 /**
57 63 * Instance of WP_Option class for Plugin Options.
58 64 *
59 65 * @since 1.0.0
60 - * @var TablePress_WP_Option
61 66 */
62 - protected $plugin_options;
67 + protected \TablePress_WP_Option $plugin_options;
63 68
64 69 /**
65 70 * Instance of WP_User_Option class for User Options.
66 71 *
67 72 * @since 1.0.0
68 - * @var TablePress_WP_User_Option
69 73 */
70 - protected $user_options;
74 + protected \TablePress_WP_User_Option $user_options;
71 75
72 76 /**
73 77 * Init Options Model by creating the object instances for the Plugin and User Options.
74 78 *
@@ -97,12 +101,12 @@
97 101 * Update a single option or an array of options with new values.
98 102 *
99 103 * @since 1.0.0
100 104 *
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).
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).
103 107 */
104 - 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 {
105 109 // Allow saving of single options that are not in an array.
106 110 if ( ! is_array( $new_options ) ) {
107 111 $new_options = array( $new_options => $single_value );
108 112 }
@@ -113,9 +117,9 @@
113 117 if ( isset( $this->default_plugin_options[ $name ] ) ) {
114 118 $plugin_options[ $name ] = $value;
115 119 } elseif ( isset( $this->default_user_options[ $name ] ) ) {
116 120 $user_options[ $name ] = $value;
117 - } else {
121 + } else { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedElse
118 122 // No valid Plugin or User Option -> discard the name/value pair.
119 123 }
120 124 }
121 125
@@ -127,13 +131,13 @@
127 131 * Get the value of a single option, or an array with all options.
128 132 *
129 133 * @since 1.0.0
130 134 *
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.
135 + * @param string|false $name Optional. Name of a single option to get, or false for all options.
136 + * @param mixed $default_value Optional. Default value, if the option $name does not exist.
133 137 * @return mixed Value of the retrieved option $name, or $default_value if it does not exist, or array of all options.
134 138 */
135 - 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 */ {
136 140 if ( false === $name ) {
137 141 return array_merge( $this->plugin_options->get(), $this->user_options->get() );
138 142 }
139 143
@@ -152,11 +156,11 @@
152 156 * Get all Plugin Options (only used in Debug).
153 157 *
154 158 * @since 1.0.0
155 159 *
156 - * @return array Array of all Plugin Options.
160 + * @return array<string, mixed> Array of all Plugin Options.
157 161 */
158 - public function _debug_get_plugin_options() {
162 + public function _debug_get_plugin_options(): array {
159 163 return $this->plugin_options->get();
160 164 }
161 165
162 166 /**
@@ -163,11 +167,11 @@
163 167 * Get all User Options (only used in Debug).
164 168 *
165 169 * @since 1.0.0
166 170 *
167 - * @return array Array of all User Options.
171 + * @return array<string, mixed> Array of all User Options.
168 172 */
169 - public function _debug_get_user_options() {
173 + public function _debug_get_user_options(): array {
170 174 return $this->user_options->get();
171 175 }
172 176
173 177 /**
@@ -175,9 +179,9 @@
175 179 * remove (no longer) existing options, e.g. after a plugin update.
176 180 *
177 181 * @since 1.0.0
178 182 */
179 - public function merge_plugin_options_defaults() {
183 + public function merge_plugin_options_defaults(): void {
180 184 $plugin_options = $this->plugin_options->get();
181 185 // Remove old (i.e. no longer existing) Plugin Options.
182 186 $plugin_options = array_intersect_key( $plugin_options, $this->default_plugin_options );
183 187 // Merge current into new Plugin Options.
@@ -191,9 +195,9 @@
191 195 * remove (no longer) existing options, e.g. after a plugin update.
192 196 *
193 197 * @since 1.0.0
194 198 */
195 - public function merge_user_options_defaults() {
199 + public function merge_user_options_defaults(): void {
196 200 $user_options = $this->user_options->get();
197 201 // Remove old (i.e. no longer existing) User Options.
198 202 $user_options = array_intersect_key( $user_options, $this->default_user_options );
199 203 // Merge current into new User Options.
@@ -202,13 +206,13 @@
202 206 $this->user_options->update( $user_options );
203 207 }
204 208
205 209 /**
206 - * Add default capabilities to "Administrator", "Editor", and "Author" user roles.
210 + * Adds the TablePress default capabilities to the "Administrator", "Editor", and "Author" user roles.
207 211 *
208 212 * @since 1.0.0
209 213 */
210 - public function add_access_capabilities() {
214 + public function add_access_capabilities(): void {
211 215 // Capabilities for all roles.
212 216 $roles = array( 'administrator', 'editor', 'author' );
213 217 foreach ( $roles as $role ) {
214 218 $role = get_role( $role );
@@ -235,9 +239,14 @@
235 239 // Capabilities for single roles.
236 240 $role = get_role( 'administrator' );
237 241 if ( ! empty( $role ) ) {
238 242 $role->add_cap( 'tablepress_edit_options' );
243 + $role->add_cap( 'tablepress_import_tables_url' );
239 244 }
245 + $role = get_role( 'editor' );
246 + if ( ! empty( $role ) ) {
247 + $role->add_cap( 'tablepress_import_tables_url' );
248 + }
240 249
241 250 // Refresh current set of capabilities of the user, to be able to directly use the new caps.
242 251 $user = wp_get_current_user();
243 252 $user->get_role_caps();
@@ -243,8 +252,27 @@
243 252 $user->get_role_caps();
244 253 }
245 254
246 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 + /**
247 275 * Remove all TablePress capabilities from all roles.
248 276 *
249 277 * @since 1.1.0
250 278 *
@@ -250,13 +278,13 @@
250 278 *
251 279 * @global WP_Roles $wp_roles WordPress User Roles abstraction object.
252 280 * @see add_access_capabilities()
253 281 */
254 - public function remove_access_capabilities() {
282 + public function remove_access_capabilities(): void {
255 283 global $wp_roles;
256 284
257 285 if ( ! isset( $wp_roles ) ) {
258 - $wp_roles = new WP_Roles();
286 + $wp_roles = new WP_Roles(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
259 287 }
260 288
261 289 foreach ( $wp_roles->roles as $role => $details ) {
262 290 $role = $wp_roles->get_role( $role );
@@ -272,9 +300,9 @@
272 300 $role->remove_cap( 'tablepress_import_tables' );
273 301 $role->remove_cap( 'tablepress_export_tables' );
274 302 $role->remove_cap( 'tablepress_access_options_screen' );
275 303 $role->remove_cap( 'tablepress_access_about_screen' );
276 - $role->remove_cap( 'tablepress_import_tables_wptr' ); // No longer used since 1.10, but might still be in the database.
304 + $role->remove_cap( 'tablepress_import_tables_wptr' ); // No longer used since 1.10, but might still be in the database.
277 305 $role->remove_cap( 'tablepress_edit_options' );
278 306 }
279 307
280 308 // Refresh current set of capabilities of the user, to be able to directly use the new caps.
@@ -286,15 +314,22 @@
286 314 * Map TablePress meta capabilities to primitive capabilities.
287 315 *
288 316 * @since 1.0.0
289 317 *
290 - * @param array $caps Current set of primitive caps.
291 - * @param string $cap Meta cap that is to be checked/mapped.
292 - * @param int $user_id User ID for which meta cap is to be checked.
293 - * @param array $args Arguments for the check, here e.g. the table ID.
294 - * @return array $caps 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.
295 323 */
296 - 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 +
297 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 ) ) {
298 333 return $caps;
299 334 }
300 335
@@ -328,18 +363,18 @@
328 363 break;
329 364 }
330 365
331 366 /**
332 - * Filter a user's TablePress capabilities.
367 + * Filters a user's TablePress capabilities.
333 368 *
334 369 * @since 1.0.0
335 370 *
336 371 * @see map_meta_cap()
337 372 *
338 - * @param array $caps The user's current TablePress capabilities.
339 - * @param string $cap Capability name.
340 - * @param int $user_id The user ID.
341 - * @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.
342 377 */
343 378 return apply_filters( 'tablepress_map_meta_caps', $caps, $cap, $user_id, $args );
344 379 }
345 380
@@ -347,9 +382,9 @@
347 382 * Delete the WP_Option and the user option of the model.
348 383 *
349 384 * @since 1.0.0
350 385 */
351 - public function destroy() {
386 + public function destroy(): void {
352 387 $this->plugin_options->delete();
353 388 $this->user_options->delete_for_all_users();
354 389 }
355 390