在插件管理中扩展WP_List_Table /处理复选框选项

时间:2012-02-14 14:39:21

标签: wordpress

我正在使用WordPress插件,该插件的一部分需要扩展WP_List_Table并将该表中检查的任何项目存储到一个选项中。我已经设法弄清楚如何正确设置和显示所需的表,但我如何处理存储选中的选项?

这是我到目前为止所得到的......

class TDBar_List_Table extends WP_List_Table {

    // Reference parent constructor
    function __construct() {
        global $status, $page;

        // Set defaults
        parent::__construct( array(
            'singular' => 'theme',
            'plural' => 'themes',
            'ajax' => false
        ));
    }

    // Set table classes
    function get_table_classes() {
        return array('widefat', 'wp-list-table', 'themes');
    }

    // Setup default column
    function column_default($item, $column_name) {
        switch($column_name) {
            case 'Title':
            case 'URI':
            case'Description':
                return $item[$column_name];
            default:
                return print_r($item, true);
        }
    }

    // Displaying checkboxes!
    function column_cb($item) {
        return sprintf(
            '<input type="checkbox" name="%1$s" id="%2$s" value="checked" />',
            //$this->_args['singular'],
            $item['Stylesheet'] . '_status',
            $item['Stylesheet'] . '_status'
        );
    }

    // Display theme title
    function column_title($item) {
        return sprintf(
            '<strong>%1$s</strong>',
            $item['Title']
        );
    }

    // Display theme preview
    function column_preview($item) {
        if (file_exists(get_theme_root() . '/' . $item['Stylesheet'] . '/screenshot.png')) {
            $preview = get_theme_root_uri() . '/' . $item['Stylesheet'] . '/screenshot.png';
        } else {
            $preview = '';
        }
            return sprintf(
                '<a href="%1$s" class="thickbox" title="%2$s"><img src="%3$s" style="width: 150px;" /></a>',
                $preview,
                $item['Title'],
                $preview
            );
    }

    // Display theme description
    function column_description($item) {
        if (isset($item['Version'])) {
            $version = 'Version ' . $item['Version'];
            if (isset($item['Author']) || isset($item['URI']))
                $version .= '&nbsp;|&nbsp;';
        } else {
            $version = '';
        }
        if (isset($item['Author'])) {
            $author = 'By ' . $item['Author'];
            if (isset($item['URI']))
                $author .= '&nbsp;|&nbsp;';
        } else {
            $author = '';
        }
        if (isset($item['URI'])) {
            $uri = $item['URI'];
        } else {
            $uri = '';
        }

        return sprintf(
            '<div class="theme-description"><p>%1$s</p></div><div class="second theme-version-author-uri">%2$s%3$s%4$s',
            $item['Description'],
            $version,
            $author,
            $uri
        );
    }

    // Setup columns
    function get_columns() {
        $columns = array(
            'cb' => '<input type="checkbox" />',
            'title' => 'Theme',
            'preview' => 'Preview',
            'description' => 'Description'
        );
        return $columns;
    }

    // Make title column sortable
    function get_sortable_columns() {
        $sortable_columns = array(
            'title' => array('Title', true)
        );
        return $sortable_columns;
    }

    // Setup bulk actions
    function get_bulk_actions() {
        $actions = array(
            'update' => 'Update'
        );
        return $actions;
    }

    // Handle bulk actions
    function process_bulk_action() {
        // Define our data source
        if (defined('WP_ALLOW_MULTISITE') && WP_ALLOW_MULTISITE == true) {
            $themes = get_allowed_themes();
        } else {
            $themes = get_themes();
        }

        if ('update' === $this->current_action()) {
            foreach ($themes as $theme) {
                if ($theme['Stylesheet'] . '_status' == 'checked') {
                    // Do stuff - here's the problem
                }
            }
        }
    }

    // Handle data preparation
    function prepare_items() {

        // How many records per page?
        $per_page = 10;

        // Define column headers
        $columns = $this->get_columns();
        $hidden = array();
        $sortable = $this->get_sortable_columns();

        // Build the array
        $this->_column_headers = array($columns, $hidden, $sortable);

        // Pass off bulk action
        $this->process_bulk_action();

        // Define our data source
        if (defined('WP_ALLOW_MULTISITE') && WP_ALLOW_MULTISITE == true) {
            $themes = get_allowed_themes();
        } else {
            $themes = get_themes();
        }

        // Handle sorting
        function usort_reorder($a,$b) {
            $orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'Title';
            $order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'asc';
            $result = strcmp($a[$orderby], $b[$orderby]);
            return ($order === 'asc') ? $result : -$result;
        }
        usort($themes, 'usort_reorder');

        //MAIN STUFF HERE
        //for ($i = 0; i < count($themes); $i++) {

        //}

        // Figure out the current page and how many items there are
        $current_page = $this->get_pagenum();
        $total_items = count($themes);

        // Only show the current page
        $themes = array_slice($themes,(($current_page-1)*$per_page),$per_page);

        // Display sorted data
        $this->items = $themes;

        // Register pagination options
        $this->set_pagination_args( array(
            'total_items' => $total_items,
            'per_page' => $per_page,
            'total_pages' => ceil($total_items/$per_page)
        ));
    }
}

问题是,我无法妥善保存。我选择了我想要的行,点击保存,然后重置。

1 个答案:

答案 0 :(得分:3)

我假设您正在讨论表格列表中的复选框,因此这将是如何处理批量操作。

您需要做的就是为您的类添加两个新方法,并在prepare_items方法中初始化它。我在我的一个插件中使用下面的代码来删除或导出,但您可以轻松地运行更新。

/**
 * Define our bulk actions
 * 
 * @since 1.2
 * @returns array() $actions Bulk actions
 */
function get_bulk_actions() {
    $actions = array(
        'delete' => __( 'Delete' , 'visual-form-builder'),
        'export-all' => __( 'Export All' , 'visual-form-builder'),
        'export-selected' => __( 'Export Selected' , 'visual-form-builder')
    );

    return $actions;
}

/**
 * Process our bulk actions
 * 
 * @since 1.2
 */
function process_bulk_action() {        
    $entry_id = ( is_array( $_REQUEST['entry'] ) ) ? $_REQUEST['entry'] : array( $_REQUEST['entry'] );

    if ( 'delete' === $this->current_action() ) {
        global $wpdb;

        foreach ( $entry_id as $id ) {
            $id = absint( $id );
            $wpdb->query( "DELETE FROM $this->entries_table_name WHERE entries_id = $id" );
        }
    }
}

现在,在prepare_items()中调用此方法,如下所示:

function prepare_items() {
       //Do other stuff in here

       /* Handle our bulk actions */
       $this->process_bulk_action();
}

有一个名为Custom List Table Example的精彩助手插件,可以更轻松地找出WP_List_Table类。