/**
  * Plugin main action processing function. Will filter the action called then
  * pass on to other sub-functions
  *
  * @since 1.0.0
  * @uses $_REQUEST global PHP object
  *
  * @param none
  *
  * @return none
  */
 function snapshot_process_actions()
 {
     if (is_multisite()) {
         if (!is_super_admin()) {
             return;
         }
     } else {
         if (!current_user_can('manage_snapshots_items')) {
             return;
         }
     }
     //echo "_POST<pre>"; print_r($_POST); echo "</pre>";
     //echo "_GET<pre>"; print_r($_GET); echo "</pre>";
     //die();
     $ACTION_FOUND = false;
     if (isset($_REQUEST['snapshot-action'])) {
         $snapshot_action = sanitize_text_field($_REQUEST['snapshot-action']);
         switch ($snapshot_action) {
             case 'add':
                 if (empty($_POST) || !wp_verify_nonce($_POST['snapshot-noonce-field'], 'snapshot-add')) {
                     return;
                 } else {
                     $this->snapshot_add_update_action_proc($_POST);
                 }
                 $ACTION_FOUND = true;
                 break;
             case 'delete-bulk':
                 if (empty($_POST) || !wp_verify_nonce($_POST['snapshot-noonce-field'], 'snapshot-delete')) {
                     return;
                 } else {
                     if (isset($_POST['delete-bulk']) && count($_POST['delete-bulk'])) {
                         $this->snapshot_delete_bulk_action_proc();
                         $ACTION_FOUND = true;
                     }
                 }
                 break;
             case 'delete-item':
                 if (empty($_GET) || !wp_verify_nonce($_GET['snapshot-noonce-field'], 'snapshot-delete-item')) {
                     return;
                 } else {
                     $this->snapshot_delete_item_action_proc();
                     $ACTION_FOUND = true;
                 }
                 break;
             case 'update':
                 if (empty($_POST) || !wp_verify_nonce($_POST['snapshot-noonce-field'], 'snapshot-update')) {
                     die;
                 } else {
                     $this->snapshot_add_update_action_proc($_POST);
                     $ACTION_FOUND = true;
                 }
                 break;
             case 'runonce':
                 if (empty($_GET) || !wp_verify_nonce($_GET['snapshot-noonce-field'], 'snapshot-runonce')) {
                     return;
                 } else {
                     if (!isset($_GET['item'])) {
                         return;
                     }
                     $this->snapshot_item_run_immediate(intval($_GET['item']));
                     $return_url = wp_get_referer();
                     if (!isset($_GET['page'])) {
                         $_GET['page'] = 'snapshots_edit_panel';
                     }
                     if ($_GET['page'] == 'snapshots_edit_panel') {
                         $return_url = remove_query_arg(array('item'), $return_url);
                     }
                     $return_url = remove_query_arg(array('snapshot-action', 'snapshot-noonce-field'), $return_url);
                     $return_url = add_query_arg('page', sanitize_text_field($_GET['page']), $return_url);
                     $return_url = add_query_arg('message', 'success-runonce', $return_url);
                     $return_url = esc_url_raw($return_url);
                     if ($return_url) {
                         wp_redirect($return_url);
                     }
                     die;
                 }
                 break;
             case 'settings-update':
                 if (empty($_POST) || !wp_verify_nonce($_POST['snapshot-noonce-field'], 'snapshot-settings')) {
                     return;
                 } else {
                     $this->snapshot_settings_config_update();
                     $ACTION_FOUND = true;
                 }
                 break;
             case 'download-archive':
             case 'download-log':
                 if (isset($_GET['snapshot-item']) && isset($_GET['snapshot-data-item'])) {
                     $item_key = intval($_GET['snapshot-item']);
                     if (isset($this->config_data['items'][$item_key])) {
                         $item = $this->config_data['items'][$item_key];
                         $data_item_key = intval($_GET['snapshot-data-item']);
                         if (isset($item['data'][$data_item_key])) {
                             $data_item = $item['data'][$data_item_key];
                             if ($snapshot_action == 'download-archive') {
                                 if (isset($data_item['filename'])) {
                                     if (empty($data_item['destination']) || $data_item['destination'] == "local") {
                                         $current_backupFolder = $this->snapshot_get_item_destination_path($item, $data_item);
                                     } else {
                                         $current_backupFolder = $this->get_setting('backupBaseFolderFull');
                                     }
                                     if (empty($current_backupFolder)) {
                                         $current_backupFolder = $this->get_setting('backupBaseFolderFull');
                                     }
                                     $current_backupFile = trailingslashit($current_backupFolder) . $data_item['filename'];
                                     if (file_exists($current_backupFile)) {
                                         header('Content-Description: Snapshot Archive File');
                                         header('Content-Type: application/zip');
                                         header('Content-Disposition: attachment; filename=' . $data_item['filename']);
                                         header('Content-Transfer-Encoding: binary');
                                         header('Expires: 0');
                                         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                                         header('Pragma: public');
                                         header('Content-Length: ' . filesize($current_backupFile));
                                         Snapshot_Helper_Utility::file_output_stream_chunked($current_backupFile);
                                         flush();
                                         die;
                                     }
                                 }
                             } else {
                                 if ($snapshot_action == 'download-log') {
                                     $backupLogFileFull = trailingslashit($this->get_setting('backupLogFolderFull')) . $item['timestamp'] . "_" . $data_item['timestamp'] . ".log";
                                     if (file_exists($backupLogFileFull)) {
                                         header('Content-Description: Snapshot Log File');
                                         header('Content-Type: application/text');
                                         header('Content-Disposition: attachment; filename=' . basename($backupLogFileFull));
                                         header('Content-Transfer-Encoding: text');
                                         header('Expires: 0');
                                         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                                         header('Pragma: public');
                                         header('Content-Length: ' . filesize($backupLogFileFull));
                                         Snapshot_Helper_Utility::file_output_stream_chunked($backupLogFileFull);
                                         flush();
                                         die;
                                     }
                                 }
                             }
                         }
                     }
                 }
                 $ACTION_FOUND = false;
                 break;
             case 'item-archives':
                 $CONFIG_CHANGED = false;
                 $item_key = intval($_GET['item']);
                 if (isset($this->config_data['items'][$item_key])) {
                     $item = $this->config_data['items'][$item_key];
                     $action = '';
                     if (isset($_GET['action']) && $_GET['action'] != "-1") {
                         $action = sanitize_text_field($_GET['action']);
                     } else {
                         if (isset($_GET['action2']) && $_GET['action2'] != "-1") {
                             $action = sanitize_text_field($_GET['action2']);
                         }
                     }
                     //echo "action=[". $action ."]<br />";
                     switch ($action) {
                         case 'resend':
                             if ($item['destination-sync'] == "mirror") {
                                 $snapshot_sync_files_option = 'wpmudev_snapshot_sync_files_' . $item['timestamp'];
                                 delete_option($snapshot_sync_files_option);
                             } else {
                                 $resend_data_items = intval($_REQUEST['data-item']);
                                 if (!is_array($resend_data_items)) {
                                     $resend_data_items = array($resend_data_items);
                                 }
                                 foreach ($resend_data_items as $data_item_key) {
                                     if (!isset($item['data'][$data_item_key])) {
                                         continue;
                                     }
                                     $data_item = $item['data'][$data_item_key];
                                     if (isset($data_item['filename'])) {
                                         $current_backupFolder = $this->get_setting('backupBaseFolderFull');
                                         $current_backupFile = trailingslashit($current_backupFolder) . $data_item['filename'];
                                         if (!file_exists($current_backupFile)) {
                                             continue;
                                         }
                                         $data_item['destination-status'] = array();
                                     }
                                     $this->config_data['items'][$item_key]['data'][$data_item_key] = $data_item;
                                     $CONFIG_CHANGED = true;
                                 }
                             }
                             break;
                         case 'delete':
                             $delete_data_items = $_REQUEST['data-item'];
                             if (!is_array($delete_data_items)) {
                                 $delete_data_items = array($delete_data_items);
                             }
                             foreach ($delete_data_items as $data_item_key) {
                                 $data_item_key = intval($data_item_key);
                                 if (!isset($item['data'][$data_item_key])) {
                                     continue;
                                 }
                                 $data_item = $item['data'][$data_item_key];
                                 // Delete the archive file
                                 if (isset($data_item['filename'])) {
                                     $current_backupFolder = $this->get_setting('backupBaseFolderFull');
                                     $current_backupFile = trailingslashit($current_backupFolder) . $data_item['filename'];
                                     if (file_exists($current_backupFile)) {
                                         unlink($current_backupFile);
                                     }
                                 }
                                 // Delete the log file
                                 $backupLogFileFull = trailingslashit($this->get_setting('backupLogFolderFull')) . $item['timestamp'] . "_" . $data_item['timestamp'] . ".log";
                                 if (file_exists($backupLogFileFull)) {
                                     unlink($backupLogFileFull);
                                 }
                                 // Delete the data_item itself
                                 unset($this->config_data['items'][$item_key]['data'][$data_item_key]);
                                 $CONFIG_CHANGED = true;
                             }
                             break;
                     }
                 }
                 if ($CONFIG_CHANGED == true) {
                     $this->save_config();
                 }
                 $per_page = 20;
                 if (isset($_POST['wp_screen_options']['option']) && $_POST['wp_screen_options']['option'] == "toplevel_page_snapshots_edit_panel_network_per_page") {
                     if (isset($_POST['wp_screen_options']['value'])) {
                         $per_page = intval($_POST['wp_screen_options']['value']);
                         if (!$per_page || $per_page < 1) {
                             $per_page = 20;
                         }
                         update_user_meta(get_current_user_id(), 'snapshot_data_items_per_page', $per_page);
                     }
                 }
                 //$this->archives_data_items_table = new Snapshot_Archives_Data_Items_Table( $this );
                 add_screen_option('per_page', array('label' => __('per Page', SNAPSHOT_I18N_DOMAIN), 'default' => $per_page));
                 $ACTION_FOUND = true;
                 break;
                 /*
                 					case 'archives-import':
                 						if ( empty($_POST) || !wp_verify_nonce($_POST['snapshot-noonce-field'],'snapshot-settings') )
                 							die();
                 						else {
                 							$this->snapshot_archives_import_proc();
                 							$ACTION_FOUND = true;
                 						}
                 
                 						break;
                 */
             /*
             					case 'archives-import':
             						if ( empty($_POST) || !wp_verify_nonce($_POST['snapshot-noonce-field'],'snapshot-settings') )
             							die();
             						else {
             							$this->snapshot_archives_import_proc();
             							$ACTION_FOUND = true;
             						}
             
             						break;
             */
             default:
                 break;
         }
     }
     if (!$ACTION_FOUND) {
         $per_page = 20;
         if (isset($_POST['wp_screen_options']['option']) && $_POST['wp_screen_options']['option'] == "toplevel_page_snapshots_edit_panel_network_per_page") {
             if (isset($_POST['wp_screen_options']['value'])) {
                 $per_page = intval($_POST['wp_screen_options']['value']);
                 if (!$per_page || $per_page < 1) {
                     $per_page = 20;
                 }
                 update_user_meta(get_current_user_id(), 'snapshot_items_per_page', $per_page);
             }
         }
         add_screen_option('per_page', array('label' => __('per Page', SNAPSHOT_I18N_DOMAIN), 'default' => $per_page));
     }
 }