]> _ Git - cube-wp-translate.git/commitdiff
Make Cube Translate plugin more robust when spreadsheet data is inconsistent. WIP...
authorStephen Cameron <stephen@cubedesigners.com>
Thu, 14 Oct 2021 21:49:11 +0000 (23:49 +0200)
committerStephen Cameron <stephen@cubedesigners.com>
Thu, 14 Oct 2021 21:49:11 +0000 (23:49 +0200)
src/Elementor/Translation.php

index 608f9829d5ab6e4b585cb9184f7d09a2c65978a0..ce4bf2012fcbdc0c8bfd1115a1f0a51428b9e48f 100644 (file)
@@ -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('/(?<field>[a-z0-9\-_\.]+)(?:#(?<id>[a-z0-9]+))?(?:\.(?<subfield>[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;