loop.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. if ( ! class_exists( 'acf_loop' ) ) :
  6. class acf_loop {
  7. /*
  8. * __construct
  9. *
  10. * This function will setup the class functionality
  11. *
  12. * @type function
  13. * @date 5/03/2014
  14. * @since 5.0.0
  15. *
  16. * @param n/a
  17. * @return n/a
  18. */
  19. function __construct() {
  20. // vars
  21. $this->loops = array();
  22. }
  23. /*
  24. * is_empty
  25. *
  26. * This function will return true if no loops exist
  27. *
  28. * @type function
  29. * @date 3/03/2016
  30. * @since 5.3.2
  31. *
  32. * @param n/a
  33. * @return (boolean)
  34. */
  35. function is_empty() {
  36. return empty( $this->loops );
  37. }
  38. /*
  39. * is_loop
  40. *
  41. * This function will return true if a loop exists for the given array index
  42. *
  43. * @type function
  44. * @date 3/03/2016
  45. * @since 5.3.2
  46. *
  47. * @param $i (int)
  48. * @return (boolean)
  49. */
  50. function is_loop( $i = 0 ) {
  51. return isset( $this->loops[ $i ] );
  52. }
  53. /*
  54. * get_i
  55. *
  56. * This function will return a valid array index for the given $i
  57. *
  58. * @type function
  59. * @date 3/03/2016
  60. * @since 5.3.2
  61. *
  62. * @param $i (mixed)
  63. * @return (int)
  64. */
  65. function get_i( $i = 0 ) {
  66. // 'active'
  67. if ( $i === 'active' ) {
  68. $i = -1;
  69. }
  70. // 'previous'
  71. if ( $i === 'previous' ) {
  72. $i = -2;
  73. }
  74. // allow negative to look at end of loops
  75. if ( $i < 0 ) {
  76. $i = count( $this->loops ) + $i;
  77. }
  78. // return
  79. return $i;
  80. }
  81. /*
  82. * add_loop
  83. *
  84. * This function will add a new loop
  85. *
  86. * @type function
  87. * @date 3/03/2016
  88. * @since 5.3.2
  89. *
  90. * @param $loop (array)
  91. * @return n/a
  92. */
  93. function add_loop( $loop = array() ) {
  94. // defaults
  95. $loop = wp_parse_args(
  96. $loop,
  97. array(
  98. 'selector' => '',
  99. 'name' => '',
  100. 'value' => false,
  101. 'field' => false,
  102. 'i' => -1,
  103. 'post_id' => 0,
  104. 'key' => '',
  105. )
  106. );
  107. // ensure array
  108. $loop['value'] = acf_get_array( $loop['value'] );
  109. // Re-index values if this loop starts from index 0.
  110. // This allows ajax previews to work ($_POST data contains random unique array keys)
  111. if ( $loop['i'] == -1 ) {
  112. $loop['value'] = array_values( $loop['value'] );
  113. }
  114. // append
  115. $this->loops[] = $loop;
  116. // return
  117. return $loop;
  118. }
  119. /*
  120. * update_loop
  121. *
  122. * This function will update a loop's setting
  123. *
  124. * @type function
  125. * @date 3/03/2016
  126. * @since 5.3.2
  127. *
  128. * @param $i (mixed)
  129. * @param $key (string) the loop setting name
  130. * @param $value (mixed) the loop setting value
  131. * @return (boolean) true on success
  132. */
  133. function update_loop( $i = 'active', $key = null, $value = null ) {
  134. // i
  135. $i = $this->get_i( $i );
  136. // bail early if no set
  137. if ( ! $this->is_loop( $i ) ) {
  138. return false;
  139. }
  140. // set
  141. $this->loops[ $i ][ $key ] = $value;
  142. // return
  143. return true;
  144. }
  145. /*
  146. * get_loop
  147. *
  148. * This function will return a loop, or loop's setting for a given index & key
  149. *
  150. * @type function
  151. * @date 3/03/2016
  152. * @since 5.3.2
  153. *
  154. * @param $i (mixed)
  155. * @param $key (string) the loop setting name
  156. * @return (mixed) false on failure
  157. */
  158. function get_loop( $i = 'active', $key = null ) {
  159. // i
  160. $i = $this->get_i( $i );
  161. // bail early if no set
  162. if ( ! $this->is_loop( $i ) ) {
  163. return false;
  164. }
  165. // check for key
  166. if ( $key !== null ) {
  167. return $this->loops[ $i ][ $key ];
  168. }
  169. // return
  170. return $this->loops[ $i ];
  171. }
  172. /*
  173. * remove_loop
  174. *
  175. * This function will remove a loop
  176. *
  177. * @type function
  178. * @date 3/03/2016
  179. * @since 5.3.2
  180. *
  181. * @param $i (mixed)
  182. * @return (boolean) true on success
  183. */
  184. function remove_loop( $i = 'active' ) {
  185. // i
  186. $i = $this->get_i( $i );
  187. // bail early if no set
  188. if ( ! $this->is_loop( $i ) ) {
  189. return false;
  190. }
  191. // remove
  192. unset( $this->loops[ $i ] );
  193. // reset keys
  194. $this->loops = array_values( $this->loops );
  195. // PHP 7.2 no longer resets array keys for empty value
  196. if ( $this->is_empty() ) {
  197. $this->loops = array();
  198. }
  199. }
  200. }
  201. // initialize
  202. acf()->loop = new acf_loop();
  203. endif; // class_exists check
  204. /*
  205. * acf_add_loop
  206. *
  207. * alias of acf()->loop->add_loop()
  208. *
  209. * @type function
  210. * @date 6/10/13
  211. * @since 5.0.0
  212. *
  213. * @param n/a
  214. * @return n/a
  215. */
  216. function acf_add_loop( $loop = array() ) {
  217. return acf()->loop->add_loop( $loop );
  218. }
  219. /*
  220. * acf_update_loop
  221. *
  222. * alias of acf()->loop->update_loop()
  223. *
  224. * @type function
  225. * @date 6/10/13
  226. * @since 5.0.0
  227. *
  228. * @param n/a
  229. * @return n/a
  230. */
  231. function acf_update_loop( $i = 'active', $key = null, $value = null ) {
  232. return acf()->loop->update_loop( $i, $key, $value );
  233. }
  234. /*
  235. * acf_get_loop
  236. *
  237. * alias of acf()->loop->get_loop()
  238. *
  239. * @type function
  240. * @date 6/10/13
  241. * @since 5.0.0
  242. *
  243. * @param n/a
  244. * @return n/a
  245. */
  246. function acf_get_loop( $i = 'active', $key = null ) {
  247. return acf()->loop->get_loop( $i, $key );
  248. }
  249. /*
  250. * acf_remove_loop
  251. *
  252. * alias of acf()->loop->remove_loop()
  253. *
  254. * @type function
  255. * @date 6/10/13
  256. * @since 5.0.0
  257. *
  258. * @param n/a
  259. * @return n/a
  260. */
  261. function acf_remove_loop( $i = 'active' ) {
  262. return acf()->loop->remove_loop( $i );
  263. }