PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 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 All 45 releases
tablepress / classes / class-wp_option.php

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

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