admin-field-group.php
2 months ago
admin-field-groups.php
10 months ago
admin-post-type.php
1 year ago
admin-post-types.php
10 months ago
admin-taxonomies.php
10 months ago
admin-taxonomy.php
1 year ago
class-acf-admin-ui-options-page.php
1 year ago
class-acf-admin-ui-options-pages.php
10 months ago
index.php
1 year ago
admin-taxonomies.php
359 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; // Exit if accessed directly. |
| 5 | } |
| 6 | |
| 7 | if ( ! class_exists( 'ACF_Admin_Taxonomies' ) ) : |
| 8 | |
| 9 | /** |
| 10 | * The ACF Post Types admin controller class |
| 11 | */ |
| 12 | class ACF_Admin_Taxonomies extends ACF_Admin_Internal_Post_Type_List { |
| 13 | |
| 14 | |
| 15 | /** |
| 16 | * The slug for the internal post type. |
| 17 | * |
| 18 | * @since ACF 6.1 |
| 19 | * @var string |
| 20 | */ |
| 21 | public $post_type = 'acf-taxonomy'; |
| 22 | |
| 23 | /** |
| 24 | * The admin body class used for the post type. |
| 25 | * |
| 26 | * @since ACF 6.1 |
| 27 | * @var string |
| 28 | */ |
| 29 | public $admin_body_class = 'acf-admin-taxonomies'; |
| 30 | |
| 31 | /** |
| 32 | * The name of the store used for the post type. |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | public $store = 'taxonomies'; |
| 37 | |
| 38 | /** |
| 39 | * Constructor. |
| 40 | * |
| 41 | * @since ACF 6.2 |
| 42 | */ |
| 43 | public function __construct() { |
| 44 | add_action( 'admin_menu', array( $this, 'admin_menu' ), 9 ); |
| 45 | parent::__construct(); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Current screen actions for the taxonomies list admin page. |
| 50 | * |
| 51 | * @since ACF 6.1 |
| 52 | */ |
| 53 | public function current_screen() { |
| 54 | // Bail early if not post types admin page. |
| 55 | if ( ! acf_is_screen( "edit-{$this->post_type}" ) ) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | parent::current_screen(); |
| 60 | |
| 61 | // Run a first-run routine to set some defaults which are stored in user preferences. |
| 62 | if ( ! acf_get_user_setting( 'taxonomies-first-run', false ) ) { |
| 63 | $option_key = 'manageedit-' . $this->post_type . 'columnshidden'; |
| 64 | $hidden_items = get_user_option( $option_key ); |
| 65 | |
| 66 | if ( ! is_array( $hidden_items ) ) { |
| 67 | $hidden_items = array(); |
| 68 | } |
| 69 | |
| 70 | if ( ! in_array( 'acf-key', $hidden_items ) ) { |
| 71 | $hidden_items[] = 'acf-key'; |
| 72 | } |
| 73 | update_user_option( get_current_user_id(), $option_key, $hidden_items, true ); |
| 74 | |
| 75 | acf_update_user_setting( 'taxonomies-first-run', true ); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Add any menu items required for taxonomies. |
| 81 | * |
| 82 | * @since ACF 6.1 |
| 83 | */ |
| 84 | public function admin_menu() { |
| 85 | $parent_slug = 'edit.php?post_type=acf-field-group'; |
| 86 | $cap = acf_get_setting( 'capability' ); |
| 87 | add_submenu_page( $parent_slug, __( 'Taxonomies', 'secure-custom-fields' ), __( 'Taxonomies', 'secure-custom-fields' ), $cap, 'edit.php?post_type=acf-taxonomy' ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Customizes the admin table columns. |
| 92 | * |
| 93 | * @date 1/4/20 |
| 94 | * @since ACF 5.9.0 |
| 95 | * |
| 96 | * @param array $_columns The columns array. |
| 97 | * @return array |
| 98 | */ |
| 99 | public function admin_table_columns( $_columns ) { |
| 100 | // Set the "no found" label to be our custom HTML for no results. |
| 101 | if ( empty( acf_request_arg( 's' ) ) ) { |
| 102 | global $wp_post_types; |
| 103 | $wp_post_types[ $this->post_type ]->labels->not_found = $this->get_not_found_html(); |
| 104 | } |
| 105 | |
| 106 | $columns = array( |
| 107 | 'cb' => $_columns['cb'], |
| 108 | 'title' => $_columns['title'], |
| 109 | 'acf-description' => __( 'Description', 'secure-custom-fields' ), |
| 110 | 'acf-key' => __( 'Key', 'secure-custom-fields' ), |
| 111 | 'acf-post-types' => __( 'Post Types', 'secure-custom-fields' ), |
| 112 | 'acf-field-groups' => __( 'Field Groups', 'secure-custom-fields' ), |
| 113 | 'acf-count' => __( 'Terms', 'secure-custom-fields' ), |
| 114 | ); |
| 115 | |
| 116 | if ( acf_get_local_json_files( $this->post_type ) ) { |
| 117 | $columns['acf-json'] = __( 'Local JSON', 'secure-custom-fields' ); |
| 118 | } |
| 119 | |
| 120 | return $columns; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Renders a specific admin table column. |
| 125 | * |
| 126 | * @date 17/4/20 |
| 127 | * @since ACF 5.9.0 |
| 128 | * |
| 129 | * @param string $column_name The name of the column to display. |
| 130 | * @param array $post The main ACF post array. |
| 131 | * @return void |
| 132 | */ |
| 133 | public function render_admin_table_column( $column_name, $post ) { |
| 134 | switch ( $column_name ) { |
| 135 | case 'acf-key': |
| 136 | echo '<i class="acf-icon acf-icon-key-solid"></i>'; |
| 137 | echo esc_html( $post['key'] ); |
| 138 | break; |
| 139 | |
| 140 | // Description. |
| 141 | case 'acf-description': |
| 142 | if ( ( is_string( $post['description'] ) || is_numeric( $post['description'] ) ) && ! empty( $post['description'] ) ) { |
| 143 | echo '<span class="acf-description">' . acf_esc_html( $post['description'] ) . '</span>'; |
| 144 | } else { |
| 145 | echo '<span class="acf-emdash" aria-hidden="true">—</span>'; |
| 146 | echo '<span class="screen-reader-text">' . esc_html__( 'No description', 'secure-custom-fields' ) . '</span>'; |
| 147 | } |
| 148 | break; |
| 149 | |
| 150 | case 'acf-field-groups': |
| 151 | $this->render_admin_table_column_field_groups( $post ); |
| 152 | break; |
| 153 | |
| 154 | case 'acf-post-types': |
| 155 | $this->render_admin_table_column_post_types( $post ); |
| 156 | break; |
| 157 | |
| 158 | case 'acf-count': |
| 159 | $this->render_admin_table_column_num_terms( $post ); |
| 160 | break; |
| 161 | |
| 162 | // Local JSON. |
| 163 | case 'acf-json': |
| 164 | $this->render_admin_table_column_local_status( $post ); |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Renders the field groups attached to the taxonomy in the list table. |
| 171 | * |
| 172 | * @since ACF 6.1 |
| 173 | * |
| 174 | * @param array $taxonomy The main taxonomy array. |
| 175 | * @return void |
| 176 | */ |
| 177 | public function render_admin_table_column_field_groups( $taxonomy ) { |
| 178 | $field_groups = acf_get_field_groups( array( 'taxonomy' => $taxonomy['taxonomy'] ) ); |
| 179 | |
| 180 | if ( empty( $field_groups ) ) { |
| 181 | echo '<span class="acf-emdash" aria-hidden="true">—</span>'; |
| 182 | echo '<span class="screen-reader-text">' . esc_html__( 'No field groups', 'secure-custom-fields' ) . '</span>'; |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | $labels = wp_list_pluck( $field_groups, 'title' ); |
| 187 | $limit = 3; |
| 188 | $shown_labels = array_slice( $labels, 0, $limit ); |
| 189 | $hidden_labels = array_slice( $labels, $limit ); |
| 190 | $text = implode( ', ', $shown_labels ); |
| 191 | |
| 192 | if ( ! empty( $hidden_labels ) ) { |
| 193 | $text .= ', <span class="acf-more-items acf-js-tooltip" title="' . implode( ', ', $hidden_labels ) . '">+' . count( $hidden_labels ) . '</span>'; |
| 194 | } |
| 195 | |
| 196 | echo acf_esc_html( $text ); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Renders the post types attached to the taxonomy in the list table. |
| 201 | * |
| 202 | * @since ACF 6.1 |
| 203 | * |
| 204 | * @param array $taxonomy The main taxonomy array. |
| 205 | * @return void |
| 206 | */ |
| 207 | public function render_admin_table_column_post_types( $taxonomy ) { |
| 208 | $post_types = get_post_types( array(), 'objects' ); |
| 209 | $labels = array(); |
| 210 | $object_types = array(); |
| 211 | |
| 212 | if ( ! empty( $taxonomy['object_type'] ) ) { |
| 213 | $object_types = (array) $taxonomy['object_type']; |
| 214 | } |
| 215 | |
| 216 | foreach ( $object_types as $post_type_slug ) { |
| 217 | if ( ! isset( $post_types[ $post_type_slug ] ) ) { |
| 218 | continue; |
| 219 | } |
| 220 | |
| 221 | $post_type = $post_types[ $post_type_slug ]; |
| 222 | |
| 223 | if ( empty( $post_type->label ) ) { |
| 224 | continue; |
| 225 | } |
| 226 | |
| 227 | $labels[] = $post_type->label; |
| 228 | } |
| 229 | |
| 230 | $acf_post_types = acf_get_internal_post_type_posts( 'acf-post-type' ); |
| 231 | |
| 232 | foreach ( $acf_post_types as $acf_post_type ) { |
| 233 | if ( is_array( $acf_post_type['taxonomies'] ) && in_array( $taxonomy['taxonomy'], $acf_post_type['taxonomies'], true ) ) { |
| 234 | $labels[] = $acf_post_type['title']; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | if ( empty( $labels ) ) { |
| 239 | echo '<span class="acf-emdash" aria-hidden="true">—</span>'; |
| 240 | echo '<span class="screen-reader-text">' . esc_html__( 'No post types', 'secure-custom-fields' ) . '</span>'; |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | $labels = array_unique( $labels ); |
| 245 | $limit = 3; |
| 246 | $shown_labels = array_slice( $labels, 0, $limit ); |
| 247 | $hidden_labels = array_slice( $labels, $limit ); |
| 248 | $text = implode( ', ', $shown_labels ); |
| 249 | |
| 250 | if ( ! empty( $hidden_labels ) ) { |
| 251 | $text .= ', <span class="acf-more-items acf-js-tooltip" title="' . implode( ', ', $hidden_labels ) . '">+' . count( $hidden_labels ) . '</span>'; |
| 252 | } |
| 253 | |
| 254 | echo acf_esc_html( $text ); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Renders the number of terms created for the taxonomy in the list table. |
| 259 | * |
| 260 | * @since ACF 6.1 |
| 261 | * |
| 262 | * @param array $taxonomy The main taxonomy array. |
| 263 | * @return void |
| 264 | */ |
| 265 | public function render_admin_table_column_num_terms( $taxonomy ) { |
| 266 | $no_terms = '<span class="acf-emdash" aria-hidden="true">—</span>'; |
| 267 | $no_terms .= '<span class="screen-reader-text">' . esc_html__( 'No terms', 'secure-custom-fields' ) . '</span>'; |
| 268 | |
| 269 | // WP doesn't count terms for taxonomies that don't exist and instead returns WP_Error. |
| 270 | if ( empty( $taxonomy['active'] ) || 'trash' === get_post_status( $taxonomy['ID'] ) ) { |
| 271 | echo acf_esc_html( $no_terms ); |
| 272 | return; |
| 273 | } |
| 274 | |
| 275 | $num_terms = wp_count_terms( |
| 276 | array( |
| 277 | 'taxonomy' => $taxonomy['taxonomy'], |
| 278 | 'hide_empty' => false, |
| 279 | 'parent' => 0, |
| 280 | ) |
| 281 | ); |
| 282 | |
| 283 | if ( ! $num_terms || ! is_numeric( $num_terms ) ) { |
| 284 | echo acf_esc_html( $no_terms ); |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | printf( |
| 289 | '<a href="%s">%s</a>', |
| 290 | esc_url( admin_url( 'edit-tags.php?taxonomy=' . $taxonomy['taxonomy'] ) ), |
| 291 | esc_html( number_format_i18n( $num_terms ) ) |
| 292 | ); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Gets the translated action notice text for list table actions (activate, deactivate, sync, etc.). |
| 297 | * |
| 298 | * @since ACF 6.1 |
| 299 | * |
| 300 | * @param string $action The action being performed. |
| 301 | * @param integer $count The number of items the action was performed on. |
| 302 | * @return string |
| 303 | */ |
| 304 | public function get_action_notice_text( $action, $count = 1 ) { |
| 305 | $text = ''; |
| 306 | $count = (int) $count; |
| 307 | |
| 308 | switch ( $action ) { |
| 309 | case 'acfactivatecomplete': |
| 310 | $text = sprintf( |
| 311 | /* translators: %s number of taxonomies activated */ |
| 312 | _n( '%s taxonomy activated.', '%s taxonomies activated.', $count, 'secure-custom-fields' ), |
| 313 | $count |
| 314 | ); |
| 315 | break; |
| 316 | case 'acfdeactivatecomplete': |
| 317 | $text = sprintf( |
| 318 | /* translators: %s number of taxonomies deactivated */ |
| 319 | _n( '%s taxonomy deactivated.', '%s taxonomies deactivated.', $count, 'secure-custom-fields' ), |
| 320 | $count |
| 321 | ); |
| 322 | break; |
| 323 | case 'acfduplicatecomplete': |
| 324 | $text = sprintf( |
| 325 | /* translators: %s number of taxonomies duplicated */ |
| 326 | _n( '%s taxonomy duplicated.', '%s taxonomies duplicated.', $count, 'secure-custom-fields' ), |
| 327 | $count |
| 328 | ); |
| 329 | break; |
| 330 | case 'acfsynccomplete': |
| 331 | $text = sprintf( |
| 332 | /* translators: %s number of taxonomies synchronized */ |
| 333 | _n( '%s taxonomy synchronized.', '%s taxonomies synchronized.', $count, 'secure-custom-fields' ), |
| 334 | $count |
| 335 | ); |
| 336 | break; |
| 337 | } |
| 338 | |
| 339 | return $text; |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Returns the registration error state. |
| 344 | * |
| 345 | * @since ACF 6.1 |
| 346 | * |
| 347 | * @return string |
| 348 | */ |
| 349 | public function get_registration_error_state() { |
| 350 | return '<span class="acf-js-tooltip dashicons dashicons-warning" title="' . |
| 351 | __( 'This taxonomy could not be registered because its key is in use by another taxonomy registered by another plugin or theme.', 'secure-custom-fields' ) . |
| 352 | '"></span> ' . _x( 'Registration Failed', 'post status', 'secure-custom-fields' ); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | // Instantiate. |
| 357 | acf_new_instance( 'ACF_Admin_Taxonomies' ); |
| 358 | endif; // Class exists check. |
| 359 |