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