Passed
Push — master ( 9ee829...6e3436 )
by Brian
04:40
created
includes/admin/class-wpinv-customers-table.php 1 patch
Indentation   +366 added lines, -366 removed lines patch added patch discarded remove patch
@@ -11,7 +11,7 @@  discard block
 block discarded – undo
11 11
 
12 12
 // Load WP_List_Table if not loaded
13 13
 if ( ! class_exists( 'WP_List_Table' ) ) {
14
-	require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
14
+    require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
15 15
 }
16 16
 
17 17
 /**
@@ -23,373 +23,373 @@  discard block
 block discarded – undo
23 23
  */
24 24
 class WPInv_Customers_Table extends WP_List_Table {
25 25
 
26
-	/**
27
-	 * @var int Number of items per page
28
-	 * @since 1.0.19
29
-	 */
30
-	public $per_page = 10;
31
-
32
-	/**
33
-	 * @var int Number of items
34
-	 * @since 1.0.19
35
-	 */
36
-	public $total = 0;
37
-
38
-	/**
39
-	 * Get things started
40
-	 *
41
-	 * @since 1.0.19
42
-	 * @see WP_List_Table::__construct()
43
-	 */
44
-	public function __construct() {
45
-
46
-		// Set parent defaults
47
-		parent::__construct(
26
+    /**
27
+     * @var int Number of items per page
28
+     * @since 1.0.19
29
+     */
30
+    public $per_page = 10;
31
+
32
+    /**
33
+     * @var int Number of items
34
+     * @since 1.0.19
35
+     */
36
+    public $total = 0;
37
+
38
+    /**
39
+     * Get things started
40
+     *
41
+     * @since 1.0.19
42
+     * @see WP_List_Table::__construct()
43
+     */
44
+    public function __construct() {
45
+
46
+        // Set parent defaults
47
+        parent::__construct(
48 48
             array(
49
-				'singular' => 'id',
50
-				'plural'   => 'ids',
51
-				'ajax'     => false,
49
+                'singular' => 'id',
50
+                'plural'   => 'ids',
51
+                'ajax'     => false,
52 52
             )
53 53
         );
54 54
 
55
-	}
56
-
57
-	/**
58
-	 * Gets the name of the primary column.
59
-	 *
60
-	 * @since 1.0.19
61
-	 * @access protected
62
-	 *
63
-	 * @return string Name of the primary column.
64
-	 */
65
-	protected function get_primary_column_name() {
66
-		return 'name';
67
-	}
68
-
69
-	/**
70
-	 * This function renders most of the columns in the list table.
71
-	 *
72
-	 * @since 1.0.19
73
-	 *
74
-	 * @param WP_User $item
75
-	 * @param string $column_name The name of the column
76
-	 *
77
-	 * @return string Column Name
78
-	 */
79
-	public function column_default( $item, $column_name ) {
80
-		$value = esc_html( get_user_meta( $item->ID, '_wpinv_' . $column_name, true ) );
81
-		return apply_filters( 'wpinv_customers_table_column' . $column_name, $value, $item );
82
-	}
83
-
84
-	/**
85
-	 * Displays the country column.
86
-	 *
87
-	 * @since 1.0.19
88
-	 *
89
-	 * @param WP_User $user
90
-	 *
91
-	 * @return string Column Name
92
-	 */
93
-	public function column_country( $user ) {
94
-		$country = wpinv_sanitize_country( $user->_wpinv_country );
95
-		if ( $country ) {
96
-			$country = wpinv_country_name( $country );
97
-		}
98
-		return esc_html( $country );
99
-	}
100
-
101
-	/**
102
-	 * Displays the state column.
103
-	 *
104
-	 * @since 1.0.19
105
-	 *
106
-	 * @param WP_User $user
107
-	 *
108
-	 * @return string Column Name
109
-	 */
110
-	public function column_state( $user ) {
111
-		$country = wpinv_sanitize_country( $user->_wpinv_country );
112
-		$state   = $user->_wpinv_state;
113
-		if ( $state ) {
114
-			$state = wpinv_state_name( $state, $country );
115
-		}
116
-
117
-		return esc_html( $state );
118
-	}
119
-
120
-	/**
121
-	 * Displays the signup column.
122
-	 *
123
-	 * @since 1.0.19
124
-	 *
125
-	 * @param WP_User $user
126
-	 *
127
-	 * @return string Column Name
128
-	 */
129
-	public function column_signup( $user ) {
130
-		return getpaid_format_date_value( $user->user_registered );
131
-	}
132
-
133
-	/**
134
-	 * Displays the total spent column.
135
-	 *
136
-	 * @since 1.0.19
137
-	 *
138
-	 * @param WP_User $user
139
-	 *
140
-	 * @return string Column Name
141
-	 */
142
-	public function column_total( $user ) {
143
-		return wpinv_price( $this->column_total_raw( $user ) );
144
-	}
145
-
146
-	/**
147
-	 * Displays the total spent column.
148
-	 *
149
-	 * @since 1.0.19
150
-	 *
151
-	 * @param WP_User $user
152
-	 *
153
-	 * @return float
154
-	 */
155
-	public function column_total_raw( $user ) {
156
-
157
-		$args = array(
158
-			'data'           => array(
159
-
160
-				'total' => array(
161
-					'type'     => 'invoice_data',
162
-					'function' => 'SUM',
163
-					'name'     => 'total_sales',
164
-				),
165
-
166
-			),
167
-			'where'          => array(
168
-
169
-				'author' => array(
170
-					'type'     => 'post_data',
171
-					'value'    => absint( $user->ID ),
172
-					'key'      => 'posts.post_author',
173
-					'operator' => '=',
174
-				),
175
-
176
-			),
177
-			'query_type'     => 'get_var',
178
-			'invoice_status' => array( 'wpi-renewal', 'wpi-processing', 'publish' ),
179
-		);
180
-
181
-		return wpinv_round_amount( GetPaid_Reports_Helper::get_invoice_report_data( $args ) );
182
-
183
-	}
184
-
185
-	/**
186
-	 * Displays the total spent column.
187
-	 *
188
-	 * @since 1.0.19
189
-	 *
190
-	 * @param WP_User $user
191
-	 *
192
-	 * @return string Column Name
193
-	 */
194
-	public function column_invoices( $user ) {
195
-
196
-		$args = array(
197
-			'data'           => array(
198
-
199
-				'ID' => array(
200
-					'type'     => 'post_data',
201
-					'function' => 'COUNT',
202
-					'name'     => 'count',
203
-					'distinct' => true,
204
-				),
205
-
206
-			),
207
-			'where'          => array(
208
-
209
-				'author' => array(
210
-					'type'     => 'post_data',
211
-					'value'    => absint( $user->ID ),
212
-					'key'      => 'posts.post_author',
213
-					'operator' => '=',
214
-				),
215
-
216
-			),
217
-			'query_type'     => 'get_var',
218
-			'invoice_status' => array_keys( wpinv_get_invoice_statuses() ),
219
-		);
220
-
221
-		return absint( GetPaid_Reports_Helper::get_invoice_report_data( $args ) );
222
-
223
-	}
224
-
225
-	/**
226
-	 * Generates content for a single row of the table
227
-	 * @since 1.0.19
228
-	 *
229
-	 * @param int $item The user id.
230
-	 */
231
-	public function single_row( $item ) {
232
-		$item = get_user_by( 'id', $item );
233
-
234
-		if ( empty( $item ) ) {
235
-			return;
236
-		}
237
-
238
-		echo '<tr>';
239
-		$this->single_row_columns( $item );
240
-		echo '</tr>';
241
-	}
242
-
243
-	/**
244
-	 * Displays the customers name
245
-	 *
246
-	 * @param  WP_User $customer customer.
247
-	 * @return string
248
-	 */
249
-	public function column_name( $customer ) {
250
-
251
-		// Customer view URL.
252
-		$view_url    = esc_url( add_query_arg( 'user_id', $customer->ID, admin_url( 'user-edit.php' ) ) );
253
-		$row_actions = $this->row_actions(
254
-			array(
255
-				'view' => '<a href="' . $view_url . '#getpaid-fieldset-billing">' . __( 'Edit Details', 'invoicing' ) . '</a>',
256
-			)
257
-		);
258
-
259
-		// Get user's address.
260
-		$address = wpinv_get_user_address( $customer->ID );
261
-
262
-		// Customer email address.
263
-		$email       = sanitize_email( $customer->user_email );
264
-
265
-		// Customer's avatar.
266
-		$avatar = esc_url( get_avatar_url( $email ) );
267
-		$avatar = "<img src='$avatar' height='32' width='32'/>";
268
-
269
-		// Customer's name.
270
-		$name   = esc_html( "{$address['first_name']} {$address['last_name']}" );
271
-
272
-		if ( empty( trim( $name ) ) ) {
273
-			$name = esc_html( $address['display_name'] );
274
-		}
275
-
276
-		if ( ! empty( $name ) ) {
277
-			$name = "<div style='overflow: hidden;height: 18px;'>$name</div>";
278
-		}
279
-
280
-		$email = "<div class='row-title'><a href='$view_url'>$email</a></div>";
281
-
282
-		return "<div style='display: flex;'><div>$avatar</div><div style='margin-left: 10px;'>$name<strong>$email</strong>$row_actions</div></div>";
283
-
284
-	}
285
-
286
-	/**
287
-	 * Retrieve the table columns
288
-	 *
289
-	 * @since 1.0.19
290
-	 * @return array $columns Array of all the list table columns
291
-	 */
292
-	public function get_columns() {
293
-
294
-		$columns = array(
295
-			'name'     => __( 'Name', 'invoicing' ),
296
-			'country'  => __( 'Country', 'invoicing' ),
297
-			'state'    => __( 'State', 'invoicing' ),
298
-			'city'     => __( 'City', 'invoicing' ),
299
-			'zip'      => __( 'ZIP', 'invoicing' ),
300
-			'address'  => __( 'Address', 'invoicing' ),
301
-			'phone'    => __( 'Phone', 'invoicing' ),
302
-			'company'  => __( 'Company', 'invoicing' ),
303
-			'invoices' => __( 'Invoices', 'invoicing' ),
304
-			'total'    => __( 'Total Spend', 'invoicing' ),
305
-			'signup'   => __( 'Date created', 'invoicing' ),
306
-		);
307
-		return apply_filters( 'wpinv_customers_table_columns', $columns );
308
-
309
-	}
310
-
311
-	/**
312
-	 * Retrieve the current page number
313
-	 *
314
-	 * @since 1.0.19
315
-	 * @return int Current page number
316
-	 */
317
-	public function get_paged() {
318
-		return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
319
-	}
320
-
321
-	/**
322
-	 * Returns bulk actions.
323
-	 *
324
-	 * @since 1.0.19
325
-	 * @return void
326
-	 */
327
-	public function bulk_actions( $which = '' ) {
328
-		return array();
329
-	}
330
-
331
-	/**
332
-	 *  Prepares the display query
333
-	 */
334
-	public function prepare_query() {
335
-		global $wpdb;
336
-
337
-		$post_types = '';
338
-
339
-		foreach ( array_keys( getpaid_get_invoice_post_types() ) as $post_type ) {
340
-			$post_types .= $wpdb->prepare( 'post_type=%s OR ', $post_type );
341
-		}
342
-
343
-		$post_types = rtrim( $post_types, ' OR' );
344
-
345
-		// Maybe search.
346
-		if ( ! empty( $_POST['s'] ) ) {
347
-			$users = get_users(
348
-				array(
349
-					'search'         => '*' . sanitize_text_field( urldecode( $_POST['s'] ) ) . '*',
350
-					'search_columns' => array( 'user_login', 'user_email', 'display_name' ),
351
-					'fields'         => 'ID',
352
-				)
353
-			);
354
-
355
-			$users      = implode( ', ', $users );
356
-			$post_types = "($post_types) AND ( post_author IN ( $users ) )";
357
-		}
358
-
359
-		// Users with invoices.
360
-    	$customers = $wpdb->get_col(
361
-			$wpdb->prepare(
362
-				"SELECT DISTINCT( post_author ) FROM $wpdb->posts WHERE $post_types LIMIT %d,%d",
363
-				$this->get_paged() * 10 - 10,
364
-				$this->per_page
365
-			)
366
-		);
367
-
368
-		$this->items = $customers;
369
-		$this->total = (int) $wpdb->get_var( "SELECT COUNT( DISTINCT( post_author ) ) FROM $wpdb->posts WHERE $post_types" );
370
-
371
-	}
372
-
373
-	/**
374
-	 * Setup the final data for the table
375
-	 *
376
-	 * @since 1.0.19
377
-	 * @return void
378
-	 */
379
-	public function prepare_items() {
380
-		$columns               = $this->get_columns();
381
-		$hidden                = array(); // No hidden columns
382
-		$sortable              = $this->get_sortable_columns();
383
-		$this->_column_headers = array( $columns, $hidden, $sortable );
384
-		$this->prepare_query();
385
-
386
-		$this->set_pagination_args(
387
-			array(
388
-				'total_items' => $this->total,
389
-				'per_page'    => $this->per_page,
390
-				'total_pages' => ceil( $this->total / $this->per_page ),
391
-			)
392
-		);
393
-
394
-	}
55
+    }
56
+
57
+    /**
58
+     * Gets the name of the primary column.
59
+     *
60
+     * @since 1.0.19
61
+     * @access protected
62
+     *
63
+     * @return string Name of the primary column.
64
+     */
65
+    protected function get_primary_column_name() {
66
+        return 'name';
67
+    }
68
+
69
+    /**
70
+     * This function renders most of the columns in the list table.
71
+     *
72
+     * @since 1.0.19
73
+     *
74
+     * @param WP_User $item
75
+     * @param string $column_name The name of the column
76
+     *
77
+     * @return string Column Name
78
+     */
79
+    public function column_default( $item, $column_name ) {
80
+        $value = esc_html( get_user_meta( $item->ID, '_wpinv_' . $column_name, true ) );
81
+        return apply_filters( 'wpinv_customers_table_column' . $column_name, $value, $item );
82
+    }
83
+
84
+    /**
85
+     * Displays the country column.
86
+     *
87
+     * @since 1.0.19
88
+     *
89
+     * @param WP_User $user
90
+     *
91
+     * @return string Column Name
92
+     */
93
+    public function column_country( $user ) {
94
+        $country = wpinv_sanitize_country( $user->_wpinv_country );
95
+        if ( $country ) {
96
+            $country = wpinv_country_name( $country );
97
+        }
98
+        return esc_html( $country );
99
+    }
100
+
101
+    /**
102
+     * Displays the state column.
103
+     *
104
+     * @since 1.0.19
105
+     *
106
+     * @param WP_User $user
107
+     *
108
+     * @return string Column Name
109
+     */
110
+    public function column_state( $user ) {
111
+        $country = wpinv_sanitize_country( $user->_wpinv_country );
112
+        $state   = $user->_wpinv_state;
113
+        if ( $state ) {
114
+            $state = wpinv_state_name( $state, $country );
115
+        }
116
+
117
+        return esc_html( $state );
118
+    }
119
+
120
+    /**
121
+     * Displays the signup column.
122
+     *
123
+     * @since 1.0.19
124
+     *
125
+     * @param WP_User $user
126
+     *
127
+     * @return string Column Name
128
+     */
129
+    public function column_signup( $user ) {
130
+        return getpaid_format_date_value( $user->user_registered );
131
+    }
132
+
133
+    /**
134
+     * Displays the total spent column.
135
+     *
136
+     * @since 1.0.19
137
+     *
138
+     * @param WP_User $user
139
+     *
140
+     * @return string Column Name
141
+     */
142
+    public function column_total( $user ) {
143
+        return wpinv_price( $this->column_total_raw( $user ) );
144
+    }
145
+
146
+    /**
147
+     * Displays the total spent column.
148
+     *
149
+     * @since 1.0.19
150
+     *
151
+     * @param WP_User $user
152
+     *
153
+     * @return float
154
+     */
155
+    public function column_total_raw( $user ) {
156
+
157
+        $args = array(
158
+            'data'           => array(
159
+
160
+                'total' => array(
161
+                    'type'     => 'invoice_data',
162
+                    'function' => 'SUM',
163
+                    'name'     => 'total_sales',
164
+                ),
165
+
166
+            ),
167
+            'where'          => array(
168
+
169
+                'author' => array(
170
+                    'type'     => 'post_data',
171
+                    'value'    => absint( $user->ID ),
172
+                    'key'      => 'posts.post_author',
173
+                    'operator' => '=',
174
+                ),
175
+
176
+            ),
177
+            'query_type'     => 'get_var',
178
+            'invoice_status' => array( 'wpi-renewal', 'wpi-processing', 'publish' ),
179
+        );
180
+
181
+        return wpinv_round_amount( GetPaid_Reports_Helper::get_invoice_report_data( $args ) );
182
+
183
+    }
184
+
185
+    /**
186
+     * Displays the total spent column.
187
+     *
188
+     * @since 1.0.19
189
+     *
190
+     * @param WP_User $user
191
+     *
192
+     * @return string Column Name
193
+     */
194
+    public function column_invoices( $user ) {
195
+
196
+        $args = array(
197
+            'data'           => array(
198
+
199
+                'ID' => array(
200
+                    'type'     => 'post_data',
201
+                    'function' => 'COUNT',
202
+                    'name'     => 'count',
203
+                    'distinct' => true,
204
+                ),
205
+
206
+            ),
207
+            'where'          => array(
208
+
209
+                'author' => array(
210
+                    'type'     => 'post_data',
211
+                    'value'    => absint( $user->ID ),
212
+                    'key'      => 'posts.post_author',
213
+                    'operator' => '=',
214
+                ),
215
+
216
+            ),
217
+            'query_type'     => 'get_var',
218
+            'invoice_status' => array_keys( wpinv_get_invoice_statuses() ),
219
+        );
220
+
221
+        return absint( GetPaid_Reports_Helper::get_invoice_report_data( $args ) );
222
+
223
+    }
224
+
225
+    /**
226
+     * Generates content for a single row of the table
227
+     * @since 1.0.19
228
+     *
229
+     * @param int $item The user id.
230
+     */
231
+    public function single_row( $item ) {
232
+        $item = get_user_by( 'id', $item );
233
+
234
+        if ( empty( $item ) ) {
235
+            return;
236
+        }
237
+
238
+        echo '<tr>';
239
+        $this->single_row_columns( $item );
240
+        echo '</tr>';
241
+    }
242
+
243
+    /**
244
+     * Displays the customers name
245
+     *
246
+     * @param  WP_User $customer customer.
247
+     * @return string
248
+     */
249
+    public function column_name( $customer ) {
250
+
251
+        // Customer view URL.
252
+        $view_url    = esc_url( add_query_arg( 'user_id', $customer->ID, admin_url( 'user-edit.php' ) ) );
253
+        $row_actions = $this->row_actions(
254
+            array(
255
+                'view' => '<a href="' . $view_url . '#getpaid-fieldset-billing">' . __( 'Edit Details', 'invoicing' ) . '</a>',
256
+            )
257
+        );
258
+
259
+        // Get user's address.
260
+        $address = wpinv_get_user_address( $customer->ID );
261
+
262
+        // Customer email address.
263
+        $email       = sanitize_email( $customer->user_email );
264
+
265
+        // Customer's avatar.
266
+        $avatar = esc_url( get_avatar_url( $email ) );
267
+        $avatar = "<img src='$avatar' height='32' width='32'/>";
268
+
269
+        // Customer's name.
270
+        $name   = esc_html( "{$address['first_name']} {$address['last_name']}" );
271
+
272
+        if ( empty( trim( $name ) ) ) {
273
+            $name = esc_html( $address['display_name'] );
274
+        }
275
+
276
+        if ( ! empty( $name ) ) {
277
+            $name = "<div style='overflow: hidden;height: 18px;'>$name</div>";
278
+        }
279
+
280
+        $email = "<div class='row-title'><a href='$view_url'>$email</a></div>";
281
+
282
+        return "<div style='display: flex;'><div>$avatar</div><div style='margin-left: 10px;'>$name<strong>$email</strong>$row_actions</div></div>";
283
+
284
+    }
285
+
286
+    /**
287
+     * Retrieve the table columns
288
+     *
289
+     * @since 1.0.19
290
+     * @return array $columns Array of all the list table columns
291
+     */
292
+    public function get_columns() {
293
+
294
+        $columns = array(
295
+            'name'     => __( 'Name', 'invoicing' ),
296
+            'country'  => __( 'Country', 'invoicing' ),
297
+            'state'    => __( 'State', 'invoicing' ),
298
+            'city'     => __( 'City', 'invoicing' ),
299
+            'zip'      => __( 'ZIP', 'invoicing' ),
300
+            'address'  => __( 'Address', 'invoicing' ),
301
+            'phone'    => __( 'Phone', 'invoicing' ),
302
+            'company'  => __( 'Company', 'invoicing' ),
303
+            'invoices' => __( 'Invoices', 'invoicing' ),
304
+            'total'    => __( 'Total Spend', 'invoicing' ),
305
+            'signup'   => __( 'Date created', 'invoicing' ),
306
+        );
307
+        return apply_filters( 'wpinv_customers_table_columns', $columns );
308
+
309
+    }
310
+
311
+    /**
312
+     * Retrieve the current page number
313
+     *
314
+     * @since 1.0.19
315
+     * @return int Current page number
316
+     */
317
+    public function get_paged() {
318
+        return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
319
+    }
320
+
321
+    /**
322
+     * Returns bulk actions.
323
+     *
324
+     * @since 1.0.19
325
+     * @return void
326
+     */
327
+    public function bulk_actions( $which = '' ) {
328
+        return array();
329
+    }
330
+
331
+    /**
332
+     *  Prepares the display query
333
+     */
334
+    public function prepare_query() {
335
+        global $wpdb;
336
+
337
+        $post_types = '';
338
+
339
+        foreach ( array_keys( getpaid_get_invoice_post_types() ) as $post_type ) {
340
+            $post_types .= $wpdb->prepare( 'post_type=%s OR ', $post_type );
341
+        }
342
+
343
+        $post_types = rtrim( $post_types, ' OR' );
344
+
345
+        // Maybe search.
346
+        if ( ! empty( $_POST['s'] ) ) {
347
+            $users = get_users(
348
+                array(
349
+                    'search'         => '*' . sanitize_text_field( urldecode( $_POST['s'] ) ) . '*',
350
+                    'search_columns' => array( 'user_login', 'user_email', 'display_name' ),
351
+                    'fields'         => 'ID',
352
+                )
353
+            );
354
+
355
+            $users      = implode( ', ', $users );
356
+            $post_types = "($post_types) AND ( post_author IN ( $users ) )";
357
+        }
358
+
359
+        // Users with invoices.
360
+        $customers = $wpdb->get_col(
361
+            $wpdb->prepare(
362
+                "SELECT DISTINCT( post_author ) FROM $wpdb->posts WHERE $post_types LIMIT %d,%d",
363
+                $this->get_paged() * 10 - 10,
364
+                $this->per_page
365
+            )
366
+        );
367
+
368
+        $this->items = $customers;
369
+        $this->total = (int) $wpdb->get_var( "SELECT COUNT( DISTINCT( post_author ) ) FROM $wpdb->posts WHERE $post_types" );
370
+
371
+    }
372
+
373
+    /**
374
+     * Setup the final data for the table
375
+     *
376
+     * @since 1.0.19
377
+     * @return void
378
+     */
379
+    public function prepare_items() {
380
+        $columns               = $this->get_columns();
381
+        $hidden                = array(); // No hidden columns
382
+        $sortable              = $this->get_sortable_columns();
383
+        $this->_column_headers = array( $columns, $hidden, $sortable );
384
+        $this->prepare_query();
385
+
386
+        $this->set_pagination_args(
387
+            array(
388
+                'total_items' => $this->total,
389
+                'per_page'    => $this->per_page,
390
+                'total_pages' => ceil( $this->total / $this->per_page ),
391
+            )
392
+        );
393
+
394
+    }
395 395
 }
Please login to merge, or discard this patch.