Base.class.php
121 lines
| 1 | <?php |
| 2 | abstract class postaffiliatepro_Form_Base extends postaffiliatepro_Base { |
| 3 | private $formName; |
| 4 | // private $settings; |
| 5 | protected $variables = array('infoMessages' => '', 'errorMessages' => ''); |
| 6 | |
| 7 | public function __construct($name = '', $action = '') { |
| 8 | $this->formName = $name; |
| 9 | $this->addVariable('form.head', '<form name="'.esc_attr($name).'" action="'.esc_attr($action).'" method="post">'); |
| 10 | $this->addVariable('form.tail', '</form>'); |
| 11 | } |
| 12 | |
| 13 | private function loadSettingsString($name) { |
| 14 | $this->addVariable('settings',"<input type='hidden' name='option_page' value='". |
| 15 | esc_attr($name)."' /><input type='hidden' name='action' value='update' />". |
| 16 | wp_nonce_field("$name-options",'_wpnonce', true, false)); |
| 17 | } |
| 18 | |
| 19 | protected abstract function initForm(); |
| 20 | |
| 21 | protected abstract function getTemplateFile(); |
| 22 | |
| 23 | protected function addSubmit($value = 'Save changes', $class = 'button-primary') { |
| 24 | $this->addVariable('submit', '<input type="submit" id="submit" name="submit" value="'.$value.'" class="'.$class.'">'); |
| 25 | } |
| 26 | |
| 27 | protected function addHtml($name, $code) { |
| 28 | $this->addVariable($name, $code); |
| 29 | } |
| 30 | |
| 31 | protected function getOption($name, $type = 'text') { |
| 32 | $result = get_option($name); |
| 33 | if ($type == 'none') { |
| 34 | return esc_attr($result); |
| 35 | } |
| 36 | if ($type == 'url') { |
| 37 | $newResult = esc_url($result); |
| 38 | if (substr($newResult, -1) !== '/') { |
| 39 | $newResult .= '/'; |
| 40 | } |
| 41 | } else { |
| 42 | $newResult = sanitize_text_field($result); |
| 43 | } |
| 44 | if ($newResult != $result) { |
| 45 | update_option($name, $newResult); |
| 46 | } |
| 47 | return esc_attr($newResult); |
| 48 | } |
| 49 | |
| 50 | protected function addCheckbox($name, $templateName = null, $additionalCode = '') { |
| 51 | $checked = ''; |
| 52 | if ($this->getOption($name) === 'true') { |
| 53 | $checked = ' checked'; |
| 54 | } |
| 55 | if ($templateName === null) { |
| 56 | $templateName = $name; |
| 57 | } |
| 58 | $this->addVariable($templateName, '<input type="checkbox" name="'.$name.'" id="'.str_replace(array(' ', '[', ']'), array('_', ''), $name.'_').'" value="true"'.$checked.' '.$additionalCode.' class="checkbox"></input>'); |
| 59 | } |
| 60 | |
| 61 | protected function addSelect($name, $options) { |
| 62 | $this->addVariable($name, "<select id='$name' name='$name'>\n".$this->getHTMLSelectOptions($options, $name)."</select>\n"); |
| 63 | } |
| 64 | |
| 65 | private function getHTMLSelectOptions($options, $name) { |
| 66 | //options = assoc. arr, key(value) and value(name) of select option |
| 67 | $html = ''; |
| 68 | $selected = $this->getOption($name, 'none'); |
| 69 | foreach ($options as $optionKey => $optionName) { |
| 70 | $html .= '<option value="'.$optionKey.'"'.(($selected == $optionKey)?' selected="selected">':'>').$optionName."</option>\n"; |
| 71 | } |
| 72 | return $html; |
| 73 | } |
| 74 | |
| 75 | protected function addPassword($name, $size = 20) { |
| 76 | $this->addVariable($name, '<input type="password" id="'.$name.'" name="'.$name.'" value="'.$this->getOption($name, 'none').'" class="password" onfocus="this.className = \'password-focus\'" onblur="this.className = \'password\'" size="'.$size.'">'); |
| 77 | } |
| 78 | |
| 79 | protected function addTextBox($name, $size = 20, $value = '') { |
| 80 | $useValue = $this->getOption($name); |
| 81 | if ($useValue === '') { |
| 82 | $useValue = $value; |
| 83 | } |
| 84 | $this->addVariable($name, '<input type="text" id="'.$name.'" name="'.$name.'" value="'.$useValue.'" class="text" onfocus="this.className = \'text-focus\'" onblur="this.className = \'text\'" size="'.$size.'">'); |
| 85 | } |
| 86 | |
| 87 | protected function addUrlTextBox($name, $size = 20) { |
| 88 | $value = $this->getOption($name, 'url'); |
| 89 | settings_errors($name); |
| 90 | $this->addVariable($name, '<input type="text" id="'.$name.'" name="'.$name.'" value="'.$value.'" class="text" onfocus="this.className = \'text-focus\'" onblur="this.className = \'text\'" size="'.$size.'">'); |
| 91 | } |
| 92 | |
| 93 | public function render($toVar = false, $template = '') { |
| 94 | $this->initForm(); |
| 95 | if ($template == '') { |
| 96 | $html = file_get_contents($this->getTemplateFile()); |
| 97 | if ($this->formName != '') { |
| 98 | $this->loadSettingsString($this->formName); |
| 99 | } |
| 100 | } else { |
| 101 | $html = file_get_contents($template); |
| 102 | } |
| 103 | |
| 104 | foreach ($this->variables as $name => $value) { |
| 105 | $html = str_replace('{'.$name.'}', $value, $html); |
| 106 | } |
| 107 | if ($toVar) { |
| 108 | return $html; |
| 109 | } |
| 110 | echo '<div class="postaffiliatepro">' . $html . '</div>'; |
| 111 | return ''; |
| 112 | } |
| 113 | |
| 114 | protected function addVariable($name, $value) { |
| 115 | if (isset($this->variables[$name])) { |
| 116 | $this->variables[$name] .= $value; |
| 117 | } else { |
| 118 | $this->variables[$name] = $value; |
| 119 | } |
| 120 | } |
| 121 | } |