Beispiel #1
0
 /**
  * Import processor.
  *
  * @since 141111 First documented version.
  */
 protected function maybeImport()
 {
     if (!current_user_can($this->plugin->cap)) {
         return;
         // Unauthenticated; ignore.
     }
     $csv_headers = [];
     // Initialize.
     $current_csv_line_number = $current_csv_line_index = 0;
     if (!($csv_resource_file = $this->csvResourceFile())) {
         return;
         // Not possible; i.e. no resource.
     }
     while (($_csv_line = fgetcsv($csv_resource_file, 0, ',', '"', '"')) !== false) {
         ++$current_csv_line_number;
         // Increment line counter.
         ++$current_csv_line_index;
         // Increment line index also.
         $_csv_line = $this->plugin->utils_string->trimDeep($_csv_line);
         if ($current_csv_line_index === 1 && !empty($_csv_line[0])) {
             foreach ($_csv_line as $_csv_header) {
                 $csv_headers[] = (string) $_csv_header;
             }
             unset($_csv_header);
             // Housekeeping.
             --$current_csv_line_number;
             continue;
             // Skip this line.
         }
         if ($current_csv_line_index >= 1 && !$csv_headers) {
             $this->errors[] = __('Missing first-line CSV headers; please try again.', 'comment-mail');
             break;
             // Stop here; we have no headers in this importation.
         }
         if ($current_csv_line_index >= 1 && !in_array('ID', $csv_headers, true)) {
             if (!in_array('email', $csv_headers, true) || !in_array('post_id', $csv_headers, true)) {
                 $this->errors[] = __('First-line CSV headers MUST contain (at a minimum); one of:', 'comment-mail') . ' ' . __('<code>"ID"</code>, or <code>"email"</code> together with a <code>"post_id"</code>.', 'comment-mail');
                 break;
                 // Stop here; we have no headers in this importation.
             }
         }
         $_import = [];
         // Reset this on each pass.
         $_import['ID'] = $this->csvLineColumnValueFor('ID', $csv_headers, $_csv_line);
         $_import['key'] = $this->csvLineColumnValueFor('key', $csv_headers, $_csv_line);
         $_import['user_id'] = $this->csvLineColumnValueFor('user_id', $csv_headers, $_csv_line);
         $_import['post_id'] = $this->csvLineColumnValueFor('post_id', $csv_headers, $_csv_line);
         $_import['comment_id'] = $this->csvLineColumnValueFor('comment_id', $csv_headers, $_csv_line);
         $_import['deliver'] = $this->csvLineColumnValueFor('deliver', $csv_headers, $_csv_line);
         $_import['status'] = $this->csvLineColumnValueFor('status', $csv_headers, $_csv_line);
         $_import['fname'] = $this->csvLineColumnValueFor('fname', $csv_headers, $_csv_line);
         $_import['lname'] = $this->csvLineColumnValueFor('lname', $csv_headers, $_csv_line);
         $_import['email'] = $this->csvLineColumnValueFor('email', $csv_headers, $_csv_line);
         $_import['insertion_ip'] = $this->csvLineColumnValueFor('insertion_ip', $csv_headers, $_csv_line);
         $_import['insertion_region'] = $this->csvLineColumnValueFor('insertion_region', $csv_headers, $_csv_line);
         $_import['insertion_country'] = $this->csvLineColumnValueFor('insertion_country', $csv_headers, $_csv_line);
         $_import['last_ip'] = $this->csvLineColumnValueFor('last_ip', $csv_headers, $_csv_line);
         $_import['last_region'] = $this->csvLineColumnValueFor('last_region', $csv_headers, $_csv_line);
         $_import['last_country'] = $this->csvLineColumnValueFor('last_country', $csv_headers, $_csv_line);
         $_import['insertion_time'] = $this->csvLineColumnValueFor('insertion_time', $csv_headers, $_csv_line);
         $_import['last_update_time'] = $this->csvLineColumnValueFor('last_update_time', $csv_headers, $_csv_line);
         $_sub_inserter = new SubInserter($_import, ['process_confirmation' => $this->process_confirmations]);
         if ($_sub_inserter->didInsertUpdate()) {
             // Have insert|update success?
             ++$this->total_imported_subs;
             // Increment counter; this was a success.
         } elseif ($_sub_inserter->hasErrors()) {
             // If the inserter has errors for this line; report those.
             $_sub_inserter_errors = array_values($_sub_inserter->errors());
             // Values only; discard keys.
             $_sub_inserter_error_prefix = sprintf(__('_Line #%1$s:_', 'comment-mail'), esc_html($current_csv_line_number));
             foreach ($_sub_inserter_errors as &$_sub_inserter_error) {
                 $_sub_inserter_error = $_sub_inserter_error_prefix . ' ' . $_sub_inserter_error;
             }
             $this->errors = array_merge($this->errors, $_sub_inserter_errors);
         }
         unset($_sub_inserter, $_sub_inserter_errors);
         // Housekeeping.
         unset($_sub_inserter_error, $_sub_inserter_error_prefix);
         if ($current_csv_line_number + 1 > $this->max_limit) {
             break;
             // Reached the max limit.
         }
     }
     unset($_csv_line, $_import);
     // Housekeeping.
     fclose($csv_resource_file);
     // Close resource file.
     $this->enqueueNoticesAndRedirect();
     // Issue notices and redirect user.
 }
 /**
  * Form processor.
  *
  * @since 141111 First documented version.
  *
  * @param array $request_args Incoming action request args.
  *
  * @see   SubManageActions::subForm()
  */
 public static function process(array $request_args)
 {
     $plugin = plugin();
     // Needed below.
     $args = ['process_confirmation' => true, 'user_initiated' => true, 'ui_protected_data_keys_enable' => true, 'ui_protected_data_user' => wp_get_current_user()];
     static::$processing = true;
     // Flag as `TRUE`; along w/ other statics below.
     if (isset($request_args['key'])) {
         // Key sanitizer; for added security.
         $request_args['key'] = $sub_key = $plugin->utils_sub->sanitizeKey($request_args['key']);
     }
     if (isset($request_args['ID'])) {
         // Updating an existing subscription via ID?
         $sub_updater = new SubUpdater($request_args, $args);
         // Run updater.
         if ($sub_updater->hasErrors()) {
             // Updater has errors?
             static::$processing_errors = $sub_updater->errors();
             static::$processing_error_codes = $sub_updater->errorCodes();
             static::$processing_errors_html = $sub_updater->errorsHtml();
         } elseif ($sub_updater->didUpdate()) {
             // Updated?
             static::$processing_successes = $sub_updater->successes();
             static::$processing_success_codes = $sub_updater->successCodes();
             static::$processing_successes_html = $sub_updater->successesHtml();
             static::$processing_email_key_change = $sub_updater->emailKeyChanged();
         }
     } elseif ($plugin->options['enable'] && $plugin->options['new_subs_enable']) {
         // This check is for added security only. The form should not be available.
         $sub_inserter = new SubInserter($request_args, $args);
         // Run inserter.
         if ($sub_inserter->hasErrors()) {
             // Inserter has errors?
             static::$processing_errors = $sub_inserter->errors();
             static::$processing_error_codes = $sub_inserter->errorCodes();
             static::$processing_errors_html = $sub_inserter->errorsHtml();
         } elseif ($sub_inserter->didInsert()) {
             // Inserted?
             static::$processing_successes = $sub_inserter->successes();
             static::$processing_success_codes = $sub_inserter->successCodes();
             static::$processing_successes_html = $sub_inserter->successesHtml();
         }
     }
 }