Last active
October 10, 2017 07:29
-
-
Save orangerdev/cf97f34b5a104afd622cca569c813ac2 to your computer and use it in GitHub Desktop.
TASK 2 - WP Endpoint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace WP_Endpoint; | |
class Notifier | |
{ | |
protected $valid = true; | |
protected $messages = []; | |
public $email = ''; | |
public $company_id = ''; | |
/** | |
* Construction | |
*/ | |
public function __construct() | |
{ | |
add_action('wp_ajax_add-notifier-email' ,[$this,'prepare']); | |
} | |
/** | |
* Validating input data | |
* @return void | |
*/ | |
public function validation() | |
{ | |
if(!filter_var($this->email,FILTER_VALIDATE_EMAIL)) : | |
$this->valid = false; | |
$this->messages[] = __('Email address is not valid','Endpoint'); | |
endif; | |
if(empty($this->company_id)) : | |
$this->valid = false; | |
$this->messages[] = __('Company ID is empty or incorrect value','Endpoint'); | |
endif; | |
} | |
/** | |
* Add new data with extra carefull | |
* @return void | |
*/ | |
public function prepare() | |
{ | |
if(isset($_POST['email']) && isset($_POST['company_id']) && current_user_can('manage_options')) : | |
$this->email = $_POST['email']; | |
$this->company_id = (int) $_POST['company_id']; | |
$this->validation(); | |
if($this->valid) : | |
$this->update(); | |
$this->messages[] = __('Field updated','Endpoint'); | |
endif; | |
else : | |
$this->valid = false; | |
$this->messages[] = __('Sorry, you didn\'t send with correct parameters','Endpoint'); | |
endif; | |
wp_send_json([ | |
'valid' => $this->valid, | |
'messages' => $this->messages | |
]); | |
} | |
/** | |
* Insert new email | |
* @return void | |
*/ | |
public function update() | |
{ | |
$emails = get_field('email_notifiers',$this->company_id); | |
$emails[]['email'] = $this->email; | |
update_field('email_notifiers',$emails,$this->company_id); | |
} | |
} | |
new Notifier; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment