Last active
November 2, 2019 19:47
-
-
Save earth3300/545db2db83486fbd731b3c9ec401076a to your computer and use it in GitHub Desktop.
A plugin to perform theme customizations, including functions.php, JS and CSS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: WP Theme Plugin | |
* Description: Theme customizations (including functions.php, JS and CSS) are added to this plugin. | |
* Plugin URI: https://gist.github.com/earth3300/545db2db83486fbd731b3c9ec401076a | |
* Version: 1.0.1 | |
* Author: earth3300 | |
* Author URI: http://github.com/earth3300/ | |
* Requires at least: 4.9.1 | |
* Tested up to: 5.2.1 | |
* | |
* @package WPThemePlugin | |
* | |
* Notes: Placing theme customizations in a plugin allows them to be moved | |
* easily and prevents them from being overwritten by theme customizations. | |
* (Also works with WooCommerce). The functions.php file in this plugin is | |
* intended to replace the theme functions.php file. | |
* | |
* This plugin contains a chained series of checks to ensure that | |
* it operates in the correct environment and that it is authorized to do so. | |
* Also, a global ON/OFF switch has been set internally, so that it can | |
* be completely disabled if need be. That switch is further down in the code. | |
* | |
* In terms of optimization, although these checks may *look* bulky, they can | |
* be performed very quickly, as PHP caches file based functions such as | |
* `file_exists`†. These functions are separated out to improve readability | |
* and so that the logic applied can be traced more easily. | |
* | |
* The net result is the loading of the files in the private `load` function | |
* and the running of the private `run` function. It is expected that a class | |
* is instantiated and then initialized in the `run` function. | |
* | |
* †{@link https://www.php.net/manual/en/function.clearstatcache.php} | |
* | |
* File: plugin.php | |
* Created: 2019-04-30 | |
* Updated: 2019-11-02 | |
* Time: 15:43 EDT | |
*/ | |
/* | |
Credits: WooCommerce. | |
Note: This plugin has been HEAVILY modified from the original | |
provided by WooCommerce. However the idea is gratefully ackowledged | |
as a relevant way to perform theme customizations. | |
*/ | |
// No direct access. | |
defined('ABSPATH') || exit; | |
/** | |
* WP Theme Plugin | |
* | |
* Allows for the customization of a theme, including JS, CSS | |
* and PHP functions. | |
* | |
* @version 1.0.0 | |
* @since 1.0.0 | |
* @package WPThemePlugin | |
*/ | |
class WPThemePlugin | |
{ | |
/** | |
* Construct | |
*/ | |
public function __construct() | |
{ | |
} | |
/** | |
* Initialize | |
* | |
* This function performs a chained series of checks. | |
* If they all pass, the sript runs. | |
* | |
* @return void | |
*/ | |
public function init() | |
{ | |
// Server Check | |
if( $this->server() ) | |
{ | |
// Platform Check | |
if ( $env = $this->platform() ) | |
{ | |
// Authorization Check | |
if( $this->authorize( $env ) ) | |
{ | |
// Is online and allowed, or run local. | |
if( $this->isOnline() && $this->allowOnline() || $this->isLocal() ) | |
{ | |
// Load the required files. | |
if ( $this->load() ) | |
{ | |
// Run the script... | |
$this->run(); | |
} | |
else | |
{ | |
exit( 'Required file(s) could not be loaded: WP Theme Plugin' ); | |
} | |
} | |
else | |
{ | |
exit( 'Online authorization has been revoked: WP Theme Plugin' ); | |
} | |
} | |
else | |
{ | |
exit( 'Authorization has been revoked: WP Theme Plugin' ); | |
} | |
} | |
else | |
{ | |
exit( 'Platform not supported: WP Theme Plugin' ); | |
} | |
} | |
else | |
{ | |
exit( 'Server Not Supported: (WP Theme Plugin). Unknown environment?'); | |
} | |
} | |
/** | |
* Verify the Server | |
* | |
* Verify the environment with a function check. | |
* | |
* @return bool | |
*/ | |
private function server() | |
{ | |
if ( 'Apache' == substr( $_SERVER['SERVER_SOFTWARE'], 0, 6 ) ) | |
{ | |
return true; | |
} | |
else | |
{ | |
exit( 'This script does not work with this server (WP Theme Plugin). Apache Required.' ); | |
} | |
} | |
/** | |
* Verify the Platform | |
* | |
* Verify the platform with a function check. | |
* | |
* @return bool | |
*/ | |
private function platform() | |
{ | |
$env['platform'] = null; | |
if ( function_exists( 'wp_title' ) ) | |
{ | |
$env['platform'] = 'wp'; | |
} | |
else | |
{ | |
$env['platform'] = false; | |
} | |
return $env; | |
} | |
/** | |
* Authorize | |
* | |
* Authorize the execution of the scripts contained herein. | |
* | |
* @return bool | |
*/ | |
private function authorize( $env ) | |
{ | |
// 0 to turn OFF (1 to turn ON). | |
if( 'wp' == $env['platform'] ) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
/** | |
* Is Online | |
* | |
* Returns true if the script is in an online environment. | |
* | |
* @return bool | |
*/ | |
private function isOnline() | |
{ | |
if( '127.0.0.1' !== $_SERVER['SERVER_ADDR'] ) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
/** | |
* Is Local | |
* | |
* Returns true if the script is in a local environment. | |
* | |
* @return bool | |
*/ | |
private function isLocal() | |
{ | |
if( '127.0.0.1' == $_SERVER['SERVER_ADDR'] ) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
/** | |
* Load the required files. | |
* | |
* Returns null if all files are turned OFF. | |
* Returns false if at least one file is not available. | |
* | |
* @return bool | |
*/ | |
private function load() | |
{ | |
$loaded = null; | |
$load['files'] = [ | |
[ 'name' => 'functions.php', 'load' => 1 ], | |
[ 'name' => 'setup.php', 'load' => 1 ], | |
]; | |
foreach( $load['files'] as $file ) | |
{ | |
if( $file['load'] ) | |
{ | |
if ( file_exists( __DIR__ . '/' . $file['name'] ) ) | |
{ | |
$loaded = true; | |
require_once( __DIR__ . '/' . $file['name'] ); | |
} | |
else | |
{ | |
$loaded = false; | |
} | |
} | |
} | |
return $loaded; | |
} | |
/** | |
* Run the class. | |
*/ | |
private function run() | |
{ | |
if( class_exists( 'WPThemePluginSetup' ) ) | |
{ | |
$class = new WPThemePluginSetup; | |
$class->init(); | |
} | |
} | |
} // End Class | |
/** | |
* WP Theme Plugin Global Function | |
* | |
* Instantiate the class and intialize it. | |
* | |
* @return void | |
*/ | |
function wp_theme_plugin() | |
{ | |
$class = new WPThemePlugin(); | |
$class->init(); | |
} | |
/** | |
* Initialise the plugin with the init action (always loads). | |
* | |
* Check to ensure the correct environment exists. | |
*/ | |
if( function_exists( 'add_action' ) ) | |
{ | |
add_action( 'init', 'wp_theme_plugin' ); | |
} | |
else | |
{ | |
exit( 'File is not in the correct environment: WordPress (WP Theme Plugin).' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment