PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.2.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.2.1
6.2.2 6.2.1 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 All 155 releases
← All changes | class/class-mainwp-execution-helper.php +195 -7 5.2 → 6.2.1 View file →
@@ -7,8 +7,13 @@
7 7 */
8 8
9 9 namespace MainWP\Dashboard;
10 10
11 +// Exit if accessed directly.
12 +if ( ! defined( 'ABSPATH' ) ) {
13 + exit;
14 +}
15 +
11 16 /**
12 17 * Class MainWP_Execution_Helper
13 18 *
14 19 * @package MainWP\Dashboard
@@ -23,9 +28,9 @@
23 28 */
24 29 private static $exec_start = null;
25 30
26 31 /**
27 - * Private static varibale to hold the instance.
32 + * Static varibale to hold the instance.
28 33 *
29 34 * @var mixed Default null
30 35 */
31 36 public static $instance = null;
@@ -30,8 +35,16 @@
30 35 */
31 36 public static $instance = null;
32 37
33 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 + /**
34 47 * Method instance()
35 48 *
36 49 * Returns new MainWP_Logger instance.
37 50 *
@@ -62,12 +75,9 @@
62 75 *
63 76 * Init execution time start value.
64 77 */
65 78 public function init_exec_time() {
66 - if ( null === static::$exec_start ) {
67 - static::$exec_start = microtime( true );
68 - }
69 - MainWP_Logger::instance()->init_execution_time(); // compatible.
79 + MainWP_Logger::instance()->init_execution_time();
70 80 return static::$exec_start;
71 81 }
72 82
73 83 /**
@@ -72,16 +82,194 @@
72 82
73 83 /**
74 84 * Method get_exec_time().
75 85 *
86 + * @deprecated Compatible.
87 + *
76 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.
77 93 */
78 - public function get_exec_time() {
94 + public function get_exec_time( $log_exec = true ) {
79 95 if ( null === static::$exec_start ) {
80 96 static::$exec_start = microtime( true );
81 97 }
82 98
83 99 $sec = microtime( true ) - static::$exec_start; // seconds.
84 - MainWP_Logger::instance()->log_action( 'execution time :: [value=' . round( $sec, 4 ) . '](seconds)', MainWP_Logger::EXECUTION_TIME_LOG_PRIORITY );
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 +
85 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 + );
86 274 }
87 275 }