public function __construct($url, $http_method = null)
 {
     parent::__construct($url, $http_method);
     // Check token is registered
     if ($this->is_token_missing()) {
         $this->json_error('401', __("Token not registered.\nTo solve the problem please uninstall and reinstall the app.", 'pnfw'));
     }
     // Update lang
     if (isset($this->lang)) {
         $this->update_token_lang();
     }
 }
 public function __construct()
 {
     parent::__construct(site_url('pnfw/register/'), 'POST');
     add_filter('wp_mail_from_name', array($this, 'from_name'));
     // Optional
     $prevToken = $this->opt_parameter('prevToken', FILTER_SANITIZE_STRING);
     $this->email = $this->opt_parameter('email', FILTER_VALIDATE_EMAIL);
     $this->activation_code = $this->get_activation_code();
     global $wpdb;
     $push_tokens = $wpdb->get_blog_prefix() . 'push_tokens';
     if (isset($prevToken)) {
         // Check if registered
         if ($this->is_token_missing($prevToken)) {
             $this->json_error('404', __('prevToken not found', 'pnfw'));
         }
         if ($prevToken == $this->token) {
             exit;
             // nothing to do
         }
         $data = array('token' => $this->token, 'os' => $this->os, 'lang' => $this->lang);
         $where = array('token' => $prevToken, 'os' => $this->os);
         $count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$push_tokens} WHERE token = %s AND os = %s", $this->token, $this->os));
         if ($count != 0) {
             pnfw_log(PNFW_SYSTEM_LOG, sprintf(__('Attempted an update of an %s token equal to a token already present: %s.', 'pnfw'), $this->os, $this->token));
             // Delete destination token to allow overwrite
             $wpdb->delete($push_tokens, $where);
         }
         // Update prevToken with new token
         $wpdb->update($push_tokens, $data, $where);
     } else {
         $user_id = email_exists($this->email);
         // If the user does not exist it is created, otherwise the role app_subscriber is added
         if (empty($user_id) || is_null($user_id)) {
             $user_id = $this->create_user($this->email);
         } else {
             $user = new WP_User($user_id);
             $user->add_role(PNFW_Push_Notifications_for_WordPress_Lite::USER_ROLE);
         }
         $activation_link = esc_url(add_query_arg(array('activation_code' => $this->activation_code), site_url('/pnfw/activate')));
         // Following code should not be accessed simultaneously by different threads
         $push_logs = $wpdb->get_blog_prefix() . 'push_logs';
         $wp_options = $wpdb->options;
         $wpdb->query("LOCK TABLES {$push_tokens} WRITE, {$push_logs} WRITE, {$wp_options} READ;");
         $active = empty($this->email);
         if (get_option('pnfw_disable_email_verification')) {
             $active = true;
         }
         // If the device does not exist it is created, otherwise the user_id is updated
         if ($this->is_token_missing()) {
             $data = array('token' => $this->token, 'os' => $this->os, 'lang' => $this->lang, 'timestamp' => current_time('mysql'), 'user_id' => $user_id, 'active' => $active, 'activation_code' => $this->activation_code);
             $wpdb->insert($push_tokens, $data);
             $wpdb->query("UNLOCK TABLES;");
             if (!get_option('pnfw_disable_email_verification')) {
                 $this->new_device_email($user_id, $activation_link);
             }
         } else {
             if ($this->reassign_token_to($user_id)) {
                 $this->new_device_email($user_id, $activation_link);
             }
         }
     }
     exit;
 }