PluginProbe
TablePress – Tables in WordPress made easy / 3.2.2
TablePress – Tables in WordPress made easy v3.2.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 / classes / class-wp_option.php

class-wp_option.php in TablePress – Tables in WordPress made easy 3.2.2, at classes/class-wp_option.php

165 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * TablePress WP 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 /**
17 * TablePress WP Option Wrapper class
18 *
19 * @package TablePress
20 * @subpackage Classes
21 * @author Tobias Bäthge
22 * @since 1.0.0
23 */
24 class TablePress_WP_Option {
25
26 /**
27 * Name/Key of the Option (in its location in the database).
28 *
29 * @since 1.0.0
30 */
31 protected string $option_name;
32
33 /**
34 * Current value of the option.
35 *
36 * @since 1.0.0
37 * @var mixed[]
38 */
39 protected array $option_value;
40
41 /**
42 * Initialize with Option Name.
43 *
44 * @since 1.0.0
45 *
46 * @param array{option_name: string, default_value: mixed[]} $params {
47 * An array of Option parameters.
48 *
49 * @type string $option_name Name of the Option.
50 * @type array $default_value Default values for the Option, as an array.
51 * }
52 */
53 public function __construct( array $params ) {
54 $this->option_name = $params['option_name'];
55
56 $option_value = $this->_get_option( $this->option_name, null );
57 if ( ! is_null( $option_value ) ) {
58 $this->option_value = (array) json_decode( $option_value, true );
59 } else {
60 $this->option_value = $params['default_value'];
61 }
62 }
63
64 /**
65 * Check if Option is set.
66 *
67 * @since 1.0.0
68 *
69 * @param string $name Name of the option to check.
70 * @return bool Whether the option is set.
71 */
72 public function is_set( string $name ): bool {
73 return isset( $this->option_value[ $name ] );
74 }
75
76 /**
77 * Get a single Option, or get all Options.
78 *
79 * @since 1.0.0
80 *
81 * @param string|false $name Optional. Name of a single option to get, or false for all options.
82 * @param mixed $default_value Optional. Default value to return, if a single option $name does not exist.
83 * @return mixed Value of the retrieved option $name or $default_value if it does not exist, or all options.
84 */
85 public function get( /* string|false */ $name = false, /* string|int|float|bool|array|null */ $default_value = null ) /* : string|int|float|bool|array|null */ {
86 if ( false === $name ) {
87 return $this->option_value;
88 }
89
90 // Single Option wanted.
91 if ( isset( $this->option_value[ $name ] ) ) {
92 return $this->option_value[ $name ];
93 } else {
94 return $default_value;
95 }
96 }
97
98 /**
99 * Update Option.
100 *
101 * @since 1.0.0
102 *
103 * @param array<string, mixed> $new_options New options (name => value).
104 * @return bool True on success, false on failure.
105 */
106 public function update( array $new_options ): bool {
107 $this->option_value = $new_options;
108 return $this->_update_option( $this->option_name, wp_json_encode( $this->option_value, TABLEPRESS_JSON_OPTIONS ) ); // @phpstan-ignore argument.type
109 }
110
111 /**
112 * Delete Option.
113 *
114 * @since 1.0.0
115 *
116 * @return bool True on success, false on failure.
117 */
118 public function delete(): bool {
119 return $this->_delete_option( $this->option_name );
120 }
121
122 /*
123 * Internal functions mapping - This needs to be re-defined by child classes.
124 */
125
126 /**
127 * Get the value of a WP Option with the WP Options API.
128 *
129 * @since 1.0.0
130 *
131 * @param string $option_name Name of the WP Option.
132 * @param mixed $default_value Default value of the WP Option.
133 * @return mixed Current value of the WP Option, or $default_value if it does not exist.
134 */
135 protected function _get_option( string $option_name, /* string|int|float|bool|array|null */ $default_value ) /* : string|int|float|bool|array|null */ {
136 return get_option( $option_name, $default_value );
137 }
138
139 /**
140 * Update the value of a WP Option with the WP Options API.
141 *
142 * @since 1.0.0
143 *
144 * @param string $option_name Name of the WP Option.
145 * @param string $new_value New value of the WP Option.
146 * @return bool True on success, false on failure.
147 */
148 protected function _update_option( string $option_name, string $new_value ): bool {
149 return update_option( $option_name, $new_value );
150 }
151
152 /**
153 * Delete a WP Option with the WP Options API.
154 *
155 * @since 1.0.0
156 *
157 * @param string $option_name Name of the WP Option.
158 * @return bool True on success, false on failure.
159 */
160 protected function _delete_option( string $option_name ): bool {
161 return delete_option( $option_name );
162 }
163
164 } // class TablePress_WP_Option
165