/**
  * Process user request.
  * 
  * @return response
  */
 public function settings_init()
 {
     // settings process
     if ('POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['action']) && $_POST['action'] == 'update-classified-settings') {
         if (wp_verify_nonce($_POST['update_request_nonce'], 'update_request')) {
             $error = false;
             if (!empty($_POST['classified_notification_receiver'])) {
                 $classified_notification_receiver = $_POST['classified_notification_receiver'];
                 if (!preg_match("/^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,4})\$/", $classified_notification_receiver)) {
                     $error = true;
                     $this->error_message = __('Please enter a valid email.', $this->plugin_slug);
                 } else {
                     update_option('classified_notification_receiver', $classified_notification_receiver);
                 }
             }
             if (!empty($_POST['free_user_classified_number'])) {
                 $free_user_classified_count = (int) $_POST['free_user_classified_number'];
                 update_option('free_user_classified_number', $free_user_classified_count);
             }
             if (!empty($_POST['premium_user_classified_number'])) {
                 $premium_user_classified_count = (int) $_POST['premium_user_classified_number'];
                 update_option('premium_user_classified_number', $premium_user_classified_count);
             }
             if (!empty($_POST['classified_premium_user_role'])) {
                 update_option('classified_premium_user_role', sanitize_text_field($_POST['classified_premium_user_role']));
             }
             if (!$error) {
                 $this->success_message = __('Successfully updated!', $this->plugin_slug);
             }
         } else {
             $this->error_message = __('Oops, Nonce mismatched!', $this->plugin_slug);
         }
     } elseif ('POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['action']) && $_POST['action'] == 'import-classified') {
         if (wp_verify_nonce($_POST['import_request_nonce'], 'import_request')) {
             if (!empty($_FILES['file']['name'])) {
                 $this_file = $_FILES['file']['tmp_name'];
                 $extension = explode('.', $_FILES['file']['name']);
                 $extension = end($extension);
                 $extension = strtolower($extension);
                 if ($extension == 'xml') {
                     // process this file
                     $process = zoo_process_xml_file($this_file);
                     if ($process) {
                         // insert into database
                         $import = zoo_save_classified_posts_from_xml($process);
                         if ($import) {
                             $this->success_message = __('Successfully imported!', $this->plugin_slug);
                         } else {
                             $this->error_message = __('Something went wrong, please try again.', $this->plugin_slug);
                         }
                     } else {
                         $this->error_message = __('Invalid xml file.', $this->plugin_slug);
                     }
                 } else {
                     $this->error_message = __('Only xml file type is allowed.', $this->plugin_slug);
                 }
             } else {
                 $this->error_message = __('Please, choose a file to start the import process.', $this->plugin_slug);
             }
         } else {
             $this->error_message = __('Oops, Nonce mismatched!', $this->plugin_slug);
         }
     } elseif ('POST' == $_SERVER['REQUEST_METHOD'] && !empty($_GET['action']) && $_GET['action'] == 'export-classified') {
         if (wp_verify_nonce($_POST['export_request_nonce'], 'export_request')) {
             $args = array('sort' => isset($_POST['sort']) ? sanitize_text_field($_POST['sort']) : '', 'from' => isset($_POST['from']) ? sanitize_text_field($_POST['from']) : '', 'to' => isset($_POST['to']) ? sanitize_text_field($_POST['to']) : '', 'number_of_posts' => isset($_POST['number_of_posts']) ? (int) $_POST['number_of_posts'] : '', 'offset' => isset($_POST['offset']) ? (int) $_POST['offset'] : '');
             zoo_export_classifieds_as_csv($args);
         } else {
             wp_die('Oops, Nonce mismatched!', $this->plugin_slug);
         }
     }
 }
 function zoo_auto_import_classifieds()
 {
     $locale = 'classified-ads';
     $host_dir = get_home_path();
     $current_time = date('F j, Y, g:i a', time() - 6 * 3600);
     $log_file_name = ZOO_CLASSIFIED_LOG_FILE;
     $log_file_path = "{$host_dir}/{$log_file_name}";
     $folder_name = ZOO_CLASSIFIED_XML_DIR;
     $xml_dir = "{$host_dir}/{$folder_name}";
     $xml_files = zoo_scan_dir($xml_dir);
     $completed_xml_folder_name = ZOO_CLASSIFIED_COMPLETED_XML_DIR;
     $completed_xml_dir = "{$host_dir}/{$completed_xml_folder_name}";
     $uncompleted_xml_folder_name = ZOO_CLASSIFIED_UNCOMPLETED_XML_DIR;
     $uncompleted_xml_dir = "{$host_dir}/{$uncompleted_xml_folder_name}";
     $log_message = null;
     $increment = 0;
     // process the xml files one by one
     if ($xml_files) {
         foreach ($xml_files as $file) {
             // we will import one file at a time
             if ($increment == 1) {
                 break;
             }
             $file_path = "{$xml_dir}/{$file}";
             $process = zoo_process_xml_file($file_path);
             if ($process) {
                 // insert into database
                 $import = zoo_save_classified_posts_from_xml($process);
                 if ($import) {
                     $log_message = $current_time . '___' . __("Successfully imported({$file}).", $locale);
                     // send a confirmation mail to admin
                     $send_to_address = get_option('zoo_send_backup_email');
                     $subject = sprintf(__('[%s] Classified Import', $locale), get_bloginfo());
                     $message = sprintf(__('%s has been successfully imported.', $locale), $file);
                     $attachment = array($file_path);
                     wp_mail($send_to_address, $subject, $message, null, $attachment);
                     // send this file to "completed-xml" folder then delete this file from "xml" folder
                     copy("{$xml_dir}/{$file}", "{$completed_xml_dir}/{$file}");
                     unlink("{$xml_dir}/{$file}");
                 } else {
                     $log_message = $current_time . '___' . __("There was a problem when importing({$file}).", $locale);
                 }
             } else {
                 $log_message = $current_time . '___' . __("Invalid xml file({$file}).", $locale);
                 // send this file to "uncompleted-xml" folder then delete this file from "xml" folder
                 copy("{$xml_dir}/{$file}", "{$uncompleted_xml_dir}/{$file}");
                 unlink("{$xml_dir}/{$file}");
             }
             file_put_contents($log_file_path, $log_message . PHP_EOL, FILE_APPEND);
             $increment++;
         }
     } else {
         $log_message = $current_time . '___' . __('No new xml file found to import.', $locale);
         file_put_contents($log_file_path, $log_message . PHP_EOL, FILE_APPEND);
     }
 }