class-acf-data.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly.
  4. }
  5. if ( ! class_exists( 'ACF_Data' ) ) :
  6. class ACF_Data {
  7. /** @var string Unique identifier. */
  8. var $cid = '';
  9. /** @var array Storage for data. */
  10. var $data = array();
  11. /** @var array Storage for data aliases. */
  12. var $aliases = array();
  13. /** @var bool Enables unique data per site. */
  14. var $multisite = false;
  15. /**
  16. * __construct
  17. *
  18. * Sets up the class functionality.
  19. *
  20. * @date 9/1/19
  21. * @since 5.7.10
  22. *
  23. * @param array $data Optional data to set.
  24. * @return void
  25. */
  26. function __construct( $data = false ) {
  27. // Set cid.
  28. $this->cid = acf_uniqid();
  29. // Set data.
  30. if ( $data ) {
  31. $this->set( $data );
  32. }
  33. // Initialize.
  34. $this->initialize();
  35. }
  36. /**
  37. * initialize
  38. *
  39. * Called during constructor to setup class functionality.
  40. *
  41. * @date 9/1/19
  42. * @since 5.7.10
  43. *
  44. * @param void
  45. * @return void
  46. */
  47. function initialize() {
  48. // Do nothing.
  49. }
  50. /**
  51. * prop
  52. *
  53. * Sets a property for the given name and returns $this for chaining.
  54. *
  55. * @date 9/1/19
  56. * @since 5.7.10
  57. *
  58. * @param (string|array) $name The data name or an array of data.
  59. * @param mixed $value The data value.
  60. * @return ACF_Data
  61. */
  62. function prop( $name = '', $value = null ) {
  63. // Update property.
  64. $this->{$name} = $value;
  65. // Return this for chaining.
  66. return $this;
  67. }
  68. /**
  69. * _key
  70. *
  71. * Returns a key for the given name allowing aliasses to work.
  72. *
  73. * @date 18/1/19
  74. * @since 5.7.10
  75. *
  76. * @param type $var Description. Default.
  77. * @return type Description.
  78. */
  79. function _key( $name = '' ) {
  80. return isset( $this->aliases[ $name ] ) ? $this->aliases[ $name ] : $name;
  81. }
  82. /**
  83. * has
  84. *
  85. * Returns true if this has data for the given name.
  86. *
  87. * @date 9/1/19
  88. * @since 5.7.10
  89. *
  90. * @param string $name The data name.
  91. * @return boolean
  92. */
  93. function has( $name = '' ) {
  94. $key = $this->_key( $name );
  95. return isset( $this->data[ $key ] );
  96. }
  97. /**
  98. * is
  99. *
  100. * Similar to has() but does not check aliases.
  101. *
  102. * @date 7/2/19
  103. * @since 5.7.11
  104. *
  105. * @param type $var Description. Default.
  106. * @return type Description.
  107. */
  108. function is( $key = '' ) {
  109. return isset( $this->data[ $key ] );
  110. }
  111. /**
  112. * get
  113. *
  114. * Returns data for the given name of null if doesn't exist.
  115. *
  116. * @date 9/1/19
  117. * @since 5.7.10
  118. *
  119. * @param string $name The data name.
  120. * @return mixed
  121. */
  122. function get( $name = false ) {
  123. // Get all.
  124. if ( $name === false ) {
  125. return $this->data;
  126. // Get specific.
  127. } else {
  128. $key = $this->_key( $name );
  129. return isset( $this->data[ $key ] ) ? $this->data[ $key ] : null;
  130. }
  131. }
  132. /**
  133. * get_data
  134. *
  135. * Returns an array of all data.
  136. *
  137. * @date 9/1/19
  138. * @since 5.7.10
  139. *
  140. * @param void
  141. * @return array
  142. */
  143. function get_data() {
  144. return $this->data;
  145. }
  146. /**
  147. * set
  148. *
  149. * Sets data for the given name and returns $this for chaining.
  150. *
  151. * @date 9/1/19
  152. * @since 5.7.10
  153. *
  154. * @param (string|array) $name The data name or an array of data.
  155. * @param mixed $value The data value.
  156. * @return ACF_Data
  157. */
  158. function set( $name = '', $value = null ) {
  159. // Set multiple.
  160. if ( is_array( $name ) ) {
  161. $this->data = array_merge( $this->data, $name );
  162. // Set single.
  163. } else {
  164. $this->data[ $name ] = $value;
  165. }
  166. // Return this for chaining.
  167. return $this;
  168. }
  169. /**
  170. * append
  171. *
  172. * Appends data for the given name and returns $this for chaining.
  173. *
  174. * @date 9/1/19
  175. * @since 5.7.10
  176. *
  177. * @param mixed $value The data value.
  178. * @return ACF_Data
  179. */
  180. function append( $value = null ) {
  181. // Append.
  182. $this->data[] = $value;
  183. // Return this for chaining.
  184. return $this;
  185. }
  186. /**
  187. * remove
  188. *
  189. * Removes data for the given name.
  190. *
  191. * @date 9/1/19
  192. * @since 5.7.10
  193. *
  194. * @param string $name The data name.
  195. * @return ACF_Data
  196. */
  197. function remove( $name = '' ) {
  198. // Remove data.
  199. unset( $this->data[ $name ] );
  200. // Return this for chaining.
  201. return $this;
  202. }
  203. /**
  204. * reset
  205. *
  206. * Resets the data.
  207. *
  208. * @date 22/1/19
  209. * @since 5.7.10
  210. *
  211. * @param void
  212. * @return void
  213. */
  214. function reset() {
  215. $this->data = array();
  216. $this->aliases = array();
  217. }
  218. /**
  219. * count
  220. *
  221. * Returns the data count.
  222. *
  223. * @date 23/1/19
  224. * @since 5.7.10
  225. *
  226. * @param void
  227. * @return int
  228. */
  229. function count() {
  230. return count( $this->data );
  231. }
  232. /**
  233. * query
  234. *
  235. * Returns a filtered array of data based on the set of key => value arguments.
  236. *
  237. * @date 23/1/19
  238. * @since 5.7.10
  239. *
  240. * @param void
  241. * @return int
  242. */
  243. function query( $args, $operator = 'AND' ) {
  244. return wp_list_filter( $this->data, $args, $operator );
  245. }
  246. /**
  247. * alias
  248. *
  249. * Sets an alias for the given name allowing data to be found via multiple identifiers.
  250. *
  251. * @date 18/1/19
  252. * @since 5.7.10
  253. *
  254. * @param type $var Description. Default.
  255. * @return type Description.
  256. */
  257. function alias( $name = '' /*, $alias, $alias2, etc */ ) {
  258. // Get all aliases.
  259. $args = func_get_args();
  260. array_shift( $args );
  261. // Loop over aliases and add to data.
  262. foreach ( $args as $alias ) {
  263. $this->aliases[ $alias ] = $name;
  264. }
  265. // Return this for chaining.
  266. return $this;
  267. }
  268. /**
  269. * switch_site
  270. *
  271. * Triggered when switching between sites on a multisite installation.
  272. *
  273. * @date 13/2/19
  274. * @since 5.7.11
  275. *
  276. * @param int $site_id New blog ID.
  277. * @param int prev_blog_id Prev blog ID.
  278. * @return void
  279. */
  280. function switch_site( $site_id, $prev_site_id ) {
  281. // Bail early if not multisite compatible.
  282. if ( ! $this->multisite ) {
  283. return;
  284. }
  285. // Bail early if no change in blog ID.
  286. if ( $site_id === $prev_site_id ) {
  287. return;
  288. }
  289. // Create storage.
  290. if ( ! isset( $this->site_data ) ) {
  291. $this->site_data = array();
  292. $this->site_aliases = array();
  293. }
  294. // Save state.
  295. $this->site_data[ $prev_site_id ] = $this->data;
  296. $this->site_aliases[ $prev_site_id ] = $this->aliases;
  297. // Reset state.
  298. $this->data = array();
  299. $this->aliases = array();
  300. // Load state.
  301. if ( isset( $this->site_data[ $site_id ] ) ) {
  302. $this->data = $this->site_data[ $site_id ];
  303. $this->aliases = $this->site_aliases[ $site_id ];
  304. unset( $this->site_data[ $site_id ] );
  305. unset( $this->site_aliases[ $site_id ] );
  306. }
  307. }
  308. }
  309. endif; // class_exists check