guard( 'manage_options' ); if ( $check ) { return $check; } $check = $this->guard_discount_pro(); if ( $check ) { return $check; } $per_page = max( 1, min( 100, $per_page ?? 20 ) ); $page = max( 1, $page ?? 1 ); $status = $status ?? 'any'; try { $args = [ 'post_status' => 'any', 'posts_per_page' => $per_page, 'paged' => $page, ]; if ( in_array( $status, [ 'active', 'inactive' ], true ) ) { // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- required for discount status filtering $args['meta_query'] = [ [ 'key' => 'discount_status', 'value' => $status, 'compare' => '=', ], ]; } $result = \WpCafePro\Models\Discount_Model::paginate( $args ); // Lean row shaper: full rules are heavy and deferred to wpcafe-get-discount. $items = array_map( [ $this, 'shape_discount_row' ], $result['items'] ); return [ 'items' => $items, 'total' => $result['total'] ?? 0, 'per_page' => $per_page, 'current_page' => $result['current_page'] ?? $page, 'last_page' => $result['last_page'] ?? 1, ]; } catch ( \Throwable $e ) { Log::error( 'Wpcafe_List_Discounts_Tool error', [ 'error' => $e->getMessage() ] ); return [ 'error' => 'Failed to list discounts: ' . $e->getMessage() ]; } } /** * Shape a Discount_Model into a lean LIST row (omits the full product_rule * and date_rule; use wpcafe-get-discount for those). * * @param \WpCafePro\Models\Discount_Model $discount Model instance. * @return array */ private function shape_discount_row( \WpCafePro\Models\Discount_Model $discount ): array { $product_rule = maybe_unserialize( $discount->product_rule ); $scope = is_array( $product_rule ) ? ( $product_rule['type'] ?? 'all_products' ) : 'all_products'; return [ 'id' => $discount->id, 'discount_title' => $discount->discount_title, 'discount_type' => $discount->discount_type, 'discount_amount' => floatval( $discount->discount_amount ), 'status' => $discount->discount_status, 'is_active' => (bool) $discount->is_active, 'product_scope' => $scope, ]; } }