<?php # -*- coding: utf-8 -*- /** * Plugin Name: Plugin Class Demo * Description: How I am using the base class in plugins. * Plugin URI: * Version: 2012.09.29 * Author: Thomas Scholz * Author URI: http://toscho.de * License: GPL * Text Domain: plugin_unique_name * Domain Path: /languages */ /* * I prefer the empty-constructor-approach showed here. * * Advantages: * - Unit tests can create new instances without activating any hooks * automatically. No Singleton. * * - No global variable needed. * * - Whoever wants to work with the plugin instance can just call * T5_Plugin_Class_Demo::get_instance(). * * - Easy to deactivate. * * - Still real OOP: no working methods are static. * * Disadvantage: * - Maybe harder to read? * */ add_action( 'plugins_loaded', array ( T5_Plugin_Class_Demo::get_instance(), 'plugin_setup' ) ); class T5_Plugin_Class_Demo { /** * Plugin instance. * * @see get_instance() * @type object */ protected static $instance = NULL; /** * URL to this plugin's directory. * * @type string */ public $plugin_url = ''; /** * Path to this plugin's directory. * * @type string */ public $plugin_path = ''; /** * Access this plugin’s working instance * * @wp-hook plugins_loaded * @since 2012.09.13 * @return object of this class */ public static function get_instance() { NULL === self::$instance and self::$instance = new self; return self::$instance; } /** * Used for regular plugin work. * * @wp-hook plugins_loaded * @since 2012.09.10 * @return void */ public function plugin_setup() { $this->plugin_url = plugins_url( '/', __FILE__ ); $this->plugin_path = plugin_dir_path( __FILE__ ); $this->load_language( 'plugin_unique_name' ); // more stuff: register actions and filters } /** * Constructor. Intentionally left empty and public. * * @see plugin_setup() * @since 2012.09.12 */ public function __construct() {} /** * Loads translation file. * * Accessible to other classes to load different language files (admin and * front-end for example). * * @wp-hook init * @param string $domain * @since 2012.09.11 * @return void */ public function load_language( $domain ) { load_plugin_textdomain( $domain, FALSE, $this->plugin_path . '/languages' ); } }