api = $api; $this->id = false; $this->default = $this->isDefaultAccount(); $this->load(); } /** * @return mixed */ abstract protected function loadDefault(); /** * @return array */ abstract public function getForm(); /** * */ public function printForm() { $form = $this->getForm(); foreach ($form['fields'] as $field) { if ($field['type'] === 'hidden') { echo '" value="' . $field['value'] . '"/>'; } } echo '
'; echo '

' . $form['title'] . '

'; echo ''; foreach ($form['fields'] as $field) { if ($field['type'] !== 'hidden') { echo ''; echo ''; echo ''; echo ''; } } echo '
'; echo '
'; } /** * @param $path * @return bool|string */ protected function getPluginData($path) { if (file_exists($path)) { $data = file_get_contents($path); if ($data) { $data = base64_decode($data); } return $data; } return false; } /** * @return bool */ public function load() { /** * @var wpdb $wpdb */ global $wpdb; if ($this->default) { $this->loadDefault(); } else { $filelds = get_object_vars($this); foreach ($filelds as $key => $val) { $this->$key = ''; } $results = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix . AEIDN_TABLE_ACCOUNT . " WHERE name='" . get_class($this) . "'"); if ($results) { $this->id = $results[0]->id; $this->name = $results[0]->name; $this->default = false; $fields = unserialize($results[0]->data); foreach ($fields as $key => $val) { if ($key !== 'id' && $key !== 'name' && $key !== 'default') { $this->$key = $val; } } return true; } } return false; } /** * @param array $data */ public function save($data = array()) { if ($data && isset($data['account_type']) && $data['account_type']) { $form = $this->getForm(); $this->default = false; update_option($form['use_default_account_option_key'], false); } else if (!$this->default && $data) { $form = $this->getForm(); foreach ($form['fields'] as $f) { $this->{$f['field']} = $data[$f['name']]; } $this->name = get_class($this); $serializedData = serialize(get_object_vars($this)); /** * @var wpdb $wpdb */ global $wpdb; $wpdb->replace($wpdb->prefix . AEIDN_TABLE_ACCOUNT, array('id' => $this->id, 'name' => $this->name, 'data' => $serializedData)); $this->id = $wpdb->insert_id; } } /** * @return bool */ public function isDefaultAccount() { return false; } } }