PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / trunk
MainWP Dashboard: Self-hosted WordPress Management for Agencies vtrunk
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / class / class-mainwp-execution-helper.php

class-mainwp-execution-helper.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies trunk, at class/class-mainwp-execution-helper.php

276 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Counter
4 *
5 * @package MainWP/Dashboard
6 * @version 4.5.1
7 */
8
9 namespace MainWP\Dashboard;
10
11 // Exit if accessed directly.
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 /**
17 * Class MainWP_Execution_Helper
18 *
19 * @package MainWP\Dashboard
20 */
21 class MainWP_Execution_Helper { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
22
23
24 /**
25 * Private variable to hold time start.
26 *
27 * @var int
28 */
29 private static $exec_start = null;
30
31 /**
32 * Static varibale to hold the instance.
33 *
34 * @var mixed Default null
35 */
36 public static $instance = null;
37
38 /**
39 * Static varibale to hold the remote call total time.
40 *
41 * @var mixed Default 0
42 */
43
44 public static $exec_stats = array();
45
46 /**
47 * Method instance()
48 *
49 * Returns new MainWP_Logger instance.
50 *
51 * @return self MainWP_Logger
52 *
53 * @uses \MainWP\Dashboard\MainWP_Logger
54 */
55 public static function instance() {
56 if ( null === static::$instance ) {
57 static::$instance = new self();
58 }
59 return static::$instance;
60 }
61
62 /**
63 * MainWP_Logger constructor.
64 *
65 * Run each time the class is called.
66 */
67 public function __construct() {
68 if ( null === static::$exec_start ) {
69 static::$exec_start = microtime( true );
70 }
71 }
72
73 /**
74 * Method init_exec_time().
75 *
76 * Init execution time start value.
77 */
78 public function init_exec_time() {
79 MainWP_Logger::instance()->init_execution_time();
80 return static::$exec_start;
81 }
82
83 /**
84 * Method get_exec_time().
85 *
86 * @deprecated Compatible.
87 *
88 * Get execution time start value.
89 *
90 * @param bool $log_exec To log execution time or not.
91 *
92 * @return int $sec Execution time.
93 */
94 public function get_exec_time( $log_exec = true ) {
95 if ( null === static::$exec_start ) {
96 static::$exec_start = microtime( true );
97 }
98
99 $sec = microtime( true ) - static::$exec_start; // seconds.
100
101 if ( $log_exec ) {
102 MainWP_Logger::instance()->log_action( 'execution time :: [value=' . round( $sec, 4 ) . '](seconds)', MainWP_Logger::EXECUTION_TIME_LOG_PRIORITY );
103 }
104
105 return $sec;
106 }
107
108
109 /**
110 * Method get_run_time().
111 *
112 * Get the execution time value.
113 *
114 * @param bool $ret_microseconds
115 *
116 * @since 5.5.
117 *
118 * @updated 6.0
119 *
120 * @return int|float execution time.
121 */
122 public static function get_run_time( $ret_microseconds = false ) {
123 if ( empty( static::$exec_start ) ) {
124 return 0;
125 }
126 $rtime = microtime( true ) - static::$exec_start; // seconds.
127 if ( $ret_microseconds ) {
128 return round( $rtime * 1000000, 4 );
129 }
130 return round( $rtime, 4 );
131 }
132
133
134 /**
135 * Method init_http_call_track.
136 *
137 * @return void
138 */
139 public static function init_http_call_track() {
140 add_action( 'http_api_debug', array( __CLASS__, 'http_call_track' ), 10, 5 );
141 }
142
143 /**
144 * Track HTTP API request/response timing.
145 *
146 * Hooked into `http_api_debug` to measure remote call duration.
147 *
148 * @param mixed $response HTTP response or WP_Error.
149 * @param string $context Execution context: 'request', 'response', 'transport', etc.
150 * @param string $class HTTP transport class name.
151 * @param array $args Request arguments passed to the HTTP API.
152 * @param string $url Request URL.
153 *
154 * @return void
155 */
156 public static function http_call_track( $response, $context, $class, $args, $url ) { //phpcs:ignore -- NOSONAR - not use some params.
157 $check_context = '';
158 if ( 'request' === $context ) {
159 $check_context = 'start_point';
160 } elseif ( 'response' === $context ) {
161 $check_context = 'end_point';
162 }
163 static::run_call_track( $check_context, $url, $args );
164 }
165
166
167 /**
168 * Method execute_call_track.
169 *
170 * @param mixed $context Curl request context.
171 * @param mixed $website Curl website.
172 * @param array $args Request args.
173 * @param string $request_id Request id.
174 * @param string $desc Exec msg.
175 *
176 * @return mixed Track id for 'request' or null.
177 */
178 public static function execute_call_track( $context, $website = false, $args = array(), $request_id = '', $desc = '' ) {
179 $url = '_na_url';
180 if ( is_string( $website ) ) {
181 $url = $website;
182 } elseif ( is_array( $website ) && isset( $website['url'] ) ) {
183 $url = $website['url'];
184 } elseif ( is_object( $website ) && property_exists( $website, 'url' ) ) {
185 $url = $website->url;
186 }
187 return static::run_call_track( $context, $url, $args, $request_id, $desc );
188 }
189
190 /**
191 * Track running timing.
192 *
193 * @param string $check_context Execution check context.
194 * @param string $url Request URL.
195 * @param array $args Request args.
196 * @param string $request_id Request ID.
197 * @param string $desc Exec desc.
198 *
199 * @return mixed void|string
200 */
201 private static function run_call_track( $check_context, $url, $args, $request_id = '', $desc = '' ) {
202 static $start = array();
203
204 if ( ! isset( static::$exec_stats['exec_time'] ) ) {
205 static::$exec_stats['exec_time'] = array();
206 }
207
208 if ( ! isset( static::$exec_stats['check_count'] ) ) {
209 static::$exec_stats['check_count'] = 0;
210 }
211
212 if ( ! isset( static::$exec_stats['check_data'] ) ) {
213 static::$exec_stats['check_data'] = array();
214 }
215
216 // include microtime to avoid overwrite.
217 if ( 'start_point' === $check_context ) {
218 $key = md5( $url . serialize( $args ) . microtime( true ) ); //phpcs:ignore -- NOSONAR - good for key.
219 $start[ $key ] = microtime( true );
220 // store request_id (required!).
221 return $key;
222 }
223
224 if ( empty( $request_id ) ) {
225 $request_id = $url;
226 }
227
228 if ( 'end_point' === $check_context && ! empty( $request_id ) ) {
229 $key = (string) $request_id;
230
231 if ( isset( $start[ $key ] ) ) {
232 $time = microtime( true ) - $start[ $key ];
233 ++static::$exec_stats['check_count'];
234 static::$exec_stats['exec_time'][] = $time;
235 static::$exec_stats['check_desc'][] = $desc;
236
237 $data = array(
238 'url' => $url,
239 );
240 if ( ! empty( $args ) ) {
241 $data['args'] = $args;
242 }
243
244 static::$exec_stats['check_data'][] = $data;
245 unset( $start[ $key ] );
246 }
247 }
248 }
249
250 /**
251 * Method get_exec_call_stats.
252 *
253 * @return array Http stats.
254 */
255 public static function get_exec_call_stats() {
256 return self::$exec_stats;
257 }
258
259 /**
260 * Method get db stats.
261 *
262 * @return array Array db queries usage stats.
263 */
264 public static function get_queries_stats() {
265 global $wpdb;
266 if ( empty( $wpdb->queries ) ) {
267 return array();
268 }
269 $queries = $wpdb->queries;
270 return array(
271 'total_queries' => is_array( $queries ) ? count( $queries ) : 0,
272 'total_runtime' => is_array( $queries ) ? array_sum( array_column( $queries, 1 ) ) : 0,
273 );
274 }
275 }
276