e52abefd by Chris Boden

Added MenuWidget

1 parent 3b5668b0
// Lines of code vs jQuery: +30ish
// Performance gain: Yes
// Time tanken vs jQuery implementation: substantially less
// Does it work: Fuck yes (jQuery didn't, dear jQuery: LEARN TO CLONE PROPERLY!)
// Native JavaScript: Tried, tested and true!
var MenuWidget = function() {
var init = function(e) {
jQuery(document).ajaxSuccess(doLinks);
doLinks();
}
var doLinks = function() {
var Links = document.getElementsByTagName('a');
for (var i = 0, len = Links.length; i < len; i++) {
var strRel = ' ' + Links[i].rel + ' ';
if (strRel.indexOf(' TzMenuDone ') != -1) {
continue;
}
var strClass = ' ' + Links[i].className + ' ';
if (strClass.indexOf(' TzAddMenuItem ') != -1) {
Links[i].onclick = function() { return false; };
addEvent(Links[i], 'click', addLink);
}
if (strClass.indexOf(' TzDelMenuItem ') != -1) {
Links[i].onclick = function() { return false; };
addEvent(Links[i], 'click', delLink);
}
}
}
var addLink = function(e) {
var p = e.target;
while (p.tagName != 'body' && p.className != 'TzMenuPageList') {
p = p.parentNode;
}
if (p.tagName == 'body') {
console.error('Something missing in widget!');
return;
}
var Container = document.createElement('span');
Container.className = 'TzPageContainer';
var Copy = p.getElementsByTagName('select')[0].cloneNode(true);
Copy.id = '';
var Del = document.createElement('a');
Del.href = '#';
Del.appendChild(document.createTextNode('Del'));
Del.onclick = function() { return false; };
Del.className = 'TzDelMenuItem';
addEvent(Del, 'click', delLink);
Container.appendChild(Copy);
Container.appendChild(Del);
p.appendChild(Container);
}
var delLink = function(e) {
var Container = e.target;
while (Container.tagName != 'body' && Container.className != 'TzPageContainer') {
Container = Container.parentNode;
}
if (Container.tagName == 'body') {
console.error('Container not found');
return;
}
Container.parentNode.removeChild(Container);
}
addEvent(window, 'load', init);
}();
\ No newline at end of file
<?php
class MenuWidget extends WP_Widget {
public static function init() {
register_widget(__CLASS__);
if (is_admin()) {
_enqueue_script('tz-menu-widget', plugins_url('MenuWidget.js', __FILE__), Array('addEvent','jQuery'));
}
}
public function __construct() {
static $options = Array(
'classname' => ''
, 'description' => 'Configurable menu to site hierarchy'
);
parent::__construct('tz-menu-widget', 'Menu', $options);
}
public function widget($args, $instance) {
static $all_pages = false;
foreach ($instance['pages'] as $page) {
if ($instance['inclusive'] == 0) {
$instance['child_of'] = $page;
_list_pages($instance);
continue;
}
if (!$all_pages) {
$all_pages = get_pages(Array('sort_column' => 'menu_order'));
}
$instance['exclude'] = '';
$children = Array($page);
foreach ($all_pages as $key => $wp_page) {
if ($wp_page->post_parent == $page || in_array($wp_page->post_parent, $children)) {
$children[] = $wp_page->ID;
}
if (!in_array($wp_page->ID, $children)) {
if (!empty($instance['exclude'])) {
$instance['exclude'] .= ',';
}
$instance['exclude'] .= $wp_page->ID;
}
}
_list_pages($instance);
}
}
public function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['pages'] = Array();
$instance['title_li'] = strip_tags(stripslashes($new_instance['title_li']));
$instance['pages'] = (array)$new_instance['pages']; // (int)strip_tags(stripslashes($new_instance['page']));
$instance['depth'] = (int)strip_tags(stripslashes($new_instance['depth']));
$instance['inclusive'] = ($new_instance['inclusive'] == 'on' ? 1 : 0);
return $instance;
}
public function form($instance) {
$instance = _parse_args((array)$instance, Array('title_li' => '', 'pages' => Array(0 => ''), 'depth' => '', 'inclusive' => ''));
$checked = ($instance['inclusive'] == 1 ? ' checked="checked" ' : '');
?>
<p>
<label for="<?php echo $this->get_field_id('title_li'); ?>">Title</label>
<br /><input type="text" id="<?php echo $this->get_field_id('title_li'); ?>" name="<?php echo $this->get_field_name('title_li'); ?>" value="<?php echo attribute_escape($instance['title_li']); ?>" />
</p>
<p class="TzMenuPageList">
<label>Page(s)</label> <a href="#" class="TzAddMenuItem">Add Another</a>
<?php
foreach ($instance['pages'] as $key => $page) {
echo '<span class="TzPageContainer">';
_dropdown_pages(Array('name' => $this->get_field_name('pages') . '[]', 'selected' => attribute_escape($instance['pages'][$key]), 'sort_column' => 'menu_order', 'echo' => 1));
if ($key > 0) {
echo '<a href="#" class="TzDelMenuItem">Del</a>';
}
echo '</span>';
}
?>
</p>
<p>
<label for="<?php echo $this->get_field_id('depth'); ?>">Depth</label> &nbsp;(0 for infinite)
<br /><input type="text" size="2" maxlength="2" id="<?php echo $this->get_field_id('depth'); ?>" name="<?php echo $this->get_field_name('depth'); ?>" value="<?php echo attribute_escape($instance['depth']); ?>" />
</p>
<p>
<input type="checkbox" id="<?php echo $this->get_field_id('inclusive'); ?>" name="<?php echo $this->get_field_name('inclusive'); ?>" <?php echo $checked; ?> />
<label for="<?php echo $this->get_field_id('inclusive'); ?>">Show Selected (vs just children)</label>
</p>
<?php
}
}
?>
\ No newline at end of file
......@@ -11,7 +11,7 @@ class WP_Option implements ArrayAccess, Countable {
$this->_ns = $ns;
$this->_data = get_option($ns);
if (!is_null($defaults) && is_array($defaults)) {
if (is_array($defaults)) {
foreach ($this->_data as $key => $val) {
$defaults[$key] = $val;
}
......@@ -53,5 +53,10 @@ class WP_Option implements ArrayAccess, Countable {
array_merge($this->_data, $data);
$this->save();
}
public function dump() {
echo "Here\n";
print_r($this->_data);
}
}
?>
......
/**
* addEvent & removeEvent -- cross-browser event handling
* Copyright (C) 2006-2007 Dao Gottwald
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Contact information:
* Dao Gottwald <dao at design-noir.de>
*
* @version 1.1.7
*/
function addEvent(o, type, fn) {
o.addEventListener(type, fn, false);
}
function removeEvent(o, type, fn) {
o.removeEventListener(type, fn, false);
}
/*@cc_on if (!window.addEventListener) {
var addEvent = function(o, type, fn) {
if (!o._events) o._events = {};
var queue = o._events[type];
if (!queue) {
o._events[type] = [fn];
if (!o._events._callback)
o._events._callback = function(e) { Event._callListeners(e, o) };
o.attachEvent("on" + type, o._events._callback);
} else if (Event._fnIndex(o, type, fn) == -1)
queue.push(fn);
else return;
Event._mem.push([o, type, fn]);
};
var removeEvent = function(o, type, fn) {
var i = Event._fnIndex(o, type, fn);
if (i < 0) return;
var queue = o._events[type];
if (queue.calling) {
delete queue[i];
if (queue.removeListeners)
queue.removeListeners.push(i);
else
queue.removeListeners = [i];
} else
if (queue.length == 1)
Event._detach(o, type);
else
queue.splice(i, 1);
};
var Event = {
AT_TARGET: 2,
BUBBLING_PHASE: 3,
stopPropagation: function() { this.cancelBubble = true },
preventDefault: function() { this.returnValue = false },
_mem: [],
_callListeners: function(e, o) {
e.stopPropagation = this.stopPropagation;
e.preventDefault = this.preventDefault;
e.currentTarget = o;
e.target = e.srcElement;
e.eventPhase = e.currentTarget == e.target ? this.AT_TARGET : this.BUBBLING_PHASE;
switch (e.type) {
case "mouseover":
e.relatedTarget = e.fromElement;
break;
case "mouseout":
e.relatedTarget = e.toElement;
}
var queue = o._events[e.type];
queue.calling = true;
for (var i = 0, l = queue.length; i < l; i++)
if (queue[i])
queue[i].call(o,e);
queue.calling = null;
if (!queue.removeListeners)
return;
if (queue.length == queue.removeListeners.length) {
this._detach(o, e.type);
return;
}
queue.removeListeners = queue.removeListeners.sort(function(a,b){return a-b});
var i = queue.removeListeners.length;
while (i--)
queue.splice(queue.removeListeners[i], 1);
if (queue.length == 0)
this._detach(o, e.type);
else
queue.removeListeners = null;
},
_detach: function(o, type) {
o.detachEvent("on" + type, o._events._callback);
delete o._events[type];
},
_fnIndex: function(o, type, fn) {
var queue = o._events[type];
if (queue)
for (var i = 0, l = queue.length; i < l; i++)
if (queue[i] == fn)
return i;
return -1;
},
_cleanup: function() {
for (var m, i = 0; m = this._mem[i]; i++)
if (m[1] != "unload" || m[2] == this._cleanup)
removeEvent(m[0], m[1], m[2]);
}
};
addEvent(window, "unload", Event._cleanup);
} @*/
......@@ -10,23 +10,22 @@ Author: Tenzing
die('PHP version 5.2.2 or greater is required');
}
new TzTools();
TzTools::load();
class TzTools {
public function __construct() {
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__));
spl_autoload_register(Array('TzTools', 'loader'));
public static function load() {
// set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__));
spl_autoload_register(Array(__CLASS__, 'autoloader'));
require_once(dirname(__FILE__) . '/wp_functions.php');
add_action('wp_print_scripts', Array('TzTools', 'registerScripts'));
}
public static function registerScripts() {
_register_script('addEvent', plugins_url('addEvent.js', __FILE__));
_register_script('xmlhttpHandler', plugins_url('xmlhttpHandler.js', __FILE__));
add_action('widgets_init', Array('MenuWidget', 'init'));
}
public static function loader($class) {
public static function autoloader($class) {
$file = dirname(__FILE__) . '/' . $class . '.php';
if (is_file($file)) {
require($file);
......
......@@ -52,4 +52,40 @@ function _nonce_field() {
$params = func_get_args();
return call_user_func_array('wp_nonce_field', $params);
}
function _loginout() {
$params = func_get_args();
return call_user_func_array('wp_loginout', $params);
}
// I made this one because register_sidebar_widget() is fucked
function _register_sidebar_widget() {
$params = func_get_args();
return call_user_func_array('wp_register_sidebar_widget', $params);
}
function _register_widget_control() {
$params = func_get_args();
return call_user_func_array('wp_register_widget_control', $params);
}
function _parse_args() {
$params = func_get_args();
return call_user_func_array('wp_parse_args', $params);
}
function _get_sidebars_widgets() {
$params = func_get_args();
return call_user_func_array('wp_get_sidebars_widgets', $params);
}
function _list_pages() {
$params = func_get_args();
return call_user_func_array('wp_list_pages', $params);
}
function _dropdown_pages() {
$params = func_get_args();
return call_user_func_array('wp_dropdown_pages', $params);
}
?>
......