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