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-post-types.php
352 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; // Exit if accessed directly. |
| 5 | } |
| 6 | |
| 7 | if ( ! class_exists( 'ACF_Admin_Post_Types' ) ) : |
| 8 | |
| 9 | /** |
| 10 | * The ACF Post Types admin controller class |
| 11 | */ |
| 12 | class ACF_Admin_Post_Types 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-post-type'; |
| 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-post-types'; |
| 30 | |
| 31 | /** |
| 32 | * The name of the store used for the post type. |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | public $store = 'post-types'; |
| 37 | |
| 38 | /** |
| 39 | * Constructor. |
| 40 | * |
| 41 | * @since ACF 6.2 |
| 42 | */ |
| 43 | public function __construct() { |
| 44 | add_action( 'admin_menu', array( $this, 'admin_menu' ), 8 ); |
| 45 | parent::__construct(); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Current screen actions for the post types 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( 'post-type-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( 'post-type-first-run', true ); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Add any menu items required for post types. |
| 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, __( 'Post Types', 'secure-custom-fields' ), __( 'Post Types', 'secure-custom-fields' ), $cap, 'edit.php?post_type=acf-post-type' ); |
| 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-taxonomies' => __( 'Taxonomies', 'secure-custom-fields' ), |
| 112 | 'acf-field-groups' => __( 'Field Groups', 'secure-custom-fields' ), |
| 113 | 'acf-count' => __( 'Posts', '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-taxonomies': |
| 151 | $this->render_admin_table_column_taxonomies( $post ); |
| 152 | break; |
| 153 | |
| 154 | case 'acf-field-groups': |
| 155 | $this->render_admin_table_column_field_groups( $post ); |
| 156 | break; |
| 157 | |
| 158 | case 'acf-count': |
| 159 | $this->render_admin_table_column_num_posts( $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 post type in the list table. |
| 171 | * |
| 172 | * @since ACF 6.1 |
| 173 | * |
| 174 | * @param array $post_type The main post type array. |
| 175 | * @return void |
| 176 | */ |
| 177 | public function render_admin_table_column_field_groups( $post_type ) { |
| 178 | $field_groups = acf_get_field_groups( array( 'post_type' => $post_type['post_type'] ) ); |
| 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 taxonomies attached to the post type in the list table. |
| 201 | * |
| 202 | * @since ACF 6.1 |
| 203 | * |
| 204 | * @param array $post_type The main post type array. |
| 205 | * @return void |
| 206 | */ |
| 207 | public function render_admin_table_column_taxonomies( $post_type ) { |
| 208 | $taxonomies = array(); |
| 209 | $labels = array(); |
| 210 | |
| 211 | if ( is_array( $post_type['taxonomies'] ) ) { |
| 212 | $taxonomies = $post_type['taxonomies']; |
| 213 | } |
| 214 | |
| 215 | $acf_taxonomies = acf_get_internal_post_type_posts( 'acf-taxonomy' ); |
| 216 | |
| 217 | foreach ( $acf_taxonomies as $acf_taxonomy ) { |
| 218 | if ( is_array( $acf_taxonomy['object_type'] ) && in_array( $post_type['post_type'], $acf_taxonomy['object_type'], true ) ) { |
| 219 | $taxonomies[] = $acf_taxonomy['taxonomy']; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | $taxonomies = array_unique( $taxonomies ); |
| 224 | |
| 225 | foreach ( $taxonomies as $tax_slug ) { |
| 226 | $taxonomy = get_taxonomy( $tax_slug ); |
| 227 | |
| 228 | if ( ! is_object( $taxonomy ) || empty( $taxonomy->label ) ) { |
| 229 | continue; |
| 230 | } |
| 231 | |
| 232 | $labels[] = $taxonomy->label; |
| 233 | } |
| 234 | |
| 235 | if ( empty( $labels ) ) { |
| 236 | echo '<span class="acf-emdash" aria-hidden="true">—</span>'; |
| 237 | echo '<span class="screen-reader-text">' . esc_html__( 'No taxonomies', 'secure-custom-fields' ) . '</span>'; |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | $limit = 3; |
| 242 | $shown_labels = array_slice( $labels, 0, $limit ); |
| 243 | $hidden_labels = array_slice( $labels, $limit ); |
| 244 | $text = implode( ', ', $shown_labels ); |
| 245 | |
| 246 | if ( ! empty( $hidden_labels ) ) { |
| 247 | $text .= ', <span class="acf-more-items acf-js-tooltip" title="' . implode( ', ', $hidden_labels ) . '">+' . count( $hidden_labels ) . '</span>'; |
| 248 | } |
| 249 | |
| 250 | echo acf_esc_html( $text ); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Renders the number of posts created for the post type in the list table. |
| 255 | * |
| 256 | * @since ACF 6.1 |
| 257 | * |
| 258 | * @param array $post_type The main post type array. |
| 259 | * @return void |
| 260 | */ |
| 261 | public function render_admin_table_column_num_posts( $post_type ) { |
| 262 | $no_posts = '<span class="acf-emdash" aria-hidden="true">—</span>'; |
| 263 | $no_posts .= '<span class="screen-reader-text">' . esc_html__( 'No posts', 'secure-custom-fields' ) . '</span>'; |
| 264 | |
| 265 | // WP doesn't count posts for post types that don't exist. |
| 266 | if ( empty( $post_type['active'] ) || 'trash' === get_post_status( $post_type['ID'] ) ) { |
| 267 | echo acf_esc_html( $no_posts ); |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | $num_posts = wp_count_posts( $post_type['post_type'] ); |
| 272 | if ( is_object( $num_posts ) && property_exists( $num_posts, 'publish' ) ) { |
| 273 | $num_posts = $num_posts->publish; |
| 274 | } |
| 275 | |
| 276 | if ( ! $num_posts || ! is_numeric( $num_posts ) ) { |
| 277 | echo acf_esc_html( $no_posts ); |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | printf( |
| 282 | '<a href="%s">%s</a>', |
| 283 | esc_url( admin_url( 'edit.php?post_type=' . $post_type['post_type'] ) ), |
| 284 | esc_html( number_format_i18n( $num_posts ) ) |
| 285 | ); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Gets the translated action notice text for list table actions (activate, deactivate, sync, etc.). |
| 290 | * |
| 291 | * @since ACF 6.1 |
| 292 | * |
| 293 | * @param string $action The action being performed. |
| 294 | * @param integer $count The number of items the action was performed on. |
| 295 | * @return string |
| 296 | */ |
| 297 | public function get_action_notice_text( $action, $count = 1 ) { |
| 298 | $text = ''; |
| 299 | $count = (int) $count; |
| 300 | |
| 301 | switch ( $action ) { |
| 302 | case 'acfactivatecomplete': |
| 303 | $text = sprintf( |
| 304 | /* translators: %s number of post types activated */ |
| 305 | _n( '%s post type activated.', '%s post types activated.', $count, 'secure-custom-fields' ), |
| 306 | $count |
| 307 | ); |
| 308 | break; |
| 309 | case 'acfdeactivatecomplete': |
| 310 | $text = sprintf( |
| 311 | /* translators: %s number of post types deactivated */ |
| 312 | _n( '%s post type deactivated.', '%s post types deactivated.', $count, 'secure-custom-fields' ), |
| 313 | $count |
| 314 | ); |
| 315 | break; |
| 316 | case 'acfduplicatecomplete': |
| 317 | $text = sprintf( |
| 318 | /* translators: %s number of post types duplicated */ |
| 319 | _n( '%s post type duplicated.', '%s post types duplicated.', $count, 'secure-custom-fields' ), |
| 320 | $count |
| 321 | ); |
| 322 | break; |
| 323 | case 'acfsynccomplete': |
| 324 | $text = sprintf( |
| 325 | /* translators: %s number of post types synchronized */ |
| 326 | _n( '%s post type synchronized.', '%s post types synchronized.', $count, 'secure-custom-fields' ), |
| 327 | $count |
| 328 | ); |
| 329 | break; |
| 330 | } |
| 331 | |
| 332 | return $text; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Returns the registration error state. |
| 337 | * |
| 338 | * @since ACF 6.1 |
| 339 | * |
| 340 | * @return string |
| 341 | */ |
| 342 | public function get_registration_error_state() { |
| 343 | return '<span class="acf-js-tooltip dashicons dashicons-warning" title="' . |
| 344 | __( 'This post type could not be registered because its key is in use by another post type registered by another plugin or theme.', 'secure-custom-fields' ) . |
| 345 | '"></span> ' . _x( 'Registration Failed', 'post status', 'secure-custom-fields' ); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | // Instantiate. |
| 350 | acf_new_instance( 'ACF_Admin_Post_Types' ); |
| 351 | endif; // Class exists check. |
| 352 |