returnResult=$returnResult; $this->defaultNonce=$this->GetDefaultNonce(); $this->Loader=$loader; $this->prefix=$this->Loader->Prefix; $this->privatePrefix='wp_ajax_'.$this->Loader->Prefix.'_'; $this->publicPrefix='wp_ajax_nopriv_'.$this->Loader->Prefix.'_'; $this->RegisterHooks(); } abstract function GetDefaultNonce(); protected function RegisterPrivate($name,$methodName,$capability='administrator', $validateNonce=true,$nonceName=false,$monitorUnhandledErrors=true) { if($nonceName==false) $nonceName=$this->defaultNonce; \add_action($this->privatePrefix.$name,function() use($methodName,$validateNonce,$nonceName,$capability,$monitorUnhandledErrors){ if($monitorUnhandledErrors==true) $this->MonitorUnhandledErrors();; if($validateNonce) $this->VerifyNonce($nonceName); if($capability!='') if(!\current_user_can($capability)) $this->SendErrorMessage(__('Forbidden','advanced-emailing-for-woocommerce')); try { $this->$methodName(); }catch(FriendlyException $exception) { $this->SendException($exception); } die(); }); } protected function RegisterPublic($name,$methodName, $validateNonce=true,$nonceName=false,$monitorUnhandledErrors=true) { if($nonceName==false) $nonceName=$this->defaultNonce; $this->RegisterPrivate($name,$methodName,'',$validateNonce,$nonceName); \add_action($this->publicPrefix.$name,function() use($methodName,$validateNonce,$nonceName,$monitorUnhandledErrors){ if($monitorUnhandledErrors==true) $this->MonitorUnhandledErrors();; if($validateNonce) $this->VerifyNonce($nonceName); try { $this->$methodName(); }catch (FriendlyException $exception) { $this->SendException($exception); } die(); }); } public function GetData(){ if($this->data==null) if(isset($_POST['data'])) $this->data=ObjectSanitizer::Sanitize(json_decode(stripslashes($_POST['data'])),(object)['Rows'=>'']); else if(isset($_GET['data'])) $this->data=ObjectSanitizer::Sanitize(json_decode(stripslashes($_GET['data'])),(object)['Rows'=>'']); else $this->SendErrorMessage(__('Invalid operation, data does not exist','advanced-emailing-for-woocommerce')); return $this->data; } public function GetOptional($propertyName,$defaultValue='') { if(!isset($_POST['data'])&&!isset($_GET['data'])) return $defaultValue; if(!property_exists($this->GetData(),$propertyName)) { return $defaultValue; } return $this->data->$propertyName; } public function GetRequiredRegex($propertyName,$pattern,$errorMessage='') { $value=$this->GetRequired($propertyName); if(!preg_match($pattern,$value)) $this->SendErrorMessage($errorMessage==''?'Invalid property '.$propertyName:$errorMessage); return $value; } public function GetRequired($propertyName) { if(!property_exists($this->GetData(),$propertyName)) { $this->SendErrorMessage('Property '.$propertyName. ' does not exists'); } return $this->data->$propertyName; } public function GetRequiredNumber($propertyName) { return intval($this->GetRequired($propertyName)); } public function SendErrorMessage($message){ echo json_encode(array( 'success'=>false, 'errorMessage'=>$message )); die(); } /** * @param $e Exception|FriendlyException */ public function SendException($e,$prefixLogDetails='',$debugModeEnabled=false){ $friendlyMessage=__('An error occurred','advanced-emailing-for-woocommerce'); if($e instanceof FriendlyException) $friendlyMessage=$e->GetFriendlyException(); $message=$e->getMessage().' - '.$e->getTraceAsString(); $detail=''; if($debugModeEnabled) $detail=$message; \rnadvanceemailingwc\Managers\LogManager::LogError($prefixLogDetails.$message); echo json_encode(array( 'success'=>false, 'errorMessage'=>$friendlyMessage, 'detail'=>$e->getMessage().' - '.$e->getTraceAsString() )); die(); } public function SendSuccessMessage($data='') { if($this->returnResult) { return $data; } echo json_encode(array( 'success'=>true, 'result'=>$data )); die(); } public function MonitorUnhandledErrors(){ \add_filter('wp_die_ajax_handler',function () { $error =\error_get_last(); $friendlyException=new FriendlyException(__('An unexpected error occurred','advanced-emailing-for-woocommerce'),$error['message']); $this->SendException($friendlyException,"An error occurred",$error['message']); die(); }); } abstract protected function RegisterHooks(); private function VerifyNonce($nonceName) { $nonce=''; if(isset($_GET['_nonce'])) $nonce=$_GET['_nonce']; if(isset($_POST['_nonce'])) $nonce=$_POST['_nonce']; if($nonce==''||!\wp_verify_nonce($nonce, $this->prefix.'_'.$nonceName)) $this->SendErrorMessage(__('Invalid request, please refresh the screen and try again','advanced-emailing-for-woocommerce')); } }