Skip to content

Instantly share code, notes, and snippets.

@GuerrillaCoder
Last active August 29, 2015 14:19
Show Gist options
  • Save GuerrillaCoder/2a439634cb2c2a589a45 to your computer and use it in GitHub Desktop.
Save GuerrillaCoder/2a439634cb2c2a589a45 to your computer and use it in GitHub Desktop.
Idea for set up structure
<?php
namespace themename\setup;
//todo: abstract actual values to another object and pass it to this contructor
class MainThemeSetupMaster
{
private $cssSetup;
private $jsSetup;
public function __construct()
{
$this->cssSetup = new CssSetup();
$this->jsSetup = new ScriptSetup();
}
public function initScripts()
{
$this->cssSetup->init();
$this->jsSetup->init();
}
}
class CssSetup
{
public function init()
{
// call privates
add_action( 'wp_enqueue_scripts',
function ()
{
$this->_theme_styles();
});
add_action( 'wp_enqueue_scripts',
function ()
{
$this->_vendor_styles();
});
}
private function _theme_styles()
{
// queue theme styles
}
private function _vendor_styles()
{
// queue vendor styles
}
}
class ScriptSetup
{
public function init()
{
// call privates
add_action( 'wp_enqueue_scripts',
function ()
{
$this->_theme_scripts();
});
add_action( 'wp_enqueue_scripts',
function ()
{
$this->_vendor_scripts();
});
}
private function _theme_scripts()
{
// queue theme scripts
}
private function _vendor_scripts()
{
// queue vendor scripts
}
}
@GuerrillaCoder
Copy link
Author

Thinking of having one main set up class that can be called like:

require_once( theme_lib . '/setup.php');

$mainSetupInstance = new themename\setup\MainThemeSetupMaster();
$mainSetupInstance->initScripts();

I would then just add other functions like initPlugins() or whatever.

Am I on the right track or is there better way?

@itsananderson
Copy link

Looks good.

One thing I'd probably do slightly different is start with all of those classes being just one class that handles all the CSS/JS/etc. and then only split them out into different classes if they become too complex. Otherwise you have to switch context a lot between the different classes. If you do that, you'll find that it cuts the size of the current file at least in half.

@GuerrillaCoder
Copy link
Author

Cool thanks. I am going to refactor it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment