Last active
November 25, 2015 08:08
-
-
Save smeric/a73aa0527715836ea199 to your computer and use it in GitHub Desktop.
WordPress-Plugin-Boilerplate
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 | |
/** | |
* Register all actions, filters and shortcodes for the plugin | |
* | |
* @link http://example.com | |
* @since 1.0.0 | |
* | |
* @package Plugin_Name | |
* @subpackage Plugin_Name/includes | |
*/ | |
/** | |
* Register all actions, filters and shortcodes for the plugin. | |
* | |
* Maintain a list of all hooks that are registered throughout | |
* the plugin, and register them with the WordPress API. Call the | |
* run function to execute the list of actions and filters. | |
* | |
* @package Plugin_Name | |
* @subpackage Plugin_Name/includes | |
* @author Your Name <[email protected]> | |
*/ | |
class Plugin_Name_Loader { | |
/** | |
* The array of actions registered with WordPress. | |
* | |
* @since 1.0.0 | |
* @access protected | |
* @var array $actions The actions registered with WordPress to fire when the plugin loads. | |
*/ | |
protected $actions; | |
/** | |
* The array of filters registered with WordPress. | |
* | |
* @since 1.0.0 | |
* @access protected | |
* @var array $filters The filters registered with WordPress to fire when the plugin loads. | |
*/ | |
protected $filters; | |
/** | |
* The array of shortcodes registered with WordPress. | |
* | |
* @since 1.0.0 | |
* @access protected | |
* @var array $shortcodes The shortcodes registered with WordPress to fire when the plugin loads. | |
*/ | |
protected $shortcodes; | |
/** | |
* | |
* @since 1.0.0 | |
* @access private | |
* @var object|Plugin_Name_Loader | |
* @see https://github.com/DevinVinson/WordPress-Plugin-Boilerplate/issues/336 | |
*/ | |
private static $instance; | |
/** | |
* Initialize the collections used to maintain the actions, filters and shortcodes. | |
* | |
* @since 1.0.0 | |
* @see https://github.com/DevinVinson/WordPress-Plugin-Boilerplate/issues/336 | |
*/ | |
private function __construct() { | |
$this->actions = array(); | |
$this->filters = array(); | |
$this->shortcodes = array(); | |
} | |
/** | |
* Add a new action to the collection to be registered with WordPress. | |
* | |
* @since 1.0.0 | |
* @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 add_action( $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. | |
* | |
* @since 1.0.0 | |
* @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 add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { | |
$this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args ); | |
} | |
/** | |
* Add a new shortcode to the collection to be registered with WordPress. | |
* | |
* @since 1.0.0 | |
* @param string $hook The name of the WordPress shortcode 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. | |
*/ | |
public function add_shortcode( $hook, $component, $callback ) { | |
$this->filters = $this->add( $this->filters, $hook, $component, $callback ); | |
} | |
/** | |
* A utility function that is used to register the actions and hooks into a single | |
* collection. | |
* | |
* @since 1.0.0 | |
* @access private | |
* @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 Optional. The priority at which the function should be fired. | |
* @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. | |
* @return array The collection of actions and filters registered with WordPress. | |
* @see https://github.com/DevinVinson/WordPress-Plugin-Boilerplate/issues/336 | |
*/ | |
private function add( $hooks, $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { | |
$hooks[ $this->hook_index( $hook, $component, $callback ) ] = array( | |
'hook' => $hook, | |
'component' => $component, | |
'callback' => $callback, | |
'priority' => $priority, | |
'accepted_args' => $accepted_args | |
); | |
return $hooks; | |
} | |
/** | |
* Remove a hook. | |
* | |
* Hook must have been added by this class for this remover to work. | |
* | |
* Usage Plugin_Name_Loader::get_instance()->remove( $hook, $component, $callback ); | |
* | |
* @since 1.0.0 | |
* @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. | |
* @see https://github.com/DevinVinson/WordPress-Plugin-Boilerplate/issues/336 | |
*/ | |
public function remove( $hook, $component, $callback ) { | |
$index = $this->hook_index( $hook, $component, $callback ); | |
if( isset( $this->filters[ $index ] ) ) { | |
remove_filter( $this->filters[ $index ][ 'hook' ], array( $this->filters[ $index ][ 'component' ], $this->filters[ $index ][ 'callback' ] ) ); | |
} | |
if( isset( $this->actions[ $index ] ) ) { | |
remove_action( $this->filters[ $index ][ 'hook' ], array( $this->filters[ $index ][ 'component' ], $this->filters[ $index ][ 'callback' ] ) ); | |
} | |
} | |
/** | |
* Utility function for indexing $this->hooks | |
* | |
* @since 1.0.0 | |
* @access protected | |
* @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. | |
* | |
* @return string | |
* @see https://github.com/DevinVinson/WordPress-Plugin-Boilerplate/issues/336 | |
*/ | |
protected function hook_index( $hook, $component, $callback ) { | |
return md5( $hook . get_class( $component ) . $callback ); | |
} | |
/** | |
* Register the filters, actions and shortcodes with WordPress. | |
* | |
* @since 1.0.0 | |
*/ | |
public function run() { | |
foreach ( $this->filters as $hook ) { | |
add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); | |
} | |
foreach ( $this->actions as $hook ) { | |
add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); | |
} | |
foreach ( $this->shortcodes as $hook ) { | |
add_shortcodes( $hook['hook'], array( $hook['component'], $hook['callback'] ) ); | |
} | |
} | |
/** | |
* Get an instance of this class | |
* | |
* @since 1.0.0 | |
* @return object|\Plugin_Name_Loader | |
* @see https://github.com/DevinVinson/WordPress-Plugin-Boilerplate/issues/336 | |
*/ | |
public static function get_instance() { | |
if( is_null( self::$instance ) ) { | |
self::$instance = new Plugin_Name_Loader(); | |
} | |
return self::$instance; | |
} | |
} |
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 | |
/** | |
* The file that defines the core plugin class | |
* | |
* A class definition that includes attributes and functions used across both the | |
* public-facing side of the site and the admin area. | |
* | |
* @link http://example.com | |
* @since 1.0.0 | |
* | |
* @package Plugin_Name | |
* @subpackage Plugin_Name/includes | |
*/ | |
/** | |
* The core plugin class. | |
* | |
* This is used to define internationalization, admin-specific hooks, and | |
* public-facing site hooks. | |
* | |
* Also maintains the unique identifier of this plugin as well as the current | |
* version of the plugin. | |
* | |
* @since 1.0.0 | |
* @package Plugin_Name | |
* @subpackage Plugin_Name/includes | |
* @author Your Name <[email protected]> | |
*/ | |
class Plugin_Name { | |
/** | |
* The loader that's responsible for maintaining and registering all hooks that power | |
* the plugin. | |
* | |
* @since 1.0.0 | |
* @access protected | |
* @var Plugin_Name_Loader $loader Maintains and registers all hooks for the plugin. | |
*/ | |
protected $loader; | |
/** | |
* The unique identifier of this plugin. | |
* | |
* @since 1.0.0 | |
* @access protected | |
* @var string $plugin_name The string used to uniquely identify this plugin. | |
*/ | |
protected $plugin_name; | |
/** | |
* The current version of the plugin. | |
* | |
* @since 1.0.0 | |
* @access protected | |
* @var string $version The current version of the plugin. | |
*/ | |
protected $version; | |
/** | |
* Define the core functionality of the plugin. | |
* | |
* Set the plugin name and the plugin version that can be used throughout the plugin. | |
* Load the dependencies, define the locale, and set the hooks for the admin area and | |
* the public-facing side of the site. | |
* | |
* @since 1.0.0 | |
*/ | |
public function __construct() { | |
$this->plugin_name = 'plugin-name'; | |
$this->version = '1.0.0'; | |
$this->load_dependencies(); | |
$this->set_locale(); | |
$this->define_admin_hooks(); | |
$this->define_public_hooks(); | |
} | |
/** | |
* Load the required dependencies for this plugin. | |
* | |
* Include the following files that make up the plugin: | |
* | |
* - Plugin_Name_Loader. Orchestrates the hooks of the plugin. | |
* - Plugin_Name_i18n. Defines internationalization functionality. | |
* - Plugin_Name_Admin. Defines all hooks for the admin area. | |
* - Plugin_Name_Public. Defines all hooks for the public side of the site. | |
* | |
* Create an instance of the loader which will be used to register the hooks | |
* with WordPress. | |
* | |
* @since 1.0.0 | |
* @access private | |
*/ | |
private function load_dependencies() { | |
/** | |
* The class responsible for orchestrating the actions and filters of the | |
* core plugin. | |
*/ | |
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-plugin-name-loader.php'; | |
/** | |
* The class responsible for defining internationalization functionality | |
* of the plugin. | |
*/ | |
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-plugin-name-i18n.php'; | |
/** | |
* The class responsible for defining all actions that occur in the admin area. | |
*/ | |
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-plugin-name-admin.php'; | |
/** | |
* The class responsible for defining all actions that occur in the public-facing | |
* side of the site. | |
*/ | |
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-plugin-name-public.php'; | |
/** | |
* Get loader using its singleton | |
* @see https://github.com/DevinVinson/WordPress-Plugin-Boilerplate/issues/336 | |
*/ | |
$this->loader = Plugin_Name_Loader::get_instance(); | |
} | |
/** | |
* Define the locale for this plugin for internationalization. | |
* | |
* Uses the Plugin_Name_i18n class in order to set the domain and to register the hook | |
* with WordPress. | |
* | |
* @since 1.0.0 | |
* @access private | |
*/ | |
private function set_locale() { | |
$plugin_i18n = new Plugin_Name_i18n(); | |
$plugin_i18n->set_domain( $this->get_plugin_name() ); | |
$this->loader->add_action( 'plugins_loaded', $plugin_i18n, 'load_plugin_textdomain' ); | |
} | |
/** | |
* Register all of the hooks related to the admin area functionality | |
* of the plugin. | |
* | |
* @since 1.0.0 | |
* @access private | |
*/ | |
private function define_admin_hooks() { | |
$plugin_admin = new Plugin_Name_Admin( $this->get_plugin_name(), $this->get_version() ); | |
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' ); | |
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' ); | |
} | |
/** | |
* Register all of the hooks related to the public-facing functionality | |
* of the plugin. | |
* | |
* @since 1.0.0 | |
* @access private | |
*/ | |
private function define_public_hooks() { | |
$plugin_public = new Plugin_Name_Public( $this->get_plugin_name(), $this->get_version() ); | |
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' ); | |
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' ); | |
} | |
/** | |
* Run the loader to execute all of the hooks with WordPress. | |
* | |
* @since 1.0.0 | |
*/ | |
public function run() { | |
$this->loader->run(); | |
} | |
/** | |
* The name of the plugin used to uniquely identify it within the context of | |
* WordPress and to define internationalization functionality. | |
* | |
* @since 1.0.0 | |
* @return string The name of the plugin. | |
*/ | |
public function get_plugin_name() { | |
return $this->plugin_name; | |
} | |
/** | |
* The reference to the class that orchestrates the hooks with the plugin. | |
* | |
* @since 1.0.0 | |
* @return Plugin_Name_Loader Orchestrates the hooks of the plugin. | |
*/ | |
public function get_loader() { | |
return $this->loader; | |
} | |
/** | |
* Retrieve the version number of the plugin. | |
* | |
* @since 1.0.0 | |
* @return string The version number of the plugin. | |
*/ | |
public function get_version() { | |
return $this->version; | |
} | |
} |
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 | |
/** | |
* The plugin bootstrap file | |
* | |
* This file is read by WordPress to generate the plugin information in the plugin | |
* admin area. This file also includes all of the dependencies used by the plugin, | |
* registers the activation and deactivation functions, and defines a function | |
* that starts the plugin. | |
* | |
* @see https://github.com/DevinVinson/WordPress-Plugin-Boilerplate/pull/322 | |
* | |
* @link http://example.com | |
* @since 1.0.0 | |
* @package Plugin_Name | |
* | |
* @wordpress-plugin | |
* Plugin Name: WordPress Plugin Boilerplate | |
* Plugin URI: http://example.com/plugin-name-uri/ | |
* Description: This is a short description of what the plugin does. It's displayed in the WordPress admin area. | |
* Version: 1.0.0 | |
* Author: Your Name or Your Company | |
* Author URI: http://example.com/ | |
* License: GPL-2.0+ | |
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt | |
* Text Domain: plugin-name | |
* Domain Path: /languages | |
*/ | |
// If this file is called directly, abort. | |
if ( ! defined( 'WPINC' ) ) { | |
die; | |
} | |
class Plugin_Name_Bootstrap { | |
/** | |
* The code that runs during plugin activation. | |
* This action is documented in includes/class-plugin-name-activator.php | |
*/ | |
public static function activate_plugin_name() { | |
require_once plugin_dir_path( __FILE__ ) . 'includes/class-plugin-name-activator.php'; | |
Plugin_Name_Activator::activate(); | |
} | |
/** | |
* The code that runs during plugin deactivation. | |
* This action is documented in includes/class-plugin-name-deactivator.php | |
*/ | |
public static function deactivate_plugin_name() { | |
require_once plugin_dir_path( __FILE__ ) . 'includes/class-plugin-name-deactivator.php'; | |
Plugin_Name_Deactivator::deactivate(); | |
} | |
/** | |
* Bootstrap. | |
*/ | |
public static function init() { | |
register_activation_hook( __FILE__, array( 'Plugin_Name_Bootstrap', 'activate_plugin_name' ) ); | |
register_deactivation_hook( __FILE__, array( 'Plugin_Name_Bootstrap', 'deactivate_plugin_name' ) ); | |
/** | |
* The core plugin class that is used to define internationalization, | |
* admin-specific hooks, and public-facing site hooks. | |
*/ | |
require plugin_dir_path( __FILE__ ) . 'includes/class-plugin-name.php'; | |
$plugin = new Plugin_Name(); | |
$plugin->run(); | |
} | |
} | |
Plugin_Name_Bootstrap::init(); |
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 | |
/** | |
* Fired when the plugin is uninstalled. | |
* | |
* @see https://github.com/DevinVinson/WordPress-Plugin-Boilerplate/pull/320 | |
* | |
* @package Plugin_Name | |
* @author Your Name <[email protected]> | |
* @license GPL-2.0+ | |
* @link http://example.com | |
* @copyright 2015 Your Name or Company Name | |
*/ | |
// If uninstall not called from WordPress, then exit | |
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) { | |
exit; | |
} | |
if ( is_multisite() ) { | |
global $wpdb; | |
$blogs = $wpdb->get_results( "SELECT blog_id FROM {$wpdb->blogs}", ARRAY_A ); | |
/* @TODO: delete all transient, options and files you may have added | |
delete_transient( 'TRANSIENT_NAME' ); | |
delete_option( 'OPTION_NAME' ); | |
//info: remove custom file directory for main site | |
$upload_dir = wp_upload_dir(); | |
$directory = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . "CUSTOM_DIRECTORY_NAME" . DIRECTORY_SEPARATOR; | |
if ( is_dir( $directory ) ) { | |
foreach ( glob( $directory . '*.*' ) as $v ) { | |
unlink( $v ); | |
} | |
rmdir( $directory ); | |
} | |
*/ | |
if ( $blogs ) { | |
foreach ( $blogs as $blog ) { | |
switch_to_blog( $blog['blog_id'] ); | |
/* @TODO: delete all transient, options and files you may have added | |
delete_transient( 'TRANSIENT_NAME' ); | |
delete_option( 'OPTION_NAME' ); | |
//info: remove custom file directory for main site | |
$upload_dir = wp_upload_dir(); | |
$directory = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . "CUSTOM_DIRECTORY_NAME" . DIRECTORY_SEPARATOR; | |
if ( is_dir( $directory ) ) { | |
foreach ( glob( $directory . '*.*' ) as $v ) { | |
unlink( $v ); | |
} | |
rmdir( $directory ); | |
} | |
//info: remove and optimize tables | |
$GLOBALS['wpdb']->query( "DROP TABLE `" . $GLOBALS['wpdb']->prefix . "TABLE_NAME`" ); | |
$GLOBALS['wpdb']->query( "OPTIMIZE TABLE `" . $GLOBALS['wpdb']->prefix . "options`" ); | |
*/ | |
restore_current_blog(); | |
} | |
} | |
} | |
else { | |
/* @TODO: delete all transient, options and files you may have added | |
delete_transient( 'TRANSIENT_NAME' ); | |
delete_option( 'OPTION_NAME' ); | |
//info: remove custom file directory for main site | |
$upload_dir = wp_upload_dir(); | |
$directory = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . "CUSTOM_DIRECTORY_NAME" . DIRECTORY_SEPARATOR; | |
if ( is_dir( $directory ) ) { | |
foreach ( glob( $directory.'*.*' ) as $v ) { | |
unlink( $v ); | |
} | |
rmdir( $directory ); | |
} | |
//info: remove and optimize tables | |
$GLOBALS['wpdb']->query( "DROP TABLE `" . $GLOBALS['wpdb']->prefix . "TABLE_NAME`" ); | |
$GLOBALS['wpdb']->query( "OPTIMIZE TABLE `" . $GLOBALS['wpdb']->prefix . "options`" ); | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment