PluginProbe
Master Addons for Elementor – Elementor Addons, Widgets, Mega Menu Builder, Popup Builder, Widget Builder & Template Kits / 3.1.2
Master Addons for Elementor – Elementor Addons, Widgets, Mega Menu Builder, Popup Builder, Widget Builder & Template Kits v3.1.2
3.2.2 3.2.3 3.2.1 3.2.0 3.1.9 3.1.8 3.1.7 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.9 trunk 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.3 1.1.4 1.1.5 All 174 releases
master-addons / inc / admin / widget-builder / class-shortcode-manager.php

class-shortcode-manager.php in Master Addons for Elementor – Elementor Addons, Widgets, Mega Menu Builder, Popup Builder, Widget Builder & Template Kits 3.1.2, at inc/admin/widget-builder/class-shortcode-manager.php

232 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Master Addons Widget Builder - Shortcode Manager
4 * Registers all custom widgets as shortcodes
5 *
6 * @package MasterAddons
7 * @subpackage WidgetBuilder
8 */
9
10 namespace MasterAddons\Inc\Admin\WidgetBuilder;
11
12 if (!defined('ABSPATH')) {
13 exit;
14 }
15
16 class Shortcode_Manager {
17
18 private static $instance = null;
19 private $widgets = [];
20
21 public static function get_instance() {
22 if (self::$instance === null) {
23 self::$instance = new self();
24 }
25 return self::$instance;
26 }
27
28 public function __construct() {
29 add_action('init', [$this, 'register_shortcodes'], 20);
30 }
31
32 /**
33 * Register all widget shortcodes
34 */
35 public function register_shortcodes() {
36 // Get all published widgets
37 $args = [
38 'post_type' => 'jltma_widget',
39 'post_status' => 'publish',
40 'posts_per_page' => -1,
41 'orderby' => 'date',
42 'order' => 'DESC'
43 ];
44
45 $widgets = get_posts($args);
46
47 if (empty($widgets)) {
48 return;
49 }
50
51 foreach ($widgets as $widget_post) {
52 $widget_id = $widget_post->ID;
53 $shortcode_tag = 'jltma_widget_' . $widget_id;
54
55 // Register shortcode with closure that captures widget ID
56 add_shortcode($shortcode_tag, function($atts, $content = null) use ($widget_id) {
57 return $this->render_widget_shortcode($atts, $content, $widget_id);
58 });
59
60 // Store widget info
61 $this->widgets[$shortcode_tag] = [
62 'id' => $widget_id,
63 'title' => $widget_post->post_title,
64 'tag' => $shortcode_tag
65 ];
66 }
67 }
68
69 /**
70 * Render widget shortcode
71 *
72 * @param array $atts Shortcode attributes
73 * @param string $content Shortcode content
74 * @param int $widget_id Widget post ID
75 * @return string Widget output
76 */
77 public function render_widget_shortcode($atts, $content = null, $widget_id = 0) {
78 if (!$widget_id) {
79 return '';
80 }
81
82 // Load widget file
83 $upload = wp_upload_dir();
84 $widget_file = $upload['basedir'] . '/master_addons/widgets/' . $widget_id . '/widget.php';
85
86 if (!file_exists($widget_file)) {
87 return '';
88 }
89
90 // Start output buffering
91 ob_start();
92
93 try {
94 // Enqueue widget assets
95 $this->enqueue_widget_assets($widget_id);
96
97 // Require widget file if not already loaded
98 require_once $widget_file;
99
100 $class_name = 'MasterAddons\\Addons\\JLTMA_WB_' . $widget_id;
101
102 if (class_exists($class_name)) {
103 // Create widget instance
104 $widget = new $class_name();
105
106 // Prepare settings from shortcode attributes
107 $settings = $this->prepare_settings($widget_id, $atts);
108
109 // Render widget with settings
110 echo '<div class="jltma-widget-shortcode jltma-wb-' . esc_attr($widget_id) . '">';
111
112 // Call the widget's render method with settings
113 $widget->render_shortcode($settings);
114
115 echo '</div>';
116 }
117 } catch (\Exception $e) {
118 if (current_user_can('manage_options')) {
119 echo '<div class="jltma-shortcode-error">' . esc_html($e->getMessage()) . '</div>';
120 }
121 }
122
123 return ob_get_clean();
124 }
125
126 /**
127 * Enqueue widget assets
128 *
129 * @param int $widget_id Widget ID
130 */
131 private function enqueue_widget_assets($widget_id) {
132 $upload = wp_upload_dir();
133 $widget_url = $upload['baseurl'] . '/master_addons/widgets/' . $widget_id;
134 $widget_dir = $upload['basedir'] . '/master_addons/widgets/' . $widget_id;
135
136 // Enqueue CSS
137 if (file_exists($widget_dir . '/style.css')) {
138 wp_enqueue_style(
139 'jltma-wb-' . $widget_id . '-style',
140 $widget_url . '/style.css',
141 [],
142 filemtime($widget_dir . '/style.css')
143 );
144 }
145
146 // Enqueue JS
147 if (file_exists($widget_dir . '/script.js')) {
148 wp_enqueue_script(
149 'jltma-wb-' . $widget_id . '-script',
150 $widget_url . '/script.js',
151 ['jquery'],
152 filemtime($widget_dir . '/script.js'),
153 true
154 );
155 }
156
157 // Enqueue includes from meta
158 $includes = get_post_meta($widget_id, '_jltma_widget_includes', true);
159
160 if (!empty($includes) && is_array($includes)) {
161 // CSS libraries
162 if (!empty($includes['css_libraries']) && is_array($includes['css_libraries'])) {
163 foreach ($includes['css_libraries'] as $css) {
164 if (!empty($css['handle']) && !empty($css['src'])) {
165 $deps = !empty($css['dependencies']) && is_array($css['dependencies']) ? $css['dependencies'] : [];
166 wp_enqueue_style($css['handle'], $css['src'], $deps, JLTMA_VER);
167 }
168 }
169 }
170
171 // JS libraries
172 if (!empty($includes['js_libraries']) && is_array($includes['js_libraries'])) {
173 foreach ($includes['js_libraries'] as $js) {
174 if (!empty($js['handle']) && !empty($js['src'])) {
175 $deps = !empty($js['dependencies']) && is_array($js['dependencies']) ? $js['dependencies'] : [];
176 wp_enqueue_script($js['handle'], $js['src'], $deps, JLTMA_VER, true);
177 }
178 }
179 }
180 }
181 }
182
183 /**
184 * Prepare settings from shortcode attributes
185 *
186 * @param int $widget_id Widget ID
187 * @param array $atts Shortcode attributes
188 * @return array Settings array
189 */
190 private function prepare_settings($widget_id, $atts) {
191 // Get widget sections/controls from meta
192 $sections = get_post_meta($widget_id, '_jltma_widget_sections', true);
193 $settings = [];
194
195 if (!empty($sections) && is_array($sections)) {
196 foreach ($sections as $section) {
197 if (!empty($section['controls']) && is_array($section['controls'])) {
198 foreach ($section['controls'] as $control) {
199 $control_name = isset($control['name']) ? $control['name'] : '';
200 if ($control_name && isset($atts[$control_name])) {
201 $settings[$control_name] = $atts[$control_name];
202 } elseif ($control_name && isset($control['default'])) {
203 $settings[$control_name] = $control['default'];
204 }
205 }
206 }
207 }
208 }
209
210 return $settings;
211 }
212
213 /**
214 * Get all registered shortcodes
215 *
216 * @return array
217 */
218 public function get_registered_shortcodes() {
219 return $this->widgets;
220 }
221
222 /**
223 * Get shortcode tag for widget
224 *
225 * @param int $widget_id Widget ID
226 * @return string Shortcode tag
227 */
228 public static function get_shortcode_tag($widget_id) {
229 return 'jltma_widget_' . $widget_id;
230 }
231 }
232