Lesson 7: Building the Auction Manager – Inserting and Managing Auction Records

Introduction

In the previous lesson, we created our first custom database table using WordPress’s dbDelta() function. Although the table exists, it is currently empty. Our plugin has a place to store auction data, but it doesn’t yet know how to create, retrieve, update, or delete auction records.

This is where an Auction Manager comes in.

Rather than scattering database queries throughout our plugin, we’ll create a dedicated class responsible for interacting with the auction table. This approach keeps our code organized, easier to maintain, and easier to extend as new features are added.

In this lesson, we’ll build the first version of the Auction Manager and learn how to insert our first auction into the database.


Learning Objectives

By the end of this lesson, you’ll be able to:

  • Understand the purpose of an Auction Manager.
  • Create a dedicated class for database operations.
  • Insert records into the database using WordPress’s $wpdb.
  • Understand how $wpdb->insert() works.
  • Keep database logic separate from the rest of your plugin.

Why Create an Auction Manager?

Imagine writing database queries everywhere throughout the plugin.

Soon you’d have SQL scattered across dozens of files.

Instead, we’ll centralize all auction-related database operations inside one class.

Our future Auction Manager will eventually handle:

  • Creating auctions
  • Updating auctions
  • Retrieving auctions
  • Deleting auctions
  • Ending auctions
  • Selecting winners
  • Checking auction status

Keeping all these operations together makes the plugin much easier to maintain.


Step 1 – Create a New Class

Inside the includes folder, create:

class-auction-manager.php

Step 2 – Create the Class

Copy the following code into the file:

<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Flipnzee_Auction_Manager {

	/**
	 * Create a new auction.
	 *
	 * @param int   $listing_id    The ID of the listing from Flipnzee Analytics.
	 * @param float $start_price   Starting auction price.
	 * @param float $reserve_price Reserve price.
	 * @param float $buy_now_price Buy Now price.
	 *
	 * @return int|false Auction ID on success, false on failure.
	 */
	public static function create_auction( $listing_id, $start_price, $reserve_price, $buy_now_price ) {

		global $wpdb;

		$table = $wpdb->prefix . 'flipnzee_auctions';

		$result = $wpdb->insert(
			$table,
			array(
				'listing_id'    => $listing_id,
				'status'        => 'draft',
				'start_price'   => $start_price,
				'reserve_price' => $reserve_price,
				'buy_now_price' => $buy_now_price,
				'current_bid'   => 0,
			),
			array(
				'%d',
				'%s',
				'%f',
				'%f',
				'%f',
				'%f',
			)
		);

		if ( false === $result ) {
			return false;
		}

		return $wpdb->insert_id;
	}
}

Understanding $wpdb->insert()

Instead of writing raw SQL, WordPress provides helper methods.

Our code:

$wpdb->insert(...)

is equivalent to writing an SQL INSERT statement, but it’s safer and easier to read.

WordPress also prepares the query correctly, reducing the risk of SQL injection.


Understanding the Parameters

The method receives three arguments:

1. Table Name

$table

This tells WordPress where to insert the data.


2. Data Array

array(
    'listing_id' => $listing_id,
    ...
)

Each array key corresponds to a database column.


3. Format Array

array(
    '%d',
    '%s',
    '%f'
)

This tells WordPress what type of data is being inserted.

Examples include:

  • %d Integer
  • %s String
  • %f Decimal

Providing formats makes the query safer and more reliable.


Step 3 – Load the Auction Manager

Open:

flipnzee-auctions.php

Below the database class, add:

/**
 * Load Auction Manager
 */
if ( file_exists( FLIPNZEE_AUCTION_PATH . 'includes/class-auction-manager.php' ) ) {
	require_once FLIPNZEE_AUCTION_PATH . 'includes/class-auction-manager.php';
}

Step 4 – Testing the Class

For now, we’re only building the class.

We won’t yet create auctions from the WordPress dashboard.

That will happen in a later lesson when we build the administration interface.

At this stage, our goal is simply to prepare the plugin architecture.


Why We Aren’t Creating Auctions Yet

Many beginners expect to see forms immediately.

Professional software development works differently.

First we build:

  • Database
  • Database classes
  • Business logic

Only then do we build:

  • Admin pages
  • Forms
  • Buttons
  • User interfaces

This layered approach produces cleaner software.


Looking Ahead

Eventually our Auction Manager will grow to include methods such as:

create_auction()

get_auction()

update_auction()

delete_auction()

close_auction()

select_winner()

Today’s version is intentionally simple.

We’ll expand it gradually throughout the series.


Lesson Summary

In this lesson, we created the first version of the Auction Manager class. Rather than placing database queries throughout the plugin, we introduced a dedicated class responsible for creating auction records.

Although the class currently performs only one operation, it establishes the architectural pattern we’ll use for all future auction-related functionality.


Key Takeaways

  • ✓ Keep database logic inside dedicated classes.
  • ✓ Use $wpdb->insert() instead of writing raw SQL.
  • ✓ Store one responsibility in one class.
  • ✓ Load new classes through the main plugin file.
  • ✓ Build the backend before building the user interface.

Common Mistakes

  • Writing SQL throughout multiple files.
  • Forgetting the format array in $wpdb->insert().
  • Mixing HTML and database code together.
  • Trying to build the admin interface before the backend is ready.

Git Commands Used

git add .

git commit -m "Lesson 7: Create Auction Manager"

git push

Project Status

✅ Development environment

✅ Plugin skeleton

✅ Plugin installation

✅ Plugin lifecycle

✅ .gitignore

✅ Data architecture

✅ Database table

✅ Auction Manager

⬜ Admin interface

⬜ Create auctions

⬜ Bid engine

⬜ Escrow workflow

⬜ Website transfer

⬜ Notifications

⬜ Version 1.0

Project Evolution

If you’ve been following the series from the beginning, you may notice that our architecture continues to evolve.

Originally, we planned for Flipnzee Auctions to manage both website listings and auctions. As the project matured, we recognized that Flipnzee Analytics already provides listing management, analytics, and verification.

Rather than duplicating those responsibilities, the Auction Manager focuses exclusively on auction operations. This separation of concerns keeps the codebase modular and allows both plugins to evolve independently while working together through the listing_id relationship.

The GitHub repository always contains the latest implementation, while these lessons document the reasoning behind each architectural decision.


Developer’s Notebook

One of the hallmarks of professional software is that it separates business logic from presentation. Users interact with forms and buttons, but those interfaces should simply call well-designed classes that perform the actual work. By introducing an Auction Manager early, we’re laying the foundation for a codebase that will remain organized even as the plugin grows to include bidding, escrow workflows, notifications, and website transfers.


Looking Ahead

In Lesson 8, we’ll use the Auction Manager to retrieve auction records from the database and display them inside WordPress. This will be our first step toward building a working administration interface and bringing real auction data onto the screen.

Leave a Reply