|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * The specific functions for the bulk action. |
| 4 | + * |
| 5 | + * @package ScrubCommentAuthorIP |
| 6 | + */ |
| 7 | + |
| 8 | +// Call our namepsace. |
| 9 | +namespace Norcross\ScrubCommentAuthorIP\BulkProcess; |
| 10 | + |
| 11 | +// Set our alias items. |
| 12 | +use Norcross\ScrubCommentAuthorIP as Core; |
| 13 | +use Norcross\ScrubCommentAuthorIP\Helpers as Helpers; |
| 14 | +use Norcross\ScrubCommentAuthorIP\Database as Database; |
| 15 | + |
| 16 | +/** |
| 17 | + * Start our engines. |
| 18 | + */ |
| 19 | +add_action( 'admin_init', __NAMESPACE__ . '\run_admin_bulk_request' ); |
| 20 | +add_action( 'admin_notices', __NAMESPACE__ . '\admin_bulk_action_notice' ); |
| 21 | +add_filter( 'removable_query_args', __NAMESPACE__ . '\admin_removable_args' ); |
| 22 | + |
| 23 | +/** |
| 24 | + * Check for the bulk request coming from the admin. |
| 25 | + * |
| 26 | + * @return void |
| 27 | + */ |
| 28 | +function run_admin_bulk_request() { |
| 29 | + |
| 30 | + // Confirm we requested this action. |
| 31 | + $confirm_action = filter_input( INPUT_GET, 'ip-scrub-run-bulk', FILTER_SANITIZE_SPECIAL_CHARS ); // phpcs:ignore -- the nonce check is happening after this. |
| 32 | + |
| 33 | + // Make sure it is what we want. |
| 34 | + if ( empty( $confirm_action ) || 'yes' !== $confirm_action ) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + // Make sure we have a nonce. |
| 39 | + $confirm_nonce = filter_input( INPUT_GET, 'ip-scrub-nonce', FILTER_SANITIZE_SPECIAL_CHARS ); // phpcs:ignore -- the nonce check is happening after this. |
| 40 | + |
| 41 | + // Handle the nonce check. |
| 42 | + if ( empty( $confirm_nonce ) || ! wp_verify_nonce( $confirm_nonce, 'ip_scrub_bulk' ) ) { |
| 43 | + |
| 44 | + // Let them know they had a failure. |
| 45 | + wp_die( esc_html__( 'There was an error validating the nonce.', 'scrub-comment-author-ip' ), esc_html__( 'Scrub Comment IP Bulk Action', 'scrub-comment-author-ip' ), [ 'back_link' => true ] ); |
| 46 | + } |
| 47 | + |
| 48 | + // Now get the IDs of the comments we have. |
| 49 | + $fetch_comments = Database\get_ids_for_update(); |
| 50 | + |
| 51 | + // If none exist, say so. |
| 52 | + if ( empty( $fetch_comments ) ) { |
| 53 | + |
| 54 | + // Now set my redirect link. |
| 55 | + $redirect_link = Helpers\fetch_settings_url( ['ip-scrub-bulk-error' => 'no-comments', 'ip-scrub-bulk-success' => 'no'] ); |
| 56 | + |
| 57 | + // Do the redirect. |
| 58 | + wp_safe_redirect( $redirect_link ); |
| 59 | + exit; |
| 60 | + } |
| 61 | + |
| 62 | + // Handle the WP_Error return on it's own. |
| 63 | + if ( is_wp_error( $fetch_comments ) ) { |
| 64 | + |
| 65 | + // Now set my redirect link. |
| 66 | + $redirect_link = Helpers\fetch_settings_url( ['ip-scrub-bulk-error' => 'query-error', 'ip-scrub-bulk-success' => 'no'] ); |
| 67 | + |
| 68 | + // Do the redirect. |
| 69 | + wp_safe_redirect( $redirect_link ); |
| 70 | + exit; |
| 71 | + } |
| 72 | + |
| 73 | + // Attempt the update. |
| 74 | + $attempt_update = Database\replace_batch_comment_ips( $fetch_comments ); |
| 75 | + |
| 76 | + // Bail if the update did. |
| 77 | + if ( empty( $attempt_update ) || is_wp_error( $attempt_update ) ) { |
| 78 | + |
| 79 | + // Now set my redirect link. |
| 80 | + $redirect_link = Helpers\fetch_settings_url( ['ip-scrub-bulk-error' => 'update-error', 'ip-scrub-bulk-success' => 'no'] ); |
| 81 | + |
| 82 | + // Do the redirect. |
| 83 | + wp_safe_redirect( $redirect_link ); |
| 84 | + exit; |
| 85 | + } |
| 86 | + |
| 87 | + // Now set my redirect link. |
| 88 | + $redirect_link = Helpers\fetch_settings_url( ['ip-scrub-bulk-count' => count( $fetch_comments ), 'ip-scrub-bulk-success' => 'yes'] ); |
| 89 | + |
| 90 | + // Do the redirect. |
| 91 | + wp_safe_redirect( $redirect_link ); |
| 92 | + exit; |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Check for the result of bulk action. |
| 97 | + * |
| 98 | + * @return void |
| 99 | + */ |
| 100 | +function admin_bulk_action_notice() { |
| 101 | + |
| 102 | + // Confirm we requested this action. |
| 103 | + $confirm_result = filter_input( INPUT_GET, 'ip-scrub-bulk-success', FILTER_SANITIZE_SPECIAL_CHARS ); // phpcs:ignore -- no need for a nonce check. |
| 104 | + |
| 105 | + // Make sure it is what we want. |
| 106 | + if ( empty( $confirm_result ) ) { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + // Handle the success first. |
| 111 | + if ( 'yes' === $confirm_result ) { |
| 112 | + |
| 113 | + // Set the counts. |
| 114 | + $set_counts = filter_input( INPUT_GET, 'ip-scrub-bulk-count', FILTER_SANITIZE_NUMBER_INT ); |
| 115 | + |
| 116 | + // Set our notice text. |
| 117 | + $set_notice = sprintf( _n( 'Success! %d comment was updated.', 'Success! %d comments were updated.', absint( $set_counts ), 'scrub-comment-author-ip' ), absint( $set_counts ) ); |
| 118 | + |
| 119 | + // Set the wrapper around it. |
| 120 | + echo '<div class="notice notice-success is-dismissible">'; |
| 121 | + |
| 122 | + // Display the actual message. |
| 123 | + echo '<p><strong>' . wp_kses_post( $set_notice ) . '</strong></p>'; |
| 124 | + |
| 125 | + // Close the wrapper. |
| 126 | + echo '</div>'; |
| 127 | + |
| 128 | + // And be done. |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + // Handle the errors now. |
| 133 | + $set_error = filter_input( INPUT_GET, 'ip-scrub-bulk-error', FILTER_SANITIZE_SPECIAL_CHARS ); |
| 134 | + |
| 135 | + // If we have no comments, show a warning. |
| 136 | + if ( 'no-comments' === $set_error ) { |
| 137 | + |
| 138 | + // Set the notice text. |
| 139 | + $set_notice = __( 'There are no comments requiring update at this time.', 'scrub-comment-author-ip' ); |
| 140 | + |
| 141 | + // Set the wrapper around it. |
| 142 | + echo '<div class="notice notice-warning is-dismissible">'; |
| 143 | + |
| 144 | + // Display the actual message. |
| 145 | + echo '<p><strong>' . wp_kses_post( $set_notice ) . '</strong></p>'; |
| 146 | + |
| 147 | + // Close the wrapper. |
| 148 | + echo '</div>'; |
| 149 | + |
| 150 | + // And finish. |
| 151 | + return; |
| 152 | + } |
| 153 | + |
| 154 | + // Handle the rest of the possible error messages. |
| 155 | + switch ( $set_error ) { |
| 156 | + |
| 157 | + case 'query-error' : |
| 158 | + $set_notice = __( 'There was an error attempting to retrieve the comments. Please check your error logs.', 'scrub-comment-author-ip' ); |
| 159 | + break; |
| 160 | + |
| 161 | + case 'update-error' : |
| 162 | + $set_notice = __( 'There was an error attempting to update the comments. Please check your error logs.', 'scrub-comment-author-ip' ); |
| 163 | + break; |
| 164 | + |
| 165 | + default : |
| 166 | + $set_notice = __( 'There was an unknown error. Please check your error logs.', 'scrub-comment-author-ip' ); |
| 167 | + break; |
| 168 | + } |
| 169 | + |
| 170 | + // Set the wrapper around it. |
| 171 | + echo '<div class="notice notice-error is-dismissible">'; |
| 172 | + |
| 173 | + // Display the actual message. |
| 174 | + echo '<p><strong>' . wp_kses_post( $set_notice ) . '</strong></p>'; |
| 175 | + |
| 176 | + // Close the wrapper. |
| 177 | + echo '</div>'; |
| 178 | + |
| 179 | + // Nothing left to display. |
| 180 | +} |
| 181 | + |
| 182 | +/** |
| 183 | + * Add our custom strings to the vars. |
| 184 | + * |
| 185 | + * @param array $args The existing array of args. |
| 186 | + * |
| 187 | + * @return array $args The modified array of args. |
| 188 | + */ |
| 189 | +function admin_removable_args( $args ) { |
| 190 | + |
| 191 | + // Set an array of the args we wanna exclude. |
| 192 | + $remove = [ |
| 193 | + 'ip-scrub-bulk-error', |
| 194 | + 'ip-scrub-bulk-count', |
| 195 | + 'ip-scrub-bulk-success', |
| 196 | + ]; |
| 197 | + |
| 198 | + // Include my new args and return. |
| 199 | + return wp_parse_args( $remove, $args ); |
| 200 | +} |
0 commit comments