Browse code

테이블 기능 구현

Nepirity Corp authored on23/03/2018 15:55:13
Showing2 changed files

1 1
new file mode 100644
... ...
@@ -0,0 +1,127 @@
1
+<?php
2
+
3
+if(!class_exists('WP_List_Table')) {
4
+  require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
5
+}
6
+
7
+class admin_list_table extends WP_List_Table {
8
+  public function __construct() {
9
+    parent::__construct(array(
10
+      'singular' => 'Customer',  //singular name of the listed records
11
+      'plural'   => 'Customers', //plural name of the listed records
12
+      'ajax'     => false,       //should this table support ajax?
13
+      'screen'   => null
14
+   ));
15
+  }
16
+
17
+  public static function get_customers($per_page = 5, $page_number = 1) {
18
+    global $wpdb;
19
+
20
+    $sql = "SELECT * FROM {$wpdb->prefix}customers";
21
+
22
+    if (!empty($_REQUEST['orderby'])) {
23
+      $sql .= ' ORDER BY ' . esc_sql($_REQUEST['orderby']);
24
+      $sql .= ! empty($_REQUEST['order']) ? ' ' . esc_sql($_REQUEST['order']) : ' ASC';
25
+    }
26
+
27
+    $sql .= " LIMIT $per_page";
28
+    $sql .= ' OFFSET ' . ($page_number - 1) * $per_page;
29
+
30
+    $result = $wpdb->get_results($sql, 'ARRAY_A');
31
+
32
+    return $result;
33
+  }
34
+
35
+  public static function delete_customer($id) {
36
+    global $wpdb;
37
+
38
+    $wpdb->delete(
39
+      "{$wpdb->prefix}customers",
40
+      array('ID' => $id),
41
+      array('%d')
42
+   );
43
+  }
44
+
45
+  public static function record_count() {
46
+    global $wpdb;
47
+
48
+    $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}customers";
49
+    return $wpdb->get_var($sql);
50
+  }
51
+
52
+  public function column_default($item, $column_name) {
53
+    switch ($column_name) {
54
+    case 'address':
55
+    case 'city':
56
+      return $item[$column_name];
57
+    default:
58
+      return print_r($item, true); //Show the whole array for troubleshooting purposes
59
+    }
60
+  }
61
+
62
+  function column_cb($item) {
63
+    return sprintf(
64
+      '<input type="checkbox" name="bulk-delete[]" value="%s" />', $item['ID']
65
+   );
66
+  }
67
+
68
+  function column_name($item) {
69
+    $delete_nonce = wp_create_nonce('delete_customer');
70
+
71
+    $title = '<strong>' . $item['name'] . '</strong>';
72
+
73
+    $actions = array(
74
+      'delete' => sprintf('<a href="?page=%s&action=%s&customer=%s&_wpnonce=%s">Delete</a>',
75
+      esc_attr($_REQUEST['page']), 'delete', absint($item['ID']), $delete_nonce)
76
+    );
77
+
78
+    return $title . $this->row_actions($actions);
79
+  }
80
+
81
+  function get_columns() {
82
+    $columns = array(
83
+      'cb'      => '<input type="checkbox" />',
84
+      'name'    => 'Name',
85
+      'address' => 'Address',
86
+      'city'    => 'City'
87
+   );
88
+
89
+    return $columns;
90
+  }
91
+
92
+  public function get_sortable_columns() {
93
+    $sortable_columns = array(
94
+      'name' => array('name', true),
95
+      'city' => array('city', true)
96
+   );
97
+
98
+    return $sortable_columns;
99
+  }
100
+
101
+  public function get_bulk_actions() {
102
+    $actions = array('bulk-delete' => 'Delete');
103
+
104
+    return $actions;
105
+  }
106
+
107
+  public function prepare_items() {
108
+    $this->_column_headers = $this->get_column_info();
109
+
110
+    /** Process bulk action */
111
+    $this->process_bulk_action();
112
+
113
+    $per_page     = $this->get_items_per_page('customers_per_page', 5);
114
+    $current_page = $this->get_pagenum();
115
+    $total_items  = self::record_count();
116
+
117
+    $this->set_pagination_args(array(
118
+      'total_items' => $total_items, //WE have to calculate the total number of items
119
+      'per_page'    => $per_page //WE have to determine how many items to show on a page
120
+    ));
121
+
122
+    $this->items = self::get_customers($per_page, $current_page);
123
+  }
124
+
125
+  public function process_bulk_action() {
126
+  }
127
+}
... ...
@@ -7,8 +7,11 @@
7 7
  * Author URI:  https://hiseon.me/
8 8
  */
9 9
 
10
+include_once "table.php";
11
+
10 12
 class admin_table_plugin {
11 13
   protected static $_instance = null;
14
+  public $_tablelist;
12 15
 
13 16
   public static function instance() {
14 17
     if (is_null(self::$_instance)) {
... ...
@@ -19,6 +22,7 @@ class admin_table_plugin {
19 22
   }
20 23
 
21 24
   protected function __construct() {
25
+    $this->init();
22 26
   }
23 27
 
24 28
   private function __clone() {}
... ...
@@ -40,17 +44,58 @@ class admin_table_plugin {
40 44
         name text,
41 45
         address text,
42 46
         city text
43
-      ) ;";
44
-      require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
45
-      dbDelta( $sql );
47
+      );";
48
+      require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
49
+      dbDelta($sql);
46 50
     }
47 51
   }
52
+
53
+  public function init() {
54
+    add_filter('set-screen-option', array($this, 'set_screen'), 10, 3);
55
+    add_action('admin_menu', array($this, 'admin_menu'));
56
+  }
57
+
58
+  public function set_screen($status, $option, $value) {
59
+    return $value;
60
+  }
61
+
62
+  public function admin_menu() {
63
+    $hook = add_menu_page('페이지 상단제목', '버튼이름2', 'manage_options', 'admintable',
64
+      array($this, 'admin_page'), 'dashicons-admin-post');
65
+
66
+    add_action("load-$hook", array($this, 'screen_option'));
67
+  }
68
+
69
+  public function screen_option() {
70
+    $option = 'per_page';
71
+    $args   = array(
72
+      'label'   => 'Customers',
73
+      'default' => 10,
74
+      'option'  => 'customers_per_page'
75
+    );
76
+
77
+    add_screen_option($option, $args);
78
+    $this->_tablelist = new admin_list_table();
79
+  }
80
+
81
+  public function admin_page() {
82
+    echo '<div class="wrap"><h2>My List Table Test</h2>';
83
+    echo '<form method="post">';
84
+
85
+    $this->_tablelist->prepare_items();
86
+    $this->_tablelist->display();
87
+
88
+    echo '</form>';
89
+    echo '</div>';
90
+  }
48 91
 }
49 92
 
50 93
 function ATP() {
51 94
   return admin_table_plugin::instance();
52 95
 }
53 96
 
97
+ATP();
98
+
54 99
 function admin_table_activate() {
55 100
   ATP()->install();
56 101
 }