PluginProbe
TablePress – Tables in WordPress made easy / 2.3.1
TablePress – Tables in WordPress made easy v2.3.1
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.1, at models/model-options.php

369 lines 11.6 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 * Add default capabilities to "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 }
245
246 // Refresh current set of capabilities of the user, to be able to directly use the new caps.
247 $user = wp_get_current_user();
248 $user->get_role_caps();
249 }
250
251 /**
252 * Remove all TablePress capabilities from all roles.
253 *
254 * @since 1.1.0
255 *
256 * @global WP_Roles $wp_roles WordPress User Roles abstraction object.
257 * @see add_access_capabilities()
258 */
259 public function remove_access_capabilities(): void {
260 global $wp_roles;
261
262 if ( ! isset( $wp_roles ) ) {
263 $wp_roles = new WP_Roles(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
264 }
265
266 foreach ( $wp_roles->roles as $role => $details ) {
267 $role = $wp_roles->get_role( $role );
268 if ( empty( $role ) ) {
269 continue;
270 }
271
272 $role->remove_cap( 'tablepress_edit_tables' );
273 $role->remove_cap( 'tablepress_delete_tables' );
274 $role->remove_cap( 'tablepress_list_tables' );
275 $role->remove_cap( 'tablepress_add_tables' );
276 $role->remove_cap( 'tablepress_copy_tables' );
277 $role->remove_cap( 'tablepress_import_tables' );
278 $role->remove_cap( 'tablepress_export_tables' );
279 $role->remove_cap( 'tablepress_access_options_screen' );
280 $role->remove_cap( 'tablepress_access_about_screen' );
281 $role->remove_cap( 'tablepress_import_tables_wptr' ); // No longer used since 1.10, but might still be in the database.
282 $role->remove_cap( 'tablepress_edit_options' );
283 }
284
285 // Refresh current set of capabilities of the user, to be able to directly use the new caps.
286 $user = wp_get_current_user();
287 $user->get_role_caps();
288 }
289
290 /**
291 * Map TablePress meta capabilities to primitive capabilities.
292 *
293 * @since 1.0.0
294 *
295 * @param string[] $caps Current set of primitive caps.
296 * @param string $cap Meta cap that is to be checked/mapped.
297 * @param int $user_id User ID for which meta cap is to be checked.
298 * @param mixed[] $args Arguments for the check, here e.g. the table ID.
299 * @return string[] Modified set of primitive caps.
300 */
301 public function map_tablepress_meta_caps( array $caps, /* string */ $cap, int $user_id, array $args ): array {
302 // 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.
303
304 // Protect against other plugins not supplying a string for the `$cap` argument.
305 if ( ! is_string( $cap ) ) {
306 return $caps;
307 }
308
309 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 ) ) {
310 return $caps;
311 }
312
313 // $table_id = ( ! empty( $args ) ) ? $args[0] : false;
314
315 // Reset current set of primitive caps.
316 $caps = array();
317
318 switch ( $cap ) {
319 case 'tablepress_edit_table':
320 $caps[] = 'tablepress_edit_tables';
321 break;
322 case 'tablepress_edit_table_id':
323 $caps[] = 'tablepress_edit_tables';
324 break;
325 case 'tablepress_copy_table':
326 $caps[] = 'tablepress_copy_tables';
327 break;
328 case 'tablepress_delete_table':
329 $caps[] = 'tablepress_delete_tables';
330 break;
331 case 'tablepress_export_table':
332 $caps[] = 'tablepress_export_tables';
333 break;
334 case 'tablepress_preview_table':
335 $caps[] = 'tablepress_edit_tables';
336 break;
337 default:
338 // Something went wrong, deny access to be on the safe side.
339 $caps[] = 'do_not_allow';
340 break;
341 }
342
343 /**
344 * Filters a user's TablePress capabilities.
345 *
346 * @since 1.0.0
347 *
348 * @see map_meta_cap()
349 *
350 * @param string[] $caps The user's current TablePress capabilities.
351 * @param string $cap Capability name.
352 * @param int $user_id The user ID.
353 * @param mixed[] $args Adds the context to the cap, typically the table ID.
354 */
355 return apply_filters( 'tablepress_map_meta_caps', $caps, $cap, $user_id, $args );
356 }
357
358 /**
359 * Delete the WP_Option and the user option of the model.
360 *
361 * @since 1.0.0
362 */
363 public function destroy(): void {
364 $this->plugin_options->delete();
365 $this->user_options->delete_for_all_users();
366 }
367
368 } // class TablePress_Options_Model
369