acf-hook-functions.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. // Register store.
  3. acf_register_store( 'hook-variations' );
  4. /**
  5. * acf_add_filter_variations
  6. *
  7. * Registers variations for the given filter.
  8. *
  9. * @date 26/1/19
  10. * @since 5.7.11
  11. *
  12. * @param string $filter The filter name.
  13. * @param array $variations An array variation keys.
  14. * @param int $index The param index to find variation values.
  15. * @return void
  16. */
  17. function acf_add_filter_variations( $filter = '', $variations = array(), $index = 0 ) {
  18. // Store replacement data.
  19. acf_get_store( 'hook-variations' )->set(
  20. $filter,
  21. array(
  22. 'type' => 'filter',
  23. 'variations' => $variations,
  24. 'index' => $index,
  25. )
  26. );
  27. // Add generic handler.
  28. // Use a priotiry of 10, and accepted args of 10 (ignored by WP).
  29. add_filter( $filter, '_acf_apply_hook_variations', 10, 10 );
  30. }
  31. /**
  32. * acf_add_action_variations
  33. *
  34. * Registers variations for the given action.
  35. *
  36. * @date 26/1/19
  37. * @since 5.7.11
  38. *
  39. * @param string $action The action name.
  40. * @param array $variations An array variation keys.
  41. * @param int $index The param index to find variation values.
  42. * @return void
  43. */
  44. function acf_add_action_variations( $action = '', $variations = array(), $index = 0 ) {
  45. // Store replacement data.
  46. acf_get_store( 'hook-variations' )->set(
  47. $action,
  48. array(
  49. 'type' => 'action',
  50. 'variations' => $variations,
  51. 'index' => $index,
  52. )
  53. );
  54. // Add generic handler.
  55. // Use a priotiry of 10, and accepted args of 10 (ignored by WP).
  56. add_action( $action, '_acf_apply_hook_variations', 10, 10 );
  57. }
  58. /**
  59. * _acf_apply_hook_variations
  60. *
  61. * Applies hook variations during apply_filters() or do_action().
  62. *
  63. * @date 25/1/19
  64. * @since 5.7.11
  65. *
  66. * @param mixed
  67. * @return mixed
  68. */
  69. function _acf_apply_hook_variations() {
  70. // Get current filter.
  71. $filter = current_filter();
  72. // Get args provided.
  73. $args = func_get_args();
  74. // Get variation information.
  75. $variations = acf_get_store( 'hook-variations' )->get( $filter );
  76. $index = $variations['index'];
  77. $type = $variations['type'];
  78. $variations = $variations['variations'];
  79. // Find field in args using index.
  80. $field = $args[ $index ];
  81. // Loop over variations and apply filters.
  82. foreach ( $variations as $variation ) {
  83. // Get value from field.
  84. // First look for "backup" value ("_name", "_key").
  85. if ( isset( $field[ "_$variation" ] ) ) {
  86. $value = $field[ "_$variation" ];
  87. } elseif ( isset( $field[ $variation ] ) ) {
  88. $value = $field[ $variation ];
  89. } else {
  90. continue;
  91. }
  92. // Apply filters.
  93. if ( $type === 'filter' ) {
  94. $args[0] = apply_filters_ref_array( "$filter/$variation=$value", $args );
  95. // Or do action.
  96. } else {
  97. do_action_ref_array( "$filter/$variation=$value", $args );
  98. }
  99. }
  100. // Return first arg.
  101. return $args[0];
  102. }
  103. // Register store.
  104. acf_register_store( 'deprecated-hooks' );
  105. /**
  106. * acf_add_deprecated_filter
  107. *
  108. * Registers a deprecated filter to run during the replacement.
  109. *
  110. * @date 25/1/19
  111. * @since 5.7.11
  112. *
  113. * @param string $deprecated The deprecated hook.
  114. * @param string $version The version this hook was deprecated.
  115. * @param string $replacement The replacement hook.
  116. * @return void
  117. */
  118. function acf_add_deprecated_filter( $deprecated, $version, $replacement ) {
  119. // Store replacement data.
  120. acf_get_store( 'deprecated-hooks' )->append(
  121. array(
  122. 'type' => 'filter',
  123. 'deprecated' => $deprecated,
  124. 'replacement' => $replacement,
  125. 'version' => $version,
  126. )
  127. );
  128. // Add generic handler.
  129. // Use a priority of 10, and accepted args of 10 (ignored by WP).
  130. add_filter( $replacement, '_acf_apply_deprecated_hook', 10, 10 );
  131. }
  132. /**
  133. * acf_add_deprecated_action
  134. *
  135. * Registers a deprecated action to run during the replacement.
  136. *
  137. * @date 25/1/19
  138. * @since 5.7.11
  139. *
  140. * @param string $deprecated The deprecated hook.
  141. * @param string $version The version this hook was deprecated.
  142. * @param string $replacement The replacement hook.
  143. * @return void
  144. */
  145. function acf_add_deprecated_action( $deprecated, $version, $replacement ) {
  146. // Store replacement data.
  147. acf_get_store( 'deprecated-hooks' )->append(
  148. array(
  149. 'type' => 'action',
  150. 'deprecated' => $deprecated,
  151. 'replacement' => $replacement,
  152. 'version' => $version,
  153. )
  154. );
  155. // Add generic handler.
  156. // Use a priority of 10, and accepted args of 10 (ignored by WP).
  157. add_filter( $replacement, '_acf_apply_deprecated_hook', 10, 10 );
  158. }
  159. /**
  160. * Applies a deprecated filter during apply_filters() or do_action().
  161. *
  162. * @date 25/1/19
  163. * @since 5.7.11
  164. *
  165. * @param mixed
  166. * @return mixed
  167. */
  168. function _acf_apply_deprecated_hook() {
  169. // Get current hook.
  170. $current_hook = current_filter();
  171. // Get args provided.
  172. $args = func_get_args();
  173. // Get deprecated items for this hook.
  174. $deprecated_hooks = acf_get_store( 'deprecated-hooks' )->query( array( 'replacement' => $current_hook ) );
  175. // Loop over results.
  176. foreach ( $deprecated_hooks as $hook ) {
  177. // Check if anyone is hooked into this deprecated hook.
  178. if ( isset( $hook['deprecated'] ) && has_filter( $hook['deprecated'] ) ) {
  179. // Log warning.
  180. // _deprecated_hook( $deprecated, $version, $hook );
  181. // Apply the item/do the action.
  182. if ( $hook['type'] === 'filter' ) {
  183. $args[0] = apply_filters_ref_array( $hook['deprecated'], $args );
  184. } else {
  185. do_action_ref_array( $hook['deprecated'], $args );
  186. }
  187. }
  188. }
  189. // Return first arg.
  190. return $args[0];
  191. }