Developing WordPress Plugins

Hi Everyone!  We are developing a WordPress Plugin and I want you guys to follow along!

Page: design-patterns-wordpress.php



* @license GPL-2.0+
* @link http://www.jamesportis.com
* @copyright 2016 James Portis
*
* @wordpress-plugin
* Plugin Name: Design Patterns in WordPress
* Plugin URI: http://www.jamesportis.com
* Description: An example WordPress plugin used to demonstrate two design patterns in the context of WordPress.
* Version: 1.0.0
* Author: James Portis
* Author URI: http://www.jamesportis.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/

// If This file is accessed directly, abort. This is a best practice for working with WordPress installations
if ( ! defined( 'WPINC' ) ) { //we do this be determining if the WPINC constant is defined
die;
}

include_once( 'class-design-patterns-wordpress.php' );

Design_Patterns_WordPress::get_instance(); //calling for the static get_instance function on the Design Patterns WordPress class

Page – class-design-patterns-wordpress.php



* @license GPL-2.0+
* @link http://www.jamesportis.com
* @copyright 2016 James Portis
*/

/**
* The core plugin class used to demonstrate Design Patterns in WordPress.
*
* @package DPWP
* @author James Portis
*/
class Design_Patterns_WordPress {

private static $instance; //defined a class variable inside the class. It is private so no outside modules can use it, and is static so the instance to which the variable refers is shared accross all objects
private function __construct() { //inside the constructor is where you do the set up for the object
// Set up the necessary data to get the object going
}
public static function get_instance() { //now we must make a function that will return instance of the class and create a new instance if it doesn't already exist. Marked as static because it can be called on any instance and it helps us maintain a single instance of the class
if ( null == self::$instance ) { //first check if the instance variable has a value, if not, then must create an instance of the class
self::$instance = new self; // creating a new instance of the class with new self and then assigning a reference to the static variable instance.
}
return self::$instance(); //now return the reference to the caller. That is a simple singleton pattern
}
}