WithdrawalsList


Source

File: classes/WithdrawalsList.php

class WithdrawalsList extends \WP_List_Table {

	function __construct() {
		global $status, $page;

		//Set parent defaults
		parent::__construct( array(
			'singular' => 'withdrawal',     //singular name of the listed records
			'plural'   => 'withdrawals',    //plural name of the listed records
			'ajax'     => false        //does this table support ajax?
		) );

		$this->process_bulk_action();
	}

	function column_default( $item, $column_name ) {
		switch ( $column_name ) {
			case 'testing_col':
				return $item->$column_name;
			default:
				return print_r( $item, true ); //Show the whole array for troubleshooting purposes
		}
	}

	function column_cb( $item ) {
		return sprintf(
			'<input type="checkbox" name="%1$s[]" value="%2$s" />',
			/*$1%s*/ $this->_args['singular'],  //Let's simply repurpose the table's singular label ("student")
			/*$2%s*/ $item->withdraw_id                //The value of the checkbox should be the record's id
		);
	}

	function column_requested_user( $item ) {
		echo "<p>{$item->user_name}</p><p>{$item->user_email}</p>";

		$actions = array();
		switch ( $item->status ) {
			case 'pending':
				$actions['completed'] = sprintf( '<a href="?page=%s&action=%s&withdraw_id=%s"> %s </a>',
					$_REQUEST['page'], 'completed', $item->withdraw_id, __( 'Complete', 'dozent' ) );
				$actions['rejected']  = sprintf( '<a href="?page=%s&action=%s&withdraw_id=%s"> %s </a>',
					$_REQUEST['page'], 'rejected', $item->withdraw_id, __( 'Reject', 'dozent' ) );
				break;
			case 'completed':
				$actions['rejected'] = sprintf( '<a href="?page=%s&action=%s&withdraw_id=%s"> %s </a>',
					$_REQUEST['page'], 'rejected', $item->withdraw_id, __( 'Reject', 'dozent' ) );
				break;
			case 'rejected':
				$actions['completed'] = sprintf( '<a href="?page=%s&action=%s&withdraw_id=%s"> %s </a>',
					$_REQUEST['page'], 'completed', $item->withdraw_id, __( 'Complete', 'dozent' ) );
				break;
		}

		$actions['delete']
			= sprintf( '<a href="?page=%s&action=%s&withdraw_id=%s" onclick="return confirm(\'Are you Sure? It can not be undone\')">Delete</a>',
			$_REQUEST['page'], 'delete',
			$item->withdraw_id );

		return "<div class='withdraw-list-row-actions'>" . $this->row_actions( $actions ) . "</div>";
	}

	function column_withdraw_method( $item ) {
		if ( $item->method_data ) {
			$data = maybe_unserialize( $item->method_data );

			$method_name = dozent_array_get( 'withdraw_method_name', $data );

			if ( $method_name ) {
				echo "<p><strong>{$method_name}</strong></p>";
			}

			unset( $data['withdraw_method_key'], $data['withdraw_method_name'] );

			if ( dozent_count( $data ) ) {
				foreach ( $data as $method_field ) {
					$label = dozent_array_get( 'label', $method_field );
					$value = dozent_array_get( 'value', $method_field );
					echo "<p class='withdraw-method-data-row'> <span class='withdraw-method-label'>{$label}</span> : <span class='withdraw-method-value'>{$value}</span> </p>";
				}
			}

		}

		return '';
	}

	function column_requested_at( $item ) {
		echo "<p>" . date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ),
				strtotime( $item->created_at ) ) . "</p>";
	}

	function column_amount( $item ) {
		echo "<p>" . dozent_price( $item->amount ) . "</p>";
		echo "<p><span class='dozent-pill dozent-pill-{$item->status}'>{$item->status}</span></p>";
	}

	function get_columns() {
		$columns = array(
			//'cb'              => '<input type="checkbox" />', //Render a checkbox instead of text
			'requested_user'  => __( 'Requested By', 'dozent' ),
			'amount'          => __( 'Amount', 'dozent' ),
			'withdraw_method' => __( 'Withdraw Method', 'dozent' ),
			'requested_at'    => __( 'Requested Time', 'dozent' ),
		);

		return $columns;
	}

	function get_bulk_actions() {
		$actions = array(//'delete'    => 'Delete'
		);

		return $actions;
	}

	function process_bulk_action() {
		global $wpdb;

		$withdraw_page_url = admin_url( 'admin.php?page=dozent_withdrawals' );
		$date              = dozent_mysql_time();
		$redirect          = false;

		//Detect when a bulk action is being triggered...
		if ( 'delete' === $this->current_action() ) {
			$should_withdraw_delete = apply_filters( 'dozent_should_withdraw_delete', true );

			if ( $should_withdraw_delete ) {
				$withdraw_id = (int) sanitize_text_field( $_GET['withdraw_id'] );

				do_action( 'dozent_before_delete_withdraw', $withdraw_id );

				$wpdb->delete( $wpdb->dozent_withdrawal, array( 'withdraw_id' => $withdraw_id ) );

				do_action( 'dozent_after_delete_withdraw', $withdraw_id );

				$redirect = true;
			} else {
				wp_die( 'Items deleted (or they would be if we had items to delete)!' );
			}
		}


		/**
		 * Reject Withdraw
		 */
		if ( 'completed' === $this->current_action() ) {
			$withdraw_id = (int) sanitize_text_field( $_GET['withdraw_id'] );
			$withdraw = $wpdb->get_row( "SELECT * FROM {$wpdb->dozent_withdrawal} WHERE withdraw_id = {$withdraw_id} " );
			if ( ! $withdraw || $withdraw->status === 'completed' ) {
				return;
			}

			do_action( 'dozent_before_completed_withdraw', $withdraw_id );

			$wpdb->update( $wpdb->dozent_withdrawal, array( 'status' => 'completed', 'updated_at' => $date ),
				array( 'withdraw_id' => $withdraw_id ) );

			do_action( 'dozent_after_completed_withdraw', $withdraw_id );

			$redirect = true;
		}

		/**
		 * Rejected
		 */
		if ( 'rejected' === $this->current_action() ) {
			$withdraw_id = (int) sanitize_text_field( $_GET['withdraw_id'] );
			$withdraw
			             = $wpdb->get_row( "SELECT * FROM {$wpdb->dozent_withdrawal} WHERE withdraw_id = {$withdraw_id} " );
			if ( ! $withdraw || $withdraw->status === 'rejected' ) {
				return;
			}

			do_action( 'dozent_before_rejected_withdraw', $withdraw_id );

			$wpdb->update( $wpdb->dozent_withdrawal, array( 'status' => 'rejected', 'updated_at' => $date ),
				array( 'withdraw_id' => $withdraw_id ) );

			do_action( 'dozent_after_rejected_withdraw', $withdraw_id );

			$redirect = true;
		}

		if ( $redirect ) {
			die( "<script>location.href='{$withdraw_page_url}';</script>" );
		}
	}

	function prepare_items() {
		$per_page = 20;

		$search_term = '';
		if ( isset( $_REQUEST['s'] ) ) {
			$search_term = sanitize_text_field( $_REQUEST['s'] );
		}

		$columns               = $this->get_columns();
		$hidden                = [];
		$this->_column_headers = [ $columns, $hidden ];
		$current_page          = $this->get_pagenum();
		$start                 = ( $current_page - 1 ) * $per_page;
		$withdraw_requests     = dozent_get_withdrawals( null, compact( 'start', 'per_page', 'search_term' ) );
		$this->items           = $withdraw_requests->results;
		$count_result          = $withdraw_requests->count;

		$this->set_pagination_args( [
			'total_items' => $count_result,
			'per_page'    => $per_page,
			'total_pages' => ceil( $count_result / $per_page )
		] );
	}
}

Methods