CustomerWPListTable.php
341 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Admin\SettingsPages\Membership\CustomersPage; |
| 4 | |
| 5 | use ProfilePress\Core\Admin\SettingsPages\Membership\SubscriptionsPage\SubscriptionWPListTable; |
| 6 | use ProfilePress\Core\Membership\Models\Customer\CustomerEntity; |
| 7 | use ProfilePress\Core\Membership\Models\Customer\CustomerStatus; |
| 8 | use ProfilePress\Core\Membership\Models\Plan\PlanFactory; |
| 9 | use ProfilePress\Core\Membership\Models\Subscription\SubscriptionStatus; |
| 10 | use ProfilePress\Core\Membership\Repositories\CustomerRepository; |
| 11 | use ProfilePress\Core\Membership\Repositories\SubscriptionRepository; |
| 12 | use ProfilePress\Core\Membership\Services\CustomerService; |
| 13 | use ProfilePress\Core\Membership\Services\OrderService; |
| 14 | |
| 15 | class CustomerWPListTable extends \WP_List_Table |
| 16 | { |
| 17 | private $views_count = []; |
| 18 | |
| 19 | public function __construct() |
| 20 | { |
| 21 | parent::__construct([ |
| 22 | 'singular' => 'ppress-customer', |
| 23 | 'plural' => 'ppress-customers', |
| 24 | 'ajax' => false |
| 25 | ]); |
| 26 | |
| 27 | $statuses = array_keys(CustomerStatus::get_all()); |
| 28 | $statuses[] = 'all'; |
| 29 | |
| 30 | foreach ($statuses as $id) { |
| 31 | |
| 32 | if ('all' == $id) { |
| 33 | $this->views_count[$id] = CustomerRepository::init()->record_count(); |
| 34 | } else { |
| 35 | $this->views_count[$id] = CustomerRepository::init()->get_count_by_status($id); |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | public function no_items() |
| 41 | { |
| 42 | _e('No customers found.', 'wp-user-avatar'); |
| 43 | } |
| 44 | |
| 45 | public function get_columns() |
| 46 | { |
| 47 | $columns = [ |
| 48 | 'cb' => '<input type="checkbox" />', |
| 49 | 'customer_name' => esc_html__('Name', 'wp-user-avatar'), |
| 50 | 'customer_email' => esc_html__('Email', 'wp-user-avatar'), |
| 51 | 'customer_subscriptions' => esc_html__('Active Subscriptions', 'wp-user-avatar'), |
| 52 | 'customer_since' => esc_html__('Customer Since', 'wp-user-avatar'), |
| 53 | 'customer_last_login' => esc_html__('Last Login', 'wp-user-avatar'), |
| 54 | ]; |
| 55 | |
| 56 | return $columns; |
| 57 | } |
| 58 | |
| 59 | public static function delete_customer_url($customer_id) |
| 60 | { |
| 61 | $nonce_delete = wp_create_nonce('pp_customer_delete_rule'); |
| 62 | |
| 63 | return add_query_arg(['ppress_customer_action' => 'delete', 'id' => $customer_id, '_wpnonce' => $nonce_delete], PPRESS_MEMBERSHIP_CUSTOMERS_SETTINGS_PAGE); |
| 64 | } |
| 65 | |
| 66 | public static function view_customer_url($customer_id) |
| 67 | { |
| 68 | return add_query_arg(['ppress_customer_action' => 'view', 'id' => $customer_id], PPRESS_MEMBERSHIP_CUSTOMERS_SETTINGS_PAGE); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @param CustomerEntity $customer |
| 73 | * |
| 74 | * @return string |
| 75 | */ |
| 76 | public function column_customer_name($customer) |
| 77 | { |
| 78 | $customer_id = absint($customer->id); |
| 79 | |
| 80 | $view_link = esc_url(self::view_customer_url($customer_id)); |
| 81 | $orders_link = OrderService::init()->get_customer_orders_url($customer->id); |
| 82 | $subs_link = add_query_arg(['by_ci' => $customer_id], PPRESS_MEMBERSHIP_SUBSCRIPTIONS_SETTINGS_PAGE); |
| 83 | |
| 84 | $actions = [ |
| 85 | 'view_customer' => sprintf('<a href="%s">%s</a>', $view_link, esc_html__('Edit', 'wp-user-avatar')), |
| 86 | 'view_orders' => sprintf('<a href="%s">%s</a>', $orders_link, esc_html__('View Orders', 'wp-user-avatar')), |
| 87 | 'view_subscriptions' => sprintf('<a href="%s">%s</a>', $subs_link, esc_html__('View Subscriptions', 'wp-user-avatar')), |
| 88 | ]; |
| 89 | |
| 90 | $customer_name = $customer->get_name(); |
| 91 | |
| 92 | $title = sprintf('<a class="row-title" href="%s">%s</a>', $view_link, $customer_name); |
| 93 | |
| 94 | return $title . $this->row_actions($actions); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @param CustomerEntity $customer |
| 99 | * |
| 100 | * @return string |
| 101 | */ |
| 102 | public function column_customer_email($customer) |
| 103 | { |
| 104 | return ! empty($customer->get_email()) ? $customer->get_email() : '———'; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @param CustomerEntity $customer |
| 109 | * |
| 110 | * @return string |
| 111 | */ |
| 112 | public function column_customer_since($customer) |
| 113 | { |
| 114 | return ppress_format_date($customer->get_date_created()); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @param CustomerEntity $customer |
| 119 | * |
| 120 | * @return string |
| 121 | */ |
| 122 | public function column_customer_last_login($customer) |
| 123 | { |
| 124 | return ! empty($customer->get_last_login()) ? $customer->get_last_login() : '———'; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @param CustomerEntity $customer |
| 129 | * |
| 130 | * @return string |
| 131 | */ |
| 132 | public function column_customer_subscriptions($customer) |
| 133 | { |
| 134 | $subs = $customer->get_active_subscriptions(); |
| 135 | |
| 136 | if (empty($subs)) return '———'; |
| 137 | |
| 138 | $subs = array_map(function ($sub) { |
| 139 | |
| 140 | return sprintf( |
| 141 | '<a target="_blank" href="%s">%s</a>', |
| 142 | SubscriptionWPListTable::view_edit_subscription_url($sub->id), |
| 143 | PlanFactory::fromId($sub->get_plan_id())->get_name() |
| 144 | ); |
| 145 | }, $subs); |
| 146 | |
| 147 | return sprintf( |
| 148 | '<a target="_blank" href="%s">%s</a>', |
| 149 | add_query_arg(['by_ci' => $customer->id], PPRESS_MEMBERSHIP_SUBSCRIPTIONS_SETTINGS_PAGE), |
| 150 | implode(', ', $subs) |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | public function get_views() |
| 155 | { |
| 156 | $views = []; |
| 157 | |
| 158 | $args = ['all' => esc_html__('All', 'wp-user-avatar')] + CustomerStatus::get_all(); |
| 159 | |
| 160 | foreach ($args as $id => $status) { |
| 161 | |
| 162 | $url = $id == 'all' ? PPRESS_MEMBERSHIP_CUSTOMERS_SETTINGS_PAGE : add_query_arg(['status' => $id], PPRESS_MEMBERSHIP_CUSTOMERS_SETTINGS_PAGE); |
| 163 | |
| 164 | $views[$id] = sprintf( |
| 165 | '<a href="%s"%s>%s <span class="count">(%s)</span></a>', |
| 166 | $url, |
| 167 | ppressGET_var('status', 'all') == $id ? ' class="current"' : '', |
| 168 | $status, |
| 169 | $this->views_count[$id] |
| 170 | ); |
| 171 | } |
| 172 | |
| 173 | return $views; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Render the bulk edit checkbox |
| 178 | * |
| 179 | * @param CustomerEntity $customer |
| 180 | * |
| 181 | * @return string |
| 182 | */ |
| 183 | public function column_cb($customer) |
| 184 | { |
| 185 | return sprintf('<input type="checkbox" name="customer_id[]" value="%s" />', $customer->id); |
| 186 | } |
| 187 | |
| 188 | public function record_count() |
| 189 | { |
| 190 | $status = ppressGET_var('status', 'all'); |
| 191 | |
| 192 | if ($status != 'all') { |
| 193 | return $this->views_count[$status]; |
| 194 | } |
| 195 | |
| 196 | return SubscriptionRepository::init()->record_count(); |
| 197 | } |
| 198 | |
| 199 | public function prepare_items() |
| 200 | { |
| 201 | $this->_column_headers = $this->get_column_info(); |
| 202 | |
| 203 | $this->process_bulk_action(); |
| 204 | |
| 205 | $per_page = $this->get_items_per_page('customers_per_page', 10); |
| 206 | $current_page = $this->get_pagenum(); |
| 207 | $offset = ($current_page - 1) * $per_page; |
| 208 | |
| 209 | $search = ! empty($_GET['s']) ? sanitize_text_field($_GET['s']) : ''; |
| 210 | |
| 211 | $start_date = ppressGET_var('start_date'); |
| 212 | $end_date = ppressGET_var('end_date'); |
| 213 | $status = ppressGET_var('status'); |
| 214 | |
| 215 | $query_args = [ |
| 216 | 'number' => $per_page, |
| 217 | 'offset' => $offset, |
| 218 | 'search' => $search, |
| 219 | 'status' => $status, |
| 220 | 'start_date' => $start_date, |
| 221 | 'end_date' => $end_date |
| 222 | ]; |
| 223 | |
| 224 | $this->items = CustomerRepository::init()->retrieveBy($query_args); |
| 225 | |
| 226 | $total_items = CustomerRepository::init()->retrieveBy($query_args, true); |
| 227 | |
| 228 | $this->set_pagination_args([ |
| 229 | 'total_items' => $total_items, |
| 230 | 'per_page' => $per_page |
| 231 | ]); |
| 232 | } |
| 233 | |
| 234 | public function current_action() |
| 235 | { |
| 236 | if (isset($_REQUEST['filter_action']) && ! empty($_REQUEST['filter_action'])) { |
| 237 | return false; |
| 238 | } |
| 239 | |
| 240 | if (isset($_REQUEST['action']) && -1 != $_REQUEST['action']) { |
| 241 | return $_REQUEST['action']; |
| 242 | } |
| 243 | |
| 244 | if (isset($_REQUEST['ppress_customer_action']) && -1 != $_REQUEST['ppress_customer_action']) { |
| 245 | return $_REQUEST['ppress_customer_action']; |
| 246 | } |
| 247 | |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | public function get_bulk_actions() |
| 252 | { |
| 253 | $actions = [ |
| 254 | 'bulk-delete' => esc_html__('Delete', 'wp-user-avatar') |
| 255 | ]; |
| 256 | |
| 257 | return $actions; |
| 258 | } |
| 259 | |
| 260 | public function process_bulk_action() |
| 261 | { |
| 262 | $customer_id = absint(ppress_var($_GET, 'id', 0)); |
| 263 | |
| 264 | if ( ! current_user_can('manage_options')) return; |
| 265 | |
| 266 | if ('delete' === $this->current_action()) { |
| 267 | |
| 268 | check_admin_referer('pp_customer_delete_rule'); |
| 269 | |
| 270 | if ( ! current_user_can('manage_options')) return; |
| 271 | |
| 272 | CustomerService::init()->delete_customer($customer_id); |
| 273 | } |
| 274 | |
| 275 | if ('bulk-delete' === $this->current_action()) { |
| 276 | |
| 277 | check_admin_referer('bulk-' . $this->_args['plural']); |
| 278 | |
| 279 | if ( ! current_user_can('manage_options')) return; |
| 280 | |
| 281 | $customer_ids = array_map('absint', $_GET['customer_id']); |
| 282 | |
| 283 | foreach ($customer_ids as $customer_id) { |
| 284 | CustomerService::init()->delete_customer($customer_id); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | if ($this->current_action() !== false) { |
| 289 | wp_safe_redirect(PPRESS_MEMBERSHIP_CUSTOMERS_SETTINGS_PAGE); |
| 290 | exit; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | public function filter_bar() |
| 295 | { |
| 296 | $start_date = isset($_GET['start_date']) ? sanitize_text_field($_GET['start_date']) : null; |
| 297 | $end_date = isset($_GET['end_date']) ? sanitize_text_field($_GET['end_date']) : null; |
| 298 | |
| 299 | $status = ppressGET_var('status'); |
| 300 | $clear_url = PPRESS_MEMBERSHIP_CUSTOMERS_SETTINGS_PAGE; |
| 301 | |
| 302 | echo '<div class="wp-filter" id="ppress-filters">'; |
| 303 | echo '<div class="filter-items">'; |
| 304 | ?> |
| 305 | |
| 306 | <span id="ppress-date-filters" class="ppress-from-to-wrapper"> |
| 307 | <span class="ppress-start-date-wrap"> |
| 308 | <input type="text" name="start_date" id="start-date" placeholder="<?= _x('From', 'date filter', 'wp-user-avatar') ?>" value="<?= $start_date ?>" class="ppress_datepicker"> |
| 309 | </span> |
| 310 | <span id="ppress-end-date-wrap"> |
| 311 | <input type="text" name="end_date" id="end-date" value="<?= $end_date ?>" placeholder="<?= _x('To', 'date filter', 'wp-user-avatar') ?>" class="ppress_datepicker"> |
| 312 | </span> |
| 313 | </span> |
| 314 | |
| 315 | <span id="ppress-after-core-filters"> |
| 316 | <input type="submit" class="button button-secondary" value="<?php esc_html_e('Filter', 'wp-user-avatar'); ?>"/> |
| 317 | |
| 318 | <?php if ( ! empty($_GET['s']) || ! empty($start_date) || ! empty($end_date)) : ?> |
| 319 | <a href="<?php echo esc_url($clear_url); ?>" class="button-secondary"> |
| 320 | <?php esc_html_e('Clear', 'wp-user-avatar'); ?> |
| 321 | </a> |
| 322 | <?php endif; ?> |
| 323 | </span> |
| 324 | |
| 325 | <?php if ( ! empty($status)) : ?> |
| 326 | <input type="hidden" name="status" value="<?php echo esc_attr($status); ?>"/> |
| 327 | <?php endif; |
| 328 | echo '</div>'; |
| 329 | $this->search_box(__('Search Customers', 'wp-user-avatar'), 'ppress_customer_search'); |
| 330 | echo '</div>'; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * @return array List of CSS classes for the table tag. |
| 335 | */ |
| 336 | public function get_table_classes() |
| 337 | { |
| 338 | return array('widefat', 'fixed', 'striped', 'customer', 'ppview'); |
| 339 | } |
| 340 | } |
| 341 |