label). */ private array $all_columns = array(); public static function instance(): self { if ( null === self::$instance ) { self::$instance = new self(); self::$instance->hooks(); } return self::$instance; } private function __construct() { $this->all_columns = array( 'seller' => '', 'category' => '', 'price' => '', 'item_condition' => '', 'location' => '', 'status' => '', 'date' => '', ); } private function hooks(): void { add_action( 'admin_init', array( $this, 'handle_actions' ) ); // Register columns for Screen Options panel (column-visibility checkboxes). add_filter( 'manage_' . $this->screen_id . '_columns', array( $this, 'get_columns' ) ); // Save per-page screen option. add_filter( 'set_screen_option_arshid6social_mkt_per_page', array( $this, 'save_per_page' ), 10, 3 ); add_filter( 'set-screen-option', array( $this, 'save_per_page_legacy' ), 10, 3 ); } // ── Screen Options ──────────────────────────────────────────────────────── /** * Called on load-{hook} — registers the per-page Screen Option. * Column checkboxes are registered automatically via the columns filter. */ public function setup_screen_options(): void { add_screen_option( 'per_page', array( 'label' => __( 'Listings per page', '6arshid-social-community' ), 'default' => 20, 'option' => 'arshid6social_mkt_per_page', ) ); } /** * Returns all available columns (used by WP to build Screen Options checkboxes). * * @return array */ public function get_columns(): array { return array( 'cb' => '', 'title' => __( 'Title', '6arshid-social-community' ), 'seller' => __( 'Seller', '6arshid-social-community' ), 'category' => __( 'Category', '6arshid-social-community' ), 'price' => __( 'Price', '6arshid-social-community' ), 'item_condition' => __( 'Condition', '6arshid-social-community' ), 'location' => __( 'Location', '6arshid-social-community' ), 'status' => __( 'Status', '6arshid-social-community' ), 'date' => __( 'Date', '6arshid-social-community' ), ); } /** Saves the per-page value (WP 5.4.2+). */ public function save_per_page( $status, string $option, $value ): int { return (int) $value; } /** Saves the per-page value (WP < 5.4.2 compatibility). */ public function save_per_page_legacy( $status, string $option, $value ) { if ( 'arshid6social_mkt_per_page' === $option ) { return (int) $value; } return $status; } // ── Action handler ──────────────────────────────────────────────────────── /** * Processes bulk actions and single-row actions before output. */ public function handle_actions(): void { if ( ! isset( $_GET['page'] ) || 'arshid6social-marketplace' !== $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification return; } if ( ! current_user_can( 'arshid6social_manage_settings' ) ) { return; } // Single-row action. if ( ! empty( $_GET['action'] ) && ! empty( $_GET['listing_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification $action = sanitize_key( wp_unslash( $_GET['action'] ) ); // phpcs:ignore WordPress.Security.NonceVerification $listing_id = absint( $_GET['listing_id'] ); // phpcs:ignore WordPress.Security.NonceVerification if ( ! check_admin_referer( 'arshid6social_mkt_' . $action . '_' . $listing_id ) ) { wp_die( esc_html__( 'Security check failed.', '6arshid-social-community' ) ); } $this->process_single_action( $action, $listing_id ); wp_safe_redirect( remove_query_arg( array( 'action', 'listing_id', '_wpnonce' ) ) ); exit; } // Bulk action. if ( ! empty( $_POST['action'] ) && ! empty( $_POST['listing_ids'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification if ( ! check_admin_referer( 'arshid6social_mkt_bulk', '_wpnonce_bulk' ) ) { wp_die( esc_html__( 'Security check failed.', '6arshid-social-community' ) ); } $action = sanitize_key( wp_unslash( $_POST['action'] ) ); // phpcs:ignore WordPress.Security.NonceVerification $listing_ids = array_map( 'absint', (array) $_POST['listing_ids'] ); // phpcs:ignore WordPress.Security.NonceVerification foreach ( $listing_ids as $id ) { $this->process_single_action( $action, $id ); } wp_safe_redirect( remove_query_arg( array( 'action', 'listing_ids', '_wpnonce_bulk' ) ) ); exit; } } // ── Render ──────────────────────────────────────────────────────────────── public function render(): void { if ( ! current_user_can( 'arshid6social_manage_settings' ) ) { wp_die( esc_html__( 'You do not have permission to access this page.', '6arshid-social-community' ) ); } global $wpdb; // ── Screen Options values ───────────────────────────────────────────── $per_page = (int) get_user_option( 'arshid6social_mkt_per_page' ); if ( $per_page < 1 ) { $per_page = 20; } $screen = get_current_screen(); $hidden_columns = $screen ? get_hidden_columns( $screen ) : array(); // ── Request params ──────────────────────────────────────────────────── $search = isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification $status_filter = isset( $_GET['status'] ) ? sanitize_key( wp_unslash( $_GET['status'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification $paged = isset( $_GET['paged'] ) ? max( 1, absint( $_GET['paged'] ) ) : 1; // phpcs:ignore WordPress.Security.NonceVerification // ── Build WHERE ─────────────────────────────────────────────────────── $where = 'WHERE 1=1'; $params = array(); if ( $status_filter && in_array( $status_filter, array( 'active', 'draft', 'archived', 'expired' ), true ) ) { $where .= ' AND l.status = %s'; $params[] = $status_filter; } if ( $search ) { $where .= ' AND (l.title LIKE %s OR u.display_name LIKE %s OR u.user_email LIKE %s)'; $like = '%' . $wpdb->esc_like( $search ) . '%'; $params[] = $like; $params[] = $like; $params[] = $like; } // ── Count per status ────────────────────────────────────────────────── // phpcs:ignore WordPress.DB.DirectDatabaseQuery $status_counts_raw = $wpdb->get_results( "SELECT status, COUNT(*) AS cnt FROM {$wpdb->prefix}arshid6social_listings GROUP BY status" ); $counts = array( 'all' => 0 ); foreach ( $status_counts_raw as $row ) { $counts[ $row->status ] = (int) $row->cnt; $counts['all'] += (int) $row->cnt; } // ── Total for pagination ────────────────────────────────────────────── // phpcs:ignore WordPress.DB.DirectDatabaseQuery,WordPress.DB.PreparedSQL.InterpolatedNotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter,WordPress.DB.PreparedSQLPlaceholders $total = (int) $wpdb->get_var( $wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQLPlaceholders "SELECT COUNT(*) FROM {$wpdb->prefix}arshid6social_listings l LEFT JOIN {$wpdb->users} u ON u.ID = l.seller_id $where", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter,WordPress.DB.PreparedSQLPlaceholders -- placeholders supplied via dynamic $where + spread $params. ...$params ) ); $total_pages = (int) ceil( $total / $per_page ); $offset = ( $paged - 1 ) * $per_page; // ── Fetch rows ──────────────────────────────────────────────────────── $select = "SELECT l.id, l.title, l.status, l.price, l.is_free, l.item_condition, l.location_city, l.location_country, l.created_at, l.category_id, u.display_name AS seller_name, u.user_email AS seller_email, c.name AS category_name FROM {$wpdb->prefix}arshid6social_listings l LEFT JOIN {$wpdb->users} u ON u.ID = l.seller_id LEFT JOIN {$wpdb->prefix}arshid6social_categories c ON c.id = l.category_id $where ORDER BY l.created_at DESC LIMIT %d OFFSET %d"; $row_params = array_merge( $params, array( $per_page, $offset ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery,WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQLPlaceholders,WordPress.DB.PreparedSQL.InterpolatedNotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter,WordPress.DB.PreparedSQLPlaceholders $listings = $wpdb->get_results( $wpdb->prepare( $select, ...$row_params ) ); // ── Helpers ─────────────────────────────────────────────────────────── $status_labels = array( 'active' => __( 'Active', '6arshid-social-community' ), 'draft' => __( 'Draft', '6arshid-social-community' ), 'archived' => __( 'Archived', '6arshid-social-community' ), 'expired' => __( 'Expired', '6arshid-social-community' ), ); $current_url = admin_url( 'admin.php?page=arshid6social-marketplace' ); // Helper: return 'hidden' class string if column should be hidden. $col_class = fn( string $col ) => in_array( $col, $hidden_columns, true ) ? ' hidden' : ''; ?>


render_tablenav( 'top', $total, $total_pages, $paged ); ?> 'delete', 'listing_id' => $listing->id, ), $current_url ), 'arshid6social_mkt_delete_' . $listing->id ); $archive_url = wp_nonce_url( add_query_arg( array( 'action' => 'set_archived', 'listing_id' => $listing->id, ), $current_url ), 'arshid6social_mkt_set_archived_' . $listing->id ); $activate_url = wp_nonce_url( add_query_arg( array( 'action' => 'set_active', 'listing_id' => $listing->id, ), $current_url ), 'arshid6social_mkt_set_active_' . $listing->id ); $status_color = match ( $listing->status ) { 'active' => '#00a32a', 'draft' => '#9ea3a8', 'expired' => '#d63638', 'archived' => '#8c8f94', default => '#9ea3a8', }; $location = trim( implode( ', ', array_filter( array( $listing->location_city, strtoupper( $listing->location_country ) ) ) ) ); $price = $listing->is_free ? __( 'Free', '6arshid-social-community' ) : \Arshid6Social\Components\Marketplace\Marketplace::format_price( $listing->price ); ?>
title ?: __( '(no title)', '6arshid-social-community' ) ); ?>
status ) : ?> | status ) : ?> |
seller_name ) : ?> seller_name ); ?> category_name ?: '—' ); ?> item_condition ) ); ?> status ] ?? ucfirst( $listing->status ) ); ?> created_at ) ) ); ?>
render_tablenav( 'bottom', $total, $total_pages, $paged ); ?>
1 ) : ?> add_query_arg( 'paged', '%#%' ), 'format' => '', 'current' => $paged, 'total' => $total_pages, 'prev_text' => '«', 'next_text' => '»', ) ) ); ?>

delete( "{$wpdb->prefix}arshid6social_listing_media", array( 'listing_id' => $listing_id ), array( '%d' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery $wpdb->delete( "{$wpdb->prefix}arshid6social_listings", array( 'id' => $listing_id ), array( '%d' ) ); return; } $new_status = match ( $action ) { 'set_active' => 'active', 'set_archived' => 'archived', default => null, }; if ( $new_status ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery $wpdb->update( "{$wpdb->prefix}arshid6social_listings", array( 'status' => $new_status, 'updated_at' => current_time( 'mysql' ), ), array( 'id' => $listing_id ), array( '%s', '%s' ), array( '%d' ) ); } } }