| 1 |
<?php |
| 2 |
/** |
| 3 |
* Donor Reports Table Class |
| 4 |
* |
| 5 |
* @package Give |
| 6 |
* @subpackage Admin/Reports |
| 7 |
* @copyright Copyright (c) 2016, GiveWP |
| 8 |
* @license https://opensource.org/licenses/gpl-license GNU Public License |
| 9 |
* @since 1.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
// Load WP_List_Table if not loaded |
| 18 |
if ( ! class_exists( 'WP_List_Table' ) ) { |
| 19 |
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Give_Donor_Reports_Table Class |
| 24 |
* |
| 25 |
* Renders the Donor Reports table |
| 26 |
* |
| 27 |
* @since 1.0 |
| 28 |
*/ |
| 29 |
class Give_Donor_Reports_Table extends WP_List_Table { |
| 30 |
|
| 31 |
/** |
| 32 |
* Number of items per page |
| 33 |
* |
| 34 |
* @var int |
| 35 |
* @since 1.0 |
| 36 |
*/ |
| 37 |
public $per_page = 30; |
| 38 |
|
| 39 |
/** |
| 40 |
* Number of donors found |
| 41 |
* |
| 42 |
* @var int |
| 43 |
* @since 1.0 |
| 44 |
*/ |
| 45 |
public $count = 0; |
| 46 |
|
| 47 |
/** |
| 48 |
* Total donors |
| 49 |
* |
| 50 |
* @var int |
| 51 |
* @since 1.0 |
| 52 |
*/ |
| 53 |
public $total = 0; |
| 54 |
|
| 55 |
/** |
| 56 |
* Get things started |
| 57 |
* |
| 58 |
* @since 1.0 |
| 59 |
* @see WP_List_Table::__construct() |
| 60 |
*/ |
| 61 |
public function __construct() { |
| 62 |
global $status, $page; |
| 63 |
|
| 64 |
// Set parent defaults |
| 65 |
parent::__construct( |
| 66 |
array( |
| 67 |
'singular' => esc_html__( 'Donor', 'give' ), // Singular name of the listed records |
| 68 |
'plural' => esc_html__( 'Donors', 'give' ), // Plural name of the listed records |
| 69 |
'ajax' => false, // Does this table support ajax? |
| 70 |
) |
| 71 |
); |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Remove default search field in favor for repositioned location |
| 77 |
* |
| 78 |
* Reposition the search field |
| 79 |
* |
| 80 |
* @since 1.0 |
| 81 |
* @access public |
| 82 |
* |
| 83 |
* @param string $text Label for the search box |
| 84 |
* @param string $input_id ID of the search box |
| 85 |
* |
| 86 |
* @return false |
| 87 |
*/ |
| 88 |
public function search_box( $text, $input_id ) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Show the search field |
| 94 |
* |
| 95 |
* @since 1.0 |
| 96 |
* @access public |
| 97 |
* |
| 98 |
* @param string $text Label for the search box |
| 99 |
* @param string $input_id ID of the search box |
| 100 |
* |
| 101 |
* @return void |
| 102 |
*/ |
| 103 |
public function give_search_box( $text, $input_id ) { |
| 104 |
$input_id = $input_id . '-search-input'; |
| 105 |
|
| 106 |
if ( ! empty( $_REQUEST['orderby'] ) ) { |
| 107 |
echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />'; |
| 108 |
} |
| 109 |
if ( ! empty( $_REQUEST['order'] ) ) { |
| 110 |
echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />'; |
| 111 |
} |
| 112 |
?> |
| 113 |
<p class="search-box donor-search" role="search"> |
| 114 |
<label class="screen-reader-text" for="<?php echo $input_id; ?>"><?php echo $text; ?>:</label> |
| 115 |
<input type="search" id="<?php echo $input_id; ?>" name="s" value="<?php _admin_search_query(); ?>" /> |
| 116 |
<?php submit_button( $text, 'button', false, false, array( 'ID' => 'search-submit' ) ); ?> |
| 117 |
</p> |
| 118 |
<?php |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Generate the table navigation above or below the table |
| 123 |
* |
| 124 |
* @since 1.0 |
| 125 |
* @access protected |
| 126 |
* |
| 127 |
* @param string $which |
| 128 |
*/ |
| 129 |
protected function display_tablenav( $which ) { |
| 130 |
|
| 131 |
if ( 'top' === $which ) { |
| 132 |
wp_nonce_field( 'bulk-' . $this->_args['plural'] ); |
| 133 |
} |
| 134 |
?> |
| 135 |
<div class="tablenav give-clearfix <?php echo esc_attr( $which ); ?>"> |
| 136 |
|
| 137 |
<?php if ( 'top' === $which ) { ?> |
| 138 |
<h2 class="alignleft reports-earnings-title"> |
| 139 |
<?php esc_html_e( 'Donors Report', 'give' ); ?> |
| 140 |
</h2> |
| 141 |
<?php } ?> |
| 142 |
|
| 143 |
<div class="alignright tablenav-right"> |
| 144 |
<div class="actions bulkactions"> |
| 145 |
<?php |
| 146 |
if ( 'top' === $which ) { |
| 147 |
$this->give_search_box( esc_html__( 'Search Donors', 'give' ), 'give-donors-report-search' ); |
| 148 |
} |
| 149 |
|
| 150 |
$this->bulk_actions( $which ); |
| 151 |
?> |
| 152 |
|
| 153 |
</div> |
| 154 |
<?php |
| 155 |
$this->extra_tablenav( $which ); |
| 156 |
$this->pagination( $which ); |
| 157 |
?> |
| 158 |
</div> |
| 159 |
|
| 160 |
|
| 161 |
<br class="clear"/> |
| 162 |
|
| 163 |
</div> |
| 164 |
<?php |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* This function renders most of the columns in the list table. |
| 169 |
* |
| 170 |
* @access public |
| 171 |
* @since 1.0 |
| 172 |
* |
| 173 |
* @param array $item Contains all the data of the donors |
| 174 |
* @param string $column_name The name of the column |
| 175 |
* |
| 176 |
* @return string Column Name |
| 177 |
*/ |
| 178 |
public function column_default( $item, $column_name ) { |
| 179 |
|
| 180 |
switch ( $column_name ) { |
| 181 |
|
| 182 |
case 'name': |
| 183 |
$name = '#' . $item['id'] . ' '; |
| 184 |
$name .= ! empty( $item['name'] ) ? $item['name'] : '<em>' . esc_html__( 'Unnamed Donor', 'give' ) . '</em>'; |
| 185 |
$view_url = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $item['id'] ); |
| 186 |
$value = '<a href="' . esc_url( $view_url ) . '">' . $name . '</a>'; |
| 187 |
break; |
| 188 |
|
| 189 |
case 'num_donations': |
| 190 |
$value = sprintf( |
| 191 |
'<a href="%s">%s</a>', |
| 192 |
admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&donor=' . absint( $item['id'] ) ), |
| 193 |
esc_html( $item['num_donations'] ) |
| 194 |
); |
| 195 |
break; |
| 196 |
|
| 197 |
case 'amount_spent': |
| 198 |
$value = give_currency_filter( give_format_amount( $item[ $column_name ], array( 'sanitize' => false ) ) ); |
| 199 |
break; |
| 200 |
|
| 201 |
default: |
| 202 |
$value = isset( $item[ $column_name ] ) ? $item[ $column_name ] : null; |
| 203 |
break; |
| 204 |
} |
| 205 |
|
| 206 |
return apply_filters( "give_report_column_{$column_name}", $value, $item['id'] ); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Retrieve the table columns |
| 211 |
* |
| 212 |
* @access public |
| 213 |
* @since 1.0 |
| 214 |
* @return array $columns Array of all the list table columns |
| 215 |
*/ |
| 216 |
public function get_columns() { |
| 217 |
$columns = array( |
| 218 |
'name' => __( 'Name', 'give' ), |
| 219 |
'email' => __( 'Email', 'give' ), |
| 220 |
'num_donations' => __( 'Donations', 'give' ), |
| 221 |
'amount_spent' => __( 'Total Donated', 'give' ), |
| 222 |
); |
| 223 |
|
| 224 |
return apply_filters( 'give_report_donor_columns', $columns ); |
| 225 |
|
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Get the sortable columns |
| 230 |
* |
| 231 |
* @access public |
| 232 |
* @since 1.0 |
| 233 |
* @return array Array of all the sortable columns |
| 234 |
*/ |
| 235 |
public function get_sortable_columns() { |
| 236 |
return array( |
| 237 |
'id' => array( 'id', true ), |
| 238 |
'name' => array( 'name', true ), |
| 239 |
'num_donations' => array( 'purchase_count', false ), |
| 240 |
'amount_spent' => array( 'purchase_value', false ), |
| 241 |
); |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Outputs the reporting views |
| 246 |
* |
| 247 |
* @access public |
| 248 |
* @since 1.0 |
| 249 |
* @return void |
| 250 |
*/ |
| 251 |
public function bulk_actions( $which = '' ) { |
| 252 |
|
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Retrieve the current page number |
| 257 |
* |
| 258 |
* @access public |
| 259 |
* @since 1.0 |
| 260 |
* @return int Current page number |
| 261 |
*/ |
| 262 |
public function get_paged() { |
| 263 |
return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Retrieves the search query string |
| 268 |
* |
| 269 |
* @access public |
| 270 |
* @since 1.0 |
| 271 |
* @return mixed string If search is present, false otherwise |
| 272 |
*/ |
| 273 |
public function get_search() { |
| 274 |
return ! empty( $_GET['s'] ) ? urldecode( trim( $_GET['s'] ) ) : false; |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Build all the reports data |
| 279 |
* |
| 280 |
* @access public |
| 281 |
* @since 1.0 |
| 282 |
* @global object $wpdb Used to query the database using the WordPress |
| 283 |
* Database API |
| 284 |
* @return array $reports_data All the data for donor reports |
| 285 |
*/ |
| 286 |
public function reports_data() { |
| 287 |
global $wpdb; |
| 288 |
|
| 289 |
$data = array(); |
| 290 |
|
| 291 |
// Get donor query. |
| 292 |
$args = $this->get_donor_query(); |
| 293 |
$donors = Give()->donors->get_donors( $args ); |
| 294 |
|
| 295 |
if ( $donors ) { |
| 296 |
|
| 297 |
$this->count = count( $donors ); |
| 298 |
|
| 299 |
foreach ( $donors as $donor ) { |
| 300 |
|
| 301 |
$user_id = ! empty( $donor->user_id ) ? absint( $donor->user_id ) : 0; |
| 302 |
|
| 303 |
$data[] = array( |
| 304 |
'id' => $donor->id, |
| 305 |
'user_id' => $user_id, |
| 306 |
'name' => $donor->name, |
| 307 |
'email' => $donor->email, |
| 308 |
'num_donations' => $donor->purchase_count, |
| 309 |
'amount_spent' => $donor->purchase_value, |
| 310 |
); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
return $data; |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* Get donor count. |
| 319 |
* |
| 320 |
* @since 1.8.1 |
| 321 |
* @access private |
| 322 |
*/ |
| 323 |
private function get_donor_count() { |
| 324 |
// Get donor query. |
| 325 |
$_donor_query = $this->get_donor_query(); |
| 326 |
|
| 327 |
$_donor_query['number'] = -1; |
| 328 |
$_donor_query['offset'] = 0; |
| 329 |
$donors = Give()->donors->get_donors( $_donor_query ); |
| 330 |
|
| 331 |
return count( $donors ); |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* Get donor query. |
| 336 |
* |
| 337 |
* @since 1.8.1 |
| 338 |
* @access public |
| 339 |
* @return array |
| 340 |
*/ |
| 341 |
public function get_donor_query() { |
| 342 |
$paged = $this->get_paged(); |
| 343 |
$offset = $this->per_page * ( $paged - 1 ); |
| 344 |
$search = $this->get_search(); |
| 345 |
$order = isset( $_GET['order'] ) ? sanitize_text_field( $_GET['order'] ) : 'DESC'; |
| 346 |
$orderby = isset( $_GET['orderby'] ) ? sanitize_text_field( $_GET['orderby'] ) : 'id'; |
| 347 |
|
| 348 |
$args = array( |
| 349 |
'number' => $this->per_page, |
| 350 |
'offset' => $offset, |
| 351 |
'order' => $order, |
| 352 |
'orderby' => $orderby, |
| 353 |
); |
| 354 |
|
| 355 |
if ( $search ) { |
| 356 |
if ( is_email( $search ) ) { |
| 357 |
$args['email'] = $search; |
| 358 |
} elseif ( is_numeric( $search ) ) { |
| 359 |
$args['id'] = $search; |
| 360 |
} else { |
| 361 |
$args['name'] = $search; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
return $args; |
| 366 |
} |
| 367 |
|
| 368 |
/** |
| 369 |
* Setup the final data for the table |
| 370 |
* |
| 371 |
* @access public |
| 372 |
* @since 1.0 |
| 373 |
* @uses Give_Donor_Reports_Table::get_columns() |
| 374 |
* @uses WP_List_Table::get_sortable_columns() |
| 375 |
* @uses Give_Donor_Reports_Table::get_pagenum() |
| 376 |
* @uses Give_Donor_Reports_Table::get_total_donors() |
| 377 |
* @return void |
| 378 |
*/ |
| 379 |
public function prepare_items() { |
| 380 |
|
| 381 |
$columns = $this->get_columns(); |
| 382 |
$hidden = array(); // No hidden columns |
| 383 |
$sortable = $this->get_sortable_columns(); |
| 384 |
|
| 385 |
$this->_column_headers = array( $columns, $hidden, $sortable ); |
| 386 |
|
| 387 |
$this->items = $this->reports_data(); |
| 388 |
|
| 389 |
$this->total = $this->get_donor_count(); |
| 390 |
|
| 391 |
$this->set_pagination_args( |
| 392 |
array( |
| 393 |
'total_items' => $this->total, |
| 394 |
'per_page' => $this->per_page, |
| 395 |
'total_pages' => ceil( $this->total / $this->per_page ), |
| 396 |
) |
| 397 |
); |
| 398 |
} |
| 399 |
} |
| 400 |
|