Parser.php
3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace WPML\PB\Config;
use WPML\FP\Fns;
use WPML\FP\Lst;
use WPML\FP\Maybe;
use WPML\FP\Obj;
class Parser {
/** @var string $configRoot */
private $configRoot;
/** @var string $defaultConditionKey */
private $defaultConditionKey;
public function __construct( $configRoot, $defaultConditionKey ) {
$this->configRoot = $configRoot;
$this->defaultConditionKey = $defaultConditionKey;
}
/**
* Receives a raw config array (from XML) and convert it into
* a page builder configuration array.
*
* @see WPML_Elementor_Translatable_Nodes::get_nodes_to_translate()
*
* @param array $allConfig
*
* @return array
*/
public function extract( array $allConfig ) {
$pbConfig = [];
$allWidgets = Obj::pathOr( [], [ 'wpml-config', $this->configRoot, 'widget' ], $allConfig );
foreach ( $allWidgets as $widget ) {
$widgetName = Obj::path( ['attr', 'name'], $widget );
$pbConfig[ $widgetName ] = [
'conditions' => $this->parseConditions( $widget, $widgetName ),
'fields' => $this->parseFields( Obj::pathOr( [], ['fields', 'field'], $widget ) ),
];
$fieldsInItems = Obj::prop( 'fields-in-item', $widget );
if ( $fieldsInItems ) {
$pbConfig[ $widgetName ]['fields_in_item'] = [];
$fieldsInItems = $this->normalize( $fieldsInItems );
foreach ( $fieldsInItems as $fieldsInItem ) {
$itemOf = Obj::path( [ 'attr', 'items_of' ], $fieldsInItem );
$pbConfig[ $widgetName ]['fields_in_item'][ $itemOf ] = $this->parseFields( Obj::propOr( [], 'field', $fieldsInItem ) );
}
}
$integrationClasses = $this->parseIntegrationClasses( $widget );
if ( $integrationClasses ) {
$pbConfig[ $widgetName ]['integration-class'] = $integrationClasses;
}
}
return $pbConfig;
}
/**
* @param array $widget
* @param string $widgetName
*
* @return array
*/
private function parseConditions( array $widget, $widgetName ) {
$makePair = function( $condition ) {
return [ Obj::pathOr( $this->defaultConditionKey, ['attr', 'key'], $condition ), $condition['value'] ];
};
return Maybe::fromNullable( Obj::path( ['conditions', 'condition'], $widget ) )
->map( [ $this, 'normalize' ] )
->map( Fns::map( $makePair ) )
->map( Lst::fromPairs() )
->getOrElse( [ $this->defaultConditionKey => $widgetName ] );
}
/**
* @param array $rawFields
*
* @return array
*/
private function parseFields( array $rawFields ) {
$parsedFields = [];
foreach ( $this->normalize( $rawFields ) as $field ) {
$key = Obj::path( [ 'attr', 'key_of' ], $field );
$fieldId = Obj::path( [ 'attr', 'field_id' ], $field );
$parsedField = [
'field' => $field['value'],
'type' => Obj::pathOr( $field['value'], [ 'attr', 'type' ], $field ),
'editor_type' => Obj::pathOr( 'LINE', [ 'attr', 'editor_type' ], $field ),
];
if ( $fieldId ) {
$parsedField['field_id'] = $fieldId;
}
if ( $key ) {
$parsedFields[ $key ] = $parsedField;
} else {
$parsedFields[] = $parsedField;
}
}
return $parsedFields;
}
/**
* @param array $widget
*
* @return array
*/
private function parseIntegrationClasses( array $widget ) {
return Maybe::fromNullable( Obj::path( [ 'integration-classes', 'integration-class' ], $widget ) )
->map( [ $this, 'normalize' ] )
->map( Lst::pluck( 'value' ) )
->getOrElse( [] );
}
/**
* If a sequence has only one element, we will wrap it
* in order to have the same data shape as for multiple elements.
*
* @param array $partialConfig
*
* @return array
*/
public function normalize( array $partialConfig ) {
$isAssocArray = count( array_filter( array_keys( $partialConfig ), 'is_string' ) ) > 0;
return $isAssocArray ? [ $partialConfig ] : $partialConfig;
}
}