/** * Gets the alerts from the database and stores them in self::$_alerts * If alerts already exists, don't make unnecessary database requests * @return int -1 if $a < $b, 0 if $a == $b, 1 if $a > $b * @since 0.1 */ private static function get_alerts() { if (empty(self::$_alerts)) { self::$_alerts = get_transient('PW_Alerts') ? get_transient('PW_Alerts') : array(); } return self::$_alerts ? self::$_alerts : false; }
/** * Detect any GET/POST variables and determine what CRUD option to do based on that * @since 0.1 */ public function process_request() { // Set $this->_model->instance, the default is 0 which is the new instance form $this->_model->instance = isset($_GET['_instance']) ? (int) $_GET['_instance'] : 0; // if $this->_model->instance if (empty($this->_model->option[$this->_model->instance])) { wp_die("Oops, this page doesn't exist.", "Page does not exist", array('response' => 403)); } // Check to see if the 'Delete' link was clicked if (isset($_GET['delete_instance']) && isset($_GET['_instance']) && check_admin_referer('delete_instance')) { PW_Alerts::add('updated', '<p><strong>' . $this->_model->singular_title . ' Instance Deleted</strong></p>'); // make a copy of the option, then unsetting the index and update with the copy $option = $this->_model->option; unset($option[(int) $_GET['_instance']]); update_option($this->_model->name, $option); // redirect the page and remove _instance' and 'delete_instance' from the URL wp_redirect(remove_query_arg(array('_instance', 'delete_instance'), wp_get_referer())); exit; } // If the POST data is set and the nonce checks out, validate and save any submitted data if (isset($_POST[$this->_model->name]) && isset($_POST['_instance']) && check_admin_referer($this->_model->name . '-options')) { // get the options from $_POST $this->_model->input = stripslashes_deep($_POST[$this->_model->name]); // save the options if ($this->_model->save($this->_model->input, $_POST['_instance'])) { if ($_POST['_instance'] == 0) { wp_redirect(add_query_arg('_instance', $this->_model->option['auto_id'] - 1, wp_get_referer())); exit; } } } }
/** * Save the option to the database if (and only if) the option passes validation * @param array $option The option value to store * @param int $instance The instance to save * @return boolean Whether or not the option was successfully saved * @since 0.1 */ public function save($input, $instance = 0) { if ($this->validate($input)) { $this->_errors = array(); // set the instance ID and increment the auto_id $instance = $instance == 0 ? $this->_option['auto_id']++ : $_POST['_instance']; $this->_option[$instance] = $input; $this->update_option($this->_option); PW_Alerts::add('updated', '<p><strong>Settings Saved</strong></p>'); return true; } // If you get to here, return false return false; }
/** * Save the option to the database if (and only if) the option passes validation * @param array $option The option value to store * @return boolean Whether or not the option was successfully saved * @since 0.1 */ public function save($input) { if ($this->validate($input)) { $this->_errors = array(); $this->update_option($input); PW_Alerts::add('updated', '<p><strong>Settings Saved</strong></p>'); return true; } // If you get to here, return false return false; }