PluginProbe
TablePress – Tables in WordPress made easy / 3.2
TablePress – Tables in WordPress made easy v3.2
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 +81 -37 2.0.43.2 View file →
@@ -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 }
@@ -111,14 +112,17 @@
111 112 }
112 113
113 114 $plugin_options = $this->plugin_options->get();
114 115 $user_options = $this->user_options->get();
116 +
117 + $old_options = array_merge( $plugin_options, $user_options );
118 +
115 119 foreach ( $new_options as $name => $value ) {
116 120 if ( isset( $this->default_plugin_options[ $name ] ) ) {
117 121 $plugin_options[ $name ] = $value;
118 122 } elseif ( isset( $this->default_user_options[ $name ] ) ) {
119 123 $user_options[ $name ] = $value;
120 - } else {
124 + } else { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedElse
121 125 // No valid Plugin or User Option -> discard the name/value pair.
122 126 }
123 127 }
124 128
@@ -123,8 +127,17 @@
123 127 }
124 128
125 129 $this->plugin_options->update( $plugin_options );
126 130 $this->user_options->update( $user_options );
131 +
132 + /**
133 + * Fires after the options have been saved.
134 + *
135 + * @since 3.1.0
136 + *
137 + * @param array<string, mixed> $old_options Array of all options before the update.
138 + */
139 + do_action( 'tablepress_event_updated_options', $old_options );
127 140 }
128 141
129 142 /**
130 143 * Get the value of a single option, or an array with all options.
@@ -134,9 +147,9 @@
134 147 * @param string|false $name Optional. Name of a single option to get, or false for all options.
135 148 * @param mixed $default_value Optional. Default value, if the option $name does not exist.
136 149 * @return mixed Value of the retrieved option $name, or $default_value if it does not exist, or array of all options.
137 150 */
138 - public function get( $name = false, $default_value = null ) {
151 + public function get( /* string|false */ $name = false, /* string|bool|int|float|null */ $default_value = null ) /* : string|bool|int|float|array|null */ {
139 152 if ( false === $name ) {
140 153 return array_merge( $this->plugin_options->get(), $this->user_options->get() );
141 154 }
142 155
@@ -155,11 +168,11 @@
155 168 * Get all Plugin Options (only used in Debug).
156 169 *
157 170 * @since 1.0.0
158 171 *
159 - * @return array Array of all Plugin Options.
172 + * @return array<string, mixed> Array of all Plugin Options.
160 173 */
161 - public function _debug_get_plugin_options() {
174 + public function _debug_get_plugin_options(): array {
162 175 return $this->plugin_options->get();
163 176 }
164 177
165 178 /**
@@ -166,11 +179,11 @@
166 179 * Get all User Options (only used in Debug).
167 180 *
168 181 * @since 1.0.0
169 182 *
170 - * @return array Array of all User Options.
183 + * @return array<string, mixed> Array of all User Options.
171 184 */
172 - public function _debug_get_user_options() {
185 + public function _debug_get_user_options(): array {
173 186 return $this->user_options->get();
174 187 }
175 188
176 189 /**
@@ -178,9 +191,9 @@
178 191 * remove (no longer) existing options, e.g. after a plugin update.
179 192 *
180 193 * @since 1.0.0
181 194 */
182 - public function merge_plugin_options_defaults() {
195 + public function merge_plugin_options_defaults(): void {
183 196 $plugin_options = $this->plugin_options->get();
184 197 // Remove old (i.e. no longer existing) Plugin Options.
185 198 $plugin_options = array_intersect_key( $plugin_options, $this->default_plugin_options );
186 199 // Merge current into new Plugin Options.
@@ -194,9 +207,9 @@
194 207 * remove (no longer) existing options, e.g. after a plugin update.
195 208 *
196 209 * @since 1.0.0
197 210 */
198 - public function merge_user_options_defaults() {
211 + public function merge_user_options_defaults(): void {
199 212 $user_options = $this->user_options->get();
200 213 // Remove old (i.e. no longer existing) User Options.
201 214 $user_options = array_intersect_key( $user_options, $this->default_user_options );
202 215 // Merge current into new User Options.
@@ -205,13 +218,13 @@
205 218 $this->user_options->update( $user_options );
206 219 }
207 220
208 221 /**
209 - * Add default capabilities to "Administrator", "Editor", and "Author" user roles.
222 + * Adds the TablePress default capabilities to the "Administrator", "Editor", and "Author" user roles.
210 223 *
211 224 * @since 1.0.0
212 225 */
213 - public function add_access_capabilities() {
226 + public function add_access_capabilities(): void {
214 227 // Capabilities for all roles.
215 228 $roles = array( 'administrator', 'editor', 'author' );
216 229 foreach ( $roles as $role ) {
217 230 $role = get_role( $role );
@@ -238,9 +251,14 @@
238 251 // Capabilities for single roles.
239 252 $role = get_role( 'administrator' );
240 253 if ( ! empty( $role ) ) {
241 254 $role->add_cap( 'tablepress_edit_options' );
255 + $role->add_cap( 'tablepress_import_tables_url' );
242 256 }
257 + $role = get_role( 'editor' );
258 + if ( ! empty( $role ) ) {
259 + $role->add_cap( 'tablepress_import_tables_url' );
260 + }
243 261
244 262 // Refresh current set of capabilities of the user, to be able to directly use the new caps.
245 263 $user = wp_get_current_user();
246 264 $user->get_role_caps();
@@ -246,8 +264,27 @@
246 264 $user->get_role_caps();
247 265 }
248 266
249 267 /**
268 + * Adds a custom "Import from URL" access capability to the "Editor" and "Administrator" user roles.
269 + *
270 + * @since 2.3.2
271 + */
272 + public function add_access_capabilities_tp232(): void {
273 + $roles = array( 'administrator', 'editor' );
274 + foreach ( $roles as $role ) {
275 + $role = get_role( $role );
276 + if ( ! empty( $role ) ) {
277 + $role->add_cap( 'tablepress_import_tables_url' );
278 + }
279 + }
280 +
281 + // Refresh current set of capabilities of the user, to be able to directly use the new caps.
282 + $user = wp_get_current_user();
283 + $user->get_role_caps();
284 + }
285 +
286 + /**
250 287 * Remove all TablePress capabilities from all roles.
251 288 *
252 289 * @since 1.1.0
253 290 *
@@ -253,13 +290,13 @@
253 290 *
254 291 * @global WP_Roles $wp_roles WordPress User Roles abstraction object.
255 292 * @see add_access_capabilities()
256 293 */
257 - public function remove_access_capabilities() {
294 + public function remove_access_capabilities(): void {
258 295 global $wp_roles;
259 296
260 297 if ( ! isset( $wp_roles ) ) {
261 - $wp_roles = new WP_Roles();
298 + $wp_roles = new WP_Roles(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
262 299 }
263 300
264 301 foreach ( $wp_roles->roles as $role => $details ) {
265 302 $role = $wp_roles->get_role( $role );
@@ -289,15 +326,22 @@
289 326 * Map TablePress meta capabilities to primitive capabilities.
290 327 *
291 328 * @since 1.0.0
292 329 *
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.
330 + * @param string[] $caps Current set of primitive caps.
331 + * @param string $cap Meta cap that is to be checked/mapped.
332 + * @param int $user_id User ID for which meta cap is to be checked.
333 + * @param mixed[] $args Arguments for the check, here e.g. the table ID.
334 + * @return string[] Modified set of primitive caps.
298 335 */
299 - public function map_tablepress_meta_caps( array $caps, $cap, $user_id, array $args ) {
336 + public function map_tablepress_meta_caps( array $caps, /* string */ $cap, int $user_id, array $args ): array {
337 + // 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.
338 +
339 + // Protect against other plugins not supplying a string for the `$cap` argument.
340 + if ( ! is_string( $cap ) ) { // @phpstan-ignore function.alreadyNarrowedType (The `is_string()` check is needed as the input is coming from a filter hook.)
341 + return $caps;
342 + }
343 +
300 344 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 345 return $caps;
302 346 }
303 347
@@ -337,12 +381,12 @@
337 381 * @since 1.0.0
338 382 *
339 383 * @see map_meta_cap()
340 384 *
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.
385 + * @param string[] $caps The user's current TablePress capabilities.
386 + * @param string $cap Capability name.
387 + * @param int $user_id The user ID.
388 + * @param mixed[] $args Adds the context to the cap, typically the table ID.
345 389 */
346 390 return apply_filters( 'tablepress_map_meta_caps', $caps, $cap, $user_id, $args );
347 391 }
348 392
@@ -350,9 +394,9 @@
350 394 * Delete the WP_Option and the user option of the model.
351 395 *
352 396 * @since 1.0.0
353 397 */
354 - public function destroy() {
398 + public function destroy(): void {
355 399 $this->plugin_options->delete();
356 400 $this->user_options->delete_for_all_users();
357 401 }
358 402