1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
<?php
final class Papi_Admin_Taxonomy {
protected $taxonomy_types = [];
public function __construct() {
$this->setup_actions();
}
public function add_form_fields() {
$html_name = esc_attr( papi_get_page_type_key() );
$taxonomy = papi_get_qs( 'taxonomy' );
$taxonomy_object = get_taxonomy( $taxonomy );
$taxonomy_types = array_filter( $this->taxonomy_types, function ( $taxonomy_type ) use ( $taxonomy ) {
return in_array( $taxonomy, $taxonomy_type->taxonomy ) && $taxonomy_type->display( $taxonomy );
} );
$taxonomy_types = array_values( $taxonomy_types );
if ( empty( $taxonomy_types ) ) {
return;
}
$taxonomy_types = $this->prepare_taxonomy_types( $taxonomy_types );
if ( count( $taxonomy_types ) > 1 ):
?>
<div class="form-field">
<label for="<?php echo $html_name; ?>">
<?php echo sprintf( __( '%s type', 'papi' ), $taxonomy_object->labels->singular_name ); ?>
</label>
<select name="<?php echo $html_name; ?>" id="<?php echo $html_name; ?>" data-papi-page-type-key="true">
<?php
foreach ( $taxonomy_types as $taxonomy_type ) {
papi_render_html_tag( 'option', [
'data-redirect' => $taxonomy_type->redirect_after_create,
'value' => esc_attr( $taxonomy_type->get_id() ),
$taxonomy_type->name
] );
}
?>
</select>
<!-- additional info? -->
</div>
<?php
else:
papi_render_html_tag( 'input', [
'data-redirect' => $taxonomy_types[0]->redirect_after_create,
'data-papi-page-type-key' => true,
'name' => $html_name,
'type' => 'hidden',
'value' => esc_attr( $taxonomy_types[0]->get_id() )
] );
endif;
}
private function prepare_taxonomy_types( array $taxonomy_types ) {
$taxonomy = papi_get_qs( 'taxonomy' );
if ( papi_filter_settings_show_standard_taxonomy_type( $taxonomy ) ) {
$id = sprintf( 'papi-standard-%s-type', $taxonomy );
$taxonomy_type = new Papi_Taxonomy_Type( $id );
$taxonomy_type->id = $id;
$taxonomy_type->name = papi_filter_settings_standard_taxonomy_type_name( $taxonomy );
$taxonomy_type->taxonomy = [$taxonomy];
$taxonomy_types[] = $taxonomy_type;
}
usort( $taxonomy_types, function ( $a, $b ) {
return strcmp( $a->name, $b->name );
} );
return papi_sort_order( array_reverse( $taxonomy_types ) );
}
private function setup_actions() {
add_action( 'admin_init', [$this, 'setup_taxonomies_hooks'] );
}
public function setup_taxonomies_hooks() {
$this->taxonomy_types = papi_get_all_entry_types( [
'types' => 'taxonomy'
] );
$taxonomies = array_reduce( $this->taxonomy_types, function ( $taxonomies, $taxonomy_type ) {
return array_merge( $taxonomies, $taxonomy_type->taxonomy );
}, [] );
$taxonomies = array_unique( $taxonomies );
foreach ( $taxonomies as $taxonomy ) {
if ( is_string( $taxonomy ) && taxonomy_exists( $taxonomy ) ) {
add_action( $taxonomy . '_add_form_fields', [$this, 'add_form_fields'] );
}
}
}
}
if ( is_admin() ) {
new Papi_Admin_Taxonomy;
}