| 1 |
<?php |
| 2 |
/** |
| 3 |
* Suppress "error - 0 - No summary was found for this file" on phpdoc generation |
| 4 |
* |
| 5 |
* @package WPDataAccess |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPDataAccess { |
| 9 |
|
| 10 |
/** |
| 11 |
* Class WPDA |
| 12 |
* |
| 13 |
* Plugin default values and settings are managed through this class. Every plugin option has a default value |
| 14 |
* which is maintained in an array together with the option name. Options are only saved in $wpdb->options when |
| 15 |
* they are changed. Otherwise the default values are used. After reading option values from $wpdb->options the |
| 16 |
* values are cached as many of them are used in multiple |
| 17 |
* places during the processing of a request. |
| 18 |
* |
| 19 |
* @package WPDataAccess |
| 20 |
* @author Peter Schulz |
| 21 |
* @since 1.0.0 |
| 22 |
*/ |
| 23 |
class WPDA { |
| 24 |
|
| 25 |
/** |
| 26 |
* Label for WordPress tables |
| 27 |
*/ |
| 28 |
const TABLE_TYPE_WP = 'wordpress table'; |
| 29 |
/** |
| 30 |
* Label for WPDA plugin tables |
| 31 |
*/ |
| 32 |
const TABLE_TYPE_WPDA = 'plugin table'; |
| 33 |
|
| 34 |
// Options are stored in arrays (OPTION_ARRAYS): |
| 35 |
// [0] = option name (as saved in wp_options). |
| 36 |
// [1] = default value (used if option is not available in wp_options). |
| 37 |
// Application options. |
| 38 |
/** |
| 39 |
* Option wpda_name and it's default value |
| 40 |
*/ |
| 41 |
const OPTION_WPDA_NAME = [ 'wpda_name', 'WP Data Access' ]; |
| 42 |
/** |
| 43 |
* Option wpda_version and it's default value |
| 44 |
*/ |
| 45 |
const OPTION_WPDA_VERSION = [ 'wpda_version', '2.0.13' ]; |
| 46 |
/** |
| 47 |
* Option wpda_prefix and it's default value |
| 48 |
*/ |
| 49 |
const OPTION_WPDA_PREFIX = [ 'wpda_prefix', 'wpda_' ]; |
| 50 |
/** |
| 51 |
* Option wpda_setup_error and it's default value |
| 52 |
*/ |
| 53 |
const OPTION_WPDA_SETUP_ERROR = [ 'wpda_setup_error', '' ]; // Values: off = do not display setup errors, other = show. |
| 54 |
/** |
| 55 |
* Option wpda_show_whats_new and it's default value |
| 56 |
*/ |
| 57 |
const OPTION_WPDA_SHOW_WHATS_NEW = [ 'wpda_show_whats_new', 'off' ]; // Values: off = hide link to what's new page, other = show. |
| 58 |
/** |
| 59 |
* Option wpda_uninstall_tables and it's default value |
| 60 |
*/ |
| 61 |
const OPTION_WPDA_UNINSTALL_TABLES = [ 'wpda_uninstall_tables', 'on' ]; // On uninstall drop WPDA tables. |
| 62 |
/** |
| 63 |
* Option wpda_uninstall_options and it's default value |
| 64 |
*/ |
| 65 |
const OPTION_WPDA_UNINSTALL_OPTIONS = [ 'wpda_uninstall_options', 'on' ]; // On uninstall delety WPDA options. |
| 66 |
/** |
| 67 |
* Option wpda_bootstrap_version and it's default value |
| 68 |
*/ |
| 69 |
const OPTION_WPDA_BOOTSTRAP_VERSION = [ 'wpda_bootstrap_version', '3.3.7' ]; |
| 70 |
/** |
| 71 |
* Option wpda_datatables_version and it's default value |
| 72 |
*/ |
| 73 |
const OPTION_WPDA_DATATABLES_VERSION = [ 'wpda_datatables_version', '1.10.16' ]; |
| 74 |
/** |
| 75 |
* Option wpda_datatables_responsive_version and it's default value |
| 76 |
*/ |
| 77 |
const OPTION_WPDA_DATATABLES_RESPONSIVE_VERSION = [ 'wpda_datatables_responsive_version', '2.1.1' ]; |
| 78 |
|
| 79 |
// Back-end options. |
| 80 |
/** |
| 81 |
* Option wpda_be_table_access and it's default value |
| 82 |
*/ |
| 83 |
const OPTION_BE_TABLE_ACCESS = [ 'wpda_be_table_access', 'show' ]; // Values: show, hide and select. |
| 84 |
/** |
| 85 |
* Option wpda_be_table_access_selected and it's default value |
| 86 |
*/ |
| 87 |
const OPTION_BE_TABLE_ACCESS_SELECTED = [ 'wpda_be_table_access_selected', '' ]; // Tables authorized in WPDA. |
| 88 |
/** |
| 89 |
* Option wpda_be_allow_structure and it's default value |
| 90 |
*/ |
| 91 |
const OPTION_BE_ALLOW_STRUCTURE = [ 'wpda_be_allow_structure', 'on' ]; // Show table structure link in list table. |
| 92 |
/** |
| 93 |
* Option wpda_be_allow_schemas and it's default value |
| 94 |
*/ |
| 95 |
const OPTION_BE_ALLOW_SCHEMAS = [ 'wpda_be_allow_schemas', 'on' ]; // Allow accessing other schemas. |
| 96 |
/** |
| 97 |
* Option wpda_be_allow_drop and it's default value |
| 98 |
*/ |
| 99 |
const OPTION_BE_ALLOW_DROP = [ 'wpda_be_allow_drop', 'on' ]; // Show drop table link in list table. |
| 100 |
/** |
| 101 |
* Option wpda_be_allow_truncate and it's default value |
| 102 |
*/ |
| 103 |
const OPTION_BE_ALLOW_TRUNCATE = [ 'wpda_be_allow_truncate', 'on' ]; // Show truncate table link in list table. |
| 104 |
/** |
| 105 |
* Option wpda_be_allow_drop_index and it's default value |
| 106 |
*/ |
| 107 |
const OPTION_BE_ALLOW_DROP_INDEX = [ 'wpda_be_allow_drop_index', 'on' ]; // Show drop index link in list table. |
| 108 |
/** |
| 109 |
* Option wpda_be_allow_rename and it's default value |
| 110 |
*/ |
| 111 |
const OPTION_BE_ALLOW_RENAME = [ 'wpda_be_allow_rename', 'on' ]; // Allow rename table/view. |
| 112 |
/** |
| 113 |
* Option wpda_be_allow_copy and it's default value |
| 114 |
*/ |
| 115 |
const OPTION_BE_ALLOW_COPY = [ 'wpda_be_allow_copy', 'on' ]; // Allow copy table/view. |
| 116 |
/** |
| 117 |
* Option wpda_be_view_link and it's default value |
| 118 |
*/ |
| 119 |
const OPTION_BE_VIEW_LINK = [ 'wpda_be_view_link', 'on' ]; // Show view link in list table. |
| 120 |
/** |
| 121 |
* Option wpda_be_allow_insert and it's default value |
| 122 |
*/ |
| 123 |
const OPTION_BE_ALLOW_INSERT = [ 'wpda_be_allow_insert', 'on' ]; // Show insert link in list table (simple form). |
| 124 |
/** |
| 125 |
* Option wpda_be_allow_update and it's default value |
| 126 |
*/ |
| 127 |
const OPTION_BE_ALLOW_UPDATE = [ 'wpda_be_allow_update', 'on' ]; // Show update link in list table (simple form). |
| 128 |
/** |
| 129 |
* Option wpda_be_allow_delete and it's default value |
| 130 |
*/ |
| 131 |
const OPTION_BE_ALLOW_DELETE = [ 'wpda_be_allow_delete', 'on' ]; // Show delete link in list table. |
| 132 |
/** |
| 133 |
* Option wpda_be_wpda_export_tables and it's default value |
| 134 |
*/ |
| 135 |
const OPTION_BE_EXPORT_TABLES = [ 'wpda_be_wpda_export_tables', 'on' ]; // Show export link in list table (main only). |
| 136 |
/** |
| 137 |
* Option wpda_be_wpda_export_rows and it's default value |
| 138 |
*/ |
| 139 |
const OPTION_BE_EXPORT_ROWS = [ 'wpda_be_wpda_export_rows', 'on' ]; // Show export link in list table (row export). |
| 140 |
/** |
| 141 |
* Option wpda_be_wpda_export_variable_prefix and it's default value |
| 142 |
*/ |
| 143 |
const OPTION_BE_EXPORT_VARIABLE_PREFIX = [ 'wpda_be_wpda_export_variable_prefix', 'on' ]; // Allows to import into repository with different wpdb prefix. |
| 144 |
/** |
| 145 |
* Option wpda_be_wpda_allow_imports and it's default value |
| 146 |
*/ |
| 147 |
const OPTION_BE_ALLOW_IMPORTS = [ 'wpda_be_wpda_allow_imports', 'on' ]; // Allow to import data (main only). |
| 148 |
/** |
| 149 |
* Option wpda_be_confirm_export and it's default value |
| 150 |
*/ |
| 151 |
const OPTION_BE_CONFIRM_EXPORT = [ 'wpda_be_confirm_export', 'on' ]; // Ask for confirmation before exporting. |
| 152 |
/** |
| 153 |
* Option wpda_be_confirm_view and it's default value |
| 154 |
*/ |
| 155 |
const OPTION_BE_CONFIRM_VIEW = [ 'wpda_be_confirm_view', 'on' ]; // Ask for confirmation before viewing. |
| 156 |
/** |
| 157 |
* Option wpda_be_pagination and it's default value |
| 158 |
*/ |
| 159 |
const OPTION_BE_PAGINATION = [ 'wpda_be_pagination', '10' ]; |
| 160 |
/** |
| 161 |
* Option wpda_be_remember_search and it's default value |
| 162 |
*/ |
| 163 |
const OPTION_BE_REMEMBER_SEARCH = [ 'wpda_be_remember_search', 'on' ]; |
| 164 |
/** |
| 165 |
* Option wpda_be_innodb_count and it's default value |
| 166 |
*/ |
| 167 |
const OPTION_BE_INNODB_COUNT = [ 'wpda_be_innodb_count', 100000 ]; |
| 168 |
/** |
| 169 |
* Option wpda_be_design_mode and it's default value |
| 170 |
*/ |
| 171 |
const OPTION_BE_DESIGN_MODE = [ 'wpda_be_design_mode', 'advanced' ]; // Default design mode (basic/advanced). |
| 172 |
/** |
| 173 |
* Option wpda_be_text_wrap_switch and it's default value |
| 174 |
*/ |
| 175 |
const OPTION_BE_TEXT_WRAP_SWITCH = [ 'wpda_be_text_wrap_switch', 'off' ]; |
| 176 |
/** |
| 177 |
* Option wpda_be_text_wrap and it's default value |
| 178 |
*/ |
| 179 |
const OPTION_BE_TEXT_WRAP = [ 'wpda_be_text_wrap', 400 ]; |
| 180 |
|
| 181 |
// Front-end options. |
| 182 |
/** |
| 183 |
* Option wpda_fe_load_bootstrap and it's default value |
| 184 |
*/ |
| 185 |
const OPTION_FE_LOAD_BOOTSTRAP = [ 'wpda_fe_load_bootstrap', 'off' ]; |
| 186 |
/** |
| 187 |
* Option wpda_fe_load_datatables and it's default value |
| 188 |
*/ |
| 189 |
const OPTION_FE_LOAD_DATATABLES = [ 'wpda_fe_load_datatables', 'on' ]; |
| 190 |
/** |
| 191 |
* Option wpda_fe_load_datatables_response and it's default value |
| 192 |
*/ |
| 193 |
const OPTION_FE_LOAD_DATATABLES_RESPONSE = [ 'wpda_fe_load_datatables_response', 'on' ]; |
| 194 |
/** |
| 195 |
* Option wpda_fe_table_access and it's default value |
| 196 |
*/ |
| 197 |
const OPTION_FE_TABLE_ACCESS = [ 'wpda_fe_table_access', 'select' ]; // Values: hide, show, select. |
| 198 |
/** |
| 199 |
* Option wpda_fe_table_access_selected and it's default value |
| 200 |
*/ |
| 201 |
const OPTION_FE_TABLE_ACCESS_SELECTED = [ 'wpda_fe_table_access_selected', '' ]; // Tables authorized in WPDA. |
| 202 |
|
| 203 |
// Manage Repository options. |
| 204 |
/** |
| 205 |
* Option wpda_mr_keep_backup_tables and it's default value |
| 206 |
*/ |
| 207 |
const OPTION_MR_KEEP_BACKUP_TABLES = [ 'wpda_mr_keep_backup_tables', 'on' ]; |
| 208 |
|
| 209 |
// Data Backup options. |
| 210 |
/** |
| 211 |
* Option wpda_db_local_path and it's default value |
| 212 |
*/ |
| 213 |
const OPTION_DB_LOCAL_PATH = [ 'wpda_db_local_path', '' ]; |
| 214 |
/** |
| 215 |
* Option wpda_db_dropbox_path and it's default value |
| 216 |
*/ |
| 217 |
const OPTION_DB_DROPBOX_PATH = [ 'wpda_db_dropbox_path', '/wp-data-access/' ]; |
| 218 |
|
| 219 |
/** |
| 220 |
* List of plugin tables |
| 221 |
*/ |
| 222 |
const WPDA_TABLES = [ |
| 223 |
'menu_items' => true, |
| 224 |
'table_design' => true, |
| 225 |
]; |
| 226 |
const WPDP_TABLES = [ |
| 227 |
'wpdp_project' => true, |
| 228 |
'wpdp_page' => true, |
| 229 |
'wpdp_table' => true, |
| 230 |
]; |
| 231 |
|
| 232 |
// Option values once queried from wp_options are stored in an array for re-use during request to prevent |
| 233 |
// executing same query on wp_options multiple times. |
| 234 |
/** |
| 235 |
* Options cache array |
| 236 |
* |
| 237 |
* @var array |
| 238 |
*/ |
| 239 |
static protected $option_cache = []; |
| 240 |
|
| 241 |
/** |
| 242 |
* List containing all WordPress tables |
| 243 |
* |
| 244 |
* @var array |
| 245 |
*/ |
| 246 |
static protected $wp_tables = []; |
| 247 |
|
| 248 |
/** |
| 249 |
* Translated table label |
| 250 |
* |
| 251 |
* Returns a translated table label. The label depends on the type of table. Provided through a function to |
| 252 |
* support internationalization. |
| 253 |
* |
| 254 |
* @since 1.0.0 |
| 255 |
* |
| 256 |
* @param string $table_type Table type (use WPDA constants). |
| 257 |
* @return string Translated table type. |
| 258 |
*/ |
| 259 |
public static function get_table_type_text( $table_type ) { |
| 260 |
|
| 261 |
switch ( $table_type ) { |
| 262 |
|
| 263 |
case self::TABLE_TYPE_WP: |
| 264 |
return __( 'WordPress table', 'wp-data-access' ); |
| 265 |
|
| 266 |
case self::TABLE_TYPE_WPDA: |
| 267 |
return __( 'plugin table', 'wp-data-access' ); |
| 268 |
|
| 269 |
default: |
| 270 |
return $table_type; |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Get plugin option values |
| 278 |
* |
| 279 |
* Get value for pluginoption saved in wp_options. If option is not found in $wpdb->options the default value |
| 280 |
* for that option is returned. Option values once taken from $wpdb->options are cached to prevent execution |
| 281 |
* of the same query multiple times during teh processing of a request. |
| 282 |
* |
| 283 |
* @since 1.0.0 |
| 284 |
* |
| 285 |
* @param array $option OPTION_ARRAY (use class constants). |
| 286 |
* @return mixed Value for OPTION_ARRAY ($option): (1) cached value (2) wp_options value (3) default value. |
| 287 |
*/ |
| 288 |
public static function get_option( $option ) { |
| 289 |
|
| 290 |
if ( isset( self::$option_cache[ $option[0] ] ) ) { |
| 291 |
return self::$option_cache[ $option[0] ]; // Re-use cached value. |
| 292 |
} |
| 293 |
|
| 294 |
$option_value = get_option( $option[0] ); |
| 295 |
if ( ! $option_value ) { |
| 296 |
// Option not found in wp_options: save default value for re-use. |
| 297 |
self::$option_cache[ $option[0] ] = $option[1]; |
| 298 |
} else { |
| 299 |
// Option found in wp_options: save for re-use. |
| 300 |
self::$option_cache[ $option[0] ] = $option_value; |
| 301 |
} |
| 302 |
|
| 303 |
return self::$option_cache[ $option[0] ]; // Return saved value. |
| 304 |
|
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Delete all plugin options from table wp_options |
| 309 |
* |
| 310 |
* @since 1.0.0 |
| 311 |
*/ |
| 312 |
public static function clear_all_options() { |
| 313 |
|
| 314 |
global $wpdb; |
| 315 |
|
| 316 |
$wpdb->query( |
| 317 |
" |
| 318 |
DELETE FROM wp_options |
| 319 |
WHERE option_name LIKE 'wpda_%' |
| 320 |
" |
| 321 |
); // db call ok; no-cache ok. |
| 322 |
|
| 323 |
self::$option_cache = []; // Reset cache. |
| 324 |
|
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* Load all WordPress tables |
| 329 |
* |
| 330 |
* @since 1.1.0 |
| 331 |
*/ |
| 332 |
public static function load_wp_tables() { |
| 333 |
|
| 334 |
if ( 0 === count( self::$wp_tables ) ) { |
| 335 |
try { |
| 336 |
global $wpdb; |
| 337 |
|
| 338 |
if ( ! is_multisite() ) { |
| 339 |
foreach ( $wpdb->tables( 'all', true ) as $table ) { |
| 340 |
self::$wp_tables[ $table ] = $table; |
| 341 |
} |
| 342 |
} else { |
| 343 |
$query = "select blog_id from {$wpdb->blogs}"; |
| 344 |
$blogs = $wpdb->get_results( $query, 'ARRAY_N' ); |
| 345 |
foreach ( $blogs as $blog ) { |
| 346 |
foreach ( $wpdb->tables( $blog === reset( $blogs ) ? 'all' : 'blog', true, $blog[0] ) as $table ) { |
| 347 |
self::$wp_tables[ $table ] = $table; |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
return true; |
| 353 |
} catch ( \Exception $e ) { |
| 354 |
wp_die( 'ERROR: ' . $e->getMessage() ); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
} |
| 359 |
|
| 360 |
/** |
| 361 |
* Checks if a table is a WordPress table |
| 362 |
* |
| 363 |
* @since 1.1.0 |
| 364 |
* |
| 365 |
* @param string $table_name Table name. |
| 366 |
* @return bool TRUE = WordPress table |
| 367 |
*/ |
| 368 |
public static function is_wp_table( $table_name ) { |
| 369 |
|
| 370 |
self::load_wp_tables(); |
| 371 |
|
| 372 |
if ( 0 === count( self::$wp_tables ) ) { |
| 373 |
return false; |
| 374 |
} |
| 375 |
|
| 376 |
return isset( self::$wp_tables[ $table_name ] ); |
| 377 |
|
| 378 |
} |
| 379 |
|
| 380 |
/** |
| 381 |
* List containing all WordPress tables |
| 382 |
* |
| 383 |
* @since 1.1.0 |
| 384 |
* |
| 385 |
* @return array |
| 386 |
*/ |
| 387 |
public static function get_wp_tables() { |
| 388 |
|
| 389 |
self::load_wp_tables(); |
| 390 |
|
| 391 |
if ( 0 === count( self::$wp_tables ) ) { |
| 392 |
wp_die( 'ERROR: No WordPress table found???' ); |
| 393 |
} |
| 394 |
|
| 395 |
return self::$wp_tables; |
| 396 |
|
| 397 |
} |
| 398 |
|
| 399 |
/** |
| 400 |
* Returns a list of all plugin tables |
| 401 |
* |
| 402 |
* @since 1.0.0 |
| 403 |
* |
| 404 |
* @return array List of plugin tables |
| 405 |
*/ |
| 406 |
public static function get_wpda_tables() { |
| 407 |
|
| 408 |
global $wpdb; |
| 409 |
$wpda_tables = []; |
| 410 |
|
| 411 |
foreach ( self::WPDA_TABLES as $key => $value ) { |
| 412 |
$wpda_tables[] = $wpdb->prefix . self::get_option( self::OPTION_WPDA_PREFIX ) . $key; |
| 413 |
} |
| 414 |
|
| 415 |
foreach ( self::WPDP_TABLES as $key => $value ) { |
| 416 |
$wpda_tables[] = $wpdb->prefix . $key; |
| 417 |
} |
| 418 |
|
| 419 |
return $wpda_tables; |
| 420 |
|
| 421 |
} |
| 422 |
|
| 423 |
/** |
| 424 |
* Return the default value for a plugin option |
| 425 |
* |
| 426 |
* @since 1.0.0 |
| 427 |
* |
| 428 |
* @param array $option OPTION_ARRAY (use class constants). |
| 429 |
* @return mixed Default value for OPTION_ARRAY ($option). |
| 430 |
*/ |
| 431 |
public static function get_option_default( $option ) { |
| 432 |
|
| 433 |
return $option[1]; // Default option value. |
| 434 |
|
| 435 |
} |
| 436 |
|
| 437 |
/** |
| 438 |
* Checks if table is plugin table |
| 439 |
* |
| 440 |
* NOTE |
| 441 |
* Variable $phpdoc_supported_solution is a temporary variable that does not add any functionality to this |
| 442 |
* function. It only serves the purpose to get class WPDA in the documentation!!! If the isset statement in |
| 443 |
* the return is performed directly on self::WPDA_TABLES, class WPDA will not appear in the phpdoc generated |
| 444 |
* documentation. To avoid class WPDA to be undocumented, we use $phpdoc_supported_solution. |
| 445 |
* |
| 446 |
* @since 1.0.0 |
| 447 |
* |
| 448 |
* @param string $real_table_name Table name to be checked. |
| 449 |
* @return bool TRUE = $table_name is a WPDA table, FALSE = $table_name is not a WPDA table. |
| 450 |
*/ |
| 451 |
public static function is_wpda_table( $real_table_name ) { |
| 452 |
|
| 453 |
if ( null === $real_table_name ) { |
| 454 |
return false; |
| 455 |
} |
| 456 |
|
| 457 |
global $wpdb; |
| 458 |
|
| 459 |
$phpdoc_supported_solution = self::WPDA_TABLES; // DO NOT DELETE THIS TO MAKE THE CODE SIMPLER!!! (read). |
| 460 |
$phpdoc_supported_solution_wpdp = self::WPDP_TABLES; // DO NOT DELETE THIS TO MAKE THE CODE SIMPLER!!! (read). |
| 461 |
|
| 462 |
return |
| 463 |
( |
| 464 |
isset( |
| 465 |
$phpdoc_supported_solution[ substr( |
| 466 |
$real_table_name, |
| 467 |
strlen( $wpdb->prefix . self::get_option( self::OPTION_WPDA_PREFIX ) ) |
| 468 |
) ] |
| 469 |
) && |
| 470 |
( |
| 471 |
$wpdb->prefix . self::get_option( self::OPTION_WPDA_PREFIX ) === |
| 472 |
substr( $real_table_name, 0, strlen( $wpdb->prefix . self::get_option( self::OPTION_WPDA_PREFIX ) ) ) |
| 473 |
) |
| 474 |
) || |
| 475 |
( |
| 476 |
isset( |
| 477 |
$phpdoc_supported_solution_wpdp[ substr( |
| 478 |
$real_table_name, |
| 479 |
strlen( $wpdb->prefix ) |
| 480 |
) ] |
| 481 |
) && |
| 482 |
( |
| 483 |
$wpdb->prefix === substr( $real_table_name, 0, strlen( $wpdb->prefix ) ) |
| 484 |
) |
| 485 |
); |
| 486 |
|
| 487 |
} |
| 488 |
|
| 489 |
/** |
| 490 |
* Save plugin option |
| 491 |
* |
| 492 |
* Saves a plugin option in $wpdb->options. |
| 493 |
* |
| 494 |
* @since 1.0.0 |
| 495 |
* |
| 496 |
* @param array $option OPTION_ARRAYS (use class constants). |
| 497 |
* @param mixed $value Value to be saved for $option. If null set to default. |
| 498 |
*/ |
| 499 |
public static function set_option( $option, $value = null ) { |
| 500 |
|
| 501 |
try { |
| 502 |
|
| 503 |
if ( is_null( $value ) ) { |
| 504 |
$option_value = $option[1]; // Set option to default. |
| 505 |
} else { |
| 506 |
$option_value = $value; // Set option value. |
| 507 |
} |
| 508 |
|
| 509 |
update_option( $option[0], $option_value ); // Save option value in wp_options. |
| 510 |
|
| 511 |
self::$option_cache[ $option[0] ] = $option_value; // Save for re-use. |
| 512 |
|
| 513 |
} catch ( \Exception $e ) { |
| 514 |
|
| 515 |
die( 'ERROR: ' . esc_html( $e->errorMessage() ) ); |
| 516 |
|
| 517 |
} |
| 518 |
|
| 519 |
} |
| 520 |
|
| 521 |
/** |
| 522 |
* Simplify data type for simple forms |
| 523 |
* |
| 524 |
* Data types used in plugin a re simplified for simple form usage. |
| 525 |
* |
| 526 |
* @since 1.0.0 |
| 527 |
* |
| 528 |
* @see \WPDataAccess\Simple_Form\WPDA_Simple_Form |
| 529 |
* |
| 530 |
* @param string $arg Data type as known to the MySQL database. |
| 531 |
* @return string Simplified data type (mainly used to recognize when to use quotes). |
| 532 |
*/ |
| 533 |
public static function get_type( $arg ) { |
| 534 |
|
| 535 |
switch ( $arg ) { |
| 536 |
|
| 537 |
case 'tinyint': |
| 538 |
case 'smallint': |
| 539 |
case 'mediumint': |
| 540 |
case 'int': |
| 541 |
case 'bigint': |
| 542 |
case 'float': |
| 543 |
case 'double': |
| 544 |
case 'decimal': |
| 545 |
case 'year': |
| 546 |
return 'number'; |
| 547 |
|
| 548 |
case 'date': |
| 549 |
case 'datetime': |
| 550 |
case 'timestamp': |
| 551 |
return 'date'; |
| 552 |
|
| 553 |
case 'time': |
| 554 |
return 'time'; |
| 555 |
|
| 556 |
case 'enum': |
| 557 |
return 'enum'; |
| 558 |
|
| 559 |
case 'set': |
| 560 |
return 'set'; |
| 561 |
|
| 562 |
default: |
| 563 |
return 'string'; |
| 564 |
|
| 565 |
} |
| 566 |
|
| 567 |
} |
| 568 |
|
| 569 |
/** |
| 570 |
* Log a message in the database |
| 571 |
* |
| 572 |
* Use this method to log messages to the database. |
| 573 |
* |
| 574 |
* NOTE Don't use $wpdb->insert! You'll miss a lot of information... |
| 575 |
* |
| 576 |
* @since 2.0.7 |
| 577 |
* |
| 578 |
* @param $log_id string Id to identify/find logged data. |
| 579 |
* @param $log_type string Possible values: 'FATAL', 'ERROR', 'WARN', 'INFO', 'DEBUG', 'TRACE' |
| 580 |
* @param $log_msg string Any text (max length 4096kb). |
| 581 |
*/ |
| 582 |
public static function log( $log_id, $log_type, $log_msg ) { |
| 583 |
global $wpdb; |
| 584 |
|
| 585 |
$sql = |
| 586 |
$wpdb->prepare( |
| 587 |
'INSERT INTO ' . $wpdb->prefix . WPDA::get_option( WPDA::OPTION_WPDA_PREFIX ) . |
| 588 |
'logging (log_time, log_id, log_type, log_msg) VALUES (now(), %s, %s, %s)' |
| 589 |
, $log_id |
| 590 |
, $log_type |
| 591 |
, $log_msg |
| 592 |
); |
| 593 |
$wpdb->query($sql); |
| 594 |
|
| 595 |
} |
| 596 |
|
| 597 |
/** |
| 598 |
* Get user role |
| 599 |
* |
| 600 |
* @since 2.0.8 |
| 601 |
* |
| 602 |
* @return mixed Current user roles or FALSE if not logged in. |
| 603 |
*/ |
| 604 |
public static function get_current_user_roles() { |
| 605 |
if( is_user_logged_in() ) { |
| 606 |
$user = wp_get_current_user(); |
| 607 |
if ( ! is_array( $user->roles ) ) { |
| 608 |
return ( array ) $user->roles; |
| 609 |
} else { |
| 610 |
return $user->roles; |
| 611 |
} |
| 612 |
} else { |
| 613 |
return false; |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
/** |
| 618 |
* Get user capability |
| 619 |
* |
| 620 |
* @since 2.0.8 |
| 621 |
* |
| 622 |
* @return mixed Current users first capability or FALSE if not logged in. |
| 623 |
*/ |
| 624 |
public static function get_current_user_capability() { |
| 625 |
if( is_user_logged_in() ) { |
| 626 |
$user = wp_get_current_user(); |
| 627 |
$allcaps = []; |
| 628 |
foreach ( $user->allcaps as $key => $val ) { |
| 629 |
array_push( $allcaps, $key ); |
| 630 |
} |
| 631 |
return $allcaps[0]; |
| 632 |
} else { |
| 633 |
return false; |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
/** |
| 638 |
* Convert memory value to integer. |
| 639 |
* |
| 640 |
* @since 2.0.8 |
| 641 |
* |
| 642 |
* @param $memory_value string Memory value (eg 128M) |
| 643 |
* @return integer Converted value in decimal |
| 644 |
*/ |
| 645 |
public static function convert_memory_to_decimal( $memory_value ) { |
| 646 |
if (preg_match('/^(\d+)(.)$/', $memory_value, $matches)) { |
| 647 |
if ($matches[2] == 'G') { |
| 648 |
return $matches[1] * 1024 * 1024 * 1024; |
| 649 |
} else if ($matches[2] == 'M') { |
| 650 |
return $matches[1] * 1024 * 1024; |
| 651 |
} else if ($matches[2] == 'K') { |
| 652 |
return $matches[1] * 1024; |
| 653 |
} |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
} |
| 658 |
|
| 659 |
} |
| 660 |
|