| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Options form input fields |
| 5 |
*/ |
| 6 |
class GFEwayOptionsForm { |
| 7 |
|
| 8 |
public $customerID; |
| 9 |
public $useStored; |
| 10 |
public $useTest; |
| 11 |
public $useBeagle; |
| 12 |
public $roundTestAmounts; |
| 13 |
public $forceTestAccount; |
| 14 |
public $sslVerifyPeer; |
| 15 |
|
| 16 |
/** |
| 17 |
* initialise from form post, if posted |
| 18 |
*/ |
| 19 |
public function __construct() { |
| 20 |
if (self::isFormPost()) { |
| 21 |
$this->customerID = self::getPostValue('customerID'); |
| 22 |
$this->useStored = self::getPostValue('useStored'); |
| 23 |
$this->useTest = self::getPostValue('useTest'); |
| 24 |
$this->useBeagle = self::getPostValue('useBeagle'); |
| 25 |
$this->roundTestAmounts = self::getPostValue('roundTestAmounts'); |
| 26 |
$this->forceTestAccount = self::getPostValue('forceTestAccount'); |
| 27 |
$this->sslVerifyPeer = self::getPostValue('sslVerifyPeer'); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Is this web request a form post? |
| 33 |
* |
| 34 |
* Checks to see whether the HTML input form was posted. |
| 35 |
* |
| 36 |
* @return boolean |
| 37 |
*/ |
| 38 |
public static function isFormPost() { |
| 39 |
return ($_SERVER['REQUEST_METHOD'] == 'POST'); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Read a field from form post input. |
| 44 |
* |
| 45 |
* Guaranteed to return a string, trimmed of leading and trailing spaces, sloshes stripped out. |
| 46 |
* |
| 47 |
* @return string |
| 48 |
* @param string $fieldname name of the field in the form post |
| 49 |
*/ |
| 50 |
public static function getPostValue($fieldname) { |
| 51 |
return isset($_POST[$fieldname]) ? stripslashes(trim($_POST[$fieldname])) : ''; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Validate the form input, and return error messages. |
| 56 |
* |
| 57 |
* Return a string detailing error messages for validation errors discovered, |
| 58 |
* or an empty string if no errors found. |
| 59 |
* The string should be HTML-clean, ready for putting inside a paragraph tag. |
| 60 |
* |
| 61 |
* @return string |
| 62 |
*/ |
| 63 |
public function validate() { |
| 64 |
$errmsg = ''; |
| 65 |
|
| 66 |
if (strlen($this->customerID) === 0) |
| 67 |
$errmsg .= "# Please enter the eWAY account number.<br/>\n"; |
| 68 |
|
| 69 |
return $errmsg; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Options admin |
| 75 |
*/ |
| 76 |
class GFEwayOptionsAdmin { |
| 77 |
|
| 78 |
private $plugin; // handle to the plugin object |
| 79 |
private $menuPage; // slug for admin menu page |
| 80 |
private $scriptURL = ''; |
| 81 |
private $frm; // handle for the form validator |
| 82 |
|
| 83 |
/** |
| 84 |
* @param GFEwayPlugin $plugin handle to the plugin object |
| 85 |
* @param string $menuPage URL slug for this admin menu page |
| 86 |
*/ |
| 87 |
public function __construct($plugin, $menuPage, $scriptURL) { |
| 88 |
$this->plugin = $plugin; |
| 89 |
$this->menuPage = $menuPage; |
| 90 |
$this->scriptURL = $scriptURL; |
| 91 |
|
| 92 |
wp_enqueue_script('jquery'); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* process the admin request |
| 97 |
*/ |
| 98 |
public function process() { |
| 99 |
$this->frm = new GFEwayOptionsForm(); |
| 100 |
if ($this->frm->isFormPost()) { |
| 101 |
check_admin_referer('save', $this->menuPage . '_wpnonce'); |
| 102 |
|
| 103 |
$errmsg = $this->frm->validate(); |
| 104 |
if (empty($errmsg)) { |
| 105 |
$this->plugin->options['customerID'] = $this->frm->customerID; |
| 106 |
$this->plugin->options['useStored'] = ($this->frm->useStored == 'Y'); |
| 107 |
$this->plugin->options['useTest'] = ($this->frm->useTest == 'Y'); |
| 108 |
$this->plugin->options['useBeagle'] = ($this->frm->useBeagle == 'Y'); |
| 109 |
$this->plugin->options['roundTestAmounts'] = ($this->frm->roundTestAmounts == 'Y'); |
| 110 |
$this->plugin->options['forceTestAccount'] = ($this->frm->forceTestAccount == 'Y'); |
| 111 |
$this->plugin->options['sslVerifyPeer'] = ($this->frm->sslVerifyPeer == 'Y'); |
| 112 |
|
| 113 |
update_option(GFEWAY_PLUGIN_OPTIONS, $this->plugin->options); |
| 114 |
$this->saveErrorMessages(); |
| 115 |
$this->plugin->showMessage(__('Options saved.')); |
| 116 |
} |
| 117 |
else { |
| 118 |
$this->plugin->showError($errmsg); |
| 119 |
} |
| 120 |
} |
| 121 |
else { |
| 122 |
// initialise form from stored options |
| 123 |
$this->frm->customerID = $this->plugin->options['customerID']; |
| 124 |
$this->frm->useStored = $this->plugin->options['useStored'] ? 'Y' : 'N'; |
| 125 |
$this->frm->useTest = $this->plugin->options['useTest'] ? 'Y' : 'N'; |
| 126 |
$this->frm->useBeagle = $this->plugin->options['useBeagle'] ? 'Y' : 'N'; |
| 127 |
$this->frm->roundTestAmounts = $this->plugin->options['roundTestAmounts'] ? 'Y' : 'N'; |
| 128 |
$this->frm->forceTestAccount = $this->plugin->options['forceTestAccount'] ? 'Y' : 'N'; |
| 129 |
$this->frm->sslVerifyPeer = $this->plugin->options['sslVerifyPeer'] ? 'Y' : 'N'; |
| 130 |
} |
| 131 |
|
| 132 |
require GFEWAY_PLUGIN_ROOT . 'views/admin-settings.php'; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* save error messages |
| 137 |
*/ |
| 138 |
private function saveErrorMessages() { |
| 139 |
$errNames = array ( |
| 140 |
GFEWAY_ERROR_ALREADY_SUBMITTED, |
| 141 |
GFEWAY_ERROR_NO_AMOUNT, |
| 142 |
GFEWAY_ERROR_REQ_CARD_HOLDER, |
| 143 |
GFEWAY_ERROR_REQ_CARD_NAME, |
| 144 |
GFEWAY_ERROR_EWAY_FAIL, |
| 145 |
); |
| 146 |
foreach ($errNames as $errName) { |
| 147 |
$msg = $this->frm->getPostValue($errName); |
| 148 |
delete_option($errName); |
| 149 |
if (!empty($msg)) { |
| 150 |
add_option($errName, $msg, '', 'no'); |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|