/**
  * 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();
         }
     }
 }
 /**
  * Form processor.
  *
  * @since 141111 First documented version.
  *
  * @param array $request_args Incoming action request args.
  *
  * @see   MenuPageActions::subForm()
  */
 public static function process(array $request_args)
 {
     $plugin = plugin();
     // Needed below.
     if (!current_user_can($plugin->manage_cap)) {
         if (!current_user_can($plugin->cap)) {
             return;
             // Unauthenticated; ignore.
         }
     }
     $reporting_errors = false;
     // Initialize.
     $process_confirmation = !empty($request_args['process_confirmation']);
     $args = compact('process_confirmation');
     if (isset($request_args['ID'])) {
         // Updating an existing subscription via ID?
         $sub_updater = new SubUpdater($request_args, $args);
         // Run updater.
         if ($sub_updater->didUpdate()) {
             // Updated successfully?
             $plugin->enqueueUserNotice(sprintf(__('Subscription ID #<code>%1$s</code> updated successfully.', 'comment-mail'), esc_html($request_args['ID'])), ['transient' => true, 'for_page' => $plugin->utils_env->currentMenuPage()]);
             $redirect_to = $plugin->utils_url->pageTableNavVarsOnly();
         } else {
             // There were errors; display those errors to the current user.
             $plugin->enqueueUserError(sprintf(__('Failed to update subscription ID #<code>%1$s</code>. Please review the following error(s):', 'comment-mail'), esc_html($request_args['ID'])) . '<ul class="pmp-list-items"><li>' . implode('</li><li>', $sub_updater->errorsHtml()) . '</li></ul>', ['transient' => true, 'for_page' => $plugin->utils_env->currentMenuPage()]);
         }
     } else {
         // We are doing a new insertion; i.e. a new subscription is being added here.
         $sub_inserter = new SubInserter($request_args, $args);
         // Run inserter.
         if ($sub_inserter->didInsert()) {
             // Inserted successfully?
             $plugin->enqueueUserNotice(sprintf(__('Subscription ID #<code>%1$s</code> created successfully.', 'comment-mail'), esc_html($sub_inserter->insertId())), ['transient' => true, 'for_page' => $plugin->utils_env->currentMenuPage()]);
             $redirect_to = $plugin->utils_url->pageTableNavVarsOnly();
         } else {
             // There were errors; display those errors to the current user.
             $plugin->enqueueUserError(__('Failed to create new subscription. Please review the following error(s):', 'comment-mail') . '<ul class="pmp-list-items"><li>' . implode('</li><li>', $sub_inserter->errorsHtml()) . '</li></ul>', ['transient' => true, 'for_page' => $plugin->utils_env->currentMenuPage()]);
         }
     }
     if (!empty($redirect_to)) {
         // If applicable.
         if (headers_sent()) {
             // Output started already?
             exit('      <script type="text/javascript">' . "         document.getElementsByTagName('body')[0].style.display = 'none';" . "         location.href = '" . $plugin->utils_string->escJsSq($redirect_to) . "';" . '      </script>' . '   </body>' . '</html>');
         }
         wp_redirect($redirect_to);
         exit;
     }
 }
Пример #3
0
 /**
  * Sub. import processor.
  *
  * @since 141111 First documented version.
  *
  * @param int       $post_id Post ID.
  * @param \stdClass $sub     Subscriber obj. data.
  */
 protected function maybeImportSub($post_id, \stdClass $sub)
 {
     if (!($post_id = (int) $post_id)) {
         return;
         // Not possible.
     }
     if (empty($sub->email) || empty($sub->time) || empty($sub->status)) {
         $this->logFailure('Not importing subscription; data missing', $sub);
         return;
         // Not possible; data missing.
     }
     if ($sub->status !== 'Y' && $sub->status !== 'R') {
         $this->logFailure('Not importing subscription; not an active subscriber', $sub);
         return;
         // Not an active subscriber.
     }
     if ($sub->status === 'Y') {
         // All comments?
         $sub_insert_data = ['post_id' => $post_id, 'status' => 'subscribed', 'deliver' => 'asap', 'fname' => $sub->fname, 'email' => $sub->email, 'insertion_time' => $sub->time];
         $sub_inserter = new SubInserter($sub_insert_data);
         if ($sub_inserter->didInsert()) {
             ++$this->total_imported_subs;
             ++$this->total_created_subs;
         } else {
             $this->logFailure('Failed to insert an All Comments (Y) subscription', array_merge($sub_insert_data, $sub_inserter->errors()));
             ++$this->total_skipped_subs;
         }
     } else {
         // Otherwise, specific comment(s) only; i.e. "Replies Only".
         $_sub_comment_ids = $this->subCommentIds($post_id, $sub->email);
         if (!empty($_sub_comment_ids)) {
             /*
              * This is where the behavior of Comment Mail and StCR diverge when it comes to how they store subscriptions.
              * StCR only stores one (1) `R` subscription per email per Post ID, while Comment Mail creates a Replies Only subscription
              * for each comment the user has posted on a given Post ID. That means the Total StCR Subscriptions imported will
              * likely be much lower than the total subscriptions created by Comment Mail. See also: http://bit.ly/1QtwEWO
              *
              * Note how we count imported subs outside of this foreach loop, but we count created subs inside the foreach loop.
              */
             ++$this->total_imported_subs;
             foreach ($_sub_comment_ids as $_comment_id) {
                 $_sub_insert_data = ['post_id' => $post_id, 'comment_id' => $_comment_id, 'status' => 'subscribed', 'deliver' => 'asap', 'fname' => $sub->fname, 'email' => $sub->email, 'insertion_time' => $sub->time];
                 $_sub_inserter = new SubInserter($_sub_insert_data);
                 if ($_sub_inserter->didInsert()) {
                     ++$this->total_created_subs;
                 } else {
                     $this->logFailure('Failed to import Replies Only (R) subscription', array_merge($_sub_insert_data, $_sub_inserter->errors()));
                     ++$this->total_skipped_subs;
                     --$this->total_imported_subs;
                     // Imported subs are counted outside this foreach loop, so we need to decrease here when we have a failure.
                 }
             }
         } else {
             // No comments associated with $sub->email were found for $post_id
             $this->logFailure('Failed to import Replies Only (R) subscription', ['note' => 'Associated comment has been deleted, trashed, or marked as spam', 'post_id' => $post_id, 'email' => $sub->email]);
             ++$this->total_skipped_subs;
         }
         unset($_comment_id, $_sub_insert_data, $_sub_inserter, $_sub_comment_ids);
         // Housekeeping.
     }
 }