acf-utility-functions.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. // Globals.
  3. global $acf_stores, $acf_instances;
  4. // Initialize plaeholders.
  5. $acf_stores = array();
  6. $acf_instances = array();
  7. /**
  8. * acf_new_instance
  9. *
  10. * Creates a new instance of the given class and stores it in the instances data store.
  11. *
  12. * @date 9/1/19
  13. * @since 5.7.10
  14. *
  15. * @param string $class The class name.
  16. * @return object The instance.
  17. */
  18. function acf_new_instance( $class = '' ) {
  19. global $acf_instances;
  20. return $acf_instances[ $class ] = new $class();
  21. }
  22. /**
  23. * acf_get_instance
  24. *
  25. * Returns an instance for the given class.
  26. *
  27. * @date 9/1/19
  28. * @since 5.7.10
  29. *
  30. * @param string $class The class name.
  31. * @return object The instance.
  32. */
  33. function acf_get_instance( $class = '' ) {
  34. global $acf_instances;
  35. if ( ! isset( $acf_instances[ $class ] ) ) {
  36. $acf_instances[ $class ] = new $class();
  37. }
  38. return $acf_instances[ $class ];
  39. }
  40. /**
  41. * acf_register_store
  42. *
  43. * Registers a data store.
  44. *
  45. * @date 9/1/19
  46. * @since 5.7.10
  47. *
  48. * @param string $name The store name.
  49. * @param array $data Array of data to start the store with.
  50. * @return ACF_Data
  51. */
  52. function acf_register_store( $name = '', $data = false ) {
  53. // Create store.
  54. $store = new ACF_Data( $data );
  55. // Register store.
  56. global $acf_stores;
  57. $acf_stores[ $name ] = $store;
  58. // Return store.
  59. return $store;
  60. }
  61. /**
  62. * acf_get_store
  63. *
  64. * Returns a data store.
  65. *
  66. * @date 9/1/19
  67. * @since 5.7.10
  68. *
  69. * @param string $name The store name.
  70. * @return ACF_Data
  71. */
  72. function acf_get_store( $name = '' ) {
  73. global $acf_stores;
  74. return isset( $acf_stores[ $name ] ) ? $acf_stores[ $name ] : false;
  75. }
  76. /**
  77. * acf_switch_stores
  78. *
  79. * Triggered when switching between sites on a multisite installation.
  80. *
  81. * @date 13/2/19
  82. * @since 5.7.11
  83. *
  84. * @param int $site_id New blog ID.
  85. * @param int prev_blog_id Prev blog ID.
  86. * @return void
  87. */
  88. function acf_switch_stores( $site_id, $prev_site_id ) {
  89. // Loop over stores and call switch_site().
  90. global $acf_stores;
  91. foreach ( $acf_stores as $store ) {
  92. $store->switch_site( $site_id, $prev_site_id );
  93. }
  94. }
  95. add_action( 'switch_blog', 'acf_switch_stores', 10, 2 );
  96. /**
  97. * acf_get_path
  98. *
  99. * Returns the plugin path to a specified file.
  100. *
  101. * @date 28/9/13
  102. * @since 5.0.0
  103. *
  104. * @param string $filename The specified file.
  105. * @return string
  106. */
  107. function acf_get_path( $filename = '' ) {
  108. return ACF_PATH . ltrim( $filename, '/' );
  109. }
  110. /**
  111. * acf_get_url
  112. *
  113. * Returns the plugin url to a specified file.
  114. * This function also defines the ACF_URL constant.
  115. *
  116. * @date 12/12/17
  117. * @since 5.6.8
  118. *
  119. * @param string $filename The specified file.
  120. * @return string
  121. */
  122. function acf_get_url( $filename = '' ) {
  123. if ( ! defined( 'ACF_URL' ) ) {
  124. define( 'ACF_URL', acf_get_setting( 'url' ) );
  125. }
  126. return ACF_URL . ltrim( $filename, '/' );
  127. }
  128. /*
  129. * acf_include
  130. *
  131. * Includes a file within the ACF plugin.
  132. *
  133. * @date 10/3/14
  134. * @since 5.0.0
  135. *
  136. * @param string $filename The specified file.
  137. * @return void
  138. */
  139. function acf_include( $filename = '' ) {
  140. $file_path = acf_get_path( $filename );
  141. if ( file_exists( $file_path ) ) {
  142. include_once $file_path;
  143. }
  144. }