Skip to content

Commit

Permalink
PLUGINS, ACTIONS & FILTERS: This code allows you to add a 'plugin' in…
Browse files Browse the repository at this point in the history
… the /plugins folder which will augment functionality provided by XOT. It allows you to create single file or folder style plugins (similar to Wordpress) which add functionality to various 'hook' points within the page. These hooks are by no means definitive and hopefully we can add lots more, however for filters to work in a better way some code will need re-written in order to pass 'content' through the filter before being used or echo-ed..

The current files should do nothing to an install. The example plugins are commented out - remove the 'REMOVE_THIS' text from the filenames in order to activate. The 'GCU Plugin' demonstrates some potentially useful functionality:

It adds a banner at the top of the page (login and editor)
It changes the <title> text
It changes the "My Projects" text to "John's Projects"
It adds text to Pod 1
It adds a title slide to data.xml on save

The plugin_one plugin simply inserts HTML comments at action hook points throughout the page. Enable this by taking out REMOVE_THIS and then refresh the page and view the source to reveal it's result...

git-svn-id: https://xerteonlinetoolkits.googlecode.com/svn/trunk@709 912cdd6b-5c7d-d5a7-a2ba-d0f0cdb91641
  • Loading branch information
JohnSmith-LT committed Mar 4, 2013
1 parent ab92163 commit 8cfccee
Show file tree
Hide file tree
Showing 6 changed files with 1,080 additions and 14 deletions.
22 changes: 14 additions & 8 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php

// Load the plugin files and fire a startup action
require_once(dirname(__FILE__) . "/plugins.php"); startup();


require_once(dirname(__FILE__) . "/config.php");

_load_language_file("/index.inc");
Expand Down Expand Up @@ -29,7 +34,7 @@
*/
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head>

<?php head_start();?>
<!--

University of Nottingham Xerte Online Toolkits
Expand All @@ -41,7 +46,7 @@
-->

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?PHP echo $xerte_toolkits_site->site_title; ?></title>
<title><?PHP echo apply_filters("head_title", $xerte_toolkits_site->site_title); ?></title>

<link href="website_code/styles/frontpage.css" media="screen" type="text/css" rel="stylesheet" />
<link href="website_code/styles/folder_popup.css" media="screen" type="text/css" rel="stylesheet" /><?PHP
Expand All @@ -64,7 +69,7 @@
_include_javascript_file("website_code/scripts/logout.js");
_include_javascript_file("website_code/scripts/import.js");
?>
</head>
<?php head_end();?></head>

<!--

Expand All @@ -75,7 +80,7 @@
-->

<body onload="javascript:sort_display_settings()" onselectstart="return false;" onscroll="body_scroll()">

<?php body_start();?>
<!--

Folder popup is the div that appears when creating a new folder
Expand Down Expand Up @@ -124,7 +129,7 @@
<div class="top_left sign_in_TL m_b_d_2_child">
<div class="top_right sign_in_TR m_b_d_2_child">
<p class="heading">
<?PHP echo INDEX_WORKSPACE_TITLE; ?>
<?PHP echo apply_filters('page_title', INDEX_WORKSPACE_TITLE);?>
</p>
</div>
</div>
Expand Down Expand Up @@ -184,11 +189,11 @@
</div>
<div class="border" style="margin-top:10px"></div>
<div class="help" style="width:48%">
<?PHP echo $xerte_toolkits_site->pod_one; ?>
<?PHP echo apply_filters('editor_pod_one', $xerte_toolkits_site->pod_one); ?>
</div>

<div class="help" style="width:48%; float:right;">
<?PHP echo $xerte_toolkits_site->pod_two; ?>
<?PHP echo apply_filters('editor_pod_two', $xerte_toolkits_site->pod_two); ?>
</div>
</div>

Expand Down Expand Up @@ -239,5 +244,6 @@
</div>


</body>
<?php body_end();?></body>
</html>
<?php shutdown();?>
17 changes: 11 additions & 6 deletions modules/xerte/engine/save.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
<?php
/**
*
*
* Save page, used by xerte to update its XML files
*
* @author Patrick Lockley
Expand All @@ -10,6 +10,7 @@
*/

require_once("../../../config.php");
require_once("../../../plugins.php");

if(!isset($_SESSION['toolkits_logon_username'])) {
print "You are not logged in.";
Expand All @@ -29,6 +30,8 @@

}

$filedata = apply_filters("editor_save_data", $_POST['filedata']);

/**
* Save and play do slightly different things. Save sends an extra variable so we update data.xml as well as preview.xml
*/
Expand All @@ -37,13 +40,13 @@

$file_handle = fopen($xerte_toolkits_site->root_file_path . $savepath,'w');

if(fwrite($file_handle, stripslashes($_POST['filedata']))!=false){
if(fwrite($file_handle, stripslashes($filedata))!=false){

receive_message($_SESSION['toolkits_logon_username'], "ADMIN", "SUCCESS", "Template " . $_POST['filename'] . " saved" , stripslashes($_POST['filedata']));
receive_message($_SESSION['toolkits_logon_username'], "ADMIN", "SUCCESS", "Template " . $_POST['filename'] . " saved" , stripslashes($filedata));

}else{

receive_message($_SESSION['toolkits_logon_username'], "ADMIN", "CRITICAL", "Template " . $_POST['filename'] . " failed to save" , stripslashes($_POST['filedata']));
receive_message($_SESSION['toolkits_logon_username'], "ADMIN", "CRITICAL", "Template " . $_POST['filename'] . " failed to save" , stripslashes($filedata));

}

Expand All @@ -55,9 +58,11 @@
* Update preview.xml
*/

$filedata = apply_filters("editor_save_preview", $_POST['filedata']);

$file_handle = fopen($xerte_toolkits_site->root_file_path . $_POST['filename'],'w');

if(fwrite($file_handle, stripslashes($_POST['filedata']))!=false){
if(fwrite($file_handle, stripslashes($filedata))!=false){

receive_message($_SESSION['toolkits_logon_username'], "ADMIN", "SUCCESS", "Template " . $_POST['filename'] . " saved" , stripslashes($_POST['filedata']));

Expand Down
116 changes: 116 additions & 0 deletions plugins.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

/**
* Action and Filter Plugins for Xerte by John Smith @ Glasgow Caledonian University
*
* First define some constants that we need
*/
define('D_S', DIRECTORY_SEPARATOR);
define('PLUGINS_PATH', dirname(__FILE__) . D_S . 'plugins');


/**
* We will be absorbing the Wordpress plugins.php code
* Wordpress 'plugins.php' has been renamed 'wp_plugins.php' to identify Wordpress codebase
*/
require_once(dirname(__FILE__) . D_S . 'website_code' . D_S . 'php' . D_S . 'wp_plugins.php');


/**
* Traverse the /plugins folder, if available and include any plugins that are found
* Handles both single file plugins and also folder style plugins containing js/css/images etc
*/
if ($dir = opendir(PLUGINS_PATH)) {

while (false !== ($filename = readdir($dir)) && $dir) {

if ($filename != "." && $filename != "..") {

if (is_dir(PLUGINS_PATH . D_S . $filename)) {

$plugin_file = PLUGINS_PATH . D_S . $filename . D_S . $filename . ".php";

if (file_exists($plugin_file)) {

require_once($plugin_file);
}
}
else {

$plugin_file = PLUGINS_PATH . D_S . $filename;

if (file_exists($plugin_file) && pathinfo($plugin_file, PATHINFO_EXTENSION) == 'php') {

require_once($plugin_file);

}
}
}
}

closedir($dir);
}


/**
* After the plugins are all loaded, let's trigger an action, just in case we can use it later
*/
do_action("plugins_loaded");


/**
* This function returns the name of the main script minus the .php extension in
* order to help identify the current page
*
* @return array Filename of current script without .php and logged in status
*/
function plugins_get_context() {

global $authmech;
if (!$authmech) $authmech = Xerte_Authentication_Factory::create($xerte_toolkits_site->authentication_method);
$basename = basename($_SERVER['SCRIPT_NAME'], '.php');
$logged_in = !$authmech->needsLogin();
return array($basename, $logged_in);

}


/**
* This function returns true if we're on initial login page
*/
function is_login_page() {

$context = plugins_get_context();
//print_r($context);
return ($context[0] == "index" && !$context[1]);

}


/**
* This function returns true if we're on editor page
*/
function is_editor_page() {

$context = plugins_get_context();
return ($context[0] == "index" && $context[1]);

}


/**
* Helper functions to trigger the appropriate action
*
* @return void
*/
function head_start() { do_action("head_start"); }

function head_end() { do_action("head_end"); }

function body_start() { do_action("body_start"); }

function body_end() { do_action("body_end"); }

function startup() { do_action("startup"); }

function shutdown() { do_action("shutdown"); }
73 changes: 73 additions & 0 deletions plugins/gcu_plugin/gcu_plugin.phpREMOVE_THIS
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

if(!class_exists('GCU_Plugin')) {

class GCU_Plugin {

public static function insert_banner() {
echo('<div style="width: 910px; margin-left: auto; margin-right: auto; background: #00529c url(http://www.gcu.ac.uk/media/gcalwebv2/gcal-site/img/site/header/header-bg.gif) repeat-x left top;">');
echo('<a href="http://www.gcal.ac.uk/">');
echo('<img src="http://www.gcu.ac.uk/media/gcalwebv2/gcal-site/img/site/generic/gcal-logo.gif" alt="Visit Glasgow Caledonian University website" border="0" /></a>');
echo('</div>');
}

public static function change_head_title($title) {
$title = "Xerte @ Glasgow Caledonian University";
return $title;
}

public static function change_page_title($title) {
$firstname = "";
if (isset($_SESSION['toolkits_firstname'])) {
$firstname = $_SESSION['toolkits_firstname'];
}
else {
global $authmech;
if ($authmech) {
$firstname = $authmech->getFirstname(); // BUG?? Returns wrong name...
}
}

if ($firstname) {
$title = str_replace("My", "$firstname's", $title);
}

return $title;
}

public static function insert_title_slide($haystack) {
$needle = '>';
$endpoint = strpos($haystack, $needle);

if ($endpoint > 0) {
$new_slide = "\n\t\t".'<title name="GCU Learning Objects present" size="30"><![CDATA[A John Smith Production]]></title>';
$haystack = substr($haystack, 0, $endpoint+1) . $new_slide . substr($haystack, $endpoint+1);
}
return $haystack;
}

public static function rewrite_pod_one($pod) {
return "<b>This added by GCU Plugin</b>" . $pod;
}
}


/**
* Wire up the actions
*/
add_action('body_start', array('GCU_Plugin', 'insert_banner'));


/**
* Wire up the filters
*/
add_filter('head_title', array('GCU_Plugin', 'change_head_title'));
add_filter('page_title', array('GCU_Plugin', 'change_page_title'));
add_filter('editor_save_data', array('GCU_Plugin', 'insert_title_slide'));
add_filter('login_pod_one', array('GCU_Plugin', 'rewrite_pod_one'));
add_filter('editor_pod_one', array('GCU_Plugin', 'rewrite_pod_one'));

}
else {
// TODO : log an error that there's been a collision
}
Loading

0 comments on commit 8cfccee

Please sign in to comment.