| 1 |
<?php |
| 2 |
|
| 3 |
namespace MasterAddons\Inc\Admin; |
| 4 |
|
| 5 |
use MasterAddons\Inc\Classes\Helper; |
| 6 |
|
| 7 |
if (!defined('ABSPATH')) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* Promote Pro Addons |
| 13 |
* Handles promotion of pro widgets in Elementor editor |
| 14 |
* Dynamically gets pro elements from Config |
| 15 |
*/ |
| 16 |
class Promote_Pro_Addons |
| 17 |
{ |
| 18 |
/** |
| 19 |
* Instance |
| 20 |
* |
| 21 |
* @var Promote_Pro_Addons |
| 22 |
*/ |
| 23 |
private static $instance = null; |
| 24 |
|
| 25 |
/** |
| 26 |
* Get Instance |
| 27 |
* |
| 28 |
* @return Promote_Pro_Addons |
| 29 |
*/ |
| 30 |
public static function get_instance() |
| 31 |
{ |
| 32 |
if (is_null(self::$instance)) { |
| 33 |
self::$instance = new self(); |
| 34 |
} |
| 35 |
return self::$instance; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Constructor |
| 40 |
*/ |
| 41 |
public function __construct() |
| 42 |
{ |
| 43 |
// Don't show pro promotions if Pro version is active |
| 44 |
if (Helper::jltma_premium()) { |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
add_filter('elementor/editor/localize_settings', [$this, 'promote_pro_addons']); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Extract icon from addon file by parsing get_icon() method |
| 53 |
* Searches in group/subcategory structure |
| 54 |
* |
| 55 |
* @param string $key The addon key (e.g., 'ma-news-ticker') |
| 56 |
* @param array $addon The addon config data |
| 57 |
* @return string|null The icon class or null if not found |
| 58 |
*/ |
| 59 |
private function get_icon_from_file($key, $addon = []) |
| 60 |
{ |
| 61 |
$group = $addon['group'] ?? ''; |
| 62 |
$subcategory = $addon['subcategory'] ?? ''; |
| 63 |
|
| 64 |
// Build possible paths in order of priority |
| 65 |
$paths = []; |
| 66 |
|
| 67 |
// New structure: group/subcategory/widget/widget.php |
| 68 |
if ($group && $subcategory) { |
| 69 |
if (defined('JLTMA_PRO_ADDONS')) { |
| 70 |
$paths[] = JLTMA_PRO_ADDONS . $group . '/' . $subcategory . '/' . $key . '/' . $key . '.php'; |
| 71 |
} |
| 72 |
if (defined('JLTMA_ADDONS')) { |
| 73 |
$paths[] = JLTMA_ADDONS . $group . '/' . $subcategory . '/' . $key . '/' . $key . '.php'; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// Dynamic scan as fallback |
| 78 |
$base_dirs = defined('JLTMA_ADDONS') ? [JLTMA_ADDONS] : []; |
| 79 |
if (defined('JLTMA_PRO_ADDONS')) { |
| 80 |
array_unshift($base_dirs, JLTMA_PRO_ADDONS); |
| 81 |
} |
| 82 |
foreach ($base_dirs as $base_dir) { |
| 83 |
if (!is_dir($base_dir)) continue; |
| 84 |
$groups = array_filter(glob($base_dir . '*'), 'is_dir'); |
| 85 |
foreach ($groups as $group_path) { |
| 86 |
$subcategories = array_filter(glob($group_path . '/*'), 'is_dir'); |
| 87 |
foreach ($subcategories as $subcategory_path) { |
| 88 |
$paths[] = $subcategory_path . '/' . $key . '/' . $key . '.php'; |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
foreach ($paths as $file_path) { |
| 94 |
if (!file_exists($file_path)) { |
| 95 |
continue; |
| 96 |
} |
| 97 |
|
| 98 |
$content = file_get_contents($file_path); |
| 99 |
if ($content === false) { |
| 100 |
continue; |
| 101 |
} |
| 102 |
|
| 103 |
// Match get_icon() method and extract the return value |
| 104 |
if (preg_match('/function\s+get_icon\s*\(\s*\)\s*\{[^}]*return\s+[\'"]([^\'"]+)[\'"]\s*;/s', $content, $matches)) { |
| 105 |
return $matches[1]; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
return null; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Get Pro Elements from centralized config |
| 114 |
* |
| 115 |
* @return array Pro elements with icon and categories |
| 116 |
*/ |
| 117 |
private function get_pro_elements() |
| 118 |
{ |
| 119 |
$pro_widgets = []; |
| 120 |
|
| 121 |
// Get all pro addons from unified config |
| 122 |
$all_addons = Config::get_addons(); |
| 123 |
|
| 124 |
foreach ($all_addons as $key => $addon) { |
| 125 |
// Only get pro elements |
| 126 |
if (empty($addon['is_pro']) || $addon['is_pro'] !== true) { |
| 127 |
continue; |
| 128 |
} |
| 129 |
|
| 130 |
$widget_data = [ |
| 131 |
'title' => $addon['title'] ?? '', |
| 132 |
'name' => $key, |
| 133 |
'icon' => 'jltma-icon eicon-lock', // Default fallback icon |
| 134 |
'categories' => '["master-addons"]', // Default category |
| 135 |
]; |
| 136 |
|
| 137 |
$icon_found = false; |
| 138 |
|
| 139 |
// Method 1: Try to get icon from the widget class if it exists |
| 140 |
if (!empty($addon['class']) && class_exists($addon['class'])) { |
| 141 |
try { |
| 142 |
$widget_instance = new $addon['class'](); |
| 143 |
|
| 144 |
if (method_exists($widget_instance, 'get_icon')) { |
| 145 |
$widget_data['icon'] = $widget_instance->get_icon(); |
| 146 |
$icon_found = true; |
| 147 |
} |
| 148 |
|
| 149 |
if (method_exists($widget_instance, 'get_categories')) { |
| 150 |
$categories = $widget_instance->get_categories(); |
| 151 |
$widget_data['categories'] = json_encode($categories); |
| 152 |
} |
| 153 |
} catch (\Exception $e) { |
| 154 |
// Silently fail |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
// Method 2: If class doesn't exist, extract icon from addon file |
| 159 |
if (!$icon_found) { |
| 160 |
$file_icon = $this->get_icon_from_file($key, $addon); |
| 161 |
if ($file_icon) { |
| 162 |
$widget_data['icon'] = $file_icon; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
// Add pro widget class for styling |
| 167 |
$widget_data['icon'] .= ' jltma-pro-widget'; |
| 168 |
|
| 169 |
$pro_widgets[] = $widget_data; |
| 170 |
} |
| 171 |
|
| 172 |
return $pro_widgets; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Promote Pro Addons in Elementor Editor |
| 177 |
* |
| 178 |
* @param array $config Elementor localize settings config |
| 179 |
* @return array Modified config |
| 180 |
*/ |
| 181 |
public function promote_pro_addons($config) |
| 182 |
{ |
| 183 |
$promotion_widgets = []; |
| 184 |
|
| 185 |
if (isset($config['promotionWidgets'])) { |
| 186 |
$promotion_widgets = $config['promotionWidgets']; |
| 187 |
} |
| 188 |
|
| 189 |
// Get pro elements dynamically |
| 190 |
$pro_elements = $this->get_pro_elements(); |
| 191 |
|
| 192 |
if (!empty($pro_elements)) { |
| 193 |
$promotion_widgets = array_merge($promotion_widgets, $pro_elements); |
| 194 |
} |
| 195 |
|
| 196 |
$config['promotionWidgets'] = $promotion_widgets; |
| 197 |
|
| 198 |
return $config; |
| 199 |
} |
| 200 |
} |
| 201 |
|