| 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 array $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 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. |
| 60 |
); |
| 61 |
|
| 62 |
/** |
| 63 |
* Instance of WP_Option class for Plugin Options. |
| 64 |
* |
| 65 |
* @since 1.0.0 |
| 66 |
*/ |
| 67 |
protected \TablePress_WP_Option $plugin_options; |
| 68 |
|
| 69 |
/** |
| 70 |
* Instance of WP_User_Option class for User Options. |
| 71 |
* |
| 72 |
* @since 1.0.0 |
| 73 |
*/ |
| 74 |
protected \TablePress_WP_User_Option $user_options; |
| 75 |
|
| 76 |
/** |
| 77 |
* Init Options Model by creating the object instances for the Plugin and User Options. |
| 78 |
* |
| 79 |
* @since 1.0.0 |
| 80 |
*/ |
| 81 |
public function __construct() { |
| 82 |
parent::__construct(); |
| 83 |
|
| 84 |
$params = array( |
| 85 |
'option_name' => 'tablepress_plugin_options', |
| 86 |
'default_value' => $this->default_plugin_options, |
| 87 |
); |
| 88 |
$this->plugin_options = TablePress::load_class( 'TablePress_WP_Option', 'class-wp_option.php', 'classes', $params ); |
| 89 |
|
| 90 |
$params = array( |
| 91 |
'option_name' => 'tablepress_user_options', |
| 92 |
'default_value' => $this->default_user_options, |
| 93 |
); |
| 94 |
$this->user_options = TablePress::load_class( 'TablePress_WP_User_Option', 'class-wp_user_option.php', 'classes', $params ); |
| 95 |
|
| 96 |
// Filter to map Meta capabilities to Primitive Capabilities. |
| 97 |
add_filter( 'map_meta_cap', array( $this, 'map_tablepress_meta_caps' ), 10, 4 ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Update a single option or an array of options with new values. |
| 102 |
* |
| 103 |
* @since 1.0.0 |
| 104 |
* |
| 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). |
| 107 |
*/ |
| 108 |
public function update( /* array|string */ $new_options, /* string|bool|int|float|null */ $single_value = null ): void { |
| 109 |
// Allow saving of single options that are not in an array. |
| 110 |
if ( ! is_array( $new_options ) ) { |
| 111 |
$new_options = array( $new_options => $single_value ); |
| 112 |
} |
| 113 |
|
| 114 |
$plugin_options = $this->plugin_options->get(); |
| 115 |
$user_options = $this->user_options->get(); |
| 116 |
|
| 117 |
$old_options = array_merge( $plugin_options, $user_options ); |
| 118 |
|
| 119 |
foreach ( $new_options as $name => $value ) { |
| 120 |
if ( isset( $this->default_plugin_options[ $name ] ) ) { |
| 121 |
$plugin_options[ $name ] = $value; |
| 122 |
} elseif ( isset( $this->default_user_options[ $name ] ) ) { |
| 123 |
$user_options[ $name ] = $value; |
| 124 |
} else { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedElse |
| 125 |
// No valid Plugin or User Option -> discard the name/value pair. |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
$this->plugin_options->update( $plugin_options ); |
| 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 ); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Get the value of a single option, or an array with all options. |
| 144 |
* |
| 145 |
* @since 1.0.0 |
| 146 |
* |
| 147 |
* @param string|false $name Optional. Name of a single option to get, or false for all options. |
| 148 |
* @param mixed $default_value Optional. Default value, if the option $name does not exist. |
| 149 |
* @return mixed Value of the retrieved option $name, or $default_value if it does not exist, or array of all options. |
| 150 |
*/ |
| 151 |
public function get( /* string|false */ $name = false, /* string|bool|int|float|null */ $default_value = null ) /* : string|bool|int|float|array|null */ { |
| 152 |
if ( false === $name ) { |
| 153 |
return array_merge( $this->plugin_options->get(), $this->user_options->get() ); |
| 154 |
} |
| 155 |
|
| 156 |
// Single Option wanted. |
| 157 |
if ( $this->plugin_options->is_set( $name ) ) { |
| 158 |
return $this->plugin_options->get( $name ); |
| 159 |
} elseif ( $this->user_options->is_set( $name ) ) { |
| 160 |
return $this->user_options->get( $name ); |
| 161 |
} else { |
| 162 |
// No valid Plugin or User Option. |
| 163 |
return $default_value; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Get all Plugin Options (only used in Debug). |
| 169 |
* |
| 170 |
* @since 1.0.0 |
| 171 |
* |
| 172 |
* @return array<string, mixed> Array of all Plugin Options. |
| 173 |
*/ |
| 174 |
public function _debug_get_plugin_options(): array { |
| 175 |
return $this->plugin_options->get(); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Get all User Options (only used in Debug). |
| 180 |
* |
| 181 |
* @since 1.0.0 |
| 182 |
* |
| 183 |
* @return array<string, mixed> Array of all User Options. |
| 184 |
*/ |
| 185 |
public function _debug_get_user_options(): array { |
| 186 |
return $this->user_options->get(); |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Merge existing Plugin Options with default Plugin Options, |
| 191 |
* remove (no longer) existing options, e.g. after a plugin update. |
| 192 |
* |
| 193 |
* @since 1.0.0 |
| 194 |
*/ |
| 195 |
public function merge_plugin_options_defaults(): void { |
| 196 |
$plugin_options = $this->plugin_options->get(); |
| 197 |
// Remove old (i.e. no longer existing) Plugin Options. |
| 198 |
$plugin_options = array_intersect_key( $plugin_options, $this->default_plugin_options ); |
| 199 |
// Merge current into new Plugin Options. |
| 200 |
$plugin_options = array_merge( $this->default_plugin_options, $plugin_options ); |
| 201 |
|
| 202 |
$this->plugin_options->update( $plugin_options ); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Merge existing User Options with default User Options, |
| 207 |
* remove (no longer) existing options, e.g. after a plugin update. |
| 208 |
* |
| 209 |
* @since 1.0.0 |
| 210 |
*/ |
| 211 |
public function merge_user_options_defaults(): void { |
| 212 |
$user_options = $this->user_options->get(); |
| 213 |
// Remove old (i.e. no longer existing) User Options. |
| 214 |
$user_options = array_intersect_key( $user_options, $this->default_user_options ); |
| 215 |
// Merge current into new User Options. |
| 216 |
$user_options = array_merge( $this->default_user_options, $user_options ); |
| 217 |
|
| 218 |
$this->user_options->update( $user_options ); |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Adds the TablePress default capabilities to the "Administrator", "Editor", and "Author" user roles. |
| 223 |
* |
| 224 |
* @since 1.0.0 |
| 225 |
*/ |
| 226 |
public function add_access_capabilities(): void { |
| 227 |
// Capabilities for all roles. |
| 228 |
$roles = array( 'administrator', 'editor', 'author' ); |
| 229 |
foreach ( $roles as $role ) { |
| 230 |
$role = get_role( $role ); |
| 231 |
if ( empty( $role ) ) { |
| 232 |
continue; |
| 233 |
} |
| 234 |
|
| 235 |
// From get_post_type_capabilities(). |
| 236 |
$role->add_cap( 'tablepress_edit_tables' ); |
| 237 |
// $role->add_cap( 'tablepress_edit_others_tables' ); |
| 238 |
$role->add_cap( 'tablepress_delete_tables' ); |
| 239 |
// $role->add_cap( 'tablepress_delete_others_tables' ); |
| 240 |
|
| 241 |
// Custom capabilities. |
| 242 |
$role->add_cap( 'tablepress_list_tables' ); |
| 243 |
$role->add_cap( 'tablepress_add_tables' ); |
| 244 |
$role->add_cap( 'tablepress_copy_tables' ); |
| 245 |
$role->add_cap( 'tablepress_import_tables' ); |
| 246 |
$role->add_cap( 'tablepress_export_tables' ); |
| 247 |
$role->add_cap( 'tablepress_access_options_screen' ); |
| 248 |
$role->add_cap( 'tablepress_access_about_screen' ); |
| 249 |
} |
| 250 |
|
| 251 |
// Capabilities for single roles. |
| 252 |
$role = get_role( 'administrator' ); |
| 253 |
if ( ! empty( $role ) ) { |
| 254 |
$role->add_cap( 'tablepress_edit_options' ); |
| 255 |
$role->add_cap( 'tablepress_import_tables_url' ); |
| 256 |
} |
| 257 |
$role = get_role( 'editor' ); |
| 258 |
if ( ! empty( $role ) ) { |
| 259 |
$role->add_cap( 'tablepress_import_tables_url' ); |
| 260 |
} |
| 261 |
|
| 262 |
// Refresh current set of capabilities of the user, to be able to directly use the new caps. |
| 263 |
$user = wp_get_current_user(); |
| 264 |
$user->get_role_caps(); |
| 265 |
} |
| 266 |
|
| 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 |
/** |
| 287 |
* Remove all TablePress capabilities from all roles. |
| 288 |
* |
| 289 |
* @since 1.1.0 |
| 290 |
* |
| 291 |
* @global WP_Roles $wp_roles WordPress User Roles abstraction object. |
| 292 |
* @see add_access_capabilities() |
| 293 |
*/ |
| 294 |
public function remove_access_capabilities(): void { |
| 295 |
global $wp_roles; |
| 296 |
|
| 297 |
if ( ! isset( $wp_roles ) ) { |
| 298 |
$wp_roles = new WP_Roles(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 299 |
} |
| 300 |
|
| 301 |
foreach ( $wp_roles->roles as $role => $details ) { |
| 302 |
$role = $wp_roles->get_role( $role ); |
| 303 |
if ( empty( $role ) ) { |
| 304 |
continue; |
| 305 |
} |
| 306 |
|
| 307 |
$role->remove_cap( 'tablepress_edit_tables' ); |
| 308 |
$role->remove_cap( 'tablepress_delete_tables' ); |
| 309 |
$role->remove_cap( 'tablepress_list_tables' ); |
| 310 |
$role->remove_cap( 'tablepress_add_tables' ); |
| 311 |
$role->remove_cap( 'tablepress_copy_tables' ); |
| 312 |
$role->remove_cap( 'tablepress_import_tables' ); |
| 313 |
$role->remove_cap( 'tablepress_export_tables' ); |
| 314 |
$role->remove_cap( 'tablepress_access_options_screen' ); |
| 315 |
$role->remove_cap( 'tablepress_access_about_screen' ); |
| 316 |
$role->remove_cap( 'tablepress_import_tables_wptr' ); // No longer used since 1.10, but might still be in the database. |
| 317 |
$role->remove_cap( 'tablepress_edit_options' ); |
| 318 |
} |
| 319 |
|
| 320 |
// Refresh current set of capabilities of the user, to be able to directly use the new caps. |
| 321 |
$user = wp_get_current_user(); |
| 322 |
$user->get_role_caps(); |
| 323 |
} |
| 324 |
|
| 325 |
/** |
| 326 |
* Map TablePress meta capabilities to primitive capabilities. |
| 327 |
* |
| 328 |
* @since 1.0.0 |
| 329 |
* |
| 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. |
| 335 |
*/ |
| 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 |
|
| 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 ) ) { |
| 345 |
return $caps; |
| 346 |
} |
| 347 |
|
| 348 |
// $table_id = ( ! empty( $args ) ) ? $args[0] : false; |
| 349 |
|
| 350 |
// Reset current set of primitive caps. |
| 351 |
$caps = array(); |
| 352 |
|
| 353 |
switch ( $cap ) { |
| 354 |
case 'tablepress_edit_table': |
| 355 |
$caps[] = 'tablepress_edit_tables'; |
| 356 |
break; |
| 357 |
case 'tablepress_edit_table_id': |
| 358 |
$caps[] = 'tablepress_edit_tables'; |
| 359 |
break; |
| 360 |
case 'tablepress_copy_table': |
| 361 |
$caps[] = 'tablepress_copy_tables'; |
| 362 |
break; |
| 363 |
case 'tablepress_delete_table': |
| 364 |
$caps[] = 'tablepress_delete_tables'; |
| 365 |
break; |
| 366 |
case 'tablepress_export_table': |
| 367 |
$caps[] = 'tablepress_export_tables'; |
| 368 |
break; |
| 369 |
case 'tablepress_preview_table': |
| 370 |
$caps[] = 'tablepress_edit_tables'; |
| 371 |
break; |
| 372 |
default: |
| 373 |
// Something went wrong, deny access to be on the safe side. |
| 374 |
$caps[] = 'do_not_allow'; |
| 375 |
break; |
| 376 |
} |
| 377 |
|
| 378 |
/** |
| 379 |
* Filters a user's TablePress capabilities. |
| 380 |
* |
| 381 |
* @since 1.0.0 |
| 382 |
* |
| 383 |
* @see map_meta_cap() |
| 384 |
* |
| 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. |
| 389 |
*/ |
| 390 |
return apply_filters( 'tablepress_map_meta_caps', $caps, $cap, $user_id, $args ); |
| 391 |
} |
| 392 |
|
| 393 |
/** |
| 394 |
* Delete the WP_Option and the user option of the model. |
| 395 |
* |
| 396 |
* @since 1.0.0 |
| 397 |
*/ |
| 398 |
public function destroy(): void { |
| 399 |
$this->plugin_options->delete(); |
| 400 |
$this->user_options->delete_for_all_users(); |
| 401 |
} |
| 402 |
|
| 403 |
} // class TablePress_Options_Model |
| 404 |
|