PluginProbe
TablePress – Tables in WordPress made easy / 2.1.7
TablePress – Tables in WordPress made easy v2.1.7
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 2.1.7, at models/model-options.php

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