v2.0.0: Elementor data layer (no Freemius, no pro gate)
This commit is contained in:
@@ -0,0 +1,123 @@
|
|||||||
|
<?php
|
||||||
|
namespace AskTamMCP\Elementor\Data;
|
||||||
|
defined('ABSPATH') || exit;
|
||||||
|
|
||||||
|
class ElementorData {
|
||||||
|
|
||||||
|
public function get_document(int $post_id) {
|
||||||
|
if (!class_exists('\Elementor\Plugin')) {
|
||||||
|
return new \WP_Error('elementor_missing', 'Elementor is not installed or active.');
|
||||||
|
}
|
||||||
|
$document = \Elementor\Plugin::$instance->documents->get($post_id);
|
||||||
|
if (!$document) {
|
||||||
|
return new \WP_Error('document_not_found', "Elementor document not found for post ID {$post_id}.");
|
||||||
|
}
|
||||||
|
return $document;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_page_data(int $post_id) {
|
||||||
|
$document = $this->get_document($post_id);
|
||||||
|
if (is_wp_error($document)) return $document;
|
||||||
|
$data = $document->get_elements_data();
|
||||||
|
if (is_array($data) && !empty($data)) return $data;
|
||||||
|
$raw = get_post_meta($post_id, '_elementor_data', true);
|
||||||
|
if (!empty($raw) && is_string($raw)) {
|
||||||
|
$decoded = json_decode($raw, true);
|
||||||
|
if (is_array($decoded)) return $decoded;
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function save_page_data(int $post_id, array $data) {
|
||||||
|
$document = $this->get_document($post_id);
|
||||||
|
if (is_wp_error($document)) return $document;
|
||||||
|
$document->save(['elements' => $data]);
|
||||||
|
update_post_meta($post_id, '_elementor_data', wp_json_encode($data));
|
||||||
|
update_post_meta($post_id, '_elementor_edit_mode', 'builder');
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_page_settings(int $post_id) {
|
||||||
|
$document = $this->get_document($post_id);
|
||||||
|
if (is_wp_error($document)) return $document;
|
||||||
|
return $document->get_settings();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function save_page_settings(int $post_id, array $settings) {
|
||||||
|
$document = $this->get_document($post_id);
|
||||||
|
if (is_wp_error($document)) return $document;
|
||||||
|
$document->save(['settings' => $settings]);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_registered_widgets(): array {
|
||||||
|
if (!class_exists('\Elementor\Plugin')) return [];
|
||||||
|
$widgets = \Elementor\Plugin::$instance->widgets_manager->get_widget_types();
|
||||||
|
$result = [];
|
||||||
|
foreach ($widgets as $widget) {
|
||||||
|
$result[] = ['name' => $widget->get_name(), 'title' => $widget->get_title()];
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function generate_id(): string {
|
||||||
|
return substr(md5(uniqid('', true)), 0, 7);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function reassign_ids(array $elements): array {
|
||||||
|
foreach ($elements as &$element) {
|
||||||
|
$element['id'] = $this->generate_id();
|
||||||
|
if (!empty($element['elements'])) {
|
||||||
|
$element['elements'] = $this->reassign_ids($element['elements']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $elements;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function count_elements(array $elements): int {
|
||||||
|
$count = count($elements);
|
||||||
|
foreach ($elements as $element) {
|
||||||
|
if (!empty($element['elements'])) {
|
||||||
|
$count += $this->count_elements($element['elements']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $count;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function find_element(array $elements, string $element_id): ?array {
|
||||||
|
foreach ($elements as $element) {
|
||||||
|
if (($element['id'] ?? '') === $element_id) return $element;
|
||||||
|
if (!empty($element['elements'])) {
|
||||||
|
$found = $this->find_element($element['elements'], $element_id);
|
||||||
|
if ($found) return $found;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function remove_element(array $elements, string $element_id): array {
|
||||||
|
$result = [];
|
||||||
|
foreach ($elements as $element) {
|
||||||
|
if (($element['id'] ?? '') === $element_id) continue;
|
||||||
|
if (!empty($element['elements'])) {
|
||||||
|
$element['elements'] = $this->remove_element($element['elements'], $element_id);
|
||||||
|
}
|
||||||
|
$result[] = $element;
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update_element_settings(array $elements, string $element_id, array $settings, bool $merge = true): array {
|
||||||
|
foreach ($elements as &$element) {
|
||||||
|
if (($element['id'] ?? '') === $element_id) {
|
||||||
|
$current = $element['settings'] ?? [];
|
||||||
|
$element['settings'] = $merge ? array_merge($current, $settings) : $settings;
|
||||||
|
return $elements;
|
||||||
|
}
|
||||||
|
if (!empty($element['elements'])) {
|
||||||
|
$element['elements'] = $this->update_element_settings($element['elements'], $element_id, $settings, $merge);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $elements;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user