| 1 |
<?php |
| 2 |
namespace Elementor\Modules\DevTools; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; // Exit if accessed directly. |
| 6 |
} |
| 7 |
|
| 8 |
class Backtrace_Helper { |
| 9 |
/** |
| 10 |
* find_who_called_me |
| 11 |
* Retrieves the function, class, file, line, type and name of the function that called the function that called this function. |
| 12 |
* |
| 13 |
* @param int $stack_depth The depth of the stack to look for. |
| 14 |
* @return array with the following keys: |
| 15 |
* function - calling function |
| 16 |
* class - calling class |
| 17 |
* file - the file that contains the calling function |
| 18 |
* line - location |
| 19 |
* type - plugin or theme |
| 20 |
* name - plugin/theme name |
| 21 |
*/ |
| 22 |
public static function find_who_called_me( $stack_depth ) { |
| 23 |
// phpcs:disable |
| 24 |
$backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); |
| 25 |
// phpcs:enable |
| 26 |
$caller = []; |
| 27 |
if ( array_key_exists( $stack_depth, $backtrace ) ) { |
| 28 |
$caller = $backtrace[ $stack_depth ]; |
| 29 |
} |
| 30 |
$caller_function = $caller['function'] ?? ''; |
| 31 |
$caller_class = $caller['class'] ?? ''; |
| 32 |
$caller_file = $caller['file'] ?? ''; |
| 33 |
$caller_line = $caller['line'] ?? ''; |
| 34 |
$source = self::get_source( $caller_file ); |
| 35 |
|
| 36 |
$res = [ |
| 37 |
'function' => $caller_function, |
| 38 |
'class' => $caller_class, |
| 39 |
'file' => $caller_file, |
| 40 |
'line' => $caller_line, |
| 41 |
'type' => $source['type'], |
| 42 |
'name' => $source['name'], |
| 43 |
]; |
| 44 |
return $res; |
| 45 |
} |
| 46 |
|
| 47 |
private static function get_source( $filename ) { |
| 48 |
|
| 49 |
$name = 'Unknown'; |
| 50 |
$type = ''; |
| 51 |
|
| 52 |
if ( str_contains( $filename, WP_CONTENT_DIR ) ) { |
| 53 |
$file = str_replace( WP_CONTENT_DIR, '', $filename ); |
| 54 |
$short_path = explode( '/', $file ); |
| 55 |
if ( count( $short_path ) >= 3 ) { |
| 56 |
$type = $short_path[1]; |
| 57 |
$name = $short_path[2]; |
| 58 |
} |
| 59 |
} |
| 60 |
return [ |
| 61 |
'name' => $name, // plugin/theme name. |
| 62 |
'type' => $type, // is it a plugin or a theme. |
| 63 |
]; |
| 64 |
} |
| 65 |
} |
| 66 |
|