Created
May 9, 2013 16:08
-
-
Save engelen/5548440 to your computer and use it in GitHub Desktop.
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: My Plugin | |
Version: 0.1 | |
*/ | |
class JWF_Plugin | |
{ | |
/** | |
* Holds the only instance of this plugin | |
* | |
* @static | |
* @var JWF_Plugin | |
* @access private | |
* @since 0.1 | |
*/ | |
private static $_instance = NULL; | |
/** | |
* Plugin version | |
* | |
* @var string | |
* @access protected | |
* @since 0.1 | |
*/ | |
protected $version = '0.1'; | |
/** | |
* The absolute path of the plugin file | |
* | |
* @var string | |
* @access protected | |
* @since 0.1 | |
*/ | |
protected $plugin_file_path = ''; | |
/** | |
* The absolute path of the plugin directory | |
* | |
* @var string | |
* @access protected | |
* @since 0.1 | |
*/ | |
protected $plugin_dir_path = ''; | |
/** | |
* Constructor | |
* | |
* @since 0.1 | |
*/ | |
private function __construct() | |
{ | |
// Actions | |
add_action( 'plugins_loaded', array( &$this, 'finish_setup' ) ); | |
add_action( 'init', array( &$this, 'handle_content_types' ) ); | |
require_once $this->get_plugin_dir_path() . 'lib/fields.php'; | |
require_once $this->get_plugin_dir_path() . 'lib/widgets.php'; | |
if ( is_admin() ) { | |
require_once $this->get_plugin_dir_path() . 'lib/admin.php'; | |
} | |
// Activation hook | |
register_activation_hook( $this->get_plugin_file_path(), array( &$this, 'plugin_activate' ) ); | |
} | |
/** | |
* Get the instance of this class, insantiating it if it doesn't exist yet | |
* | |
* @since 0.1 | |
* | |
* @return JWF_Plugin Class instance | |
*/ | |
public static function get_instance() | |
{ | |
if ( !is_object( self::$_instance ) ) { | |
self::$_instance = new JWF_Plugin(); | |
} | |
return self::$_instance; | |
} | |
/** | |
* Handle final aspects of plugin setup, such as adding action hooks | |
* | |
* @since 0.1 | |
*/ | |
public function finish_setup() | |
{ | |
do_action( 'jwf/after_setup', &$this ); | |
} | |
/** | |
* Handle inital installation and upgrading of the plugin | |
* | |
* @since 0.1 | |
*/ | |
public function plugin_activate() | |
{ | |
$version = $this->get_version(); | |
$db_version = get_option( 'jwf_version' ); | |
$difference = version_compare( $db_version, $version ); | |
if ($difference != 0) { | |
// Save new version | |
update_option( 'jwf_version', $this->get_version() ); | |
} | |
} | |
/** | |
* Register and handle content types | |
*/ | |
public function handle_content_types() | |
{ | |
// Register post types and such | |
} | |
/** | |
* Get the main plugin file path | |
* | |
* @since 0.1 | |
* | |
* @return string Absolute path to the main plugin file | |
*/ | |
public function get_plugin_file_path() | |
{ | |
if ( empty( $this->plugin_file_path ) ) { | |
$this->plugin_file_path = realpath(__FILE__); | |
} | |
return $this->plugin_file_path; | |
} | |
/** | |
* Get the main plugin file path | |
* | |
* @since 0.1 | |
* | |
* @return string Absolute path to the main plugin file | |
*/ | |
public function get_plugin_dir_path() | |
{ | |
if ( empty( $this->plugin_dir_path ) ) { | |
$this->plugin_dir_path = plugin_dir_path( $this->get_plugin_file_path() ); | |
} | |
return trailingslashit( $this->plugin_dir_path ); | |
} | |
/** | |
* Get the plugin version | |
* | |
* @since 0.1 | |
* | |
* @return string Plugin version | |
*/ | |
public function get_version() | |
{ | |
return $this->version; | |
} | |
} | |
JWF_Plugin::get_instance(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment