| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
if( ! class_exists( 'MywpDeveloperAbstractModule' ) ) { |
| 8 |
return false; |
| 9 |
} |
| 10 |
|
| 11 |
if ( ! class_exists( 'MywpDeveloperModuleDevTimes' ) ) : |
| 12 |
|
| 13 |
final class MywpDeveloperModuleDevTimes extends MywpDeveloperAbstractModule { |
| 14 |
|
| 15 |
static protected $id = 'dev_times'; |
| 16 |
|
| 17 |
static protected $priority = 100; |
| 18 |
|
| 19 |
static private $init_process; |
| 20 |
|
| 21 |
protected static function after_init() { |
| 22 |
|
| 23 |
global $timestart; |
| 24 |
|
| 25 |
$init_process = MywpDeveloper::get_process(); |
| 26 |
|
| 27 |
$define_microtime = MywpHelper::get_define( 'MYWP_DEVELOPER_MICROTIME' ); |
| 28 |
|
| 29 |
if( ! empty( $define_microtime ) ) { |
| 30 |
|
| 31 |
$init_process['microtime'] = $define_microtime; |
| 32 |
|
| 33 |
} elseif ( ! empty( $timestart ) ) { |
| 34 |
|
| 35 |
$init_process['microtime'] = $timestart; |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
$define_memory_get_usage = MywpHelper::get_define( 'MYWP_DEVELOPER_MEMORY_GET_USAGE' ); |
| 40 |
|
| 41 |
if( ! empty( $define_memory_get_usage ) ) { |
| 42 |
|
| 43 |
$init_process['memory_get_usage'] = $define_memory_get_usage; |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
$define_load_avg = MywpHelper::get_define( 'MYWP_DEVELOPER_LOAD_AVG' ); |
| 48 |
|
| 49 |
if( ! empty( $define_load_avg ) ) { |
| 50 |
|
| 51 |
$init_process['load_avg'] = $define_load_avg; |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
self::$init_process = $init_process; |
| 56 |
|
| 57 |
self::add_actions( 'plugins_loaded' ); |
| 58 |
self::add_actions( 'setup_theme' ); |
| 59 |
self::add_actions( 'after_setup_theme' ); |
| 60 |
self::add_actions( 'init' ); |
| 61 |
self::add_actions( 'wp_loaded' ); |
| 62 |
|
| 63 |
if( is_admin() ) { |
| 64 |
|
| 65 |
self::add_actions( 'admin_menu' ); |
| 66 |
self::add_actions( 'admin_init' ); |
| 67 |
self::add_actions( 'admin_head' ); |
| 68 |
self::add_actions( 'admin_footer' ); |
| 69 |
|
| 70 |
} else { |
| 71 |
|
| 72 |
self::add_actions( 'parse_request' ); |
| 73 |
self::add_actions( 'wp' ); |
| 74 |
self::add_actions( 'template_redirect' ); |
| 75 |
self::add_actions( 'wp_head' ); |
| 76 |
self::add_actions( 'wp_footer' ); |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
private static function add_actions( $action_hook_name = false ) { |
| 83 |
|
| 84 |
if( empty( $action_hook_name ) ) { |
| 85 |
|
| 86 |
$called_text = sprintf( '%s::%s( %s )' , __CLASS__ , '$action_hook_name' ); |
| 87 |
|
| 88 |
MywpHelper::error_not_found_message( '$action_hook_name' , $called_text ); |
| 89 |
|
| 90 |
return false; |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
if( $action_hook_name !== 'plugins_loaded' ) { |
| 95 |
|
| 96 |
add_action( $action_hook_name , array( __CLASS__ , 'process_set_1' ) , -10 ); |
| 97 |
add_action( $action_hook_name , array( __CLASS__ , 'process_set_10' ) , 10 ); |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
add_action( $action_hook_name , array( __CLASS__ , 'process_set_100' ) , 100 ); |
| 102 |
add_action( $action_hook_name , array( __CLASS__ , 'process_set_10000' ) , 10000 ); |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
private static function set_process( $priority ) { |
| 107 |
|
| 108 |
if( ! MywpDeveloper::is_debug() ) { |
| 109 |
|
| 110 |
return false; |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
$current_filter = MywpDeveloper::get_current_filter(); |
| 115 |
|
| 116 |
if( empty( $current_filter ) ) { |
| 117 |
|
| 118 |
return false; |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
$processes = self::get_processes(); |
| 123 |
|
| 124 |
$processes[ $current_filter ][ $priority ] = MywpDeveloper::get_process(); |
| 125 |
|
| 126 |
$mywp_cache = new MywpCache( 'mywp_times' ); |
| 127 |
|
| 128 |
$mywp_cache->update_cache( $processes ); |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
private static function get_processes() { |
| 133 |
|
| 134 |
$mywp_cache = new MywpCache( 'mywp_times' ); |
| 135 |
|
| 136 |
$procecces = $mywp_cache->get_cache(); |
| 137 |
|
| 138 |
if( empty( $procecces ) ) { |
| 139 |
|
| 140 |
$procecces = array(); |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
return $procecces; |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
private static function get_process( $filter_name = false ) { |
| 149 |
|
| 150 |
if( empty( $filter_name ) ) { |
| 151 |
|
| 152 |
return false; |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
$processes = self::get_processes(); |
| 157 |
|
| 158 |
if( empty( $processes[ $filter_name ] ) ) { |
| 159 |
|
| 160 |
return false; |
| 161 |
|
| 162 |
} |
| 163 |
|
| 164 |
return $processes[ $filter_name ]; |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
public static function process_set_1() { |
| 169 |
|
| 170 |
self::set_process( -1 ); |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
public static function process_set_10() { |
| 175 |
|
| 176 |
self::set_process( 10 ); |
| 177 |
|
| 178 |
} |
| 179 |
|
| 180 |
public static function process_set_100() { |
| 181 |
|
| 182 |
self::set_process( 100 ); |
| 183 |
|
| 184 |
} |
| 185 |
|
| 186 |
public static function process_set_10000() { |
| 187 |
|
| 188 |
self::set_process( 10000 ); |
| 189 |
|
| 190 |
} |
| 191 |
|
| 192 |
public static function mywp_debug_renders( $debug_renders ) { |
| 193 |
|
| 194 |
$debug_renders[ self::$id ] = array( |
| 195 |
'debug_type' => 'dev', |
| 196 |
'title' => __( 'Action Times' , 'my-wp' ), |
| 197 |
); |
| 198 |
|
| 199 |
return $debug_renders; |
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
protected static function mywp_developer_debug() { |
| 204 |
|
| 205 |
$processes = self::get_processes(); |
| 206 |
|
| 207 |
if( empty( $processes ) ) { |
| 208 |
|
| 209 |
return false; |
| 210 |
|
| 211 |
} |
| 212 |
|
| 213 |
$first_process = self::get_first_process(); |
| 214 |
|
| 215 |
if( empty( $first_process ) ) { |
| 216 |
|
| 217 |
return false; |
| 218 |
|
| 219 |
} |
| 220 |
|
| 221 |
printf( '%s = ' , __( 'All' ) ); |
| 222 |
|
| 223 |
echo self::print_format( $first_process ); |
| 224 |
|
| 225 |
$before_screen_process = self::get_before_screen_process(); |
| 226 |
|
| 227 |
if( empty( $before_screen_process ) ) { |
| 228 |
|
| 229 |
return false; |
| 230 |
|
| 231 |
} |
| 232 |
|
| 233 |
printf( '%s = ' , __( 'Before Screen' , 'my-wp' ) ); |
| 234 |
|
| 235 |
echo self::print_format( $before_screen_process ); |
| 236 |
|
| 237 |
$screen_process = self::get_screen_process(); |
| 238 |
|
| 239 |
if( empty( $screen_process ) ) { |
| 240 |
|
| 241 |
return false; |
| 242 |
|
| 243 |
} |
| 244 |
|
| 245 |
printf( '%s = ' , __( 'Screen' , 'my-wp' ) ); |
| 246 |
|
| 247 |
echo self::print_format( $screen_process ); |
| 248 |
|
| 249 |
foreach( $processes as $action_hook_name => $priority_processes ) { |
| 250 |
|
| 251 |
echo $action_hook_name . "\n"; |
| 252 |
|
| 253 |
$last_priority_process = end( $priority_processes ); |
| 254 |
$first_priority_process = reset( $priority_processes ); |
| 255 |
|
| 256 |
$total_screen_process = self::format_process( $first_priority_process , $last_priority_process ); |
| 257 |
|
| 258 |
printf( '%s = ' , __( 'Total' , 'my-wp' ) ); |
| 259 |
|
| 260 |
echo self::print_format( $total_screen_process ); |
| 261 |
|
| 262 |
$priority_processes_count = count( $priority_processes ); |
| 263 |
$priority_processes_keys = array_keys( $priority_processes ); |
| 264 |
|
| 265 |
for( $i = 0; $i < ( $priority_processes_count -1 ); $i++ ) { |
| 266 |
|
| 267 |
$current_priority = $priority_processes_keys[ $i ]; |
| 268 |
|
| 269 |
$next_priority = $priority_processes_keys[ ( $i + 1 ) ]; |
| 270 |
|
| 271 |
$current_process = $priority_processes[ $current_priority ]; |
| 272 |
|
| 273 |
$next_process = $priority_processes[ $next_priority ]; |
| 274 |
|
| 275 |
$diff_process = self::format_process( $current_process , $next_process ); |
| 276 |
|
| 277 |
printf( '[%s] - [%s] = ' , $current_priority , $next_priority ); |
| 278 |
|
| 279 |
echo self::print_format( $diff_process ); |
| 280 |
|
| 281 |
echo self::print_filters( $action_hook_name , $current_priority , $next_priority ); |
| 282 |
|
| 283 |
} |
| 284 |
|
| 285 |
} |
| 286 |
|
| 287 |
} |
| 288 |
|
| 289 |
protected static function mywp_debug_render() { |
| 290 |
|
| 291 |
$processes = self::get_processes(); |
| 292 |
|
| 293 |
if( empty( $processes ) ) { |
| 294 |
|
| 295 |
return false; |
| 296 |
|
| 297 |
} |
| 298 |
|
| 299 |
echo '<table class="debug-table">'; |
| 300 |
|
| 301 |
$first_process = self::get_first_process(); |
| 302 |
|
| 303 |
if( empty( $first_process ) ) { |
| 304 |
|
| 305 |
return false; |
| 306 |
|
| 307 |
} |
| 308 |
|
| 309 |
echo '<tr>'; |
| 310 |
|
| 311 |
printf( '<th>%s</th>' , __( 'All' ) ); |
| 312 |
|
| 313 |
printf( '<td>%s</td>' , self::print_format( $first_process ) ); |
| 314 |
|
| 315 |
echo '</tr>'; |
| 316 |
|
| 317 |
echo '</table>'; |
| 318 |
|
| 319 |
echo '<table class="debug-table">'; |
| 320 |
|
| 321 |
$before_screen_process = self::get_before_screen_process(); |
| 322 |
|
| 323 |
if( empty( $before_screen_process ) ) { |
| 324 |
|
| 325 |
return false; |
| 326 |
|
| 327 |
} |
| 328 |
|
| 329 |
echo '<tr>'; |
| 330 |
|
| 331 |
printf( '<th>%s</th>' , __( 'Before Screen' , 'my-wp' ) ); |
| 332 |
|
| 333 |
printf( '<td>%s</td>' , self::print_format( $before_screen_process ) ); |
| 334 |
|
| 335 |
echo '</tr>'; |
| 336 |
|
| 337 |
$screen_process = self::get_screen_process(); |
| 338 |
|
| 339 |
if( empty( $screen_process ) ) { |
| 340 |
|
| 341 |
return false; |
| 342 |
|
| 343 |
} |
| 344 |
|
| 345 |
echo '<tr>'; |
| 346 |
|
| 347 |
printf( '<th>%s</th>' , __( 'Screen' , 'my-wp' ) ); |
| 348 |
|
| 349 |
printf( '<td>%s</td>' , self::print_format( $screen_process ) ); |
| 350 |
|
| 351 |
echo '</tr>'; |
| 352 |
|
| 353 |
echo '</table>'; |
| 354 |
|
| 355 |
foreach( $processes as $action_hook_name => $priority_processes ) { |
| 356 |
|
| 357 |
printf( '<p>%s</p>' , $action_hook_name ); |
| 358 |
|
| 359 |
echo '<table class="debug-table">'; |
| 360 |
|
| 361 |
$last_priority_process = end( $priority_processes ); |
| 362 |
$first_priority_process = reset( $priority_processes ); |
| 363 |
|
| 364 |
$total_screen_process = self::format_process( $first_priority_process , $last_priority_process ); |
| 365 |
|
| 366 |
echo '<tr>'; |
| 367 |
|
| 368 |
printf( '<th>%s</th>' , __( 'Total' , 'my-wp' ) ); |
| 369 |
|
| 370 |
printf( '<td>%s</td>' , self::print_format( $total_screen_process ) ); |
| 371 |
|
| 372 |
echo '</tr>'; |
| 373 |
|
| 374 |
$priority_processes_count = count( $priority_processes ); |
| 375 |
$priority_processes_keys = array_keys( $priority_processes ); |
| 376 |
|
| 377 |
for( $i = 0; $i < ( $priority_processes_count -1 ); $i++ ) { |
| 378 |
|
| 379 |
$current_priority = $priority_processes_keys[ $i ]; |
| 380 |
|
| 381 |
$next_priority = $priority_processes_keys[ ( $i + 1 ) ]; |
| 382 |
|
| 383 |
$current_process = $priority_processes[ $current_priority ]; |
| 384 |
|
| 385 |
$next_process = $priority_processes[ $next_priority ]; |
| 386 |
|
| 387 |
$diff_process = self::format_process( $current_process , $next_process ); |
| 388 |
|
| 389 |
echo '<tr>'; |
| 390 |
|
| 391 |
printf( '<th>%s</th>' , sprintf( '[%s] - [%s]' , $current_priority , $next_priority ) ); |
| 392 |
|
| 393 |
printf( '<td>%s' , self::print_format( $diff_process ) ); |
| 394 |
|
| 395 |
echo self::print_filters( $action_hook_name , $current_priority , $next_priority , true ); |
| 396 |
|
| 397 |
echo '</td>'; |
| 398 |
|
| 399 |
echo '</tr>'; |
| 400 |
|
| 401 |
} |
| 402 |
|
| 403 |
echo '</table>'; |
| 404 |
|
| 405 |
} |
| 406 |
|
| 407 |
//printf( '<pre>%s</pre>' , print_r( $processes , true ) ); |
| 408 |
|
| 409 |
} |
| 410 |
|
| 411 |
private static function get_first_process() { |
| 412 |
|
| 413 |
$processes = self::get_processes(); |
| 414 |
|
| 415 |
if( empty( $processes ) ) { |
| 416 |
|
| 417 |
return false; |
| 418 |
|
| 419 |
} |
| 420 |
|
| 421 |
$init_process = self::$init_process; |
| 422 |
|
| 423 |
$last_process_priorities = end( $processes ); |
| 424 |
|
| 425 |
$last_process = end( $last_process_priorities ); |
| 426 |
|
| 427 |
return self::format_process( $init_process , $last_process ); |
| 428 |
|
| 429 |
} |
| 430 |
|
| 431 |
private static function get_before_screen_process() { |
| 432 |
|
| 433 |
$processes = self::get_processes(); |
| 434 |
|
| 435 |
if( empty( $processes ) ) { |
| 436 |
|
| 437 |
return false; |
| 438 |
|
| 439 |
} |
| 440 |
|
| 441 |
$init_process = self::$init_process; |
| 442 |
|
| 443 |
$before_screen_process = false; |
| 444 |
|
| 445 |
if( ! empty( $processes['admin_head'][-1] ) ) { |
| 446 |
|
| 447 |
$before_screen_process = $processes['admin_head'][-1]; |
| 448 |
|
| 449 |
} elseif( ! empty( $processes['wp_head'][-1] ) ) { |
| 450 |
|
| 451 |
$before_screen_process = $processes['wp_head'][-1]; |
| 452 |
|
| 453 |
} |
| 454 |
|
| 455 |
return self::format_process( $init_process , $before_screen_process ); |
| 456 |
|
| 457 |
} |
| 458 |
|
| 459 |
private static function get_screen_process() { |
| 460 |
|
| 461 |
$processes = self::get_processes(); |
| 462 |
|
| 463 |
if( empty( $processes ) ) { |
| 464 |
|
| 465 |
return false; |
| 466 |
|
| 467 |
} |
| 468 |
|
| 469 |
$before_screen_process = false; |
| 470 |
|
| 471 |
if( ! empty( $processes['admin_head'][-1] ) ) { |
| 472 |
|
| 473 |
$before_screen_process = $processes['admin_head'][-1]; |
| 474 |
|
| 475 |
} elseif( ! empty( $processes['wp_head'][-1] ) ) { |
| 476 |
|
| 477 |
$before_screen_process = $processes['wp_head'][-1]; |
| 478 |
|
| 479 |
} |
| 480 |
|
| 481 |
$last_process_priorities = end( $processes ); |
| 482 |
|
| 483 |
$last_process = end( $last_process_priorities ); |
| 484 |
|
| 485 |
return self::format_process( $before_screen_process , $last_process ); |
| 486 |
|
| 487 |
} |
| 488 |
|
| 489 |
private static function format_process( $start_process = false , $end_process = false ) { |
| 490 |
|
| 491 |
if( empty( $start_process ) or empty( $end_process ) ) { |
| 492 |
|
| 493 |
return false; |
| 494 |
|
| 495 |
} |
| 496 |
|
| 497 |
$format_process = array( |
| 498 |
'start' => $start_process, |
| 499 |
'end' => $end_process, |
| 500 |
'second' => ( $end_process['microtime'] - $start_process['microtime'] ), |
| 501 |
'memory' => ( $end_process['memory_get_usage'] - $start_process['memory_get_usage'] ), |
| 502 |
'load_avg' => $start_process['load_avg'], |
| 503 |
); |
| 504 |
|
| 505 |
return $format_process; |
| 506 |
|
| 507 |
} |
| 508 |
|
| 509 |
private static function print_format( $process ) { |
| 510 |
|
| 511 |
if( ! isset( $process['second'] ) or ! isset( $process['memory'] ) ) { |
| 512 |
|
| 513 |
return false; |
| 514 |
|
| 515 |
} |
| 516 |
|
| 517 |
if( function_exists( 'sys_getloadavg' ) ) { |
| 518 |
|
| 519 |
$print_format = sprintf( 'Time: %s - Memory: %s - LoadAvg: %s' , self::get_second( $process['second'] ) , MywpHelper::get_byte( $process['memory'] ) , strip_tags( $process['load_avg'] ) ); |
| 520 |
|
| 521 |
} else { |
| 522 |
|
| 523 |
$print_format = sprintf( 'Time: %s - Memory: %s' , self::get_second( $process['second'] ) , MywpHelper::get_byte( $process['memory'] ) ); |
| 524 |
|
| 525 |
} |
| 526 |
|
| 527 |
$print_format .= "\n"; |
| 528 |
|
| 529 |
return $print_format; |
| 530 |
|
| 531 |
} |
| 532 |
|
| 533 |
private static function print_filters( $action_hook_name , $from_priority , $to_priority , $is_list = false ) { |
| 534 |
|
| 535 |
global $wp_actions; |
| 536 |
|
| 537 |
if( empty( $action_hook_name ) ) { |
| 538 |
|
| 539 |
return false; |
| 540 |
|
| 541 |
} |
| 542 |
|
| 543 |
$filter_to_func = MywpDeveloper::get_filter_to_func( $action_hook_name ); |
| 544 |
|
| 545 |
if( empty( $filter_to_func ) ) { |
| 546 |
|
| 547 |
return false; |
| 548 |
|
| 549 |
} |
| 550 |
|
| 551 |
$print_filters = false; |
| 552 |
|
| 553 |
if( $is_list ) { |
| 554 |
|
| 555 |
$print_filters = '<textarea readonly="readonly" style="height: 30px;">'; |
| 556 |
|
| 557 |
} |
| 558 |
|
| 559 |
foreach( $filter_to_func as $func ) { |
| 560 |
|
| 561 |
if( $func['priority'] >= $from_priority && $func['priority'] <= $to_priority ) { |
| 562 |
|
| 563 |
$print_filters .= sprintf( '(%s) %s' , $func['priority'] , $func['print_format'] ); |
| 564 |
|
| 565 |
$print_filters .= "\n"; |
| 566 |
|
| 567 |
} |
| 568 |
|
| 569 |
} |
| 570 |
|
| 571 |
if( $is_list ) { |
| 572 |
|
| 573 |
$print_filters .= '</textarea>'; |
| 574 |
|
| 575 |
} else { |
| 576 |
|
| 577 |
$print_filters .= "\n"; |
| 578 |
|
| 579 |
} |
| 580 |
|
| 581 |
return $print_filters; |
| 582 |
|
| 583 |
} |
| 584 |
|
| 585 |
private static function get_second( $second = false ) { |
| 586 |
|
| 587 |
if( $second === false ) { |
| 588 |
|
| 589 |
return false; |
| 590 |
|
| 591 |
} |
| 592 |
|
| 593 |
$decimals = 3; |
| 594 |
|
| 595 |
$number = false; |
| 596 |
|
| 597 |
if( function_exists( 'number_format_i18n' ) ) { |
| 598 |
|
| 599 |
$number = number_format_i18n( $second , $decimals ); |
| 600 |
|
| 601 |
} else { |
| 602 |
|
| 603 |
$number = number_format( $second , $decimals ); |
| 604 |
|
| 605 |
} |
| 606 |
|
| 607 |
return "{$number} seconds"; |
| 608 |
|
| 609 |
} |
| 610 |
|
| 611 |
} |
| 612 |
|
| 613 |
MywpDeveloperModuleDevTimes::init(); |
| 614 |
|
| 615 |
endif; |
| 616 |
|