PlanWPListTable.php
336 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Admin\SettingsPages\Membership\PlansPage; |
| 4 | |
| 5 | use ProfilePress\Core\Membership\Controllers\SubscriptionPlanController; |
| 6 | use ProfilePress\Core\Membership\Models\Plan\PlanEntity; |
| 7 | use ProfilePress\Core\Membership\Repositories\PlanRepository; |
| 8 | |
| 9 | class PlanWPListTable extends \WP_List_Table |
| 10 | { |
| 11 | private $views_count = []; |
| 12 | |
| 13 | public function __construct() |
| 14 | { |
| 15 | parent::__construct(array( |
| 16 | 'singular' => 'ppress-membership-plan', |
| 17 | 'plural' => 'ppress-membership-plans', |
| 18 | 'ajax' => false |
| 19 | )); |
| 20 | |
| 21 | $this->views_count['all'] = PlanRepository::init()->record_count(); |
| 22 | $this->views_count['active'] = PlanRepository::init()->get_count_by_status('true'); |
| 23 | $this->views_count['inactive'] = PlanRepository::init()->get_count_by_status('false'); |
| 24 | } |
| 25 | |
| 26 | public function no_items() |
| 27 | { |
| 28 | _e('No membership plan found.', 'wp-user-avatar'); |
| 29 | } |
| 30 | |
| 31 | public function get_columns() |
| 32 | { |
| 33 | $columns = [ |
| 34 | 'cb' => '<input type="checkbox" />', |
| 35 | 'name' => esc_html__('Plan Name', 'wp-user-avatar'), |
| 36 | 'billing_details' => esc_html__('Billing Details', 'wp-user-avatar'), |
| 37 | 'checkout_url' => esc_html__('Checkout URL', 'wp-user-avatar'), |
| 38 | 'status' => esc_html__('Status', 'wp-user-avatar'), |
| 39 | ]; |
| 40 | |
| 41 | return $columns; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Render the bulk edit checkbox |
| 46 | * |
| 47 | * @param PlanEntity $item |
| 48 | * |
| 49 | * @return string |
| 50 | */ |
| 51 | public function column_cb($item) |
| 52 | { |
| 53 | return sprintf('<input type="checkbox" name="plan_id[]" value="%s" />', $item->id); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | public function column_name(PlanEntity $item) |
| 58 | { |
| 59 | $plan_id = absint($item->id); |
| 60 | |
| 61 | $is_active = $item->is_active(); |
| 62 | |
| 63 | $edit_link = add_query_arg(['ppress_subp_action' => 'edit', 'id' => $plan_id], PPRESS_MEMBERSHIP_SUBSCRIPTION_PLANS_SETTINGS_PAGE); |
| 64 | $duplicate_link = add_query_arg(['ppress_subp_action' => 'duplicate', 'id' => $plan_id, '_wpnonce' => wp_create_nonce('pp_subscription_plan_duplicate_rule')], PPRESS_MEMBERSHIP_SUBSCRIPTION_PLANS_SETTINGS_PAGE); |
| 65 | $activate_link = add_query_arg(['ppress_subp_action' => 'activate', 'id' => $plan_id, '_wpnonce' => wp_create_nonce('pp_subscription_plan_activate_rule')], PPRESS_MEMBERSHIP_SUBSCRIPTION_PLANS_SETTINGS_PAGE); |
| 66 | $deactivate_link = add_query_arg(['ppress_subp_action' => 'deactivate', 'id' => $plan_id, '_wpnonce' => wp_create_nonce('pp_subscription_plan_deactivate_rule')], PPRESS_MEMBERSHIP_SUBSCRIPTION_PLANS_SETTINGS_PAGE); |
| 67 | $delete_link = self::delete_plan_url($plan_id); |
| 68 | |
| 69 | $actions = [ |
| 70 | 'id' => sprintf(__('ID: %d', 'wp-user-avatar'), $plan_id), |
| 71 | 'edit' => sprintf('<a href="%s">%s</a>', $edit_link, esc_html__('Edit', 'wp-user-avatar')), |
| 72 | 'duplicate' => sprintf('<a href="%s">%s</a>', $duplicate_link, esc_html__('Duplicate', 'wp-user-avatar')) |
| 73 | ]; |
| 74 | |
| 75 | if (true === $is_active) { |
| 76 | $actions['deactivate'] = sprintf('<a href="%s">%s</a>', $deactivate_link, esc_html__('Deactivate', 'wp-user-avatar')); |
| 77 | } |
| 78 | |
| 79 | if (false === $is_active) { |
| 80 | $actions['activate'] = sprintf('<a href="%s">%s</a>', $activate_link, esc_html__('Activate', 'wp-user-avatar')); |
| 81 | } |
| 82 | |
| 83 | $actions['delete'] = sprintf('<a class="pp-confirm-delete" href="%s">%s</a>', $delete_link, esc_html__('Delete', 'wp-user-avatar')); |
| 84 | |
| 85 | $a = '<a href="' . $edit_link . '">' . esc_html($item->name) . '</a>'; |
| 86 | |
| 87 | return '<strong>' . $a . '</strong>' . $this->row_actions($actions); |
| 88 | } |
| 89 | |
| 90 | public function column_billing_details(PlanEntity $item) |
| 91 | { |
| 92 | $billing_data = wp_json_encode(apply_filters('ppress_admin_membership_plan_billing_data', [ |
| 93 | 'price' => ppress_sanitize_amount($item->price), |
| 94 | 'billing_frequency' => sanitize_text_field($item->billing_frequency), |
| 95 | 'total_payments' => absint($item->total_payments), |
| 96 | 'signup_fee' => ppress_sanitize_amount($item->signup_fee), |
| 97 | 'subscription_length' => sanitize_text_field($item->subscription_length), |
| 98 | 'free_trial' => sanitize_text_field($item->free_trial), |
| 99 | ], $item)); |
| 100 | |
| 101 | printf('<div class="ppress-plan-billing-details" data-billing-details="%s"></div>', esc_attr($billing_data)); |
| 102 | } |
| 103 | |
| 104 | public function column_checkout_url(PlanEntity $item) |
| 105 | { |
| 106 | $url = ppress_plan_checkout_url($item->id); |
| 107 | |
| 108 | if ( ! $url) return esc_html__('Checkout page not found', 'wp-user-avatar'); |
| 109 | |
| 110 | ob_start(); |
| 111 | ?> |
| 112 | <div style="display: flex; align-items: center; gap: 5px;"> |
| 113 | <input type="text" class="ppress-checkout-url" onfocus="this.select();" readonly="readonly" value="<?php echo esc_url($url); ?>" style="min-width: 200px;"/> |
| 114 | |
| 115 | <button type="button" class="button ppress-copy-url-icon" title="<?php esc_attr_e('Copy URL', 'wp-user-avatar'); ?>"> |
| 116 | <span class="dashicons dashicons-admin-page"></span> |
| 117 | </button> |
| 118 | <span class="ppress-copy-msg" style="display:none; color:green;"><?php esc_html_e('Copied!', 'wp-user-avatar'); ?></span> |
| 119 | </div> |
| 120 | <?php |
| 121 | return ob_get_clean(); |
| 122 | } |
| 123 | |
| 124 | public function column_status(PlanEntity $item) |
| 125 | { |
| 126 | $status = sprintf('<span class="dashicons-before dashicons-yes">%s</span>', esc_html__('Active', 'wp-user-avatar')); |
| 127 | if ( ! $item->is_active()) { |
| 128 | $status = sprintf('<span class="dashicons-before dashicons-no-alt">%s</span>', esc_html__('Inactive', 'wp-user-avatar')); |
| 129 | } |
| 130 | |
| 131 | return $status; |
| 132 | } |
| 133 | |
| 134 | public static function delete_plan_url($plan_id) |
| 135 | { |
| 136 | $nonce_delete = wp_create_nonce('pp_subscription_plan_delete_rule'); |
| 137 | |
| 138 | return add_query_arg(['action' => 'delete', 'id' => $plan_id, '_wpnonce' => $nonce_delete], PPRESS_MEMBERSHIP_SUBSCRIPTION_PLANS_SETTINGS_PAGE); |
| 139 | } |
| 140 | |
| 141 | public function get_plans($per_page, $current_page = 1) |
| 142 | { |
| 143 | $status = ppressGET_var('status', 'all'); |
| 144 | $status_map = ['active' => 'true', 'inactive' => 'false']; |
| 145 | |
| 146 | return PlanRepository::init()->retrieveAll($per_page, $current_page, $status_map[$status] ?? ''); |
| 147 | } |
| 148 | |
| 149 | public function record_count() |
| 150 | { |
| 151 | $status = ppressGET_var('status', 'all'); |
| 152 | |
| 153 | if ($status != 'all' && isset($this->views_count[$status])) { |
| 154 | return $this->views_count[$status]; |
| 155 | } |
| 156 | |
| 157 | return PlanRepository::init()->record_count(); |
| 158 | } |
| 159 | |
| 160 | public function prepare_items() |
| 161 | { |
| 162 | $this->_column_headers = $this->get_column_info(); |
| 163 | |
| 164 | $this->process_bulk_action(); |
| 165 | |
| 166 | $per_page = $this->get_items_per_page('plans_per_page', 10); |
| 167 | $current_page = $this->get_pagenum(); |
| 168 | $total_items = $this->record_count(); |
| 169 | |
| 170 | $this->set_pagination_args([ |
| 171 | 'total_items' => $total_items, //WE have to calculate the total number of items |
| 172 | 'per_page' => $per_page //WE have to determine how many items to show on a page |
| 173 | ]); |
| 174 | |
| 175 | $this->items = $this->get_plans($per_page, $current_page); |
| 176 | } |
| 177 | |
| 178 | public function current_action() |
| 179 | { |
| 180 | if (isset($_REQUEST['filter_action']) && ! empty($_REQUEST['filter_action'])) { |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | if (isset($_REQUEST['action']) && -1 != $_REQUEST['action']) { |
| 185 | return $_REQUEST['action']; |
| 186 | } |
| 187 | |
| 188 | if (isset($_REQUEST['ppress_subp_action']) && -1 != $_REQUEST['ppress_subp_action']) { |
| 189 | return $_REQUEST['ppress_subp_action']; |
| 190 | } |
| 191 | |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | public function get_bulk_actions() |
| 196 | { |
| 197 | $actions = [ |
| 198 | 'bulk-delete' => esc_html__('Delete', 'wp-user-avatar'), |
| 199 | 'bulk-activate' => esc_html__('Activate', 'wp-user-avatar'), |
| 200 | 'bulk-deactivate' => esc_html__('Deactivate', 'wp-user-avatar') |
| 201 | ]; |
| 202 | |
| 203 | return $actions; |
| 204 | } |
| 205 | |
| 206 | public function process_bulk_action() |
| 207 | { |
| 208 | $plan_id = absint(ppress_var($_GET, 'id', 0)); |
| 209 | |
| 210 | $planObj = ppress_get_plan($plan_id); |
| 211 | |
| 212 | // Bail if user is not an admin or without admin privileges. |
| 213 | if ( ! current_user_can('manage_options')) return; |
| 214 | |
| 215 | if ('deactivate' === $this->current_action()) { |
| 216 | |
| 217 | check_admin_referer('pp_subscription_plan_deactivate_rule'); |
| 218 | |
| 219 | if ( ! current_user_can('manage_options')) return; |
| 220 | |
| 221 | SubscriptionPlanController::get_instance()->deactivate_plan($planObj); |
| 222 | } |
| 223 | |
| 224 | if ('activate' === $this->current_action()) { |
| 225 | |
| 226 | check_admin_referer('pp_subscription_plan_activate_rule'); |
| 227 | |
| 228 | if ( ! current_user_can('manage_options')) return; |
| 229 | |
| 230 | SubscriptionPlanController::get_instance()->activate_plan($planObj); |
| 231 | } |
| 232 | |
| 233 | if ('delete' === $this->current_action()) { |
| 234 | |
| 235 | check_admin_referer('pp_subscription_plan_delete_rule'); |
| 236 | |
| 237 | if ( ! current_user_can('manage_options')) return; |
| 238 | |
| 239 | SubscriptionPlanController::get_instance()->delete_plan($planObj); |
| 240 | } |
| 241 | |
| 242 | if ('duplicate' === $this->current_action()) { |
| 243 | |
| 244 | check_admin_referer('pp_subscription_plan_duplicate_rule'); |
| 245 | |
| 246 | if ( ! current_user_can('manage_options')) return; |
| 247 | |
| 248 | $dup_plan_id = SubscriptionPlanController::get_instance()->duplicate_plan($planObj); |
| 249 | |
| 250 | if (is_int($dup_plan_id)) { |
| 251 | ppress_do_admin_redirect(add_query_arg(['ppress_subp_action' => 'edit', 'id' => $dup_plan_id, 'saved' => 'true'], PPRESS_MEMBERSHIP_SUBSCRIPTION_PLANS_SETTINGS_PAGE)); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | if ('bulk-delete' === $this->current_action()) { |
| 256 | |
| 257 | check_admin_referer('bulk-' . $this->_args['plural']); |
| 258 | |
| 259 | if ( ! current_user_can('manage_options')) return; |
| 260 | |
| 261 | $plan_ids = array_map('absint', $_POST['plan_id']); |
| 262 | |
| 263 | foreach ($plan_ids as $plan_id) { |
| 264 | |
| 265 | SubscriptionPlanController::get_instance()->delete_plan($plan_id); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | if ('bulk-activate' === $this->current_action()) { |
| 270 | |
| 271 | check_admin_referer('bulk-' . $this->_args['plural']); |
| 272 | |
| 273 | if ( ! current_user_can('manage_options')) return; |
| 274 | |
| 275 | $plan_ids = array_map('absint', $_POST['plan_id']); |
| 276 | |
| 277 | foreach ($plan_ids as $plan_id) { |
| 278 | |
| 279 | SubscriptionPlanController::get_instance()->activate_plan($plan_id); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | if ('bulk-deactivate' === $this->current_action()) { |
| 284 | |
| 285 | check_admin_referer('bulk-' . $this->_args['plural']); |
| 286 | |
| 287 | if ( ! current_user_can('manage_options')) return; |
| 288 | |
| 289 | $plan_ids = array_map('absint', $_POST['plan_id']); |
| 290 | |
| 291 | foreach ($plan_ids as $plan_id) { |
| 292 | |
| 293 | SubscriptionPlanController::get_instance()->deactivate_plan($plan_id); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | if ($this->current_action() !== false) { |
| 298 | ppress_do_admin_redirect(PPRESS_MEMBERSHIP_SUBSCRIPTION_PLANS_SETTINGS_PAGE); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * @return array List of CSS classes for the table tag. |
| 304 | */ |
| 305 | public function get_table_classes() |
| 306 | { |
| 307 | return array('widefat', 'fixed', 'striped', 'subscription_plan', 'ppview'); |
| 308 | } |
| 309 | |
| 310 | public function get_views() |
| 311 | { |
| 312 | $views = []; |
| 313 | |
| 314 | $args = [ |
| 315 | 'all' => esc_html__('All', 'wp-user-avatar'), |
| 316 | 'active' => esc_html__('Active', 'wp-user-avatar'), |
| 317 | 'inactive' => esc_html__('Inactive', 'wp-user-avatar'), |
| 318 | ]; |
| 319 | |
| 320 | foreach ($args as $id => $status) { |
| 321 | |
| 322 | $url = $id == 'all' ? PPRESS_MEMBERSHIP_SUBSCRIPTION_PLANS_SETTINGS_PAGE : add_query_arg(['status' => $id], PPRESS_MEMBERSHIP_SUBSCRIPTION_PLANS_SETTINGS_PAGE); |
| 323 | |
| 324 | $views[$id] = sprintf( |
| 325 | '<a href="%s"%s>%s <span class="count">(%s)</span></a>', |
| 326 | $url, |
| 327 | ppressGET_var('status', 'all') == $id ? ' class="current"' : '', |
| 328 | $status, |
| 329 | $this->views_count[$id] |
| 330 | ); |
| 331 | } |
| 332 | |
| 333 | return $views; |
| 334 | } |
| 335 | } |
| 336 |