st-admin-outgoing-req-page.php
1 month ago
st-admin-system-page.php
1 year ago
st-admin-troubleshooting-page.php
3 months ago
st-admin-outgoing-req-page.php
545 lines
| 1 | <?php |
| 2 | /** |
| 3 | * SureTriggers Outgoing Requests Page. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @package SureTriggers |
| 7 | * @author BSF <username@example.com> |
| 8 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 9 | * @link https://www.brainstormforce.com/ |
| 10 | * @since 1.0.0 |
| 11 | */ |
| 12 | |
| 13 | use SureTriggers\Controllers\WebhookRequestsController; |
| 14 | global $wpdb; |
| 15 | |
| 16 | // Handle disable-logging toggle. |
| 17 | if ( |
| 18 | isset( $_POST['suretriggers_logging_nonce'] ) && |
| 19 | wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['suretriggers_logging_nonce'] ) ), 'suretriggers_logging_nonce_action' ) && |
| 20 | current_user_can( 'manage_options' ) |
| 21 | ) { |
| 22 | $disable = isset( $_POST['st_disable_logging'] ) && '1' === sanitize_text_field( wp_unslash( $_POST['st_disable_logging'] ) ); |
| 23 | update_option( 'suretriggers_disable_request_logging', $disable ); |
| 24 | } |
| 25 | |
| 26 | $logging_disabled = (bool) get_option( 'suretriggers_disable_request_logging', false ); |
| 27 | $tab_url = add_query_arg( |
| 28 | [ |
| 29 | 'tab' => 'st_outgoing_requests', |
| 30 | '_wpnonce' => wp_create_nonce( 'suretriggers_tab_nonce' ), |
| 31 | ], |
| 32 | admin_url( 'admin.php?page=suretriggers-status' ) |
| 33 | ); |
| 34 | ?> |
| 35 | <div style="margin: 16px 0; padding: 16px 20px; background: #fff; border: 1px solid #c3c4c7; border-left: 4px solid <?php echo esc_attr( $logging_disabled ? '#d63638' : '#00a32a' ); ?>; border-radius: 2px; display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 12px;"> |
| 36 | <div> |
| 37 | <strong style="font-size: 14px;"><?php esc_html_e( 'Outgoing Request Logging', 'suretriggers' ); ?></strong> |
| 38 | <p style="margin: 4px 0 0; color: #50575e; font-size: 13px;"> |
| 39 | <?php if ( $logging_disabled ) : ?> |
| 40 | <?php esc_html_e( 'Logging is currently disabled. No new requests are being stored. Failed requests cannot be retried automatically.', 'suretriggers' ); ?> |
| 41 | <?php else : ?> |
| 42 | <?php esc_html_e( 'Logging is enabled. Every outgoing webhook request is stored in the database for retry and history.', 'suretriggers' ); ?> |
| 43 | <?php endif; ?> |
| 44 | </p> |
| 45 | </div> |
| 46 | <form method="post" action="<?php echo esc_url( $tab_url ); ?>"> |
| 47 | <?php wp_nonce_field( 'suretriggers_logging_nonce_action', 'suretriggers_logging_nonce' ); ?> |
| 48 | <?php if ( $logging_disabled ) : ?> |
| 49 | <input type="hidden" name="st_disable_logging" value="0" /> |
| 50 | <button type="submit" class="button button-primary"><?php esc_html_e( 'Enable Logging', 'suretriggers' ); ?></button> |
| 51 | <?php else : ?> |
| 52 | <input type="hidden" name="st_disable_logging" value="1" /> |
| 53 | <button type="submit" class="button button-secondary" style="border-color: #d63638; color: #d63638;"><?php esc_html_e( 'Disable Logging', 'suretriggers' ); ?></button> |
| 54 | <?php endif; ?> |
| 55 | </form> |
| 56 | </div> |
| 57 | <?php |
| 58 | |
| 59 | if ( ! class_exists( 'WP_List_Table' ) ) { |
| 60 | require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * SureTriggersWebhookRequestsTable - List table for Webhook requests. |
| 65 | * |
| 66 | * @category SureTriggersWebhookRequestsTable |
| 67 | * @package SureTriggers |
| 68 | * @author BSF <username@example.com> |
| 69 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 70 | * @link https://www.brainstormforce.com/ |
| 71 | * @since 1.0.0 |
| 72 | * |
| 73 | * @psalm-suppress UndefinedTrait |
| 74 | */ |
| 75 | class SureTriggersWebhookRequestsTable extends WP_List_Table { |
| 76 | |
| 77 | /** |
| 78 | * Webhook Requests List Table name. |
| 79 | * |
| 80 | * @var string |
| 81 | */ |
| 82 | private $table_name; |
| 83 | |
| 84 | /** |
| 85 | * Initialise data. |
| 86 | * |
| 87 | * @param string $table_name Table Name. |
| 88 | */ |
| 89 | public function __construct( $table_name ) { |
| 90 | parent::__construct( |
| 91 | [ |
| 92 | 'singular' => 'webhook_request', |
| 93 | 'plural' => 'webhook_requests', |
| 94 | 'ajax' => false, |
| 95 | ] |
| 96 | ); |
| 97 | |
| 98 | $this->table_name = $table_name; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Table Classes. |
| 103 | * |
| 104 | * @return array |
| 105 | */ |
| 106 | protected function table_classes() { |
| 107 | return [ 'wp-list-table', 'widefat', 'striped' ]; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Table Display. |
| 112 | * |
| 113 | * @return void |
| 114 | */ |
| 115 | public function display() { |
| 116 | $this->display_tablenav( 'top' ); |
| 117 | ?> |
| 118 | <table class="<?php echo esc_attr( implode( ' ', $this->table_classes() ) ); ?>"> |
| 119 | <thead> |
| 120 | <?php $this->print_column_headers(); ?> |
| 121 | </thead> |
| 122 | <tbody id="the-list" data-wp-lists="list:<?php echo esc_attr( $this->_args['singular'] ); ?>"> |
| 123 | <?php $this->display_rows_or_placeholder(); ?> |
| 124 | </tbody> |
| 125 | <tfoot> |
| 126 | <?php $this->print_column_headers( false ); ?> |
| 127 | </tfoot> |
| 128 | </table> |
| 129 | <?php |
| 130 | $this->display_tablenav( 'bottom' ); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Get Columns. |
| 135 | * |
| 136 | * @return array |
| 137 | */ |
| 138 | public function get_columns() { |
| 139 | return [ |
| 140 | 'cb' => '<input type="checkbox" />', |
| 141 | 'id' => __( 'ID', 'suretriggers' ), |
| 142 | 'response_code' => __( 'Response Code', 'suretriggers' ), |
| 143 | 'status' => __( 'Status', 'suretriggers' ), |
| 144 | 'trigger_event' => __( 'Trigger Event', 'suretriggers' ), |
| 145 | 'error_info' => __( 'Error Info', 'suretriggers' ), |
| 146 | 'created_at' => __( 'Created At', 'suretriggers' ), |
| 147 | 'request_actions' => __( 'Actions', 'suretriggers' ), |
| 148 | ]; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Get Sortable Columns. |
| 153 | * |
| 154 | * @return array |
| 155 | */ |
| 156 | public function get_sortable_columns() { |
| 157 | return [ |
| 158 | 'id' => [ 'id', true ], |
| 159 | 'created_at' => [ 'created_at', false ], |
| 160 | ]; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Checkbox column for bulk actions. |
| 165 | * |
| 166 | * @param array $item Item. |
| 167 | * |
| 168 | * @return string |
| 169 | */ |
| 170 | public function column_cb( $item ) { |
| 171 | $id = absint( $item['id'] ); |
| 172 | return '<input id="cb-select-' . $id . '" type="checkbox" name="st_bulk_ids[]" value="' . $id . '" />' |
| 173 | . '<label for="cb-select-' . $id . '"><span class="screen-reader-text">' |
| 174 | . sprintf( esc_html__( 'Select request %s', 'suretriggers' ), $id ) |
| 175 | . '</span></label>'; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Get bulk actions. |
| 180 | * |
| 181 | * @return array |
| 182 | */ |
| 183 | protected function get_bulk_actions() { |
| 184 | return [ |
| 185 | 'bulk_retry' => __( 'Retry Selected', 'suretriggers' ), |
| 186 | 'bulk_delete' => __( 'Delete Selected', 'suretriggers' ), |
| 187 | ]; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Column actions — Retry/Rerun and View Response buttons. |
| 192 | * |
| 193 | * @param array $item Item. |
| 194 | * |
| 195 | * @return string |
| 196 | */ |
| 197 | public function column_request_actions( $item ) { |
| 198 | $id = absint( $item['id'] ); |
| 199 | $output = ''; |
| 200 | |
| 201 | if ( 'failed' === $item['status'] ) { |
| 202 | $output .= '<button type="button" class="button button-primary st-retry-btn" onclick="stRetryRequest(' . $id . ')">' . esc_html__( 'Retry', 'suretriggers' ) . '</button> '; |
| 203 | } elseif ( 'success' === $item['status'] ) { |
| 204 | $output .= '<button type="button" class="button st-retry-btn" onclick="stRetryRequest(' . $id . ')">' . esc_html__( 'Rerun', 'suretriggers' ) . '</button> '; |
| 205 | } |
| 206 | |
| 207 | $output .= '<button type="button" class="button st-view-response-btn" onclick="stViewResponse(' . $id . ')">' . esc_html__( 'View Response', 'suretriggers' ) . '</button>'; |
| 208 | |
| 209 | return $output; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Column trigger event. |
| 214 | * |
| 215 | * @param array $item Item. |
| 216 | * |
| 217 | * @return mixed|string |
| 218 | */ |
| 219 | public function column_trigger_event( $item ) { |
| 220 | $data = $item['request_data']; |
| 221 | $data = json_decode( $data, true ); |
| 222 | if ( is_array( $data ) && isset( $data['body']['trigger'] ) ) { |
| 223 | return $data['body']['trigger']; |
| 224 | } |
| 225 | return ''; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Prepare Items. |
| 230 | * |
| 231 | * @return void |
| 232 | */ |
| 233 | public function prepare_items() { |
| 234 | global $wpdb; |
| 235 | |
| 236 | $columns = $this->get_columns(); |
| 237 | $sortable = $this->get_sortable_columns(); |
| 238 | |
| 239 | $this->_column_headers = [ $columns, [], $sortable ]; |
| 240 | |
| 241 | if ( isset( $_POST['suretriggers_requests_nonce'] ) |
| 242 | && wp_verify_nonce( sanitize_text_field( $_POST['suretriggers_requests_nonce'] ), 'suretriggers_requests_nonce_action' ) |
| 243 | && current_user_can( 'manage_options' ) |
| 244 | ) { |
| 245 | // Handle single retry/rerun. |
| 246 | if ( ! empty( $_POST['retry_st_request'] ) && isset( $_POST['st_retry_id'] ) ) { |
| 247 | $id = absint( $_POST['st_retry_id'] ); |
| 248 | if ( $id ) { |
| 249 | WebhookRequestsController::suretriggers_retry_trigger_request( $id ); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // Handle bulk actions. |
| 254 | $bulk_action = isset( $_POST['st_bulk_action'] ) ? sanitize_text_field( $_POST['st_bulk_action'] ) : ''; |
| 255 | if ( '' !== $bulk_action && isset( $_POST['st_bulk_ids'] ) ) { |
| 256 | $bulk_ids = array_map( 'absint', (array) $_POST['st_bulk_ids'] ); |
| 257 | $bulk_ids = array_filter( $bulk_ids ); |
| 258 | |
| 259 | if ( ! empty( $bulk_ids ) ) { |
| 260 | if ( 'bulk_retry' === $bulk_action ) { |
| 261 | foreach ( $bulk_ids as $bulk_id ) { |
| 262 | WebhookRequestsController::suretriggers_retry_trigger_request( $bulk_id ); |
| 263 | } |
| 264 | } elseif ( 'bulk_delete' === $bulk_action ) { |
| 265 | foreach ( $bulk_ids as $delete_id ) { |
| 266 | $wpdb->delete( |
| 267 | $this->table_name, |
| 268 | [ 'id' => $delete_id ], |
| 269 | [ '%d' ] |
| 270 | ); |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | $status_filter = isset( $_REQUEST['status_filter'] ) ? sanitize_text_field( $_REQUEST['status_filter'] ) : ''; |
| 278 | $date_from = isset( $_REQUEST['date_from'] ) ? sanitize_text_field( $_REQUEST['date_from'] ) : ''; |
| 279 | $date_to = isset( $_REQUEST['date_to'] ) ? sanitize_text_field( $_REQUEST['date_to'] ) : ''; |
| 280 | $allowed_orderby = [ 'id', 'response_code', 'status', 'error_info', 'created_at' ]; |
| 281 | $orderby_input = isset( $_REQUEST['orderby'] ) ? sanitize_text_field( $_REQUEST['orderby'] ) : 'id'; |
| 282 | $orderby = in_array( $orderby_input, $allowed_orderby, true ) ? $orderby_input : 'id'; |
| 283 | $order = isset( $_REQUEST['order'] ) && 'DESC' === strtoupper( sanitize_text_field( $_REQUEST['order'] ) ) ? 'DESC' : 'ASC'; |
| 284 | $per_page = $this->get_items_per_page( 'webhook_requests_per_page', 10 ); |
| 285 | $current_page = $this->get_pagenum(); |
| 286 | |
| 287 | $offset = ( $current_page - 1 ) * $per_page; |
| 288 | |
| 289 | // Build WHERE clause — pre-prepare each condition individually. |
| 290 | $where_prepared = '1=1'; |
| 291 | |
| 292 | if ( '' !== $status_filter ) { |
| 293 | $allowed_statuses = [ 'success', 'failed', 'pending' ]; |
| 294 | if ( in_array( $status_filter, $allowed_statuses, true ) ) { |
| 295 | $where_prepared .= $wpdb->prepare( ' AND status = %s', $status_filter ); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | if ( '' !== $date_from && preg_match( '/^\d{4}-\d{2}-\d{2}$/', $date_from ) ) { |
| 300 | $where_prepared .= $wpdb->prepare( ' AND created_at >= %s', $date_from . ' 00:00:00' ); |
| 301 | } |
| 302 | |
| 303 | if ( '' !== $date_to && preg_match( '/^\d{4}-\d{2}-\d{2}$/', $date_to ) ) { |
| 304 | $where_prepared .= $wpdb->prepare( ' AND created_at <= %s', $date_to . ' 23:59:59' ); |
| 305 | } |
| 306 | |
| 307 | $total_items = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}suretriggers_webhook_requests WHERE " . $where_prepared ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $where_prepared is built from individual $wpdb->prepare() calls above. |
| 308 | |
| 309 | $this->items = $wpdb->get_results( $wpdb->prepare( "SELECT id, response_code, request_data, status, error_info, created_at FROM {$wpdb->prefix}suretriggers_webhook_requests WHERE " . $where_prepared . ' ORDER BY ' . esc_sql( $orderby ) . ' ' . esc_sql( $order ) . ' LIMIT %d OFFSET %d', $per_page, $offset ), ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $where_prepared is built from individual $wpdb->prepare() calls above, $orderby/$order are allowlist-validated. |
| 310 | |
| 311 | $this->set_pagination_args( |
| 312 | [ |
| 313 | 'total_items' => $total_items, |
| 314 | 'per_page' => $per_page, |
| 315 | ] |
| 316 | ); |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Column Default. |
| 321 | * |
| 322 | * @param array $item Item. |
| 323 | * @param string $column_name Column Name. |
| 324 | * |
| 325 | * @return string |
| 326 | */ |
| 327 | public function column_default( $item, $column_name ) { |
| 328 | return isset( $item[ $column_name ] ) ? esc_html( $item[ $column_name ] ) : ''; |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Extra table navigation — filters. |
| 333 | * |
| 334 | * @param string $which Which. |
| 335 | * |
| 336 | * @return void |
| 337 | */ |
| 338 | public function extra_tablenav( $which ) { |
| 339 | if ( 'top' !== $which ) { |
| 340 | return; |
| 341 | } |
| 342 | |
| 343 | if ( isset( $_REQUEST['suretriggers_requests_nonce'] ) && ! wp_verify_nonce( sanitize_text_field( $_REQUEST['suretriggers_requests_nonce'] ), 'suretriggers_requests_nonce_action' ) ) { |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | $status_filter = isset( $_REQUEST['status_filter'] ) ? sanitize_text_field( $_REQUEST['status_filter'] ) : ''; |
| 348 | $date_from = isset( $_REQUEST['date_from'] ) ? sanitize_text_field( $_REQUEST['date_from'] ) : ''; |
| 349 | $date_to = isset( $_REQUEST['date_to'] ) ? sanitize_text_field( $_REQUEST['date_to'] ) : ''; |
| 350 | ?> |
| 351 | <div class="alignleft actions st-filters-row"> |
| 352 | <select name="status_filter"> |
| 353 | <option value=""><?php esc_html_e( 'All Requests', 'suretriggers' ); ?></option> |
| 354 | <option value="success" <?php selected( $status_filter, 'success' ); ?>> |
| 355 | <?php esc_html_e( 'Success Requests', 'suretriggers' ); ?> |
| 356 | </option> |
| 357 | <option value="failed" <?php selected( $status_filter, 'failed' ); ?>> |
| 358 | <?php esc_html_e( 'Failed Requests', 'suretriggers' ); ?> |
| 359 | </option> |
| 360 | <option value="pending" <?php selected( $status_filter, 'pending' ); ?>> |
| 361 | <?php esc_html_e( 'Pending Requests', 'suretriggers' ); ?> |
| 362 | </option> |
| 363 | </select> |
| 364 | |
| 365 | <label for="st-date-from" class="screen-reader-text"><?php esc_html_e( 'From date', 'suretriggers' ); ?></label> |
| 366 | <input type="date" id="st-date-from" name="date_from" value="<?php echo esc_attr( $date_from ); ?>" placeholder="<?php esc_attr_e( 'From', 'suretriggers' ); ?>" class="st-date-input" /> |
| 367 | |
| 368 | <label for="st-date-to" class="screen-reader-text"><?php esc_html_e( 'To date', 'suretriggers' ); ?></label> |
| 369 | <input type="date" id="st-date-to" name="date_to" value="<?php echo esc_attr( $date_to ); ?>" placeholder="<?php esc_attr_e( 'To', 'suretriggers' ); ?>" class="st-date-input" /> |
| 370 | |
| 371 | <input type="submit" name="suretriggers_filter_request" id="suretriggers_filter_request" class="button" value="<?php echo esc_attr__( 'Filter', 'suretriggers' ); ?>"> |
| 372 | </div> |
| 373 | <?php |
| 374 | } |
| 375 | |
| 376 | } |
| 377 | $table_name = WebhookRequestsController::get_table_name(); |
| 378 | $list_table = new SureTriggersWebhookRequestsTable( $table_name ); |
| 379 | ?> |
| 380 | <form id="suretriggers-requests-table-form" method="post"> |
| 381 | <input type="hidden" name="page" value="suretriggers-status" /> |
| 382 | <input type="hidden" name="tab" value="st_outgoing_requests" /> |
| 383 | <input type="hidden" name="st_retry_id" value=""> |
| 384 | <input type="hidden" name="retry_st_request" value=""> |
| 385 | <input type="hidden" name="st_bulk_action" value=""> |
| 386 | <input type="hidden" name="_wpnonce" value="<?php echo esc_attr( wp_create_nonce( 'suretriggers_tab_nonce' ) ); ?>" /> |
| 387 | <?php |
| 388 | wp_nonce_field( 'suretriggers_requests_nonce_action', 'suretriggers_requests_nonce' ); |
| 389 | if ( isset( $_REQUEST['suretriggers_requests_nonce'] ) && ! wp_verify_nonce( sanitize_text_field( $_REQUEST['suretriggers_requests_nonce'] ), 'suretriggers_requests_nonce_action' ) ) { |
| 390 | if ( isset( $_REQUEST['status_filter'] ) ) { |
| 391 | echo '<input type="hidden" name="status_filter" value="' . esc_attr( sanitize_text_field( $_REQUEST['status_filter'] ) ) . '">'; |
| 392 | } |
| 393 | } |
| 394 | $list_table->prepare_items(); |
| 395 | $list_table->display(); |
| 396 | echo '<div style="margin-top: 10px;"> |
| 397 | <p dir="auto" style="font-style: italic; color: #666; margin-inline-start: 55%;">'; |
| 398 | esc_html_e( 'Note: Successful outgoing requests will be automatically deleted after 30 days, while failed outgoing requests will be automatically deleted after 60 days.', 'suretriggers' ); |
| 399 | echo '</p> |
| 400 | </div>'; |
| 401 | ?> |
| 402 | </form> |
| 403 | |
| 404 | <!-- View Response Modal --> |
| 405 | <div id="st-response-modal" class="st-modal-overlay" style="display:none;"> |
| 406 | <div class="st-modal-content"> |
| 407 | <div class="st-modal-header"> |
| 408 | <h3><?php esc_html_e( 'Request & Response Details', 'suretriggers' ); ?></h3> |
| 409 | <button type="button" class="st-modal-close" aria-label="<?php esc_attr_e( 'Close', 'suretriggers' ); ?>">×</button> |
| 410 | </div> |
| 411 | <div class="st-modal-body"> |
| 412 | <div class="st-modal-section"> |
| 413 | <h4><?php esc_html_e( 'Status', 'suretriggers' ); ?></h4> |
| 414 | <p id="st-modal-status"></p> |
| 415 | </div> |
| 416 | <div class="st-modal-section"> |
| 417 | <h4><?php esc_html_e( 'Response Code', 'suretriggers' ); ?></h4> |
| 418 | <p id="st-modal-response-code"></p> |
| 419 | </div> |
| 420 | <div class="st-modal-section"> |
| 421 | <h4><?php esc_html_e( 'Error Info', 'suretriggers' ); ?></h4> |
| 422 | <p id="st-modal-error-info"></p> |
| 423 | </div> |
| 424 | <div class="st-modal-section"> |
| 425 | <h4><?php esc_html_e( 'Trigger Event', 'suretriggers' ); ?></h4> |
| 426 | <p id="st-modal-trigger-event"></p> |
| 427 | </div> |
| 428 | <div class="st-modal-section"> |
| 429 | <h4><?php esc_html_e( 'Created At', 'suretriggers' ); ?></h4> |
| 430 | <p id="st-modal-created-at"></p> |
| 431 | </div> |
| 432 | <div class="st-modal-section"> |
| 433 | <h4><?php esc_html_e( 'Request Data', 'suretriggers' ); ?></h4> |
| 434 | <pre id="st-modal-request-data" class="st-json-display"></pre> |
| 435 | </div> |
| 436 | </div> |
| 437 | </div> |
| 438 | </div> |
| 439 | |
| 440 | <?php |
| 441 | $modal_data = []; |
| 442 | if ( is_array( $list_table->items ) ) { |
| 443 | foreach ( $list_table->items as $row ) { |
| 444 | $request_data = json_decode( $row['request_data'], true ); |
| 445 | $trigger = ''; |
| 446 | if ( is_array( $request_data ) && isset( $request_data['body']['trigger'] ) ) { |
| 447 | $trigger = $request_data['body']['trigger']; |
| 448 | } |
| 449 | // Strip sensitive auth/header fields before exposing to page source. |
| 450 | $safe_request_data = $request_data; |
| 451 | if ( is_array( $safe_request_data ) ) { |
| 452 | unset( $safe_request_data['headers'] ); |
| 453 | } |
| 454 | |
| 455 | $modal_data[ $row['id'] ] = [ |
| 456 | 'status' => $row['status'], |
| 457 | 'response_code' => $row['response_code'], |
| 458 | 'error_info' => $row['error_info'], |
| 459 | 'trigger_event' => $trigger, |
| 460 | 'created_at' => $row['created_at'], |
| 461 | 'request_data' => $safe_request_data, |
| 462 | ]; |
| 463 | } |
| 464 | } |
| 465 | ?> |
| 466 | <script type="text/javascript"> |
| 467 | var stModalData = <?php echo wp_json_encode( $modal_data ); ?>; |
| 468 | var stForm = document.getElementById('suretriggers-requests-table-form'); |
| 469 | |
| 470 | function stRetryRequest(id) { |
| 471 | stForm.querySelector('input[name="st_retry_id"]').value = id; |
| 472 | stForm.querySelector('input[name="retry_st_request"]').value = '1'; |
| 473 | stForm.querySelector('input[name="st_bulk_action"]').value = ''; |
| 474 | stForm.submit(); |
| 475 | } |
| 476 | |
| 477 | function stViewResponse(id) { |
| 478 | var data = stModalData[id]; |
| 479 | if (!data) { return; } |
| 480 | document.getElementById('st-modal-status').textContent = data.status; |
| 481 | document.getElementById('st-modal-response-code').textContent = data.response_code; |
| 482 | document.getElementById('st-modal-error-info').textContent = data.error_info || '\u2014'; |
| 483 | document.getElementById('st-modal-trigger-event').textContent = data.trigger_event || '\u2014'; |
| 484 | document.getElementById('st-modal-created-at').textContent = data.created_at; |
| 485 | document.getElementById('st-modal-request-data').textContent = data.request_data ? JSON.stringify(data.request_data, null, 2) : '\u2014'; |
| 486 | document.getElementById('st-response-modal').style.display = ''; |
| 487 | } |
| 488 | |
| 489 | jQuery(document).ready(function($) { |
| 490 | $('#suretriggers-requests-table-form #_wpnonce').remove(); |
| 491 | |
| 492 | $(document).on('click', '#doaction, #doaction2', function(e) { |
| 493 | e.preventDefault(); |
| 494 | var action = $(this).prev('select').val(); |
| 495 | if ('-1' === action) { return; } |
| 496 | var checked = $('input[name="st_bulk_ids[]"]:checked'); |
| 497 | if (checked.length === 0) { |
| 498 | alert('<?php echo esc_js( __( 'Please select at least one request.', 'suretriggers' ) ); ?>'); |
| 499 | return; |
| 500 | } |
| 501 | $('input[name="st_bulk_action"]').val(action); |
| 502 | $('input[name="st_retry_id"]').val(''); |
| 503 | $('input[name="retry_st_request"]').val(''); |
| 504 | $('form#suretriggers-requests-table-form').submit(); |
| 505 | }); |
| 506 | |
| 507 | $('#suretriggers_filter_request').on('click', function(e) { |
| 508 | e.preventDefault(); |
| 509 | $('<input>').attr({ type: 'hidden', name: 'paged', value: '1' }).appendTo('#suretriggers-requests-table-form'); |
| 510 | $('input[name="st_bulk_action"]').val(''); |
| 511 | $('input[name="st_retry_id"]').val(''); |
| 512 | $('input[name="retry_st_request"]').val(''); |
| 513 | $('#suretriggers-requests-table-form').submit(); |
| 514 | }); |
| 515 | |
| 516 | $(document).on('click', '.tablenav-pages a', function(e) { |
| 517 | var paged = $(this).attr('href').match(/paged=(\d+)/); |
| 518 | if (paged && paged[1]) { |
| 519 | e.preventDefault(); |
| 520 | $('<input>').attr({ type: 'hidden', name: 'paged', value: paged[1] }).appendTo('#suretriggers-requests-table-form'); |
| 521 | var filterValue = $('select[name="status_filter"]').val(); |
| 522 | if (filterValue) { |
| 523 | $('<input>').attr({ type: 'hidden', name: 'status_filter', value: filterValue }).appendTo('#suretriggers-requests-table-form'); |
| 524 | } |
| 525 | var dateFrom = $('#st-date-from').val(); |
| 526 | if (dateFrom) { |
| 527 | $('<input>').attr({ type: 'hidden', name: 'date_from', value: dateFrom }).appendTo('#suretriggers-requests-table-form'); |
| 528 | } |
| 529 | var dateTo = $('#st-date-to').val(); |
| 530 | if (dateTo) { |
| 531 | $('<input>').attr({ type: 'hidden', name: 'date_to', value: dateTo }).appendTo('#suretriggers-requests-table-form'); |
| 532 | } |
| 533 | $('#suretriggers-requests-table-form').submit(); |
| 534 | } |
| 535 | }); |
| 536 | |
| 537 | $(document).on('click', '.st-modal-close, .st-modal-overlay', function(e) { |
| 538 | if (e.target === this) { $('#st-response-modal').hide(); } |
| 539 | }); |
| 540 | $(document).on('keydown', function(e) { |
| 541 | if (27 === e.keyCode) { $('#st-response-modal').hide(); } |
| 542 | }); |
| 543 | }); |
| 544 | </script> |
| 545 |