inspector.php
145 lines
| 1 | <?php |
| 2 | namespace Elementor\Core\Debug; |
| 3 | |
| 4 | use Elementor\Settings; |
| 5 | use Elementor\Tools; |
| 6 | |
| 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 8 | exit; // Exit if accessed directly |
| 9 | } |
| 10 | |
| 11 | class Inspector { |
| 12 | |
| 13 | protected $is_enabled = false; |
| 14 | |
| 15 | protected $log = []; |
| 16 | |
| 17 | /** |
| 18 | * @since 2.1.2 |
| 19 | * @access public |
| 20 | */ |
| 21 | public function __construct() { |
| 22 | $is_debug = ( defined( 'WP_DEBUG' ) && WP_DEBUG ); |
| 23 | $option = get_option( 'elementor_enable_inspector', null ); |
| 24 | |
| 25 | $this->is_enabled = is_null( $option ) ? $is_debug : 'enable' === $option; |
| 26 | |
| 27 | if ( $this->is_enabled ) { |
| 28 | add_action( 'admin_bar_menu', [ $this, 'add_menu_in_admin_bar' ], 201 ); |
| 29 | } |
| 30 | |
| 31 | add_action( 'elementor/admin/after_create_settings/' . Tools::PAGE_ID, [ $this, 'register_admin_tools_fields' ], 50 ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @since 2.1.3 |
| 36 | * @access public |
| 37 | */ |
| 38 | public function is_enabled() { |
| 39 | return $this->is_enabled; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @since 2.1.3 |
| 44 | * @access public |
| 45 | */ |
| 46 | public function register_admin_tools_fields( Tools $tools ) { |
| 47 | $tools->add_fields( Settings::TAB_GENERAL, 'tools', [ |
| 48 | 'enable_inspector' => [ |
| 49 | 'label' => esc_html__( 'Debug Bar', 'elementor' ), |
| 50 | 'field_args' => [ |
| 51 | 'type' => 'select', |
| 52 | 'std' => $this->is_enabled ? 'enable' : '', |
| 53 | 'options' => [ |
| 54 | '' => esc_html__( 'Disable', 'elementor' ), |
| 55 | 'enable' => esc_html__( 'Enable', 'elementor' ), |
| 56 | ], |
| 57 | 'desc' => esc_html__( 'Debug Bar adds an admin bar menu that lists all the templates that are used on a page that is being displayed.', 'elementor' ), |
| 58 | ], |
| 59 | ], |
| 60 | ] ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @since 2.1.2 |
| 65 | * @access public |
| 66 | */ |
| 67 | public function parse_template_path( $template ) { |
| 68 | // `untrailingslashit` for windows path style. |
| 69 | if ( 0 === strpos( $template, untrailingslashit( ELEMENTOR_PATH ) ) ) { |
| 70 | return 'Elementor - ' . basename( $template ); |
| 71 | } |
| 72 | |
| 73 | if ( 0 === strpos( $template, get_stylesheet_directory() ) ) { |
| 74 | return wp_get_theme()->get( 'Name' ) . ' - ' . basename( $template ); |
| 75 | } |
| 76 | |
| 77 | $plugins_dir = dirname( ELEMENTOR_PATH ); |
| 78 | if ( 0 === strpos( $template, $plugins_dir ) ) { |
| 79 | return ltrim( str_replace( $plugins_dir, '', $template ), '/\\' ); |
| 80 | } |
| 81 | |
| 82 | return str_replace( WP_CONTENT_DIR, '', $template ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @since 2.1.2 |
| 87 | * @access public |
| 88 | */ |
| 89 | public function add_log( $module, $title, $url = '' ) { |
| 90 | if ( ! $this->is_enabled ) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | if ( ! isset( $this->log[ $module ] ) ) { |
| 95 | $this->log[ $module ] = []; |
| 96 | } |
| 97 | |
| 98 | $this->log[ $module ][] = [ |
| 99 | 'title' => $title, |
| 100 | 'url' => $url, |
| 101 | ]; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * @since 2.1.2 |
| 106 | * @access public |
| 107 | */ |
| 108 | public function add_menu_in_admin_bar( \WP_Admin_Bar $wp_admin_bar ) { |
| 109 | if ( empty( $this->log ) ) { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | $wp_admin_bar->add_node( [ |
| 114 | 'id' => 'elementor_inspector', |
| 115 | 'title' => esc_html__( 'Elementor Debugger', 'elementor' ), |
| 116 | ] ); |
| 117 | |
| 118 | foreach ( $this->log as $module => $log ) { |
| 119 | $module_id = sanitize_key( $module ); |
| 120 | |
| 121 | $wp_admin_bar->add_menu( [ |
| 122 | 'id' => 'elementor_inspector_' . $module_id, |
| 123 | 'parent' => 'elementor_inspector', |
| 124 | 'title' => $module, |
| 125 | ] ); |
| 126 | |
| 127 | foreach ( $log as $index => $row ) { |
| 128 | $url = $row['url']; |
| 129 | |
| 130 | unset( $row['url'] ); |
| 131 | |
| 132 | $wp_admin_bar->add_menu( [ |
| 133 | 'id' => 'elementor_inspector_log_' . $module_id . '_' . $index, |
| 134 | 'parent' => 'elementor_inspector_' . $module_id, |
| 135 | 'href' => $url, |
| 136 | 'title' => implode( ' > ', $row ), |
| 137 | 'meta' => [ |
| 138 | 'target' => '_blank', |
| 139 | ], |
| 140 | ] ); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 |