Last active
April 23, 2018 16:07
-
-
Save mikeyhoward1977/ea37822991774c2980e81f0bf0b4141e to your computer and use it in GitHub Desktop.
WordPress Plugin Boilerplate
This file contains hidden or 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: Mikes Plugin Boilerplate | |
* Plugin URI: https://mikesplugins.co.uk | |
* Description: A basic boilerplate for writing WordPress plugins | |
* Version: 1.0 | |
* Date: 23 April 2018 | |
* Author: Mike Howard | |
* Author URI: https://mikesplugins.co.uk/ | |
* Text Domain: mikes-plugins | |
* Domain Path: /languages | |
* License: GPL2 | |
* License URI: https://www.gnu.org/licenses/gpl-2.0.html | |
* GitHub Plugin URI: https://gist.github.com/mikeyhoward1977/ea37822991774c2980e81f0bf0b4141e | |
* Tags: | |
* | |
* | |
* This boilerplate is free software; you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License, version 2, as | |
* published by the Free Software Foundation. | |
* | |
* This boilerplate is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this boilerplate; if not, see https://www.gnu.org/licenses/gpl-2.0.html | |
* | |
* @package Mikes_Plugins | |
* @category Core | |
* @author Mike Howard | |
* @version 1.0 | |
*/ | |
/** | |
* - Replace 'Mikes_Plugins' with your own unique class name | |
* - Replace 'MP_PLUGIN_' with your own unique plugin prefix | |
* - Replace 'mp_plugin_' with your own unique plugin prefix | |
* - Replace '$mp_plugin' with your own unique plugin prefix | |
* - Replace 'WP_MPS()' with your own unique function name | |
*/ | |
// Exit if accessed directly. | |
if ( ! defined( 'ABSPATH' ) ) | |
exit; | |
if ( ! class_exists( 'Mikes_Plugins' ) ) : | |
/** | |
* Main Mikes_Plugins Class. | |
* | |
* @since 1.0 | |
*/ | |
final class Mikes_Plugins { | |
/** Singleton *************************************************************/ | |
/** | |
* @var Mikes_Plugins The one true Mikes_Plugins | |
* @since 1.0 | |
*/ | |
private static $instance; | |
/** | |
* Main Mikes_Plugins Instance. | |
* | |
* Insures that only one instance of Mikes_Plugins exists in memory at any one | |
* time. Also prevents needing to define globals all over the place. | |
* | |
* @since 1.0 | |
* @static | |
* @static var arr $instance | |
* @uses Mikes_Plugins::setup_constants() Setup the constants needed. | |
* @uses Mikes_Plugins::includes() Include the required files. | |
* @uses Mikes_Plugins::load_textdomain() Load the language files. | |
* @see WP_SPP() | |
* @return obj Mikes_Plugins The one true Mikes_Plugins | |
*/ | |
public static function instance() { | |
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof Mikes_Plugins ) ) { | |
self::$instance = new Mikes_Plugins; | |
self::$instance->setup_constants(); | |
add_action( 'plugins_loaded', array( self::$instance, 'load_textdomain' ) ); | |
self::$instance->includes(); | |
self::$instance->hooks(); | |
} | |
return self::$instance; | |
} // instance | |
/** | |
* Throw error on object clone. | |
* | |
* The whole idea of the singleton design pattern is that there is a single | |
* object therefore, we don't want the object to be cloned. | |
* | |
* @since 1.0 | |
* @access protected | |
* @return void | |
*/ | |
public function __clone() { | |
// Cloning instances of the class is forbidden. | |
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'mikes-plugins' ), '1.0' ); | |
} // __clone | |
/** | |
* Disable unserializing of the class. | |
* | |
* @since 1.0 | |
* @access protected | |
* @return void | |
*/ | |
public function __wakeup() { | |
// Unserializing instances of the class is forbidden. | |
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'mikes-plugins' ), '1.0' ); | |
} // __wakeup | |
/** | |
* Setup plugin constants. | |
* | |
* @access private | |
* @since 1.0 | |
* @return void | |
*/ | |
private function setup_constants() { | |
if ( ! defined( 'MP_PLUGIN_VERSION' ) ) { | |
define( 'MP_PLUGIN_VERSION', '1.0' ); | |
} | |
if ( ! defined( 'MP_PLUGIN_DIR' ) ) { | |
define( 'MP_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); | |
} | |
if ( ! defined( 'MP_PLUGIN_URL' ) ) { | |
define( 'MP_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); | |
} | |
if ( ! defined( 'WP_SPP_PLUGIN_FILE' ) ) { | |
define( 'MP_PLUGIN_FILE', __FILE__ ); | |
} | |
} // setup_constants | |
/** | |
* Include required files. | |
* | |
* @access private | |
* @since 1.0 | |
* @return void | |
*/ | |
private function includes() { | |
// i.e. require_once MP_PLUGIN_DIR . 'includes/misc-functions.php'; | |
if ( is_admin() ) { | |
// i.e require_once MP_PLUGIN_DIR . 'includes/admin/settings.php'; | |
} | |
} // includes | |
/** | |
* Hooks. | |
* | |
* @access private | |
* @since 1.0 | |
* @return void | |
*/ | |
private function hooks() { | |
// Upgrades | |
add_action( 'admin_init', array( self::$instance, 'upgrades' ) ); | |
} // hooks | |
/** | |
* Load the text domain for translations. | |
* | |
* @access private | |
* @since 1.0 | |
* @return void | |
*/ | |
public function load_textdomain() { | |
// Set filter for plugin's languages directory. | |
$mp_plugin_lang_dir = dirname( plugin_basename( WP_SPP_PLUGIN_FILE ) ) . '/languages/'; | |
$mp_plugin_lang_dir = apply_filters( 'mp_plugin_languages_directory', $mp_plugin_lang_dir ); | |
// Traditional WordPress plugin locale filter. | |
$locale = is_admin() && function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale(); | |
$locale = apply_filters( 'plugin_locale', $locale, 'mikes-plugins' ); | |
load_textdomain( 'mikes-plugins', WP_LANG_DIR . '/mikes-plugins/mikes-plugins-' . $locale . '.mo' ); | |
load_plugin_textdomain( 'mikes-plugins', false, $mp_plugin_lang_dir ); | |
} // load_textdomain | |
/***************************************** | |
-- UPGRADE PROCEDURES | |
*****************************************/ | |
/** | |
* Perform automatic database upgrades when necessary | |
* | |
* @since 1.0 | |
* @return void | |
*/ | |
public function upgrades() { | |
$did_upgrade = false; | |
$mp_plugin_version = preg_replace( '/[^0-9.].*/', '', get_option( 'mp_plugin_version' ) ); | |
if ( version_compare( $mp_plugin_version, MP_PLUGIN_VERSION, '<' ) ) { | |
// Let us know that an upgrade has happened | |
$did_upgrade = true; | |
} | |
if ( $did_upgrade ) { | |
update_option( 'mp_plugin_version_upgraded_from', $mp_plugin_version ); | |
update_option( 'mp_plugin_version', preg_replace( '/[^0-9.].*/', '', MP_PLUGIN_VERSION ) ); | |
} | |
} // upgrades | |
} // class Mikes_Plugins | |
endif; | |
/** | |
* The main function for that returns Mikes_Plugins | |
* | |
* The main function responsible for returning the one true Mikes_Plugins | |
* Instance to functions everywhere. | |
* | |
* Use this function like you would a global variable, except without needing | |
* to declare the global. | |
* | |
* Example: <?php $wp_mps = WP_MPS(); ?> | |
* | |
* @since 1.0 | |
* @return obj Mikes_Plugins The one true Mikes_Plugins Instance. | |
*/ | |
function WP_MPS() { | |
return Mikes_Plugins::instance(); | |
} // WP_MPS | |
// Get Mikes_Plugins Running | |
WP_MPS(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment