keys is the keys of the data in 1) that is viewed, e.g. array('_plugin', 'news') or
array('_other', 'box', 'fields', 'title'). As a $_GET['keys'] variable it is imploded with a
comma as the separator, e.g. '&keys=_other,box,fields,title'
3. $this->action determins what is about to happen
4. $this->action_keys is used when deleting stuff, as $this->key sets the navigation.
SAVING:
=======
- The fields that are to be saved are defined in the $this->fields, which contains 'var' for
single value variables and 'array' for variabels that can contain array values,
associative or just normal.
- Default values for creating new stuff is defined in $this->default.
- In x-settings.php keys that are deeper than 1, e.g. $data['one']['two'] are retrieved
as 'one,two'.
- $_POST['index'] sets the last key to save to. This needs to be set in
validate_submission() in each plugins x-settings-object.php.
- $_POST['originating_keys'] defines where the request came from - used to
see of the name was changed.
- For a key that contains deeper levels of data, the entire data contained within
that key must be defined in $_POST. E.g. for More Fields, the entire 'fields' key
must be defined in $_POST when saving the box. This is done with $this->settings_hidden(),
which will seralize any array data.
Copyright (C) 2010 Henrik Melin, Kal Ström
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 Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
$more_common = 'MORE_PLUGINS_ADMIN_SPUTNIK_4';
if (!defined($more_common)) {
class more_plugins_admin_object_sputnik_4 {
var $name, $slug, $settings_file, $dir, $options_url, $option_key, $data, $url;
var $action, $navigation, $message, $error;
/*
**
**
*/
function more_plugins_admin_object_sputnik_4 ($settings) {
$this->name = $settings['name'];
$this->slug = sanitize_title($settings['name']);
$this->fields = $settings['fields'];
if (isset($settings['settings_file']))
$this->settings_file = $settings['settings_file'];
else $this->settings_file = $this->slug . '-settings.php';
$this->dir = plugin_dir_path($settings['file']); //WP_PLUGIN_DIR . '/' . $this->slug . '/';
$this->url = plugin_dir_url($settings['file']); //get_option('siteurl') . '/wp-content/plugins/' . $this->slug . '/';
$this->options_url = 'options-general.php?page=' . $this->slug;
$this->settings_url = $this->options_url;
$this->option_key = $settings['option_key'];
$this->default = $settings['default'];
$this->default_keys = ($a = $settings['default']) ? $a : array();
// Create Settins Menu
add_action('admin_menu', array(&$this, 'admin_menu'));
add_action('admin_head', array(&$this, 'admin_head'));
// Handle requests
add_action('settings_page_' . $this->slug, array(&$this, 'request_handler'));
// Add JS & css on settings page
add_action('admin_head-settings_page_' . $this->slug, array(&$this, 'settings_head'));
// add_action('admin_print_scripts-settings_page_' . $this->slug, array(&$this, 'settings_init'));
add_action('load-settings_page_' . $this->slug, array(&$this, 'settings_init'));
// add_action('admin_init', array(&$this, 'settings_init'), 50);
add_filter('plugin_row_meta', array(&$this, 'plugin_row_meta'), 10, 2);
add_action('init', array(&$this, 'admin_init'), 11);
// add_action('admin_print_scripts-' . $page, 'my_plugin_admin_styles');
$this->add_actions();
$this->add_key = '57UPhPh';
// $this->data = $this->read_data();
}
function admin_init() {
/*
// Read in saved files
$dir = WP_PLUGIN_DIR . '/' . $this->slug . '/saved/';
if (is_dir($dir)) {
// Pre PHP 5 compatablity
if (is_callable('scandir'))
$ls = scandir($dir);
else {
$ls = array();
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
if ($filename[0] != '.') $ls[] = $filename;
}
}
$pts = array();
foreach ($ls as $l) if (strpos($l, '.php')) $pts[] = $l;
foreach ($pts as $file) require($dir . $file);
$this->data = $this->load_objects();
}
*/
}
function add_actions() {
// This function was intentionally left blank
}
function plugin_data() {
return get_option($this->option_key, array());
}
/*
**
** Add links to the Plugins page.
*/
function plugin_row_meta ($links, $file) {
if (strpos('padding' . $file, $this->slug)) {
$links[] = '' . __('Settings','more-plugins') . '';
$links[] = '' . __('Support','more-plugins') . '';
$links[] = '' . __('Donate','sitemap') . '';
}
return $links;
}
/*
**
**
*/
function admin_menu () {
add_options_page($this->name, $this->name, 8, $this->slug, array(&$this, 'options_page'));
}
/*
**
**
*/
function admin_head () {
add_thickbox();
}
function is_plugin_installed() {
}
/*
**
**
*/
function options_page() {
$this->options_page_wrapper_header();
// Errors trump notifications
if ($this->error) echo '
CTRL/CMD + c), paste it into a text file and save it as %s to the aforementioned directory. If an object exists both in the %s settings and as a file, the file will override the data stored in the database.', 'more-plugins'), $this->name, "$dir", "$filename", $this->name); ?>
options_page_wrapper_footer();
}
/*
**
**
*/
function data_subset ($args = array()) {
$ret = array();
foreach ($this->data as $key => $d) {
$exclude = false;
foreach ($args as $k => $a)
if ($d[$k] != $a) $exclude = true;
if (!$exclude) $ret[$key] = $d;
}
return $ret;
}
function get_data($s = array(), $override = false) {
if (empty($s) && !$override) $s = $this->keys;
if (count($s) == 0) return $this->data;
if (count($s) == 1) return $this->data[$s[0]];
if (count($s) == 2) return $this->data[$s[0]][$s[1]];
if (count($s) == 3) return $this->data[$s[0]][$s[1]][$s[2]];
if (count($s) == 4) return $this->data[$s[0]][$s[1]][$s[2]][$s[3]];
return $this->data;
}
function set_data($value, $s = array(), $override = false) {
if (empty($s) && !$override) $s = $this->keys;
if (count($s) == 0) $this->data = $value;
if (count($s) == 1) $this->data[$s[0]] = $value;
if (count($s) == 2) $this->data[$s[0]][$s[1]] = $value;
if (count($s) == 3) $this->data[$s[0]][$s[1]][$s[2]] = $value;
if (count($s) == 4) $this->data[$s[0]][$s[1]][$s[2]][$s[3]] = $value;
return $this->data;
}
function unset_data($s = array()) {
if (empty($s)) $s = $this->keys;
$key = array_pop($s);
$arr = $this->get_data($s, true);
if ($arr[$key]) unset($arr[$key]);
$this->set_data($arr, $s, true);
return $this->data;
}
/*
** settings_init()
**
** Extract variables that define what we're trying to do.
*/
function settings_init() {
// Single vars
$fs = array('action', 'navigation');
foreach ($fs as $f) $this->{$f} = attribute_escape($_GET[$f]);
// Array vars
$fs = array('keys', 'action_keys');
foreach ($fs as $f) {
$a = attribute_escape($_GET[$f]);
$argh = $this->extract_array($a);
$this->{$f} = $argh;
}
$this->after_settings_init();
return true;
}
function after_settings_init() {
/*
** This function is intentionally left blank
**
** Overwritten by indiviudal plugin admin objects, if needed.
*/
}
/*
**
** Parse requests...
*/
function request_handler () {
// Load up our data, internal and external
$this->load_objects();
// Ponce som en lugercheck!
if ($nonce = attribute_escape($_GET['_wpnonce']))
check_admin_referer($this->nonce_action());
// Check whatever you want - validate_submission should return false if
// things don't stack up.
if (!($this->validate_sumbission())) {
if ($this->action == 'save') {
$keys = $this->keys;
if (!empty($this->action_keys)) {
$keys = $this->action_keys;
$this->keys = $keys;
}
$this->set_data($this->extract_submission(), $keys);
}
return false;
}
if ($this->navigation == 'export') {
return $this->export_data();
}
if ($this->action == 'move') {
// At what level are we moving?
$action_keys = $this->extract_array(attribute_escape($_GET['action_keys']));
if (empty($action_keys)) array_push($action_keys, '_plugin');
$data = $this->get_data($action_keys);
if (empty($data))
return $this->error(__('Someting has gone awry. Sorry.', 'more-plugins'));
// Which element is being moved?
$row = attribute_escape($_GET['row']);
// Move a key
$up = ('up' == attribute_escape($_GET['direction'])) ? true : false;
$data = $this->move_field($data, $row, $up);
// Save the data
$this->set_data($data, $action_keys);
$this->save_data();
}
if ($this->action == 'save') {
$arr = $this->extract_submission();
// The $_POST['index'] needs to be set externally, this is
// last index of the data to be saved
$index = $arr['index'];
$keys = $arr['originating_keys'];
$old_last_key = $keys[count($keys) - 1];
// We can only save to '_plugin'
if ($keys[0] != '_plugin') {
$arr['ancestor_key'] = $keys[1];
$keys[0] = '_plugin';
}
// Is this not new stuff?
if ($index != $this->add_key) {
// Ok, so it's not new, but has it changed?
if ($old_last_key != $index) {
// The old keys are now redundant
$this->unset_data($keys);
}
}
// Set the appropiate focus
array_pop($keys);
array_push($keys, $index);
unset($arr['originating_keys']);
// Set and save and provide feedback
if (count($keys) > 1) {
$this->set_data($arr, $keys);
$this->save_data();
$this->message = __('Saved!', 'more_plugins');
}
}
if ($this->action == 'delete') {
$data = $this->unset_data($this->action_keys);
$this->save_data();
$this->message = __('Deleted!', 'more-plugins');
}
if (count($this->keys) && $this->action == 'add') {
// Extract the last key
$last = $this->keys[count($this->keys) - 1];
// Are we trying to add stuff?
if ($last == $this->add_key) {
$this->data = $this->set_data($this->default, $this->keys);
}
}
$this->after_request_handler();
}
function after_request_handler() {
/*
** This function is intentionally left blank
**
** Overwritten by indiviudal plugin admin objects, if needed - mostly
** used for cross more-plugins functionality
*/
}
function extract_submission() {
// Add required params
array_push($this->fields['array'], 'originating_keys');
array_push($this->fields['var'], 'index');
array_push($this->fields['var'], 'ancestor_key');
// Ekkstrakkt
$arr = array();
foreach($this->fields['var'] as $field) {
$v = attribute_escape($_POST[$field]);
$arr[$field] = htmlspecialchars(stripslashes($v));
}
foreach($this->fields['array'] as $level1 => $field) {
if (!is_array($field)) {
$vals = $this->extract_array($_POST[$field]);
foreach ($vals as $k => $v) {
if (!is_array($v) && !is_object($v)) {
$arr[$field][$k] = htmlspecialchars(stripslashes($v));
} else $arr[$field][$k] = $this->object_to_array($v);
}
} else {
foreach ($field as $level2 => $field2) {
$post = $this->extract_array($_POST[$level1 . ',' . $field2]);
$arr[$level1][$field2] = htmlspecialchars(stripslashes($post[0]));
}
}
}
return $arr;
}
/*
** Might be storing serialized data or might be a
** comma separated list
*/
function extract_array($a) {
// *Might* be storing serialized data or *might* be a
// comma separated list
if (is_array($a)) return $a;
if ($a) {
// Is $a seralized?
$b = maybe_unserialize(stripslashes($a));
if (is_object($b)) $b = $this->object_to_array($b);
if (is_array($b)) return $b;
// Is this a comma separated list?
if (strpos($a, ','))
return explode(',', $a);
// $a is just a single value
return array($a);
}
// $a is empty
return array();
}
/*
**
**
*/
function stripslashes_deep ($string) {
while(strpos($string, '\\'))
$string = stripslashes($string);
return $string;
}
/*
**
**
*/
function object_to_array($data) {
if (is_array($data) || is_object($data)) {
$result = array();
foreach($data as $key => $value) $result[$key] = $this->object_to_array($value);
return $result;
}
return $data;
}
/*
** Get the index name from the $_POST variable
** to be used in validate_submission() in individual
** settings classes.
**
*/
function get_index($key) {
$val = attribute_escape($_POST[$key]);
$val = sanitize_title($val);
$val = str_replace('-', '_', $val);
return $val;
}
/*
**
**
*/
/*
function read_data() {
return array();
}
*/
/*
**
**
*/
function save_data($data = array()) {
if (empty($data)) $data = $this->data['_plugin'];
update_option($this->option_key, $data);
}
/*
**
** Overwrite this function in subclass to validate
** the submission data.
*/
function validate_sumbission () {
// Somthing
return true;
}
/*
**
**
*/
function error($error) {
$this->error = $error;
return false;
}
/*
**
**
*/
function set_navigation($navigation) {
$_GET['navigation'] = $navigation;
$_POST['navigation'] = $navigation;
$this->navigation = $navigation;
return $navigation;
}
/*
**
**
*/
function options_page_wrapper_header () {
if ($this->headed) return false;
$url = get_option('siteurl');
?>
headed = true;
}
/*
**
**
*/
function options_page_wrapper_footer() {
if ($this->footed) return false;
?>
footed = true;
}
/*
**
**
*/
function condition($condition, $message, $type = 'error') {
if (!isset($this->is_ok)) $this->is_ok = true;
// If there is an error already return
if (!$this->is_ok && $type = 'error') return $this->is_ok;
if ($condition == false && $type != 'silent') {
echo '
' . $message . '
';
// Don't set the error flag if this is a warning.
if ($type == 'error') $this->is_ok = false;
}
return ($condition == true);
}
/*
**
**
*/
function checkboxes($name, $title, $values, $arr) {
?>