-
jLinux
The Linux Dude
-
Posts: 157
Threads: 35
Joined: Jun 2015
Reputation:
2
09-25-2015, 02:25 PM
(This post was last modified: 09-26-2015, 11:08 AM by jLinux.)
I needed a decent plugin system, I tried this one, and while im sure it was awesome when it first came out, I had some issues with it (Deprecated functions used, Plugins couldn't be OOP, didn't use a model, etc)
So I created my own, and thought id share it: https://github.com/jhyland87/CI3_Plugin_System
Ill add a better README later, and some demos and instructions, so just bookmark it for meow.
Heres an example plugin..
PHP Code: /** * Plugin Name: Hello World * Plugin URI: https://github.com/jhyland87/CI3_Plugin_System * Version: 1.0 * Description: Apply colors to asset table cells or rows depending on values * Author: Justin Hyland * Author URI: http://justinhyland.com */ class Hello_world extends CI3_plugin_system { use plugin_trait;
public function __construct() { parent::__construct();
add_filter('plugin_test.name', [$this,'alter_name'], 10);
add_action('plugin_test.log', [$this, 'log_stuff']);
}
// Controller for plugin, used to manage the plugin, not required though. public function controller($data = NULL) { $content = '';
// See if form was submitted if($foo = $this->post('foo')) { // Do something with POST data $content .= "You said <strong>{$foo}</strong>..<hr>"; }
$content .= '<form action="" method="POST">' . '<input type="text" name="foo" value="' . @$foo . '"><br>' . '<input type="submit">' . '</form>';
return $content; }
// Just an example filter to alter the values of an array public function alter_name($data) { array_walk($data, function(&$item, $key){ if(strtolower($item) === 'jane') $item = 'john';
$item = ucfirst($item); });
return $data; }
// Example plugin action, just logs.. stuff public function log_stuff($prefix, $data) {
log_message('info', "[{$prefix}] " . __METHOD__ . ": Logging stuff - " . ((is_array($data) || is_object($data)) ? serialize($data) : $data)); } }
Then to use it, just put a bunch of do_action() functions all over..
PHP Code: // Fire off an action do_action('namespace.action1');
// Fire off an action with parameters do_action('namespace.action2', array('hello','world'));
// Filter some data $name = do_action('namespace.action3', array('John','Doe'));
And before you go through the code and tear it apart, I'm just gonna say, I'm a Linux Engineer, not a PHP developer, I just do PHP on my free time, and I'm in the middle of a PHP project that I needed this for.
Thanks!
P.S. Here are some of the main functions and the comments for them, just because
PHP Code: /** * Update All Plugin Headers * * Execute self::update_plugin_headers for each plugin found in static::$plugins * * @access public * @since 0.1.0 * @return boolean */ update_all_plugin_headers();
/** * Update Plugin Headers * * Parse a given plugins PHP file for the header information in the comments, and update the database info * accordingly * * @param string $plugin Plugin System Name to check * @access public * @todo Try to retrieve only the top X lines of the file, not the whole file * @since 0.1.0 * @return TRUE Always true */ update_plugin_headers($plugin);
/** * Install Plugin * * Install a plugin by adding it to the database and executing any installation code thats in * the plugins install method * * @param string $plugin Plugins system name (Folder name) * @access public * @param boolean */ install_plugin($system_name);
/** * Enable Plugin * * Enable a plugin by setting the plugins.status to 1 in the plugins table * * @oaram string $plugin Plugin Name * @param mixed $data Any data that should be handed down to the * plugins deactivate method (optional) * @access public * @since 0.1.0 * @return bool */ enable_plugin($system_name, $data);
/** * Disable Plugin * * Disable a plugin by setting the plugins.status to 0 in the plugins table * * @oaram string $plugin Plugin Name * @param mixed $data Any data that should be handed down to the * plugins activate method (optional) * @access public * @since 0.1.0 * @return bool */ disable_plugin($system_name, $data);
/** * Plugin Details * * Retrieve the details of a plugin from the database * * @param string $plugin Plugin system name * @access public * @since 0.1.0 * @return object|bool */ plugin_details($system_name);
/** * Get Messages * * Get all messages, or a specific type of message * * @param string $type Type of error to retrieve (error, debug, warn) * @access public * @since 0.1.0 * @return array|bool */ get_messages();
/** * Print Messages * * Print all messages, or messages of a certain type * * @param string $type Type of error to display (error, debug, warn) * @access public * @since 0.1.0 * @return array|bool */ print_messages();
/** * Get Orphaned Plugins * * Look in the plugin directory for any folders that do not have an associated entry * in the plugins table * * @access public * @since 0.1.0 * @return array|bool If no orphaned plugins are found, return false */ get_orphaned_plugins();
/** * Add Action * * Assign a function to be executed by a certain tag. An action will just fire off a function * when the tag is called, a filter will parse data and return the modified data * * @param string $tag Tag to add filter to * @param string|array $function Either a string (function), or an array (Object, method) * @param integer $priority Priority * @param string $type Either action or filter * @access public * @since 0.1.0 * @return boolean */ add_action('namespace.action', [$this, 'method'], 10); add_action('namespace.action', 'print_r');
/** * Add Filter * * Just a wrapper for add_function except adds it as type 'filter'. Filters will * take in data and perform an action on it, then return it, actions will just * fire off a function * * @param string $tag Tag to add filter to * @param string|array $function Either a string (function), or an array (Object, method) * @param integer $priority Priority * @access public * @since 0.1.0 * @return boolean */ add_filter('namespace.tag', [$this, 'method', 2]);
/** * Get Actions * * Gets actions.... * * @access public * @since 0.1.0 * @return array */ get_actions();
/** * Print Plugins * * Retrieve all plugin information from the database * * @access public * @since 0.1.0 * @return array */ retrieve_plugins();
/** * Do Action * * Execute a specific action, pass optional arguments to it * @param string $tag Tag to execute * @param null $args Arguments to hand to functions assigned to tag * @access public * @since 0.1.0 * @return mixed Returns whatever the type of $args is */ do_action('namespace.tag', ['one','two']);
/** * Remove Action * * Remove a function from an action * * @param string $tag Tag to check in * @param mixed $function Function to be removed * @param integer $priority Priority to check for function * @access public * @since 0.1.0 * @return boolean */ remove_action('namespace.tag', $func);
/** * Current Action * * Set the currently running action * * @access public * @since 0.1.0 * @return string */ current_action();
/** * Has Run * * See if a particular action has ran yet * * @param string $action Action to check for * @access public * @since 0.1.0 * @return boolean */ has_run('namespace.tag');
/** * Doing Action * * If the param is NULL, then this will return what action is being executed, * if an action is supplied, then it will return boolean based on if that action * is being executed or not * * @param null $action Action to check for */ doing_action('namespace.tag');
/** * Did Action * * Returns if a tag has been fired or not * * @param string $tag Tag to check if ran or not */ did_action('namespace.tag');
|