/** * Force the wizard to be completed * * 1. You cannot bypass the wizard. * 2. You cannot skip ahead to future steps. * 3. You cannot go back to previous steps. */ private function maybe_force_redirect() { if (!$this->is_wizard()) { wp_safe_redirect(wpem_get_wizard_url()); exit; } if ($this->last_viewed->name !== wpem_get_current_step()->name) { wp_safe_redirect($this->last_viewed->url); exit; } }
/** * Save fields * * @return array|bool */ public function save() { $saved = []; $invalid = []; foreach ($this->fields as $field) { preg_match('/\\[(.*?)\\]/', $field['name'], $key); // Get key from brackets $key = isset($key[1]) ? $key[1] : null; $name = preg_replace('/[\\[].*[\\]]/', '', $field['name']); // Strip brackets $value = $key && isset($_POST[$name][$key]) ? $_POST[$name][$key] : (isset($_POST[$name]) ? $_POST[$name] : null); // Support arrays $sanitizer = empty($field['sanitizer']) ? null : $field['sanitizer']; // Maybe sanitize if ($value && $sanitizer && is_callable($sanitizer)) { $value = is_array($value) ? array_map($sanitizer, $value) : $sanitizer($value); } // Maybe validate if (!$value && !empty($field['required'])) { $invalid[] = $key ? sprintf('%s[%s]', $name, $key) : $name; continue; } // Maybe use default if (!$value && isset($field['default'])) { $value = $field['default']; } $result = false; // Only save WPEM options directly to the database if (0 === strpos($name, 'wpem_')) { if ($key) { $option = (array) get_option($name, []); $option[$key] = $value; $value = $option; } $result = update_option($name, $value); } // Maybe save to log if (empty($field['skip_log'])) { $log = new Log(); $result = $log->add_step_field($name, $value); } if ($result) { $saved[$name] = $value; } } if (!empty($invalid)) { wp_safe_redirect(add_query_arg(['step' => wpem_get_current_step()->name, 'error' => true, 'fields' => implode(',', $invalid)], wpem_get_wizard_url())); exit; } return $saved; }
/** * Register the steps used by the wizard */ private function register_steps() { // Some steps depend on the image api $this->steps = [new Step_Start($this->log), new Step_Settings($this->log, $this->image_api), new Step_Contact($this->log), new Step_Theme($this->log, $this->image_api)]; foreach ($this->steps as $i => $step) { $step->position = $i + 1; $step->url = add_query_arg(['step' => $step->name], wpem_get_wizard_url()); } $this->last_viewed = $this->get_step_by('name', get_option('wpem_last_viewed', 'start')); }
/** * Update options in bulk * * @param array $options */ private function bulk_update_options($options) { if (empty($options) || !is_array($options)) { return; } foreach ($options as $option) { $name = sanitize_key($option['name']); $label = !empty($option['label']) ? $option['label'] : $name; $value = filter_input(INPUT_POST, $name); // Validate if (!empty($option['required']) && '' === $value) { wp_safe_redirect(add_query_arg(array('step' => $this->name, 'error' => true), wpem_get_wizard_url())); exit; } // Sanitize if (!empty($option['sanitizer']) && function_exists($option['sanitizer'])) { $value = call_user_func($option['sanitizer'], $value); } update_option($name, $value); $log = new WPEM_Log(); $log->add_step_field($name, $value); } }