Нет описания

DataStorage.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Initializes a temporary data storage engine used by core in various capacities.
  10. */
  11. class PUM_Utils_DataStorage {
  12. /**
  13. * Retrieves stored data by key.
  14. *
  15. * Given a key, get the information from the database directly.
  16. *
  17. * @param string $key The stored option key.
  18. * @param null|mixed $default Optional. A default value to retrieve should `$value` be empty.
  19. * Default null.
  20. *
  21. * @return mixed|false The stored data, value of `$default` if not null, otherwise false.
  22. */
  23. public static function get( $key, $default = null ) {
  24. global $wpdb;
  25. $value = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = '%s'", $key ) );
  26. if ( empty( $value ) && ! is_null( $default ) ) {
  27. return $default;
  28. }
  29. return empty( $value ) ? false : maybe_unserialize( $value );
  30. }
  31. /**
  32. * Write some data based on key and value.
  33. *
  34. * @param string $key The option_name.
  35. * @param mixed $value The value to store.
  36. */
  37. public static function write( $key, $value ) {
  38. global $wpdb;
  39. $value = maybe_serialize( $value );
  40. $data = array(
  41. 'option_name' => $key,
  42. 'option_value' => $value,
  43. 'autoload' => 'no',
  44. );
  45. $formats = self::get_data_formats( $value );
  46. $wpdb->replace( $wpdb->options, $data, $formats );
  47. }
  48. /**
  49. * Derives the formats array based on the type of $value.
  50. *
  51. * @param mixed $value Value to store.
  52. *
  53. * @return array Formats array. First and last values will always be string ('%s').
  54. */
  55. public static function get_data_formats( $value ) {
  56. switch ( gettype( $value ) ) {
  57. case 'integer':
  58. $formats = array( '%s', '%d', '%s' );
  59. break;
  60. case 'double':
  61. $formats = array( '%s', '%f', '%s' );
  62. break;
  63. default:
  64. case 'string':
  65. $formats = array( '%s', '%s', '%s' );
  66. break;
  67. }
  68. return $formats;
  69. }
  70. /**
  71. * Deletes a piece of stored data by key.
  72. *
  73. * @param string $key The stored option name to delete.
  74. *
  75. * @return int|false The number of rows deleted, or false on error.
  76. */
  77. public static function delete( $key ) {
  78. global $wpdb;
  79. return $wpdb->delete( $wpdb->options, array( 'option_name' => $key ) );
  80. }
  81. /**
  82. * Deletes all options matching a given RegEx pattern.
  83. *
  84. * @param string $pattern Pattern to match against option keys.
  85. *
  86. * @return int|false The number of rows deleted, or false on error.
  87. */
  88. public static function delete_by_match( $pattern ) {
  89. global $wpdb;
  90. // Double check to make sure the batch_id got included before proceeding.
  91. if ( "^[0-9a-z\\_]+" !== $pattern && ! empty( $pattern ) ) {
  92. $query = "DELETE FROM $wpdb->options WHERE option_name REGEXP %s";
  93. $result = $wpdb->query( $wpdb->prepare( $query, $pattern ) );
  94. } else {
  95. $result = false;
  96. }
  97. return $result;
  98. }
  99. }