init = false; $this->modules = getModules(); if(!is_admin() && !isset($_POST['vietswiss_save'])) { return; } $this->initOptions(); $this->handlePost(); add_action( 'admin_menu', array( $this, 'add_plugin_page' ) ); } public function handlePost() { if(!isset($_POST['vietswiss_save'])) { return; } $inputs = $_POST; unset($inputs['vietswiss_save']); foreach($inputs as $inputName => $input) { $nameSplit = explode('_', $inputName); $section = array_shift($nameSplit); $slug = implode('_', $nameSplit); if(!isset($this->options[$section]) || !isset($this->options[$section][$slug])) { continue; } if($this->options[$section][$slug]['type'] === 'file'){ // validate json $json = json_decode($input, true); if($json === null){ continue; } file_put_contents($this->options[$section][$slug]['value'], json_encode($json,JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)); continue; } $this->options[$section][$slug]['value'] = $this->sanitizeInput($input, $this->options[$section][$slug]['type']); } $this->saveOptions(); } public function getOption($section, $input){ if(!$this->init){ $this->initOptions(); } if(isset($this->options[$section][$input])){ return $this->options[$section][$input]['value']; } return null; } public function sanitizeInput($input, $type) { switch($type) { case 'boolean': return $input === 'yes' ? '1' : '0'; break; case 'number': return intval($input); break; default: return strval($input); break; } } public function saveOptions() { update_option('vietswiss_settings', $this->options); } public function initOptions() { $this->options = get_option( 'vietswiss_settings' ); $configTree = []; if(!is_array($this->options)) { $this->options = []; } foreach($this->modules as $module) { foreach($module->getOptions() as $option) { if(!isset($this->options[$option->getSection()])) { $this->options[$option->getSection()] = []; } if(!isset($configTree[$option->getSection()])) { $configTree[$option->getSection()] = []; } $configTree[$option->getSection()][] = $option->getSlug(); if(isset($this->options[$option->getSection()][$option->getSlug()])) { $oldOptionValue = $this->options[$option->getSection()][$option->getSlug()]['value']; $this->options[$option->getSection()][$option->getSlug()] = ['name' => $option->getName(), 'type' => $option->getType() , 'value' => $oldOptionValue]; }else { $this->options[$option->getSection()][$option->getSlug()] = ['name' => $option->getName(), 'type' => $option->getType() , 'value' => $option->getDefault()]; } } } foreach($this->options as $section => $elements) { if(!isset($configTree[$section])) { unset($this->options[$section]); continue; } foreach($elements as $elementSlug => $_) { if(!in_array($elementSlug, $configTree[$section])) { unset($this->options[$section][$elementSlug]); } } } $this->init = true; $this->saveOptions(); } /** * Add options page */ public function add_plugin_page() { // This page will be under "Settings" add_menu_page( 'Fasoon Mutation Form', 'Mutation From Plugin', 'manage_options', 'vietswiss-admin', array( $this, 'create_admin_page' ) ); } public function create_admin_page() { $this->initOptions(); ?>

Fasoon Mutation Form Plugin

options as $sectionName => $section) { echo "

{$sectionName}

"; foreach($section as $optionSlug => $option) { $this->printField($option['name'], $sectionName.'_'.$optionSlug, $option['value'], $option['type']); } echo "
"; } ?>
booleanFieldTemplate($name, $slug, $value); break; case 'number': echo $this->numberFieldTemplate($name, $slug, $value); break; case 'file': echo $this->fileFieldTemplate($name, $slug, $value); break; default: echo $this->textFieldTemplate($name, $slug, $value); break; } } public function booleanFieldTemplate($name, $slug, $value) : string { return " "; } public function numberFieldTemplate($name, $slug, $value) : string { return " "; } public function fileFieldTemplate($name, $slug, $value) : string { $content = file_get_contents($value); return " "; } public function textFieldTemplate($name, $slug, $value) : string { return " "; } }