activation-tracker.php
3 years ago
display-options.php
3 years ago
field-type-settings.php
3 years ago
field.php
3 years ago
filed-validation.php
3 years ago
myaccount-edit-address.php
3 years ago
myaccount-field-processor.php
3 years ago
plugin.php
3 years ago
settings.php
3 years ago
tracker.php
3 years ago
user-meta-checkout.php
3 years ago
user-meta.php
3 years ago
user-profile.php
3 years ago
field.php
273 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 = array( |
| 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 | } else { |
| 204 | if ( $this->is_custom_field() ) { |
| 205 | return '1'; |
| 206 | } else { |
| 207 | return '0'; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Set field type. |
| 214 | * |
| 215 | * @param string $type . |
| 216 | */ |
| 217 | public function set_type( $type ) { |
| 218 | $this->field_data[ self::FIELD_TYPE ] = $type; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Get field type. |
| 223 | * |
| 224 | * @return string |
| 225 | */ |
| 226 | public function get_type() { |
| 227 | return isset( $this->field_data[ self::FIELD_TYPE ] ) ? $this->field_data[ self::FIELD_TYPE ] : self::DEFAULT_FIELD_TYPE; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Get default value. |
| 232 | * |
| 233 | * @return string |
| 234 | */ |
| 235 | public function get_default() { |
| 236 | return isset( $this->field_data[ self::FIELD_DEFAULT ] ) ? $this->field_data[ self::FIELD_DEFAULT ] : ''; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Prepare display_on option name. |
| 241 | * |
| 242 | * @param string $display_on |
| 243 | * |
| 244 | * @return string |
| 245 | */ |
| 246 | public function prepare_display_on_option_name( $display_on ) { |
| 247 | return 'display_on_option_' . $display_on; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Get field name for formatted address. |
| 252 | */ |
| 253 | public function get_name_for_address_format() { |
| 254 | $name = $this->field_data['name']; |
| 255 | if ( isset( $this->field_data[ $this->prepare_display_on_option_name( self::DISPLAY_OPTION_STATE_CODE ) ] ) |
| 256 | && 1 === intval( $this->field_data[ $this->prepare_display_on_option_name( self::DISPLAY_OPTION_STATE_CODE ) ] ) |
| 257 | ) { |
| 258 | $name = 'state_code'; |
| 259 | } |
| 260 | return $name; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Get display comma before field. |
| 265 | * Currently used only on state/county field. |
| 266 | */ |
| 267 | public function get_display_comma_before() { |
| 268 | return isset( $this->field_data[ $this->prepare_display_on_option_name( self::DISPLAY_OPTION_STATE_COMMA_BEFORE ) ] ) |
| 269 | ? $this->field_data[ $this->prepare_display_on_option_name( self::DISPLAY_OPTION_STATE_COMMA_BEFORE ) ] : '0'; |
| 270 | } |
| 271 | |
| 272 | } |
| 273 |