PluginProbe
TablePress – Tables in WordPress made easy / 2.3
TablePress – Tables in WordPress made easy v2.3
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 / classes / class-wp_user_option.php

class-wp_user_option.php in TablePress – Tables in WordPress made easy 2.3, at classes/class-wp_user_option.php

106 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * TablePress WP User Option Wrapper class for WordPress Options
4 *
5 * Wraps the WordPress Options API, so that (especially) arrays are stored as JSON, instead of being serialized by PHP.
6 *
7 * @package TablePress
8 * @subpackage Classes
9 * @author Tobias Bäthge
10 * @since 1.0.0
11 */
12
13 // Prohibit direct script loading.
14 defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );
15
16 // Load parent class.
17 TablePress::load_file( 'class-wp_option.php', 'classes' );
18
19 /**
20 * TablePress WP User Option Wrapper class
21 *
22 * @package TablePress
23 * @subpackage Classes
24 * @author Tobias Bäthge
25 * @since 1.0.0
26 */
27 class TablePress_WP_User_Option extends TablePress_WP_Option {
28
29 /**
30 * Get the value of a WP User Option with the WP User Options API.
31 *
32 * @since 1.0.0
33 *
34 * @param string $option_name Name of the WP User Option.
35 * @param mixed $default_value Default value of the WP User Option.
36 * @return mixed Current value of the WP User Option, or $default_value if it does not exist.
37 */
38 #[\Override]
39 protected function _get_option( string $option_name, /* string|int|float|bool|array|null */ $default_value ) /* : string|int|float|bool|array|null */ {
40 // Non-logged-in user can never have a saved option value.
41 if ( ! is_user_logged_in() ) {
42 return $default_value;
43 }
44
45 $option_value = get_user_option( $option_name );
46 // get_user_option() only knows false as the default value, so we have to wrap that.
47 if ( false === $option_value ) {
48 $option_value = $default_value;
49 }
50 return $option_value;
51 }
52
53 /**
54 * Update the value of a WP User Option with the WP User Options API.
55 *
56 * @since 1.0.0
57 *
58 * @param string $option_name Name of the WP User Option.
59 * @param string $new_value New value of the WP User Option (not slashed).
60 * @return bool True on success, false on failure.
61 */
62 #[\Override]
63 protected function _update_option( string $option_name, string $new_value ): bool {
64 // Non-logged-in user can never have a saved option value to be updated.
65 if ( ! is_user_logged_in() ) {
66 return false;
67 }
68
69 // WP expects a slashed value.
70 $new_value = wp_slash( $new_value );
71 return (bool) update_user_option( get_current_user_id(), $option_name, $new_value, false );
72 }
73
74 /**
75 * Delete a WP User Option with the WP User Options API.
76 *
77 * @since 1.0.0
78 *
79 * @param string $option_name Name of the WP User Option.
80 * @return bool True on success, false on failure.
81 */
82 #[\Override]
83 protected function _delete_option( string $option_name ): bool {
84 // Non-logged-in user can never have a saved option value to be deleted.
85 if ( ! is_user_logged_in() ) {
86 return false;
87 }
88
89 return delete_user_option( get_current_user_id(), $option_name, false );
90 }
91
92 /**
93 * Delete a WP User Option with the WP User Options API, for all users of the site.
94 *
95 * @since 1.0.0
96 */
97 public function delete_for_all_users(): void {
98 $users = get_users();
99 foreach ( $users as $user ) {
100 delete_user_option( $user->ID, $this->option_name, false );
101 // @todo Add other user options for different actions as well.
102 }
103 }
104
105 } // class TablePress_WP_User_Option
106