PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.4.0-beta2
Secure Custom Fields v6.4.0-beta2
6.9.5 6.9.4 6.9.3 6.9.2 6.9.1 6.9.0 6.8.9 6.8.7 6.8.8 6.8.6 6.8.4 6.8.5 trunk 6.4.0-beta1 6.4.0-beta2 6.4.1 6.4.1-beta3 6.4.1-beta4 6.4.1-beta5 6.4.1-beta6 6.4.1-beta7 6.4.2 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.5.5 6.5.6 6.5.7 6.6.0 6.7.0 6.7.1 6.8.0 6.8.1 6.8.2 6.8.3
secure-custom-fields / includes / admin / post-types / admin-post-types.php
secure-custom-fields / includes / admin / post-types Last commit date
admin-field-group.php 1 year ago admin-field-groups.php 1 year ago admin-post-type.php 1 year ago admin-post-types.php 1 year ago admin-taxonomies.php 1 year ago admin-taxonomy.php 1 year ago index.php 1 year ago
admin-post-types.php
353 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 #[AllowDynamicProperties]
13 class ACF_Admin_Post_Types extends ACF_Admin_Internal_Post_Type_List {
14
15 /**
16 * The slug for the internal post type.
17 *
18 * @since 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 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 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 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 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', 'acf' ), __( 'Post Types', 'acf' ), $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 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 $this->not_found_label = $wp_post_types[ $this->post_type ]->labels->not_found;
104 $wp_post_types[ $this->post_type ]->labels->not_found = $this->get_not_found_html();
105 }
106
107 $columns = array(
108 'cb' => $_columns['cb'],
109 'title' => $_columns['title'],
110 'acf-description' => __( 'Description', 'acf' ),
111 'acf-key' => __( 'Key', 'acf' ),
112 'acf-taxonomies' => __( 'Taxonomies', 'acf' ),
113 'acf-field-groups' => __( 'Field Groups', 'acf' ),
114 'acf-count' => __( 'Posts', 'acf' ),
115 );
116
117 if ( acf_get_local_json_files( $this->post_type ) ) {
118 $columns['acf-json'] = __( 'Local JSON', 'acf' );
119 }
120
121 return $columns;
122 }
123
124 /**
125 * Renders a specific admin table column.
126 *
127 * @date 17/4/20
128 * @since 5.9.0
129 *
130 * @param string $column_name The name of the column to display.
131 * @param array $post The main ACF post array.
132 * @return void
133 */
134 public function render_admin_table_column( $column_name, $post ) {
135 switch ( $column_name ) {
136 case 'acf-key':
137 echo '<i class="acf-icon acf-icon-key-solid"></i>';
138 echo esc_html( $post['key'] );
139 break;
140
141 // Description.
142 case 'acf-description':
143 if ( ( is_string( $post['description'] ) || is_numeric( $post['description'] ) ) && ! empty( $post['description'] ) ) {
144 echo '<span class="acf-description">' . acf_esc_html( $post['description'] ) . '</span>';
145 } else {
146 echo '<span class="acf-emdash" aria-hidden="true">—</span>';
147 echo '<span class="screen-reader-text">' . esc_html__( 'No description', 'acf' ) . '</span>';
148 }
149 break;
150
151 case 'acf-taxonomies':
152 $this->render_admin_table_column_taxonomies( $post );
153 break;
154
155 case 'acf-field-groups':
156 $this->render_admin_table_column_field_groups( $post );
157 break;
158
159 case 'acf-count':
160 $this->render_admin_table_column_num_posts( $post );
161 break;
162
163 // Local JSON.
164 case 'acf-json':
165 $this->render_admin_table_column_local_status( $post );
166 break;
167 }
168 }
169
170 /**
171 * Renders the field groups attached to the post type in the list table.
172 *
173 * @since 6.1
174 *
175 * @param array $post_type The main post type array.
176 * @return void
177 */
178 public function render_admin_table_column_field_groups( $post_type ) {
179 $field_groups = acf_get_field_groups( array( 'post_type' => $post_type['post_type'] ) );
180
181 if ( empty( $field_groups ) ) {
182 echo '<span class="acf-emdash" aria-hidden="true">—</span>';
183 echo '<span class="screen-reader-text">' . esc_html__( 'No field groups', 'acf' ) . '</span>';
184 return;
185 }
186
187 $labels = wp_list_pluck( $field_groups, 'title' );
188 $limit = 3;
189 $shown_labels = array_slice( $labels, 0, $limit );
190 $hidden_labels = array_slice( $labels, $limit );
191 $text = implode( ', ', $shown_labels );
192
193 if ( ! empty( $hidden_labels ) ) {
194 $text .= ', <span class="acf-more-items acf-js-tooltip" title="' . implode( ', ', $hidden_labels ) . '">+' . count( $hidden_labels ) . '</span>';
195 }
196
197 echo acf_esc_html( $text );
198 }
199
200 /**
201 * Renders the taxonomies attached to the post type in the list table.
202 *
203 * @since 6.1
204 *
205 * @param array $post_type The main post type array.
206 * @return void
207 */
208 public function render_admin_table_column_taxonomies( $post_type ) {
209 $taxonomies = array();
210 $labels = array();
211
212 if ( is_array( $post_type['taxonomies'] ) ) {
213 $taxonomies = $post_type['taxonomies'];
214 }
215
216 $acf_taxonomies = acf_get_internal_post_type_posts( 'acf-taxonomy' );
217
218 foreach ( $acf_taxonomies as $acf_taxonomy ) {
219 if ( is_array( $acf_taxonomy['object_type'] ) && in_array( $post_type['post_type'], $acf_taxonomy['object_type'], true ) ) {
220 $taxonomies[] = $acf_taxonomy['taxonomy'];
221 }
222 }
223
224 $taxonomies = array_unique( $taxonomies );
225
226 foreach ( $taxonomies as $tax_slug ) {
227 $taxonomy = get_taxonomy( $tax_slug );
228
229 if ( ! is_object( $taxonomy ) || empty( $taxonomy->label ) ) {
230 continue;
231 }
232
233 $labels[] = $taxonomy->label;
234 }
235
236 if ( empty( $labels ) ) {
237 echo '<span class="acf-emdash" aria-hidden="true">—</span>';
238 echo '<span class="screen-reader-text">' . esc_html__( 'No taxonomies', 'acf' ) . '</span>';
239 return;
240 }
241
242 $limit = 3;
243 $shown_labels = array_slice( $labels, 0, $limit );
244 $hidden_labels = array_slice( $labels, $limit );
245 $text = implode( ', ', $shown_labels );
246
247 if ( ! empty( $hidden_labels ) ) {
248 $text .= ', <span class="acf-more-items acf-js-tooltip" title="' . implode( ', ', $hidden_labels ) . '">+' . count( $hidden_labels ) . '</span>';
249 }
250
251 echo acf_esc_html( $text );
252 }
253
254 /**
255 * Renders the number of posts created for the post type in the list table.
256 *
257 * @since 6.1
258 *
259 * @param array $post_type The main post type array.
260 * @return void
261 */
262 public function render_admin_table_column_num_posts( $post_type ) {
263 $no_posts = '<span class="acf-emdash" aria-hidden="true">—</span>';
264 $no_posts .= '<span class="screen-reader-text">' . esc_html__( 'No posts', 'acf' ) . '</span>';
265
266 // WP doesn't count posts for post types that don't exist.
267 if ( empty( $post_type['active'] ) || 'trash' === get_post_status( $post_type['ID'] ) ) {
268 echo acf_esc_html( $no_posts );
269 return;
270 }
271
272 $num_posts = wp_count_posts( $post_type['post_type'] );
273 if ( is_object( $num_posts ) && property_exists( $num_posts, 'publish' ) ) {
274 $num_posts = $num_posts->publish;
275 }
276
277 if ( ! $num_posts || ! is_numeric( $num_posts ) ) {
278 echo acf_esc_html( $no_posts );
279 return;
280 }
281
282 printf(
283 '<a href="%s">%s</a>',
284 esc_url( admin_url( 'edit.php?post_type=' . $post_type['post_type'] ) ),
285 esc_html( number_format_i18n( $num_posts ) )
286 );
287 }
288
289 /**
290 * Gets the translated action notice text for list table actions (activate, deactivate, sync, etc.).
291 *
292 * @since 6.1
293 *
294 * @param string $action The action being performed.
295 * @param integer $count The number of items the action was performed on.
296 * @return string
297 */
298 public function get_action_notice_text( $action, $count = 1 ) {
299 $text = '';
300 $count = (int) $count;
301
302 switch ( $action ) {
303 case 'acfactivatecomplete':
304 $text = sprintf(
305 /* translators: %s number of post types activated */
306 _n( 'Post type activated.', '%s post types activated.', $count, 'acf' ),
307 $count
308 );
309 break;
310 case 'acfdeactivatecomplete':
311 $text = sprintf(
312 /* translators: %s number of post types deactivated */
313 _n( 'Post type deactivated.', '%s post types deactivated.', $count, 'acf' ),
314 $count
315 );
316 break;
317 case 'acfduplicatecomplete':
318 $text = sprintf(
319 /* translators: %s number of post types duplicated */
320 _n( 'Post type duplicated.', '%s post types duplicated.', $count, 'acf' ),
321 $count
322 );
323 break;
324 case 'acfsynccomplete':
325 $text = sprintf(
326 /* translators: %s number of post types synchronized */
327 _n( 'Post type synchronized.', '%s post types synchronized.', $count, 'acf' ),
328 $count
329 );
330 break;
331 }
332
333 return $text;
334 }
335
336 /**
337 * Returns the registration error state.
338 *
339 * @since 6.1
340 *
341 * @return string
342 */
343 public function get_registration_error_state() {
344 return '<span class="acf-js-tooltip dashicons dashicons-warning" title="' .
345 __( 'This post type could not be registered because its key is in use by another post type registered by another plugin or theme.', 'acf' ) .
346 '"></span> ' . _x( 'Registration Failed', 'post status', 'acf' );
347 }
348 }
349
350 // Instantiate.
351 acf_new_instance( 'ACF_Admin_Post_Types' );
352 endif; // Class exists check.
353