|
| 1 | +<?php |
| 2 | + |
| 3 | +// If this file is called directly, abort. |
| 4 | +if ( ! defined( 'WPINC' ) ) { |
| 5 | + die; |
| 6 | +} |
| 7 | + |
| 8 | +/** |
| 9 | + * Handles AJAX request to refresh benchmark results. |
| 10 | + * |
| 11 | + * This function verifies the AJAX nonce, executes the benchmarks, |
| 12 | + * and returns the results as an HTML table. |
| 13 | + * |
| 14 | + * @since 1.0.0 |
| 15 | + * @return void |
| 16 | + */ |
| 17 | +function benchpress_ajax_refresh() { |
| 18 | + check_ajax_referer( 'benchpress_nonce' ); |
| 19 | + |
| 20 | + // Run benchmarks and return HTML table. |
| 21 | + ob_start(); |
| 22 | + $table = new BenchPress_Table(); |
| 23 | + $table->prepare_items(); |
| 24 | + $table->display(); |
| 25 | + $output = ob_get_clean(); |
| 26 | + |
| 27 | + wp_send_json_success( [ 'html' => $output ] ); |
| 28 | +} |
| 29 | +add_action( 'wp_ajax_benchpress_refresh', 'benchpress_ajax_refresh' ); |
| 30 | + |
| 31 | +/** |
| 32 | + * Handles AJAX request to create a snapshot of benchmark results. |
| 33 | + * |
| 34 | + * This function verifies the AJAX nonce, runs benchmarks, saves the results |
| 35 | + * to the database, and returns a success message upon completion. |
| 36 | + * |
| 37 | + * @global wpdb $wpdb WordPress database abstraction object. |
| 38 | + * |
| 39 | + * @since 1.0.0 |
| 40 | + * @return void |
| 41 | + */ |
| 42 | +function benchpress_ajax_snapshot() { |
| 43 | + check_ajax_referer( 'benchpress_nonce' ); |
| 44 | + |
| 45 | + // Run benchmarks and save to database. |
| 46 | + global $wpdb; |
| 47 | + $table_name = $wpdb->prefix . 'benchpress_snapshots'; |
| 48 | + |
| 49 | + // Run the benchmarks |
| 50 | + $benchmarks = benchpress_run_all_benchmarks(); |
| 51 | + $snapshot_data = json_encode( $benchmarks ); |
| 52 | + |
| 53 | + // Insert snapshot into database. |
| 54 | + $wpdb->insert( |
| 55 | + $table_name, |
| 56 | + [ |
| 57 | + 'snapshot_data' => $snapshot_data, |
| 58 | + 'created_at' => current_time( 'mysql' ), |
| 59 | + ] |
| 60 | + ); |
| 61 | + |
| 62 | + wp_send_json_success( [ 'message' => esc_html__( 'Snapshot saved!', 'benchpress' ) ] ); |
| 63 | +} |
| 64 | +add_action( 'wp_ajax_benchpress_snapshot', 'benchpress_ajax_snapshot' ); |
| 65 | + |
| 66 | +/** |
| 67 | + * Handles AJAX request to delete a specific snapshot. |
| 68 | + * |
| 69 | + * This function verifies the AJAX nonce, deletes the specified snapshot |
| 70 | + * from the database, and returns a success or error message based on the result. |
| 71 | + * |
| 72 | + * @global wpdb $wpdb WordPress database abstraction object. |
| 73 | + * |
| 74 | + * @since 1.0.0 |
| 75 | + * @return void |
| 76 | + */ |
| 77 | +function benchpress_delete_snapshot() { |
| 78 | + check_ajax_referer( 'benchpress_nonce' ); |
| 79 | + |
| 80 | + if ( isset( $_POST['snapshot_id'] ) ) { |
| 81 | + global $wpdb; |
| 82 | + $table_name = $wpdb->prefix . 'benchpress_snapshots'; |
| 83 | + $snapshot_id = intval( $_POST['snapshot_id'] ); |
| 84 | + |
| 85 | + // Delete snapshot by ID. |
| 86 | + $deleted = $wpdb->delete( $table_name, [ 'id' => $snapshot_id ], [ '%d' ] ); |
| 87 | + |
| 88 | + if ( $deleted ) { |
| 89 | + wp_send_json_success( [ 'message' => esc_html__( 'Snapshot deleted successfully.', 'benchpress' ) ] ); |
| 90 | + } else { |
| 91 | + wp_send_json_error( [ 'message' => esc_html__( 'Failed to delete snapshot.', 'benchpress' ) ] ); |
| 92 | + } |
| 93 | + } else { |
| 94 | + wp_send_json_error( [ 'message' => esc_html__( 'Invalid snapshot ID.', 'benchpress' ) ] ); |
| 95 | + } |
| 96 | +} |
| 97 | +add_action( 'wp_ajax_benchpress_delete_snapshot', 'benchpress_delete_snapshot' ); |
| 98 | + |
| 99 | +/** |
| 100 | + * Handles AJAX request to clear all snapshots. |
| 101 | + * |
| 102 | + * This function verifies the AJAX nonce, removes all snapshots from |
| 103 | + * the database, and returns a success or error message based on the result. |
| 104 | + * |
| 105 | + * @global wpdb $wpdb WordPress database abstraction object. |
| 106 | + * |
| 107 | + * @since 1.0.0 |
| 108 | + * @return void |
| 109 | + */ |
| 110 | +function benchpress_clear_all_snapshots() { |
| 111 | + check_ajax_referer( 'benchpress_nonce' ); |
| 112 | + |
| 113 | + global $wpdb; |
| 114 | + $table_name = $wpdb->prefix . 'benchpress_snapshots'; |
| 115 | + |
| 116 | + // Delete all rows from the snapshots table. |
| 117 | + $deleted = $wpdb->query( "TRUNCATE TABLE $table_name" ); |
| 118 | + |
| 119 | + if ( $deleted !== false ) { |
| 120 | + wp_send_json_success( [ 'message' => esc_html__( 'All snapshots deleted successfully.', 'benchpress' ) ] ); |
| 121 | + } else { |
| 122 | + wp_send_json_error( [ 'message' => esc_html__( 'Failed to delete snapshots.', 'benchpress' ) ] ); |
| 123 | + } |
| 124 | +} |
| 125 | +add_action( 'wp_ajax_benchpress_clear_all_snapshots', 'benchpress_clear_all_snapshots' ); |
| 126 | + |
| 127 | +/** |
| 128 | + * Handles AJAX request to download snapshots as a CSV file. |
| 129 | + * |
| 130 | + * This function verifies the AJAX nonce, retrieves all snapshots from |
| 131 | + * the database, and outputs them as a CSV file for download. |
| 132 | + * |
| 133 | + * @global wpdb $wpdb WordPress database abstraction object. |
| 134 | + * |
| 135 | + * @since 1.0.0 |
| 136 | + * @return void |
| 137 | + */ |
| 138 | +function benchpress_download_snapshots() { |
| 139 | + check_ajax_referer( 'benchpress_nonce' ); |
| 140 | + |
| 141 | + global $wpdb; |
| 142 | + $table_name = $wpdb->prefix . 'benchpress_snapshots'; |
| 143 | + |
| 144 | + // Fetch all snapshots. |
| 145 | + $snapshots = $wpdb->get_results( "SELECT * FROM $table_name ORDER BY created_at DESC", ARRAY_A ); |
| 146 | + |
| 147 | + if ( empty( $snapshots ) ) { |
| 148 | + wp_die( esc_html__( 'No snapshots available to download.', 'benchpress' ) ); |
| 149 | + } |
| 150 | + |
| 151 | + // Set headers to initiate file download. |
| 152 | + header( 'Content-Type: text/csv; charset=utf-8' ); |
| 153 | + header( 'Content-Disposition: attachment; filename=' . sanitize_title( get_bloginfo( 'name' ) ) . '-benchpress-' . date( 'Y-m-d_H-i-s' ) . '.csv' ); |
| 154 | + |
| 155 | + // Open output stream for CSV. |
| 156 | + $output = fopen( 'php://output', 'w' ); |
| 157 | + |
| 158 | + // Add CSV headers. |
| 159 | + fputcsv( $output, [ 'ID', 'Date', 'Benchmark Name', 'Execution Time', 'Description' ] ); |
| 160 | + |
| 161 | + // Loop through each snapshot and format data. |
| 162 | + foreach ( $snapshots as $snapshot ) { |
| 163 | + $snapshot_data = json_decode( $snapshot['snapshot_data'], true ); |
| 164 | + foreach ( $snapshot_data as $benchmark ) { |
| 165 | + fputcsv( $output, [ |
| 166 | + $snapshot['id'], |
| 167 | + $snapshot['created_at'], |
| 168 | + $benchmark['name'], |
| 169 | + $benchmark['execution_time'], |
| 170 | + $benchmark['description'], |
| 171 | + ] ); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + fclose( $output ); |
| 176 | + exit; |
| 177 | +} |
| 178 | +add_action( 'wp_ajax_benchpress_download_snapshots', 'benchpress_download_snapshots' ); |
0 commit comments