modes = array( 'grid' => __('Grid View'), 'list' => __('List View'), ); $args = wp_parse_args($args, array( 'singular' => __('Customer', 'wp-ultimo'), // singular name of the listed records 'plural' => __('Customers', 'wp-ultimo'), // plural name of the listed records 'ajax' => true, // does this table support ajax? 'add_new' => array( 'url' => wu_get_form_url('add_new_customer'), 'classes' => 'wubox', ), )); parent::__construct($args); } // end __construct; /** * Adds the extra search field when the search element is present. * * @since 2.0.0 * @return array */ public function get_extra_query_fields() { $_filter_fields = parent::get_extra_query_fields(); $search = isset($_GET['s']) ? sanitize_text_field($_GET['s']) : false; if (!empty($search)) { // Search relevant users $user_ids = get_users(array( 'number' => -1, 'search' => '*' . $search . '*', 'fields' => 'ids', )); // No results, go back if (empty($user_ids)) { return $_filter_fields; } // end if; // Finally, include these user IDs in the customers query. $_filter_fields['user_id__in'] = $user_ids; unset($_filter_fields['search']); } // end if; $_filter_fields['type'] = 'customer'; if (wu_request('filter', 'all') === 'vip') { $_filter_fields['vip'] = 1; } elseif (wu_request('filter', 'all') === 'online') { $_filter_fields['last_login_query'] = array( 'after' => '-3 minutes', ); } // end if; return $_filter_fields; } // end get_extra_query_fields; /** * Displays the content of the name column. * * @since 2.0.0 * * @param WP_Ultimo\Models\Customer $item Customer object. */ public function column_name($item): string { // Get user info $user = get_user_by('id', $item->get_user_id()); $url_atts = array( 'id' => $item->get_id(), ); // Check if user exists if (!$user) { $actions = array( 'delete' => sprintf('%s', __('Delete', 'wp-ultimo'), wu_get_form_url('delete_modal', $url_atts), __('Delete', 'wp-ultimo')), ); return sprintf('#%s - %s', $item->get_user_id(), __('User not found', 'wp-ultimo')) . $this->row_actions($actions); } // end if; $customer_id = sprintf('#%s', $item->get_id(), $item->get_id()); $customer_user = sprintf('%s', wu_network_admin_url('wp-ultimo-edit-customer', array( 'id' => $item->get_id(), )), $user->display_name); // Concatenate the two blocks $title = "$customer_user"; $desc = sprintf('(%s)', wu_tooltip_text(__('Send an email to this customer', 'wp-ultimo')), $user->user_email, $user->user_email); // Concatenate switch to url $is_modal_switch_to = \WP_Ultimo\User_Switching::get_instance()->check_user_switching_is_activated() ? '' : 'wubox'; $url_switch_to = sprintf('%s', __('Switch To', 'wp-ultimo'), $is_modal_switch_to, \WP_Ultimo\User_Switching::get_instance()->render($item->get_user_id()), __('Switch To', 'wp-ultimo')); $actions = array( 'edit' => sprintf('%s', wu_network_admin_url('wp-ultimo-edit-customer', $url_atts), __('Edit', 'wp-ultimo')), 'switch-to' => $item->get_user_id() !== get_current_user_id() ? $url_switch_to : false, 'delete' => sprintf( '%s', __('Delete', 'wp-ultimo'), wu_get_form_url( 'delete_modal', array( 'model' => 'customer', 'id' => $item->get_id() ) ), __('Delete', 'wp-ultimo') ), ); $actions = array_filter($actions); return $title . $desc . $this->row_actions($actions); } // end column_name; /** * Displays the customer photo and special status. * * @since 2.0.0 * * @param WP_Ultimo\Models\Customer $item Customer object. * @return string */ public function column_customer_status($item) { $html = '
'; if ($item->is_vip()) { $html .= sprintf('%s', __('VIP', 'wp-ultimo')); } // end if; $html .= get_avatar($item->get_user_id(), 36, 'identicon', '', array( 'force_display' => true, )); $html .= '
'; return $html; } // end column_customer_status; /** * Returns the number of memberships owned by this customer. * * @since 2.0.0 * * @todo: Make this works. * @param WP_Ultimo\Models\Customer $item Customer object. */ public function column_memberships($item): string { $subscription_count = count($item->get_memberships()); $url_atts = array( 'customer_id' => $item->get_id(), ); $actions = array( 'view' => sprintf('%s', wu_network_admin_url('wp-ultimo-memberships', $url_atts), __('View', 'wp-ultimo')), ); return $subscription_count . $this->row_actions($actions); } // end column_memberships; /** * Returns the list of columns for this particular List Table. * * @since 2.0.0 * @return array */ public function get_columns() { $columns = array( 'cb' => '', 'customer_status' => '', 'name' => __('Name', 'wp-ultimo'), 'last_login' => __('Last Login', 'wp-ultimo'), 'date_registered' => __('Customer Since', 'wp-ultimo'), 'memberships' => __('Memberships', 'wp-ultimo'), 'id' => __('ID', 'wp-ultimo'), ); return $columns; } // end get_columns; /** * Handles the item display for grid mode. * * @since 2.0.0 * * @param WP_Ultimo\Models\Customer $item The line item being displayed. * @return void */ public function single_row_grid($item) { wu_get_template('base/customers/grid-item', array( 'item' => $item, 'table' => $this, )); } // end single_row_grid; /** * Returns the filters for this page. * * @since 2.0.0 */ public function get_filters(): array { $filters = $this->get_schema_columns(array( 'searchable' => true, 'date_query' => true, ), 'or'); $labels = $this->get_columns(); $filters = array_map(function($item) use ($labels) { $label = wu_get_isset($labels, $item->name); $label = $label ? sprintf('%s (%s)', $item->label, $item->name) : $item->name; $filter_type = 'text'; $rule = 'is'; if ($item->date_query === true) { $filter_type = 'date'; $rule = 'is_after'; } elseif (in_array(strtolower((string) $item->name), array('smallint'), true)) { $filter_type = 'bool'; $rule = 'is_true'; } // end if; return array( 'field' => $item->name, 'label' => $label, 'type' => $filter_type, 'rule' => $rule, 'value' => wu_request($item->name, ''), ); }, $filters); return array_values($filters); } // end get_filters; /** * Returns the pre-selected filters on the filter bar. * * @since 2.0.0 * @return array */ public function get_views() { return array( 'all' => array( 'field' => 'filter', 'url' => add_query_arg(array( 'filter' => 'all' )), 'label' => __('All Customers', 'wp-ultimo'), 'count' => 0, ), 'vip' => array( 'field' => 'filter', 'url' => add_query_arg('filter', 'vip'), 'label' => __('VIP Customers', 'wp-ultimo'), 'count' => 0, ), 'online' => array( 'field' => 'filter', 'url' => add_query_arg('filter', 'online'), 'label' => __('Online Customers', 'wp-ultimo'), 'count' => 0, ), ); } // end get_views; /** * Displays the last login. * * @since 2.0.0 * * @param WP_Ultimo\Models\Customer $item Customer object. * @return string The last login information. */ public function column_last_login($item) { if ($item->is_online()) { return '' . __('Online', 'wp-ultimo'); } // end if; return $this->_column_datetime($item->get_last_login()); } // end column_last_login; } // end class Customer_List_Table;