| @@ -4,13 +4,13 @@ | ||
| 4 | 4 | * acf_get_taxonomies |
| 5 | 5 | * |
| 6 | 6 | * Returns an array of taxonomy names. |
| 7 | 7 | * |
| 8 | -* @date 7/10/13 | |
| 9 | -* @since 5.0.0 | |
| 8 | +* @date 7/10/13 | |
| 9 | +* @since 5.0.0 | |
| 10 | 10 | * |
| 11 | -* @param array $args An array of args used in the get_taxonomies() function. | |
| 12 | -* @return array An array of taxonomy names. | |
| 11 | +* @param array $args An array of args used in the get_taxonomies() function. | |
| 12 | +* @return array An array of taxonomy names. | |
| 13 | 13 | */ |
| 14 | 14 | |
| 15 | 15 | function acf_get_taxonomies( $args = array() ) { |
| 16 | 16 | |
| @@ -15,62 +15,64 @@ | ||
| 15 | 15 | function acf_get_taxonomies( $args = array() ) { |
| 16 | 16 | |
| 17 | 17 | // vars |
| 18 | 18 | $taxonomies = array(); |
| 19 | - | |
| 19 | + | |
| 20 | 20 | // get taxonomy objects |
| 21 | 21 | $objects = get_taxonomies( $args, 'objects' ); |
| 22 | - | |
| 22 | + | |
| 23 | 23 | // loop |
| 24 | - foreach( $objects as $i => $object ) { | |
| 25 | - | |
| 24 | + foreach ( $objects as $i => $object ) { | |
| 25 | + | |
| 26 | 26 | // bail early if is builtin (WP) private post type |
| 27 | 27 | // - nav_menu_item, revision, customize_changeset, etc |
| 28 | - if( $object->_builtin && !$object->public ) continue; | |
| 29 | - | |
| 28 | + if ( $object->_builtin && ! $object->public ) { | |
| 29 | + continue; | |
| 30 | + } | |
| 31 | + | |
| 30 | 32 | // append |
| 31 | 33 | $taxonomies[] = $i; |
| 32 | 34 | } |
| 33 | - | |
| 35 | + | |
| 34 | 36 | // custom post_type arg which does not yet exist in core |
| 35 | - if( isset($args['post_type']) ) { | |
| 36 | - $taxonomies = acf_get_taxonomies_for_post_type($args['post_type']); | |
| 37 | + if ( isset( $args['post_type'] ) ) { | |
| 38 | + $taxonomies = acf_get_taxonomies_for_post_type( $args['post_type'] ); | |
| 37 | 39 | } |
| 38 | - | |
| 40 | + | |
| 39 | 41 | // filter |
| 40 | - $taxonomies = apply_filters('acf/get_taxonomies', $taxonomies, $args); | |
| 41 | - | |
| 42 | + $taxonomies = apply_filters( 'acf/get_taxonomies', $taxonomies, $args ); | |
| 43 | + | |
| 42 | 44 | // return |
| 43 | 45 | return $taxonomies; |
| 44 | 46 | } |
| 45 | 47 | |
| 46 | 48 | /** |
| 47 | -* acf_get_taxonomies_for_post_type | |
| 48 | -* | |
| 49 | -* Returns an array of taxonomies for a given post type(s) | |
| 50 | -* | |
| 51 | -* @date 7/9/18 | |
| 52 | -* @since 5.7.5 | |
| 53 | -* | |
| 54 | -* @param string|array $post_types The post types to compare against. | |
| 55 | -* @return array | |
| 56 | -*/ | |
| 49 | + * acf_get_taxonomies_for_post_type | |
| 50 | + * | |
| 51 | + * Returns an array of taxonomies for a given post type(s) | |
| 52 | + * | |
| 53 | + * @date 7/9/18 | |
| 54 | + * @since 5.7.5 | |
| 55 | + * | |
| 56 | + * @param string|array $post_types The post types to compare against. | |
| 57 | + * @return array | |
| 58 | + */ | |
| 57 | 59 | function acf_get_taxonomies_for_post_type( $post_types = 'post' ) { |
| 58 | - | |
| 60 | + | |
| 59 | 61 | // vars |
| 60 | 62 | $taxonomies = array(); |
| 61 | - | |
| 63 | + | |
| 62 | 64 | // loop |
| 63 | - foreach( (array) $post_types as $post_type ) { | |
| 65 | + foreach ( (array) $post_types as $post_type ) { | |
| 64 | 66 | $object_taxonomies = get_object_taxonomies( $post_type ); |
| 65 | - foreach( (array) $object_taxonomies as $taxonomy ) { | |
| 66 | - $taxonomies[] = $taxonomy; | |
| 67 | + foreach ( (array) $object_taxonomies as $taxonomy ) { | |
| 68 | + $taxonomies[] = $taxonomy; | |
| 67 | 69 | } |
| 68 | 70 | } |
| 69 | - | |
| 71 | + | |
| 70 | 72 | // remove duplicates |
| 71 | - $taxonomies = array_unique($taxonomies); | |
| 72 | - | |
| 73 | + $taxonomies = array_unique( $taxonomies ); | |
| 74 | + | |
| 73 | 75 | // return |
| 74 | 76 | return $taxonomies; |
| 75 | 77 | } |
| 76 | 78 | |
| @@ -78,225 +80,235 @@ | ||
| 78 | 80 | * acf_get_taxonomy_labels |
| 79 | 81 | * |
| 80 | 82 | * Returns an array of taxonomies in the format "name => label" for use in a select field. |
| 81 | 83 | * |
| 82 | -* @date 3/8/18 | |
| 83 | -* @since 5.7.2 | |
| 84 | +* @date 3/8/18 | |
| 85 | +* @since 5.7.2 | |
| 84 | 86 | * |
| 85 | -* @param array $taxonomies Optional. An array of specific taxonomies to return. | |
| 86 | -* @return array | |
| 87 | +* @param array $taxonomies Optional. An array of specific taxonomies to return. | |
| 88 | +* @return array | |
| 87 | 89 | */ |
| 88 | 90 | |
| 89 | 91 | function acf_get_taxonomy_labels( $taxonomies = array() ) { |
| 90 | - | |
| 92 | + | |
| 91 | 93 | // default |
| 92 | - if( empty($taxonomies) ) { | |
| 94 | + if ( empty( $taxonomies ) ) { | |
| 93 | 95 | $taxonomies = acf_get_taxonomies(); |
| 94 | 96 | } |
| 95 | - | |
| 97 | + | |
| 96 | 98 | // vars |
| 97 | - $ref = array(); | |
| 99 | + $ref = array(); | |
| 98 | 100 | $data = array(); |
| 99 | - | |
| 101 | + | |
| 100 | 102 | // loop |
| 101 | - foreach( $taxonomies as $taxonomy ) { | |
| 102 | - | |
| 103 | + foreach ( $taxonomies as $taxonomy ) { | |
| 104 | + | |
| 103 | 105 | // vars |
| 104 | 106 | $object = get_taxonomy( $taxonomy ); |
| 105 | - $label = $object->labels->singular_name; | |
| 106 | - | |
| 107 | + $label = $object->labels->singular_name; | |
| 108 | + | |
| 107 | 109 | // append |
| 108 | 110 | $data[ $taxonomy ] = $label; |
| 109 | - | |
| 111 | + | |
| 110 | 112 | // increase counter |
| 111 | - if( !isset($ref[ $label ]) ) { | |
| 113 | + if ( ! isset( $ref[ $label ] ) ) { | |
| 112 | 114 | $ref[ $label ] = 0; |
| 113 | 115 | } |
| 114 | 116 | $ref[ $label ]++; |
| 115 | 117 | } |
| 116 | - | |
| 118 | + | |
| 117 | 119 | // show taxonomy name next to label for shared labels |
| 118 | - foreach( $data as $taxonomy => $label ) { | |
| 119 | - if( $ref[$label] > 1 ) { | |
| 120 | + foreach ( $data as $taxonomy => $label ) { | |
| 121 | + if ( $ref[ $label ] > 1 ) { | |
| 120 | 122 | $data[ $taxonomy ] .= ' (' . $taxonomy . ')'; |
| 121 | 123 | } |
| 122 | 124 | } |
| 123 | - | |
| 125 | + | |
| 124 | 126 | // return |
| 125 | 127 | return $data; |
| 126 | 128 | } |
| 127 | 129 | |
| 128 | 130 | /** |
| 129 | -* acf_get_term_title | |
| 130 | -* | |
| 131 | -* Returns the title for this term object. | |
| 132 | -* | |
| 133 | -* @date 10/9/18 | |
| 134 | -* @since 5.0.0 | |
| 135 | -* | |
| 136 | -* @param object $term The WP_Term object. | |
| 137 | -* @return string | |
| 138 | -*/ | |
| 131 | + * acf_get_term_title | |
| 132 | + * | |
| 133 | + * Returns the title for this term object. | |
| 134 | + * | |
| 135 | + * @date 10/9/18 | |
| 136 | + * @since 5.0.0 | |
| 137 | + * | |
| 138 | + * @param object $term The WP_Term object. | |
| 139 | + * @return string | |
| 140 | + */ | |
| 139 | 141 | |
| 140 | 142 | function acf_get_term_title( $term ) { |
| 141 | 143 | $title = $term->name; |
| 142 | - | |
| 144 | + | |
| 143 | 145 | // Allow for empty name. |
| 144 | - if( $title === '' ) { | |
| 145 | - $title = __('(no title)', 'acf'); | |
| 146 | + if ( $title === '' ) { | |
| 147 | + $title = __( '(no title)', 'acf' ); | |
| 146 | 148 | } |
| 147 | - | |
| 149 | + | |
| 148 | 150 | // Prepend ancestors indentation. |
| 149 | - if( is_taxonomy_hierarchical($term->taxonomy) ) { | |
| 151 | + if ( is_taxonomy_hierarchical( $term->taxonomy ) ) { | |
| 150 | 152 | $ancestors = get_ancestors( $term->term_id, $term->taxonomy ); |
| 151 | - $title = str_repeat('- ', count($ancestors)) . $title; | |
| 153 | + $title = str_repeat( '- ', count( $ancestors ) ) . $title; | |
| 152 | 154 | } |
| 153 | - | |
| 155 | + | |
| 154 | 156 | return $title; |
| 155 | 157 | } |
| 156 | 158 | |
| 157 | 159 | /** |
| 158 | -* acf_get_grouped_terms | |
| 159 | -* | |
| 160 | -* Returns an array of terms for the given query $args and groups by taxonomy name. | |
| 161 | -* | |
| 162 | -* @date 2/8/18 | |
| 163 | -* @since 5.7.2 | |
| 164 | -* | |
| 165 | -* @param array $args An array of args used in the get_terms() function. | |
| 166 | -* @return array | |
| 167 | -*/ | |
| 160 | + * acf_get_grouped_terms | |
| 161 | + * | |
| 162 | + * Returns an array of terms for the given query $args and groups by taxonomy name. | |
| 163 | + * | |
| 164 | + * @date 2/8/18 | |
| 165 | + * @since 5.7.2 | |
| 166 | + * | |
| 167 | + * @param array $args An array of args used in the get_terms() function. | |
| 168 | + * @return array | |
| 169 | + */ | |
| 168 | 170 | |
| 169 | 171 | function acf_get_grouped_terms( $args ) { |
| 170 | - | |
| 172 | + | |
| 171 | 173 | // vars |
| 172 | 174 | $data = array(); |
| 173 | - | |
| 175 | + | |
| 174 | 176 | // defaults |
| 175 | - $args = wp_parse_args($args, array( | |
| 176 | - 'taxonomy' => null, | |
| 177 | - 'hide_empty' => false, | |
| 178 | - 'update_term_meta_cache' => false, | |
| 179 | - )); | |
| 180 | - | |
| 177 | + $args = wp_parse_args( | |
| 178 | + $args, | |
| 179 | + array( | |
| 180 | + 'taxonomy' => null, | |
| 181 | + 'hide_empty' => false, | |
| 182 | + 'update_term_meta_cache' => false, | |
| 183 | + ) | |
| 184 | + ); | |
| 185 | + | |
| 181 | 186 | // vars |
| 182 | - $taxonomies = acf_get_taxonomy_labels( acf_get_array($args['taxonomy']) ); | |
| 183 | - $is_single = (count($taxonomies) == 1); | |
| 184 | - | |
| 187 | + $taxonomies = acf_get_taxonomy_labels( acf_get_array( $args['taxonomy'] ) ); | |
| 188 | + $is_single = ( count( $taxonomies ) == 1 ); | |
| 189 | + | |
| 185 | 190 | // specify exact taxonomies required for _acf_terms_clauses() to work. |
| 186 | - $args['taxonomy'] = array_keys($taxonomies); | |
| 187 | - | |
| 191 | + $args['taxonomy'] = array_keys( $taxonomies ); | |
| 192 | + | |
| 188 | 193 | // add filter to group results by taxonomy |
| 189 | - if( !$is_single ) { | |
| 190 | - add_filter('terms_clauses', '_acf_terms_clauses', 10, 3); | |
| 194 | + if ( ! $is_single ) { | |
| 195 | + add_filter( 'terms_clauses', '_acf_terms_clauses', 10, 3 ); | |
| 191 | 196 | } |
| 192 | - | |
| 197 | + | |
| 193 | 198 | // get terms |
| 194 | 199 | $terms = get_terms( $args ); |
| 195 | - | |
| 200 | + | |
| 196 | 201 | // remove this filter (only once) |
| 197 | - if( !$is_single ) { | |
| 198 | - remove_filter('terms_clauses', '_acf_terms_clauses', 10, 3); | |
| 202 | + if ( ! $is_single ) { | |
| 203 | + remove_filter( 'terms_clauses', '_acf_terms_clauses', 10, 3 ); | |
| 199 | 204 | } |
| 200 | - | |
| 205 | + | |
| 201 | 206 | // loop |
| 202 | - foreach( $taxonomies as $taxonomy => $label ) { | |
| 203 | - | |
| 207 | + foreach ( $taxonomies as $taxonomy => $label ) { | |
| 208 | + | |
| 204 | 209 | // vars |
| 205 | 210 | $this_terms = array(); |
| 206 | - | |
| 211 | + | |
| 207 | 212 | // populate $this_terms |
| 208 | - foreach( $terms as $term ) { | |
| 209 | - if( $term->taxonomy == $taxonomy ) { | |
| 213 | + foreach ( $terms as $term ) { | |
| 214 | + if ( $term->taxonomy == $taxonomy ) { | |
| 210 | 215 | $this_terms[] = $term; |
| 211 | 216 | } |
| 212 | 217 | } |
| 213 | - | |
| 218 | + | |
| 214 | 219 | // bail early if no $items |
| 215 | - if( empty($this_terms) ) continue; | |
| 216 | - | |
| 220 | + if ( empty( $this_terms ) ) { | |
| 221 | + continue; | |
| 222 | + } | |
| 223 | + | |
| 217 | 224 | // sort into hierachial order |
| 218 | 225 | // this will fail if a search has taken place because parents wont exist |
| 219 | - if( is_taxonomy_hierarchical($taxonomy) && empty($args['s'])) { | |
| 220 | - | |
| 226 | + if ( is_taxonomy_hierarchical( $taxonomy ) && empty( $args['s'] ) ) { | |
| 227 | + | |
| 221 | 228 | // get all terms from this taxonomy |
| 222 | - $all_terms = get_terms(array_merge($args, array( | |
| 223 | - 'number' => 0, | |
| 224 | - 'offset' => 0, | |
| 225 | - 'taxonomy' => $taxonomy | |
| 226 | - ))); | |
| 227 | - | |
| 229 | + $all_terms = get_terms( | |
| 230 | + array_merge( | |
| 231 | + $args, | |
| 232 | + array( | |
| 233 | + 'number' => 0, | |
| 234 | + 'offset' => 0, | |
| 235 | + 'taxonomy' => $taxonomy, | |
| 236 | + ) | |
| 237 | + ) | |
| 238 | + ); | |
| 239 | + | |
| 228 | 240 | // vars |
| 229 | - $length = count($this_terms); | |
| 241 | + $length = count( $this_terms ); | |
| 230 | 242 | $offset = 0; |
| 231 | - | |
| 243 | + | |
| 232 | 244 | // find starting point (offset) |
| 233 | - foreach( $all_terms as $i => $term ) { | |
| 234 | - if( $term->term_id == $this_terms[0]->term_id ) { | |
| 245 | + foreach ( $all_terms as $i => $term ) { | |
| 246 | + if ( $term->term_id == $this_terms[0]->term_id ) { | |
| 235 | 247 | $offset = $i; |
| 236 | 248 | break; |
| 237 | 249 | } |
| 238 | 250 | } |
| 239 | - | |
| 251 | + | |
| 240 | 252 | // order terms |
| 241 | - $parent = acf_maybe_get( $args, 'parent', 0 ); | |
| 242 | - $parent = acf_maybe_get( $args, 'child_of', $parent ); | |
| 253 | + $parent = acf_maybe_get( $args, 'parent', 0 ); | |
| 254 | + $parent = acf_maybe_get( $args, 'child_of', $parent ); | |
| 243 | 255 | $ordered_terms = _get_term_children( $parent, $all_terms, $taxonomy ); |
| 244 | - | |
| 256 | + | |
| 245 | 257 | // compare aray lengths |
| 246 | 258 | // if $ordered_posts is smaller than $all_posts, WP has lost posts during the get_page_children() function |
| 247 | 259 | // this is possible when get_post( $args ) filter out parents (via taxonomy, meta and other search parameters) |
| 248 | - if( count($ordered_terms) == count($all_terms) ) { | |
| 249 | - $this_terms = array_slice($ordered_terms, $offset, $length); | |
| 260 | + if ( count( $ordered_terms ) == count( $all_terms ) ) { | |
| 261 | + $this_terms = array_slice( $ordered_terms, $offset, $length ); | |
| 250 | 262 | } |
| 251 | 263 | } |
| 252 | - | |
| 264 | + | |
| 253 | 265 | // populate group |
| 254 | 266 | $data[ $label ] = array(); |
| 255 | - foreach( $this_terms as $term ) { | |
| 267 | + foreach ( $this_terms as $term ) { | |
| 256 | 268 | $data[ $label ][ $term->term_id ] = $term; |
| 257 | - } | |
| 269 | + } | |
| 258 | 270 | } |
| 259 | - | |
| 271 | + | |
| 260 | 272 | // return |
| 261 | 273 | return $data; |
| 262 | 274 | } |
| 263 | 275 | |
| 264 | 276 | /** |
| 265 | -* _acf_terms_clauses | |
| 266 | -* | |
| 267 | -* Used in the 'terms_clauses' filter to order terms by taxonomy name. | |
| 268 | -* | |
| 269 | -* @date 2/8/18 | |
| 270 | -* @since 5.7.2 | |
| 271 | -* | |
| 272 | -* @param array $pieces Terms query SQL clauses. | |
| 273 | -* @param array $taxonomies An array of taxonomies. | |
| 274 | -* @param array $args An array of terms query arguments. | |
| 275 | -* @return array $pieces | |
| 276 | -*/ | |
| 277 | + * _acf_terms_clauses | |
| 278 | + * | |
| 279 | + * Used in the 'terms_clauses' filter to order terms by taxonomy name. | |
| 280 | + * | |
| 281 | + * @date 2/8/18 | |
| 282 | + * @since 5.7.2 | |
| 283 | + * | |
| 284 | + * @param array $pieces Terms query SQL clauses. | |
| 285 | + * @param array $taxonomies An array of taxonomies. | |
| 286 | + * @param array $args An array of terms query arguments. | |
| 287 | + * @return array $pieces | |
| 288 | + */ | |
| 277 | 289 | |
| 278 | 290 | function _acf_terms_clauses( $pieces, $taxonomies, $args ) { |
| 279 | - | |
| 291 | + | |
| 280 | 292 | // prepend taxonomy to 'orderby' SQL |
| 281 | - if( is_array($taxonomies) ) { | |
| 282 | - $sql = "FIELD(tt.taxonomy,'" . implode("', '", array_map('esc_sql', $taxonomies)) . "')"; | |
| 283 | - $pieces['orderby'] = str_replace("ORDER BY", "ORDER BY $sql,", $pieces['orderby']); | |
| 293 | + if ( is_array( $taxonomies ) ) { | |
| 294 | + $sql = "FIELD(tt.taxonomy,'" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "')"; | |
| 295 | + $pieces['orderby'] = str_replace( 'ORDER BY', "ORDER BY $sql,", $pieces['orderby'] ); | |
| 284 | 296 | } |
| 285 | - | |
| 286 | - // return | |
| 297 | + | |
| 298 | + // return | |
| 287 | 299 | return $pieces; |
| 288 | 300 | } |
| 289 | 301 | |
| 290 | 302 | /** |
| 291 | -* acf_get_pretty_taxonomies | |
| 292 | -* | |
| 293 | -* Deprecated in favor of acf_get_taxonomy_labels() function. | |
| 294 | -* | |
| 295 | -* @date 7/10/13 | |
| 296 | -* @since 5.0.0 | |
| 297 | -* @deprecated 5.7.2 | |
| 298 | -*/ | |
| 303 | + * acf_get_pretty_taxonomies | |
| 304 | + * | |
| 305 | + * Deprecated in favor of acf_get_taxonomy_labels() function. | |
| 306 | + * | |
| 307 | + * @date 7/10/13 | |
| 308 | + * @since 5.0.0 | |
| 309 | + * @deprecated 5.7.2 | |
| 310 | + */ | |
| 299 | 311 | |
| 300 | 312 | function acf_get_pretty_taxonomies( $taxonomies = array() ) { |
| 301 | 313 | return acf_get_taxonomy_labels( $taxonomies ); |
| 302 | 314 | } |
| @@ -301,62 +313,64 @@ | ||
| 301 | 313 | return acf_get_taxonomy_labels( $taxonomies ); |
| 302 | 314 | } |
| 303 | 315 | |
| 304 | 316 | /** |
| 305 | -* acf_get_term | |
| 306 | -* | |
| 307 | -* Similar to get_term() but with some extra functionality. | |
| 308 | -* | |
| 309 | -* @date 19/8/18 | |
| 310 | -* @since 5.7.3 | |
| 311 | -* | |
| 312 | -* @param mixed $term_id The term ID or a string of "taxonomy:slug". | |
| 313 | -* @param string $taxonomy The taxonomyname. | |
| 314 | -* @return WP_Term | |
| 315 | -*/ | |
| 317 | + * acf_get_term | |
| 318 | + * | |
| 319 | + * Similar to get_term() but with some extra functionality. | |
| 320 | + * | |
| 321 | + * @date 19/8/18 | |
| 322 | + * @since 5.7.3 | |
| 323 | + * | |
| 324 | + * @param mixed $term_id The term ID or a string of "taxonomy:slug". | |
| 325 | + * @param string $taxonomy The taxonomyname. | |
| 326 | + * @return WP_Term | |
| 327 | + */ | |
| 316 | 328 | |
| 317 | 329 | function acf_get_term( $term_id, $taxonomy = '' ) { |
| 318 | - | |
| 330 | + | |
| 319 | 331 | // allow $term_id parameter to be a string of "taxonomy:slug" or "taxonomy:id" |
| 320 | - if( is_string($term_id) && strpos($term_id, ':') ) { | |
| 321 | - list( $taxonomy, $term_id ) = explode(':', $term_id); | |
| 322 | - $term = get_term_by( 'slug', $term_id, $taxonomy ); | |
| 323 | - if( $term ) return $term; | |
| 332 | + if ( is_string( $term_id ) && strpos( $term_id, ':' ) ) { | |
| 333 | + list( $taxonomy, $term_id ) = explode( ':', $term_id ); | |
| 334 | + $term = get_term_by( 'slug', $term_id, $taxonomy ); | |
| 335 | + if ( $term ) { | |
| 336 | + return $term; | |
| 337 | + } | |
| 324 | 338 | } |
| 325 | - | |
| 339 | + | |
| 326 | 340 | // return |
| 327 | 341 | return get_term( $term_id, $taxonomy ); |
| 328 | 342 | } |
| 329 | 343 | |
| 330 | 344 | /** |
| 331 | -* acf_encode_term | |
| 332 | -* | |
| 333 | -* Returns a "taxonomy:slug" string for a given WP_Term. | |
| 334 | -* | |
| 335 | -* @date 27/8/18 | |
| 336 | -* @since 5.7.4 | |
| 337 | -* | |
| 338 | -* @param WP_Term $term The term object. | |
| 339 | -* @return string | |
| 340 | -*/ | |
| 345 | + * acf_encode_term | |
| 346 | + * | |
| 347 | + * Returns a "taxonomy:slug" string for a given WP_Term. | |
| 348 | + * | |
| 349 | + * @date 27/8/18 | |
| 350 | + * @since 5.7.4 | |
| 351 | + * | |
| 352 | + * @param WP_Term $term The term object. | |
| 353 | + * @return string | |
| 354 | + */ | |
| 341 | 355 | function acf_encode_term( $term ) { |
| 342 | 356 | return "{$term->taxonomy}:{$term->slug}"; |
| 343 | 357 | } |
| 344 | 358 | |
| 345 | 359 | /** |
| 346 | -* acf_decode_term | |
| 347 | -* | |
| 348 | -* Decodes a "taxonomy:slug" string into an array of taxonomy and slug. | |
| 349 | -* | |
| 350 | -* @date 27/8/18 | |
| 351 | -* @since 5.7.4 | |
| 352 | -* | |
| 353 | -* @param WP_Term $term The term object. | |
| 354 | -* @return string | |
| 355 | -*/ | |
| 360 | + * acf_decode_term | |
| 361 | + * | |
| 362 | + * Decodes a "taxonomy:slug" string into an array of taxonomy and slug. | |
| 363 | + * | |
| 364 | + * @date 27/8/18 | |
| 365 | + * @since 5.7.4 | |
| 366 | + * | |
| 367 | + * @param WP_Term $term The term object. | |
| 368 | + * @return string | |
| 369 | + */ | |
| 356 | 370 | function acf_decode_term( $string ) { |
| 357 | - if( is_string($string) && strpos($string, ':') ) { | |
| 358 | - list( $taxonomy, $slug ) = explode(':', $string); | |
| 371 | + if ( is_string( $string ) && strpos( $string, ':' ) ) { | |
| 372 | + list( $taxonomy, $slug ) = explode( ':', $string ); | |
| 359 | 373 | return compact( 'taxonomy', 'slug' ); |
| 360 | 374 | } |
| 361 | 375 | return false; |
| 362 | 376 | } |
| @@ -361,133 +375,133 @@ | ||
| 361 | 375 | return false; |
| 362 | 376 | } |
| 363 | 377 | |
| 364 | 378 | /** |
| 365 | -* acf_get_encoded_terms | |
| 366 | -* | |
| 367 | -* Returns an array of WP_Term objects from an array of encoded strings | |
| 368 | -* | |
| 369 | -* @date 9/9/18 | |
| 370 | -* @since 5.7.5 | |
| 371 | -* | |
| 372 | -* @param array $values The array of encoded strings. | |
| 373 | -* @return array | |
| 374 | -*/ | |
| 379 | + * acf_get_encoded_terms | |
| 380 | + * | |
| 381 | + * Returns an array of WP_Term objects from an array of encoded strings | |
| 382 | + * | |
| 383 | + * @date 9/9/18 | |
| 384 | + * @since 5.7.5 | |
| 385 | + * | |
| 386 | + * @param array $values The array of encoded strings. | |
| 387 | + * @return array | |
| 388 | + */ | |
| 375 | 389 | function acf_get_encoded_terms( $values ) { |
| 376 | - | |
| 390 | + | |
| 377 | 391 | // vars |
| 378 | 392 | $terms = array(); |
| 379 | - | |
| 393 | + | |
| 380 | 394 | // loop over values |
| 381 | - foreach( (array) $values as $value ) { | |
| 382 | - | |
| 395 | + foreach ( (array) $values as $value ) { | |
| 396 | + | |
| 383 | 397 | // find term from string |
| 384 | 398 | $term = acf_get_term( $value ); |
| 385 | - | |
| 399 | + | |
| 386 | 400 | // append |
| 387 | - if( $term instanceof WP_Term ) { | |
| 401 | + if ( $term instanceof WP_Term ) { | |
| 388 | 402 | $terms[] = $term; |
| 389 | 403 | } |
| 390 | 404 | } |
| 391 | - | |
| 405 | + | |
| 392 | 406 | // return |
| 393 | 407 | return $terms; |
| 394 | 408 | } |
| 395 | 409 | |
| 396 | 410 | /** |
| 397 | -* acf_get_choices_from_terms | |
| 398 | -* | |
| 399 | -* Returns an array of choices from the terms provided. | |
| 400 | -* | |
| 401 | -* @date 8/9/18 | |
| 402 | -* @since 5.7.5 | |
| 403 | -* | |
| 404 | -* @param array $values and array of WP_Terms objects or encoded strings. | |
| 405 | -* @param string $format The value format (term_id, slug). | |
| 406 | -* @return array | |
| 407 | -*/ | |
| 411 | + * acf_get_choices_from_terms | |
| 412 | + * | |
| 413 | + * Returns an array of choices from the terms provided. | |
| 414 | + * | |
| 415 | + * @date 8/9/18 | |
| 416 | + * @since 5.7.5 | |
| 417 | + * | |
| 418 | + * @param array $values and array of WP_Terms objects or encoded strings. | |
| 419 | + * @param string $format The value format (term_id, slug). | |
| 420 | + * @return array | |
| 421 | + */ | |
| 408 | 422 | function acf_get_choices_from_terms( $terms, $format = 'term_id' ) { |
| 409 | - | |
| 423 | + | |
| 410 | 424 | // vars |
| 411 | 425 | $groups = array(); |
| 412 | - | |
| 426 | + | |
| 413 | 427 | // get taxonomy lables |
| 414 | 428 | $labels = acf_get_taxonomy_labels(); |
| 415 | - | |
| 429 | + | |
| 416 | 430 | // convert array of encoded strings to terms |
| 417 | - $term = reset($terms); | |
| 418 | - if( !$term instanceof WP_Term ) { | |
| 431 | + $term = reset( $terms ); | |
| 432 | + if ( ! $term instanceof WP_Term ) { | |
| 419 | 433 | $terms = acf_get_encoded_terms( $terms ); |
| 420 | 434 | } |
| 421 | - | |
| 435 | + | |
| 422 | 436 | // loop over terms |
| 423 | - foreach( $terms as $term ) { | |
| 424 | - $group = $labels[ $term->taxonomy ]; | |
| 425 | - $choice = acf_get_choice_from_term( $term, $format ); | |
| 437 | + foreach ( $terms as $term ) { | |
| 438 | + $group = $labels[ $term->taxonomy ]; | |
| 439 | + $choice = acf_get_choice_from_term( $term, $format ); | |
| 426 | 440 | $groups[ $group ][ $choice['id'] ] = $choice['text']; |
| 427 | 441 | } |
| 428 | - | |
| 442 | + | |
| 429 | 443 | // return |
| 430 | 444 | return $groups; |
| 431 | 445 | } |
| 432 | 446 | |
| 433 | 447 | /** |
| 434 | -* acf_get_choices_from_grouped_terms | |
| 435 | -* | |
| 436 | -* Returns an array of choices from the grouped terms provided. | |
| 437 | -* | |
| 438 | -* @date 8/9/18 | |
| 439 | -* @since 5.7.5 | |
| 440 | -* | |
| 441 | -* @param array $value A grouped array of WP_Terms objects. | |
| 442 | -* @param string $format The value format (term_id, slug). | |
| 443 | -* @return array | |
| 444 | -*/ | |
| 448 | + * acf_get_choices_from_grouped_terms | |
| 449 | + * | |
| 450 | + * Returns an array of choices from the grouped terms provided. | |
| 451 | + * | |
| 452 | + * @date 8/9/18 | |
| 453 | + * @since 5.7.5 | |
| 454 | + * | |
| 455 | + * @param array $value A grouped array of WP_Terms objects. | |
| 456 | + * @param string $format The value format (term_id, slug). | |
| 457 | + * @return array | |
| 458 | + */ | |
| 445 | 459 | function acf_get_choices_from_grouped_terms( $value, $format = 'term_id' ) { |
| 446 | - | |
| 460 | + | |
| 447 | 461 | // vars |
| 448 | 462 | $groups = array(); |
| 449 | - | |
| 463 | + | |
| 450 | 464 | // loop over values |
| 451 | - foreach( $value as $group => $terms ) { | |
| 465 | + foreach ( $value as $group => $terms ) { | |
| 452 | 466 | $groups[ $group ] = array(); |
| 453 | - foreach( $terms as $term_id => $term ) { | |
| 454 | - $choice = acf_get_choice_from_term( $term, $format ); | |
| 467 | + foreach ( $terms as $term_id => $term ) { | |
| 468 | + $choice = acf_get_choice_from_term( $term, $format ); | |
| 455 | 469 | $groups[ $group ][ $choice['id'] ] = $choice['text']; |
| 456 | 470 | } |
| 457 | 471 | } |
| 458 | - | |
| 472 | + | |
| 459 | 473 | // return |
| 460 | 474 | return $groups; |
| 461 | 475 | } |
| 462 | 476 | |
| 463 | 477 | /** |
| 464 | -* acf_get_choice_from_term | |
| 465 | -* | |
| 466 | -* Returns an array containing the id and text for this item. | |
| 467 | -* | |
| 468 | -* @date 10/9/18 | |
| 469 | -* @since 5.7.6 | |
| 470 | -* | |
| 471 | -* @param object $item The item object such as WP_Post or WP_Term. | |
| 472 | -* @param string $format The value format (term_id, slug) | |
| 473 | -* @return array | |
| 474 | -*/ | |
| 478 | + * acf_get_choice_from_term | |
| 479 | + * | |
| 480 | + * Returns an array containing the id and text for this item. | |
| 481 | + * | |
| 482 | + * @date 10/9/18 | |
| 483 | + * @since 5.7.6 | |
| 484 | + * | |
| 485 | + * @param object $item The item object such as WP_Post or WP_Term. | |
| 486 | + * @param string $format The value format (term_id, slug) | |
| 487 | + * @return array | |
| 488 | + */ | |
| 475 | 489 | function acf_get_choice_from_term( $term, $format = 'term_id' ) { |
| 476 | - | |
| 490 | + | |
| 477 | 491 | // vars |
| 478 | - $id = $term->term_id; | |
| 492 | + $id = $term->term_id; | |
| 479 | 493 | $text = acf_get_term_title( $term ); |
| 480 | - | |
| 494 | + | |
| 481 | 495 | // return format |
| 482 | - if( $format == 'slug' ) { | |
| 483 | - $id = acf_encode_term($term); | |
| 496 | + if ( $format == 'slug' ) { | |
| 497 | + $id = acf_encode_term( $term ); | |
| 484 | 498 | } |
| 485 | - | |
| 499 | + | |
| 486 | 500 | // return |
| 487 | 501 | return array( |
| 488 | - 'id' => $id, | |
| 489 | - 'text' => $text | |
| 502 | + 'id' => $id, | |
| 503 | + 'text' => $text, | |
| 490 | 504 | ); |
| 491 | 505 | } |
| 492 | 506 | |
| 493 | 507 | /** |
| @@ -493,16 +507,16 @@ | ||
| 493 | 507 | /** |
| 494 | 508 | * Returns a valid post_id string for a given term and taxonomy. |
| 495 | 509 | * No longer needed since WP introduced the termmeta table in WP 4.4. |
| 496 | 510 | * |
| 497 | - * @date 6/2/17 | |
| 498 | - * @since 5.5.6 | |
| 511 | + * @date 6/2/17 | |
| 512 | + * @since 5.5.6 | |
| 499 | 513 | * @deprecated 5.9.2 |
| 500 | 514 | * |
| 501 | - * @param $taxonomy (string) The taxonomy type. | |
| 502 | - * @param $term_id (int) The term ID. | |
| 503 | - * @return (string) | |
| 515 | + * @param $taxonomy (string) The taxonomy type. | |
| 516 | + * @param $term_id (int) The term ID. | |
| 517 | + * @return (string) | |
| 504 | 518 | */ |
| 505 | 519 | function acf_get_term_post_id( $taxonomy, $term_id ) { |
| 506 | 520 | _deprecated_function( __FUNCTION__, '5.9.2', 'string format term_%d' ); |
| 507 | 521 | return 'term_' . $term_id; |
| 508 | -} | |
| 522 | +} | |