From faed480fa48df59d2a4522555a8b24e6ee725f03 Mon Sep 17 00:00:00 2001 From: kawsarahmedr Date: Wed, 31 Jul 2024 13:25:38 +0600 Subject: [PATCH] Bulk delete --- .../Admin/ListTables/CampaignsListTable.php | 124 ++++++++---------- includes/Admin/ListTables/ListTable.php | 89 ++++++++++--- 2 files changed, 127 insertions(+), 86 deletions(-) diff --git a/includes/Admin/ListTables/CampaignsListTable.php b/includes/Admin/ListTables/CampaignsListTable.php index 9dd8961..9cce8e3 100644 --- a/includes/Admin/ListTables/CampaignsListTable.php +++ b/includes/Admin/ListTables/CampaignsListTable.php @@ -13,24 +13,27 @@ class CampaignsListTable extends ListTable { /** - * Get campaigns started + * Constructor. * - * @param array $args Optional. + * @param array $args An associative array of arguments. * - * @see WP_List_Table::__construct() - * @since 1.0.0 + * @see WP_List_Table::__construct() for more information on default arguments. + * @since 1.0.0 */ public function __construct( $args = array() ) { - $args = (array) wp_parse_args( - $args, - array( - 'singular' => 'campaign', - 'plural' => 'campaigns', - 'ajax' => 'true', + parent::__construct( + wp_parse_args( + $args, + array( + 'singular' => 'campaign', + 'plural' => 'campaigns', + 'screen' => get_current_screen(), + 'args' => array(), + ) ) ); - $this->screen = get_current_screen(); - parent::__construct( $args ); + + $this->base_url = admin_url( 'admin.php?page=wc-donation-manager' ); } /** @@ -41,23 +44,31 @@ public function __construct( $args = array() ) { */ public function prepare_items() { wp_verify_nonce( '_nonce' ); - $columns = $this->get_columns(); - $sortable = $this->get_sortable_columns(); - $hidden = $this->get_hidden_columns(); - $this->_column_headers = array( $columns, $hidden, $sortable ); - $per_page = $this->get_items_per_page( 'wcdm_campaigns_per_page', 20 ); - $order_by = isset( $_GET['orderby'] ) ? sanitize_key( wp_unslash( $_GET['orderby'] ) ) : ''; - $order = isset( $_GET['order'] ) ? sanitize_key( wp_unslash( $_GET['order'] ) ) : ''; - $search = isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : ''; - $current_page = isset( $_GET['paged'] ) ? sanitize_key( wp_unslash( $_GET['paged'] ) ) : 1; - $args = array( + // $columns = $this->get_columns(); + // $sortable = $this->get_sortable_columns(); + // $hidden = $this->get_hidden_columns(); + // $this->_column_headers = array( $columns, $hidden, $sortable ); + // $per_page = $this->get_items_per_page( 'wcdm_campaigns_per_page', 20 ); + // $order_by = isset( $_GET['orderby'] ) ? sanitize_key( wp_unslash( $_GET['orderby'] ) ) : ''; + // $order = isset( $_GET['order'] ) ? sanitize_key( wp_unslash( $_GET['order'] ) ) : ''; + // $search = isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : ''; + // $current_page = isset( $_GET['paged'] ) ? sanitize_key( wp_unslash( $_GET['paged'] ) ) : 1; + + $this->process_actions(); + $per_page = $this->get_items_per_page( 'wcdm_campaigns_per_page', 20 ); + $paged = $this->get_pagenum(); + $search = $this->get_request_search(); + $order_by = $this->get_request_orderby( 'order_id' ); + $order = $this->get_request_order(); + + $args = array( 'post_type' => 'wcdm_campaigns', 'post_status' => 'any', 'order' => $order, 'order_by' => $order_by, 's' => $search, 'posts_per_page' => $per_page, - 'paged' => $current_page, + 'paged' => $paged, ); $query = new \WP_Query( $args ); @@ -72,6 +83,27 @@ public function prepare_items() { ); } + /** + * handle bulk delete action. + * + * @param array $ids List of item IDs. + * + * @since 1.0.0 + * @return void + */ + protected function bulk_delete( $ids ) { + $performed = 0; + foreach ( $ids as $id ) { + if ( wp_delete_post( $id, true ) ) { + ++$performed; + } + } + if ( ! empty( $performed ) ) { + // translators: %s: number of accounts. + WCDM()->flash->success( sprintf( __( '%s campaign(s) deleted successfully.', 'wc-donation-manager' ), number_format_i18n( $performed ) ) ); + } + } + /** * No items found text. * @@ -131,56 +163,12 @@ public function get_hidden_columns() { * * @return array */ - public function get_bulk_actions() { + protected function get_bulk_actions() { return array( 'delete' => __( 'Delete', 'wc-donation-manager' ), ); } - /** - * Process bulk action. - * - * @param string $doaction Action name. - * - * @since 1.0.2 - */ - public function process_bulk_action( $doaction ) { - wp_verify_nonce( '_nonce' ); - - if ( ! empty( $doaction ) && check_admin_referer( 'bulk-' . $this->_args['plural'] ) ) { - $id = filter_input( INPUT_GET, 'id' ); - $ids = filter_input( INPUT_GET, 'ids', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); - if ( ! empty( $id ) ) { - $ids = wp_parse_id_list( $id ); - $doaction = ( - 1 !== $_REQUEST['action'] ) ? $_REQUEST['action'] : $_REQUEST['action2']; - } elseif ( ! empty( $ids ) ) { - $ids = array_map( 'absint', $ids ); - } elseif ( wp_get_referer() ) { - wp_safe_redirect( wp_get_referer() ); - exit; - } - - switch ( $doaction ) { - case 'delete': - $deleted = 0; - foreach ( $ids as $id ) { - $campaign = wcdm_get_campaign( $id ); - if ( $campaign && $campaign->delete() ) { - ++$deleted; - } - } - // translators: %d: number of campaigns deleted. - WCDM()->flash->success( sprintf( _n( '%d campaign deleted.', '%d campaigns deleted.', $deleted, 'wc-donation-manager' ), $deleted ) ); - break; - } - - wp_safe_redirect( remove_query_arg( array( 'action', 'action2', 'id', 'ids', 'paged' ) ) ); - exit(); - } - - parent::process_bulk_actions( $doaction ); - } - /** * Define primary column. * diff --git a/includes/Admin/ListTables/ListTable.php b/includes/Admin/ListTables/ListTable.php index 6dff6ce..444aefe 100644 --- a/includes/Admin/ListTables/ListTable.php +++ b/includes/Admin/ListTables/ListTable.php @@ -16,43 +16,96 @@ * @package WooCommerceDonationManager */ class ListTable extends \WP_List_Table { + /** - * Tab Post + * Current page URL. * - * @var object + * @since 1.0.0 + * @var string */ - public $items; + protected $base_url; /** - * Total count + * Return the sortable column specified for this request to order the results by, if any. + * + * @param string $fallback Default order. * - * @var int + * @return string */ - public $total_count; + protected function get_request_orderby( $fallback = 'id' ) { + wp_verify_nonce( '_wpnonce' ); + $orderby = isset( $_GET['orderby'] ) ? sanitize_text_field( wp_unslash( $_GET['orderby'] ) ) : ''; + return in_array( $orderby, $this->get_sortable_columns(), true ) ? $orderby : $fallback; + } + /** - * Total pages count + * Return the order specified for this request, if any. + * + * @param string $fallback Default order. * - * @var int + * @return string */ - public $total_pages; + protected function get_request_order( $fallback = 'DESC' ) { + wp_verify_nonce( '_wpnonce' ); + $order = isset( $_GET['order'] ) ? sanitize_text_field( wp_unslash( $_GET['order'] ) ) : ''; + return in_array( strtoupper( $order ), array( 'ASC', 'DESC' ), true ) ? strtoupper( $order ) : $fallback; + } /** - * Process bulk action. + * Return the status filter for this request, if any. + * + * @param string $fallback Default status. * - * @param string $doaction Action name. + * @since 1.0.0 + * @return string + */ + protected function get_request_status( $fallback = null ) { + wp_verify_nonce( '_wpnonce' ); + $status = ( ! empty( $_GET['status'] ) ) ? sanitize_text_field( wp_unslash( $_GET['status'] ) ) : ''; + + return empty( $status ) ? $fallback : $status; + } + + /** + * Return the search filter for this request, if any. * * @since 1.0.0 + * @return string + */ + public function get_request_search() { + wp_verify_nonce( '_wpnonce' ); + return ! empty( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : ''; + } + + /** + * Checks if the current request has a bulk action. If that is the case it will validate and will + * execute the bulk method handler. Regardless if the action is valid or not it will redirect to + * the previous page removing the current arguments that makes this request a bulk action. */ - public function process_bulk_actions( $doaction ) { - if ( ! empty( $_GET['_wp_http_referer'] ) || ! empty( $_GET['_wpnonce'] ) ) { // phpcs:ignore + protected function process_actions() { + $this->_column_headers = array( $this->get_columns(), get_hidden_columns( $this->screen ), $this->get_sortable_columns() ); + + // Detect when a bulk action is being triggered. + $action = $this->current_action(); + if ( ! $action ) { + return; + } + + check_admin_referer( 'bulk-' . $this->_args['plural'] ); + + $ids = isset( $_GET['id'] ) ? map_deep( wp_unslash( $_GET['id'] ), 'intval' ) : array(); + $ids = wp_parse_id_list( $ids ); + $method = 'bulk_' . $action; + if ( array_key_exists( $action, $this->get_bulk_actions() ) && method_exists( $this, $method ) && ! empty( $ids ) ) { + $this->$method( $ids ); + } + + if ( isset( $_SERVER['REQUEST_URI'] ) || isset( $_REQUEST['_wp_http_referer'] ) ) { wp_safe_redirect( remove_query_arg( - array( - '_wp_http_referer', - '_wpnonce', - ), - wp_unslash( $_SERVER['REQUEST_URI'] ) // phpcs:ignore + array( '_wp_http_referer', '_wpnonce', 'id', 'action', 'action2' ), + esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) ); exit;