key = $key; $this->callback = $callback; $this->menuSlugs = $menuSlugs; } /** * * @return string */ public function getKey() { return $this->key; } /** * * @return string */ public function getNonceKey() { $result = 'dup_nonce_'; foreach ($this->menuSlugs as $slug) { $result .= $slug . '_'; } return str_replace(array('-', '.', '\\', '/'), '_', $result . $this->key); } /** * * @return string The token. */ public function getNonce() { return wp_create_nonce($this->getNonceKey()); } /** * * @param bool $echo * @return string */ public function getActionNonceFileds($echo = true) { ob_start(); wp_nonce_field($this->getNonceKey()); echo ''; if ($echo) { ob_end_flush(); return ''; } else { return ob_get_clean(); } } /** * return true if current page is the page of current action * * @param [string] $currentMenuSlugs * @return boolean */ public function isPageOfCurrentAction($currentMenuSlugs) { foreach ($this->menuSlugs as $index => $slug) { if (!isset($currentMenuSlugs[$index]) || $currentMenuSlugs[$index] != $slug) { return false; } } return true; } /** * return true if current current action is called * * @param [string] $currentMenuSlugs * @param string $action * @return boolean */ public function isCurrentAction($currentMenuSlugs, $action) { if ($action !== $this->key) { return false; } foreach ($this->menuSlugs as $index => $slug) { if (!isset($currentMenuSlugs[$index]) || $currentMenuSlugs[$index] != $slug) { return false; } } return true; } /** * * @return bool */ protected function verifyNonce() { $nonce = \DupProSnapLibUtil::filterInputRequest( '_wpnonce', FILTER_SANITIZE_STRING, array( 'options' => array( 'default' => false ) ) ); return wp_verify_nonce($nonce, $this->getNonceKey()); } /** * * @return mixed */ public function exec(&$resultData = array()) { $result = true; try { if (!$this->verifyNonce()) { throw new \Exception('Security issue on action ' . $this->key); } $funcResultData = call_user_func($this->callback); $resultData = array_merge($resultData, $funcResultData); } catch (\Exception $e) { $resultData['errorMessage'] = $e->getMessage(); $result = false; } catch (\Error $e) { $resultData['errorMessage'] = $e->getMessage(); $result = false; } return $result; } }