From c5b96055f013b04b27e73b1c615fe62c8d40235f Mon Sep 17 00:00:00 2001 From: Stephen Cameron Date: Thu, 29 Jul 2021 18:20:19 +0200 Subject: [PATCH] Adjustments to Cube Translate for Elementor. Addresses issue where IDs were sometimes incorrectly cast as integers instead of strings. Wait #3211 @2 --- src/Elementor/Translation.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Elementor/Translation.php b/src/Elementor/Translation.php index ba11be4..608f982 100644 --- a/src/Elementor/Translation.php +++ b/src/Elementor/Translation.php @@ -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); -- 2.39.5