= $three_months_ago; } ); // Sort errors by timestamp DESC (newest first). usort( $recent_errors, function ( $a, $b ) { return $b['timestamp'] <=> $a['timestamp']; } ); return new WP_REST_Response( array_values( $recent_errors ), 200 ); } /** * POST /error * * Append a new error log to the stored option. * * @param WP_REST_Request $request The REST request containing the error message and metadata. * * @return WP_REST_Response A REST response indicating success or failure. */ function rwpai_post_error_log( WP_REST_Request $request ) { // Rate limit check to prevent abuse. $rate_limit = rwpai_rate_limit_check( 'error_log', 40, MINUTE_IN_SECONDS ); if ( is_wp_error( $rate_limit ) ) { return new WP_REST_Response( array( 'error' => 'Rate limit exceeded. Please try again later.' ), 429 ); } $body = $request->get_json_params(); if ( empty( $body['message'] ) ) { return new WP_REST_Response( array( 'error' => 'Message field is required.' ), 400 ); } // do strlimit on message length to avoid huge logs. $body['message'] = mb_strimwidth( $body['message'], 0, 2000, '...' ); $errors = get_option( 'rwpai_errors', array() ); if ( ! is_array( $errors ) ) { $errors = array(); } $body['timestamp'] = time(); // Create the new log entry. $new_log = $body; $errors[] = $new_log; // Optional: Keep the log array from growing too large (e.g., max 1000 entries). if ( count( $errors ) > 1000 ) { $errors = array_slice( $errors, -1000 ); // Keep last 1000 only. } update_option( 'rwpai_errors', $errors, false ); return new WP_REST_Response( array( 'success' => true, 'data' => $new_log, ), 201 ); } /** * DELETE /error * * Clear all stored error logs. * * @return WP_REST_Response A REST response confirming that logs were cleared. */ function rwpai_clean_error_logs() { update_option( 'rwpai_errors', array(), false ); return new WP_REST_Response( array( 'success' => true, ), 201 ); }