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