Skip to content

Commit

Permalink
Adding in the autoloading of the classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
krugazul committed Jan 29, 2025
1 parent 5a48b93 commit 7908d18
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 37 deletions.
12 changes: 2 additions & 10 deletions classes/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,11 @@
* @package lsx-starter-plugin
*/
class Admin {

/**
* Holds class instance
*
* @since 1.0.0
*
* @var object \lsx_starter_plugin\classes\Admin()
*/
protected static $instance = null;

/**
* Contructor
*/
public function __construct() {
}
}

return new Admin();
53 changes: 29 additions & 24 deletions classes/class-core.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace lsx_starter_plugin\classes;

use lsx_starter_plugin\classes;

/**
* This class loads the other classes and function files
*
Expand All @@ -18,20 +20,15 @@ class Core {
protected static $instance = null;

/**
* @var object \lsx_starter_plugin\classes\Setup();
*/
public $setup;

/**
* @var object \lsx_starter_plugin\classes\Admin();
*/
public $admin;

/**
* Holds the template redirect functions
* @var object \lsx_starter_plugin\classes\Templates();
* An array of the classes initiated, the filename is the index.
*
* $classes['setup'] = \lsx_starter_plugin\classes\Setup();
* $classes['admin'] = \lsx_starter_plugin\classes\Admin();
* $classes['templates'] = \lsx_starter_plugin\classes\Templates();
*
* @var array
*/
public $templates;
public $classes = [];

/**
* Contructor
Expand All @@ -57,20 +54,28 @@ public static function get_instance() {
}

/**
* Loads the variable classes and the static classes.
* Registers our block patterns with the
*
* @return void
*/
private function load_classes() {
// Load plugin settings related functionality.
require_once LSX_STARTER_PLUGIN_PATH . 'classes/class-setup.php';
$this->setup = new Setup();
public function load_classes() {
$directory = LSX_STARTER_PLUGIN_PATH . 'classes/';

foreach ( glob( $directory . '*.php') as $file ) {
if ( 'class-core.php' === $file ) {
continue;
}

// Load plugin settings related functionality.
require_once LSX_STARTER_PLUGIN_PATH . 'classes/class-templates.php';
$this->templates = new Templates( LSX_STARTER_PLUGIN_PATH );
// Extract the filename and classname without the directory path and extension
$filename = basename( $file, '.php' );
$filename = str_replace( 'class-', '', $filename );

// Load plugin admin related functionality.
/*require_once LSX_STARTER_PLUGIN_PATH . 'classes/class-admin.php';
$this->admin = new Admin();*/
// Initiate the class.
$this->classes[ $filename ] = require_once $file;
if ( 'templates' === $filename ) {
$this->classes['templates']->set_path( LSX_STARTER_PLUGIN_PATH );
}
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions classes/class-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ public function admin_assets() {
wp_enqueue_style( 'lsx-starter-plugin-admin', LSX_STARTER_PLUGIN_URL . 'assets/css/lsx-starter-plugin-admin.css', array(), LSX_STARTER_PLUGIN_VER );
}
}

return new Setup();
17 changes: 14 additions & 3 deletions classes/class-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,20 @@ class Templates {
*
* @access private
*/
public function __construct( $path ) {
$this->path = $path;
public function __construct() {
add_action( 'init', [ $this, 'register_post_type_templates' ] );
}

/**
* Sets the path the templates should use.
*
* @param [type] $path
* @return void
*/
public function set_path( $path ) {
$this->path = $path;
}

/**
* Registers our plugins templates.
*
Expand Down Expand Up @@ -83,7 +92,9 @@ public function register_post_type_templates() {
*/
protected function get_template_content( $template ) {
ob_start();
include LSX_STARTER_PLUGIN_PATH . "/templates/{$template}";
include $this->path . "/templates/{$template}";
return ob_get_clean();
}
}

return new Templates();

0 comments on commit 7908d18

Please sign in to comment.