PluginProbe ʕ •ᴥ•ʔ
Flexible Checkout Fields for WooCommerce – WooCommerce Checkout Manager / trunk
Flexible Checkout Fields for WooCommerce – WooCommerce Checkout Manager vtrunk
4.1.39 4.1.38 trunk 1.2 1.2.1 1.2.2 1.2.3 1.3 1.3.1 1.3.2 1.3.3 1.4 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6 1.6.1 1.6.10 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7 1.7.1 1.7.2 1.8 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 1.9.2 1.9.3 2.0.0 2.0.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.1.1 2.1.4 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.10 2.4.11 2.4.12 2.4.13 2.4.14 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.6.0 2.7.0 2.7.1 2.8.0 2.9.0 2.9.1 2.9.2 3.0.0 3.0.1 3.0.10 3.0.11 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.3.0 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.10 3.5.11 3.5.12 3.5.13 3.5.14 3.5.15 3.5.16 3.5.17 3.5.18 3.5.19 3.5.2 3.5.3 3.5.4 3.5.5 3.5.6 3.5.7 3.5.8 3.5.9 3.6.0 4.0.0 4.1.0 4.1.1 4.1.10 4.1.11 4.1.12 4.1.13 4.1.14 4.1.15 4.1.16 4.1.17 4.1.18 4.1.19 4.1.2 4.1.20 4.1.21 4.1.22 4.1.23 4.1.24 4.1.25 4.1.26 4.1.27 4.1.28 4.1.29 4.1.3 4.1.30 4.1.31 4.1.32 4.1.34 4.1.35 4.1.36 4.1.37 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9
flexible-checkout-fields / classes / field.php
flexible-checkout-fields / classes Last commit date
activation-tracker.php 1 week ago display-options.php 1 week ago field-type-settings.php 1 week ago field.php 1 week ago filed-validation.php 1 week ago myaccount-edit-address.php 1 week ago myaccount-field-processor.php 1 week ago plugin.php 1 week ago settings.php 1 week ago tracker.php 1 week ago user-meta-checkout.php 1 week ago user-meta.php 1 week ago user-profile.php 1 week ago
field.php
270 lines
1 <?php
2
3 /**
4 * Checkout field settings.
5 */
6 class Flexible_Checkout_Fields_Field {
7
8 const FIELD_TYPE = 'type';
9 const FIELD_CUSTOM_FIELD = 'custom_field';
10 const FIELD_VISIBLE = 'visible';
11 const FIELD_DEFAULT = 'default';
12
13 const FIELD_DISPLAY_ON_OPTION_NEW_LINE_BEFORE = 'display_on_option_new_line_before';
14 const FIELD_DISPLAY_ON_OPTION_SHOW_LABEL = 'display_on_option_show_label';
15
16 const FIELD_TYPE_EXCLUDE_IN_ADMIN = 'exclude_in_admin';
17 const FIELD_TYPE_EXCLUDE_FOR_USER = 'exclude_for_user';
18
19 const DEFAULT_FIELD_TYPE = Flexible_Checkout_Fields_Field_Type_Settings::FIELD_TYPE_TEXT;
20
21 const FIELD_TYPE_STATE = 'state';
22
23 const DISPLAY_OPTION_STATE_CODE = 'state_code';
24 const DISPLAY_OPTION_STATE_COMMA_BEFORE = 'state_code_comma_before';
25
26 /**
27 * Field data.
28 *
29 * @var array
30 */
31 private $field_data;
32
33 /**
34 * Plugin.
35 *
36 * @var Flexible_Checkout_Fields_Plugin
37 */
38 private $plugin;
39
40 /**
41 * Flexible_Checkout_Fields_Field constructor.
42 *
43 * @param array $field_data Field data.
44 * @param Flexible_Checkout_Fields_Plugin $plugin Plugin.
45 */
46 public function __construct( array $field_data, $plugin ) {
47 $this->plugin = $plugin;
48 $this->field_data = $field_data;
49 }
50
51 /**
52 * @param array $field_data Field data.
53 * @param array $field_settings .
54 * @param Flexible_Checkout_Fields_Plugin $plugin Plugin.
55 *
56 * @return Flexible_Checkout_Fields_Field
57 */
58 public static function create_with_settings( $field_data, $field_settings, $plugin ) {
59 $fcf_field = new self( $field_data, $plugin );
60 $fcf_field->add_field_settings( $field_settings );
61 return $fcf_field;
62 }
63
64 /**
65 * Add settings to field.
66 *
67 * @param array $field_settings .
68 */
69 public function add_field_settings( array $field_settings ) {
70 foreach ( $field_settings as $key => $setting ) {
71 $this->field_data[ $key ] = $setting;
72 }
73 }
74
75 /**
76 * Get field setting.
77 *
78 * @param string $setting_name .
79 * @param null|string|array $default_value .
80 *
81 * @return array|string|null
82 */
83 public function get_field_setting( $setting_name, $default_value = null ) {
84 if ( $setting_name === self::FIELD_DISPLAY_ON_OPTION_SHOW_LABEL ) {
85 return $this->get_display_on_option_show_label();
86 }
87 if ( $setting_name === self::FIELD_DISPLAY_ON_OPTION_NEW_LINE_BEFORE ) {
88 return $this->get_display_on_option_new_line_before();
89 }
90 if ( isset( $this->field_data[ $setting_name ] ) ) {
91 return $this->field_data[ $setting_name ];
92 } else {
93 return $default_value;
94 }
95 }
96
97 /**
98 * Get field types from plugin.
99 *
100 * @return array
101 */
102 private function get_field_types_from_plugin() {
103 return $this->plugin->get_fields();
104 }
105
106 /**
107 * Get field type settings.
108 *
109 * @return array
110 */
111 private function get_field_type_settings() {
112 $default_values = [
113 self::FIELD_TYPE_EXCLUDE_IN_ADMIN => false,
114 self::FIELD_TYPE_EXCLUDE_FOR_USER => false,
115 ];
116 $field_types = $this->get_field_types_from_plugin();
117 if ( isset( $this->field_data[ self::FIELD_TYPE ] ) && isset( $field_types[ $this->field_data[ self::FIELD_TYPE ] ] ) ) {
118 $field_type_settings = $field_types[ $this->field_data[ self::FIELD_TYPE ] ];
119 $field_type_settings = array_merge( $default_values, $field_type_settings );
120 return $field_type_settings;
121 }
122 return $default_values;
123 }
124
125 /**
126 * Is visible?
127 *
128 * @return bool
129 */
130 public function is_custom_field() {
131 if ( isset( $this->field_data[ self::FIELD_CUSTOM_FIELD ] ) && 1 === intval( $this->field_data[ self::FIELD_CUSTOM_FIELD ] ) ) {
132 return true;
133 }
134 return false;
135 }
136
137 /**
138 * Is visible?
139 *
140 * @return bool
141 */
142 public function is_visible() {
143 if ( isset( $this->field_data[ self::FIELD_VISIBLE ] ) && 0 === intval( $this->field_data[ self::FIELD_VISIBLE ] ) ) {
144 return false;
145 }
146 return true;
147 }
148
149 /**
150 * Is field excluded for user?
151 * Field is excluded from user when is custom field and is not visible or field type is excluded for user.
152 *
153 * @return bool
154 */
155 public function is_field_excluded_for_user() {
156 if ( ! $this->is_custom_field() ) {
157 return false;
158 }
159 $field_type_settings = $this->get_field_type_settings();
160 if ( true === $field_type_settings[ self::FIELD_TYPE_EXCLUDE_FOR_USER ] ) {
161 return true;
162 }
163 return false;
164 }
165
166 /**
167 * Is field excluded in admin?
168 *
169 * @return bool
170 */
171 public function is_field_excluded_in_admin() {
172 if ( ! $this->is_custom_field() ) {
173 return false;
174 }
175 $field_type_settings = $this->get_field_type_settings();
176 if ( true === $field_type_settings[ self::FIELD_TYPE_EXCLUDE_IN_ADMIN ] ) {
177 return true;
178 }
179 return false;
180 }
181
182 /**
183 * .
184 *
185 * @return string
186 */
187 public function get_display_on_option_new_line_before() {
188 if ( isset( $this->field_data[ self::FIELD_DISPLAY_ON_OPTION_NEW_LINE_BEFORE ] ) ) {
189 return $this->field_data[ self::FIELD_DISPLAY_ON_OPTION_NEW_LINE_BEFORE ];
190 } else {
191 return '1';
192 }
193 }
194
195 /**
196 * .
197 *
198 * @return string
199 */
200 public function get_display_on_option_show_label() {
201 if ( isset( $this->field_data[ self::FIELD_DISPLAY_ON_OPTION_SHOW_LABEL ] ) ) {
202 return $this->field_data[ self::FIELD_DISPLAY_ON_OPTION_SHOW_LABEL ];
203 } elseif ( $this->is_custom_field() ) {
204 return '1';
205 } else {
206 return '0';
207 }
208 }
209
210 /**
211 * Set field type.
212 *
213 * @param string $type .
214 */
215 public function set_type( $type ) {
216 $this->field_data[ self::FIELD_TYPE ] = $type;
217 }
218
219 /**
220 * Get field type.
221 *
222 * @return string
223 */
224 public function get_type() {
225 return isset( $this->field_data[ self::FIELD_TYPE ] ) ? $this->field_data[ self::FIELD_TYPE ] : self::DEFAULT_FIELD_TYPE;
226 }
227
228 /**
229 * Get default value.
230 *
231 * @return string
232 */
233 public function get_default() {
234 return isset( $this->field_data[ self::FIELD_DEFAULT ] ) ? $this->field_data[ self::FIELD_DEFAULT ] : '';
235 }
236
237 /**
238 * Prepare display_on option name.
239 *
240 * @param string $display_on
241 *
242 * @return string
243 */
244 public function prepare_display_on_option_name( $display_on ) {
245 return 'display_on_option_' . $display_on;
246 }
247
248 /**
249 * Get field name for formatted address.
250 */
251 public function get_name_for_address_format() {
252 $name = $this->field_data['name'];
253 if ( isset( $this->field_data[ $this->prepare_display_on_option_name( self::DISPLAY_OPTION_STATE_CODE ) ] )
254 && 1 === intval( $this->field_data[ $this->prepare_display_on_option_name( self::DISPLAY_OPTION_STATE_CODE ) ] )
255 ) {
256 $name = 'state_code';
257 }
258 return $name;
259 }
260
261 /**
262 * Get display comma before field.
263 * Currently used only on state/county field.
264 */
265 public function get_display_comma_before() {
266 return isset( $this->field_data[ $this->prepare_display_on_option_name( self::DISPLAY_OPTION_STATE_COMMA_BEFORE ) ] )
267 ? $this->field_data[ $this->prepare_display_on_option_name( self::DISPLAY_OPTION_STATE_COMMA_BEFORE ) ] : '0';
268 }
269 }
270