class-wc-helper-options.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit;
  4. }
  5. /**
  6. * WC_Helper_Options Class
  7. *
  8. * An interface to the woocommerce_helper_data entry in the wp_options table.
  9. */
  10. class WC_Helper_Options {
  11. /**
  12. * The option name used to store the helper data.
  13. *
  14. * @var string
  15. */
  16. private static $option_name = 'woocommerce_helper_data';
  17. /**
  18. * Update an option by key
  19. *
  20. * All helper options are grouped in a single options entry. This method
  21. * is not thread-safe, use with caution.
  22. *
  23. * @param string $key The key to update.
  24. * @param mixed $value The new option value.
  25. *
  26. * @return bool True if the option has been updated.
  27. */
  28. public static function update( $key, $value ) {
  29. $options = get_option( self::$option_name, array() );
  30. $options[ $key ] = $value;
  31. return update_option( self::$option_name, $options, true );
  32. }
  33. /**
  34. * Get an option by key
  35. *
  36. * @see self::update
  37. *
  38. * @param string $key The key to fetch.
  39. * @param mixed $default The default option to return if the key does not exist.
  40. *
  41. * @return mixed An option or the default.
  42. */
  43. public static function get( $key, $default = false ) {
  44. $options = get_option( self::$option_name, array() );
  45. if ( array_key_exists( $key, $options ) ) {
  46. return $options[ $key ];
  47. }
  48. return $default;
  49. }
  50. }