ModuleWithItems.php
2.47 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
<?php
namespace WPML\PB\SiteOrigin\Modules;
use WPML\FP\Obj;
abstract class ModuleWithItems implements \IWPML_Page_Builders_Module {
/**
* @param string $field
*
* @return string
*/
abstract protected function get_title( $field );
/** @return array */
abstract protected function get_fields();
/**
* @param string $field
*
* @return string
*/
abstract protected function get_editor_type( $field );
/**
* @return string
*/
abstract public function get_items_field();
/**
* @param mixed $settings
*
* @return array
*/
abstract public function get_items( $settings );
/**
* @param string|int $node_id
* @param mixed $settings
* @param \WPML_PB_String[] $strings
*
* @return \WPML_PB_String[]
*/
public function get( $node_id, $settings, $strings ) {
foreach ( $this->get_items( $settings ) as $key => $item ) {
foreach( $this->get_fields() as $field ) {
$pathInFlatField = explode( '>', $field );
$string_value = Obj::path( $pathInFlatField, $item );
if ( $string_value ) {
$strings[] = new \WPML_PB_String(
$string_value,
$this->get_string_name( $node_id, $this->get_items_field(), $key, $field ),
$this->get_title( $field ),
$this->get_editor_type( $field )
);
}
}
}
return $strings;
}
/**
* @param string|int $node_id
* @param mixed $element
* @param \WPML_PB_String $string
*
* @return array
*/
public function update( $node_id, $element, \WPML_PB_String $string ) {
foreach ( $this->get_items( $element ) as $key => $item ) {
foreach( $this->get_fields() as $field ) {
if ( $this->get_string_name( $node_id, $this->get_items_field(), $key, $field ) == $string->get_name() ) {
$pathInFlatField = explode( '>', $field );
$stringInFlatField = Obj::path( $pathInFlatField, $item );
if ( is_string( $stringInFlatField ) ) {
$item = Obj::assocPath( $pathInFlatField, $string->get_value(), $item );
}
return [ $key, $item ];
}
}
}
return [];
}
private function get_string_name( $node_id, $type, $key, $field ) {
return $node_id . '-' . $type . '-' . $key . '-' . $field;
}
/**
* @param string $key
*
* @return array
*/
public function get_field_path( $key ) {
$path = $this->get_items_field();
if ( strpos( $path, '>' ) ) {
list( $parent, $path ) = explode( '>', $path, 2 );
list( $x, $y ) = explode( '>', $key, 2 );
return [ $parent, $x, $path, $y ];
} else {
return [ $path, $key ];
}
}
}