CMB2_Option

Handles getting/setting of values to an option array for a specific option key.

Defined (2)

The class is defined in the following location(s).

/includes/libraries/metabox/includes/CMB2_Options.php  
  1. class CMB2_Option { 
  2.  
  3. /** 
  4. * Options array 
  5. * @var array 
  6. */ 
  7. protected $options = array(); 
  8.  
  9. /** 
  10. * Current option key 
  11. * @var string 
  12. */ 
  13. protected $key = ''; 
  14.  
  15. /** 
  16. * Initiate option object 
  17. * @param string $option_key Option key where data will be saved. 
  18. * Leave empty for temporary data store. 
  19. * @since 2.0.0 
  20. */ 
  21. public function __construct( $option_key = '' ) { 
  22. $this->key = ! empty( $option_key ) ? $option_key : ''; 
  23.  
  24. /** 
  25. * Delete the option from the db 
  26. * @since 2.0.0 
  27. * @return bool Delete success or failure 
  28. */ 
  29. public function delete_option() { 
  30. $deleted = $this->key ? delete_option( $this->key ) : true; 
  31. $this->options = $deleted ? array() : $this->options; 
  32. return $this->options; 
  33.  
  34. /** 
  35. * Removes an option from an option array 
  36. * @since 1.0.1 
  37. * @param string $field_id Option array field key 
  38. * @return array Modified options 
  39. */ 
  40. public function remove( $field_id, $resave = false ) { 
  41.  
  42. $this->get_options(); 
  43.  
  44. if ( isset( $this->options[ $field_id ] ) ) { 
  45. unset( $this->options[ $field_id ] ); 
  46.  
  47. if ( $resave ) { 
  48. $this->set(); 
  49.  
  50. return $this->options; 
  51.  
  52. /** 
  53. * Retrieves an option from an option array 
  54. * @since 1.0.1 
  55. * @param string $field_id Option array field key 
  56. * @param mixed $default Fallback value for the option 
  57. * @return array Requested field or default 
  58. */ 
  59. public function get( $field_id, $default = false ) { 
  60. $opts = $this->get_options(); 
  61.  
  62. if ( 'all' == $field_id ) { 
  63. return $opts; 
  64. } elseif ( array_key_exists( $field_id, $opts ) ) { 
  65. return false !== $opts[ $field_id ] ? $opts[ $field_id ] : $default; 
  66.  
  67. return $default; 
  68.  
  69. /** 
  70. * Updates Option data 
  71. * @since 1.0.1 
  72. * @param string $field_id Option array field key 
  73. * @param mixed $value Value to update data with 
  74. * @param bool $resave Whether to re-save the data 
  75. * @param bool $single Whether data should not be an array 
  76. * @return boolean Return status of update 
  77. */ 
  78. public function update( $field_id, $value = '', $resave = false, $single = true ) { 
  79. $this->get_options(); 
  80.  
  81. if ( true !== $field_id ) { 
  82.  
  83. if ( ! $single ) { 
  84. // If multiple, add to array 
  85. $this->options[ $field_id ][] = $value; 
  86. } else { 
  87. $this->options[ $field_id ] = $value; 
  88.  
  89.  
  90. if ( $resave || true === $field_id ) { 
  91. return $this->set(); 
  92.  
  93. return true; 
  94.  
  95. /** 
  96. * Saves the option array 
  97. * Needs to be run after finished using remove/update_option 
  98. * @uses apply_filters() Calls 'cmb2_override_option_save_{$this->key}' hook 
  99. * to allow overwriting the option value to be stored. 
  100. * @since 1.0.1 
  101. * @param array $options Optional options to override 
  102. * @return bool Success/Failure 
  103. */ 
  104. public function set( $options = array() ) { 
  105. $this->options = ! empty( $options ) || empty( $options ) && empty( $this->key ) 
  106. ? $options 
  107. : $this->options; 
  108.  
  109. if ( empty( $this->key ) ) { 
  110. return false; 
  111.  
  112. $test_save = apply_filters( "cmb2_override_option_save_{$this->key}", 'cmb2_no_override_option_save', $this->options, $this ); 
  113.  
  114. if ( 'cmb2_no_override_option_save' !== $test_save ) { 
  115. return $test_save; 
  116.  
  117. // If no override, update the option 
  118. return update_option( $this->key, $this->options ); 
  119.  
  120. /** 
  121. * Retrieve option value based on name of option. 
  122. * @uses apply_filters() Calls 'cmb2_override_option_get_{$this->key}' hook to allow 
  123. * overwriting the option value to be retrieved. 
  124. * @since 1.0.1 
  125. * @param mixed $default Optional. Default value to return if the option does not exist. 
  126. * @return mixed Value set for the option. 
  127. */ 
  128. public function get_options( $default = null ) { 
  129. if ( empty( $this->options ) && ! empty( $this->key ) ) { 
  130.  
  131. $test_get = apply_filters( "cmb2_override_option_get_{$this->key}", 'cmb2_no_override_option_get', $default, $this ); 
  132.  
  133. if ( 'cmb2_no_override_option_get' !== $test_get ) { 
  134. $this->options = $test_get; 
  135. } else { 
  136. // If no override, get the option 
  137. $this->options = get_option( $this->key, $default ); 
  138.  
  139. return (array) $this->options; 
  140.  
/vendor/wordimpress/maps-builder-core/includes/libraries/metabox/includes/CMB2_Options.php  
  1. class CMB2_Option { 
  2.  
  3. /** 
  4. * Options array 
  5. * @var array 
  6. */ 
  7. protected $options = array(); 
  8.  
  9. /** 
  10. * Current option key 
  11. * @var string 
  12. */ 
  13. protected $key = ''; 
  14.  
  15. /** 
  16. * Initiate option object 
  17. * @param string $option_key Option key where data will be saved. 
  18. * Leave empty for temporary data store. 
  19. * @since 2.0.0 
  20. */ 
  21. public function __construct( $option_key = '' ) { 
  22. $this->key = ! empty( $option_key ) ? $option_key : ''; 
  23.  
  24. /** 
  25. * Delete the option from the db 
  26. * @since 2.0.0 
  27. * @return bool Delete success or failure 
  28. */ 
  29. public function delete_option() { 
  30. $deleted = $this->key ? delete_option( $this->key ) : true; 
  31. $this->options = $deleted ? array() : $this->options; 
  32. return $this->options; 
  33.  
  34. /** 
  35. * Removes an option from an option array 
  36. * @since 1.0.1 
  37. * @param string $field_id Option array field key 
  38. * @return array Modified options 
  39. */ 
  40. public function remove( $field_id, $resave = false ) { 
  41.  
  42. $this->get_options(); 
  43.  
  44. if ( isset( $this->options[ $field_id ] ) ) { 
  45. unset( $this->options[ $field_id ] ); 
  46.  
  47. if ( $resave ) { 
  48. $this->set(); 
  49.  
  50. return $this->options; 
  51.  
  52. /** 
  53. * Retrieves an option from an option array 
  54. * @since 1.0.1 
  55. * @param string $field_id Option array field key 
  56. * @param mixed $default Fallback value for the option 
  57. * @return array Requested field or default 
  58. */ 
  59. public function get( $field_id, $default = false ) { 
  60. $opts = $this->get_options(); 
  61.  
  62. if ( 'all' == $field_id ) { 
  63. return $opts; 
  64. } elseif ( array_key_exists( $field_id, $opts ) ) { 
  65. return false !== $opts[ $field_id ] ? $opts[ $field_id ] : $default; 
  66.  
  67. return $default; 
  68.  
  69. /** 
  70. * Updates Option data 
  71. * @since 1.0.1 
  72. * @param string $field_id Option array field key 
  73. * @param mixed $value Value to update data with 
  74. * @param bool $resave Whether to re-save the data 
  75. * @param bool $single Whether data should not be an array 
  76. * @return boolean Return status of update 
  77. */ 
  78. public function update( $field_id, $value = '', $resave = false, $single = true ) { 
  79. $this->get_options(); 
  80.  
  81. if ( true !== $field_id ) { 
  82.  
  83. if ( ! $single ) { 
  84. // If multiple, add to array 
  85. $this->options[ $field_id ][] = $value; 
  86. } else { 
  87. $this->options[ $field_id ] = $value; 
  88.  
  89.  
  90. if ( $resave || true === $field_id ) { 
  91. return $this->set(); 
  92.  
  93. return true; 
  94.  
  95. /** 
  96. * Saves the option array 
  97. * Needs to be run after finished using remove/update_option 
  98. * @uses apply_filters() Calls 'cmb2_override_option_save_{$this->key}' hook 
  99. * to allow overwriting the option value to be stored. 
  100. * @since 1.0.1 
  101. * @param array $options Optional options to override 
  102. * @return bool Success/Failure 
  103. */ 
  104. public function set( $options = array() ) { 
  105. $this->options = ! empty( $options ) || empty( $options ) && empty( $this->key ) 
  106. ? $options 
  107. : $this->options; 
  108.  
  109. if ( empty( $this->key ) ) { 
  110. return false; 
  111.  
  112. $test_save = apply_filters( "cmb2_override_option_save_{$this->key}", 'cmb2_no_override_option_save', $this->options, $this ); 
  113.  
  114. if ( 'cmb2_no_override_option_save' !== $test_save ) { 
  115. return $test_save; 
  116.  
  117. // If no override, update the option 
  118. return update_option( $this->key, $this->options ); 
  119.  
  120. /** 
  121. * Retrieve option value based on name of option. 
  122. * @uses apply_filters() Calls 'cmb2_override_option_get_{$this->key}' hook to allow 
  123. * overwriting the option value to be retrieved. 
  124. * @since 1.0.1 
  125. * @param mixed $default Optional. Default value to return if the option does not exist. 
  126. * @return mixed Value set for the option. 
  127. */ 
  128. public function get_options( $default = null ) { 
  129. if ( empty( $this->options ) && ! empty( $this->key ) ) { 
  130.  
  131. $test_get = apply_filters( "cmb2_override_option_get_{$this->key}", 'cmb2_no_override_option_get', $default, $this ); 
  132.  
  133. if ( 'cmb2_no_override_option_get' !== $test_get ) { 
  134. $this->options = $test_get; 
  135. } else { 
  136. // If no override, get the option 
  137. $this->options = get_option( $this->key, $default ); 
  138.  
  139. return (array) $this->options; 
  140.