From 87662a41162f149da7fa2e9491f1d1b4e1945015 Mon Sep 17 00:00:00 2001 From: Stephen Cameron Date: Thu, 14 Oct 2021 23:49:11 +0200 Subject: [PATCH] Make Cube Translate plugin more robust when spreadsheet data is inconsistent. WIP #3211 @2 --- src/Elementor/Translation.php | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/Elementor/Translation.php b/src/Elementor/Translation.php index 608f982..ce4bf20 100644 --- a/src/Elementor/Translation.php +++ b/src/Elementor/Translation.php @@ -184,7 +184,7 @@ class Translation 'cube-link-carousel' => [ 'links' => [ 'title' => self::TYPE_TEXT, - 'url' => self::TYPE_TEXT, + 'link.url' => self::TYPE_TEXT, ], ], 'cube-photo-grid' => [ @@ -436,6 +436,12 @@ class Translation $widget_type = trim($row['widget']); $field_name = trim($row['field']); $content = trim($row['translation']); + + // Fallback to using content in original column in case translation hasn't been filled in + if (empty($content) && key_exists('content', $row)) { + $content = $row['content']; + } + $this->update_element($this->data, $element_ID, $widget_type, $field_name, $content); } @@ -488,7 +494,14 @@ class Translation preg_match('/(?[a-z0-9\-_\.]+)(?:#(?[a-z0-9]+))?(?:\.(?[a-z\-_\.]+))?/', $field_name, $matches); extract($matches); // For easier access to $field, $id and $subfield - if (empty($field)) return $element; // Bail out if at least the field isn't found + // Convert to Arrays object so we can check for field names using dot notation (which the regex might return) + // Keeping the dot notation references and using the helper functions simplifies the code here + $settings_data = Arrays::create($element['settings']); + + // Bail out if we don't find a valid field name OR if the data is non-existent + if (empty($field) || !$settings_data->has($field)) { + return $element; + } //=== Find the field and update the value according to field pattern above if (!empty($id) && !empty($subfield)) { // Repeater item (field name has both ID and subfield present) @@ -518,14 +531,17 @@ class Translation protected function has_changed($old_value, $new_value) { - if ($old_value === $new_value) { + // Bail out if no change OR if old_value is an array -- it's expected to be a string + // NOTE: this shouldn't normally occur but it's possible if widget data structures have been updated and the + // spreadsheet still has a reference to the old name / value (Elementor won't delete previously saved data) + if ($old_value === $new_value || is_array($old_value)) { return false; } // Log the changes $this->updated_content[] = [ - 'old' => $old_value, - 'new' => $new_value, + 'old' => (string)$old_value, + 'new' => (string)$new_value, ]; return true; -- 2.39.5