Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Anonymous objects are anonymous #1

Merged
merged 3 commits into from

2 participants

@Tarendai

A few changes:

  • Some formatting consistency changes, if( vs if ( wasn't consistently being used
  • Removed the trailing ?>, potential cause of issues, works better without it
  • The class was created as an anonymous object, which can cause problems "How would you remove an action or a filter of an anonymous object?", a primary instance is now stored as a static var, and created on the wp_loaded hook, so no more global namespace polution
@bradvin
Owner

This looks great - thanks
Just want to clarify one thing :

add_action( 'wp_loaded', PluginName::get_instance() );

will that not create the instance as soon as the line is executed rather than when the action wp_loaded is fired?

I am thinking something along these lines

add_action( 'wp_loaded', create_function('', 'PluginName::get_instance();') );

What do you think?

@Tarendai

create_function is effectively an eval call, and yes it would,

I'm basing it off of this:

https://gist.github.com/toscho/5258504

if ( T5_Spam_Block::start_me() )
    add_action(
        'wp_loaded',
        array ( T5_Spam_Block::get_instance(), 'plugin_setup' )
    );

So it should probably be changed again to follow that closer

@Tarendai

Creation now takes the form:

if ( PluginName::start_me() ) {
  add_action( 'init', array( PluginName::get_instance(), 'init' );
}

So takes care of init, adds a mechanism to prevent plugin loading if it's not needed, and doesn't polute the global namespace. Also takes the init action out of the constructor

@bradvin bradvin merged commit e45e5b7 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 43 additions and 33 deletions.
  1. +43 −33 plugin.php
View
76 plugin.php
@@ -12,7 +12,7 @@
Copyright 2011 TODO (email@domain.com)
This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License, version 2, as
+ it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
@@ -23,73 +23,83 @@
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-
+
*/
class PluginName {
-
+
/*--------------------------------------------*
* Constants
*--------------------------------------------*/
const name = 'Plugin Name';
const slug = 'plugin-name-slug';
-
+
+ public static $main_instance;
+
+ public static function get_instance() {
+ if ( !isset( self::$main_instance ) ) {
+ self::$main_instance = new PluginName();
+ }
+ return self::$main_instance;
+ }
+
+ public static function start_me() {
+ return true;
+ }
+
/**
* Constructor
*/
function __construct() {
//register an activation hook for the plugin
register_activation_hook( __FILE__, array( &$this, 'install_plugin' ) );
-
- //Create an init action
- add_action( 'init', array( &$this, 'init_plugin' ) );
}
-
+
/**
* Runs when the plugin is activated
- */
+ */
function install_plugin() {
// do not generate any output here
}
-
+
/**
* Runs when the plugin is initialized
*/
function init_plugin() {
// Setup localization
load_plugin_textdomain( self::slug, false, dirname( plugin_basename( __FILE__ ) ) . '/lang' );
-
+
// Load JavaScript and stylesheets
$this->register_scripts_and_styles();
-
+
// Register a shortcode
- add_shortcode('shortcode', 'render_shortcode');
-
+ add_shortcode( 'shortcode', 'render_shortcode' );
+
if ( is_admin() ) {
//this will run when in the WordPress admin
} else {
//this will run when on the frontend
}
-
+
/*
* TODO: Define custom functionality for your plugin here
*
- * For more information:
+ * For more information:
* http://codex.wordpress.org/Plugin_API#Hooks.2C_Actions_and_Filters
*/
add_action( 'TODO', array( $this, 'action_callback_method_name' ) );
- add_filter( 'TODO', array( $this, 'filter_callback_method_name' ) );
+ add_filter( 'TODO', array( $this, 'filter_callback_method_name' ) );
}
function action_callback_method_name() {
// TODO define your action method here
}
-
+
function filter_callback_method_name() {
// TODO define your filter method here
}
-
- function render_shortcode($atts) {
+
+ function render_shortcode( $atts ) {
// Extract the attributes
extract(shortcode_atts(array(
'attr1' => 'foo', //foo is a default value
@@ -97,7 +107,7 @@ function render_shortcode($atts) {
), $atts));
// you can now access the attribute values using $attr1 and $attr2
}
-
+
/**
* Registers and enqueues stylesheets for the administration panel and the
* public facing site.
@@ -106,12 +116,12 @@ private function register_scripts_and_styles() {
if ( is_admin() ) {
$this->load_file( self::slug . '-admin-script', '/js/admin.js', true );
$this->load_file( self::slug . '-admin-style', '/css/admin.css' );
- } else {
+ } else {
$this->load_file( self::slug . '-script', '/js/widget.js', true );
$this->load_file( self::slug . '-style', '/css/widget.css' );
} // end if/else
} // end register_scripts_and_styles
-
+
/**
* Helper function for registering and enqueueing scripts and styles.
*
@@ -120,23 +130,23 @@ private function register_scripts_and_styles() {
* @is_script Optional argument for if the incoming file_path is a JavaScript source file.
*/
private function load_file( $name, $file_path, $is_script = false ) {
-
- $url = plugins_url($file_path, __FILE__);
- $file = plugin_dir_path(__FILE__) . $file_path;
+ $url = plugins_url( $file_path, __FILE__ );
+ $file = plugin_dir_path( __FILE__ ) . $file_path;
- if( file_exists( $file ) ) {
- if( $is_script ) {
- wp_register_script( $name, $url, array('jquery') ); //depends on jquery
+ if ( file_exists( $file ) ) {
+ if ( $is_script ) {
+ wp_register_script( $name, $url, array( 'jquery' ) ); //depends on jquery
wp_enqueue_script( $name );
} else {
wp_register_style( $name, $url );
wp_enqueue_style( $name );
} // end if
} // end if
-
+
} // end load_file
-
+
} // end class
-new PluginName();
-?>
+if ( PluginName::start_me() ) {
+ add_action( 'init', array( PluginName::get_instance(), 'init' ) );
+}
Something went wrong with that request. Please try again.