class-api-requests-logs-list-table.php
7 years ago
class-gateway-error-logs-list-table.php
7 years ago
class-sales-logs-list-table.php
7 years ago
class-update-logs-list-table.php
7 years ago
logs.php
7 years ago
class-sales-logs-list-table.php
425 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Sales Log View Class |
| 4 | * |
| 5 | * @package Give |
| 6 | * @subpackage Admin/Reports |
| 7 | * @copyright Copyright (c) 2016, WordImpress |
| 8 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 9 | */ |
| 10 | |
| 11 | // Exit if accessed directly. |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; |
| 14 | } |
| 15 | |
| 16 | // Load WP_List_Table if not loaded. |
| 17 | if ( ! class_exists( 'WP_List_Table' ) ) { |
| 18 | require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Give_Sales_Log_Table Class |
| 23 | * |
| 24 | * Renders the sales log list table |
| 25 | * |
| 26 | * @since 1.0 |
| 27 | */ |
| 28 | class Give_Sales_Log_Table extends WP_List_Table { |
| 29 | /** |
| 30 | * Number of results to show per page |
| 31 | * |
| 32 | * @since 1.0 |
| 33 | * @var int |
| 34 | */ |
| 35 | public $per_page = 30; |
| 36 | |
| 37 | /** |
| 38 | * Get things started |
| 39 | * |
| 40 | * @since 1.0 |
| 41 | * @see WP_List_Table::__construct() |
| 42 | */ |
| 43 | public function __construct() { |
| 44 | global $status, $page; |
| 45 | |
| 46 | // Set parent defaults |
| 47 | parent::__construct( array( |
| 48 | 'singular' => give_get_forms_label_singular(), // Singular name of the listed records |
| 49 | 'plural' => give_get_forms_label_plural(), // Plural name of the listed records |
| 50 | 'ajax' => false,// Does this table support ajax? |
| 51 | ) ); |
| 52 | |
| 53 | add_action( 'give_log_view_actions', array( $this, 'give_forms_filter' ) ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * This function renders most of the columns in the list table. |
| 58 | * |
| 59 | * @access public |
| 60 | * @since 1.0 |
| 61 | * |
| 62 | * @param array $item Contains all the data of the discount code |
| 63 | * @param string $column_name The name of the column |
| 64 | * |
| 65 | * @return string Column Name |
| 66 | */ |
| 67 | public function column_default( $item, $column_name ) { |
| 68 | |
| 69 | $payment = give_get_payment_by( 'id', $item['payment_id'] ); |
| 70 | |
| 71 | switch ( $column_name ) { |
| 72 | case 'form' : |
| 73 | $form_title = get_the_title( $item[ $column_name ] ); |
| 74 | $form_title = empty( $form_title ) ? sprintf( __( 'Untitled (#%s)', 'give' ), $item[ $column_name ] ) : $form_title; |
| 75 | return '<a href="' . esc_url( add_query_arg( 'form', $item[ $column_name ] ) ) . '" >' . esc_html( $form_title ). '</a>'; |
| 76 | |
| 77 | case 'amount' : |
| 78 | $value = give_currency_filter( give_format_amount( $item['amount'], array( 'sanitize' => false, 'donation_id' => $item['payment_id'] ) ), array( 'currency_code' => give_get_payment_currency_code( $item['payment_id'] ) ) ); |
| 79 | $value .= sprintf( '<br><small>%1$s %2$s</small>', __( 'via', 'give' ), give_get_gateway_admin_label( $payment->gateway ) ); |
| 80 | |
| 81 | return $value; |
| 82 | |
| 83 | case 'status' : |
| 84 | |
| 85 | $value = '<div class="give-donation-status status-' . sanitize_title( give_get_payment_status( $payment, true ) ) . '"><span class="give-donation-status-icon"></span> ' . give_get_payment_status( $payment, true ) . '</div>'; |
| 86 | |
| 87 | if ( $payment->mode == 'test' ) { |
| 88 | $value .= Give()->tooltips->render_span( array( |
| 89 | 'label' => __( 'This donation was made in test mode.', 'give' ), |
| 90 | 'tag_content' => __( 'Test', 'give' ), |
| 91 | 'attributes' => array( |
| 92 | 'class' => 'give-item-label give-item-label-orange give-test-mode-transactions-label', |
| 93 | ), |
| 94 | ) ); |
| 95 | } |
| 96 | |
| 97 | return $value; |
| 98 | |
| 99 | case 'donation' : |
| 100 | $value = Give()->tooltips->render_link( array( |
| 101 | 'label' => sprintf( esc_attr__( 'View Donation #%s', 'give' ), $payment->ID ), |
| 102 | 'tag_content' => "#$payment->ID", |
| 103 | 'link' => esc_url( add_query_arg( 'id', $payment->ID, admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&view=view-payment-details' ) ) ), |
| 104 | ) ); |
| 105 | |
| 106 | if ( ! empty( $item['donor_id'] ) ) { |
| 107 | $value .= sprintf( |
| 108 | ' %1$s <a href="%2$s">%3$s</a><br>', |
| 109 | esc_html__( 'by', 'give' ), |
| 110 | admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&donor=' . $item['donor_id'] ), |
| 111 | $item['donor_name'] |
| 112 | ); |
| 113 | } else { |
| 114 | $value .= sprintf( |
| 115 | ' %1$s %2$s<br>', |
| 116 | esc_html__( 'by', 'give' ), |
| 117 | __( 'No donor attached', 'give' ) |
| 118 | );; |
| 119 | } |
| 120 | |
| 121 | return $value; |
| 122 | |
| 123 | default: |
| 124 | return $item[ $column_name ]; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Retrieve the table columns |
| 130 | * |
| 131 | * @access public |
| 132 | * @since 1.0 |
| 133 | * @return array $columns Array of all the list table columns |
| 134 | */ |
| 135 | public function get_columns() { |
| 136 | $columns = array( |
| 137 | 'ID' => __( 'Log ID', 'give' ), |
| 138 | 'donation' => __( 'Donation', 'give' ), |
| 139 | 'form' => __( 'Form', 'give' ), |
| 140 | 'status' => __( 'Status', 'give' ), |
| 141 | 'amount' => __( 'Donation Amount', 'give' ), |
| 142 | 'date' => __( 'Date', 'give' ), |
| 143 | ); |
| 144 | |
| 145 | return $columns; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Retrieve the current page number |
| 150 | * |
| 151 | * @access public |
| 152 | * @since 1.0 |
| 153 | * @return int Current page number |
| 154 | */ |
| 155 | public function get_paged() { |
| 156 | return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Retrieves the user we are filtering logs by, if any |
| 161 | * |
| 162 | * @access public |
| 163 | * @since 1.0 |
| 164 | * @return mixed int If User ID, string If Email/Login |
| 165 | */ |
| 166 | public function get_filtered_user() { |
| 167 | return isset( $_GET['user'] ) ? absint( $_GET['user'] ) : false; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Retrieves the ID of the give_form we're filtering logs by |
| 172 | * |
| 173 | * @access public |
| 174 | * @since 1.0 |
| 175 | * @return int Download ID |
| 176 | */ |
| 177 | public function get_filtered_give_form() { |
| 178 | return ! empty( $_GET['form'] ) ? absint( $_GET['form'] ) : false; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Retrieves the search query string |
| 183 | * |
| 184 | * @access public |
| 185 | * @since 1.0 |
| 186 | * @return string|bool string If search is present, false otherwise |
| 187 | */ |
| 188 | public function get_search() { |
| 189 | return ! empty( $_GET['s'] ) ? urldecode( trim( $_GET['s'] ) ) : false; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | /** |
| 194 | * Display Tablenav (extended) |
| 195 | * |
| 196 | * Display the table navigation above or below the table even when no items in the logs, so nav doesn't disappear |
| 197 | * |
| 198 | * @see : https://github.com/WordImpress/Give/issues/564 |
| 199 | * |
| 200 | * @since 1.4.1 |
| 201 | * @access protected |
| 202 | * |
| 203 | * @param string $which |
| 204 | */ |
| 205 | protected function display_tablenav( $which ) { |
| 206 | ?> |
| 207 | <div class="tablenav <?php echo esc_attr( $which ); ?>"> |
| 208 | |
| 209 | <?php if ( 'top' === $which ) : ?> |
| 210 | <div class="alignleft actions bulkactions"> |
| 211 | <?php $this->bulk_actions( $which ); ?> |
| 212 | </div> |
| 213 | <?php endif; ?> |
| 214 | |
| 215 | <?php |
| 216 | $this->extra_tablenav( $which ); |
| 217 | $this->pagination( $which ); |
| 218 | ?> |
| 219 | |
| 220 | <br class="clear"/> |
| 221 | </div> |
| 222 | <?php |
| 223 | } |
| 224 | |
| 225 | |
| 226 | /** |
| 227 | * Gets the meta query for the log query |
| 228 | * |
| 229 | * This is used to return log entries that match our search query, user query, or form query |
| 230 | * |
| 231 | * @since 1.0 |
| 232 | * @access public |
| 233 | * |
| 234 | * @return array $meta_query |
| 235 | */ |
| 236 | public function get_meta_query() { |
| 237 | $user = $this->get_filtered_user(); |
| 238 | $give_form = $this->get_filtered_give_form(); |
| 239 | |
| 240 | $meta_query = array(); |
| 241 | |
| 242 | if ( $user ) { |
| 243 | // Show only logs from a specific user. |
| 244 | $meta_query[] = array( |
| 245 | 'key' => '_give_log_user_id', |
| 246 | 'value' => $user, |
| 247 | ); |
| 248 | } |
| 249 | |
| 250 | if ( $give_form ) { |
| 251 | $meta_query[] = array( |
| 252 | 'key' => '_give_log_form_id', |
| 253 | 'value' => $give_form, |
| 254 | ); |
| 255 | } |
| 256 | |
| 257 | $search = $this->get_search(); |
| 258 | if ( $search ) { |
| 259 | if ( is_email( $search ) ) { |
| 260 | // This is an email search. We use this to ensure it works for guest users and logged-in users. |
| 261 | $key = '_give_log_user_info'; |
| 262 | $compare = 'LIKE'; |
| 263 | } else { |
| 264 | // Look for a user |
| 265 | $key = '_give_log_user_id'; |
| 266 | $compare = 'LIKE'; |
| 267 | |
| 268 | if ( ! is_numeric( $search ) ) { |
| 269 | // Searching for user by username |
| 270 | $user = get_user_by( 'login', $search ); |
| 271 | |
| 272 | if ( $user ) { |
| 273 | // Found one, set meta value to user's ID. |
| 274 | $search = $user->ID; |
| 275 | } else { |
| 276 | // No user found so let's do a real search query. |
| 277 | $users = new WP_User_Query( array( |
| 278 | 'search' => $search, |
| 279 | 'search_columns' => array( 'user_url', 'user_nicename' ), |
| 280 | 'number' => 1, |
| 281 | 'fields' => 'ids', |
| 282 | ) ); |
| 283 | |
| 284 | $found_user = $users->get_results(); |
| 285 | |
| 286 | if ( $found_user ) { |
| 287 | $search = $found_user[0]; |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | if ( ! $this->file_search ) { |
| 294 | // Meta query only works for non file name search. |
| 295 | $meta_query[] = array( |
| 296 | 'key' => $key, |
| 297 | 'value' => $search, |
| 298 | 'compare' => $compare, |
| 299 | ); |
| 300 | |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | return $meta_query; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Outputs the log views |
| 309 | * |
| 310 | * @access public |
| 311 | * @since 1.0 |
| 312 | * @param string $which |
| 313 | * @return void |
| 314 | */ |
| 315 | function bulk_actions( $which = '' ) { |
| 316 | give_log_views(); |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Sets up the forms filter |
| 321 | * |
| 322 | * @access public |
| 323 | * @since 1.0 |
| 324 | * @return void |
| 325 | */ |
| 326 | public function give_forms_filter() { |
| 327 | echo Give()->html->forms_dropdown( array( |
| 328 | 'selected' => $this->get_filtered_give_form(), |
| 329 | 'name' => 'form', |
| 330 | 'id' => 'give-log-form-filter', |
| 331 | 'chosen' => true, |
| 332 | ) ); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Gets the log entries for the current view |
| 337 | * |
| 338 | * @access public |
| 339 | * @since 1.0 |
| 340 | * |
| 341 | * @return array $logs_data Array of all the Log entires |
| 342 | */ |
| 343 | public function get_logs() { |
| 344 | $logs_data = array(); |
| 345 | $log_query = $this->get_query_params(); |
| 346 | $logs = Give()->logs->get_connected_logs( $log_query ); |
| 347 | |
| 348 | if ( $logs ) { |
| 349 | foreach ( $logs as $log ) { |
| 350 | /* @var Give_payment $payment */ |
| 351 | $payment = new Give_Payment( $log->log_parent ); |
| 352 | |
| 353 | // Make sure this payment hasn't been deleted |
| 354 | if ( get_post( $payment->ID ) ) : |
| 355 | $logs_data[] = array( |
| 356 | 'ID' => '<span class="give-item-label give-item-label-gray">' . $log->ID . '</span>', |
| 357 | 'payment_id' => $payment->ID, |
| 358 | 'form' => $payment->form_id, |
| 359 | 'amount' => $payment->total, |
| 360 | 'donor_id' => $payment->customer_id, |
| 361 | 'donor_name' => trim( "{$payment->first_name} $payment->last_name" ), |
| 362 | 'date' => $payment->date, |
| 363 | ); |
| 364 | |
| 365 | endif; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | return $logs_data; |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Setup the final data for the table |
| 374 | * |
| 375 | * @access public |
| 376 | * @since 1.0 |
| 377 | * @uses Give_Sales_Log_Table::get_columns() |
| 378 | * @uses WP_List_Table::get_sortable_columns() |
| 379 | * @uses Give_Sales_Log_Table::get_pagenum() |
| 380 | * @uses Give_Sales_Log_Table::get_logs() |
| 381 | * @uses Give_Sales_Log_Table::get_log_count() |
| 382 | * |
| 383 | * @return void |
| 384 | */ |
| 385 | public function prepare_items() { |
| 386 | $columns = $this->get_columns(); |
| 387 | $hidden = array(); |
| 388 | $sortable = $this->get_sortable_columns(); |
| 389 | $this->_column_headers = array( $columns, $hidden, $sortable ); |
| 390 | $current_page = $this->get_pagenum(); |
| 391 | $this->items = $this->get_logs(); |
| 392 | $total_items = Give()->logs->get_log_count( 0, 'sale', $this->get_meta_query() ); |
| 393 | |
| 394 | $this->set_pagination_args( array( |
| 395 | 'total_items' => $total_items, |
| 396 | 'per_page' => $this->per_page, |
| 397 | 'total_pages' => ceil( $total_items / $this->per_page ), |
| 398 | ) |
| 399 | ); |
| 400 | } |
| 401 | |
| 402 | |
| 403 | /** |
| 404 | * Get log query param. |
| 405 | * |
| 406 | * @since 2.0 |
| 407 | * @access public |
| 408 | * |
| 409 | * @return array |
| 410 | */ |
| 411 | public function get_query_params() { |
| 412 | $paged = $this->get_paged(); |
| 413 | $user = $this->get_filtered_user(); |
| 414 | |
| 415 | $log_query = array( |
| 416 | 'log_type' => 'sale', |
| 417 | 'paged' => $paged, |
| 418 | 'meta_query' => $this->get_meta_query(), |
| 419 | 'number' => $this->per_page, |
| 420 | ); |
| 421 | |
| 422 | return $log_query; |
| 423 | } |
| 424 | } |
| 425 |