wp-user-avatar
/
src
/
Admin
/
SettingsPages
/
Membership
/
SubscriptionsPage
/
SubscriptionWPListTable.php
SubscriptionWPListTable.php
416 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Admin\SettingsPages\Membership\SubscriptionsPage; |
| 4 | |
| 5 | use ProfilePress\Core\Membership\Models\Customer\CustomerFactory; |
| 6 | use ProfilePress\Core\Membership\Models\Order\OrderFactory; |
| 7 | use ProfilePress\Core\Membership\Models\Plan\PlanFactory; |
| 8 | use ProfilePress\Core\Membership\Models\Subscription\SubscriptionBillingFrequency; |
| 9 | use ProfilePress\Core\Membership\Models\Subscription\SubscriptionEntity; |
| 10 | use ProfilePress\Core\Membership\Models\Subscription\SubscriptionStatus; |
| 11 | use ProfilePress\Core\Membership\Repositories\SubscriptionRepository; |
| 12 | use ProfilePress\Core\Membership\Services\Calculator; |
| 13 | use ProfilePress\Core\Membership\Services\SubscriptionService; |
| 14 | use ProfilePressVendor\Carbon\CarbonImmutable; |
| 15 | |
| 16 | class SubscriptionWPListTable extends \WP_List_Table |
| 17 | { |
| 18 | private $views_count = []; |
| 19 | |
| 20 | public function __construct() |
| 21 | { |
| 22 | parent::__construct([ |
| 23 | 'singular' => 'ppress-subscription', |
| 24 | 'plural' => 'ppress-subscriptions', |
| 25 | 'ajax' => false |
| 26 | ]); |
| 27 | |
| 28 | $subscription_statuses = array_keys(SubscriptionStatus::get_all()); |
| 29 | $subscription_statuses[] = 'all'; |
| 30 | |
| 31 | foreach ($subscription_statuses as $id) { |
| 32 | |
| 33 | if ('all' == $id) { |
| 34 | $this->views_count[$id] = SubscriptionRepository::init()->record_count(); |
| 35 | } else { |
| 36 | $this->views_count[$id] = SubscriptionRepository::init()->get_count_by_status($id); |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | public function no_items() |
| 42 | { |
| 43 | _e('No subscriptions found.', 'wp-user-avatar'); |
| 44 | } |
| 45 | |
| 46 | public function get_columns() |
| 47 | { |
| 48 | $columns = [ |
| 49 | 'cb' => '<input type="checkbox" />', |
| 50 | 'subscription' => esc_html__('Subscription', 'wp-user-avatar'), |
| 51 | 'plan' => esc_html__('Plan', 'wp-user-avatar'), |
| 52 | 'status' => esc_html__('Status', 'wp-user-avatar'), |
| 53 | 'initial_payment' => esc_html__('Initial Order', 'wp-user-avatar'), |
| 54 | 'renewal_date' => esc_html__('Renewal Date', 'wp-user-avatar'), |
| 55 | ]; |
| 56 | |
| 57 | if (in_array(ppressGET_var('status'), [SubscriptionStatus::EXPIRED, SubscriptionStatus::CANCELLED])) { |
| 58 | $columns['renewal_date'] = esc_html__('Expiration Date', 'wp-user-avatar'); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Filter subscription table columns to allow custom columns to be added. |
| 63 | * |
| 64 | * @param array $columns Array of columns with column key => column label pairs. |
| 65 | */ |
| 66 | return apply_filters('ppress_subscription_table_columns', $columns); |
| 67 | } |
| 68 | |
| 69 | public function get_views() |
| 70 | { |
| 71 | $views = []; |
| 72 | |
| 73 | $args = ['all' => esc_html__('All', 'wp-user-avatar')] + SubscriptionStatus::get_all(); |
| 74 | |
| 75 | foreach ($args as $id => $status) { |
| 76 | |
| 77 | $url = $id == 'all' ? PPRESS_MEMBERSHIP_SUBSCRIPTIONS_SETTINGS_PAGE : add_query_arg(['status' => $id], PPRESS_MEMBERSHIP_SUBSCRIPTIONS_SETTINGS_PAGE); |
| 78 | |
| 79 | $views[$id] = sprintf( |
| 80 | '<a href="%s"%s>%s <span class="count">(%s)</span></a>', |
| 81 | $url, |
| 82 | ppressGET_var('status', 'all') == $id ? ' class="current"' : '', |
| 83 | $status, |
| 84 | $this->views_count[$id] |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | return $views; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Render the bulk edit checkbox |
| 93 | * |
| 94 | * @param SubscriptionEntity $subscription |
| 95 | * |
| 96 | * @return string |
| 97 | */ |
| 98 | public function column_cb($subscription) |
| 99 | { |
| 100 | return sprintf('<input type="checkbox" name="subscription_id[]" value="%s" />', $subscription->id); |
| 101 | } |
| 102 | |
| 103 | public static function view_edit_subscription_url($subscription_id) |
| 104 | { |
| 105 | return esc_url(add_query_arg([ |
| 106 | 'ppress_subscription_action' => 'edit', |
| 107 | 'id' => $subscription_id |
| 108 | ], PPRESS_MEMBERSHIP_SUBSCRIPTIONS_SETTINGS_PAGE)); |
| 109 | } |
| 110 | |
| 111 | public function column_subscription(SubscriptionEntity $subscription) |
| 112 | { |
| 113 | $subscription_id = absint($subscription->id); |
| 114 | |
| 115 | $edit_link = self::view_edit_subscription_url($subscription_id); |
| 116 | $delete_link = self::delete_subscription_url($subscription_id); |
| 117 | |
| 118 | $actions = [ |
| 119 | 'edit' => sprintf('<a href="%s">%s</a>', $edit_link, esc_html__('Edit', 'wp-user-avatar')), |
| 120 | ]; |
| 121 | |
| 122 | $actions['delete'] = sprintf('<a class="pp-confirm-delete" href="%s">%s</a>', $delete_link, esc_html__('Delete', 'wp-user-avatar')); |
| 123 | |
| 124 | $customer_name = CustomerFactory::fromId($subscription->customer_id)->get_name(); |
| 125 | |
| 126 | if ( ! empty($customer_name)) { |
| 127 | $title = sprintf(__('#%1$s - %2$s', 'wp-user-avatar'), $subscription->get_id(), $customer_name); |
| 128 | } else { |
| 129 | $title = sprintf(__('#%1$s - No Customer Assigned', 'wp-user-avatar'), $subscription->get_id()); |
| 130 | } |
| 131 | |
| 132 | $title = sprintf('<a class="row-title" href="%s">%s</a>', $edit_link, $title); |
| 133 | |
| 134 | return $title . $this->row_actions($actions); |
| 135 | } |
| 136 | |
| 137 | public function column_plan(SubscriptionEntity $subscription) |
| 138 | { |
| 139 | $planFactory = PlanFactory::fromId($subscription->plan_id); |
| 140 | $output = sprintf( |
| 141 | '<span class="ppress-line-header"><a href="%s">%s</a></span>', |
| 142 | $planFactory->get_edit_plan_url(), |
| 143 | $planFactory->get_name() |
| 144 | ); |
| 145 | |
| 146 | $output .= sprintf('<span class="ppress-line-note">%s</span>', $subscription->get_subscription_terms()); |
| 147 | |
| 148 | return $output; |
| 149 | } |
| 150 | |
| 151 | public function column_initial_payment(SubscriptionEntity $subscription) |
| 152 | { |
| 153 | $initial_amount = $subscription->get_initial_amount(); |
| 154 | |
| 155 | $output = sprintf( |
| 156 | '<span class="ppress-line-header">%s</span>', |
| 157 | ppress_display_amount($initial_amount, OrderFactory::fromId($subscription->parent_order_id)->currency) |
| 158 | ); |
| 159 | |
| 160 | $output .= sprintf('<span class="ppress-line-note">%s</span>', ppress_format_date($subscription->created_date)); |
| 161 | |
| 162 | return $output; |
| 163 | } |
| 164 | |
| 165 | public function column_renewal_date(SubscriptionEntity $subscription) |
| 166 | { |
| 167 | return $subscription->get_formatted_expiration_date(); |
| 168 | } |
| 169 | |
| 170 | public function column_date_created(SubscriptionEntity $subscription) |
| 171 | { |
| 172 | $date = $subscription->created_date; |
| 173 | |
| 174 | if (empty($date)) return '—'; |
| 175 | |
| 176 | return ppress_format_date_time($subscription->created_date); |
| 177 | } |
| 178 | |
| 179 | public function column_status(SubscriptionEntity $subscription) |
| 180 | { |
| 181 | return self::get_subscription_status_badge($subscription->status); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Handle rendering of custom subscription table columns. |
| 186 | * |
| 187 | * Allows developers to add custom columns via the ppress_subscription_table_columns filter, |
| 188 | * and render them via the ppress_subscription_table_column_{column_name} filter. |
| 189 | * |
| 190 | * @param SubscriptionEntity $subscription The subscription entity. |
| 191 | * @param string $column_name The name of the column being rendered. |
| 192 | */ |
| 193 | public function column_default($subscription, $column_name) |
| 194 | { |
| 195 | return apply_filters("ppress_subscription_table_column_{$column_name}", '', $subscription, $column_name); |
| 196 | } |
| 197 | |
| 198 | public static function delete_subscription_url($subscription_id) |
| 199 | { |
| 200 | $nonce_delete = wp_create_nonce('pp_subscription_delete_rule'); |
| 201 | |
| 202 | return add_query_arg([ |
| 203 | 'action' => 'delete', |
| 204 | 'id' => $subscription_id, |
| 205 | '_wpnonce' => $nonce_delete |
| 206 | ], PPRESS_MEMBERSHIP_SUBSCRIPTIONS_SETTINGS_PAGE); |
| 207 | } |
| 208 | |
| 209 | public function record_count() |
| 210 | { |
| 211 | $status = ppressGET_var('status', 'all'); |
| 212 | |
| 213 | if ($status != 'all') { |
| 214 | return $this->views_count[$status]; |
| 215 | } |
| 216 | |
| 217 | return SubscriptionRepository::init()->record_count(); |
| 218 | } |
| 219 | |
| 220 | public function prepare_items() |
| 221 | { |
| 222 | $this->_column_headers = $this->get_column_info(); |
| 223 | |
| 224 | $this->process_bulk_action(); |
| 225 | |
| 226 | $per_page = $this->get_items_per_page('subscriptions_per_page', 10); |
| 227 | $current_page = $this->get_pagenum(); |
| 228 | $offset = ($current_page - 1) * $per_page; |
| 229 | |
| 230 | $search = ! empty($_GET['s']) ? sanitize_text_field($_GET['s']) : ''; |
| 231 | |
| 232 | $start_date = ppressGET_var('start_date'); |
| 233 | $end_date = ppressGET_var('end_date'); |
| 234 | $status = ppressGET_var('status'); |
| 235 | |
| 236 | $query_args = [ |
| 237 | 'number' => $per_page, |
| 238 | 'offset' => $offset, |
| 239 | 'search' => $search, |
| 240 | 'status' => [$status] |
| 241 | ]; |
| 242 | |
| 243 | if ( ! empty($start_date)) { |
| 244 | $query_args['start_date'] = CarbonImmutable::parse($start_date, wp_timezone())->startOfDay()->utc()->toDateTimeString(); |
| 245 | } |
| 246 | |
| 247 | if ( ! empty($end_date)) { |
| 248 | $query_args['end_date'] = CarbonImmutable::parse($end_date, wp_timezone())->endOfDay()->utc()->toDateTimeString(); |
| 249 | } |
| 250 | |
| 251 | if (ppressGET_var('by_ci')) { |
| 252 | $query_args['customer_id'] = absint($_GET['by_ci']); |
| 253 | } |
| 254 | |
| 255 | if (ppressGET_var('by_plan')) { |
| 256 | $query_args['plan_id'] = absint($_GET['by_plan']); |
| 257 | } |
| 258 | |
| 259 | $this->items = SubscriptionRepository::init()->retrieveBy($query_args); |
| 260 | |
| 261 | $total_items = SubscriptionRepository::init()->retrieveBy($query_args, true); |
| 262 | |
| 263 | $this->set_pagination_args([ |
| 264 | 'total_items' => $total_items, |
| 265 | 'per_page' => $per_page |
| 266 | ]); |
| 267 | } |
| 268 | |
| 269 | public function current_action() |
| 270 | { |
| 271 | if (isset($_REQUEST['filter_action']) && ! empty($_REQUEST['filter_action'])) { |
| 272 | return false; |
| 273 | } |
| 274 | |
| 275 | if (isset($_REQUEST['action']) && -1 != $_REQUEST['action']) { |
| 276 | return $_REQUEST['action']; |
| 277 | } |
| 278 | |
| 279 | if (isset($_REQUEST['ppress_subscription_action']) && -1 != $_REQUEST['ppress_subscription_action']) { |
| 280 | return $_REQUEST['ppress_subscription_action']; |
| 281 | } |
| 282 | |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | public function get_bulk_actions() |
| 287 | { |
| 288 | $actions = [ |
| 289 | 'bulk-delete' => esc_html__('Delete', 'wp-user-avatar') |
| 290 | ]; |
| 291 | |
| 292 | return $actions; |
| 293 | } |
| 294 | |
| 295 | public function process_bulk_action() |
| 296 | { |
| 297 | $subscription_id = absint(ppress_var($_GET, 'id', 0)); |
| 298 | |
| 299 | if ( ! current_user_can('manage_options')) return; |
| 300 | |
| 301 | if ('delete' === $this->current_action()) { |
| 302 | |
| 303 | check_admin_referer('pp_subscription_delete_rule'); |
| 304 | |
| 305 | if ( ! current_user_can('manage_options')) return; |
| 306 | |
| 307 | SubscriptionService::init()->delete_subscription($subscription_id); |
| 308 | } |
| 309 | |
| 310 | if ('bulk-delete' === $this->current_action()) { |
| 311 | |
| 312 | check_admin_referer('bulk-' . $this->_args['plural']); |
| 313 | |
| 314 | if ( ! current_user_can('manage_options')) return; |
| 315 | |
| 316 | $subscription_ids = array_map('absint', $_GET['subscription_id']); |
| 317 | |
| 318 | foreach ($subscription_ids as $subscription_id) { |
| 319 | SubscriptionService::init()->delete_subscription($subscription_id); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | if ($this->current_action() !== false) { |
| 324 | ppress_do_admin_redirect(PPRESS_MEMBERSHIP_SUBSCRIPTIONS_SETTINGS_PAGE); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | public function filter_bar() |
| 329 | { |
| 330 | $start_date = isset($_GET['start_date']) ? sanitize_text_field($_GET['start_date']) : null; |
| 331 | $end_date = isset($_GET['end_date']) ? sanitize_text_field($_GET['end_date']) : null; |
| 332 | $customer = isset($_GET['by_ci']) ? absint($_GET['by_ci']) : 'all'; |
| 333 | $membership_plan = isset($_GET['by_plan']) ? absint($_GET['by_plan']) : 'all'; |
| 334 | |
| 335 | $status = ppressGET_var('status'); |
| 336 | $clear_url = PPRESS_MEMBERSHIP_SUBSCRIPTIONS_SETTINGS_PAGE; |
| 337 | |
| 338 | echo '<div class="wp-filter" id="ppress-filters">'; |
| 339 | echo '<div class="filter-items">'; |
| 340 | ?> |
| 341 | |
| 342 | <span id="ppress-date-filters" class="ppress-from-to-wrapper"> |
| 343 | <span class="ppress-start-date-wrap"> |
| 344 | <input type="text" name="start_date" id="start-date" placeholder="<?= _x('From', 'date filter', 'wp-user-avatar') ?>" value="<?= $start_date ?>" class="ppress_datepicker"> |
| 345 | </span> |
| 346 | <span id="ppress-end-date-wrap"> |
| 347 | <input type="text" name="end_date" id="end-date" value="<?= $end_date ?>" placeholder="<?= _x('To', 'date filter', 'wp-user-avatar') ?>" class="ppress_datepicker"> |
| 348 | </span> |
| 349 | </span> |
| 350 | |
| 351 | <span id="ppress-customer-filter"> |
| 352 | <select name="by_ci" class="ppress-select2-field customer_user" style="min-width:180px"> |
| 353 | <option value="all"><?= esc_html__('All Customers', 'wp-user-avatar') ?></option> |
| 354 | <?php if ( ! empty($customer) && 'all' != $customer) : ?> |
| 355 | <option value="<?= $customer ?>" selected><?= CustomerFactory::fromId($customer)->get_name() ?></option> |
| 356 | <?php endif; ?> |
| 357 | </select> |
| 358 | </span> |
| 359 | |
| 360 | <span id="ppress-plans-filter"> |
| 361 | <select name="by_plan" class="ppress-select2-field membership_plan" style="min-width:180px"> |
| 362 | <option value="all"><?= esc_html__('All Membership Plans', 'wp-user-avatar') ?></option> |
| 363 | <?php if ( ! empty($membership_plan) && 'all' != $membership_plan) : ?> |
| 364 | <option value="<?= $membership_plan ?>" selected><?= ppress_get_plan($membership_plan)->get_name() ?></option> |
| 365 | <?php endif; ?> |
| 366 | </select> |
| 367 | </span> |
| 368 | |
| 369 | <span id="ppress-after-core-filters"> |
| 370 | <input type="submit" class="button button-secondary" value="<?php esc_html_e('Filter', 'wp-user-avatar'); ?>"/> |
| 371 | |
| 372 | <?php if ( ! empty($_GET['s']) || ! empty($start_date) || ! empty($end_date) || $customer !== 'all' || $membership_plan !== 'all') : ?> |
| 373 | <a href="<?php echo esc_url($clear_url); ?>" class="button-secondary"> |
| 374 | <?php esc_html_e('Clear', 'wp-user-avatar'); ?> |
| 375 | </a> |
| 376 | <?php endif; ?> |
| 377 | </span> |
| 378 | |
| 379 | <?php if ( ! empty($status)) : ?> |
| 380 | <input type="hidden" name="status" value="<?php echo esc_attr($status); ?>"/> |
| 381 | <?php endif; |
| 382 | echo '</div>'; |
| 383 | $this->search_box(__('Search Subscriptions', 'wp-user-avatar'), 'ppress_subscription_search'); |
| 384 | echo '</div>'; |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Returns markup for an subscription status badge. |
| 389 | * |
| 390 | * @param string $subscription_status |
| 391 | * |
| 392 | * @return string |
| 393 | * |
| 394 | */ |
| 395 | public static function get_subscription_status_badge($subscription_status) |
| 396 | { |
| 397 | ob_start(); |
| 398 | ?> |
| 399 | |
| 400 | <span class="ppress-admin-status-badge ppress-admin-status-badge--<?php echo esc_attr($subscription_status); ?>"> |
| 401 | <span class="ppress-admin-status-badge__text"> |
| 402 | <?php echo SubscriptionStatus::get_label($subscription_status); ?> |
| 403 | </span> |
| 404 | </span> |
| 405 | <?php return ob_get_clean(); |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * @return array List of CSS classes for the table tag. |
| 410 | */ |
| 411 | public function get_table_classes() |
| 412 | { |
| 413 | return array('widefat', 'fixed', 'striped', 'subscription', 'ppview'); |
| 414 | } |
| 415 | } |
| 416 |