NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Options.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * Container for an array of options.
  4. */
  5. class scbOptions {
  6. /**
  7. * The option name.
  8. * @var string
  9. */
  10. protected $key;
  11. /**
  12. * The default values.
  13. * @var array
  14. */
  15. protected $defaults;
  16. /**
  17. * Used by WP hooks.
  18. * @var null
  19. */
  20. public $wp_filter_id;
  21. /**
  22. * Create a new set of options.
  23. *
  24. * @param string $key Option name.
  25. * @param string $file Reference to main plugin file.
  26. * @param array $defaults (optional) An associative array of default values.
  27. *
  28. * @return void
  29. */
  30. public function __construct( $key, $file, $defaults = array() ) {
  31. $this->key = $key;
  32. $this->defaults = $defaults;
  33. if ( $file ) {
  34. scbUtil::add_activation_hook( $file, array( $this, '_activation' ) );
  35. scbUtil::add_uninstall_hook( $file, array( $this, 'delete' ) );
  36. }
  37. }
  38. /**
  39. * Returns option name.
  40. *
  41. * @return string
  42. */
  43. public function get_key() {
  44. return $this->key;
  45. }
  46. /**
  47. * Get option values for one or all fields.
  48. *
  49. * @param string|array $field (optional) The field to get.
  50. * @param mixed $default (optional) The value returned when the key is not found.
  51. *
  52. * @return mixed Whatever is in those fields.
  53. */
  54. public function get( $field = null, $default = null ) {
  55. $data = array_merge( $this->defaults, get_option( $this->key, array() ) );
  56. return scbForms::get_value( $field, $data, $default );
  57. }
  58. /**
  59. * Get default values for one or all fields.
  60. *
  61. * @param string|array $field (optional) The field to get.
  62. *
  63. * @return mixed Whatever is in those fields.
  64. */
  65. public function get_defaults( $field = null ) {
  66. return scbForms::get_value( $field, $this->defaults );
  67. }
  68. /**
  69. * Set all data fields, certain fields or a single field.
  70. *
  71. * @param string|array $field The field to update or an associative array.
  72. * @param mixed $value (optional) The new value ( ignored if $field is array ).
  73. *
  74. * @return void
  75. */
  76. public function set( $field, $value = '' ) {
  77. if ( is_array( $field ) ) {
  78. $newdata = $field;
  79. } else {
  80. $newdata = array( $field => $value );
  81. }
  82. $this->update( array_merge( $this->get(), $newdata ) );
  83. }
  84. /**
  85. * Reset option to defaults.
  86. *
  87. * @return void
  88. */
  89. public function reset() {
  90. $this->update( $this->defaults, false );
  91. }
  92. /**
  93. * Remove any keys that are not in the defaults array.
  94. *
  95. * @return void
  96. */
  97. public function cleanup() {
  98. $this->update( $this->get(), true );
  99. }
  100. /**
  101. * Update raw data.
  102. *
  103. * @param mixed $newdata
  104. * @param bool $clean (optional) Whether to remove unrecognized keys or not.
  105. *
  106. * @return void
  107. */
  108. public function update( $newdata, $clean = true ) {
  109. if ( $clean ) {
  110. $newdata = $this->_clean( $newdata );
  111. }
  112. update_option( $this->key, array_merge( $this->get(), $newdata ) );
  113. }
  114. /**
  115. * Delete the option.
  116. *
  117. * @return void
  118. */
  119. public function delete() {
  120. delete_option( $this->key );
  121. }
  122. //_____INTERNAL METHODS_____
  123. /**
  124. * Saves an extra query.
  125. *
  126. * @return void
  127. */
  128. public function _activation() {
  129. add_option( $this->key, $this->defaults );
  130. }
  131. /**
  132. * Keep only the keys defined in $this->defaults
  133. *
  134. * @param array $data
  135. *
  136. * @return array
  137. */
  138. private function _clean( $data ) {
  139. return wp_array_slice_assoc( $data, array_keys( $this->defaults ) );
  140. }
  141. private function &_get( $field, $data ) {
  142. }
  143. /**
  144. * Magic method: $options->field
  145. *
  146. * @param string|array $field The field to get.
  147. *
  148. * @return mixed
  149. */
  150. public function __get( $field ) {
  151. return $this->get( $field );
  152. }
  153. /**
  154. * Magic method: $options->field = $value
  155. *
  156. * @return void
  157. */
  158. public function __set( $field, $value ) {
  159. $this->set( $field, $value );
  160. }
  161. /**
  162. * Magic method: isset( $options->field )
  163. *
  164. * @return bool
  165. */
  166. public function __isset( $field ) {
  167. $data = $this->get();
  168. return isset( $data[ $field ] );
  169. }
  170. }