refactor Elementor support (WP-854)#500
Conversation
add base helper methods and raw array for elements
| namespace Smartling\ContentTypes\Elementor; | ||
|
|
||
| class ElementFactory { | ||
| public const UNKNOWN_ELEMENT = 'unknown'; |
There was a problem hiding this comment.
This is rather the base element, this naming might be confusing, I'm open to propositions
There was a problem hiding this comment.
I think we need two classes.
- The first one is base class (maybe abstract). It just implements reusable methods (like
getRelated) - Another one is
Unknown, it will try to handle anything that was not parsed. I don't say that this class should be smart and extract what other classes missed. It can simply log - "I got this json .....". So initial responsibility will be just alerting us about an unknown element. Later you can add more logic that will try to send this json as is to smartling. But we need to brainstorm this.
There was a problem hiding this comment.
Extracted abstract class
| return is_array($value) && 0 < count($value); | ||
| } | ||
|
|
||
| public function flatten(array $array, string $base = '', string $divider = '/'): array |
There was a problem hiding this comment.
This is different from FieldFilterHelper, this preserves array elements types instead of converting them to string
| $keys = ['background_image/alt', 'text']; | ||
| foreach ($this->elements as $element) { | ||
| if ($element instanceof Element) { | ||
| $return[] = $element->getTranslatableStrings(); |
There was a problem hiding this comment.
Recursively search for strings in nested objects
| { | ||
| foreach ($this->elements as &$element) { | ||
| if ($element instanceof Element) { | ||
| $element = $element->setTargetContent( |
There was a problem hiding this comment.
Recursively set relations and strings in nested objects
dimitrystd
left a comment
There was a problem hiding this comment.
I didn't finish the review.
| namespace Smartling\ContentTypes\Elementor; | ||
|
|
||
| class ElementFactory { | ||
| public const UNKNOWN_ELEMENT = 'unknown'; |
There was a problem hiding this comment.
I think we need two classes.
- The first one is base class (maybe abstract). It just implements reusable methods (like
getRelated) - Another one is
Unknown, it will try to handle anything that was not parsed. I don't say that this class should be smart and extract what other classes missed. It can simply log - "I got this json .....". So initial responsibility will be just alerting us about an unknown element. Later you can add more logic that will try to send this json as is to smartling. But we need to brainstorm this.
| return $return; | ||
| } | ||
|
|
||
| public function getTranslatableStrings(): array |
There was a problem hiding this comment.
I found an example of icon-list in your tests. Why these 4 fields are not mentioned here?
There was a problem hiding this comment.
Those are from some extension, we'll need to handle them at some point of time, but first I'll find out which extension adds these properties, they're present for all objects, but the values everywhere I've seen so far are defaults
| foreach ($this->arrayHelper->flatten($info) as $item) { | ||
| assert($item instanceof Content); | ||
| } | ||
| $this->info = $info; |
There was a problem hiding this comment.
Instead of using the reference, I would recommend creating a new array. You cannot guarantee that the array passed as $info will not be modified by caller code.
There was a problem hiding this comment.
No references are used here.
We ensure that every array element is Content, to avoid issues where __construct is called with an array of something else.
Further changes to $info will not affect the created RelatedContentInfo
| return $this->id; | ||
| } | ||
|
|
||
| public function getIntSettingByKey(string $key, array $settings): ?int |
There was a problem hiding this comment.
Mark all helper methods as protected. Maybe even make them static.
There was a problem hiding this comment.
Made protected
review fixes and changes
8ec3e1a to
6772518
Compare
superseded by \ElementorTest::testElementor
tests/IntegrationTests/testdata/elementor.json
tests/Smartling/ContentTypes/testMergeElementorData.json