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 '

' . $this->error . '

'; else if ($this->message) echo '

' . $this->message . '

'; // Load the settings file if (!$this->footed) if ($this->settings_file) require($this->dir . $this->settings_file); $this->options_page_wrapper_footer(); } function export_data() { $this->options_page_wrapper_header(); $data = $this->get_data(); $function = str_replace('-', '_', $this->slug); $filter = $function . '_saved'; $k = $this->keys[1]; $a = str_replace('-', '_', $k); $f = $function . '_saved_' . $a; $j = maybe_serialize($data); $export = ""; $filename = $a . '.php'; $dir = $this->dir . 'saved/'; if (false) { $file = $this->dir . 'registered/' . $filename; if (!$handle = fopen($file, 'a')) { echo "Cannot open file ($filename)"; exit; } // Write $somecontent to our opened file. if (fwrite($handle, $export) === FALSE) { echo "Cannot write to file ($filename)"; exit; } fclose($handle); } $this->navigation_bar(array('Export')); ?>

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'); ?>

name; ?>

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) { ?> $title2) : // $selected = ($arr[$name] == $key) ? ' checked="checked"' : ''; $checked = (in_array($key, (array) $arr[$name])) ? " checked='checked'" : ''; ?> = count($data) - 1 && !$up) return $data; if ($nbr == 0 && $up) return $data; $new = array(); $ctr = 0; $offset = ($up) ? 0 : 1; foreach ($data as $key => $arr) { if ($ctr == $nbr - 1 + $offset) $tmp_key = $key; else $new[$key] = $arr; if ($ctr == $nbr + $offset) $new[$tmp_key] = $data[$tmp_key]; $ctr++; } return $new; } /* ** ** */ function updown_link ($nbr, $total, $args = array()) { $html = ''; $link = array('row' => $nbr, 'navigation' => $this->navigation, 'action' => 'move'); // Are we adding more stuff to our link? if (!empty($args)) $link = array_merge($link, $args); // Build the links if ($nbr > 0) $html .= ' | ' . $this->settings_link('&uarr', array_merge($link, array('direction' => 'up'))); if ($nbr < $total - 1) $html .= ' | ' . $this->settings_link('&darr', array_merge($link, array('direction' => 'down'))); return $html; } /* ** ** */ function settings_link ($text, $args) { $link = $this->options_url; foreach ($args as $key => $value) { if ($key == 'class') continue; if (!$value) continue; if (is_array($value)) $value = implode(',', $value); $link .= '&' . $key . '=' . urlencode($value); } $link = wp_nonce_url($link, $this->nonce_action($args)); $class = ($c = $args['class']) ? $c : 'more-common'; $html = "$text"; if (!$text) return $link; return $html; } /* ** ** */ function nonce_action($args = array()) { if (empty($args)) $args = $_GET; $action = $this->slug . '-action_'; if ($a = attribute_escape($args['navigation'])) $action .= $a; if ($a = attribute_escape($args['action'])) $action .= $a; return $action; } /* ** ** */ function table_header($titles) { ?>
keys; $s = array(); // Deal with comma separated keys foreach ((array) $k as $b) { if (strpos($b, ',')) { $c = explode(',', str_replace(' ', '', $b)); foreach($c as $d) $s[] = $d; } else $s[] = $b; } // Deal with comma separated field names if (strpos($name, ',')) { $c = explode(',', str_replace(' ', '', $name)); foreach($c as $d) $s[] = $d; } else $s[] = $name; // Iterate through the data $subdata = $this->data; foreach ($s as $key) { $subdata = $subdata[$key]; } if (!is_array($subdata)) $subdata = stripslashes($subdata); return $subdata; } /* ** ** */ function settings_input($name, $s = array()) { $value = $this->get_val($name, $s); $html = ""; return $html; } /* ** ** */ function settings_bool($name) { $vars = array(true => 'Yes', false => 'No'); $html = $this->settings_radiobuttons($name, $vars); return $html; } function settings_radiobuttons($name, $vars, $comments = array()) { $html = ''; $set = $this->get_val($name); foreach ($vars as $key => $value) { $checked = ($key == $set) ? ' checked="checked"' : ''; $html .= " "; if ($c = $comments[$key]) $html .= $this->format_comment($c); } return $html; } function settings_hidden($name) { $value = $this->get_val($name); $value = maybe_serialize($value); $html = ""; return $html; } /* ** ** */ function get_roles() { global $wp_roles; $user_levels = array(); foreach($wp_roles->roles as $role) { $name = str_replace('|User role', '', $role['name']); $value = sanitize_title($name); if ($value) $user_levels[$value] = $name; } return $user_levels; } /* ** ** */ function checkbox_list($name, $vars, $options = array()) { $values = (array) $this->get_val($name); $html = ''; foreach ($vars as $key => $val) { // Options will over-ride values $class = ($a = $options[$key]['class']) ? 'class="' . $a . '"' : ''; $readonly = ($options[$key]['disabled']) ? ' disabled="disabled"' : ''; if (array_key_exists('value', (array) $options[$key])) $checked = ($options[$key]['value']) ? ' checked="checked" ' : ''; else $checked = (in_array($key, $values)) ? ' checked="checked"' : ''; $html .= ""; if ($t = $options[$key]['text']) $html .= '' . $t . ''; } // $html .= ''; return $html; } function settings_select($name, $vars) { $values = $this->get_val($name); $html = ""; return $html; } function settings_textarea($name) { $value = $this->get_val($name); $html = ""; return $html; } /* ** ** function add_button ($options) { ?>

url . 'more-plugins/more-plugins.css'; else $css = WP_PLUGIN_URL . '/more-plugins.css'; ?> 'save', 'keys' => $_GET['keys']); $args = wp_parse_args($args, $defaults); ?> settings_link(false, $args); ?>
' . $comment . ''; } function settings_save_button() { $keys = implode(',', (array) $this->keys); ?> ' />
$pt) { $name = ($t = $pt->labels->singular_name) ? $t : $pt->label; $ret[$key] = $name; } return $ret; } function permalink_warning() { global $wp_rewrite; if (empty($wp_rewrite->permalink_structure)) { $html = ''; $html .= __('Permalinks are currently not enabled! To use this feature, enable permalinks in the Permalink Settings.', 'more-plugins'); $html .= ''; return $html; } else return ''; } } // end class } // endif defined define($more_common, true); if (!is_callable('__d')) { function __d($d) { echo '
';
		print_r($d);
		echo '
'; } } ?>