sprintf( '%s (%d)', admin_url( 'admin.php?page=metaforge_options_pages' ), __( 'All', 'metaforge-engine' ), $count ), ); } public function get_columns() { return array( 'cb' => '', 'title' => 'Title', 'slug' => 'Slug / Menu Slug', 'parent' => 'Parent Menu', 'fields' => 'Fields', ); } protected function get_primary_column_name() { return 'title'; } 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_options_pages', 'metaforge_bulk_options_pages_nonce' ); if ( ! current_user_can( 'manage_metaforge' ) ) { wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'metaforge-engine' ) ); } $delete_ids = isset( $_GET['bulk-delete'] ) ? array_map( 'intval', wp_unslash( (array) $_GET['bulk-delete'] ) ) : array(); if ( ! empty( $delete_ids ) ) { $pages = get_option( 'metaforge_options_pages', array() ); foreach ( $delete_ids as $id ) { unset( $pages[ $id ] ); } update_option( 'metaforge_options_pages', array_values( $pages ) ); echo '
Options pages deleted.
metaforge_opts_' . esc_html( $item['slug'] ?? '' ) . '';
}
public function column_parent( $item ) {
$parent = $item['parent'] ?? 'metaforge_options_pages';
if ( $parent === 'mfe_options_pages' ) {
$parent = 'metaforge_options_pages';
}
$labels = array(
'metaforge_options_pages' => 'Options Pages (Default)',
'metaforge' => 'MetaForge',
'options-general.php' => 'Settings',
'themes.php' => 'Appearance',
'tools.php' => 'Tools',
'plugins.php' => 'Plugins',
'edit.php' => 'Posts',
'edit.php?post_type=page' => 'Pages',
'none' => 'Top Level Menu',
);
$label = $labels[ $parent ] ?? $parent;
if ( strpos( $parent, 'metaforge_opts_' ) === 0 ) {
$parent_slug = substr( $parent, 9 );
$pages = get_option( 'metaforge_options_pages', array() );
foreach ( $pages as $p ) {
if ( ( $p['slug'] ?? '' ) === $parent_slug ) {
$label = 'Child of ' . ( $p['title'] ?? '' );
break;
}
}
}
return '' . esc_html( $label ) . '';
}
public function column_fields( $item ) {
$count = $item['fields_count'] ?? 0;
$tag_class = $count > 0 ? 'mfe-tag-blue' : 'mfe-tag-gray';
return ''
. $count . ' field' . ( $count !== 1 ? 's' : '' ) . '';
}
public function prepare_items() {
$this->process_bulk_action();
$pages = get_option( 'metaforge_options_pages', array() );
$data = array();
$mgr = null;
if ( class_exists( 'MetaForge_OptionsPageManager' ) ) {
$mgr = new MetaForge_OptionsPageManager();
}
foreach ( $pages as $index => $page ) {
$page['index'] = $index;
$slug = $page['slug'] ?? '';
$field_count = 0;
if ( $mgr && ! empty( $slug ) ) {
$groups = $mgr->get_groups_for_options_page( $slug );
foreach ( $groups as $g ) {
$field_count += count( $g['fields'] ?? array() );
}
}
$page['fields_count'] = $field_count;
$data[] = $page;
}
// 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 ( $page ) use ( $search ) {
return stripos( $page['title'], $search ) !== false || stripos( $page['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_options_pages_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 ),
)
);
}
public function single_row( $item ) {
echo '