PluginProbe
TablePress – Tables in WordPress made easy / 2.4.3
TablePress – Tables in WordPress made easy v2.4.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_option.php

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

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