Skip to content

refactor Elementor support (WP-854)#500

Merged
vsolovei-smartling merged 15 commits into
elementor-refactorfrom
WP-854-elementor-2
Dec 18, 2023
Merged

refactor Elementor support (WP-854)#500
vsolovei-smartling merged 15 commits into
elementor-refactorfrom
WP-854-elementor-2

Conversation

@vsolovei-smartling

@vsolovei-smartling vsolovei-smartling commented Dec 14, 2023

Copy link
Copy Markdown
Contributor

Comment thread inc/Smartling/ContentTypes/Elementor/Element.php
namespace Smartling\ContentTypes\Elementor;

class ElementFactory {
public const UNKNOWN_ELEMENT = 'unknown';

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is rather the base element, this naming might be confusing, I'm open to propositions

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted abstract class

Comment thread inc/Smartling/ContentTypes/Elementor/Elements/Unknown.php
Comment thread inc/Smartling/Helpers/ArrayHelper.php
return is_array($value) && 0 < count($value);
}

public function flatten(array $array, string $base = '', string $divider = '/'): array

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recursively search for strings in nested objects

{
foreach ($this->elements as &$element) {
if ($element instanceof Element) {
$element = $element->setTargetContent(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recursively set relations and strings in nested objects

@dimitrystd dimitrystd left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't finish the review.

Comment thread inc/Smartling/ContentTypes/Elementor/Elements/Accordion.php Outdated
namespace Smartling\ContentTypes\Elementor;

class ElementFactory {
public const UNKNOWN_ELEMENT = 'unknown';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@vsolovei-smartling vsolovei-smartling changed the base branch from master to elementor-refactor December 15, 2023 14:05
return $return;
}

public function getTranslatableStrings(): array

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found an example of icon-list in your tests. Why these 4 fields are not mentioned here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mark all helper methods as protected. Maybe even make them static.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made protected

Comment thread inc/Smartling/ContentTypes/Elementor/ElementFactory.php Outdated
Comment thread inc/Smartling/ContentTypes/Elementor/ElementFactory.php
@vsolovei-smartling vsolovei-smartling merged commit 126f1b1 into elementor-refactor Dec 18, 2023
@vsolovei-smartling vsolovei-smartling deleted the WP-854-elementor-2 branch December 18, 2023 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants