Update of /cvsroot/php-blog/jBlog
In directory sc8-pr-cvs1:/tmp/cvs-serv17360
Modified Files:
jBlog_admin_plugins.inc.php jBlog_plugin_api.php
Log Message:
You can now add/remove instances of registered plugins.
Just visit your "Configure Plugins" admin section.
Nice and easy, and no more overhead than how it was before.
Index: jBlog_admin_plugins.inc.php
===================================================================
RCS file: /cvsroot/php-blog/jBlog/jBlog_admin_plugins.inc.php,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- jBlog_admin_plugins.inc.php 10 Mar 2003 02:13:38 -0000 1.1
+++ jBlog_admin_plugins.inc.php 10 Mar 2003 20:38:17 -0000 1.2
@@ -5,27 +5,61 @@
include_once "jBlog_plugin_api.php";
include_once "jBlog_sidebar_items.php";
-/* Temporary hack to ease dev's lives:
- * Force registration of the built-in plugins for now */
-jblog_plugin_api::register_default_plugins();
-
-if (isset($_POST['jBlog']['placement'])) {
+if (isset($_POST['SAVE']) && isset($_POST['jBlog']['placement'])) {
foreach ($_POST['jBlog']['placement'] as $plugin_name => $placement) {
jblog_plugin_api::update_plugin_placement(
addslashes($plugin_name),
addslashes($placement)
);
}
-
+}
+if (isset($_POST['NEW']) && $_POST['jBlog']['plugin_to_add'] != '0') {
+ $inst = jblog_plugin_api::create_plugin_instance($_POST['jBlog']['plugin_to_add']);
+ /* TODO: redirect to configuration for that instance */
+}
+if (isset($_POST['REMOVE'])) {
+ foreach ($_POST['jBlog']['plugin_to_remove'] as $key) {
+ jblog_plugin_api::remove_plugin_instance($key);
+ }
}
/* very simple configuration for the moment; just change the placement */
$plugins = jblog_plugin_api::enum_plugins();
+/* list of classes for creating instances */
+$classes = jblog_plugin_api::enum_plugin_classes();
+
?>
<form action="?jBlog[adminModule]=plugins" method="post">
+
+<select name="jBlog[plugin_to_add]">
+<option value="0">[ Select a plugin to add ]</option>
+<?php
+foreach ($classes as $class_name) {
+ $plugin =& jblog_plugin_api::load_plugin($class_name);
+
+ /* query for its name, description and configuration data */
+ $bag = new jBlog_property_bag;
+ $plugin->introspect($bag);
+
+ $name = htmlentities($bag->get('name'));
+ $desc = htmlentities($bag->get('description'));
+
+ echo "<option value=\"$class_name\">$name - $desc</option>\n";
+}
+
+
+?>
+</select>
+
+<input name="NEW" value="Add plugin" type="submit">
+
+<br />
+<br />
+
<table border="1" cellpadding="5">
<tr>
+<th> </th>
<th>Plugin</th>
<th>Description</th>
<th>Placement</th>
@@ -66,9 +100,12 @@
$name = "<a href=\"$url\">$name</a>";
}
- echo "<tr><td>$name</td><td>$desc</td><td>$place</td></tr>\n";
+ echo "<tr><td><input type=\"checkbox\" name=\"jBlog[plugin_to_remove][]\" value=\"$plugin_data[name]\"></td><td>$name</td><td>$desc</td><td>$place</td></tr>\n";
}
?>
</table>
+<br />
+
+<input type="submit" name="REMOVE" value="Remove Ticked Plugins">
<input type="submit" name="SAVE" value="Save">
</form>
Index: jBlog_plugin_api.php
===================================================================
RCS file: /cvsroot/php-blog/jBlog/jBlog_plugin_api.php,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- jBlog_plugin_api.php 10 Mar 2003 02:20:53 -0000 1.2
+++ jBlog_plugin_api.php 10 Mar 2003 20:38:19 -0000 1.3
@@ -1,49 +1,100 @@
<?php
if (IN_JBLOG !== true) die ("Don't hack!");
+include_once "jBlog_sidebar_items.php";
+
/* This file defines the plugin API for jBlog.
* By extending these classes, you can add your own code
* to appear in the sidebar(s) of jBlog.
*/
-
+/*
+ * The system defines a number of built-in plugins; these are
+ * identified by @class_name.
+ *
+ * Third-party plugins are identified by the name of the folder into
+ * which they were uploaded (so there is no @ sign at the start of
+ * their class name.
+ *
+ * The user creates instances of plugins; an instance is assigned
+ * an identifier like this:
+ * classname:uniqid()
+ *
+ * The user can configure instances of plugins.
+ */
class jblog_plugin_api {
function register_default_plugins()
{
- jblog_plugin_api::register_plugin(array(
- 'jBlog_calendar_plugin',
- 'jBlog_quicksearch_plugin',
- 'jBlog_archives_plugin',
- 'jBlog_topreferrers_plugin',
- 'jBlog_syndication_plugin',
- 'jBlog_superuser_plugin',
- 'jBlog_plug_plugin',
- ));
+ $classes = jblog_plugin_api::enum_plugin_classes();
+ foreach ($classes as $class_name) {
+ if ($class_name{0} == '@') {
+ jblog_plugin_api::create_plugin_instance($class_name);
+ }
+ }
}
- /* This should be used for built-in plugins only.
- * run-time registration of an existing class as a plugin */
- function register_plugin($class_names)
+ /* Create an instance of a plugin.
+ * $plugin_class_id is of the form:
+ * @class_name for a built-in plugin
+ * or
+ * plugin_dir_name for a third-party plugin
+ * returns the instance identifier for the newly created plugin.
+ *
+ * TO BE IMPLEMENTED:
+ * If $copy_from_instance is not null, and identifies another plugin
+ * of the same class, then the persistent state will be copied.
+ * This allows the user to clone a plugin.
+ */
+ function create_plugin_instance($plugin_class_id, $copy_from_instance=null)
{
global $jBlog;
- if (!is_array($class_names)) {
- $class_names = array($class_names);
- }
+ $id = md5(uniqid(""));
+
+ $key = $plugin_class_id . ":" . $id;
- foreach ($class_names as $class_name) {
- /* is it already registered ? */
- $reg = jBlog_db_query("SELECT * from $jBlog[dbPrefix]plugins where name='@$class_name'", true);
- if ($reg === false) {
- /* no, so register it */
- jBlog_db_query("INSERT INTO $jBlog[dbPrefix]plugins (name) values ('@$class_name')");
+ jBlog_db_query("INSERT INTO $jBlog[dbPrefix]plugins (name) values ('$key')");
+
+ return $key;
+ }
+
+ function remove_plugin_instance($plugin_instance_id)
+ {
+ global $jBlog;
+ jBlog_db_query("DELETE FROM $jBlog[dbPrefix]plugins where name='$plugin_instance_id'");
+ }
+
+ /* Retrieves a list of available plugins */
+ function enum_plugin_classes()
+ {
+ global $jBlog;
+
+ $classes = array();
+
+ /* built-in classes first */
+ $cls = get_declared_classes();
+ foreach ($cls as $class_name) {
+ $p = get_parent_class($class_name);
+ while ($p != 'jblog_plugin' && $p !== false) {
+ $p = get_parent_class($p);
+ }
+ if ($p == 'jblog_plugin') {
+ $classes[] = '@' . $class_name;
}
}
+ /* third-party classes next */
+ $d = opendir($jBlog['jBlogPath'] . "plugins");
+ while (($f = readdir($d)) !== false) {
+ if ($f{0} == '.' || $f == 'CVS')
+ continue;
+ $classes[] = $f;
+ }
+ return $classes;
}
-
- /* Retrieves a list of plugin records */
+
+ /* Retrieves a list of plugin instances */
function enum_plugins($filter = '*')
{
global $jBlog;
@@ -57,8 +108,10 @@
}
/* Creates an instance of a named plugin */
- function &load_plugin($name)
+ function &load_plugin($instance_id)
{
+ list($name, $uniq) = explode(":", $instance_id);
+
if ($name{0} == '@') {
$class_name = substr($name, 1);
} else {
@@ -67,7 +120,8 @@
return false;
}
- $p =& new $class_name;
+ $p =& new $class_name($instance_id);
+
return $p;
}
@@ -138,6 +192,14 @@
}
class jBlog_plugin {
+ var $instance = null;
+
+ /* Be sure to call this method from your derived classes constructors,
+ * otherwise your config data will not be stored or retrieved correctly */
+ function jBlog_plugin($instance)
+ {
+ $this->instance = $instance;
+ }
/* Called by jBlog when it wants to display information
* about your plugin.
@@ -186,7 +248,7 @@
/* Fetches a configuration value for this plugin */
function get_config($name, $defaultvalue = null)
{
- $name = get_class($this) . ":" . $name;
+ $name = $this->instance . "/" . $name;
return jBlog_get_config_var($name, $defaultvalue);
}
|