keyboard-navigation/keyboard-navigation.php

119 lines
4.3 KiB
PHP
Raw Normal View History

2009-09-12 19:42:24 +00:00
<?php
/*
Plugin Name: Keyboard Navigation
Plugin URI: http://www.coswellproductions.com/wordpress/wordpress-plugins/
Description: Attach keyboard navigation to blog entries.
Version: 1.0
Author: John Bintz
Author URI: http://www.coswellproductions.org/wordpress/
Copyright 2008-2009 John Bintz (email : jcoswell@coswellproductions.org)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class KeyboardNavigation {
function init() {
wp_enqueue_script('prototype');
add_action('wp_footer', array(&$this, "footer"));
add_action('admin_menu', array(&$this, "admin_menu"));
add_action('admin_head', array(&$this, "admin_head"));
$this->messages = array();
$this->fields = array(
'previous' => __("Previous [Left arrow]", 'keyboard-navigation'),
'next' => __("Next [Right arrow]", 'keyboard-navigation'),
'first' => __("First [Shift-Left arrow]", 'keyboard-navigation'),
'last' => __("Last [Shift-Right arrow]", 'keyboard-navigation'),
);
if (isset($_POST['kbnav'])) {
if (is_array($_POST['kbnav'])) {
if (isset($_POST['kbnav']['_nonce'])) {
if (wp_verify_nonce($_POST['kbnav']['_nonce'], 'keyboard-navigation')) {
if (isset($_POST['kbnav']['module'])) {
$method = "handle_update_" . $_POST['kbnav']['module'];
if (method_exists($this, $method)) { $this->{$method}($_POST['kbnav']); }
}
}
}
}
}
}
function handle_update_options($info) {
$options = array();
foreach (array('selectors', 'highlight') as $field) {
if (isset($info[$field])) { $options[$field] = $info[$field]; }
}
update_option('keyboard-navigation-options', $options);
$this->messages[] = __('Options updated.', 'keyboard-navigation');
}
function KeyboardNavigation() {}
function footer() {
$plugin_url_root = pathfinding_get_admin_url() . '/' . pathfinding_get_plugin_path(); ?>
<script type="text/javascript" src="<?php echo $plugin_url_root ?>/keyboard_navigation.js"></script>
<script type="text/javascript">
var keyboard_navigation_fields = {};
<?php foreach (array_keys($this->fields) as $field) {
$selector = get_option("keyboard-navigation-selector-${field}");
if (!empty($selector)) { ?>
keyboard_navigation_fields['<?php echo $field ?>'] = "<?php echo addslashes($selector) ?>";
<?php }
} ?>
var highlight_selectors = <?php echo (WP_ADMIN && (get_option("keyboard-navigation-highlight-selected-elements") == 1)) ? "true" : "false" ?>;
var results = KeyboardNavigation.get_hrefs(keyboard_navigation_fields, highlight_selectors);
if (results != false) { KeyboardNavigation.add_events(results); }
</script>
<?php
}
function admin_menu() {
add_options_page('Keyboard Navigation', __("Keyboard Navigation", 'keyboard-navigation'), 'edit_themes', 'keyboard-navigation', array($this, "link_editor"));
}
function admin_head() {
global $plugin_page;
if ($plugin_page == plugin_basename(dirname(__FILE__))) {
echo '<link rel="stylesheet" href="' . plugin_dir_url(__FILE__) . '/styles.css" type="text/css" />';
}
}
function link_editor() {
$plugin_dir_url = plugin_dir_url(__FILE__);
$nonce = wp_create_nonce('keyboard-navigation');
$options = get_option('keyboard-navigation-options');
if (!is_array($options)) { $options = array(); }
$plugin_data = get_plugin_data(__FILE__);
include(dirname(__FILE__) . '/admin.inc');
}
}
$keyboard_navigation = new KeyboardNavigation();
add_action('init', array(&$keyboard_navigation, 'init'));
?>