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