class-wpml-beaver-builder-module-with-items.php
2.41 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
<?php
/**
* Class WPML_Beaver_Builder_Module_With_Items
*/
abstract class WPML_Beaver_Builder_Module_With_Items implements IWPML_Page_Builders_Module {
/**
* @param string $field
*
* @return string
*/
abstract protected function get_title( $field );
/** @return array */
protected function get_fields() {
return array( 'label', 'content' );
}
/**
* @param string $field
*
* @return string
*/
protected function get_editor_type( $field ) {
switch( $field ) {
case 'label':
return 'LINE';
case 'content':
return 'VISUAL';
default:
return '';
}
}
/**
* @param object $settings
*
* @return array
*/
protected function &get_items( $settings ) {
return $settings->items;
}
/**
* @param string|int $node_id
* @param object $settings
* @param WPML_PB_String[] $strings
*
* @return WPML_PB_String[]
*/
public function get( $node_id, $settings, $strings ) {
foreach ( $this->get_items( $settings ) as $item ) {
foreach( $this->get_fields() as $field ) {
if ( is_array( $item->$field ) ) {
foreach ( $item->$field as $key => $value ) {
$strings[] = new WPML_PB_String(
$value,
$this->get_string_name( $node_id, $value, $field, $key ),
$this->get_title( $field ),
$this->get_editor_type( $field )
);
}
} else {
$strings[] = new WPML_PB_String(
$item->$field,
$this->get_string_name( $node_id, $item->$field, $field ),
$this->get_title( $field ),
$this->get_editor_type( $field )
);
}
}
}
return $strings;
}
/**
* @param string|int $node_id
* @param object $settings
* @param WPML_PB_String $string
*
* @return null
*/
public function update( $node_id, $settings, WPML_PB_String $string ) {
foreach ( $this->get_items( $settings ) as &$item ) {
foreach( $this->get_fields() as $field ) {
if ( is_array( $item->$field ) ) {
foreach ( $item->$field as $key => &$value ) {
if ( $this->get_string_name( $node_id, $value, $field, $key ) == $string->get_name() ) {
$value = $string->get_value();
}
}
} else {
if ( $this->get_string_name( $node_id, $item->$field, $field ) == $string->get_name() ) {
$item->$field = $string->get_value();
}
}
}
}
return null;
}
private function get_string_name( $node_id, $value, $type, $key = '' ) {
return md5( $value ) . '-' . $type . $key . '-' . $node_id;
}
}