PluginProbe
TablePress – Tables in WordPress made easy / 2.3.2
TablePress – Tables in WordPress made easy v2.3.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
tablepress / models / model-options.php

model-options.php in TablePress – Tables in WordPress made easy 2.3.2, at models/model-options.php

393 lines 12.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Options Model
4 *
5 * @package TablePress
6 * @subpackage Models
7 * @author Tobias Bäthge
8 * @since 1.0.0
9 */
10
11 // Prohibit direct script loading.
12 defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );
13
14 /**
15 * Options Model class
16 *
17 * @package TablePress
18 * @subpackage Models
19 * @author Tobias Bäthge
20 * @since 1.0.0
21 */
22 class TablePress_Options_Model extends TablePress_Model {
23
24 /**
25 * Default Plugin Options.
26 *
27 * @since 1.0.0
28 * @since 2.0.0 Added the "modules" option.
29 * @var array<string, mixed>
30 */
31 protected $default_plugin_options = array(
32 'plugin_options_db_version' => 0,
33 'table_scheme_db_version' => 0,
34 'prev_tablepress_version' => '0',
35 'tablepress_version' => TablePress::version,
36 'first_activation' => 0,
37 'message_plugin_update' => false,
38 'message_donation_nag' => true,
39 'use_custom_css' => true,
40 'use_custom_css_file' => true,
41 'custom_css' => '',
42 'custom_css_minified' => '',
43 'custom_css_version' => 0,
44 'modules' => false,
45 );
46
47 /**
48 * Default User Options.
49 *
50 * @since 1.0.0
51 * @var array<string, mixed>>
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,
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.
59 );
60
61 /**
62 * Instance of WP_Option class for Plugin Options.
63 *
64 * @since 1.0.0
65 * @var TablePress_WP_Option
66 */
67 protected $plugin_options;
68
69 /**
70 * Instance of WP_User_Option class for User Options.
71 *
72 * @since 1.0.0
73 * @var TablePress_WP_User_Option
74 */
75 protected $user_options;
76
77 /**
78 * Init Options Model by creating the object instances for the Plugin and User Options.
79 *
80 * @since 1.0.0
81 */
82 public function __construct() {
83 parent::__construct();
84
85 $params = array(
86 'option_name' => 'tablepress_plugin_options',
87 'default_value' => $this->default_plugin_options,
88 );
89 $this->plugin_options = TablePress::load_class( 'TablePress_WP_Option', 'class-wp_option.php', 'classes', $params );
90
91 $params = array(
92 'option_name' => 'tablepress_user_options',
93 'default_value' => $this->default_user_options,
94 );
95 $this->user_options = TablePress::load_class( 'TablePress_WP_User_Option', 'class-wp_user_option.php', 'classes', $params );
96
97 // Filter to map Meta capabilities to Primitive Capabilities.
98 add_filter( 'map_meta_cap', array( $this, 'map_tablepress_meta_caps' ), 10, 4 );
99 }
100
101 /**
102 * Update a single option or an array of options with new values.
103 *
104 * @since 1.0.0
105 *
106 * @param array<string, mixed>|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).
108 */
109 public function update( /* array|string */ $new_options, /* string|bool|int|float|null */ $single_value = null ): void {
110 // Allow saving of single options that are not in an array.
111 if ( ! is_array( $new_options ) ) {
112 $new_options = array( $new_options => $single_value );
113 }
114
115 $plugin_options = $this->plugin_options->get();
116 $user_options = $this->user_options->get();
117 foreach ( $new_options as $name => $value ) {
118 if ( isset( $this->default_plugin_options[ $name ] ) ) {
119 $plugin_options[ $name ] = $value;
120 } elseif ( isset( $this->default_user_options[ $name ] ) ) {
121 $user_options[ $name ] = $value;
122 } else { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedElse
123 // No valid Plugin or User Option -> discard the name/value pair.
124 }
125 }
126
127 $this->plugin_options->update( $plugin_options );
128 $this->user_options->update( $user_options );
129 }
130
131 /**
132 * Get the value of a single option, or an array with all options.
133 *
134 * @since 1.0.0
135 *
136 * @param string|false $name Optional. Name of a single option to get, or false for all options.
137 * @param mixed $default_value Optional. Default value, if the option $name does not exist.
138 * @return mixed Value of the retrieved option $name, or $default_value if it does not exist, or array of all options.
139 */
140 public function get( /* string|false */ $name = false, /* string|bool|int|float|null */ $default_value = null ) /* : string|bool|int|float|array|null */ {
141 if ( false === $name ) {
142 return array_merge( $this->plugin_options->get(), $this->user_options->get() );
143 }
144
145 // Single Option wanted.
146 if ( $this->plugin_options->is_set( $name ) ) {
147 return $this->plugin_options->get( $name );
148 } elseif ( $this->user_options->is_set( $name ) ) {
149 return $this->user_options->get( $name );
150 } else {
151 // No valid Plugin or User Option.
152 return $default_value;
153 }
154 }
155
156 /**
157 * Get all Plugin Options (only used in Debug).
158 *
159 * @since 1.0.0
160 *
161 * @return array<string, mixed> Array of all Plugin Options.
162 */
163 public function _debug_get_plugin_options(): array {
164 return $this->plugin_options->get();
165 }
166
167 /**
168 * Get all User Options (only used in Debug).
169 *
170 * @since 1.0.0
171 *
172 * @return array<string, mixed> Array of all User Options.
173 */
174 public function _debug_get_user_options(): array {
175 return $this->user_options->get();
176 }
177
178 /**
179 * Merge existing Plugin Options with default Plugin Options,
180 * remove (no longer) existing options, e.g. after a plugin update.
181 *
182 * @since 1.0.0
183 */
184 public function merge_plugin_options_defaults(): void {
185 $plugin_options = $this->plugin_options->get();
186 // Remove old (i.e. no longer existing) Plugin Options.
187 $plugin_options = array_intersect_key( $plugin_options, $this->default_plugin_options );
188 // Merge current into new Plugin Options.
189 $plugin_options = array_merge( $this->default_plugin_options, $plugin_options );
190
191 $this->plugin_options->update( $plugin_options );
192 }
193
194 /**
195 * Merge existing User Options with default User Options,
196 * remove (no longer) existing options, e.g. after a plugin update.
197 *
198 * @since 1.0.0
199 */
200 public function merge_user_options_defaults(): void {
201 $user_options = $this->user_options->get();
202 // Remove old (i.e. no longer existing) User Options.
203 $user_options = array_intersect_key( $user_options, $this->default_user_options );
204 // Merge current into new User Options.
205 $user_options = array_merge( $this->default_user_options, $user_options );
206
207 $this->user_options->update( $user_options );
208 }
209
210 /**
211 * Adds the TablePress default capabilities to the "Administrator", "Editor", and "Author" user roles.
212 *
213 * @since 1.0.0
214 */
215 public function add_access_capabilities(): void {
216 // Capabilities for all roles.
217 $roles = array( 'administrator', 'editor', 'author' );
218 foreach ( $roles as $role ) {
219 $role = get_role( $role );
220 if ( empty( $role ) ) {
221 continue;
222 }
223
224 // From get_post_type_capabilities().
225 $role->add_cap( 'tablepress_edit_tables' );
226 // $role->add_cap( 'tablepress_edit_others_tables' );
227 $role->add_cap( 'tablepress_delete_tables' );
228 // $role->add_cap( 'tablepress_delete_others_tables' );
229
230 // Custom capabilities.
231 $role->add_cap( 'tablepress_list_tables' );
232 $role->add_cap( 'tablepress_add_tables' );
233 $role->add_cap( 'tablepress_copy_tables' );
234 $role->add_cap( 'tablepress_import_tables' );
235 $role->add_cap( 'tablepress_export_tables' );
236 $role->add_cap( 'tablepress_access_options_screen' );
237 $role->add_cap( 'tablepress_access_about_screen' );
238 }
239
240 // Capabilities for single roles.
241 $role = get_role( 'administrator' );
242 if ( ! empty( $role ) ) {
243 $role->add_cap( 'tablepress_edit_options' );
244 $role->add_cap( 'tablepress_import_tables_url' );
245 }
246 $role = get_role( 'editor' );
247 if ( ! empty( $role ) ) {
248 $role->add_cap( 'tablepress_import_tables_url' );
249 }
250
251 // Refresh current set of capabilities of the user, to be able to directly use the new caps.
252 $user = wp_get_current_user();
253 $user->get_role_caps();
254 }
255
256 /**
257 * Adds a custom "Import from URL" access capability to the "Editor" and "Administrator" user roles.
258 *
259 * @since 2.3.2
260 */
261 public function add_access_capabilities_tp232(): void {
262 $roles = array( 'administrator', 'editor' );
263 foreach ( $roles as $role ) {
264 $role = get_role( $role );
265 if ( ! empty( $role ) ) {
266 $role->add_cap( 'tablepress_import_tables_url' );
267 }
268 }
269
270 // Refresh current set of capabilities of the user, to be able to directly use the new caps.
271 $user = wp_get_current_user();
272 $user->get_role_caps();
273 }
274
275 /**
276 * Remove all TablePress capabilities from all roles.
277 *
278 * @since 1.1.0
279 *
280 * @global WP_Roles $wp_roles WordPress User Roles abstraction object.
281 * @see add_access_capabilities()
282 */
283 public function remove_access_capabilities(): void {
284 global $wp_roles;
285
286 if ( ! isset( $wp_roles ) ) {
287 $wp_roles = new WP_Roles(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
288 }
289
290 foreach ( $wp_roles->roles as $role => $details ) {
291 $role = $wp_roles->get_role( $role );
292 if ( empty( $role ) ) {
293 continue;
294 }
295
296 $role->remove_cap( 'tablepress_edit_tables' );
297 $role->remove_cap( 'tablepress_delete_tables' );
298 $role->remove_cap( 'tablepress_list_tables' );
299 $role->remove_cap( 'tablepress_add_tables' );
300 $role->remove_cap( 'tablepress_copy_tables' );
301 $role->remove_cap( 'tablepress_import_tables' );
302 $role->remove_cap( 'tablepress_export_tables' );
303 $role->remove_cap( 'tablepress_access_options_screen' );
304 $role->remove_cap( 'tablepress_access_about_screen' );
305 $role->remove_cap( 'tablepress_import_tables_wptr' ); // No longer used since 1.10, but might still be in the database.
306 $role->remove_cap( 'tablepress_edit_options' );
307 }
308
309 // Refresh current set of capabilities of the user, to be able to directly use the new caps.
310 $user = wp_get_current_user();
311 $user->get_role_caps();
312 }
313
314 /**
315 * Map TablePress meta capabilities to primitive capabilities.
316 *
317 * @since 1.0.0
318 *
319 * @param string[] $caps Current set of primitive caps.
320 * @param string $cap Meta cap that is to be checked/mapped.
321 * @param int $user_id User ID for which meta cap is to be checked.
322 * @param mixed[] $args Arguments for the check, here e.g. the table ID.
323 * @return string[] Modified set of primitive caps.
324 */
325 public function map_tablepress_meta_caps( array $caps, /* string */ $cap, int $user_id, array $args ): array {
326 // 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.
327
328 // Protect against other plugins not supplying a string for the `$cap` argument.
329 if ( ! is_string( $cap ) ) {
330 return $caps;
331 }
332
333 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 ) ) {
334 return $caps;
335 }
336
337 // $table_id = ( ! empty( $args ) ) ? $args[0] : false;
338
339 // Reset current set of primitive caps.
340 $caps = array();
341
342 switch ( $cap ) {
343 case 'tablepress_edit_table':
344 $caps[] = 'tablepress_edit_tables';
345 break;
346 case 'tablepress_edit_table_id':
347 $caps[] = 'tablepress_edit_tables';
348 break;
349 case 'tablepress_copy_table':
350 $caps[] = 'tablepress_copy_tables';
351 break;
352 case 'tablepress_delete_table':
353 $caps[] = 'tablepress_delete_tables';
354 break;
355 case 'tablepress_export_table':
356 $caps[] = 'tablepress_export_tables';
357 break;
358 case 'tablepress_preview_table':
359 $caps[] = 'tablepress_edit_tables';
360 break;
361 default:
362 // Something went wrong, deny access to be on the safe side.
363 $caps[] = 'do_not_allow';
364 break;
365 }
366
367 /**
368 * Filters a user's TablePress capabilities.
369 *
370 * @since 1.0.0
371 *
372 * @see map_meta_cap()
373 *
374 * @param string[] $caps The user's current TablePress capabilities.
375 * @param string $cap Capability name.
376 * @param int $user_id The user ID.
377 * @param mixed[] $args Adds the context to the cap, typically the table ID.
378 */
379 return apply_filters( 'tablepress_map_meta_caps', $caps, $cap, $user_id, $args );
380 }
381
382 /**
383 * Delete the WP_Option and the user option of the model.
384 *
385 * @since 1.0.0
386 */
387 public function destroy(): void {
388 $this->plugin_options->delete();
389 $this->user_options->delete_for_all_users();
390 }
391
392 } // class TablePress_Options_Model
393