PluginProbe
TablePress – Tables in WordPress made easy / 1.12
TablePress – Tables in WordPress made easy v1.12
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 / models / model-options.php

model-options.php in TablePress – Tables in WordPress made easy 1.12, at models/model-options.php

357 lines 10.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Options Model
4 *
5 * @package TablePress
6 * @subpackage Models
7 * @author Tobias Bäthge
8 * @since 1.0.0
9 */
10
11 // Prohibit direct script loading.
12 defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );
13
14 /**
15 * Options Model class
16 * @package TablePress
17 * @subpackage Models
18 * @author Tobias Bäthge
19 * @since 1.0.0
20 */
21 class TablePress_Options_Model extends TablePress_Model {
22
23 /**
24 * Default Plugin Options.
25 *
26 * @since 1.0.0
27 * @var array
28 */
29 protected $default_plugin_options = array(
30 'plugin_options_db_version' => 0,
31 'table_scheme_db_version' => 0,
32 'prev_tablepress_version' => '0',
33 'tablepress_version' => TablePress::version,
34 'first_activation' => 0,
35 'message_plugin_update' => false,
36 'message_donation_nag' => true,
37 'use_custom_css' => true,
38 'use_custom_css_file' => true,
39 'custom_css' => '',
40 'custom_css_minified' => '',
41 'custom_css_version' => 0,
42 );
43
44 /**
45 * Default User Options.
46 *
47 * @since 1.0.0
48 * @var array
49 */
50 protected $default_user_options = array(
51 'user_options_db_version' => TablePress::db_version, // to prevent saving on first load
52 'admin_menu_parent_page' => 'middle',
53 'message_first_visit' => true,
54 );
55
56 /**
57 * Instance of WP_Option class for Plugin Options.
58 *
59 * @since 1.0.0
60 * @var TablePress_WP_Option
61 */
62 protected $plugin_options;
63
64 /**
65 * Instance of WP_User_Option class for User Options.
66 *
67 * @since 1.0.0
68 * @var TablePress_WP_User_Option
69 */
70 protected $user_options;
71
72 /**
73 * Init Options Model by creating the object instances for the Plugin and User Options.
74 *
75 * @since 1.0.0
76 */
77 public function __construct() {
78 parent::__construct();
79
80 $params = array(
81 'option_name' => 'tablepress_plugin_options',
82 'default_value' => $this->default_plugin_options,
83 );
84 $this->plugin_options = TablePress::load_class( 'TablePress_WP_Option', 'class-wp_option.php', 'classes', $params );
85
86 $params = array(
87 'option_name' => 'tablepress_user_options',
88 'default_value' => $this->default_user_options,
89 );
90 $this->user_options = TablePress::load_class( 'TablePress_WP_User_Option', 'class-wp_user_option.php', 'classes', $params );
91
92 // Filter to map Meta capabilities to Primitive Capabilities.
93 add_filter( 'map_meta_cap', array( $this, 'map_tablepress_meta_caps' ), 10, 4 );
94 }
95
96 /**
97 * Update a single option or an array of options with new values.
98 *
99 * @since 1.0.0
100 *
101 * @param array|string $new_options Array of new options (name => value) or name of a single option.
102 * @param mixed $single_value Optional. New value for a single option (only if $new_options is not an array).
103 */
104 public function update( $new_options, $single_value = null ) {
105 // Allow saving of single options that are not in an array.
106 if ( ! is_array( $new_options ) ) {
107 $new_options = array( $new_options => $single_value );
108 }
109
110 $plugin_options = $this->plugin_options->get();
111 $user_options = $this->user_options->get();
112 foreach ( $new_options as $name => $value ) {
113 if ( isset( $this->default_plugin_options[ $name ] ) ) {
114 $plugin_options[ $name ] = $value;
115 } elseif ( isset( $this->default_user_options[ $name ] ) ) {
116 $user_options[ $name ] = $value;
117 } else {
118 // No valid Plugin or User Option -> discard the name/value pair.
119 }
120 }
121
122 $this->plugin_options->update( $plugin_options );
123 $this->user_options->update( $user_options );
124 }
125
126 /**
127 * Get the value of a single option, or an array with all options.
128 *
129 * @since 1.0.0
130 *
131 * @param string|bool $name Optional. Name of a single option to get, or false for all options.
132 * @param mixed $default_value Optional. Default value, if the option $name does not exist.
133 * @return mixed Value of the retrieved option $name, or $default_value if it does not exist, or array of all options.
134 */
135 public function get( $name = false, $default_value = null ) {
136 if ( false === $name ) {
137 return array_merge( $this->plugin_options->get(), $this->user_options->get() );
138 }
139
140 // Single Option wanted.
141 if ( $this->plugin_options->is_set( $name ) ) {
142 return $this->plugin_options->get( $name );
143 } elseif ( $this->user_options->is_set( $name ) ) {
144 return $this->user_options->get( $name );
145 } else {
146 // No valid Plugin or User Option.
147 return $default_value;
148 }
149 }
150
151 /**
152 * Get all Plugin Options (only used in Debug).
153 *
154 * @since 1.0.0
155 *
156 * @return array Array of all Plugin Options.
157 */
158 public function _debug_get_plugin_options() {
159 return $this->plugin_options->get();
160 }
161
162 /**
163 * Get all User Options (only used in Debug).
164 *
165 * @since 1.0.0
166 *
167 * @return array Array of all User Options.
168 */
169 public function _debug_get_user_options() {
170 return $this->user_options->get();
171 }
172
173 /**
174 * Merge existing Plugin Options with default Plugin Options,
175 * remove (no longer) existing options, e.g. after a plugin update.
176 *
177 * @since 1.0.0
178 */
179 public function merge_plugin_options_defaults() {
180 $plugin_options = $this->plugin_options->get();
181 // Remove old (i.e. no longer existing) Plugin Options.
182 $plugin_options = array_intersect_key( $plugin_options, $this->default_plugin_options );
183 // Merge current into new Plugin Options.
184 $plugin_options = array_merge( $this->default_plugin_options, $plugin_options );
185
186 $this->plugin_options->update( $plugin_options );
187 }
188
189 /**
190 * Merge existing User Options with default User Options,
191 * remove (no longer) existing options, e.g. after a plugin update.
192 *
193 * @since 1.0.0
194 */
195 public function merge_user_options_defaults() {
196 $user_options = $this->user_options->get();
197 // Remove old (i.e. no longer existing) User Options.
198 $user_options = array_intersect_key( $user_options, $this->default_user_options );
199 // Merge current into new User Options.
200 $user_options = array_merge( $this->default_user_options, $user_options );
201
202 $this->user_options->update( $user_options );
203 }
204
205 /**
206 * Add default capabilities to "Administrator", "Editor", and "Author" user roles.
207 *
208 * @since 1.0.0
209 */
210 public function add_access_capabilities() {
211 // Capabilities for all roles.
212 $roles = array( 'administrator', 'editor', 'author' );
213 foreach ( $roles as $role ) {
214 $role = get_role( $role );
215 if ( empty( $role ) ) {
216 continue;
217 }
218
219 // From get_post_type_capabilities().
220 $role->add_cap( 'tablepress_edit_tables' );
221 // $role->add_cap( 'tablepress_edit_others_tables' );
222 $role->add_cap( 'tablepress_delete_tables' );
223 // $role->add_cap( 'tablepress_delete_others_tables' );
224
225 // Custom capabilities.
226 $role->add_cap( 'tablepress_list_tables' );
227 $role->add_cap( 'tablepress_add_tables' );
228 $role->add_cap( 'tablepress_copy_tables' );
229 $role->add_cap( 'tablepress_import_tables' );
230 $role->add_cap( 'tablepress_export_tables' );
231 $role->add_cap( 'tablepress_access_options_screen' );
232 $role->add_cap( 'tablepress_access_about_screen' );
233 }
234
235 // Capabilities for single roles.
236 $role = get_role( 'administrator' );
237 if ( ! empty( $role ) ) {
238 $role->add_cap( 'tablepress_edit_options' );
239 }
240
241 // Refresh current set of capabilities of the user, to be able to directly use the new caps.
242 $user = wp_get_current_user();
243 $user->get_role_caps();
244 }
245
246 /**
247 * Remove all TablePress capabilities from all roles.
248 *
249 * @since 1.1.0
250 *
251 * @global WP_Roles $wp_roles WordPress User Roles abstraction object.
252 * @see add_access_capabilities()
253 */
254 public function remove_access_capabilities() {
255 global $wp_roles;
256
257 if ( ! isset( $wp_roles ) ) {
258 $wp_roles = new WP_Roles();
259 }
260
261 foreach ( $wp_roles->roles as $role => $details ) {
262 $role = $wp_roles->get_role( $role );
263 if ( empty( $role ) ) {
264 continue;
265 }
266
267 $role->remove_cap( 'tablepress_edit_tables' );
268 $role->remove_cap( 'tablepress_delete_tables' );
269 $role->remove_cap( 'tablepress_list_tables' );
270 $role->remove_cap( 'tablepress_add_tables' );
271 $role->remove_cap( 'tablepress_copy_tables' );
272 $role->remove_cap( 'tablepress_import_tables' );
273 $role->remove_cap( 'tablepress_export_tables' );
274 $role->remove_cap( 'tablepress_access_options_screen' );
275 $role->remove_cap( 'tablepress_access_about_screen' );
276 $role->remove_cap( 'tablepress_import_tables_wptr' ); // No longer used since 1.10, but might still be in the database.
277 $role->remove_cap( 'tablepress_edit_options' );
278 }
279
280 // Refresh current set of capabilities of the user, to be able to directly use the new caps.
281 $user = wp_get_current_user();
282 $user->get_role_caps();
283 }
284
285 /**
286 * Map TablePress meta capabilities to primitive capabilities.
287 *
288 * @since 1.0.0
289 *
290 * @param array $caps Current set of primitive caps.
291 * @param string $cap Meta cap that is to be checked/mapped.
292 * @param int $user_id User ID for which meta cap is to be checked.
293 * @param array $args Arguments for the check, here e.g. the table ID.
294 * @return array $caps Modified set of primitive caps.
295 */
296 public function map_tablepress_meta_caps( array $caps, $cap, $user_id, array $args ) {
297 if ( ! in_array( $cap, array( 'tablepress_edit_table', 'tablepress_edit_table_id', 'tablepress_copy_table', 'tablepress_delete_table', 'tablepress_export_table', 'tablepress_preview_table' ), true ) ) {
298 return $caps;
299 }
300
301 // $table_id = ( ! empty( $args ) ) ? $args[0] : false;
302
303 // Reset current set of primitive caps.
304 $caps = array();
305
306 switch ( $cap ) {
307 case 'tablepress_edit_table':
308 $caps[] = 'tablepress_edit_tables';
309 break;
310 case 'tablepress_edit_table_id':
311 $caps[] = 'tablepress_edit_tables';
312 break;
313 case 'tablepress_copy_table':
314 $caps[] = 'tablepress_copy_tables';
315 break;
316 case 'tablepress_delete_table':
317 $caps[] = 'tablepress_delete_tables';
318 break;
319 case 'tablepress_export_table':
320 $caps[] = 'tablepress_export_tables';
321 break;
322 case 'tablepress_preview_table':
323 $caps[] = 'tablepress_edit_tables';
324 break;
325 default:
326 // Something went wrong, deny access to be on the safe side.
327 $caps[] = 'do_not_allow';
328 break;
329 }
330
331 /**
332 * Filter a user's TablePress capabilities.
333 *
334 * @since 1.0.0
335 *
336 * @see map_meta_cap()
337 *
338 * @param array $caps The user's current TablePress capabilities.
339 * @param string $cap Capability name.
340 * @param int $user_id The user ID.
341 * @param array $args Adds the context to the cap, typically the table ID.
342 */
343 return apply_filters( 'tablepress_map_meta_caps', $caps, $cap, $user_id, $args );
344 }
345
346 /**
347 * Delete the WP_Option and the user option of the model.
348 *
349 * @since 1.0.0
350 */
351 public function destroy() {
352 $this->plugin_options->delete();
353 $this->user_options->delete_for_all_users();
354 }
355
356 } // class TablePress_Options_Model
357