class-wpml-custom-field-xml-settings-import.php
3.54 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
<?php
use WPML\FP\Obj;
class WPML_Custom_Field_XML_Settings_Import {
/** @var WPML_Custom_Field_Setting_Factory $setting_factory */
private $setting_factory;
/** @var array $settings_array */
private $settings_array;
/**
* WPML_Custom_Field_XML_Settings_Import constructor.
*
* @param WPML_Custom_Field_Setting_Factory $setting_factory
* @param array $settings_array
*/
public function __construct( $setting_factory, $settings_array ) {
$this->setting_factory = $setting_factory;
$this->settings_array = $settings_array;
}
/**
* Runs the actual import of the xml
*/
public function run() {
$config = $this->settings_array;
foreach (
array(
'post_meta_setting' => array(
WPML_POST_META_CONFIG_INDEX_PLURAL,
WPML_POST_META_CONFIG_INDEX_SINGULAR
),
'term_meta_setting' => array(
WPML_TERM_META_CONFIG_INDEX_PLURAL,
WPML_TERM_META_CONFIG_INDEX_SINGULAR
)
) as $setting_constructor => $settings
) {
if ( ! empty( $config[ $settings[0] ] ) ) {
$field = $config[ $settings[0] ][ $settings[1] ];
$cf = ! is_numeric( key( current( $config[ $settings[0] ] ) ) ) ? array( $field ) : $field;
foreach ( $cf as $c ) {
$setting = call_user_func_array( array(
$this->setting_factory,
$setting_constructor
), array( trim( $c['value'] ) ) );
$this->import_action( $c, $setting );
$setting->make_read_only();
$this->import_editor_settings( $c, $setting );
if ( isset( $c[ 'attr' ][ 'translate_link_target' ] ) || isset( $c[ 'custom-field' ] ) ) {
$setting->set_translate_link_target( isset( $c[ 'attr' ][ 'translate_link_target' ] ) ? (bool) $c[ 'attr' ][ 'translate_link_target' ] : false, isset( $c[ 'custom-field' ] ) ? $c[ 'custom-field' ] : array() );
}
if ( isset( $c[ 'attr' ][ 'convert_to_sticky' ] ) ) {
$setting->set_convert_to_sticky( (bool) $c[ 'attr' ][ 'convert_to_sticky' ] );
}
$setting->set_encoding( Obj::path( [ 'attr', 'encoding' ], $c ) );
}
}
}
$this->import_custom_field_texts();
}
private function import_action( $c, $setting ) {
if ( ! $setting->is_unlocked() ) {
switch ( $c['attr']['action'] ) {
case 'translate':
$setting->set_to_translatable();
break;
case 'copy':
$setting->set_to_copy();
break;
case 'copy-once':
$setting->set_to_copy_once();
break;
default:
$setting->set_to_nothing();
break;
}
}
}
private function import_editor_settings( $c, $setting ) {
if ( isset( $c[ 'attr' ][ 'style' ] ) ) {
$setting->set_editor_style( $c[ 'attr' ][ 'style' ] );
}
if ( isset( $c[ 'attr' ][ 'label' ] ) ) {
$setting->set_editor_label( $c[ 'attr' ][ 'label' ] );
}
if ( isset( $c[ 'attr' ][ 'group' ] ) ) {
$setting->set_editor_group( $c[ 'attr' ][ 'group' ] );
}
}
private function import_custom_field_texts() {
$config = $this->settings_array;
if ( isset( $config['custom-fields-texts']['key'] ) ) {
foreach( $config['custom-fields-texts']['key'] as $field ) {
$setting = $this->setting_factory->post_meta_setting( $field['attr']['name'] );
$setting->set_attributes_whitelist( $this->get_custom_field_texts_keys( $field['key'] ) );
}
}
}
private function get_custom_field_texts_keys( $data ) {
if ( isset( $data['attr'] ) ) { // single
$data = array( $data );
}
$sub_fields = array();
foreach( $data as $key ) {
$sub_fields[ $key['attr']['name'] ] = isset( $key['key'] ) ? $this->get_custom_field_texts_keys( $key['key'] ) : array();
}
return $sub_fields;
}
}