trait-execution-time.php
1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/**
* Methods for obeying system max execution time.
*
* @link https://wordpress.org/plugins/broken-link-checker/
* @since 2.0.0
*
* @author WPMUDEV (https://wpmudev.com)
* @package WPMUDEV_BLC\Core\Traits
*
* @copyright (c) 2022, Incsub (http://incsub.com)
*/
namespace WPMUDEV_BLC\Core\Traits;
// Abort if called directly.
defined( 'WPINC' ) || die;
/**
* Class Enqueue
*
* @package WPMUDEV_BLC\Core\Traits
*/
trait Execution_Time {
/**
* Holds the microtime that timer started on.
*
* @var null
*/
protected static $execution_start_time = null;
/**
* Starts the timer. Required to make sure execution doesn't go above max execution time.
*
* @return void
*/
public function start_timer() {
if ( empty( self::$execution_start_time ) ) {
self::$execution_start_time = microtime( true );
}
}
/**
* Returns the time lapsed since timer started.
*
* @return float
*/
public function runtime() {
//return microtime( true ) - (float) self::$execution_start_time;
return timer_stop();
}
/**
* Returns true if runtime has passed max execution time.
*
* @return bool
*/
public function runtime_passed_limit() {
return $this->runtime() >= $this->max_execution_time();
}
/**
* Max execution time for process. Default is one third of system max_execution_time.
*
* @return mixed
*/
public function max_execution_time() {
$max_execution_time = intval( ini_get( 'max_execution_time' ) );
return intval(
apply_filters(
'wpmudev_blc_max_execution_time',
! empty( $max_execution_time ) ? ( 0.75 * $max_execution_time ) : 15,
$max_execution_time
)
);
}
}