| 1 |
<?php |
| 2 |
|
| 3 |
namespace MasterAddons\Inc\Admin\Templates\Includes\Sources; |
| 4 |
|
| 5 |
use MasterAddons\Inc\Admin\Templates; |
| 6 |
|
| 7 |
if (!defined('ABSPATH')) { |
| 8 |
exit; // Exit if accessed directly. |
| 9 |
} |
| 10 |
|
| 11 |
class Local extends Base |
| 12 |
{ |
| 13 |
|
| 14 |
private $_object_cache = array(); |
| 15 |
|
| 16 |
public function get_slug() |
| 17 |
{ |
| 18 |
return 'master-addons'; |
| 19 |
} |
| 20 |
|
| 21 |
public function get_version() |
| 22 |
{ |
| 23 |
return '1.0.0'; |
| 24 |
} |
| 25 |
|
| 26 |
public function get_items($tab = null) |
| 27 |
{ |
| 28 |
if (!$tab) { |
| 29 |
return array(); |
| 30 |
} |
| 31 |
|
| 32 |
$cached = $this->get_templates_cache(); |
| 33 |
|
| 34 |
if (!empty($cached[$tab])) { |
| 35 |
return array_values($cached[$tab]); |
| 36 |
} |
| 37 |
|
| 38 |
$templates = $this->get_local_templates($tab); |
| 39 |
|
| 40 |
if (!$templates) { |
| 41 |
return array(); |
| 42 |
} |
| 43 |
|
| 44 |
if (empty($cached)) { |
| 45 |
$cached = array(); |
| 46 |
} |
| 47 |
|
| 48 |
$cached[$tab] = $templates; |
| 49 |
|
| 50 |
$this->set_templates_cache($cached); |
| 51 |
|
| 52 |
return $templates; |
| 53 |
} |
| 54 |
|
| 55 |
public function get_local_templates($tab) |
| 56 |
{ |
| 57 |
// Get local templates from WordPress database |
| 58 |
$args = array( |
| 59 |
'post_type' => 'elementor_library', |
| 60 |
'post_status' => 'publish', |
| 61 |
'posts_per_page' => -1, |
| 62 |
'meta_query' => array( |
| 63 |
array( |
| 64 |
'key' => '_elementor_template_type', |
| 65 |
'value' => $this->map_tab_to_type($tab), |
| 66 |
'compare' => '=' |
| 67 |
) |
| 68 |
) |
| 69 |
); |
| 70 |
|
| 71 |
$query = new \WP_Query($args); |
| 72 |
$templates = array(); |
| 73 |
|
| 74 |
if ($query->have_posts()) { |
| 75 |
while ($query->have_posts()) { |
| 76 |
$query->the_post(); |
| 77 |
$post_id = \get_the_ID(); |
| 78 |
|
| 79 |
$templates[] = array( |
| 80 |
'template_id' => $this->id_prefix() . $post_id, |
| 81 |
'title' => \get_the_title(), |
| 82 |
'type' => \get_post_meta($post_id, '_elementor_template_type', true), |
| 83 |
'source' => $this->get_slug(), |
| 84 |
'thumbnail' => \get_the_post_thumbnail_url($post_id, 'medium'), |
| 85 |
'date' => \get_the_date(), |
| 86 |
'author' => \get_the_author() |
| 87 |
); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
\wp_reset_postdata(); |
| 92 |
|
| 93 |
return $templates; |
| 94 |
} |
| 95 |
|
| 96 |
public function map_tab_to_type($tab) |
| 97 |
{ |
| 98 |
// Map tab names to Elementor template types |
| 99 |
$mapping = array( |
| 100 |
'master_pages' => 'section', |
| 101 |
'master_headers' => 'header', |
| 102 |
'master_footers' => 'footer' |
| 103 |
); |
| 104 |
|
| 105 |
return isset($mapping[$tab]) ? $mapping[$tab] : 'section'; |
| 106 |
} |
| 107 |
|
| 108 |
public function get_categories($tab = null) |
| 109 |
{ |
| 110 |
if (!$tab) { |
| 111 |
return array(); |
| 112 |
} |
| 113 |
|
| 114 |
$cached = $this->get_categories_cache(); |
| 115 |
|
| 116 |
if (!empty($cached[$tab])) { |
| 117 |
return $this->prepare_categories($cached[$tab]); |
| 118 |
} |
| 119 |
|
| 120 |
$categories = $this->get_local_categories($tab); |
| 121 |
|
| 122 |
if (!$categories) { |
| 123 |
return array(); |
| 124 |
} |
| 125 |
|
| 126 |
if (empty($cached)) { |
| 127 |
$cached = array(); |
| 128 |
} |
| 129 |
|
| 130 |
$cached[$tab] = $categories; |
| 131 |
|
| 132 |
$this->set_categories_cache($cached); |
| 133 |
|
| 134 |
return $this->prepare_categories($categories); |
| 135 |
} |
| 136 |
|
| 137 |
public function get_local_categories($tab) |
| 138 |
{ |
| 139 |
// Get categories from local templates |
| 140 |
$args = array( |
| 141 |
'post_type' => 'elementor_library', |
| 142 |
'post_status' => 'publish', |
| 143 |
'posts_per_page' => -1, |
| 144 |
'meta_query' => array( |
| 145 |
array( |
| 146 |
'key' => '_elementor_template_type', |
| 147 |
'value' => $this->map_tab_to_type($tab), |
| 148 |
'compare' => '=' |
| 149 |
) |
| 150 |
) |
| 151 |
); |
| 152 |
|
| 153 |
$query = new \WP_Query($args); |
| 154 |
$categories = array(); |
| 155 |
|
| 156 |
if ($query->have_posts()) { |
| 157 |
while ($query->have_posts()) { |
| 158 |
$query->the_post(); |
| 159 |
$post_id = \get_the_ID(); |
| 160 |
|
| 161 |
$template_categories = \wp_get_post_terms($post_id, 'elementor_library_category', array('fields' => 'all')); |
| 162 |
|
| 163 |
if (is_array($template_categories) && !is_wp_error($template_categories)) { |
| 164 |
foreach ($template_categories as $category) { |
| 165 |
if (is_object($category) && isset($category->slug) && isset($category->name)) { |
| 166 |
if (!isset($categories[$category->slug])) { |
| 167 |
$categories[$category->slug] = $category->name; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
\wp_reset_postdata(); |
| 176 |
|
| 177 |
return $categories; |
| 178 |
} |
| 179 |
|
| 180 |
public function prepare_categories($categories) |
| 181 |
{ |
| 182 |
$result = array(); |
| 183 |
|
| 184 |
foreach ($categories as $slug => $title) { |
| 185 |
$result[] = array( |
| 186 |
'slug' => $slug, |
| 187 |
'title' => $title, |
| 188 |
); |
| 189 |
} |
| 190 |
|
| 191 |
return $result; |
| 192 |
} |
| 193 |
|
| 194 |
public function get_keywords($tab = null) |
| 195 |
{ |
| 196 |
if (!$tab) { |
| 197 |
return array(); |
| 198 |
} |
| 199 |
|
| 200 |
$cached = $this->get_keywords_cache(); |
| 201 |
|
| 202 |
if (!empty($cached[$tab])) { |
| 203 |
return $cached[$tab]; |
| 204 |
} |
| 205 |
|
| 206 |
$keywords = $this->get_local_keywords($tab); |
| 207 |
|
| 208 |
if (!$keywords) { |
| 209 |
return array(); |
| 210 |
} |
| 211 |
|
| 212 |
if (empty($cached)) { |
| 213 |
$cached = array(); |
| 214 |
} |
| 215 |
|
| 216 |
$cached[$tab] = $keywords; |
| 217 |
|
| 218 |
$this->set_keywords_cache($cached); |
| 219 |
|
| 220 |
return $keywords; |
| 221 |
} |
| 222 |
|
| 223 |
public function get_local_keywords($tab) |
| 224 |
{ |
| 225 |
// Get keywords from local templates |
| 226 |
$args = array( |
| 227 |
'post_type' => 'elementor_library', |
| 228 |
'post_status' => 'publish', |
| 229 |
'posts_per_page' => -1, |
| 230 |
'meta_query' => array( |
| 231 |
array( |
| 232 |
'key' => '_elementor_template_type', |
| 233 |
'value' => $this->map_tab_to_type($tab), |
| 234 |
'compare' => '=' |
| 235 |
) |
| 236 |
) |
| 237 |
); |
| 238 |
|
| 239 |
$query = new \WP_Query($args); |
| 240 |
$keywords = array(); |
| 241 |
|
| 242 |
if ($query->have_posts()) { |
| 243 |
while ($query->have_posts()) { |
| 244 |
$query->the_post(); |
| 245 |
$post_id = \get_the_ID(); |
| 246 |
|
| 247 |
$template_keywords = \wp_get_post_terms($post_id, 'elementor_library_tag', array('fields' => 'all')); |
| 248 |
|
| 249 |
if (is_array($template_keywords) && !is_wp_error($template_keywords)) { |
| 250 |
foreach ($template_keywords as $keyword) { |
| 251 |
if (is_object($keyword) && isset($keyword->name)) { |
| 252 |
if (!in_array($keyword->name, $keywords)) { |
| 253 |
$keywords[] = $keyword->name; |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
\wp_reset_postdata(); |
| 262 |
|
| 263 |
return $keywords; |
| 264 |
} |
| 265 |
|
| 266 |
public function get_item($template_id, $tab = false) |
| 267 |
{ |
| 268 |
$id = str_replace($this->id_prefix(), '', $template_id); |
| 269 |
|
| 270 |
if (!$tab) { |
| 271 |
$tab = isset($_REQUEST['tab']) ? sanitize_key($_REQUEST['tab']) : false; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only tab parameter for template display |
| 272 |
} |
| 273 |
|
| 274 |
$post = \get_post($id); |
| 275 |
|
| 276 |
if (!$post || $post->post_type !== 'elementor_library') { |
| 277 |
return false; |
| 278 |
} |
| 279 |
|
| 280 |
$elementor_data = \get_post_meta($id, '_elementor_data', true); |
| 281 |
$template_type = \get_post_meta($id, '_elementor_template_type', true); |
| 282 |
|
| 283 |
if (empty($elementor_data)) { |
| 284 |
return false; |
| 285 |
} |
| 286 |
|
| 287 |
$content = json_decode($elementor_data, true); |
| 288 |
|
| 289 |
if (!empty($content)) { |
| 290 |
$content = $this->replace_elements_ids($content); |
| 291 |
$content = $this->process_export_import_content($content, 'on_import'); |
| 292 |
} |
| 293 |
|
| 294 |
return array( |
| 295 |
'page_settings' => array(), |
| 296 |
'type' => $template_type, |
| 297 |
'license' => 'local', |
| 298 |
'content' => $content |
| 299 |
); |
| 300 |
} |
| 301 |
|
| 302 |
// Required methods from base class |
| 303 |
public function replace_elements_ids($content) |
| 304 |
{ |
| 305 |
// Simple implementation - can be enhanced later |
| 306 |
return $content; |
| 307 |
} |
| 308 |
|
| 309 |
public function process_export_import_content($content, $method) |
| 310 |
{ |
| 311 |
// Simple implementation - can be enhanced later |
| 312 |
return $content; |
| 313 |
} |
| 314 |
|
| 315 |
public function id_prefix() |
| 316 |
{ |
| 317 |
return 'local_'; |
| 318 |
} |
| 319 |
|
| 320 |
public function transient_lifetime() |
| 321 |
{ |
| 322 |
return HOUR_IN_SECONDS; // Shorter cache for local templates |
| 323 |
} |
| 324 |
} |
| 325 |
|