]> _ Git - cube-wp-translate.git/commitdiff
Adjustments to Cube Translate for Elementor. Addresses issue where IDs were sometimes...
authorStephen Cameron <stephen@cubedesigners.com>
Thu, 29 Jul 2021 16:20:19 +0000 (18:20 +0200)
committerStephen Cameron <stephen@cubedesigners.com>
Thu, 29 Jul 2021 16:20:19 +0000 (18:20 +0200)
src/Elementor/Translation.php

index ba11be4d10b844755e0cf76a6538a37093b2b0bd..608f9829d5ab6e4b585cb9184f7d09a2c65978a0 100644 (file)
@@ -5,6 +5,7 @@ use Elementor\Plugin;
 use Atomastic\Arrays\Arrays;
 use PhpOffice\PhpSpreadsheet\IOFactory;
 use PhpOffice\PhpSpreadsheet\Spreadsheet;
+use PhpOffice\PhpSpreadsheet\Cell\DataType;
 use PhpOffice\PhpSpreadsheet\Style\Fill;
 use PhpOffice\PhpSpreadsheet\Writer\Html;
 use Jfcherng\Diff\DiffHelper;
@@ -118,6 +119,9 @@ class Translation
                 'text' => self::TYPE_TEXT,
                 'link.url' => self::TYPE_TEXT,
             ],
+            'image' => [
+                'caption' => self::TYPE_TEXT,
+            ],
             'image-box' => [
                 'title_text' => self::TYPE_TEXT,
                 'description_text' => self::TYPE_TEXTAREA,
@@ -145,8 +149,7 @@ class Translation
             'cube-timeline-horizontal' => [
                 'items' => [ // Repeater
                     'title' => self::TYPE_TEXT,
-                    'cta_text' => self::TYPE_TEXT,
-                    'cta_link.url' => self::TYPE_TEXT,
+                    'content' => self::TYPE_HTML,
                 ],
             ],
             'cube-testimonial-carousel' => [
@@ -194,7 +197,6 @@ class Translation
 
         // Widgets that shouldn't be translated
         $this->ignored_widgets = [
-            'image',
             'image-carousel',
             'spacer',
             'google_maps',
@@ -451,7 +453,11 @@ class Translation
         // Search through all data until we find the item we're looking for
         foreach ($data as &$element) {
 
-            if ($element['id'] === $element_ID && isset($element['widgetType']) && $element['widgetType'] === $widget_type) {
+            // ID check is loose because previous translation exports might have had IDs unexpectedly
+            // cast as integers, resulting in mismatches later if comparison is strict.
+            // For example: (string)'482016e2' becomes (int)48201600
+            // See: https://phpspreadsheet.readthedocs.io/en/latest/topics/accessing-cells/#excel-datatypes
+            if ($element['id'] == $element_ID && isset($element['widgetType']) && $element['widgetType'] === $widget_type) {
 
                 // Match found! Now update data...
                 $element = $this->update_field($element, $field_name, $contents);
@@ -564,7 +570,9 @@ class Translation
         for ($i = 0; $i < count($data); $i++) {
             $x = $i + 2; // Offset the row number to allow for header row (i is zero-indexed but rows start from 1)
             $sheet
-                ->setCellValue('A' . $x, $data[$i]['id'])
+                // ID field needs to be explicitly set as a string to avoid possibility of casting as an integer,
+                // which in turn can cause weird PHP conversions to occur (eg. '482016e2' ==> 48201600)
+                ->setCellValueExplicit('A' . $x, $data[$i]['id'], DataType::TYPE_STRING)
                 ->setCellValue('B' . $x, $data[$i]['widget'])
                 ->setCellValue('C' . $x, $data[$i]['field'])
                 ->setCellValue('D' . $x, $data[$i]['content'])->getStyle('D' . $x)->getAlignment()->setWrapText(true);