fix: bypass WP object cache in CLI/cron via raw $wpdb queries for elementor data persistence

This commit is contained in:
2026-05-29 15:30:07 +00:00
parent cd6b16c870
commit 4006e7462f
+49 -2
View File
@@ -16,6 +16,19 @@ class ElementorData {
}
public function get_page_data(int $post_id) {
if (PHP_SAPI === 'cli' || (defined('DOING_CRON') && DOING_CRON)) {
// Read directly from DB — bypasses WP object cache entirely
global $wpdb;
$raw = $wpdb->get_var($wpdb->prepare(
"SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id=%d AND meta_key='_elementor_data' LIMIT 1",
$post_id
));
if (!empty($raw) && is_string($raw)) {
$decoded = json_decode($raw, true);
if (is_array($decoded)) return $decoded;
}
return [];
}
$document = $this->get_document($post_id);
if (is_wp_error($document)) return $document;
$data = $document->get_elements_data();
@@ -29,6 +42,40 @@ class ElementorData {
}
public function save_page_data(int $post_id, array $data) {
if (PHP_SAPI === 'cli' || (defined('DOING_CRON') && DOING_CRON)) {
// Bypass WP meta cache entirely using raw $wpdb queries.
// WP object cache causes stale reads in long-running CLI scripts where
// multiple add/delete calls within the same process leave the cache inconsistent.
global $wpdb;
$json = wp_json_encode($data);
$table = $wpdb->postmeta;
$exists = $wpdb->get_var($wpdb->prepare(
"SELECT meta_id FROM {$table} WHERE post_id=%d AND meta_key='_elementor_data' LIMIT 1",
$post_id
));
if ($exists) {
$wpdb->query($wpdb->prepare(
"UPDATE {$table} SET meta_value=%s WHERE meta_id=%d",
$json, (int)$exists
));
} else {
$wpdb->query($wpdb->prepare(
"INSERT INTO {$table} (post_id, meta_key, meta_value) VALUES (%d, '_elementor_data', %s)",
$post_id, $json
));
}
$modeExists = $wpdb->get_var($wpdb->prepare(
"SELECT meta_id FROM {$table} WHERE post_id=%d AND meta_key='_elementor_edit_mode' LIMIT 1",
$post_id
));
if ($modeExists) {
$wpdb->query($wpdb->prepare("UPDATE {$table} SET meta_value='builder' WHERE meta_id=%d", (int)$modeExists));
} else {
$wpdb->query($wpdb->prepare("INSERT INTO {$table} (post_id, meta_key, meta_value) VALUES (%d, '_elementor_edit_mode', 'builder')", $post_id));
}
wp_cache_delete($post_id, 'post_meta');
return true;
}
$document = $this->get_document($post_id);
if (is_wp_error($document)) return $document;
$document->save(['elements' => $data]);
@@ -53,7 +100,7 @@ class ElementorData {
public function get_registered_widgets(): array {
if (!class_exists('\Elementor\Plugin')) return [];
$widgets = \Elementor\Plugin::$instance->widgets_manager->get_widget_types();
$result = [];
$result = [];
foreach ($widgets as $widget) {
$result[] = ['name' => $widget->get_name(), 'title' => $widget->get_title()];
}
@@ -110,7 +157,7 @@ class ElementorData {
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'] ?? [];
$current = $element['settings'] ?? [];
$element['settings'] = $merge ? array_merge($current, $settings) : $settings;
return $elements;
}