I’ve read through your post in it’s entirety, I certainly don’t see anything particularly wrong with it, but it sounds to me like your more frustrated with how it’s been laid out.
I personally have a big objected oriented programming background, it allows me to establish a structure and makes my code much more re-usable. My suggestion to you would be to attempt an object oriented approach for your next plugin, or the restructuring of this plugin you wrote about in the original question.
WordPress Plugin Boiler Plate is what I whole heartily recommend. I usually start by heading over to wwpb.me to generate a plugin skeleton for myself that is functional out of the gate, simply fill out the form, download, and extract into your plugins directory of your wordpress install.
Once you have your running skeleton in place I’d suggest giving this tutorial a read, it’s a great primer on how to build up the walking skeleton you just downloaded:
Speed up Development Using the WordPress Plugin Boilerplate pt1
The WordPress Plugin Boilerplate Part 2: Developing a Plugin
The WordPress Plugin Boilerplate Part 3: The Last Steps
After giving yourself a quick read in that, I usually like to create a data class that stores all my custom database queries such as:
<?php
class example_Events_Data {
private $wpdb;
public function __construct() {
global $wpdb;
$this->wpdb = $wpdb;
}
public function get_events($start_date = null, $end_date = null) {
$query = "SELECT wp_example_event.id, wp_example_event.name, wp_example_event.description, wp_example_event.date, wp_example_event.end_date, wp_example_event_type.id as event_type_id, wp_example_event_type.type as event_type
FROM wp_example_event
JOIN wp_example_event_type on wp_example_event.type_id = wp_example_event_type.id
ORDER BY wp_example_event.name ASC";
$events = $this->wpdb->get_results($query, ARRAY_A);
return $events;
}
Then to use this inside your admin / public facing controllers it’s as simple as doing:
<?php
class Example_Events_Admin {
/**
* The ID of this plugin.
*
* @since 1.0.0
* @access private
* @var string $plugin_name The ID of this plugin.
*/
private $plugin_name;
/**
* The version of this plugin.
*
* @since 1.0.0
* @access private
* @var string $version The current version of this plugin.
*/
private $version;
private $data;
private $utils;
/**
* Initialize the class and set its properties.
*
* @since 1.0.0
* @param string $plugin_name The name of this plugin.
* @param string $version The version of this plugin.
*/
public function __construct( $plugin_name, $version ) {
$this->plugin_name = $plugin_name;
$this->version = $version;
$this->data = new example_Events_Data();
}
I also like to create myself a utility class that houses very common functions throughout my plugin, such as form validation rules, implemented and used in exactly the same way.
You simply include them in your main plugin file with require_once and instantiate where needed.
Using the data class allows me to centralize all my data calls in one place, and use them throughout the plugin development.
Using these practices has reduced my development time significantly, and made my plugins a whole lot easier to maintain.
I hope you find this answer useful!