class-wpml-tm-page-builders-field-wrapper.php
3.9 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
* WPML_TM_Page_Builders_Field_Wrapper class file.
*
* @package wpml-page-builders
*/
/**
* Class WPML_TM_Page_Builders_Field_Wrapper
*/
class WPML_TM_Page_Builders_Field_Wrapper {
const SLUG_BASE = 'package-string-';
/**
* Field slug.
*
* @var false|string
*/
private $field_slug;
/**
* Package id.
*
* @var string|false|null
*/
private $package_id;
/**
* String id.
*
* @var string|false|null
*/
private $string_id;
/**
* WPML_TM_Page_Builders_Field_Wrapper constructor.
*
* @param string $field_slug Field slug.
*/
public function __construct( $field_slug ) {
$this->field_slug = $field_slug;
}
/**
* Check if package is valid.
*
* @param bool $package_must_exist Demand existence of the package.
*
* @return bool
*/
public function is_valid( $package_must_exist = false ) {
$result = $this->get_package_id() && $this->get_string_id();
if ( $result && $package_must_exist ) {
$result = $this->get_package() !== null;
}
return $result;
}
/**
* Get package id.
*
* @return false|string
*/
public function get_package_id() {
if ( null === $this->package_id ) {
$this->package_id = $this->extract_string_package_id( $this->field_slug );
}
return $this->package_id;
}
/**
* Get package.
*
* @return WPML_Package|null
*/
public function get_package() {
if ( ! $this->get_package_id() ) {
return null;
}
return apply_filters( 'wpml_st_get_string_package', false, $this->get_package_id() );
}
/**
* Get string id.
*
* @return false|string
*/
public function get_string_id() {
if ( null === $this->string_id ) {
$this->string_id = $this->extract_string_id( $this->field_slug );
}
return $this->string_id;
}
/**
* Get field slug.
*
* @return string
*/
public function get_field_slug() {
return $this->field_slug;
}
/**
* Get string type.
*
* @return false|string
*/
public function get_string_type() {
$result = false;
if ( $this->is_valid( true ) ) {
$package_strings = $this->get_package()->get_package_strings();
if ( ! $package_strings ) {
return false;
}
$package_strings = wp_list_pluck( $package_strings, 'type', 'id' );
$result = $package_strings[ $this->get_string_id() ];
}
return $result;
}
/**
* Get string wrap tag.
*
* @param stdClass $string WPML string.
*
* @return string
*/
public static function get_wrap_tag( $string ) {
return isset( $string->wrap_tag ) ? $string->wrap_tag : '';
}
/**
* Generate field slug.
*
* @param int $package_id Package id.
* @param int $string_id String id.
*
* @return string
*/
public static function generate_field_slug( $package_id, $string_id ) {
return self::SLUG_BASE . $package_id . '-' . $string_id;
}
/**
* Extract string id.
*
* @param string $field_slug Field slug.
*
* @return false|string
*/
private function extract_string_id( $field_slug ) {
$result = false;
if ( is_string( $field_slug ) && preg_match( '#^' . self::SLUG_BASE . '#', $field_slug ) ) {
$result = preg_replace( '#^' . self::SLUG_BASE . '([0-9]+)-([0-9]+)$#', '$2', $field_slug, 1 );
}
return is_numeric( $result ) ? $result : false;
}
/**
* Extract string package id.
*
* @param string $field_slug Field slug.
*
* @return false|string
*/
private function extract_string_package_id( $field_slug ) {
$result = false;
if ( is_string( $field_slug ) && preg_match( '#^' . self::SLUG_BASE . '#', $field_slug ) ) {
$result = preg_replace( '#^' . self::SLUG_BASE . '([0-9]+)-([0-9]+)$#', '$1', $field_slug, 1 );
}
return is_numeric( $result ) ? $result : false;
}
/**
* Get string title.
*
* @return string|bool
*/
public function get_string_title() {
if ( null === $this->string_id ) {
$this->string_id = $this->extract_string_id( $this->field_slug );
}
return apply_filters( 'wpml_string_title_from_id', false, $this->string_id );
}
}