Created
November 7, 2010 05:31
-
-
Save danott/665974 to your computer and use it in GitHub Desktop.
A template for EE plugins
This file contains hidden or 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 Template ExpressionEngine Plugin v1.0 | |
| * | |
| * Provides a simple framework for developing EE plugins. | |
| * | |
| * Copyright 2010, Daniel Ott | |
| * Dual licensed under the MIT or GPL Version 2 licenses. | |
| */ | |
| $plugin_info = array( | |
| 'pi_name' => 'Plugin Template', | |
| 'pi_version' => '1.0', | |
| 'pi_author' => 'Daniel Ott', | |
| 'pi_author_url' => 'http://danott.us/', | |
| 'pi_description' => 'This plugin will be a real boy some day', | |
| 'pi_usage' => Plugin_template::usage() | |
| ); | |
| /** | |
| * Super Class | |
| * | |
| * @package Plugin Template | |
| * @category Plugin | |
| * @author Daniel Ott | |
| * @copyright Copyright (c) 2010 Daniel Ott | |
| * @link http://danott.us | |
| */ | |
| class Plugin_template { | |
| /* | |
| * Initialize variables for EE1 and EE2 use in a single plugin. | |
| * They're later referenced in the constructor based on app version number | |
| */ | |
| var $TMPL; | |
| var $return_data; | |
| var $tagdata; | |
| /* Parameters | |
| * An array of 'key' => 'value' pairs that can be set for this plugin. | |
| * Utilized by @danott EE-Params | |
| */ | |
| var $params = array( | |
| 'entry_id' => NULL, | |
| 'vars' => NULL | |
| ); | |
| /* Set From Embed | |
| * An array of 'key' => 'value' pairs that can be set for this plugin. | |
| * If a value occurs in $params, and you would like it to be able to be set | |
| * by an embed tag, put it in this array as well with the value TRUE. | |
| * Utilized by @danott EE-Params | |
| */ | |
| var $set_from_embed = array( | |
| 'entry_id' => TRUE | |
| ); | |
| /** | |
| * Plugin_template() Constructor | |
| * | |
| * Execute the function of this plugin. | |
| */ | |
| function Plugin_template() | |
| { | |
| /* EE1 & EE2 Compatability! Booyah! */ | |
| if (version_compare(APP_VER, '2', '<')) | |
| { | |
| // EE 1.x is in play | |
| global $TMPL; | |
| $this->TMPL =& $TMPL; | |
| } else { | |
| // EE 2.x is in play | |
| $this->EE =& get_instance(); | |
| $this->TMPL =& $this->EE->TMPL; | |
| } | |
| /* @danott ee-params | |
| * In pursuit of a one-size-fits all approach for setting EE Plugin parameters */ | |
| foreach ($this->params as $param_array_key => &$param_array_value) | |
| { | |
| /* Save the default value in a temporary variable. | |
| * We will revert to this value later if the parameter isn't set by other means. */ | |
| $default = $param_array_value; | |
| $param_array_value = NULL; | |
| /* First precedence - | |
| * Parameter set using the plugin parameters. */ | |
| if ($this->TMPL->fetch_param($param_array_key)) | |
| { | |
| $param_array_value = $this->TMPL->fetch_param($param_array_key); | |
| } | |
| /* Second precedence - | |
| * Parameter set using embed parameters. | |
| * To set using embed parameters, a key must be set to TRUE in $this->set_from_embed['var'] = TRUE; | |
| * Also, it must not have been set in the plugin parameters. | |
| * If this is true, and the embed parameter exists, we'll set it. */ | |
| if (! isset($param_array_value) | |
| && isset($this->set_from_embed) | |
| && array_key_exists($param_array_key, $this->set_from_embed) | |
| && $this->set_from_embed[$param_array_key] === TRUE | |
| && isset($TMPL->embed_vars["embed:" . $param_array_key])) | |
| { | |
| $param_array_value = $TMPL->embed_vars["embed:" . $param_array_key]; | |
| } | |
| /* PLUMBING | |
| * Seperate parameter into an array using the pipe character. This way, you can pass | |
| * {exp:plugin vars="1|2|3|4"} | |
| * | |
| * This will create an array in the form of | |
| * array("1","2","3","4"); | |
| */ | |
| if (isset($param_array_value)) | |
| { | |
| // This lookbehind regex allows for escaping the pipe character \| in passed values | |
| $parts = preg_split("/(?<!\\\)\|/", $param_array_value, false, PREG_SPLIT_NO_EMPTY); | |
| foreach ($parts as $parts_index => &$part ) | |
| { | |
| // Unescape pipe characters in all the parts | |
| $part = str_replace('\\|', '|', $part); | |
| /* COLONOSCOPY | |
| * In the "plumbing" stage we already split the value into an array. | |
| * This logic allows passing parameters in the form of: | |
| * {exp:plugin vars="foo:bar|biz:baz"} | |
| * And parsing it into an array such that | |
| * $params['vars'] = array('foo' => "bar", 'biz' => "baz") | |
| * | |
| * Try to seperate using the colon. On the other side of the colon is excrement. Poop joke. */ | |
| if (preg_match("/^([\w-]*):(.*)$/", $part, $excrement)) | |
| { | |
| // Set the 'key' => 'value' pair. ($excrement[0] is the entire matched $part) | |
| $parts[$excrement[1]] = $excrement[2]; | |
| // The standard numbered index would still exist. It's not needed anymore. | |
| unset($parts[$parts_index]); | |
| } | |
| } | |
| // The parsed value is no good to us if we don't actually save it. Save it! | |
| $param_array_value = $parts; | |
| } // end if (isset($param_array_value)) | |
| /* Let's consider the case where we used the tag {exp:plugin var="value"} | |
| * At this point in the process we would have: $params['var'] = array('value'); | |
| * There's a 99% chance that is not what we want. We'd rather have: $params['var'] = 'value' | |
| * This if-statement does exactly that. */ | |
| if (count($param_array_value) == 1) | |
| { | |
| $param_array_value = $param_array_value[0]; | |
| } | |
| /* If we get to this point, and nothing has been set, | |
| * we can revert to the default value */ | |
| if (! isset($param_array_value)) | |
| { | |
| $param_array_value = $default; | |
| } | |
| } // end foreach ($params as $param_array_key => &$attributes) | |
| // end @danott ee-params | |
| // The data between the {exp:plugin} and {/exp:plugin} | |
| $this->tagdata = $this->TMPL->tagdata; | |
| } // end of Constructor function | |
| /** | |
| * usage() | |
| * Information for the plugin within the EE Control Panel | |
| */ | |
| function usage() | |
| { | |
| ob_start(); | |
| ?> | |
| This plugin could be described here. | |
| <?php | |
| $buffer = ob_get_contents(); | |
| ob_end_clean(); | |
| return $buffer; | |
| } // end of function usage() | |
| } // end of Loopee class | |
| /* end of file pi.plugin_template.php */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment