| 1 |
<?php |
| 2 |
|
| 3 |
namespace MasterAddons\Modules\Utilities; |
| 4 |
|
| 5 |
/** |
| 6 |
* Author Name: Liton Arefin |
| 7 |
* Author URL: https://jeweltheme.com |
| 8 |
* Date: 01/26/2025 |
| 9 |
*/ |
| 10 |
if (!defined('ABSPATH')) exit; // If this file is called directly, abort. |
| 11 |
|
| 12 |
if (!class_exists('MasterAddons\Modules\Utilities\WhichElement')) { |
| 13 |
class WhichElement |
| 14 |
{ |
| 15 |
private static $instance = null; |
| 16 |
private static $plugins = []; |
| 17 |
|
| 18 |
public static function get_instance() |
| 19 |
{ |
| 20 |
if (!self::$instance) { |
| 21 |
self::$instance = new self; |
| 22 |
} |
| 23 |
return self::$instance; |
| 24 |
} |
| 25 |
|
| 26 |
public function __construct() |
| 27 |
{ |
| 28 |
// Enqueue scripts and styles for editor preview |
| 29 |
\add_action('elementor/preview/enqueue_styles', [$this, 'enqueue_preview_styles']); |
| 30 |
\add_action('elementor/preview/enqueue_scripts', [$this, 'enqueue_preview_scripts']); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Enqueue styles in Elementor preview |
| 35 |
*/ |
| 36 |
public function enqueue_preview_styles() |
| 37 |
{ |
| 38 |
\wp_add_inline_style('elementor-frontend', $this->get_inline_styles()); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Enqueue scripts in Elementor preview |
| 43 |
*/ |
| 44 |
public function enqueue_preview_scripts() |
| 45 |
{ |
| 46 |
\wp_localize_script( |
| 47 |
'jquery', |
| 48 |
'jltmaElementsName', |
| 49 |
[ |
| 50 |
'widgetPluginMap' => self::get_widget_data() |
| 51 |
] |
| 52 |
); |
| 53 |
|
| 54 |
// Add inline script for Elements Name functionality |
| 55 |
\wp_add_inline_script('jquery', $this->get_inline_script()); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Get inline CSS for Elements Name |
| 60 |
*/ |
| 61 |
private function get_inline_styles() |
| 62 |
{ |
| 63 |
return " |
| 64 |
.jltma-elements-name__label { |
| 65 |
position: absolute; |
| 66 |
z-index: 999; |
| 67 |
display: none; |
| 68 |
padding: 1px 5px; |
| 69 |
border-radius: 2px; |
| 70 |
background-color: #d30c5c; |
| 71 |
color: #fff; |
| 72 |
text-align: center; |
| 73 |
font-weight: normal; |
| 74 |
font-size: 12px; |
| 75 |
} |
| 76 |
|
| 77 |
.jltma-elements-name--labelPosition-top-left .jltma-elements-name__label { |
| 78 |
top: 0; |
| 79 |
left: 0; |
| 80 |
} |
| 81 |
|
| 82 |
.jltma-elements-name--showLabelOn-hover .elementor-widget:hover .jltma-elements-name__label { |
| 83 |
display: inline-block; |
| 84 |
} |
| 85 |
"; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Get inline JavaScript for Elements Name |
| 90 |
*/ |
| 91 |
private function get_inline_script() |
| 92 |
{ |
| 93 |
return " |
| 94 |
;(function($, elementsName) { |
| 95 |
'use strict'; |
| 96 |
|
| 97 |
$(window).on('elementor/frontend/init', function() { |
| 98 |
var isEditMode = elementorFrontend.config.environmentMode.edit; |
| 99 |
|
| 100 |
// Only enable in editor mode |
| 101 |
if (!isEditMode) { |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
// Add body classes for styling |
| 106 |
var classes = [ |
| 107 |
'jltma-elements-name', |
| 108 |
'jltma-elements-name--labelPosition-top-left', |
| 109 |
'jltma-elements-name--showLabelOn-hover' |
| 110 |
]; |
| 111 |
|
| 112 |
elementorFrontend.elements.\$body.addClass(classes.join(' ')); |
| 113 |
|
| 114 |
// Add labels to widgets |
| 115 |
elementorFrontend.hooks.addAction('frontend/element_ready/widget', function(\$scope) { |
| 116 |
var widgetType = \$scope.data('widget_type').split('.')[0], |
| 117 |
widgetData = elementsName.widgetPluginMap[widgetType] || {}, |
| 118 |
pluginName = widgetData.plugin || '', |
| 119 |
widgetName = widgetData.widget || '', |
| 120 |
labelText = pluginName ? pluginName + ' (' + widgetName + ')' : widgetName; |
| 121 |
|
| 122 |
// Only add label if we have plugin/widget info |
| 123 |
if (pluginName || widgetName) { |
| 124 |
\$scope.append('<span class=\"jltma-elements-name__label\">' + labelText + '</span>'); |
| 125 |
} |
| 126 |
}); |
| 127 |
}); |
| 128 |
}(jQuery, jltmaElementsName)); |
| 129 |
"; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Get plugin directory name from plugin base |
| 134 |
* |
| 135 |
* @param $plugin_base_name |
| 136 |
* @return bool|string |
| 137 |
*/ |
| 138 |
private static function get_plugin_slug($plugin_base_name) |
| 139 |
{ |
| 140 |
return substr($plugin_base_name, 0, strpos($plugin_base_name, '/')); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Get the active plugins. |
| 145 |
* |
| 146 |
* @return array |
| 147 |
*/ |
| 148 |
protected static function get_active_plugins() |
| 149 |
{ |
| 150 |
if (empty(self::$plugins)) { |
| 151 |
// Ensure get_plugins function is loaded |
| 152 |
if (!\function_exists('get_plugins')) { |
| 153 |
require_once \ABSPATH . 'wp-admin/includes/plugin.php'; |
| 154 |
} |
| 155 |
|
| 156 |
$active_plugins = \get_option('active_plugins'); |
| 157 |
self::$plugins = \array_intersect_key(\get_plugins(), \array_flip($active_plugins)); |
| 158 |
} |
| 159 |
return self::$plugins; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Get widget data mapping |
| 164 |
* |
| 165 |
* @return array |
| 166 |
*/ |
| 167 |
public static function get_widget_data() |
| 168 |
{ |
| 169 |
$widget_types = \Elementor\Plugin::instance()->widgets_manager->get_widget_types(); |
| 170 |
$data_map = []; |
| 171 |
$plugins = self::get_active_plugins(); |
| 172 |
|
| 173 |
foreach ($widget_types as $widget_key => $widget_data) { |
| 174 |
$reflection = new \ReflectionClass($widget_data); |
| 175 |
|
| 176 |
$widget_file = \plugin_basename($reflection->getFileName()); |
| 177 |
$plugin_slug = self::get_plugin_slug($widget_file); |
| 178 |
|
| 179 |
foreach ($plugins as $plugin_root => $plugin_meta) { |
| 180 |
$_plugin_slug = self::get_plugin_slug($plugin_root); |
| 181 |
if ($plugin_slug === $_plugin_slug) { |
| 182 |
$data_map[$widget_key] = [ |
| 183 |
'plugin' => $plugin_meta['Name'], |
| 184 |
'widget' => $widget_data->get_title() |
| 185 |
]; |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
return $data_map; |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
if (class_exists('MasterAddons\Modules\Utilities\WhichElement')) { |
| 196 |
WhichElement::get_instance(); |
| 197 |
} |
| 198 |
|