PluginProbe
Code Profiler – WordPress Performance Profiling and Debugging Made Easy / trunk
Code Profiler – WordPress Performance Profiling and Debugging Made Easy vtrunk
1.9.5 1.9.4 1.9.3 trunk 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.6 1.6.1 1.6.10 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 All 40 releases
code-profiler / lib / class-logs.php

class-logs.php in Code Profiler – WordPress Performance Profiling and Debugging Made Easy trunk, at lib/class-logs.php

97 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 +=====================================================================+
4 | ____ _ ____ __ _ _ |
5 | / ___|___ __| | ___ | _ \ _ __ ___ / _(_) | ___ _ __ |
6 | | | / _ \ / _` |/ _ \ | |_) | '__/ _ \| |_| | |/ _ \ '__| |
7 | | |__| (_) | (_| | __/ | __/| | | (_) | _| | | __/ | |
8 | \____\___/ \__,_|\___| |_| |_| \___/|_| |_|_|\___|_| |
9 | |
10 | (c) Jerome Bruandet ~ https://nintechnet.com/codeprofiler/ |
11 +=====================================================================+
12 */
13
14 if (! defined( 'ABSPATH' ) ) {
15 die( 'Forbidden' );
16 }
17
18 // =====================================================================
19
20 class CodeProfiler_Logs {
21
22
23 private static $HTTP_log_reg = '^last_request\.\d+?\.\d+?\.php$';
24
25 /**
26 * Retrieve the last HTTP response log, if any.
27 */
28 public static function get_HTTP_log() {
29 /**
30 * E.g., "last_request.1727412303.5681.php".
31 */
32 $file = code_profiler_glob( CODE_PROFILER_UPLOAD_DIR, self::$HTTP_log_reg, true );
33
34 if (! empty( $file[0] ) ) {
35
36 $log = file( $file[0] );
37 if (! empty( $log[0] ) && ! empty( $log[1] ) &&
38 ! empty( $log[2] ) && ! empty( $log[3] )
39 ) {
40
41 $page = json_decode( $log[0], true );
42 $payload = json_decode( $log[1], true );
43 $headers = json_decode( $log[2], true );
44 $body = json_decode( $log[3], true );
45
46 $data = "==================================================\n".
47 __('Requested page:', 'code-profiler') ."\n\n".
48 "$page\n";
49
50 if ( $payload != 'x') {
51 $data .= "==================================================\n".
52 __('Post payload:', 'code-profiler') ."\n\n".
53 print_r( $payload, true ) ."\n";
54 }
55
56 $data .= "==================================================\n".
57 __('Response headers:', 'code-profiler') ."\n\n".
58 "$headers\n".
59 "==================================================\n".
60 __('Response body:', 'code-profiler') ."\n\n".
61 "$body\n";
62
63 return $data;
64 }
65 }
66 return false;
67 }
68
69
70 /**
71 * Save the last HTTP response log to disk.
72 */
73 public static function save_HTTP_log( $page, $payload, $response_headers, $body ) {
74
75 $file = code_profiler_glob( CODE_PROFILER_UPLOAD_DIR, self::$HTTP_log_reg, true );
76
77 if (! empty( $file[0] ) ) {
78 $fh = fopen( $file[0], 'w');
79
80 } else {
81 $fh = fopen(
82 CODE_PROFILER_UPLOAD_DIR .'/last_request.'. microtime( true ) .'.php', 'w'
83 );
84 }
85
86 fwrite( $fh,
87 json_encode( $page ) ."\n". json_encode( $payload ) ."\n".
88 json_encode( $response_headers ) ."\n". json_encode( $body )
89 );
90
91 fclose( $fh );
92 }
93
94 }
95 // =====================================================================
96 // EOF
97