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
+47
View File
@@ -16,6 +16,19 @@ class ElementorData {
} }
public function get_page_data(int $post_id) { 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); $document = $this->get_document($post_id);
if (is_wp_error($document)) return $document; if (is_wp_error($document)) return $document;
$data = $document->get_elements_data(); $data = $document->get_elements_data();
@@ -29,6 +42,40 @@ class ElementorData {
} }
public function save_page_data(int $post_id, array $data) { 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); $document = $this->get_document($post_id);
if (is_wp_error($document)) return $document; if (is_wp_error($document)) return $document;
$document->save(['elements' => $data]); $document->save(['elements' => $data]);