GroupWPListTable.php
215 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Admin\SettingsPages\Membership\GroupsPage; |
| 4 | |
| 5 | use ProfilePress\Core\Admin\SettingsPages\Membership\PlansPage\PlanWPListTable; |
| 6 | use ProfilePress\Core\Membership\Models\Group\GroupEntity; |
| 7 | use ProfilePress\Core\Membership\Repositories\GroupRepository; |
| 8 | |
| 9 | class GroupWPListTable extends \WP_List_Table |
| 10 | { |
| 11 | public function __construct() |
| 12 | { |
| 13 | parent::__construct(array( |
| 14 | 'singular' => 'ppress-group-code', |
| 15 | 'plural' => 'ppress-group-codes', |
| 16 | 'ajax' => false |
| 17 | )); |
| 18 | } |
| 19 | |
| 20 | public function no_items() |
| 21 | { |
| 22 | _e('No group found.', 'wp-user-avatar'); |
| 23 | } |
| 24 | |
| 25 | public function get_columns() |
| 26 | { |
| 27 | return [ |
| 28 | 'cb' => '<input type="checkbox" />', |
| 29 | 'group_name' => esc_html__('Group Name', 'wp-user-avatar'), |
| 30 | 'group_plans' => esc_html__('Plans in Group', 'wp-user-avatar'), |
| 31 | 'checkout_url' => esc_html__('Checkout URL', 'wp-user-avatar') |
| 32 | ]; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Render the bulk edit checkbox |
| 37 | * |
| 38 | * @param GroupEntity $item |
| 39 | * |
| 40 | * @return string |
| 41 | */ |
| 42 | public function column_cb($item) |
| 43 | { |
| 44 | return sprintf('<input type="checkbox" name="group_id[]" value="%s" />', $item->id); |
| 45 | } |
| 46 | |
| 47 | public function column_group_name(GroupEntity $item) |
| 48 | { |
| 49 | $group_id = absint($item->id); |
| 50 | |
| 51 | $edit_link = esc_url(add_query_arg([ |
| 52 | 'ppress_group_action' => 'edit', |
| 53 | 'id' => $group_id |
| 54 | ], PPRESS_MEMBERSHIP_GROUPS_SETTINGS_PAGE)); |
| 55 | $delete_link = self::delete_group_url($group_id); |
| 56 | |
| 57 | $actions = [ |
| 58 | 'edit' => sprintf('<a href="%s">%s</a>', $edit_link, esc_html__('Edit', 'wp-user-avatar')), |
| 59 | ]; |
| 60 | |
| 61 | $actions['delete'] = sprintf('<a class="pp-confirm-delete" href="%s">%s</a>', $delete_link, esc_html__('Delete', 'wp-user-avatar')); |
| 62 | |
| 63 | $a = '<a href="' . $edit_link . '">' . esc_html($item->name) . '</a>'; |
| 64 | |
| 65 | return '<strong>' . $a . '</strong>' . $this->row_actions($actions); |
| 66 | } |
| 67 | |
| 68 | public function column_group_plans(GroupEntity $item) |
| 69 | { |
| 70 | $plans = $item->plan_ids; |
| 71 | |
| 72 | if (empty($plans)) return '———'; |
| 73 | |
| 74 | $plans = array_map(function ($plan_id) { |
| 75 | |
| 76 | $plan = ppress_get_plan($plan_id); |
| 77 | |
| 78 | return sprintf( |
| 79 | '<a target="_blank" href="%s">%s</a>', |
| 80 | $plan->get_edit_plan_url(), |
| 81 | $plan->get_name() |
| 82 | ); |
| 83 | |
| 84 | }, $plans); |
| 85 | |
| 86 | return sprintf('%s', implode(', ', $plans)); |
| 87 | } |
| 88 | |
| 89 | public function column_checkout_url(GroupEntity $item) |
| 90 | { |
| 91 | $url = $item->get_checkout_url(); |
| 92 | |
| 93 | if ( ! $url) return esc_html__('Checkout page not found', 'wp-user-avatar'); |
| 94 | |
| 95 | ob_start(); |
| 96 | ?> |
| 97 | <div style="display: flex; align-items: center; gap: 5px;"> |
| 98 | <input type="text" class="ppress-checkout-url" onfocus="this.select();" readonly="readonly" value="<?php echo esc_url($url); ?>" style="min-width: 200px;"/> |
| 99 | |
| 100 | <button type="button" class="button ppress-copy-url-icon" title="<?php esc_attr_e('Copy URL', 'wp-user-avatar'); ?>"> |
| 101 | <span class="dashicons dashicons-admin-page"></span> |
| 102 | </button> |
| 103 | <span class="ppress-copy-msg" style="display:none; color:green;"><?php esc_html_e('Copied!', 'wp-user-avatar'); ?></span> |
| 104 | </div> |
| 105 | <?php |
| 106 | return ob_get_clean(); |
| 107 | |
| 108 | } |
| 109 | |
| 110 | public static function delete_group_url($group_id) |
| 111 | { |
| 112 | return add_query_arg([ |
| 113 | 'action' => 'delete', |
| 114 | 'id' => $group_id, |
| 115 | '_wpnonce' => wp_create_nonce('pp_group_delete_rule') |
| 116 | ], PPRESS_MEMBERSHIP_GROUPS_SETTINGS_PAGE); |
| 117 | } |
| 118 | |
| 119 | public function get_groups($per_page, $current_page = 1) |
| 120 | { |
| 121 | return GroupRepository::init()->retrieveAll($per_page, $current_page); |
| 122 | } |
| 123 | |
| 124 | public function record_count() |
| 125 | { |
| 126 | return GroupRepository::init()->retrieveAll(0, 1, 'DESC', true); |
| 127 | } |
| 128 | |
| 129 | public function prepare_items() |
| 130 | { |
| 131 | $this->_column_headers = $this->get_column_info(); |
| 132 | |
| 133 | $this->process_bulk_action(); |
| 134 | |
| 135 | $per_page = $this->get_items_per_page('groups_per_page', 10); |
| 136 | $current_page = $this->get_pagenum(); |
| 137 | $total_items = $this->record_count(); |
| 138 | |
| 139 | $this->set_pagination_args([ |
| 140 | 'total_items' => $total_items, //WE have to calculate the total number of items |
| 141 | 'per_page' => $per_page //WE have to determine how many items to show on a page |
| 142 | ]); |
| 143 | |
| 144 | $this->items = $this->get_groups($per_page, $current_page); |
| 145 | } |
| 146 | |
| 147 | public function current_action() |
| 148 | { |
| 149 | if (isset($_REQUEST['filter_action']) && ! empty($_REQUEST['filter_action'])) { |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | if (isset($_REQUEST['action']) && -1 != $_REQUEST['action']) { |
| 154 | return $_REQUEST['action']; |
| 155 | } |
| 156 | |
| 157 | if (isset($_REQUEST['ppress_group_action']) && -1 != $_REQUEST['ppress_group_action']) { |
| 158 | return $_REQUEST['ppress_group_action']; |
| 159 | } |
| 160 | |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | public function get_bulk_actions() |
| 165 | { |
| 166 | $actions = [ |
| 167 | 'bulk-delete' => esc_html__('Delete', 'wp-user-avatar') |
| 168 | ]; |
| 169 | |
| 170 | return $actions; |
| 171 | } |
| 172 | |
| 173 | public function process_bulk_action() |
| 174 | { |
| 175 | $group_id = absint(ppress_var($_GET, 'id', 0)); |
| 176 | |
| 177 | // Bail if user is not an admin or without admin privileges. |
| 178 | if ( ! current_user_can('manage_options')) return; |
| 179 | |
| 180 | if ('delete' === $this->current_action()) { |
| 181 | |
| 182 | check_admin_referer('pp_group_delete_rule'); |
| 183 | |
| 184 | if ( ! current_user_can('manage_options')) return; |
| 185 | |
| 186 | GroupRepository::init()->delete($group_id); |
| 187 | } |
| 188 | |
| 189 | if ('bulk-delete' === $this->current_action()) { |
| 190 | |
| 191 | check_admin_referer('bulk-' . $this->_args['plural']); |
| 192 | |
| 193 | if ( ! current_user_can('manage_options')) return; |
| 194 | |
| 195 | $group_ids = array_map('absint', $_POST['group_id']); |
| 196 | |
| 197 | foreach ($group_ids as $group_id) { |
| 198 | GroupRepository::init()->delete($group_id); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | if ($this->current_action() !== false) { |
| 203 | ppress_do_admin_redirect(PPRESS_MEMBERSHIP_GROUPS_SETTINGS_PAGE); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * @return array List of CSS classes for the table tag. |
| 209 | */ |
| 210 | public function get_table_classes() |
| 211 | { |
| 212 | return array('widefat', 'fixed', 'striped', 'group', 'ppview'); |
| 213 | } |
| 214 | } |
| 215 |