Browse code

주석 추가

Nepirity Corp authored on11/05/2018 13:49:29
Showing1 changed files
... ...
@@ -52,6 +52,9 @@ class admin_list_table extends WP_List_Table {
52 52
     return $wpdb->get_var($sql);
53 53
   }
54 54
 
55
+  /*
56
+   * 그외 컬럼의 값
57
+   */
55 58
   public function column_default($item, $column_name) {
56 59
     switch ($column_name) {
57 60
     case 'address':
... ...
@@ -62,12 +65,18 @@ class admin_list_table extends WP_List_Table {
62 65
     }
63 66
   }
64 67
 
68
+  /*
69
+   * cb 컬럼의 값
70
+   */
65 71
   function column_cb($item) {
66 72
     return sprintf(
67 73
       '<input type="checkbox" name="bulk-delete[]" value="%s" />', $item['ID']
68 74
     );
69 75
   }
70 76
 
77
+  /*
78
+   * name 컬럼의 값
79
+   */
71 80
   function column_name($item) {
72 81
     $delete_nonce = wp_create_nonce('delete_item');
73 82
 
Browse code

수정 내용 저장

Nepirity Corp authored on23/03/2018 16:34:18
Showing1 changed files
... ...
@@ -32,9 +32,7 @@ class admin_list_table extends WP_List_Table {
32 32
     $sql .= " LIMIT $per_page";
33 33
     $sql .= ' OFFSET ' . ($page_number - 1) * $per_page;
34 34
 
35
-    $result = $wpdb->get_results($sql, 'ARRAY_A');
36
-
37
-    return $result;
35
+    return $wpdb->get_results($sql, 'ARRAY_A');
38 36
   }
39 37
 
40 38
   public function delete_item($id) {
Browse code

리펙토링

Nepirity Corp authored on23/03/2018 16:26:52
Showing1 changed files
... ...
@@ -19,7 +19,6 @@ class admin_list_table extends WP_List_Table {
19 19
     return $wpdb->prefix . "customers";
20 20
   }
21 21
 
22
-
23 22
   public function get_item_data($per_page = 5, $page_number = 1) {
24 23
     global $wpdb;
25 24
 
... ...
@@ -131,10 +130,7 @@ class admin_list_table extends WP_List_Table {
131 130
     if ('delete' === $this->current_action()) {
132 131
       $nonce = esc_attr($_REQUEST['_wpnonce']);
133 132
 
134
-      if (! wp_verify_nonce($nonce, 'delete_item')) {
135
-        die('Go get a life script kiddies');
136
-      }
137
-      else {
133
+      if (wp_verify_nonce($nonce, 'delete_item')) {
138 134
         $this->delete_item(absint($_GET['customer']));
139 135
       }
140 136
     }
Browse code

리펙토링

Nepirity Corp authored on23/03/2018 16:24:46
Showing1 changed files
... ...
@@ -14,10 +14,16 @@ class admin_list_table extends WP_List_Table {
14 14
     ));
15 15
   }
16 16
 
17
+  protected function get_table_name() {
18
+    global $wpdb;
19
+    return $wpdb->prefix . "customers";
20
+  }
21
+
22
+
17 23
   public function get_item_data($per_page = 5, $page_number = 1) {
18 24
     global $wpdb;
19 25
 
20
-    $sql = "SELECT * FROM {$wpdb->prefix}customers";
26
+    $sql = "SELECT * FROM ". $this->get_table_name();
21 27
 
22 28
     if (!empty($_REQUEST['orderby'])) {
23 29
       $sql .= ' ORDER BY ' . esc_sql($_REQUEST['orderby']);
... ...
@@ -36,7 +42,7 @@ class admin_list_table extends WP_List_Table {
36 42
     global $wpdb;
37 43
 
38 44
     $wpdb->delete(
39
-      "{$wpdb->prefix}customers",
45
+      $this->get_table_name(),
40 46
       array('ID' => $id),
41 47
       array('%d')
42 48
     );
... ...
@@ -45,7 +51,7 @@ class admin_list_table extends WP_List_Table {
45 51
   public function record_count() {
46 52
     global $wpdb;
47 53
 
48
-    $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}customers";
54
+    $sql = "SELECT COUNT(*) FROM ". $this->get_table_name();
49 55
     return $wpdb->get_var($sql);
50 56
   }
51 57
 
Browse code

함수 이름 수정

Nepirity Corp authored on23/03/2018 16:21:24
Showing1 changed files
... ...
@@ -14,7 +14,7 @@ class admin_list_table extends WP_List_Table {
14 14
     ));
15 15
   }
16 16
 
17
-  public function get_customers($per_page = 5, $page_number = 1) {
17
+  public function get_item_data($per_page = 5, $page_number = 1) {
18 18
     global $wpdb;
19 19
 
20 20
     $sql = "SELECT * FROM {$wpdb->prefix}customers";
... ...
@@ -32,7 +32,7 @@ class admin_list_table extends WP_List_Table {
32 32
     return $result;
33 33
   }
34 34
 
35
-  public function delete_customer($id) {
35
+  public function delete_item($id) {
36 36
     global $wpdb;
37 37
 
38 38
     $wpdb->delete(
... ...
@@ -66,7 +66,7 @@ class admin_list_table extends WP_List_Table {
66 66
   }
67 67
 
68 68
   function column_name($item) {
69
-    $delete_nonce = wp_create_nonce('delete_customer');
69
+    $delete_nonce = wp_create_nonce('delete_item');
70 70
 
71 71
     $title = '<strong>' . $item['name'] . '</strong>';
72 72
 
... ...
@@ -118,18 +118,18 @@ class admin_list_table extends WP_List_Table {
118 118
       'per_page'    => $per_page //WE have to determine how many items to show on a page
119 119
     ));
120 120
 
121
-    $this->items = $this->get_customers($per_page, $current_page);
121
+    $this->items = $this->get_item_data($per_page, $current_page);
122 122
   }
123 123
 
124 124
   public function process_bulk_action() {
125 125
     if ('delete' === $this->current_action()) {
126 126
       $nonce = esc_attr($_REQUEST['_wpnonce']);
127 127
 
128
-      if (! wp_verify_nonce($nonce, 'delete_customer')) {
128
+      if (! wp_verify_nonce($nonce, 'delete_item')) {
129 129
         die('Go get a life script kiddies');
130 130
       }
131 131
       else {
132
-        $this->delete_customer(absint($_GET['customer']));
132
+        $this->delete_item(absint($_GET['customer']));
133 133
       }
134 134
     }
135 135
 
... ...
@@ -138,7 +138,7 @@ class admin_list_table extends WP_List_Table {
138 138
       $delete_ids = esc_sql($_POST['bulk-delete']);
139 139
 
140 140
       foreach ($delete_ids as $id) {
141
-        $this->delete_customer($id);
141
+        $this->delete_item($id);
142 142
       }
143 143
     }
144 144
   }
Browse code

삭제 기능 구현

Nepirity Corp authored on23/03/2018 16:11:57
Showing1 changed files
... ...
@@ -11,10 +11,10 @@ class admin_list_table extends WP_List_Table {
11 11
       'plural'   => 'Customers', //plural name of the listed records
12 12
       'ajax'     => false,       //should this table support ajax?
13 13
       'screen'   => null
14
-   ));
14
+    ));
15 15
   }
16 16
 
17
-  public static function get_customers($per_page = 5, $page_number = 1) {
17
+  public function get_customers($per_page = 5, $page_number = 1) {
18 18
     global $wpdb;
19 19
 
20 20
     $sql = "SELECT * FROM {$wpdb->prefix}customers";
... ...
@@ -32,17 +32,17 @@ class admin_list_table extends WP_List_Table {
32 32
     return $result;
33 33
   }
34 34
 
35
-  public static function delete_customer($id) {
35
+  public function delete_customer($id) {
36 36
     global $wpdb;
37 37
 
38 38
     $wpdb->delete(
39 39
       "{$wpdb->prefix}customers",
40 40
       array('ID' => $id),
41 41
       array('%d')
42
-   );
42
+    );
43 43
   }
44 44
 
45
-  public static function record_count() {
45
+  public function record_count() {
46 46
     global $wpdb;
47 47
 
48 48
     $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}customers";
... ...
@@ -62,7 +62,7 @@ class admin_list_table extends WP_List_Table {
62 62
   function column_cb($item) {
63 63
     return sprintf(
64 64
       '<input type="checkbox" name="bulk-delete[]" value="%s" />', $item['ID']
65
-   );
65
+    );
66 66
   }
67 67
 
68 68
   function column_name($item) {
... ...
@@ -84,7 +84,7 @@ class admin_list_table extends WP_List_Table {
84 84
       'name'    => 'Name',
85 85
       'address' => 'Address',
86 86
       'city'    => 'City'
87
-   );
87
+    );
88 88
 
89 89
     return $columns;
90 90
   }
... ...
@@ -93,7 +93,7 @@ class admin_list_table extends WP_List_Table {
93 93
     $sortable_columns = array(
94 94
       'name' => array('name', true),
95 95
       'city' => array('city', true)
96
-   );
96
+    );
97 97
 
98 98
     return $sortable_columns;
99 99
   }
... ...
@@ -107,21 +107,39 @@ class admin_list_table extends WP_List_Table {
107 107
   public function prepare_items() {
108 108
     $this->_column_headers = $this->get_column_info();
109 109
 
110
-    /** Process bulk action */
111 110
     $this->process_bulk_action();
112 111
 
113 112
     $per_page     = $this->get_items_per_page('customers_per_page', 5);
114 113
     $current_page = $this->get_pagenum();
115
-    $total_items  = self::record_count();
114
+    $total_items  = $this->record_count();
116 115
 
117 116
     $this->set_pagination_args(array(
118 117
       'total_items' => $total_items, //WE have to calculate the total number of items
119 118
       'per_page'    => $per_page //WE have to determine how many items to show on a page
120 119
     ));
121 120
 
122
-    $this->items = self::get_customers($per_page, $current_page);
121
+    $this->items = $this->get_customers($per_page, $current_page);
123 122
   }
124 123
 
125 124
   public function process_bulk_action() {
125
+    if ('delete' === $this->current_action()) {
126
+      $nonce = esc_attr($_REQUEST['_wpnonce']);
127
+
128
+      if (! wp_verify_nonce($nonce, 'delete_customer')) {
129
+        die('Go get a life script kiddies');
130
+      }
131
+      else {
132
+        $this->delete_customer(absint($_GET['customer']));
133
+      }
134
+    }
135
+
136
+    if ((isset($_POST['action']) && $_POST['action'] == 'bulk-delete')
137
+      || (isset($_POST['action2']) && $_POST['action2'] == 'bulk-delete')) {
138
+      $delete_ids = esc_sql($_POST['bulk-delete']);
139
+
140
+      foreach ($delete_ids as $id) {
141
+        $this->delete_customer($id);
142
+      }
143
+    }
126 144
   }
127 145
 }
Browse code

테이블 기능 구현

Nepirity Corp authored on23/03/2018 15:55:13
Showing1 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
+}