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.

'; } } } public function get_sortable_columns() { return array( 'title' => array( 'title', false ), 'slug' => array( 'slug', false ), 'parent' => array( 'parent', false ), 'fields' => array( 'fields_count', false ), ); } public function column_default( $item, $column_name ) { return isset( $item[ $column_name ] ) ? esc_html( $item[ $column_name ] ) : ''; } public function column_cb( $item ) { return sprintf( '', esc_attr( $item['index'] ) ); } public function column_title( $item ) { $view_url = admin_url( 'admin.php?page=metaforge_opts_' . $item['slug'] ); $edit_url = admin_url( 'admin.php?page=metaforge_options_pages&action=edit&index=' . $item['index'] ); $delete_url = wp_nonce_url( admin_url( 'admin.php?page=metaforge_options_pages&action=delete&index=' . $item['index'] ), 'metaforge_delete_opts_page' ); $actions = array( 'view' => 'View', 'edit' => 'Edit', 'delete' => 'Delete', ); return sprintf( '
%s %s
', esc_url( $view_url ), esc_html( $item['title'] ), $this->row_actions( $actions ) ); } public function column_slug( $item ) { return '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 ''; $this->single_row_columns( $item ); echo ''; } }