sprintf(
'%s (%d)',
admin_url( 'admin.php?page=metaforge_taxonomies' ),
__( 'All', 'metaforge-engine' ),
$count
),
);
}
public function get_columns() {
return array(
'cb' => '',
'title' => 'Name',
'slug' => 'Slug',
'post_types' => 'Post Types',
'hierarchical' => 'Hierarchical',
'rest_api' => 'REST API',
);
}
public function get_bulk_actions() {
return array(
'bulk-delete' => 'Delete',
);
}
public function process_bulk_action() {
if ( 'bulk-delete' === $this->current_action() ) {
check_admin_referer( 'metaforge_bulk_taxonomies', 'metaforge_bulk_taxonomies_nonce' );
if ( ! current_user_can( 'manage_metaforge' ) ) {
wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'metaforge-engine' ) );
}
$delete_slugs = isset( $_GET['bulk-delete'] ) ? array_map( 'sanitize_key', wp_unslash( (array) $_GET['bulk-delete'] ) ) : array();
if ( ! empty( $delete_slugs ) ) {
require_once METAFORGE_PATH . 'admin/TaxonomyManager.php';
$manager = new MetaForge_TaxonomyManager();
foreach ( $delete_slugs as $slug ) {
$manager->delete_taxonomy_completely( $slug );
}
echo '
';
}
}
}
public function get_sortable_columns() {
return array(
'title' => array( 'plural_label', false ),
'slug' => array( 'slug', false ),
);
}
public function column_default( $item, $column_name ) {
return isset( $item[ $column_name ] ) ? $item[ $column_name ] : '';
}
public function column_cb( $item ) {
return sprintf(
'',
esc_attr( $item['slug'] ?? '' )
);
}
public function column_title( $item ) {
$slug = esc_attr( $item['slug'] ?? '' );
$edit_url = '?page=metaforge_taxonomies&action=edit&slug=' . rawurlencode( $slug );
$actions = array(
'edit' => 'Edit',
'delete' => 'Delete',
);
return ' ' . esc_html( $item['plural_label'] ) . ''
. $this->row_actions( $actions );
}
public function column_slug( $item ) {
return esc_html( $item['slug'] ?? '' );
}
public function column_post_types( $item ) {
$pts = $item['post_types'] ?? array();
if ( empty( $pts ) ) {
return '—';
}
$html = '';
foreach ( $pts as $pt ) {
$html .= '' . esc_html( ucfirst( $pt ) ) . '';
}
return $html;
}
public function column_hierarchical( $item ) {
$yes = ! empty( $item['hierarchical'] );
$label = $yes ? 'Yes' : 'No';
$tag_class = $yes ? 'mfe-tag-green' : 'mfe-tag-gray';
return '' . $label . '';
}
public function column_rest_api( $item ) {
$yes = ! empty( $item['show_in_rest'] );
$label = $yes ? 'Yes' : 'No';
$tag_class = $yes ? 'mfe-tag-green' : 'mfe-tag-gray';
return '' . $label . '';
}
public function prepare_items() {
$this->process_bulk_action();
$taxs = get_option( 'metaforge_taxonomies', array() );
$data = array();
foreach ( $taxs as $index => $tax ) {
$tax['index'] = $index;
$data[] = $tax;
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- WP List Table search parameter.
$search = isset( $_REQUEST['s'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) : '';
if ( $search ) {
$data = array_filter(
$data,
function ( $tax ) use ( $search ) {
return stripos( $tax['plural_label'], $search ) !== false || stripos( $tax['slug'], $search ) !== false;
}
);
}
// phpcs:disable WordPress.Security.NonceVerification.Recommended -- WP List Table sorting parameters.
if ( isset( $_GET['orderby'] ) ) {
$orderby = sanitize_key( wp_unslash( $_GET['orderby'] ) );
$order = isset( $_GET['order'] ) ? sanitize_key( wp_unslash( $_GET['order'] ) ) : 'asc';
usort(
$data,
function ( $a, $b ) use ( $orderby, $order ) {
$valA = isset( $a[ $orderby ] ) ? $a[ $orderby ] : '';
$valB = isset( $b[ $orderby ] ) ? $b[ $orderby ] : '';
$result = strnatcasecmp( strval( $valA ), strval( $valB ) );
return ( $order === 'asc' ) ? $result : -$result;
}
);
}
// phpcs:enable WordPress.Security.NonceVerification.Recommended
$per_page = $this->get_items_per_page( 'metaforge_taxonomies_per_page', 20 );
$current_page = $this->get_pagenum();
$total_items = count( $data );
$data = array_slice( $data, ( ( $current_page - 1 ) * $per_page ), $per_page );
$this->items = $data;
$this->_column_headers = array(
$this->get_columns(),
get_hidden_columns( $this->screen ),
$this->get_sortable_columns(),
'title',
);
$this->set_pagination_args(
array(
'total_items' => $total_items,
'per_page' => $per_page,
'total_pages' => ceil( $total_items / $per_page ),
)
);
}
// Assigning ID to rows for AJAX deletion
public function single_row( $item ) {
echo '';
$this->single_row_columns( $item );
echo '
';
}
}