Skip to content

Commit

Permalink
Refactor to allow built-in public taxonomies to be displayed
Browse files Browse the repository at this point in the history
also modifies the view to show singular name
  • Loading branch information
freibergergarcia committed Feb 11, 2024
1 parent 13eb2ec commit c64ac6b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
25 changes: 24 additions & 1 deletion includes/Admin/Post_Type_Form_Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function render_form(): void {

$post_type_data = null;
$post_type = get_post_type_object( $post_type_name );
$taxonomies = get_taxonomies( array( '_builtin' => false ) );
$taxonomies = $this->get_taxonomies_filtered();

if ( $post_type instanceof WP_Post_Type ) {
$post_types = get_option( CUSTOM_PTT_POST_TYPE_OPTION_NAME, array() );
Expand All @@ -94,4 +94,27 @@ public function render_form(): void {

require __DIR__ . '/templates/custom-ptt-post-types-form.php';
}

/**
* Filter taxonomies to show built-in public ones as well as custom ones.
*
* @return array
*
* @since 0.2.1
*/
private function get_taxonomies_filtered(): array {
$taxonomies = get_taxonomies( array(), 'objects' );
$filtered_taxonomies = array();

foreach ( $taxonomies as $key => $taxonomy ) {

if ( $taxonomy->_builtin && ! $taxonomy->public ) {
continue;
}

$filtered_taxonomies[ $key ] = $taxonomy;
}

return $filtered_taxonomies;
}
}
12 changes: 9 additions & 3 deletions includes/Admin/templates/custom-ptt-post-types-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,16 @@
<div class="mb-6">
<label class="block text-gray-700 text-sm font-bold mb-2"><?php esc_html_e( 'Attach to Taxonomies:', 'custom-post-types-post-types' ); ?></label>
<div class="flex flex-col pl-4">
<?php foreach ( $taxonomies as $taxonomy_slug => $taxonomy_label ) { ?>
<?php
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ) {
if ( ! $taxonomy instanceof WP_Taxonomy ) {
continue;
}

?>
<div class="mb-2">
<input type="checkbox" id="taxonomy-<?php echo esc_attr( $taxonomy_slug ); ?>" name="taxonomies[]" value="<?php echo esc_attr( $taxonomy_slug ); ?>" <?php checked( in_array( $taxonomy_slug, $post_type_data['taxonomies'] ?? array(), true ) ); ?> class="form-checkbox">
<label for="taxonomy-<?php echo esc_attr( $taxonomy_slug ); ?>" class="text-sm"><?php echo esc_html( $taxonomy_label ); ?></label>
<input type="checkbox" id="taxonomy-<?php echo esc_attr( $taxonomy->rewrite->slug ); ?>" name="taxonomies[]" value="<?php echo esc_attr( $taxonomy->rewrite->slug ); ?>" <?php checked( in_array( $taxonomy->rewrite->slug, $post_type_data['taxonomies'] ?? array(), true ) ); ?> class="form-checkbox">
<label for="taxonomy-<?php echo esc_attr( $taxonomy->rewrite->slug ); ?>" class="text-sm"><?php echo esc_html( $taxonomy->labels->singular_name ); ?></label>
</div>
<?php } ?>
</div>
Expand Down

0 comments on commit c64ac6b

Please sign in to comment.