PluginProbe
TablePress – Tables in WordPress made easy / 1.14
TablePress – Tables in WordPress made easy v1.14
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 1.14, at classes/class-wp_option.php

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