Skip to content

Commit

Permalink
Merge pull request #1 from boobika/boilerplate
Browse files Browse the repository at this point in the history
boilerplate
  • Loading branch information
boobika authored Apr 16, 2023
2 parents cd9ca7a + 21802bc commit 8927325
Show file tree
Hide file tree
Showing 11 changed files with 530 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
27 changes: 27 additions & 0 deletions 404.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* The template for displaying 404 pages (not found)
*
* @link https://codex.wordpress.org/Creating_an_Error_404_Page
*
* @package WordPress
* @subpackage Twenty_Twenty_One
* @since Twenty Twenty-One 1.0
*/

get_header();
?>

<header class="page-header alignwide">
<h1 class="page-title"><?php esc_html_e( 'Nothing here', 'twentytwentyone' ); ?></h1>
</header><!-- .page-header -->

<div class="error-404 not-found default-max-width">
<div class="page-content">
<p><?php esc_html_e( 'It looks like nothing was found at this location. Maybe try a search?', 'twentytwentyone' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .page-content -->
</div><!-- .error-404 -->

<?php
get_footer();
10 changes: 10 additions & 0 deletions footer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
/**
* The template for displaying the footer
*
* @link https://developer.wordpress.org/themes/basics/template-files/#template-partials
*/
?>
<?php wp_footer(); ?>
</body>
</html>
17 changes: 17 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

if (!defined('ABSPATH')) {
exit;
} // Exit if accessed directly

define('BOILERPLATE_TEMPLATE_DIR', get_template_directory());
define('BOILERPLATE_TEMPLATE_URI', get_template_directory_uri());

require_once BOILERPLATE_TEMPLATE_DIR . '/inc/PSR4Autoloader.php';
$psr4autoloader = new \Boilerplate\Psr4Autoloader();
$psr4autoloader->addNamespace('Boilerplate', BOILERPLATE_TEMPLATE_DIR . '/inc');
$psr4autoloader->register();

$loader = new \Boilerplate\Loader();
$loader->run();

17 changes: 17 additions & 0 deletions header.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* The header for our theme
*
* @link https://developer.wordpress.org/themes/basics/template-files/#template-partials
*/
?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo('charset'); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<?php wp_head(); ?>
</head>

<body <?php body_class(); ?>>
<?php do_action('wp_body_open'); ?>
95 changes: 95 additions & 0 deletions inc/Loader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace Boilerplate;

/**
* Register all actions and filters for the theme.
*
* Maintain a list of all hooks that are registered throughout
* the themen, and register them with the WordPress API. Call the
* run function to execute the list of actions and filters.
*/
class Loader
{
/**
* @var array $actions The actions registered with WordPress to fire when the theme loads.
*/
protected $_actions;

/**
* @var array $filters The filters registered with WordPress to fire when the theme loads.
*/
protected $_filters;

public function __construct()
{
$this->_actions = [];
$this->_filters = [];
}

/**
* Add a new action to the collection to be registered with WordPress.
*
* @param string $hook The name of the WordPress action that is being registered.
* @param object $component A reference to the instance of the object on which the action is defined.
* @param string $callback The name of the function definition on the $component.
* @param int $priority Optional. he priority at which the function should be fired. Default is 10.
* @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1.
*/
public function addAction($hook, $component, $callback, $priority = 10, $accepted_args = 1)
{
$this->_actions = $this->_add($this->_actions, $hook, $component, $callback, $priority, $accepted_args);
}

/**
* Add a new filter to the collection to be registered with WordPress.
*
* @param string $hook The name of the WordPress filter that is being registered.
* @param object $component A reference to the instance of the object on which the filter is defined.
* @param string $callback The name of the function definition on the $component.
* @param int $priority Optional. he priority at which the function should be fired. Default is 10.
* @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1
*/
public function addFilter($hook, $component, $callback, $priority = 10, $accepted_args = 1)
{
$this->_filters = $this->_add($this->_filters, $hook, $component, $callback, $priority, $accepted_args);
}

/**
* A utility function that is used to register the actions and hooks into a single collection.
*
* @param array $hooks The collection of hooks that is being registered (that is, actions or filters).
* @param string $hook The name of the WordPress filter that is being registered.
* @param object $component A reference to the instance of the object on which the filter is defined.
* @param string $callback The name of the function definition on the $component.
* @param int $priority The priority at which the function should be fired.
* @param int $accepted_args The number of arguments that should be passed to the $callback.
* @return array The collection of actions and filters registered with WordPress.
*/
protected function _add($hooks, $hook, $component, $callback, $priority, $accepted_args)
{
$hooks[] = [
'hook' => $hook,
'component' => $component,
'callback' => $callback,
'priority' => $priority,
'accepted_args' => $accepted_args
];

return $hooks;
}

/**
* Register the filters and actions with WordPress.
*/
public function run()
{
foreach ($this->_filters as $hook) {
add_filter($hook['hook'], [$hook['component'], $hook['callback']], $hook['priority'], $hook['accepted_args']);
}

foreach ($this->_actions as $hook) {
add_action($hook['hook'], [$hook['component'], $hook['callback']], $hook['priority'], $hook['accepted_args']);
}
}
}
187 changes: 187 additions & 0 deletions inc/PSR4Autoloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?php
namespace Boilerplate;

/**
* An example of a general-purpose implementation that includes the optional
* functionality of allowing multiple base directories for a single namespace
* prefix.
*
* Given a foo-bar package of classes in the file system at the following
* paths ...
*
* /path/to/packages/foo-bar/
* src/
* Baz.php # Foo\Bar\Baz
* Qux/
* Quux.php # Foo\Bar\Qux\Quux
* tests/
* BazTest.php # Foo\Bar\BazTest
* Qux/
* QuuxTest.php # Foo\Bar\Qux\QuuxTest
*
* ... add the path to the class files for the \Foo\Bar\ namespace prefix
* as follows:
*
* <?php
* // instantiate the loader
* $loader = new \Psr4AutoloaderClass;
*
* // register the autoloader
* $loader->register();
*
* // register the base directories for the namespace prefix
* $loader->addNamespace('Foo\Bar', '/path/to/packages/foo-bar/src');
* $loader->addNamespace('Foo\Bar', '/path/to/packages/foo-bar/tests');
*
* The following line would cause the autoloader to attempt to load the
* \Foo\Bar\Qux\Quux class from /path/to/packages/foo-bar/src/Qux/Quux.php:
*
* <?php
* new \Foo\Bar\Qux\Quux;
*
* The following line would cause the autoloader to attempt to load the
* \Foo\Bar\Qux\QuuxTest class from /path/to/packages/foo-bar/tests/Qux/QuuxTest.php:
*
* <?php
* new \Foo\Bar\Qux\QuuxTest;
*/
class Psr4Autoloader
{
/**
* An associative array where the key is a namespace prefix and the value
* is an array of base directories for classes in that namespace.
*
* @var array
*/
protected $prefixes = array();

/**
* Register loader with SPL autoloader stack.
*
* @return void
*/
public function register()
{
spl_autoload_register(array($this, 'loadClass'));
}

/**
* Adds a base directory for a namespace prefix.
*
* @param string $prefix The namespace prefix.
* @param string $base_dir A base directory for class files in the
* namespace.
* @param bool $prepend If true, prepend the base directory to the stack
* instead of appending it; this causes it to be searched first rather
* than last.
* @return void
*/
public function addNamespace($prefix, $base_dir, $prepend = false)
{
// normalize namespace prefix
$prefix = trim($prefix, '\\') . '\\';

// normalize the base directory with a trailing separator
$base_dir = rtrim($base_dir, DIRECTORY_SEPARATOR) . '/';

// initialize the namespace prefix array
if (isset($this->prefixes[$prefix]) === false) {
$this->prefixes[$prefix] = array();
}

// retain the base directory for the namespace prefix
if ($prepend) {
array_unshift($this->prefixes[$prefix], $base_dir);
} else {
array_push($this->prefixes[$prefix], $base_dir);
}
}

/**
* Loads the class file for a given class name.
*
* @param string $class The fully-qualified class name.
* @return mixed The mapped file name on success, or boolean false on
* failure.
*/
public function loadClass($class)
{
// the current namespace prefix
$prefix = $class;

// work backwards through the namespace names of the fully-qualified
// class name to find a mapped file name
while (false !== $pos = strrpos($prefix, '\\')) {

// retain the trailing namespace separator in the prefix
$prefix = substr($class, 0, $pos + 1);

// the rest is the relative class name
$relative_class = substr($class, $pos + 1);

// try to load a mapped file for the prefix and relative class
$mapped_file = $this->loadMappedFile($prefix, $relative_class);
if ($mapped_file) {
return $mapped_file;
}

// remove the trailing namespace separator for the next iteration
// of strrpos()
$prefix = rtrim($prefix, '\\');
}

// never found a mapped file
return false;
}

/**
* Load the mapped file for a namespace prefix and relative class.
*
* @param string $prefix The namespace prefix.
* @param string $relative_class The relative class name.
* @return mixed Boolean false if no mapped file can be loaded, or the
* name of the mapped file that was loaded.
*/
protected function loadMappedFile($prefix, $relative_class)
{
// are there any base directories for this namespace prefix?
if (isset($this->prefixes[$prefix]) === false) {
return false;
}

// look through base directories for this namespace prefix
foreach ($this->prefixes[$prefix] as $base_dir) {

// replace the namespace prefix with the base directory,
// replace namespace separators with directory separators
// in the relative class name, append with .php
$file = $base_dir
. str_replace('\\', '/', $relative_class)
. '.php';

// if the mapped file exists, require it
if ($this->requireFile($file)) {
// yes, we're done
return $file;
}
}

// never found it
return false;
}

/**
* If a file exists, require it from the file system.
*
* @param string $file The file to require.
* @return bool True if the file exists, false if not.
*/
protected function requireFile($file)
{
if (file_exists($file)) {
require $file;
return true;
}
return false;
}
}
Loading

0 comments on commit 8927325

Please sign in to comment.