| @@ -1,151 +1,118 @@ | ||
| 1 | -<?php | |
| 1 | +<?php | |
| 2 | 2 | |
| 3 | 3 | /* |
| 4 | 4 | * get_field() |
| 5 | 5 | * |
| 6 | 6 | * This function will return a custom field value for a specific field name/key + post_id. |
| 7 | -* There is a 3rd parameter to turn on/off formating. This means that an image field will not use | |
| 7 | +* There is a 3rd parameter to turn on/off formating. This means that an image field will not use | |
| 8 | 8 | * its 'return option' to format the value but return only what was saved in the database |
| 9 | 9 | * |
| 10 | -* @type function | |
| 11 | -* @since 3.6 | |
| 12 | -* @date 29/01/13 | |
| 10 | +* @type function | |
| 11 | +* @since 3.6 | |
| 12 | +* @date 29/01/13 | |
| 13 | 13 | * |
| 14 | -* @param $selector (string) the field name or key | |
| 15 | -* @param $post_id (mixed) the post_id of which the value is saved against | |
| 16 | -* @param $format_value (boolean) whether or not to format the value as described above | |
| 17 | -* @return (mixed) | |
| 14 | +* @param $selector (string) the field name or key | |
| 15 | +* @param $post_id (mixed) the post_id of which the value is saved against | |
| 16 | +* @param $format_value (boolean) whether or not to format the value as described above | |
| 17 | +* @return (mixed) | |
| 18 | 18 | */ |
| 19 | - | |
| 19 | + | |
| 20 | 20 | function get_field( $selector, $post_id = false, $format_value = true ) { |
| 21 | - | |
| 21 | + | |
| 22 | 22 | // filter post_id |
| 23 | 23 | $post_id = acf_get_valid_post_id( $post_id ); |
| 24 | - | |
| 25 | - | |
| 24 | + | |
| 26 | 25 | // get field |
| 27 | 26 | $field = acf_maybe_get_field( $selector, $post_id ); |
| 28 | - | |
| 29 | - | |
| 27 | + | |
| 30 | 28 | // create dummy field |
| 31 | - if( !$field ) { | |
| 32 | - | |
| 33 | - $field = acf_get_valid_field(array( | |
| 34 | - 'name' => $selector, | |
| 35 | - 'key' => '', | |
| 36 | - 'type' => '', | |
| 37 | - )); | |
| 38 | - | |
| 39 | - | |
| 29 | + if ( ! $field ) { | |
| 30 | + | |
| 31 | + $field = acf_get_valid_field( | |
| 32 | + array( | |
| 33 | + 'name' => $selector, | |
| 34 | + 'key' => '', | |
| 35 | + 'type' => '', | |
| 36 | + ) | |
| 37 | + ); | |
| 38 | + | |
| 40 | 39 | // prevent formatting |
| 41 | 40 | $format_value = false; |
| 42 | - | |
| 41 | + | |
| 43 | 42 | } |
| 44 | - | |
| 45 | - | |
| 43 | + | |
| 46 | 44 | // get value for field |
| 47 | 45 | $value = acf_get_value( $post_id, $field ); |
| 48 | - | |
| 49 | - | |
| 46 | + | |
| 50 | 47 | // format value |
| 51 | - if( $format_value ) { | |
| 52 | - | |
| 48 | + if ( $format_value ) { | |
| 49 | + | |
| 53 | 50 | // get value for field |
| 54 | 51 | $value = acf_format_value( $value, $post_id, $field ); |
| 55 | - | |
| 52 | + | |
| 56 | 53 | } |
| 57 | - | |
| 58 | - | |
| 54 | + | |
| 59 | 55 | // return |
| 60 | 56 | return $value; |
| 61 | - | |
| 57 | + | |
| 62 | 58 | } |
| 63 | 59 | |
| 60 | +/** | |
| 61 | + * This function is the same as echo get_field(). | |
| 62 | + * | |
| 63 | + * @since 1.0.3 | |
| 64 | + * @date 29/01/13 | |
| 65 | + * | |
| 66 | + * @param string $selector The field name or key. | |
| 67 | + * @param mixed $post_id The post_id of which the value is saved against. | |
| 68 | + * @return void | |
| 69 | + */ | |
| 70 | +function the_field( $selector, $post_id = false, $format_value = true ) { | |
| 71 | + $value = get_field( $selector, $post_id, $format_value ); | |
| 64 | 72 | |
| 65 | -/* | |
| 66 | -* the_field() | |
| 67 | -* | |
| 68 | -* This function is the same as echo get_field(). | |
| 69 | -* | |
| 70 | -* @type function | |
| 71 | -* @since 1.0.3 | |
| 72 | -* @date 29/01/13 | |
| 73 | -* | |
| 74 | -* @param $selector (string) the field name or key | |
| 75 | -* @param $post_id (mixed) the post_id of which the value is saved against | |
| 76 | -* @return n/a | |
| 77 | -*/ | |
| 73 | + if ( is_array( $value ) ) { | |
| 74 | + $value = implode( ', ', $value ); | |
| 75 | + } | |
| 78 | 76 | |
| 79 | -function the_field( $selector, $post_id = false, $format_value = true ) { | |
| 80 | - | |
| 81 | - $value = get_field($selector, $post_id, $format_value); | |
| 82 | - | |
| 83 | - if( is_array($value) ) { | |
| 84 | - | |
| 85 | - $value = @implode( ', ', $value ); | |
| 86 | - | |
| 87 | - } | |
| 88 | - | |
| 89 | 77 | echo $value; |
| 90 | - | |
| 91 | 78 | } |
| 92 | 79 | |
| 80 | +/** | |
| 81 | + * This function will return an array containing all the field data for a given field_name. | |
| 82 | + * | |
| 83 | + * @since 3.6 | |
| 84 | + * @date 3/02/13 | |
| 85 | + * | |
| 86 | + * @param string $selector The field name or key. | |
| 87 | + * @param mixed $post_id The post_id of which the value is saved against. | |
| 88 | + * @param bool $format_value Whether to format the field value. | |
| 89 | + * @param bool $load_value Whether to load the field value. | |
| 90 | + * | |
| 91 | + * @return array|false $field | |
| 92 | + */ | |
| 93 | +function get_field_object( $selector, $post_id = false, $format_value = true, $load_value = true ) { | |
| 94 | + // Compatibility with ACF ~4. | |
| 95 | + if ( is_array( $format_value ) && isset( $format_value['format_value'] ) ) { | |
| 96 | + $format_value = $format_value['format_value']; | |
| 97 | + } | |
| 93 | 98 | |
| 94 | -/* | |
| 95 | -* get_field_object() | |
| 96 | -* | |
| 97 | -* This function will return an array containing all the field data for a given field_name | |
| 98 | -* | |
| 99 | -* @type function | |
| 100 | -* @since 3.6 | |
| 101 | -* @date 3/02/13 | |
| 102 | -* | |
| 103 | -* @param $selector (string) the field name or key | |
| 104 | -* @param $post_id (mixed) the post_id of which the value is saved against | |
| 105 | -* @param $format_value (boolean) whether or not to format the field value | |
| 106 | -* @param $load_value (boolean) whether or not to load the field value | |
| 107 | -* @return $field (array) | |
| 108 | -*/ | |
| 99 | + $post_id = acf_get_valid_post_id( $post_id ); | |
| 100 | + $field = acf_maybe_get_field( $selector, $post_id ); | |
| 109 | 101 | |
| 110 | -function get_field_object( $selector, $post_id = false, $format_value = true, $load_value = true ) { | |
| 111 | - | |
| 112 | - // compatibilty | |
| 113 | - if( is_array($format_value) ) extract( $format_value ); | |
| 114 | - | |
| 115 | - | |
| 116 | - // get valid post_id | |
| 117 | - $post_id = acf_get_valid_post_id( $post_id ); | |
| 118 | - | |
| 119 | - | |
| 120 | - // get field key | |
| 121 | - $field = acf_maybe_get_field( $selector, $post_id ); | |
| 122 | - | |
| 123 | - | |
| 124 | - // bail early if no field found | |
| 125 | - if( !$field ) return false; | |
| 126 | - | |
| 127 | - | |
| 128 | - // load value | |
| 129 | - if( $load_value ) { | |
| 130 | - | |
| 102 | + if ( ! $field ) { | |
| 103 | + return false; | |
| 104 | + } | |
| 105 | + | |
| 106 | + if ( $load_value ) { | |
| 131 | 107 | $field['value'] = acf_get_value( $post_id, $field ); |
| 132 | - | |
| 133 | 108 | } |
| 134 | - | |
| 135 | - | |
| 136 | - // format value | |
| 137 | - if( $format_value ) { | |
| 138 | - | |
| 139 | - // get value for field | |
| 109 | + | |
| 110 | + if ( $format_value ) { | |
| 140 | 111 | $field['value'] = acf_format_value( $field['value'], $post_id, $field ); |
| 141 | - | |
| 142 | 112 | } |
| 143 | - | |
| 144 | - | |
| 145 | - // return | |
| 113 | + | |
| 146 | 114 | return $field; |
| 147 | - | |
| 148 | 115 | } |
| 149 | 116 | |
| 150 | 117 | /* |
| 151 | 118 | * acf_get_object_field |
| @@ -152,39 +119,39 @@ | ||
| 152 | 119 | * |
| 153 | 120 | * This function will return a field for the given selector. |
| 154 | 121 | * It will also review the field_reference to ensure the correct field is returned which makes it useful for the template API |
| 155 | 122 | * |
| 156 | -* @type function | |
| 157 | -* @date 4/08/2015 | |
| 158 | -* @since 5.2.3 | |
| 123 | +* @type function | |
| 124 | +* @date 4/08/2015 | |
| 125 | +* @since 5.2.3 | |
| 159 | 126 | * |
| 160 | -* @param $selector (mixed) identifyer of field. Can be an ID, key, name or post object | |
| 161 | -* @param $post_id (mixed) the post_id of which the value is saved against | |
| 162 | -* @param $strict (boolean) if true, return a field only when a field key is found. | |
| 163 | -* @return $field (array) | |
| 127 | +* @param $selector (mixed) identifyer of field. Can be an ID, key, name or post object | |
| 128 | +* @param $post_id (mixed) the post_id of which the value is saved against | |
| 129 | +* @param $strict (boolean) if true, return a field only when a field key is found. | |
| 130 | +* @return $field (array) | |
| 164 | 131 | */ |
| 165 | 132 | function acf_maybe_get_field( $selector, $post_id = false, $strict = true ) { |
| 166 | - | |
| 133 | + | |
| 167 | 134 | // init |
| 168 | 135 | acf_init(); |
| 169 | - | |
| 136 | + | |
| 170 | 137 | // Check if field key was given. |
| 171 | - if( acf_is_field_key($selector) ) { | |
| 138 | + if ( acf_is_field_key( $selector ) ) { | |
| 172 | 139 | return acf_get_field( $selector ); |
| 173 | 140 | } |
| 174 | - | |
| 141 | + | |
| 175 | 142 | // Lookup field via reference. |
| 176 | 143 | $post_id = acf_get_valid_post_id( $post_id ); |
| 177 | - $field = acf_get_meta_field( $selector, $post_id ); | |
| 178 | - if( $field ) { | |
| 144 | + $field = acf_get_meta_field( $selector, $post_id ); | |
| 145 | + if ( $field ) { | |
| 179 | 146 | return $field; |
| 180 | 147 | } |
| 181 | - | |
| 148 | + | |
| 182 | 149 | // Lookup field loosely via name. |
| 183 | - if( !$strict ) { | |
| 184 | - return acf_get_field( $selector ); | |
| 150 | + if ( ! $strict ) { | |
| 151 | + return acf_get_field( $selector ); | |
| 185 | 152 | } |
| 186 | - | |
| 153 | + | |
| 187 | 154 | // Return no result. |
| 188 | 155 | return false; |
| 189 | 156 | } |
| 190 | 157 | |
| @@ -192,59 +159,57 @@ | ||
| 192 | 159 | * acf_maybe_get_sub_field |
| 193 | 160 | * |
| 194 | 161 | * This function will attempt to find a sub field |
| 195 | 162 | * |
| 196 | -* @type function | |
| 197 | -* @date 3/10/2016 | |
| 198 | -* @since 5.4.0 | |
| 163 | +* @type function | |
| 164 | +* @date 3/10/2016 | |
| 165 | +* @since 5.4.0 | |
| 199 | 166 | * |
| 200 | -* @param $post_id (int) | |
| 201 | -* @return $post_id (int) | |
| 167 | +* @param $post_id (int) | |
| 168 | +* @return $post_id (int) | |
| 202 | 169 | */ |
| 203 | 170 | |
| 204 | 171 | function acf_maybe_get_sub_field( $selectors, $post_id = false, $strict = true ) { |
| 205 | - | |
| 206 | - // bail ealry if not enough selectors | |
| 207 | - if( !is_array($selectors) || count($selectors) < 3 ) return false; | |
| 208 | - | |
| 209 | - | |
| 172 | + | |
| 173 | + // bail early if not enough selectors | |
| 174 | + if ( ! is_array( $selectors ) || count( $selectors ) < 3 ) { | |
| 175 | + return false; | |
| 176 | + } | |
| 177 | + | |
| 210 | 178 | // vars |
| 211 | - $offset = acf_get_setting('row_index_offset'); | |
| 212 | - $selector = acf_extract_var( $selectors, 0 ); | |
| 179 | + $offset = acf_get_setting( 'row_index_offset' ); | |
| 180 | + $selector = acf_extract_var( $selectors, 0 ); | |
| 213 | 181 | $selectors = array_values( $selectors ); // reset keys |
| 214 | - | |
| 215 | - | |
| 182 | + | |
| 216 | 183 | // attempt get field |
| 217 | 184 | $field = acf_maybe_get_field( $selector, $post_id, $strict ); |
| 218 | - | |
| 219 | - | |
| 185 | + | |
| 220 | 186 | // bail early if no field |
| 221 | - if( !$field ) return false; | |
| 222 | - | |
| 223 | - | |
| 187 | + if ( ! $field ) { | |
| 188 | + return false; | |
| 189 | + } | |
| 190 | + | |
| 224 | 191 | // loop |
| 225 | - for( $j = 0; $j < count($selectors); $j+=2 ) { | |
| 226 | - | |
| 192 | + for ( $j = 0; $j < count( $selectors ); $j += 2 ) { | |
| 193 | + | |
| 227 | 194 | // vars |
| 228 | - $sub_i = $selectors[ $j ]; | |
| 229 | - $sub_s = $selectors[ $j+1 ]; | |
| 195 | + $sub_i = $selectors[ $j ]; | |
| 196 | + $sub_s = $selectors[ $j + 1 ]; | |
| 230 | 197 | $field_name = $field['name']; |
| 231 | - | |
| 232 | - | |
| 198 | + | |
| 233 | 199 | // find sub field |
| 234 | 200 | $field = acf_get_sub_field( $sub_s, $field ); |
| 235 | - | |
| 236 | - | |
| 201 | + | |
| 237 | 202 | // bail early if no sub field |
| 238 | - if( !$field ) return false; | |
| 239 | - | |
| 240 | - | |
| 203 | + if ( ! $field ) { | |
| 204 | + return false; | |
| 205 | + } | |
| 206 | + | |
| 241 | 207 | // add to name |
| 242 | - $field['name'] = $field_name . '_' . ($sub_i-$offset) . '_' . $field['name']; | |
| 243 | - | |
| 208 | + $field['name'] = $field_name . '_' . ( $sub_i - $offset ) . '_' . $field['name']; | |
| 209 | + | |
| 244 | 210 | } |
| 245 | - | |
| 246 | - | |
| 211 | + | |
| 247 | 212 | // return |
| 248 | 213 | return $field; |
| 249 | 214 | } |
| 250 | 215 | |
| @@ -253,39 +218,38 @@ | ||
| 253 | 218 | * |
| 254 | 219 | * This function will return an array containing all the custom field values for a specific post_id. |
| 255 | 220 | * The function is not very elegant and wastes a lot of PHP memory / SQL queries if you are not using all the values. |
| 256 | 221 | * |
| 257 | -* @type function | |
| 258 | -* @since 3.6 | |
| 259 | -* @date 29/01/13 | |
| 222 | +* @type function | |
| 223 | +* @since 3.6 | |
| 224 | +* @date 29/01/13 | |
| 260 | 225 | * |
| 261 | -* @param $post_id (mixed) the post_id of which the value is saved against | |
| 262 | -* @param $format_value (boolean) whether or not to format the field value | |
| 263 | -* @return (array) associative array where field name => field value | |
| 226 | +* @param $post_id (mixed) the post_id of which the value is saved against | |
| 227 | +* @param $format_value (boolean) whether or not to format the field value | |
| 228 | +* @return (array) associative array where field name => field value | |
| 264 | 229 | */ |
| 265 | 230 | |
| 266 | 231 | function get_fields( $post_id = false, $format_value = true ) { |
| 267 | - | |
| 232 | + | |
| 268 | 233 | // vars |
| 269 | 234 | $fields = get_field_objects( $post_id, $format_value ); |
| 270 | - $meta = array(); | |
| 271 | - | |
| 272 | - | |
| 235 | + $meta = array(); | |
| 236 | + | |
| 273 | 237 | // bail early |
| 274 | - if( !$fields ) return false; | |
| 275 | - | |
| 276 | - | |
| 238 | + if ( ! $fields ) { | |
| 239 | + return false; | |
| 240 | + } | |
| 241 | + | |
| 277 | 242 | // populate |
| 278 | - foreach( $fields as $k => $field ) { | |
| 279 | - | |
| 243 | + foreach ( $fields as $k => $field ) { | |
| 244 | + | |
| 280 | 245 | $meta[ $k ] = $field['value']; |
| 281 | - | |
| 246 | + | |
| 282 | 247 | } |
| 283 | - | |
| 284 | - | |
| 248 | + | |
| 285 | 249 | // return |
| 286 | - return $meta; | |
| 287 | - | |
| 250 | + return $meta; | |
| 251 | + | |
| 288 | 252 | } |
| 289 | 253 | |
| 290 | 254 | |
| 291 | 255 | /* |
| @@ -293,63 +257,71 @@ | ||
| 293 | 257 | * |
| 294 | 258 | * This function will return an array containing all the custom field objects for a specific post_id. |
| 295 | 259 | * The function is not very elegant and wastes a lot of PHP memory / SQL queries if you are not using all the fields / values. |
| 296 | 260 | * |
| 297 | -* @type function | |
| 298 | -* @since 3.6 | |
| 299 | -* @date 29/01/13 | |
| 261 | +* @type function | |
| 262 | +* @since 3.6 | |
| 263 | +* @date 29/01/13 | |
| 300 | 264 | * |
| 301 | -* @param $post_id (mixed) the post_id of which the value is saved against | |
| 302 | -* @param $format_value (boolean) whether or not to format the field value | |
| 303 | -* @param $load_value (boolean) whether or not to load the field value | |
| 304 | -* @return (array) associative array where field name => field | |
| 265 | +* @param $post_id (mixed) the post_id of which the value is saved against | |
| 266 | +* @param $format_value (boolean) whether or not to format the field value | |
| 267 | +* @param $load_value (boolean) whether or not to load the field value | |
| 268 | +* @return (array) associative array where field name => field | |
| 305 | 269 | */ |
| 306 | 270 | |
| 307 | 271 | function get_field_objects( $post_id = false, $format_value = true, $load_value = true ) { |
| 308 | - | |
| 272 | + | |
| 309 | 273 | // init |
| 310 | 274 | acf_init(); |
| 311 | - | |
| 275 | + | |
| 312 | 276 | // validate post_id |
| 313 | 277 | $post_id = acf_get_valid_post_id( $post_id ); |
| 314 | - | |
| 278 | + | |
| 315 | 279 | // get meta |
| 316 | 280 | $meta = acf_get_meta( $post_id ); |
| 317 | - | |
| 281 | + | |
| 318 | 282 | // bail early if no meta |
| 319 | - if( empty($meta) ) return false; | |
| 320 | - | |
| 283 | + if ( empty( $meta ) ) { | |
| 284 | + return false; | |
| 285 | + } | |
| 286 | + | |
| 321 | 287 | // populate vars |
| 322 | 288 | $fields = array(); |
| 323 | - foreach( $meta as $key => $value ) { | |
| 324 | - | |
| 289 | + foreach ( $meta as $key => $value ) { | |
| 290 | + | |
| 325 | 291 | // bail if reference key does not exist |
| 326 | - if( !isset($meta["_$key"]) ) continue; | |
| 327 | - | |
| 292 | + if ( ! isset( $meta[ "_$key" ] ) || ( ! is_string( $meta[ "_$key" ] ) && ! is_numeric( $meta[ "_$key" ] ) ) ) { | |
| 293 | + continue; | |
| 294 | + } | |
| 295 | + | |
| 328 | 296 | // get field |
| 329 | - $field = acf_get_field($meta["_$key"]); | |
| 330 | - | |
| 297 | + $field = acf_get_field( $meta[ "_$key" ] ); | |
| 298 | + | |
| 331 | 299 | // bail early if no field, or if the field's name is different to $key |
| 332 | 300 | // - solves problem where sub fields (and clone fields) are incorrectly allowed |
| 333 | - if( !$field || $field['name'] !== $key ) continue; | |
| 334 | - | |
| 301 | + if ( ! $field || $field['name'] !== $key ) { | |
| 302 | + continue; | |
| 303 | + } | |
| 304 | + | |
| 335 | 305 | // load value |
| 336 | - if( $load_value ) { | |
| 306 | + if ( $load_value ) { | |
| 337 | 307 | $field['value'] = acf_get_value( $post_id, $field ); |
| 338 | 308 | } |
| 339 | - | |
| 309 | + | |
| 340 | 310 | // format value |
| 341 | - if( $format_value ) { | |
| 311 | + if ( $format_value ) { | |
| 342 | 312 | $field['value'] = acf_format_value( $field['value'], $post_id, $field ); |
| 343 | 313 | } |
| 344 | - | |
| 314 | + | |
| 345 | 315 | // append to $value |
| 346 | 316 | $fields[ $key ] = $field; |
| 347 | 317 | } |
| 348 | - | |
| 318 | + | |
| 349 | 319 | // no value |
| 350 | - if( empty($fields) ) return false; | |
| 351 | - | |
| 320 | + if ( empty( $fields ) ) { | |
| 321 | + return false; | |
| 322 | + } | |
| 323 | + | |
| 352 | 324 | // return |
| 353 | 325 | return $fields; |
| 354 | 326 | } |
| 355 | 327 | |
| @@ -359,135 +331,135 @@ | ||
| 359 | 331 | * |
| 360 | 332 | * Checks if a field (such as Repeater or Flexible Content) has any rows of data to loop over. |
| 361 | 333 | * This function is intended to be used in conjunction with the_row() to step through available values. |
| 362 | 334 | * |
| 363 | - * @date 2/09/13 | |
| 364 | - * @since 4.3.0 | |
| 335 | + * @date 2/09/13 | |
| 336 | + * @since 4.3.0 | |
| 365 | 337 | * |
| 366 | - * @param string $selector The field name or field key. | |
| 367 | - * @param mixed $post_id The post ID where the value is saved. Defaults to the current post. | |
| 368 | - * @return bool | |
| 338 | + * @param string $selector The field name or field key. | |
| 339 | + * @param mixed $post_id The post ID where the value is saved. Defaults to the current post. | |
| 340 | + * @return bool | |
| 369 | 341 | */ |
| 370 | 342 | function have_rows( $selector, $post_id = false ) { |
| 371 | - | |
| 343 | + | |
| 372 | 344 | // Validate and backup $post_id. |
| 373 | 345 | $_post_id = $post_id; |
| 374 | - $post_id = acf_get_valid_post_id( $post_id ); | |
| 375 | - | |
| 346 | + $post_id = acf_get_valid_post_id( $post_id ); | |
| 347 | + | |
| 376 | 348 | // Vars. |
| 377 | - $key = "selector={$selector}/post_id={$post_id}"; | |
| 378 | - $active_loop = acf_get_loop('active'); | |
| 379 | - $prev_loop = acf_get_loop('previous'); | |
| 380 | - $new_loop = false; | |
| 381 | - $sub_field = false; | |
| 382 | - | |
| 349 | + $key = "selector={$selector}/post_id={$post_id}"; | |
| 350 | + $active_loop = acf_get_loop( 'active' ); | |
| 351 | + $prev_loop = acf_get_loop( 'previous' ); | |
| 352 | + $new_loop = false; | |
| 353 | + $sub_field = false; | |
| 354 | + | |
| 383 | 355 | // Check if no active loop. |
| 384 | - if( !$active_loop ) { | |
| 356 | + if ( ! $active_loop ) { | |
| 385 | 357 | $new_loop = 'parent'; |
| 386 | - | |
| 387 | - // Detect "change" compared to the active loop. | |
| 388 | - } elseif( $key !== $active_loop['key'] ) { | |
| 389 | - | |
| 358 | + | |
| 359 | + // Detect "change" compared to the active loop. | |
| 360 | + } elseif ( $key !== $active_loop['key'] ) { | |
| 361 | + | |
| 390 | 362 | // Find sub field and check if a sub value exists. |
| 391 | 363 | $sub_field_exists = false; |
| 392 | - $sub_field = acf_get_sub_field($selector, $active_loop['field']); | |
| 393 | - if( $sub_field ) { | |
| 364 | + $sub_field = acf_get_sub_field( $selector, $active_loop['field'] ); | |
| 365 | + if ( $sub_field ) { | |
| 394 | 366 | $sub_field_exists = isset( $active_loop['value'][ $active_loop['i'] ][ $sub_field['key'] ] ); |
| 395 | 367 | } |
| 396 | - | |
| 368 | + | |
| 397 | 369 | // Detect change in post_id. |
| 398 | - if( $post_id != $active_loop['post_id'] ) { | |
| 399 | - | |
| 370 | + if ( $post_id != $active_loop['post_id'] ) { | |
| 371 | + | |
| 400 | 372 | // Case: Change in $post_id was due to this being a nested loop and not specifying the $post_id. |
| 401 | 373 | // Action: Move down one level into a new loop. |
| 402 | - if( empty($_post_id) && $sub_field_exists ) { | |
| 374 | + if ( empty( $_post_id ) && $sub_field_exists ) { | |
| 403 | 375 | $new_loop = 'child'; |
| 404 | - | |
| 405 | - // Case: Change in $post_id was due to a nested loop ending. | |
| 406 | - // Action: move up one level through the loops. | |
| 407 | - } elseif( $prev_loop && $prev_loop['post_id'] == $post_id ) { | |
| 408 | - acf_remove_loop('active'); | |
| 376 | + | |
| 377 | + // Case: Change in $post_id was due to a nested loop ending. | |
| 378 | + // Action: move up one level through the loops. | |
| 379 | + } elseif ( $prev_loop && $prev_loop['post_id'] == $post_id ) { | |
| 380 | + acf_remove_loop( 'active' ); | |
| 409 | 381 | $active_loop = $prev_loop; |
| 410 | - | |
| 411 | - // Case: Chang in $post_id is the most obvious, used in an WP_Query loop with multiple $post objects. | |
| 412 | - // Action: leave this current loop alone and create a new parent loop. | |
| 382 | + | |
| 383 | + // Case: Chang in $post_id is the most obvious, used in an WP_Query loop with multiple $post objects. | |
| 384 | + // Action: leave this current loop alone and create a new parent loop. | |
| 413 | 385 | } else { |
| 414 | 386 | $new_loop = 'parent'; |
| 415 | 387 | } |
| 416 | - | |
| 417 | - // Detect change in selector. | |
| 418 | - } elseif( $selector != $active_loop['selector'] ) { | |
| 419 | - | |
| 388 | + | |
| 389 | + // Detect change in selector. | |
| 390 | + } elseif ( $selector != $active_loop['selector'] ) { | |
| 391 | + | |
| 420 | 392 | // Case: Change in $field_name was due to this being a nested loop. |
| 421 | 393 | // Action: move down one level into a new loop. |
| 422 | - if( $sub_field_exists ) { | |
| 394 | + if ( $sub_field_exists ) { | |
| 423 | 395 | $new_loop = 'child'; |
| 424 | - | |
| 425 | - // Case: Change in $field_name was due to a nested loop ending. | |
| 426 | - // Action: move up one level through the loops. | |
| 427 | - } elseif( $prev_loop && $prev_loop['selector'] == $selector && $prev_loop['post_id'] == $post_id ) { | |
| 428 | - acf_remove_loop('active'); | |
| 396 | + | |
| 397 | + // Case: Change in $field_name was due to a nested loop ending. | |
| 398 | + // Action: move up one level through the loops. | |
| 399 | + } elseif ( $prev_loop && $prev_loop['selector'] == $selector && $prev_loop['post_id'] == $post_id ) { | |
| 400 | + acf_remove_loop( 'active' ); | |
| 429 | 401 | $active_loop = $prev_loop; |
| 430 | - | |
| 431 | - // Case: Change in $field_name is the most obvious, this is a new loop for a different field within the $post. | |
| 432 | - // Action: leave this current loop alone and create a new parent loop. | |
| 402 | + | |
| 403 | + // Case: Change in $field_name is the most obvious, this is a new loop for a different field within the $post. | |
| 404 | + // Action: leave this current loop alone and create a new parent loop. | |
| 433 | 405 | } else { |
| 434 | 406 | $new_loop = 'parent'; |
| 435 | 407 | } |
| 436 | 408 | } |
| 437 | 409 | } |
| 438 | - | |
| 410 | + | |
| 439 | 411 | // Add loop if required. |
| 440 | - if( $new_loop ) { | |
| 412 | + if ( $new_loop ) { | |
| 441 | 413 | $args = array( |
| 442 | - 'key' => $key, | |
| 443 | - 'selector' => $selector, | |
| 444 | - 'post_id' => $post_id, | |
| 445 | - 'name' => null, | |
| 446 | - 'value' => null, | |
| 447 | - 'field' => null, | |
| 448 | - 'i' => -1, | |
| 414 | + 'key' => $key, | |
| 415 | + 'selector' => $selector, | |
| 416 | + 'post_id' => $post_id, | |
| 417 | + 'name' => null, | |
| 418 | + 'value' => null, | |
| 419 | + 'field' => null, | |
| 420 | + 'i' => -1, | |
| 449 | 421 | ); |
| 450 | - | |
| 422 | + | |
| 451 | 423 | // Case: Parent loop. |
| 452 | - if( $new_loop === 'parent' ) { | |
| 424 | + if ( $new_loop === 'parent' ) { | |
| 453 | 425 | $field = get_field_object( $selector, $post_id, false ); |
| 454 | - if( $field ) { | |
| 426 | + if ( $field ) { | |
| 455 | 427 | $args['field'] = $field; |
| 456 | 428 | $args['value'] = $field['value']; |
| 457 | - $args['name'] = $field['name']; | |
| 429 | + $args['name'] = $field['name']; | |
| 458 | 430 | unset( $args['field']['value'] ); |
| 459 | 431 | } |
| 460 | - | |
| 461 | - // Case: Child loop ($sub_field must exist). | |
| 432 | + | |
| 433 | + // Case: Child loop ($sub_field must exist). | |
| 462 | 434 | } else { |
| 463 | - $args['field'] = $sub_field; | |
| 464 | - $args['value'] = $active_loop['value'][ $active_loop['i'] ][ $sub_field['key'] ]; | |
| 465 | - $args['name'] = "{$active_loop['name']}_{$active_loop['i']}_{$sub_field['name']}"; | |
| 435 | + $args['field'] = $sub_field; | |
| 436 | + $args['value'] = $active_loop['value'][ $active_loop['i'] ][ $sub_field['key'] ]; | |
| 437 | + $args['name'] = "{$active_loop['name']}_{$active_loop['i']}_{$sub_field['name']}"; | |
| 466 | 438 | $args['post_id'] = $active_loop['post_id']; |
| 467 | 439 | } |
| 468 | - | |
| 440 | + | |
| 469 | 441 | // Bail early if value is either empty or a non array. |
| 470 | - if( !$args['value'] || !is_array($args['value']) ) { | |
| 442 | + if ( ! $args['value'] || ! is_array( $args['value'] ) ) { | |
| 471 | 443 | return false; |
| 472 | 444 | } |
| 473 | - | |
| 445 | + | |
| 474 | 446 | // Allow for non repeatable data for Group and Clone fields. |
| 475 | - if( acf_get_field_type_prop($args['field']['type'], 'have_rows') === 'single' ) { | |
| 447 | + if ( acf_get_field_type_prop( $args['field']['type'], 'have_rows' ) === 'single' ) { | |
| 476 | 448 | $args['value'] = array( $args['value'] ); |
| 477 | 449 | } |
| 478 | - | |
| 450 | + | |
| 479 | 451 | // Add loop. |
| 480 | - $active_loop = acf_add_loop($args); | |
| 452 | + $active_loop = acf_add_loop( $args ); | |
| 481 | 453 | } |
| 482 | - | |
| 454 | + | |
| 483 | 455 | // Return true if next row exists. |
| 484 | - if( $active_loop && isset($active_loop['value'][ $active_loop['i']+1 ]) ) { | |
| 456 | + if ( $active_loop && isset( $active_loop['value'][ $active_loop['i'] + 1 ] ) ) { | |
| 485 | 457 | return true; |
| 486 | - } | |
| 487 | - | |
| 458 | + } | |
| 459 | + | |
| 488 | 460 | // Return false if no next row. |
| 489 | - acf_remove_loop('active'); | |
| 461 | + acf_remove_loop( 'active' ); | |
| 490 | 462 | return false; |
| 491 | 463 | } |
| 492 | 464 | |
| 493 | 465 | |
| @@ -495,102 +467,95 @@ | ||
| 495 | 467 | * the_row |
| 496 | 468 | * |
| 497 | 469 | * This function will progress the global repeater or flexible content value 1 row |
| 498 | 470 | * |
| 499 | -* @type function | |
| 500 | -* @date 2/09/13 | |
| 501 | -* @since 4.3.0 | |
| 471 | +* @type function | |
| 472 | +* @date 2/09/13 | |
| 473 | +* @since 4.3.0 | |
| 502 | 474 | * |
| 503 | -* @param N/A | |
| 504 | -* @return (array) the current row data | |
| 475 | +* @param N/A | |
| 476 | +* @return (array) the current row data | |
| 505 | 477 | */ |
| 506 | 478 | |
| 507 | 479 | function the_row( $format = false ) { |
| 508 | - | |
| 480 | + | |
| 509 | 481 | // vars |
| 510 | - $i = acf_get_loop('active', 'i'); | |
| 511 | - | |
| 512 | - | |
| 482 | + $i = acf_get_loop( 'active', 'i' ); | |
| 483 | + | |
| 513 | 484 | // increase |
| 514 | 485 | $i++; |
| 515 | - | |
| 516 | - | |
| 486 | + | |
| 517 | 487 | // update |
| 518 | - acf_update_loop('active', 'i', $i); | |
| 519 | - | |
| 520 | - | |
| 488 | + acf_update_loop( 'active', 'i', $i ); | |
| 489 | + | |
| 521 | 490 | // return |
| 522 | 491 | return get_row( $format ); |
| 523 | - | |
| 492 | + | |
| 524 | 493 | } |
| 525 | 494 | |
| 526 | 495 | function get_row( $format = false ) { |
| 527 | - | |
| 496 | + | |
| 528 | 497 | // vars |
| 529 | - $loop = acf_get_loop('active'); | |
| 530 | - | |
| 531 | - | |
| 498 | + $loop = acf_get_loop( 'active' ); | |
| 499 | + | |
| 532 | 500 | // bail early if no loop |
| 533 | - if( !$loop ) return false; | |
| 534 | - | |
| 535 | - | |
| 501 | + if ( ! $loop ) { | |
| 502 | + return false; | |
| 503 | + } | |
| 504 | + | |
| 536 | 505 | // get value |
| 537 | 506 | $value = acf_maybe_get( $loop['value'], $loop['i'] ); |
| 538 | - | |
| 539 | - | |
| 507 | + | |
| 540 | 508 | // bail early if no current value |
| 541 | 509 | // possible if get_row_layout() is called before the_row() |
| 542 | - if( !$value ) return false; | |
| 543 | - | |
| 544 | - | |
| 510 | + if ( ! $value ) { | |
| 511 | + return false; | |
| 512 | + } | |
| 513 | + | |
| 545 | 514 | // format |
| 546 | - if( $format ) { | |
| 547 | - | |
| 515 | + if ( $format ) { | |
| 516 | + | |
| 548 | 517 | // vars |
| 549 | 518 | $field = $loop['field']; |
| 550 | - | |
| 551 | - | |
| 519 | + | |
| 552 | 520 | // single row |
| 553 | - if( acf_get_field_type_prop($field['type'], 'have_rows') === 'single' ) { | |
| 554 | - | |
| 521 | + if ( acf_get_field_type_prop( $field['type'], 'have_rows' ) === 'single' ) { | |
| 522 | + | |
| 555 | 523 | // format value |
| 556 | 524 | $value = acf_format_value( $value, $loop['post_id'], $field ); |
| 557 | - | |
| 558 | - // multiple rows | |
| 525 | + | |
| 526 | + // multiple rows | |
| 559 | 527 | } else { |
| 560 | - | |
| 528 | + | |
| 561 | 529 | // format entire value |
| 562 | 530 | // - solves problem where cached value is incomplete |
| 563 | 531 | // - no performance issues here thanks to cache |
| 564 | 532 | $value = acf_format_value( $loop['value'], $loop['post_id'], $field ); |
| 565 | 533 | $value = acf_maybe_get( $value, $loop['i'] ); |
| 566 | - | |
| 534 | + | |
| 567 | 535 | } |
| 568 | - | |
| 569 | 536 | } |
| 570 | - | |
| 571 | - | |
| 537 | + | |
| 572 | 538 | // return |
| 573 | 539 | return $value; |
| 574 | - | |
| 540 | + | |
| 575 | 541 | } |
| 576 | 542 | |
| 577 | 543 | function get_row_index() { |
| 578 | - | |
| 544 | + | |
| 579 | 545 | // vars |
| 580 | - $i = acf_get_loop('active', 'i'); | |
| 581 | - $offset = acf_get_setting('row_index_offset'); | |
| 582 | - | |
| 583 | - | |
| 546 | + $i = acf_get_loop( 'active', 'i' ); | |
| 547 | + $offset = acf_get_setting( 'row_index_offset' ); | |
| 548 | + | |
| 584 | 549 | // return |
| 585 | 550 | return $offset + $i; |
| 586 | - | |
| 551 | + | |
| 587 | 552 | } |
| 588 | 553 | |
| 589 | 554 | function the_row_index() { |
| 590 | - | |
| 555 | + | |
| 591 | 556 | echo get_row_index(); |
| 592 | - | |
| 557 | + | |
| 593 | 558 | } |
| 594 | 559 | |
| 595 | 560 | |
| 596 | 561 | /* |
| @@ -597,41 +562,40 @@ | ||
| 597 | 562 | * get_row_sub_field |
| 598 | 563 | * |
| 599 | 564 | * This function is used inside a 'has_sub_field' while loop to return a sub field object |
| 600 | 565 | * |
| 601 | -* @type function | |
| 602 | -* @date 16/05/2016 | |
| 603 | -* @since 5.3.8 | |
| 566 | +* @type function | |
| 567 | +* @date 16/05/2016 | |
| 568 | +* @since 5.3.8 | |
| 604 | 569 | * |
| 605 | -* @param $selector (string) | |
| 606 | -* @return (array) | |
| 570 | +* @param $selector (string) | |
| 571 | +* @return (array) | |
| 607 | 572 | */ |
| 608 | 573 | |
| 609 | 574 | function get_row_sub_field( $selector ) { |
| 610 | - | |
| 575 | + | |
| 611 | 576 | // vars |
| 612 | - $row = acf_get_loop('active'); | |
| 613 | - | |
| 614 | - | |
| 577 | + $row = acf_get_loop( 'active' ); | |
| 578 | + | |
| 615 | 579 | // bail early if no row |
| 616 | - if( !$row ) return false; | |
| 617 | - | |
| 618 | - | |
| 580 | + if ( ! $row ) { | |
| 581 | + return false; | |
| 582 | + } | |
| 583 | + | |
| 619 | 584 | // attempt to find sub field |
| 620 | - $sub_field = acf_get_sub_field($selector, $row['field']); | |
| 621 | - | |
| 622 | - | |
| 585 | + $sub_field = acf_get_sub_field( $selector, $row['field'] ); | |
| 586 | + | |
| 623 | 587 | // bail early if no field |
| 624 | - if( !$sub_field ) return false; | |
| 625 | - | |
| 626 | - | |
| 588 | + if ( ! $sub_field ) { | |
| 589 | + return false; | |
| 590 | + } | |
| 591 | + | |
| 627 | 592 | // update field's name based on row data |
| 628 | 593 | $sub_field['name'] = "{$row['name']}_{$row['i']}_{$sub_field['name']}"; |
| 629 | - | |
| 630 | - | |
| 594 | + | |
| 631 | 595 | // return |
| 632 | 596 | return $sub_field; |
| 633 | - | |
| 597 | + | |
| 634 | 598 | } |
| 635 | 599 | |
| 636 | 600 | |
| 637 | 601 | /* |
| @@ -638,37 +602,36 @@ | ||
| 638 | 602 | * get_row_sub_value |
| 639 | 603 | * |
| 640 | 604 | * This function is used inside a 'has_sub_field' while loop to return a sub field value |
| 641 | 605 | * |
| 642 | -* @type function | |
| 643 | -* @date 16/05/2016 | |
| 644 | -* @since 5.3.8 | |
| 606 | +* @type function | |
| 607 | +* @date 16/05/2016 | |
| 608 | +* @since 5.3.8 | |
| 645 | 609 | * |
| 646 | -* @param $selector (string) | |
| 647 | -* @return (mixed) | |
| 610 | +* @param $selector (string) | |
| 611 | +* @return (mixed) | |
| 648 | 612 | */ |
| 649 | 613 | |
| 650 | 614 | function get_row_sub_value( $selector ) { |
| 651 | - | |
| 615 | + | |
| 652 | 616 | // vars |
| 653 | - $row = acf_get_loop('active'); | |
| 654 | - | |
| 655 | - | |
| 617 | + $row = acf_get_loop( 'active' ); | |
| 618 | + | |
| 656 | 619 | // bail early if no row |
| 657 | - if( !$row ) return null; | |
| 658 | - | |
| 659 | - | |
| 620 | + if ( ! $row ) { | |
| 621 | + return null; | |
| 622 | + } | |
| 623 | + | |
| 660 | 624 | // return value |
| 661 | - if( isset($row['value'][ $row['i'] ][ $selector ]) ) { | |
| 662 | - | |
| 625 | + if ( isset( $row['value'][ $row['i'] ][ $selector ] ) ) { | |
| 626 | + | |
| 663 | 627 | return $row['value'][ $row['i'] ][ $selector ]; |
| 664 | - | |
| 628 | + | |
| 665 | 629 | } |
| 666 | - | |
| 667 | - | |
| 630 | + | |
| 668 | 631 | // return |
| 669 | 632 | return null; |
| 670 | - | |
| 633 | + | |
| 671 | 634 | } |
| 672 | 635 | |
| 673 | 636 | |
| 674 | 637 | /* |
| @@ -676,25 +639,24 @@ | ||
| 676 | 639 | * |
| 677 | 640 | * This function will find the current loop and unset it from the global array. |
| 678 | 641 | * To bo used when loop finishes or a break is used |
| 679 | 642 | * |
| 680 | -* @type function | |
| 681 | -* @date 26/10/13 | |
| 682 | -* @since 5.0.0 | |
| 643 | +* @type function | |
| 644 | +* @date 26/10/13 | |
| 645 | +* @since 5.0.0 | |
| 683 | 646 | * |
| 684 | -* @param $hard_reset (boolean) completely wipe the global variable, or just unset the active row | |
| 685 | -* @return (boolean) | |
| 647 | +* @param $hard_reset (boolean) completely wipe the global variable, or just unset the active row | |
| 648 | +* @return (boolean) | |
| 686 | 649 | */ |
| 687 | 650 | |
| 688 | 651 | function reset_rows() { |
| 689 | - | |
| 652 | + | |
| 690 | 653 | // remove last loop |
| 691 | - acf_remove_loop('active'); | |
| 692 | - | |
| 693 | - | |
| 654 | + acf_remove_loop( 'active' ); | |
| 655 | + | |
| 694 | 656 | // return |
| 695 | 657 | return true; |
| 696 | - | |
| 658 | + | |
| 697 | 659 | } |
| 698 | 660 | |
| 699 | 661 | |
| 700 | 662 | /* |
| @@ -700,43 +662,41 @@ | ||
| 700 | 662 | /* |
| 701 | 663 | * has_sub_field() |
| 702 | 664 | * |
| 703 | 665 | * This function is used inside a while loop to return either true or false (loop again or stop). |
| 704 | -* When using a repeater or flexible content field, it will loop through the rows until | |
| 666 | +* When using a repeater or flexible content field, it will loop through the rows until | |
| 705 | 667 | * there are none left or a break is detected |
| 706 | 668 | * |
| 707 | -* @type function | |
| 708 | -* @since 1.0.3 | |
| 709 | -* @date 29/01/13 | |
| 669 | +* @type function | |
| 670 | +* @since 1.0.3 | |
| 671 | +* @date 29/01/13 | |
| 710 | 672 | * |
| 711 | -* @param $field_name (string) the field name | |
| 712 | -* @param $post_id (mixed) the post_id of which the value is saved against | |
| 713 | -* @return (boolean) | |
| 673 | +* @param $field_name (string) the field name | |
| 674 | +* @param $post_id (mixed) the post_id of which the value is saved against | |
| 675 | +* @return (boolean) | |
| 714 | 676 | */ |
| 715 | 677 | |
| 716 | 678 | function has_sub_field( $field_name, $post_id = false ) { |
| 717 | - | |
| 679 | + | |
| 718 | 680 | // vars |
| 719 | 681 | $r = have_rows( $field_name, $post_id ); |
| 720 | - | |
| 721 | - | |
| 682 | + | |
| 722 | 683 | // if has rows, progress through 1 row for the while loop to work |
| 723 | - if( $r ) { | |
| 724 | - | |
| 684 | + if ( $r ) { | |
| 685 | + | |
| 725 | 686 | the_row(); |
| 726 | - | |
| 687 | + | |
| 727 | 688 | } |
| 728 | - | |
| 729 | - | |
| 689 | + | |
| 730 | 690 | // return |
| 731 | 691 | return $r; |
| 732 | - | |
| 692 | + | |
| 733 | 693 | } |
| 734 | 694 | |
| 735 | 695 | function has_sub_fields( $field_name, $post_id = false ) { |
| 736 | - | |
| 696 | + | |
| 737 | 697 | return has_sub_field( $field_name, $post_id ); |
| 738 | - | |
| 698 | + | |
| 739 | 699 | } |
| 740 | 700 | |
| 741 | 701 | |
| 742 | 702 | /* |
| @@ -743,29 +703,29 @@ | ||
| 743 | 703 | * get_sub_field() |
| 744 | 704 | * |
| 745 | 705 | * This function is used inside a 'has_sub_field' while loop to return a sub field value |
| 746 | 706 | * |
| 747 | -* @type function | |
| 748 | -* @since 1.0.3 | |
| 749 | -* @date 29/01/13 | |
| 707 | +* @type function | |
| 708 | +* @since 1.0.3 | |
| 709 | +* @date 29/01/13 | |
| 750 | 710 | * |
| 751 | -* @param $field_name (string) the field name | |
| 752 | -* @return (mixed) | |
| 711 | +* @param $field_name (string) the field name | |
| 712 | +* @return (mixed) | |
| 753 | 713 | */ |
| 754 | 714 | |
| 755 | 715 | function get_sub_field( $selector = '', $format_value = true ) { |
| 756 | - | |
| 716 | + | |
| 757 | 717 | // get sub field |
| 758 | 718 | $sub_field = get_sub_field_object( $selector, $format_value ); |
| 759 | - | |
| 760 | - | |
| 719 | + | |
| 761 | 720 | // bail early if no sub field |
| 762 | - if( !$sub_field ) return false; | |
| 763 | - | |
| 764 | - | |
| 765 | - // return | |
| 721 | + if ( ! $sub_field ) { | |
| 722 | + return false; | |
| 723 | + } | |
| 724 | + | |
| 725 | + // return | |
| 766 | 726 | return $sub_field['value']; |
| 767 | - | |
| 727 | + | |
| 768 | 728 | } |
| 769 | 729 | |
| 770 | 730 | |
| 771 | 731 | /* |
| @@ -772,26 +732,26 @@ | ||
| 772 | 732 | * the_sub_field() |
| 773 | 733 | * |
| 774 | 734 | * This function is the same as echo get_sub_field |
| 775 | 735 | * |
| 776 | -* @type function | |
| 777 | -* @since 1.0.3 | |
| 778 | -* @date 29/01/13 | |
| 736 | +* @type function | |
| 737 | +* @since 1.0.3 | |
| 738 | +* @date 29/01/13 | |
| 779 | 739 | * |
| 780 | -* @param $field_name (string) the field name | |
| 781 | -* @return n/a | |
| 740 | +* @param $field_name (string) the field name | |
| 741 | +* @return n/a | |
| 782 | 742 | */ |
| 783 | 743 | |
| 784 | 744 | function the_sub_field( $field_name, $format_value = true ) { |
| 785 | - | |
| 745 | + | |
| 786 | 746 | $value = get_sub_field( $field_name, $format_value ); |
| 787 | - | |
| 788 | - if( is_array($value) ) { | |
| 789 | - | |
| 790 | - $value = implode(', ',$value); | |
| 791 | - | |
| 747 | + | |
| 748 | + if ( is_array( $value ) ) { | |
| 749 | + | |
| 750 | + $value = implode( ', ', $value ); | |
| 751 | + | |
| 792 | 752 | } |
| 793 | - | |
| 753 | + | |
| 794 | 754 | echo $value; |
| 795 | 755 | } |
| 796 | 756 | |
| 797 | 757 | |
| @@ -799,54 +759,52 @@ | ||
| 799 | 759 | * get_sub_field_object() |
| 800 | 760 | * |
| 801 | 761 | * This function is used inside a 'has_sub_field' while loop to return a sub field object |
| 802 | 762 | * |
| 803 | -* @type function | |
| 804 | -* @since 3.5.8.1 | |
| 805 | -* @date 29/01/13 | |
| 763 | +* @type function | |
| 764 | +* @since 3.5.8.1 | |
| 765 | +* @date 29/01/13 | |
| 806 | 766 | * |
| 807 | -* @param $child_name (string) the field name | |
| 808 | -* @return (array) | |
| 767 | +* @param $child_name (string) the field name | |
| 768 | +* @return (array) | |
| 809 | 769 | */ |
| 810 | 770 | |
| 811 | 771 | function get_sub_field_object( $selector, $format_value = true, $load_value = true ) { |
| 812 | - | |
| 772 | + | |
| 813 | 773 | // vars |
| 814 | - $row = acf_get_loop('active'); | |
| 815 | - | |
| 816 | - | |
| 774 | + $row = acf_get_loop( 'active' ); | |
| 775 | + | |
| 817 | 776 | // bail early if no row |
| 818 | - if( !$row ) return false; | |
| 819 | - | |
| 820 | - | |
| 777 | + if ( ! $row ) { | |
| 778 | + return false; | |
| 779 | + } | |
| 780 | + | |
| 821 | 781 | // attempt to find sub field |
| 822 | - $sub_field = get_row_sub_field($selector); | |
| 823 | - | |
| 824 | - | |
| 782 | + $sub_field = get_row_sub_field( $selector ); | |
| 783 | + | |
| 825 | 784 | // bail early if no sub field |
| 826 | - if( !$sub_field ) return false; | |
| 827 | - | |
| 828 | - | |
| 785 | + if ( ! $sub_field ) { | |
| 786 | + return false; | |
| 787 | + } | |
| 788 | + | |
| 829 | 789 | // load value |
| 830 | - if( $load_value ) { | |
| 831 | - | |
| 790 | + if ( $load_value ) { | |
| 791 | + | |
| 832 | 792 | $sub_field['value'] = get_row_sub_value( $sub_field['key'] ); |
| 833 | - | |
| 793 | + | |
| 834 | 794 | } |
| 835 | - | |
| 836 | - | |
| 795 | + | |
| 837 | 796 | // format value |
| 838 | - if( $format_value ) { | |
| 839 | - | |
| 797 | + if ( $format_value ) { | |
| 798 | + | |
| 840 | 799 | // get value for field |
| 841 | 800 | $sub_field['value'] = acf_format_value( $sub_field['value'], $row['post_id'], $sub_field ); |
| 842 | - | |
| 801 | + | |
| 843 | 802 | } |
| 844 | - | |
| 845 | - | |
| 803 | + | |
| 846 | 804 | // return |
| 847 | 805 | return $sub_field; |
| 848 | - | |
| 806 | + | |
| 849 | 807 | } |
| 850 | 808 | |
| 851 | 809 | |
| 852 | 810 | /* |
| @@ -853,122 +811,142 @@ | ||
| 853 | 811 | * get_row_layout() |
| 854 | 812 | * |
| 855 | 813 | * This function will return a string representation of the current row layout within a 'have_rows' loop |
| 856 | 814 | * |
| 857 | -* @type function | |
| 858 | -* @since 3.0.6 | |
| 859 | -* @date 29/01/13 | |
| 815 | +* @type function | |
| 816 | +* @since 3.0.6 | |
| 817 | +* @date 29/01/13 | |
| 860 | 818 | * |
| 861 | -* @param n/a | |
| 862 | -* @return (string) | |
| 819 | +* @param n/a | |
| 820 | +* @return (string) | |
| 863 | 821 | */ |
| 864 | 822 | |
| 865 | 823 | function get_row_layout() { |
| 866 | - | |
| 824 | + | |
| 867 | 825 | // vars |
| 868 | 826 | $row = get_row(); |
| 869 | - | |
| 870 | - | |
| 827 | + | |
| 871 | 828 | // return |
| 872 | - if( isset($row['acf_fc_layout']) ) { | |
| 873 | - | |
| 829 | + if ( isset( $row['acf_fc_layout'] ) ) { | |
| 830 | + | |
| 874 | 831 | return $row['acf_fc_layout']; |
| 875 | - | |
| 832 | + | |
| 876 | 833 | } |
| 877 | - | |
| 878 | - | |
| 834 | + | |
| 879 | 835 | // return |
| 880 | 836 | return false; |
| 881 | - | |
| 837 | + | |
| 882 | 838 | } |
| 883 | 839 | |
| 840 | +/** | |
| 841 | + * This function is used to add basic shortcode support for the ACF plugin | |
| 842 | + * eg. [acf field="heading" post_id="123" format_value="1"] | |
| 843 | + * | |
| 844 | + * @since 1.1.1 | |
| 845 | + * @date 29/01/13 | |
| 846 | + * | |
| 847 | + * @param array $atts The shortcode attributes. | |
| 848 | + * | |
| 849 | + * @return string | |
| 850 | + */ | |
| 851 | +function acf_shortcode( $atts ) { | |
| 852 | + // Return if the ACF shortcode is disabled. | |
| 853 | + if ( ! acf_get_setting( 'enable_shortcode' ) ) { | |
| 854 | + return; | |
| 855 | + } | |
| 884 | 856 | |
| 885 | -/* | |
| 886 | -* acf_shortcode() | |
| 887 | -* | |
| 888 | -* This function is used to add basic shortcode support for the ACF plugin | |
| 889 | -* eg. [acf field="heading" post_id="123" format_value="1"] | |
| 890 | -* | |
| 891 | -* @type function | |
| 892 | -* @since 1.1.1 | |
| 893 | -* @date 29/01/13 | |
| 894 | -* | |
| 895 | -* @param $field (string) the field name or key | |
| 896 | -* @param $post_id (mixed) the post_id of which the value is saved against | |
| 897 | -* @param $format_value (boolean) whether or not to format the field value | |
| 898 | -* @return (string) | |
| 899 | -*/ | |
| 857 | + if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) { | |
| 858 | + // Prevent the ACF shortcode in FSE block template parts by default. | |
| 859 | + if ( ! doing_filter( 'the_content' ) && ! apply_filters( 'acf/shortcode/allow_in_block_themes_outside_content', false ) ) { | |
| 860 | + return; | |
| 861 | + } | |
| 862 | + } | |
| 900 | 863 | |
| 901 | -function acf_shortcode( $atts ) { | |
| 902 | - | |
| 903 | - // extract attributs | |
| 904 | - extract( shortcode_atts( array( | |
| 905 | - 'field' => '', | |
| 906 | - 'post_id' => false, | |
| 907 | - 'format_value' => true | |
| 908 | - ), $atts ) ); | |
| 909 | - | |
| 910 | - | |
| 911 | - // get value and return it | |
| 912 | - $value = get_field( $field, $post_id, $format_value ); | |
| 913 | - | |
| 914 | - | |
| 915 | - // array | |
| 916 | - if( is_array($value) ) { | |
| 917 | - | |
| 918 | - $value = @implode( ', ', $value ); | |
| 919 | - | |
| 864 | + // Limit previews of ACF shortcode data for users without publish_posts permissions. | |
| 865 | + $preview_capability = apply_filters( 'acf/shortcode/preview_capability', 'publish_posts' ); | |
| 866 | + if ( is_preview() && ! current_user_can( $preview_capability ) ) { | |
| 867 | + return apply_filters( 'acf/shortcode/preview_capability_message', __( '[ACF shortcode value disabled for preview]', 'acf' ) ); | |
| 920 | 868 | } |
| 921 | - | |
| 922 | - | |
| 923 | - // return | |
| 869 | + | |
| 870 | + // Mitigate issue where some AJAX requests can return ACF field data. | |
| 871 | + $ajax_capability = apply_filters( 'acf/ajax/shortcode_capability', 'edit_posts' ); | |
| 872 | + if ( wp_doing_ajax() && ( $ajax_capability !== false ) && ! current_user_can( $ajax_capability ) ) { | |
| 873 | + return; | |
| 874 | + } | |
| 875 | + | |
| 876 | + $atts = shortcode_atts( | |
| 877 | + array( | |
| 878 | + 'field' => '', | |
| 879 | + 'post_id' => false, | |
| 880 | + 'format_value' => true, | |
| 881 | + ), | |
| 882 | + $atts, | |
| 883 | + 'acf' | |
| 884 | + ); | |
| 885 | + | |
| 886 | + $access_already_prevented = apply_filters( 'acf/prevent_access_to_unknown_fields', false ); | |
| 887 | + $filter_applied = false; | |
| 888 | + | |
| 889 | + if ( ! $access_already_prevented ) { | |
| 890 | + $filter_applied = true; | |
| 891 | + add_filter( 'acf/prevent_access_to_unknown_fields', '__return_true' ); | |
| 892 | + } | |
| 893 | + | |
| 894 | + // Try to get the field value. | |
| 895 | + $value = get_field( $atts['field'], $atts['post_id'], $atts['format_value'] ); | |
| 896 | + | |
| 897 | + if ( $filter_applied ) { | |
| 898 | + remove_filter( 'acf/prevent_access_to_unknown_fields', '__return_true' ); | |
| 899 | + } | |
| 900 | + | |
| 901 | + if ( is_array( $value ) ) { | |
| 902 | + $value = implode( ', ', $value ); | |
| 903 | + } | |
| 904 | + | |
| 924 | 905 | return $value; |
| 925 | - | |
| 926 | 906 | } |
| 907 | +add_shortcode( 'acf', 'acf_shortcode' ); | |
| 927 | 908 | |
| 928 | -add_shortcode('acf', 'acf_shortcode'); | |
| 929 | 909 | |
| 930 | - | |
| 931 | 910 | /* |
| 932 | 911 | * update_field() |
| 933 | 912 | * |
| 934 | 913 | * This function will update a value in the database |
| 935 | 914 | * |
| 936 | -* @type function | |
| 937 | -* @since 3.1.9 | |
| 938 | -* @date 29/01/13 | |
| 915 | +* @type function | |
| 916 | +* @since 3.1.9 | |
| 917 | +* @date 29/01/13 | |
| 939 | 918 | * |
| 940 | -* @param $selector (string) the field name or key | |
| 941 | -* @param $value (mixed) the value to save in the database | |
| 942 | -* @param $post_id (mixed) the post_id of which the value is saved against | |
| 943 | -* @return (boolean) | |
| 919 | +* @param $selector (string) the field name or key | |
| 920 | +* @param $value (mixed) the value to save in the database | |
| 921 | +* @param $post_id (mixed) the post_id of which the value is saved against | |
| 922 | +* @return (boolean) | |
| 944 | 923 | */ |
| 945 | 924 | |
| 946 | 925 | function update_field( $selector, $value, $post_id = false ) { |
| 947 | - | |
| 926 | + | |
| 948 | 927 | // filter post_id |
| 949 | 928 | $post_id = acf_get_valid_post_id( $post_id ); |
| 950 | - | |
| 951 | - | |
| 929 | + | |
| 952 | 930 | // get field |
| 953 | 931 | $field = acf_maybe_get_field( $selector, $post_id, false ); |
| 954 | - | |
| 955 | - | |
| 932 | + | |
| 956 | 933 | // create dummy field |
| 957 | - if( !$field ) { | |
| 958 | - | |
| 959 | - $field = acf_get_valid_field(array( | |
| 960 | - 'name' => $selector, | |
| 961 | - 'key' => '', | |
| 962 | - 'type' => '', | |
| 963 | - )); | |
| 964 | - | |
| 934 | + if ( ! $field ) { | |
| 935 | + | |
| 936 | + $field = acf_get_valid_field( | |
| 937 | + array( | |
| 938 | + 'name' => $selector, | |
| 939 | + 'key' => '', | |
| 940 | + 'type' => '', | |
| 941 | + ) | |
| 942 | + ); | |
| 943 | + | |
| 965 | 944 | } |
| 966 | - | |
| 967 | - | |
| 945 | + | |
| 968 | 946 | // save |
| 969 | 947 | return acf_update_value( $value, $post_id, $field ); |
| 970 | - | |
| 948 | + | |
| 971 | 949 | } |
| 972 | 950 | |
| 973 | 951 | |
| 974 | 952 | /* |
| @@ -975,45 +953,44 @@ | ||
| 975 | 953 | * update_sub_field |
| 976 | 954 | * |
| 977 | 955 | * This function will update a value of a sub field in the database |
| 978 | 956 | * |
| 979 | -* @type function | |
| 980 | -* @date 2/04/2014 | |
| 981 | -* @since 5.0.0 | |
| 957 | +* @type function | |
| 958 | +* @date 2/04/2014 | |
| 959 | +* @since 5.0.0 | |
| 982 | 960 | * |
| 983 | -* @param $selector (mixed) the sub field name or key, or an array of ancestors | |
| 984 | -* @param $value (mixed) the value to save in the database | |
| 985 | -* @param $post_id (mixed) the post_id of which the value is saved against | |
| 986 | -* @return (boolean) | |
| 961 | +* @param $selector (mixed) the sub field name or key, or an array of ancestors | |
| 962 | +* @param $value (mixed) the value to save in the database | |
| 963 | +* @param $post_id (mixed) the post_id of which the value is saved against | |
| 964 | +* @return (boolean) | |
| 987 | 965 | */ |
| 988 | 966 | |
| 989 | 967 | function update_sub_field( $selector, $value, $post_id = false ) { |
| 990 | - | |
| 968 | + | |
| 991 | 969 | // vars |
| 992 | 970 | $sub_field = false; |
| 993 | - | |
| 994 | - | |
| 971 | + | |
| 995 | 972 | // get sub field |
| 996 | - if( is_array($selector) ) { | |
| 997 | - | |
| 998 | - $post_id = acf_get_valid_post_id( $post_id ); | |
| 973 | + if ( is_array( $selector ) ) { | |
| 974 | + | |
| 975 | + $post_id = acf_get_valid_post_id( $post_id ); | |
| 999 | 976 | $sub_field = acf_maybe_get_sub_field( $selector, $post_id, false ); |
| 1000 | - | |
| 977 | + | |
| 1001 | 978 | } else { |
| 1002 | - | |
| 1003 | - $post_id = acf_get_loop('active', 'post_id'); | |
| 979 | + | |
| 980 | + $post_id = acf_get_loop( 'active', 'post_id' ); | |
| 1004 | 981 | $sub_field = get_row_sub_field( $selector ); |
| 1005 | - | |
| 982 | + | |
| 1006 | 983 | } |
| 1007 | - | |
| 1008 | - | |
| 984 | + | |
| 1009 | 985 | // bail early if no sub field |
| 1010 | - if( !$sub_field ) return false; | |
| 986 | + if ( ! $sub_field ) { | |
| 987 | + return false; | |
| 988 | + } | |
| 1011 | 989 | |
| 1012 | - | |
| 1013 | 990 | // update |
| 1014 | 991 | return acf_update_value( $value, $post_id, $sub_field ); |
| 1015 | - | |
| 992 | + | |
| 1016 | 993 | } |
| 1017 | 994 | |
| 1018 | 995 | |
| 1019 | 996 | /* |
| @@ -1020,30 +997,28 @@ | ||
| 1020 | 997 | * delete_field() |
| 1021 | 998 | * |
| 1022 | 999 | * This function will remove a value from the database |
| 1023 | 1000 | * |
| 1024 | -* @type function | |
| 1025 | -* @since 3.1.9 | |
| 1026 | -* @date 29/01/13 | |
| 1001 | +* @type function | |
| 1002 | +* @since 3.1.9 | |
| 1003 | +* @date 29/01/13 | |
| 1027 | 1004 | * |
| 1028 | -* @param $selector (string) the field name or key | |
| 1029 | -* @param $post_id (mixed) the post_id of which the value is saved against | |
| 1030 | -* @return (boolean) | |
| 1005 | +* @param $selector (string) the field name or key | |
| 1006 | +* @param $post_id (mixed) the post_id of which the value is saved against | |
| 1007 | +* @return (boolean) | |
| 1031 | 1008 | */ |
| 1032 | 1009 | |
| 1033 | 1010 | function delete_field( $selector, $post_id = false ) { |
| 1034 | - | |
| 1011 | + | |
| 1035 | 1012 | // filter post_id |
| 1036 | 1013 | $post_id = acf_get_valid_post_id( $post_id ); |
| 1037 | - | |
| 1038 | - | |
| 1014 | + | |
| 1039 | 1015 | // get field |
| 1040 | 1016 | $field = acf_maybe_get_field( $selector, $post_id ); |
| 1041 | - | |
| 1042 | - | |
| 1017 | + | |
| 1043 | 1018 | // delete |
| 1044 | - return acf_delete_value( $post_id, $field ); | |
| 1045 | - | |
| 1019 | + return $field ? acf_delete_value( $post_id, $field ) : false; | |
| 1020 | + | |
| 1046 | 1021 | } |
| 1047 | 1022 | |
| 1048 | 1023 | |
| 1049 | 1024 | /* |
| @@ -1050,22 +1025,22 @@ | ||
| 1050 | 1025 | * delete_sub_field |
| 1051 | 1026 | * |
| 1052 | 1027 | * This function will delete a value of a sub field in the database |
| 1053 | 1028 | * |
| 1054 | -* @type function | |
| 1055 | -* @date 2/04/2014 | |
| 1056 | -* @since 5.0.0 | |
| 1029 | +* @type function | |
| 1030 | +* @date 2/04/2014 | |
| 1031 | +* @since 5.0.0 | |
| 1057 | 1032 | * |
| 1058 | -* @param $selector (mixed) the sub field name or key, or an array of ancestors | |
| 1059 | -* @param $value (mixed) the value to save in the database | |
| 1060 | -* @param $post_id (mixed) the post_id of which the value is saved against | |
| 1061 | -* @return (boolean) | |
| 1033 | +* @param $selector (mixed) the sub field name or key, or an array of ancestors | |
| 1034 | +* @param $value (mixed) the value to save in the database | |
| 1035 | +* @param $post_id (mixed) the post_id of which the value is saved against | |
| 1036 | +* @return (boolean) | |
| 1062 | 1037 | */ |
| 1063 | 1038 | |
| 1064 | 1039 | function delete_sub_field( $selector, $post_id = false ) { |
| 1065 | - | |
| 1040 | + | |
| 1066 | 1041 | return update_sub_field( $selector, null, $post_id ); |
| 1067 | - | |
| 1042 | + | |
| 1068 | 1043 | } |
| 1069 | 1044 | |
| 1070 | 1045 | |
| 1071 | 1046 | /* |
| @@ -1072,51 +1047,49 @@ | ||
| 1072 | 1047 | * add_row |
| 1073 | 1048 | * |
| 1074 | 1049 | * This function will add a row of data to a field |
| 1075 | 1050 | * |
| 1076 | -* @type function | |
| 1077 | -* @date 16/10/2015 | |
| 1078 | -* @since 5.2.3 | |
| 1051 | +* @type function | |
| 1052 | +* @date 16/10/2015 | |
| 1053 | +* @since 5.2.3 | |
| 1079 | 1054 | * |
| 1080 | -* @param $selector (string) | |
| 1081 | -* @param $row (array) | |
| 1082 | -* @param $post_id (mixed) | |
| 1083 | -* @return (boolean) | |
| 1055 | +* @param $selector (string) | |
| 1056 | +* @param $row (array) | |
| 1057 | +* @param $post_id (mixed) | |
| 1058 | +* @return (boolean) | |
| 1084 | 1059 | */ |
| 1085 | 1060 | |
| 1086 | 1061 | function add_row( $selector, $row = false, $post_id = false ) { |
| 1087 | - | |
| 1062 | + | |
| 1088 | 1063 | // filter post_id |
| 1089 | 1064 | $post_id = acf_get_valid_post_id( $post_id ); |
| 1090 | - | |
| 1091 | - | |
| 1065 | + | |
| 1092 | 1066 | // get field |
| 1093 | 1067 | $field = acf_maybe_get_field( $selector, $post_id, false ); |
| 1094 | - | |
| 1095 | - | |
| 1068 | + | |
| 1096 | 1069 | // bail early if no field |
| 1097 | - if( !$field ) return false; | |
| 1098 | - | |
| 1099 | - | |
| 1070 | + if ( ! $field ) { | |
| 1071 | + return false; | |
| 1072 | + } | |
| 1073 | + | |
| 1100 | 1074 | // get raw value |
| 1101 | 1075 | $value = acf_get_value( $post_id, $field ); |
| 1102 | - | |
| 1103 | - | |
| 1076 | + | |
| 1104 | 1077 | // ensure array |
| 1105 | - $value = acf_get_array($value); | |
| 1106 | - | |
| 1107 | - | |
| 1078 | + $value = acf_get_array( $value ); | |
| 1079 | + | |
| 1108 | 1080 | // append |
| 1109 | 1081 | $value[] = $row; |
| 1110 | - | |
| 1111 | - | |
| 1082 | + | |
| 1083 | + // Paginated repeaters should be saved normally. | |
| 1084 | + $field['pagination'] = false; | |
| 1085 | + | |
| 1112 | 1086 | // update value |
| 1113 | 1087 | acf_update_value( $value, $post_id, $field ); |
| 1114 | - | |
| 1115 | - | |
| 1088 | + | |
| 1116 | 1089 | // return |
| 1117 | - return count($value); | |
| 1118 | - | |
| 1090 | + return count( $value ); | |
| 1091 | + | |
| 1119 | 1092 | } |
| 1120 | 1093 | |
| 1121 | 1094 | |
| 1122 | 1095 | /* |
| @@ -1123,61 +1096,56 @@ | ||
| 1123 | 1096 | * add_sub_row |
| 1124 | 1097 | * |
| 1125 | 1098 | * This function will add a row of data to a field |
| 1126 | 1099 | * |
| 1127 | -* @type function | |
| 1128 | -* @date 16/10/2015 | |
| 1129 | -* @since 5.2.3 | |
| 1100 | +* @type function | |
| 1101 | +* @date 16/10/2015 | |
| 1102 | +* @since 5.2.3 | |
| 1130 | 1103 | * |
| 1131 | -* @param $selector (string) | |
| 1132 | -* @param $row (array) | |
| 1133 | -* @param $post_id (mixed) | |
| 1134 | -* @return (boolean) | |
| 1104 | +* @param $selector (string) | |
| 1105 | +* @param $row (array) | |
| 1106 | +* @param $post_id (mixed) | |
| 1107 | +* @return (boolean) | |
| 1135 | 1108 | */ |
| 1136 | 1109 | |
| 1137 | 1110 | function add_sub_row( $selector, $row = false, $post_id = false ) { |
| 1138 | - | |
| 1111 | + | |
| 1139 | 1112 | // vars |
| 1140 | 1113 | $sub_field = false; |
| 1141 | - | |
| 1142 | - | |
| 1114 | + | |
| 1143 | 1115 | // get sub field |
| 1144 | - if( is_array($selector) ) { | |
| 1145 | - | |
| 1146 | - $post_id = acf_get_valid_post_id( $post_id ); | |
| 1116 | + if ( is_array( $selector ) ) { | |
| 1117 | + | |
| 1118 | + $post_id = acf_get_valid_post_id( $post_id ); | |
| 1147 | 1119 | $sub_field = acf_maybe_get_sub_field( $selector, $post_id, false ); |
| 1148 | - | |
| 1120 | + | |
| 1149 | 1121 | } else { |
| 1150 | - | |
| 1151 | - $post_id = acf_get_loop('active', 'post_id'); | |
| 1122 | + | |
| 1123 | + $post_id = acf_get_loop( 'active', 'post_id' ); | |
| 1152 | 1124 | $sub_field = get_row_sub_field( $selector ); |
| 1153 | - | |
| 1125 | + | |
| 1154 | 1126 | } |
| 1155 | - | |
| 1156 | - | |
| 1127 | + | |
| 1157 | 1128 | // bail early if no sub field |
| 1158 | - if( !$sub_field ) return false; | |
| 1159 | - | |
| 1160 | - | |
| 1129 | + if ( ! $sub_field ) { | |
| 1130 | + return false; | |
| 1131 | + } | |
| 1132 | + | |
| 1161 | 1133 | // get raw value |
| 1162 | 1134 | $value = acf_get_value( $post_id, $sub_field ); |
| 1163 | - | |
| 1164 | - | |
| 1135 | + | |
| 1165 | 1136 | // ensure array |
| 1166 | 1137 | $value = acf_get_array( $value ); |
| 1167 | - | |
| 1168 | - | |
| 1138 | + | |
| 1169 | 1139 | // append |
| 1170 | 1140 | $value[] = $row; |
| 1171 | 1141 | |
| 1172 | - | |
| 1173 | 1142 | // update |
| 1174 | 1143 | acf_update_value( $value, $post_id, $sub_field ); |
| 1175 | - | |
| 1176 | - | |
| 1144 | + | |
| 1177 | 1145 | // return |
| 1178 | - return count($value); | |
| 1179 | - | |
| 1146 | + return count( $value ); | |
| 1147 | + | |
| 1180 | 1148 | } |
| 1181 | 1149 | |
| 1182 | 1150 | |
| 1183 | 1151 | /* |
| @@ -1184,57 +1152,51 @@ | ||
| 1184 | 1152 | * update_row |
| 1185 | 1153 | * |
| 1186 | 1154 | * This function will update a row of data to a field |
| 1187 | 1155 | * |
| 1188 | -* @type function | |
| 1189 | -* @date 19/10/2015 | |
| 1190 | -* @since 5.2.3 | |
| 1156 | +* @type function | |
| 1157 | +* @date 19/10/2015 | |
| 1158 | +* @since 5.2.3 | |
| 1191 | 1159 | * |
| 1192 | -* @param $selector (string) | |
| 1193 | -* @param $i (int) | |
| 1194 | -* @param $row (array) | |
| 1195 | -* @param $post_id (mixed) | |
| 1196 | -* @return (boolean) | |
| 1160 | +* @param $selector (string) | |
| 1161 | +* @param $i (int) | |
| 1162 | +* @param $row (array) | |
| 1163 | +* @param $post_id (mixed) | |
| 1164 | +* @return (boolean) | |
| 1197 | 1165 | */ |
| 1198 | 1166 | |
| 1199 | 1167 | function update_row( $selector, $i = 1, $row = false, $post_id = false ) { |
| 1200 | - | |
| 1168 | + | |
| 1201 | 1169 | // vars |
| 1202 | - $offset = acf_get_setting('row_index_offset'); | |
| 1203 | - $i = $i - $offset; | |
| 1204 | - | |
| 1205 | - | |
| 1170 | + $offset = acf_get_setting( 'row_index_offset' ); | |
| 1171 | + $i = $i - $offset; | |
| 1172 | + | |
| 1206 | 1173 | // filter post_id |
| 1207 | 1174 | $post_id = acf_get_valid_post_id( $post_id ); |
| 1208 | - | |
| 1209 | - | |
| 1175 | + | |
| 1210 | 1176 | // get field |
| 1211 | 1177 | $field = acf_maybe_get_field( $selector, $post_id, false ); |
| 1212 | - | |
| 1213 | - | |
| 1178 | + | |
| 1214 | 1179 | // bail early if no field |
| 1215 | - if( !$field ) return false; | |
| 1216 | - | |
| 1217 | - | |
| 1180 | + if ( ! $field ) { | |
| 1181 | + return false; | |
| 1182 | + } | |
| 1183 | + | |
| 1218 | 1184 | // get raw value |
| 1219 | 1185 | $value = acf_get_value( $post_id, $field ); |
| 1220 | - | |
| 1221 | - | |
| 1186 | + | |
| 1222 | 1187 | // ensure array |
| 1223 | - $value = acf_get_array($value); | |
| 1224 | - | |
| 1225 | - | |
| 1188 | + $value = acf_get_array( $value ); | |
| 1189 | + | |
| 1226 | 1190 | // update |
| 1227 | 1191 | $value[ $i ] = $row; |
| 1228 | - | |
| 1229 | - | |
| 1192 | + | |
| 1230 | 1193 | // update value |
| 1231 | 1194 | acf_update_value( $value, $post_id, $field ); |
| 1232 | - | |
| 1233 | - | |
| 1195 | + | |
| 1234 | 1196 | // return |
| 1235 | 1197 | return true; |
| 1236 | - | |
| 1198 | + | |
| 1237 | 1199 | } |
| 1238 | 1200 | |
| 1239 | 1201 | |
| 1240 | 1202 | /* |
| @@ -1241,63 +1203,58 @@ | ||
| 1241 | 1203 | * update_sub_row |
| 1242 | 1204 | * |
| 1243 | 1205 | * This function will add a row of data to a field |
| 1244 | 1206 | * |
| 1245 | -* @type function | |
| 1246 | -* @date 16/10/2015 | |
| 1247 | -* @since 5.2.3 | |
| 1207 | +* @type function | |
| 1208 | +* @date 16/10/2015 | |
| 1209 | +* @since 5.2.3 | |
| 1248 | 1210 | * |
| 1249 | -* @param $selector (string) | |
| 1250 | -* @param $row (array) | |
| 1251 | -* @param $post_id (mixed) | |
| 1252 | -* @return (boolean) | |
| 1211 | +* @param $selector (string) | |
| 1212 | +* @param $row (array) | |
| 1213 | +* @param $post_id (mixed) | |
| 1214 | +* @return (boolean) | |
| 1253 | 1215 | */ |
| 1254 | 1216 | |
| 1255 | 1217 | function update_sub_row( $selector, $i = 1, $row = false, $post_id = false ) { |
| 1256 | - | |
| 1218 | + | |
| 1257 | 1219 | // vars |
| 1258 | 1220 | $sub_field = false; |
| 1259 | - $offset = acf_get_setting('row_index_offset'); | |
| 1260 | - $i = $i - $offset; | |
| 1261 | - | |
| 1262 | - | |
| 1221 | + $offset = acf_get_setting( 'row_index_offset' ); | |
| 1222 | + $i = $i - $offset; | |
| 1223 | + | |
| 1263 | 1224 | // get sub field |
| 1264 | - if( is_array($selector) ) { | |
| 1265 | - | |
| 1266 | - $post_id = acf_get_valid_post_id( $post_id ); | |
| 1225 | + if ( is_array( $selector ) ) { | |
| 1226 | + | |
| 1227 | + $post_id = acf_get_valid_post_id( $post_id ); | |
| 1267 | 1228 | $sub_field = acf_maybe_get_sub_field( $selector, $post_id, false ); |
| 1268 | - | |
| 1229 | + | |
| 1269 | 1230 | } else { |
| 1270 | - | |
| 1271 | - $post_id = acf_get_loop('active', 'post_id'); | |
| 1231 | + | |
| 1232 | + $post_id = acf_get_loop( 'active', 'post_id' ); | |
| 1272 | 1233 | $sub_field = get_row_sub_field( $selector ); |
| 1273 | - | |
| 1234 | + | |
| 1274 | 1235 | } |
| 1275 | - | |
| 1276 | - | |
| 1236 | + | |
| 1277 | 1237 | // bail early if no sub field |
| 1278 | - if( !$sub_field ) return false; | |
| 1279 | - | |
| 1280 | - | |
| 1238 | + if ( ! $sub_field ) { | |
| 1239 | + return false; | |
| 1240 | + } | |
| 1241 | + | |
| 1281 | 1242 | // get raw value |
| 1282 | 1243 | $value = acf_get_value( $post_id, $sub_field ); |
| 1283 | - | |
| 1284 | - | |
| 1244 | + | |
| 1285 | 1245 | // ensure array |
| 1286 | 1246 | $value = acf_get_array( $value ); |
| 1287 | - | |
| 1288 | - | |
| 1247 | + | |
| 1289 | 1248 | // append |
| 1290 | 1249 | $value[ $i ] = $row; |
| 1291 | 1250 | |
| 1292 | - | |
| 1293 | 1251 | // update |
| 1294 | 1252 | acf_update_value( $value, $post_id, $sub_field ); |
| 1295 | - | |
| 1296 | - | |
| 1253 | + | |
| 1297 | 1254 | // return |
| 1298 | 1255 | return true; |
| 1299 | - | |
| 1256 | + | |
| 1300 | 1257 | } |
| 1301 | 1258 | |
| 1302 | 1259 | |
| 1303 | 1260 | /* |
| @@ -1304,60 +1261,55 @@ | ||
| 1304 | 1261 | * delete_row |
| 1305 | 1262 | * |
| 1306 | 1263 | * This function will delete a row of data from a field |
| 1307 | 1264 | * |
| 1308 | -* @type function | |
| 1309 | -* @date 19/10/2015 | |
| 1310 | -* @since 5.2.3 | |
| 1265 | +* @type function | |
| 1266 | +* @date 19/10/2015 | |
| 1267 | +* @since 5.2.3 | |
| 1311 | 1268 | * |
| 1312 | -* @param $selector (string) | |
| 1313 | -* @param $i (int) | |
| 1314 | -* @param $post_id (mixed) | |
| 1315 | -* @return (boolean) | |
| 1269 | +* @param $selector (string) | |
| 1270 | +* @param $i (int) | |
| 1271 | +* @param $post_id (mixed) | |
| 1272 | +* @return (boolean) | |
| 1316 | 1273 | */ |
| 1317 | 1274 | |
| 1318 | 1275 | function delete_row( $selector, $i = 1, $post_id = false ) { |
| 1319 | - | |
| 1276 | + | |
| 1320 | 1277 | // vars |
| 1321 | - $offset = acf_get_setting('row_index_offset'); | |
| 1322 | - $i = $i - $offset; | |
| 1323 | - | |
| 1324 | - | |
| 1278 | + $offset = acf_get_setting( 'row_index_offset' ); | |
| 1279 | + $i = $i - $offset; | |
| 1280 | + | |
| 1325 | 1281 | // filter post_id |
| 1326 | 1282 | $post_id = acf_get_valid_post_id( $post_id ); |
| 1327 | - | |
| 1328 | - | |
| 1283 | + | |
| 1329 | 1284 | // get field |
| 1330 | 1285 | $field = acf_maybe_get_field( $selector, $post_id ); |
| 1331 | - | |
| 1332 | - | |
| 1286 | + | |
| 1333 | 1287 | // bail early if no field |
| 1334 | - if( !$field ) return false; | |
| 1335 | - | |
| 1336 | - | |
| 1288 | + if ( ! $field ) { | |
| 1289 | + return false; | |
| 1290 | + } | |
| 1291 | + | |
| 1337 | 1292 | // get value |
| 1338 | 1293 | $value = acf_get_value( $post_id, $field ); |
| 1339 | - | |
| 1340 | - | |
| 1294 | + | |
| 1341 | 1295 | // ensure array |
| 1342 | - $value = acf_get_array($value); | |
| 1343 | - | |
| 1344 | - | |
| 1296 | + $value = acf_get_array( $value ); | |
| 1297 | + | |
| 1345 | 1298 | // bail early if index doesn't exist |
| 1346 | - if( !isset($value[ $i ]) ) return false; | |
| 1347 | - | |
| 1348 | - | |
| 1299 | + if ( ! isset( $value[ $i ] ) ) { | |
| 1300 | + return false; | |
| 1301 | + } | |
| 1302 | + | |
| 1349 | 1303 | // unset |
| 1350 | 1304 | unset( $value[ $i ] ); |
| 1351 | - | |
| 1352 | - | |
| 1305 | + | |
| 1353 | 1306 | // update |
| 1354 | 1307 | acf_update_value( $value, $post_id, $field ); |
| 1355 | - | |
| 1356 | - | |
| 1308 | + | |
| 1357 | 1309 | // return |
| 1358 | 1310 | return true; |
| 1359 | - | |
| 1311 | + | |
| 1360 | 1312 | } |
| 1361 | 1313 | |
| 1362 | 1314 | |
| 1363 | 1315 | /* |
| @@ -1364,67 +1316,63 @@ | ||
| 1364 | 1316 | * delete_sub_row |
| 1365 | 1317 | * |
| 1366 | 1318 | * This function will add a row of data to a field |
| 1367 | 1319 | * |
| 1368 | -* @type function | |
| 1369 | -* @date 16/10/2015 | |
| 1370 | -* @since 5.2.3 | |
| 1320 | +* @type function | |
| 1321 | +* @date 16/10/2015 | |
| 1322 | +* @since 5.2.3 | |
| 1371 | 1323 | * |
| 1372 | -* @param $selector (string) | |
| 1373 | -* @param $row (array) | |
| 1374 | -* @param $post_id (mixed) | |
| 1375 | -* @return (boolean) | |
| 1324 | +* @param $selector (string) | |
| 1325 | +* @param $row (array) | |
| 1326 | +* @param $post_id (mixed) | |
| 1327 | +* @return (boolean) | |
| 1376 | 1328 | */ |
| 1377 | 1329 | |
| 1378 | 1330 | function delete_sub_row( $selector, $i = 1, $post_id = false ) { |
| 1379 | - | |
| 1331 | + | |
| 1380 | 1332 | // vars |
| 1381 | 1333 | $sub_field = false; |
| 1382 | - $offset = acf_get_setting('row_index_offset'); | |
| 1383 | - $i = $i - $offset; | |
| 1384 | - | |
| 1385 | - | |
| 1334 | + $offset = acf_get_setting( 'row_index_offset' ); | |
| 1335 | + $i = $i - $offset; | |
| 1336 | + | |
| 1386 | 1337 | // get sub field |
| 1387 | - if( is_array($selector) ) { | |
| 1388 | - | |
| 1389 | - $post_id = acf_get_valid_post_id( $post_id ); | |
| 1338 | + if ( is_array( $selector ) ) { | |
| 1339 | + | |
| 1340 | + $post_id = acf_get_valid_post_id( $post_id ); | |
| 1390 | 1341 | $sub_field = acf_maybe_get_sub_field( $selector, $post_id, false ); |
| 1391 | - | |
| 1342 | + | |
| 1392 | 1343 | } else { |
| 1393 | - | |
| 1394 | - $post_id = acf_get_loop('active', 'post_id'); | |
| 1344 | + | |
| 1345 | + $post_id = acf_get_loop( 'active', 'post_id' ); | |
| 1395 | 1346 | $sub_field = get_row_sub_field( $selector ); |
| 1396 | - | |
| 1347 | + | |
| 1397 | 1348 | } |
| 1398 | - | |
| 1399 | - | |
| 1349 | + | |
| 1400 | 1350 | // bail early if no sub field |
| 1401 | - if( !$sub_field ) return false; | |
| 1402 | - | |
| 1403 | - | |
| 1351 | + if ( ! $sub_field ) { | |
| 1352 | + return false; | |
| 1353 | + } | |
| 1354 | + | |
| 1404 | 1355 | // get raw value |
| 1405 | 1356 | $value = acf_get_value( $post_id, $sub_field ); |
| 1406 | - | |
| 1407 | - | |
| 1357 | + | |
| 1408 | 1358 | // ensure array |
| 1409 | 1359 | $value = acf_get_array( $value ); |
| 1410 | - | |
| 1411 | - | |
| 1360 | + | |
| 1412 | 1361 | // bail early if index doesn't exist |
| 1413 | - if( !isset($value[ $i ]) ) return false; | |
| 1414 | - | |
| 1415 | - | |
| 1362 | + if ( ! isset( $value[ $i ] ) ) { | |
| 1363 | + return false; | |
| 1364 | + } | |
| 1365 | + | |
| 1416 | 1366 | // append |
| 1417 | 1367 | unset( $value[ $i ] ); |
| 1418 | 1368 | |
| 1419 | - | |
| 1420 | 1369 | // update |
| 1421 | 1370 | acf_update_value( $value, $post_id, $sub_field ); |
| 1422 | - | |
| 1423 | - | |
| 1371 | + | |
| 1424 | 1372 | // return |
| 1425 | 1373 | return true; |
| 1426 | - | |
| 1374 | + | |
| 1427 | 1375 | } |
| 1428 | 1376 | |
| 1429 | 1377 | |
| 1430 | 1378 | /* |
| @@ -1431,49 +1379,49 @@ | ||
| 1431 | 1379 | * Depreceated Functions |
| 1432 | 1380 | * |
| 1433 | 1381 | * These functions are outdated |
| 1434 | 1382 | * |
| 1435 | -* @type function | |
| 1436 | -* @date 4/03/2014 | |
| 1437 | -* @since 1.0.0 | |
| 1383 | +* @type function | |
| 1384 | +* @date 4/03/2014 | |
| 1385 | +* @since 1.0.0 | |
| 1438 | 1386 | * |
| 1439 | -* @param n/a | |
| 1440 | -* @return n/a | |
| 1387 | +* @param n/a | |
| 1388 | +* @return n/a | |
| 1441 | 1389 | */ |
| 1442 | 1390 | |
| 1443 | 1391 | function create_field( $field ) { |
| 1444 | 1392 | |
| 1445 | 1393 | acf_render_field( $field ); |
| 1446 | - | |
| 1394 | + | |
| 1447 | 1395 | } |
| 1448 | 1396 | |
| 1449 | 1397 | function render_field( $field ) { |
| 1450 | 1398 | |
| 1451 | 1399 | acf_render_field( $field ); |
| 1452 | - | |
| 1400 | + | |
| 1453 | 1401 | } |
| 1454 | 1402 | |
| 1455 | 1403 | function reset_the_repeater_field() { |
| 1456 | - | |
| 1404 | + | |
| 1457 | 1405 | return reset_rows(); |
| 1458 | - | |
| 1406 | + | |
| 1459 | 1407 | } |
| 1460 | 1408 | |
| 1461 | 1409 | function the_repeater_field( $field_name, $post_id = false ) { |
| 1462 | - | |
| 1410 | + | |
| 1463 | 1411 | return has_sub_field( $field_name, $post_id ); |
| 1464 | - | |
| 1412 | + | |
| 1465 | 1413 | } |
| 1466 | 1414 | |
| 1467 | 1415 | function the_flexible_field( $field_name, $post_id = false ) { |
| 1468 | - | |
| 1416 | + | |
| 1469 | 1417 | return has_sub_field( $field_name, $post_id ); |
| 1470 | - | |
| 1418 | + | |
| 1471 | 1419 | } |
| 1472 | 1420 | |
| 1473 | 1421 | function acf_filter_post_id( $post_id ) { |
| 1474 | - | |
| 1422 | + | |
| 1475 | 1423 | return acf_get_valid_post_id( $post_id ); |
| 1476 | - | |
| 1424 | + | |
| 1477 | 1425 | } |
| 1478 | 1426 | |
| 1479 | -?> | |
| 1427 | + | |