admin-field-group.php
5 months ago
admin-field-groups.php
6 months ago
admin-post-type.php
6 months ago
admin-post-types.php
6 months ago
admin-taxonomies.php
6 months ago
admin-taxonomy.php
6 months ago
index.php
2 years ago
admin-taxonomy.php
379 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package ACF |
| 4 | * @author WP Engine |
| 5 | * |
| 6 | * © 2026 Advanced Custom Fields (ACF®). All rights reserved. |
| 7 | * "ACF" is a trademark of WP Engine. |
| 8 | * Licensed under the GNU General Public License v2 or later. |
| 9 | * https://www.gnu.org/licenses/gpl-2.0.html |
| 10 | */ |
| 11 | |
| 12 | if ( ! class_exists( 'ACF_Admin_Taxonomy' ) ) : |
| 13 | |
| 14 | /** |
| 15 | * ACF Admin Field Group Class |
| 16 | * |
| 17 | * All the logic for editing a taxonomy. |
| 18 | */ |
| 19 | class ACF_Admin_Taxonomy extends ACF_Admin_Internal_Post_Type { |
| 20 | |
| 21 | /** |
| 22 | * The slug for the internal post type. |
| 23 | * |
| 24 | * @since 6.1 |
| 25 | * @var string |
| 26 | */ |
| 27 | public $post_type = 'acf-taxonomy'; |
| 28 | |
| 29 | /** |
| 30 | * The admin body class used for the post type. |
| 31 | * |
| 32 | * @since 6.1 |
| 33 | * @var string |
| 34 | */ |
| 35 | public $admin_body_class = 'acf-admin-single-taxonomy'; |
| 36 | |
| 37 | /** |
| 38 | * This function will customize the message shown when editing a field group |
| 39 | * |
| 40 | * @since 5.0.0 |
| 41 | * |
| 42 | * @param array $messages Post type messages. |
| 43 | * @return array |
| 44 | */ |
| 45 | public function post_updated_messages( $messages ) { |
| 46 | $messages['acf-taxonomy'] = array( |
| 47 | 0 => '', // Unused. Messages start at index 1. |
| 48 | 1 => $this->taxonomy_saved_message(), |
| 49 | 2 => __( 'Taxonomy updated.', 'acf' ), |
| 50 | 3 => __( 'Taxonomy deleted.', 'acf' ), |
| 51 | 4 => $this->taxonomy_saved_message(), |
| 52 | 5 => false, // taxonomy does not support revisions. |
| 53 | 6 => $this->taxonomy_saved_message( true ), |
| 54 | 7 => __( 'Taxonomy saved.', 'acf' ), |
| 55 | 8 => __( 'Taxonomy submitted.', 'acf' ), |
| 56 | 9 => __( 'Taxonomy scheduled for.', 'acf' ), |
| 57 | 10 => __( 'Taxonomy draft updated.', 'acf' ), |
| 58 | ); |
| 59 | |
| 60 | return $messages; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Renders the post type created message. |
| 65 | * |
| 66 | * @since 6.1 |
| 67 | * |
| 68 | * @param boolean $created True if the post was just created. |
| 69 | * @return string |
| 70 | */ |
| 71 | public function taxonomy_saved_message( $created = false ) { |
| 72 | global $post_id; |
| 73 | |
| 74 | $title = get_the_title( $post_id ); |
| 75 | |
| 76 | /* translators: %s taxonomy name */ |
| 77 | $item_saved_text = sprintf( __( '%s taxonomy updated', 'acf' ), $title ); |
| 78 | /* translators: %s taxonomy name */ |
| 79 | $add_fields_text = sprintf( __( 'Add fields to %s', 'acf' ), $title ); |
| 80 | |
| 81 | if ( $created ) { |
| 82 | /* translators: %s taxonomy name */ |
| 83 | $item_saved_text = sprintf( __( '%s taxonomy created', 'acf' ), $title ); |
| 84 | } |
| 85 | |
| 86 | $add_fields_link = wp_nonce_url( |
| 87 | admin_url( 'post-new.php?post_type=acf-field-group&use_taxonomy=' . $post_id ), |
| 88 | 'add-fields-' . $post_id |
| 89 | ); |
| 90 | |
| 91 | $create_taxonomy_link = admin_url( 'post-new.php?post_type=acf-taxonomy' ); |
| 92 | $duplicate_taxonomy_link = wp_nonce_url( |
| 93 | admin_url( 'post-new.php?post_type=acf-taxonomy&use_taxonomy=' . $post_id ), |
| 94 | 'acfduplicate-' . $post_id |
| 95 | ); |
| 96 | |
| 97 | $create_post_type_link = wp_nonce_url( |
| 98 | admin_url( 'post-new.php?post_type=acf-post-type&use_taxonomy=' . $post_id ), |
| 99 | 'create-post-type-' . $post_id |
| 100 | ); |
| 101 | |
| 102 | ob_start(); ?> |
| 103 | <p class="acf-item-saved-text"><?php echo esc_html( $item_saved_text ); ?></p> |
| 104 | <div class="acf-item-saved-links"> |
| 105 | <a href="<?php echo esc_url( $add_fields_link ); ?>"><?php esc_html_e( 'Add fields', 'acf' ); ?></a> |
| 106 | <a class="acf-link-field-groups" href="#"><?php esc_html_e( 'Link field groups', 'acf' ); ?></a> |
| 107 | <a href="<?php echo esc_url( $create_taxonomy_link ); ?>"><?php esc_html_e( 'Create taxonomy', 'acf' ); ?></a> |
| 108 | <a href="<?php echo esc_url( $duplicate_taxonomy_link ); ?>"><?php esc_html_e( 'Duplicate taxonomy', 'acf' ); ?></a> |
| 109 | <a href="<?php echo esc_url( $create_post_type_link ); ?>"><?php esc_html_e( 'Create post type', 'acf' ); ?></a> |
| 110 | </div> |
| 111 | <?php |
| 112 | return ob_get_clean(); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Enqueues any scripts necessary for internal post type. |
| 117 | * |
| 118 | * @since 5.0.0 |
| 119 | */ |
| 120 | public function admin_enqueue_scripts() { |
| 121 | |
| 122 | wp_enqueue_style( 'acf-field-group' ); |
| 123 | |
| 124 | acf_localize_text( |
| 125 | array( |
| 126 | 'Tag' => __( 'Tag', 'acf' ), |
| 127 | 'Tags' => __( 'Tags', 'acf' ), |
| 128 | 'Category' => __( 'Category', 'acf' ), |
| 129 | 'Categories' => __( 'Categories', 'acf' ), |
| 130 | 'Default' => __( 'Default', 'acf' ), |
| 131 | ) |
| 132 | ); |
| 133 | |
| 134 | parent::admin_enqueue_scripts(); |
| 135 | |
| 136 | do_action( 'acf/taxonomy/admin_enqueue_scripts' ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Sets up all functionality for the taxonomy edit page to work. |
| 141 | * |
| 142 | * @since 3.1.8 |
| 143 | */ |
| 144 | public function admin_head() { |
| 145 | |
| 146 | // global. |
| 147 | global $post, $acf_taxonomy; |
| 148 | |
| 149 | // set global var. |
| 150 | $acf_taxonomy = acf_get_internal_post_type( $post->ID, $this->post_type ); |
| 151 | |
| 152 | if ( ! empty( $acf_taxonomy['not_registered'] ) ) { |
| 153 | acf_add_admin_notice( |
| 154 | __( 'This taxonomy could not be registered because its key is in use by another taxonomy registered by another plugin or theme.', 'acf' ), |
| 155 | 'error' |
| 156 | ); |
| 157 | } |
| 158 | |
| 159 | // metaboxes. |
| 160 | add_meta_box( 'acf-basic-settings', __( 'Basic Settings', 'acf' ), array( $this, 'mb_basic_settings' ), 'acf-taxonomy', 'normal', 'high' ); |
| 161 | add_meta_box( 'acf-advanced-settings', __( 'Advanced Settings', 'acf' ), array( $this, 'mb_advanced_settings' ), 'acf-taxonomy', 'normal', 'high' ); |
| 162 | |
| 163 | // actions. |
| 164 | add_action( 'post_submitbox_misc_actions', array( $this, 'post_submitbox_misc_actions' ), 10, 0 ); |
| 165 | add_action( 'edit_form_after_title', array( $this, 'edit_form_after_title' ), 10, 0 ); |
| 166 | |
| 167 | // filters. |
| 168 | add_filter( 'screen_settings', array( $this, 'screen_settings' ), 10, 1 ); |
| 169 | add_filter( 'get_user_option_screen_layout_acf-taxonomy', array( $this, 'screen_layout' ), 10, 1 ); |
| 170 | add_filter( 'get_user_option_metaboxhidden_acf-taxonomy', array( $this, 'force_basic_settings' ), 10, 1 ); |
| 171 | add_filter( 'get_user_option_closedpostboxes_acf-taxonomy', array( $this, 'force_basic_settings' ), 10, 1 ); |
| 172 | add_filter( 'get_user_option_closedpostboxes_acf-taxonomy', array( $this, 'force_advanced_settings' ), 10, 1 ); |
| 173 | |
| 174 | // 3rd party hook. |
| 175 | do_action( 'acf/taxonomy/admin_head' ); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * This action will allow ACF to render metaboxes after the title. |
| 180 | */ |
| 181 | public function edit_form_after_title() { |
| 182 | |
| 183 | // globals. |
| 184 | global $post; |
| 185 | |
| 186 | // render post data. |
| 187 | acf_form_data( |
| 188 | array( |
| 189 | 'screen' => 'taxonomy', |
| 190 | 'post_id' => $post->ID, |
| 191 | 'delete_fields' => 0, |
| 192 | 'validation' => 1, |
| 193 | ) |
| 194 | ); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * This function will add extra HTML to the acf form data element |
| 199 | * |
| 200 | * @since 5.3.8 |
| 201 | * |
| 202 | * @param array $args Arguments array to pass through to action. |
| 203 | * @return void |
| 204 | */ |
| 205 | public function form_data( $args ) { |
| 206 | do_action( 'acf/taxonomy/form_data', $args ); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * This function will append extra l10n strings to the acf JS object |
| 211 | * |
| 212 | * @since 5.3.8 |
| 213 | * |
| 214 | * @param array $l10n The array of translated strings. |
| 215 | * @return array $l10n |
| 216 | */ |
| 217 | public function admin_l10n( $l10n ) { |
| 218 | return apply_filters( 'acf/taxonomy/admin_l10n', $l10n ); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Admin footer third party hook support |
| 223 | * |
| 224 | * @since 5.3.2 |
| 225 | */ |
| 226 | public function admin_footer() { |
| 227 | do_action( 'acf/taxonomy/admin_footer' ); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Screen settings html output |
| 232 | * |
| 233 | * @since 3.6.0 |
| 234 | * |
| 235 | * @param string $html Current screen settings HTML. |
| 236 | * @return string $html |
| 237 | */ |
| 238 | public function screen_settings( $html ) { |
| 239 | return $html; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Sets the "Edit Field Group" screen to use a one-column layout. |
| 244 | * |
| 245 | * @param integer $columns Number of columns for layout. |
| 246 | * @return integer |
| 247 | */ |
| 248 | public function screen_layout( $columns = 0 ) { |
| 249 | return 1; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Force basic settings to always be visible |
| 254 | * |
| 255 | * @param array $hidden_metaboxes The metaboxes hidden on this page. |
| 256 | * @return array |
| 257 | */ |
| 258 | public function force_basic_settings( $hidden_metaboxes ) { |
| 259 | if ( ! is_array( $hidden_metaboxes ) ) { |
| 260 | return $hidden_metaboxes; |
| 261 | } |
| 262 | return array_diff( $hidden_metaboxes, array( 'acf-basic-settings' ) ); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Force advanced settings to be visible |
| 267 | * |
| 268 | * @param array $hidden_metaboxes The metaboxes hidden on this page. |
| 269 | * @return array |
| 270 | */ |
| 271 | public function force_advanced_settings( $hidden_metaboxes ) { |
| 272 | if ( ! is_array( $hidden_metaboxes ) ) { |
| 273 | return $hidden_metaboxes; |
| 274 | } |
| 275 | return array_diff( $hidden_metaboxes, array( 'acf-advanced-settings' ) ); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * This function will customize the publish metabox |
| 280 | * |
| 281 | * @since 5.2.9 |
| 282 | */ |
| 283 | public function post_submitbox_misc_actions() { |
| 284 | global $acf_taxonomy; |
| 285 | $status_label = $acf_taxonomy['active'] ? _x( 'Active', 'post status', 'acf' ) : _x( 'Inactive', 'post status', 'acf' ); |
| 286 | |
| 287 | ?> |
| 288 | <script type="text/javascript"> |
| 289 | (function($) { |
| 290 | $('#post-status-display').html( '<?php echo esc_html( $status_label ); ?>' ); |
| 291 | })(jQuery); |
| 292 | </script> |
| 293 | <?php |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Saves taxonomy data. |
| 298 | * |
| 299 | * @since 1.0.0 |
| 300 | * |
| 301 | * @param integer $post_id The post ID. |
| 302 | * @param WP_Post $post The post object. |
| 303 | * @return integer $post_id |
| 304 | */ |
| 305 | public function save_post( $post_id, $post ) { |
| 306 | if ( ! $this->verify_save_post( $post_id, $post ) ) { |
| 307 | return $post_id; |
| 308 | } |
| 309 | |
| 310 | // Disable filters to ensure ACF loads raw data from DB. |
| 311 | acf_disable_filters(); |
| 312 | |
| 313 | // phpcs:disable WordPress.Security.NonceVerification.Missing -- Validated in $this->verify_save_post() above. |
| 314 | // phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized when saved. |
| 315 | $_POST['acf_taxonomy']['ID'] = $post_id; |
| 316 | $_POST['acf_taxonomy']['title'] = isset( $_POST['acf_taxonomy']['labels']['name'] ) ? $_POST['acf_taxonomy']['labels']['name'] : ''; |
| 317 | |
| 318 | if ( ! acf_get_setting( 'enable_meta_box_cb_edit' ) ) { |
| 319 | $_POST['acf_taxonomy']['meta_box_cb'] = ''; |
| 320 | $_POST['acf_taxonomy']['meta_box_sanitize_cb'] = ''; |
| 321 | |
| 322 | if ( ! empty( $_POST['acf_taxonomy']['meta_box'] ) && 'custom' === $_POST['acf_taxonomy']['meta_box'] ) { |
| 323 | $_POST['acf_taxonomy']['meta_box'] = 'default'; |
| 324 | } |
| 325 | |
| 326 | $existing_post = acf_maybe_unserialize( $post->post_content ); |
| 327 | |
| 328 | if ( ! empty( $existing_post['meta_box'] ) ) { |
| 329 | $_POST['acf_taxonomy']['meta_box'] = $existing_post['meta_box']; |
| 330 | } |
| 331 | |
| 332 | if ( ! empty( $existing_post['meta_box_cb'] ) ) { |
| 333 | $_POST['acf_taxonomy']['meta_box_cb'] = $existing_post['meta_box_cb']; |
| 334 | } |
| 335 | |
| 336 | if ( ! empty( $existing_post['meta_box_sanitize_cb'] ) ) { |
| 337 | $_POST['acf_taxonomy']['meta_box_sanitize_cb'] = $existing_post['meta_box_sanitize_cb']; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | // Save the taxonomy. |
| 342 | acf_update_internal_post_type( $_POST['acf_taxonomy'], $this->post_type ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Validated in verify_save_post |
| 343 | // phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 344 | // phpcs:enable WordPress.Security.NonceVerification.Missing |
| 345 | |
| 346 | return $post_id; |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Renders HTML for the 'acf-taxonomy-fields' metabox. |
| 351 | * |
| 352 | * @since 5.0.0 |
| 353 | */ |
| 354 | public function mb_basic_settings() { |
| 355 | global $acf_taxonomy; |
| 356 | |
| 357 | if ( ! acf_is_internal_post_type_key( $acf_taxonomy['key'], 'acf-taxonomy' ) ) { |
| 358 | $acf_taxonomy['key'] = uniqid( 'taxonomy_' ); |
| 359 | } |
| 360 | |
| 361 | acf_get_view( $this->post_type . '/basic-settings' ); |
| 362 | } |
| 363 | |
| 364 | |
| 365 | /** |
| 366 | * Renders the HTML for the 'acf-taxonomy-options' metabox. |
| 367 | * |
| 368 | * @since 5.0.0 |
| 369 | */ |
| 370 | public function mb_advanced_settings() { |
| 371 | acf_get_view( $this->post_type . '/advanced-settings' ); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | new ACF_Admin_Taxonomy(); |
| 376 | endif; // Class exists check. |
| 377 | |
| 378 | ?> |
| 379 |