activation-tracker.php
4 years ago
display-options.php
4 years ago
field-options.php
4 years ago
field-type-settings.php
4 years ago
field.php
4 years ago
filed-validation.php
4 years ago
myaccount-edit-address.php
4 years ago
myaccount-field-processor.php
4 years ago
plugin.php
4 years ago
settings.php
4 years ago
tracker.php
4 years ago
user-meta-checkout.php
4 years ago
user-meta.php
4 years ago
user-profile.php
4 years ago
plugin.php
1030 lines
| 1 | <?php |
| 2 | |
| 3 | use WPDesk\FCF\Free\Plugin as PluginFree; |
| 4 | |
| 5 | /** |
| 6 | * Class Plugin |
| 7 | * |
| 8 | * @package WPDesk\WooCommerceFakturownia |
| 9 | */ |
| 10 | class Flexible_Checkout_Fields_Plugin extends \FcfVendor\WPDesk\PluginBuilder\Plugin\AbstractPlugin { |
| 11 | |
| 12 | /** @see validate_checkout method https://github.com/woocommerce/woocommerce/blob/master/includes/class-wc-checkout.php#L719 */ |
| 13 | const FIELDS_REQUIREMENT_CONTROLLED_BY_WOOCOMMERCE = array( |
| 14 | 'billing_country', |
| 15 | 'shipping_country', |
| 16 | 'billing_state', |
| 17 | 'shipping_state', |
| 18 | 'billing_postcode', |
| 19 | 'shipping_postcode', |
| 20 | ); |
| 21 | |
| 22 | /** |
| 23 | * Scripts version. |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | private $scripts_version = FLEXIBLE_CHECKOUT_FIELDS_VERSION . '.19'; |
| 28 | |
| 29 | protected $fields = array(); |
| 30 | |
| 31 | public $sections = array(); |
| 32 | |
| 33 | public $all_sections = array(); |
| 34 | |
| 35 | public $page_size = array(); |
| 36 | |
| 37 | public $field_validation; |
| 38 | |
| 39 | /** |
| 40 | * Plugin path. |
| 41 | * |
| 42 | * @var string |
| 43 | */ |
| 44 | private $plugin_path; |
| 45 | |
| 46 | /** |
| 47 | * Plugin namespaces |
| 48 | * |
| 49 | * Fot backward compatibility |
| 50 | * |
| 51 | * @var string |
| 52 | */ |
| 53 | public $plugin_namespace = 'inspire_checkout_fields'; |
| 54 | |
| 55 | /** |
| 56 | * Instance of new version main class of plugin. |
| 57 | * |
| 58 | * @var PluginFree |
| 59 | */ |
| 60 | private $plugin_free; |
| 61 | |
| 62 | |
| 63 | /** |
| 64 | * Plugin constructor. |
| 65 | * |
| 66 | * @param \WPDesk_Plugin_Info $plugin_info Plugin info. |
| 67 | */ |
| 68 | public function __construct( \FcfVendor\WPDesk_Plugin_Info $plugin_info ) { |
| 69 | parent::__construct( $plugin_info ); |
| 70 | $this->plugin_info = $plugin_info; |
| 71 | $this->plugin_free = new PluginFree( $plugin_info, $this ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Init base variables for plugin |
| 76 | */ |
| 77 | public function init_base_variables() { |
| 78 | $this->plugin_url = $this->plugin_info->get_plugin_url(); |
| 79 | $this->plugin_path = $this->plugin_info->get_plugin_dir(); |
| 80 | $this->template_path = $this->plugin_info->get_text_domain(); |
| 81 | $this->settings_url = admin_url( 'admin.php?page=wc-settings&tab=integration§ion=integration-fakturownia' ); |
| 82 | $this->default_view_args = [ 'plugin_url' => $this->get_plugin_url() ]; |
| 83 | $this->plugin_has_settings = false; |
| 84 | $this->plugin_namespace = 'inspire_checkout_fields'; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Init. |
| 89 | */ |
| 90 | public function init() { |
| 91 | $this->plugin_free->init(); |
| 92 | $this->init_base_variables(); |
| 93 | $this->load_dependencies(); |
| 94 | $this->hooks(); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Load dependencies. |
| 99 | */ |
| 100 | private function load_dependencies() { |
| 101 | new WPDesk_Flexible_Checkout_Fields_Tracker(); |
| 102 | require_once __DIR__ . '/settings.php'; |
| 103 | require_once __DIR__ . '/field-options.php'; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Hooks. |
| 108 | */ |
| 109 | public function hooks() { |
| 110 | $this->plugin_free->hooks(); |
| 111 | parent::hooks(); |
| 112 | |
| 113 | $this->settings = new Flexible_Checkout_Fields_Settings( $this, self::FIELDS_REQUIREMENT_CONTROLLED_BY_WOOCOMMERCE ); |
| 114 | |
| 115 | add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ), 100 ); |
| 116 | |
| 117 | add_action( 'woocommerce_checkout_fields', array( $this, 'changeCheckoutFields' ), 9999 ); |
| 118 | add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'updateCheckoutFields' ), 9, 2 ); |
| 119 | |
| 120 | add_action( 'woocommerce_admin_order_data_after_billing_address', array( |
| 121 | $this, |
| 122 | 'addCustomBillingFieldsToAdmin' |
| 123 | ) ); |
| 124 | add_action( 'woocommerce_admin_order_data_after_shipping_address', array( |
| 125 | $this, |
| 126 | 'addCustomShippingFieldsToAdmin' |
| 127 | ) ); |
| 128 | add_action( 'woocommerce_admin_order_data_after_shipping_address', array( |
| 129 | $this, |
| 130 | 'addCustomOrderFieldsToAdmin' |
| 131 | ) ); |
| 132 | |
| 133 | add_action( 'woocommerce_billing_fields', array( $this, 'addCustomFieldsBillingFields' ), 9999 ); |
| 134 | add_action( 'woocommerce_shipping_fields', array( $this, 'addCustomFieldsShippingFields' ), 9999 ); |
| 135 | add_action( 'woocommerce_order_fields', array( $this, 'addCustomFieldsOrderFields' ), 9999 ); |
| 136 | |
| 137 | |
| 138 | add_action( 'woocommerce_before_checkout_form', array( $this, 'woocommerce_before_checkout_form' ), 10 ); |
| 139 | add_action( 'woocommerce_before_edit_address_form_shipping', array( |
| 140 | $this, |
| 141 | 'woocommerce_before_checkout_form' |
| 142 | ), 10 ); |
| 143 | add_action( 'woocommerce_before_edit_address_form_billing', array( |
| 144 | $this, |
| 145 | 'woocommerce_before_checkout_form' |
| 146 | ), 10 ); |
| 147 | |
| 148 | add_filter( 'flexible_chekout_fields_fields', array( $this, 'getCheckoutFields' ), 10, 2 ); |
| 149 | |
| 150 | add_action( 'woocommerce_default_address_fields', array( $this, 'woocommerce_default_address_fields' ), 9999 ); |
| 151 | add_filter( 'woocommerce_get_country_locale', array( $this, 'woocommerce_get_country_locale' ), 9999 ); |
| 152 | add_filter( 'woocommerce_get_country_locale_base', array( $this, 'woocommerce_get_country_locale_base' ), 9999 ); |
| 153 | |
| 154 | add_action( 'woocommerce_get_country_locale_default', array( $this, 'woocommerce_get_country_locale_default' ), 11 ); |
| 155 | |
| 156 | add_filter( 'woocommerce_screen_ids', array( $this, 'add_woocommerce_screen_ids' ) ); |
| 157 | |
| 158 | new Flexible_Checkout_Fields_Disaplay_Options( $this ); |
| 159 | |
| 160 | $user_meta = new Flexible_Checkout_Fields_User_Meta( $this ); |
| 161 | |
| 162 | $user_profile = new Flexible_Checkout_Fields_User_Profile( $this, $user_meta ); |
| 163 | $user_profile->hooks(); |
| 164 | |
| 165 | $user_meta = new Flexible_Checkout_Fields_User_Meta_Checkout( $this, $user_meta ); |
| 166 | $user_meta->hooks(); |
| 167 | |
| 168 | $this->field_validation = new Flexible_Checkout_Fields_Field_Validation( $this ); |
| 169 | $this->field_validation->hooks(); |
| 170 | |
| 171 | $my_account_fields_processor = new Flexible_Checkout_Fields_Myaccount_Field_Processor( $this ); |
| 172 | $my_account_fields_processor->hooks(); |
| 173 | |
| 174 | $my_account_edit_address = new Flexible_Checkout_Fields_Myaccount_Edit_Address( $this ); |
| 175 | $my_account_edit_address->hooks(); |
| 176 | |
| 177 | $plugin = $this; |
| 178 | add_filter( 'flexible_checkout_fields', static function() use( $plugin ) { |
| 179 | return $plugin; |
| 180 | }); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Get plugin path. |
| 185 | * |
| 186 | * @return string |
| 187 | */ |
| 188 | public function get_plugin_path() { |
| 189 | return $this->plugin_path; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | /** |
| 194 | * Load plugin textdomain |
| 195 | */ |
| 196 | public function load_plugin_text_domain() { |
| 197 | load_plugin_textdomain( 'wpdesk-plugin', false, $this->get_text_domain() . '/classes/wpdesk/lang/' ); |
| 198 | load_plugin_textdomain( $this->get_text_domain(), false, $this->get_text_domain() . '/lang/' ); |
| 199 | } |
| 200 | |
| 201 | public function plugins_loaded() { |
| 202 | $this->init_fields(); |
| 203 | //do użycia dla pola miasto, kod pocztowy i stan |
| 204 | $this->init_sections(); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Get setting value. |
| 209 | * |
| 210 | * @param string $name Setting name. |
| 211 | * @param mixed $default Default setting value. |
| 212 | * |
| 213 | * @return mixed|void |
| 214 | */ |
| 215 | public function get_setting_value( $name, $default = null ) { |
| 216 | return get_option( $this->get_namespace() . '_' . $name, $default ); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Change params used by js locale woocommerce/assets/js/frontend/address-i18n.js so it would not overwrite backend settings. |
| 221 | * |
| 222 | * This is a locale for default country. |
| 223 | * |
| 224 | * @param array $base Local base. |
| 225 | * |
| 226 | * @return array |
| 227 | */ |
| 228 | public function woocommerce_get_country_locale_base( $base ) { |
| 229 | $settings = $this->get_settings(); |
| 230 | |
| 231 | foreach ( $base as $key => $field ) { |
| 232 | unset( $base[ $key ]['placeholder'] ); |
| 233 | unset( $base[ $key ]['label'] ); |
| 234 | if ( version_compare( WC()->version, '4.4.1', '>=' ) ) { |
| 235 | unset( $base[ $key ]['class'] ); |
| 236 | } |
| 237 | |
| 238 | // field is force-required for given locale when FCF have shipping or billing field required |
| 239 | $shipping_key = 'shipping_' . $key; |
| 240 | $billing_key = 'billing_' . $key; |
| 241 | if ( ( isset( $settings['shipping'][ $shipping_key ] ) && $settings['shipping'][ $shipping_key ]['required'] ) |
| 242 | || ( isset( $settings['billing'][ $billing_key ] ) && $settings['billing'][ $billing_key ]['required'] ) ) { |
| 243 | $base [ $key ]['required'] = true; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | return $base; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Change params used by js locale woocommerce/assets/js/frontend/address-i18n.js so it would not overwrite backend settings |
| 252 | * |
| 253 | * @param array $locale Table of field settings per locale |
| 254 | * |
| 255 | * @return array |
| 256 | */ |
| 257 | public function woocommerce_get_country_locale( $locale ) { |
| 258 | if ( is_checkout() || is_account_page() ) { |
| 259 | foreach ( $locale as $country => $fields ) { |
| 260 | foreach ( $fields as $field => &$settings ) { |
| 261 | unset( $locale[ $country ][ $field ]['priority'] ); |
| 262 | unset( $locale[ $country ][ $field ]['label'] ); |
| 263 | unset( $locale[ $country ][ $field ]['placeholder'] ); |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | return $locale; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Remove priority from default address field |
| 273 | * |
| 274 | * @param array $fields Fields. |
| 275 | * |
| 276 | * @return array |
| 277 | */ |
| 278 | public function woocommerce_default_address_fields( $fields ) { |
| 279 | if ( is_checkout() || is_account_page() ) { |
| 280 | foreach ( $fields as $key => $field ) { |
| 281 | unset( $fields[ $key ]['priority'] ); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | return $fields; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Init sections. |
| 290 | */ |
| 291 | public function init_sections() { |
| 292 | $sections = array( |
| 293 | 'billing' => array( |
| 294 | 'section' => 'billing', |
| 295 | 'tab' => 'fields_billing', |
| 296 | 'tab_title' => __( 'Billing', 'flexible-checkout-fields' ), |
| 297 | 'custom_section' => false, |
| 298 | 'user_meta' => true, |
| 299 | ), |
| 300 | 'shipping' => array( |
| 301 | 'section' => 'shipping', |
| 302 | 'tab' => 'fields_shipping', |
| 303 | 'tab_title' => __( 'Shipping', 'flexible-checkout-fields' ), |
| 304 | 'custom_section' => false, |
| 305 | 'user_meta' => true, |
| 306 | ), |
| 307 | 'order' => array( |
| 308 | 'section' => 'order', |
| 309 | 'tab' => 'fields_order', |
| 310 | 'tab_title' => __( 'Order', 'flexible-checkout-fields' ), |
| 311 | 'custom_section' => false, |
| 312 | 'user_meta' => false, |
| 313 | ), |
| 314 | ); |
| 315 | |
| 316 | $all_sections = unserialize( serialize( $sections ) ); |
| 317 | |
| 318 | $this->sections = apply_filters( 'flexible_checkout_fields_sections', $sections ); |
| 319 | |
| 320 | $this->all_sections = apply_filters( 'flexible_checkout_fields_all_sections', $all_sections ); |
| 321 | } |
| 322 | |
| 323 | private function init_fields() { |
| 324 | $this->fields[ Flexible_Checkout_Fields_Field_Type_Settings::FIELD_TYPE_TEXT ] = array( |
| 325 | 'name' => __( 'Single Line Text', 'flexible-checkout-fields' ) |
| 326 | ); |
| 327 | |
| 328 | $this->fields[ Flexible_Checkout_Fields_Field_Type_Settings::FIELD_TYPE_TEXTAREA ] = array( |
| 329 | 'name' => __( 'Paragraph Text', 'flexible-checkout-fields' ) |
| 330 | ); |
| 331 | } |
| 332 | |
| 333 | private function pro_fields( $fields ) { |
| 334 | $add_fields = array(); |
| 335 | |
| 336 | $add_fields['inspirecheckbox'] = array( |
| 337 | 'name' => __( 'Checkbox', 'flexible-checkout-fields' ), |
| 338 | 'pro' => true |
| 339 | ); |
| 340 | |
| 341 | $add_fields['checkbox'] = array( |
| 342 | 'name' => __( 'Checkbox', 'flexible-checkout-fields' ), |
| 343 | 'pro' => true |
| 344 | ); |
| 345 | |
| 346 | $add_fields['inspireradio'] = array( |
| 347 | 'name' => __( 'Radio button', 'flexible-checkout-fields' ), |
| 348 | 'pro' => true |
| 349 | ); |
| 350 | |
| 351 | $add_fields['select'] = array( |
| 352 | 'name' => __( 'Select (Drop Down)', 'flexible-checkout-fields' ), |
| 353 | 'pro' => true |
| 354 | ); |
| 355 | |
| 356 | $add_fields['wpdeskmultiselect'] = array( |
| 357 | 'name' => __( 'Multi-select', 'flexible-checkout-fields' ), |
| 358 | 'pro' => true |
| 359 | ); |
| 360 | |
| 361 | $add_fields['datepicker'] = array( |
| 362 | 'name' => __( 'Date', 'flexible-checkout-fields' ), |
| 363 | 'pro' => true |
| 364 | ); |
| 365 | |
| 366 | $add_fields['timepicker'] = array( |
| 367 | 'name' => __( 'Time', 'flexible-checkout-fields' ), |
| 368 | 'pro' => true |
| 369 | ); |
| 370 | |
| 371 | $add_fields['colorpicker'] = array( |
| 372 | 'name' => __( 'Color Picker', 'flexible-checkout-fields' ), |
| 373 | 'pro' => true |
| 374 | ); |
| 375 | |
| 376 | $add_fields['heading'] = array( |
| 377 | 'name' => __( 'Headline', 'flexible-checkout-fields' ), |
| 378 | 'pro' => true |
| 379 | ); |
| 380 | |
| 381 | $add_fields['info'] = array( |
| 382 | 'name' => __( 'HTML', 'flexible-checkout-fields' ), |
| 383 | 'pro' => true |
| 384 | ); |
| 385 | |
| 386 | $add_fields['file'] = array( |
| 387 | 'name' => __( 'File Upload', 'flexible-checkout-fields' ), |
| 388 | 'pro' => true |
| 389 | ); |
| 390 | |
| 391 | foreach ( $add_fields as $key => $field ) { |
| 392 | $fields[ $key ] = $field; |
| 393 | } |
| 394 | |
| 395 | return $fields; |
| 396 | |
| 397 | } |
| 398 | |
| 399 | public function get_fields() { |
| 400 | $this->fields = $this->pro_fields( $this->fields ); |
| 401 | |
| 402 | return apply_filters( 'flexible_checkout_fields_fields', $this->fields ); |
| 403 | } |
| 404 | |
| 405 | |
| 406 | /** |
| 407 | * Remove unavailable sections from settings. |
| 408 | * Removes sections added by PRO plugin, after PRO plugin disable. |
| 409 | * |
| 410 | * @param array $settings Settings. |
| 411 | * |
| 412 | * @return array |
| 413 | */ |
| 414 | private function get_settings_for_available_sections( array $settings ) { |
| 415 | $this->init_sections(); |
| 416 | if ( is_array( $settings ) && is_array( $this->sections ) ) { |
| 417 | foreach ( $settings as $section => $section_settings ) { |
| 418 | $unset = true; |
| 419 | foreach ( $this->sections as $section_data ) { |
| 420 | if ( $section_data['section'] === $section ) { |
| 421 | $unset = false; |
| 422 | } |
| 423 | } |
| 424 | if ( $unset ) { |
| 425 | unset( $settings[ $section ] ); |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | return $settings; |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Get settings. |
| 435 | * |
| 436 | * @return array |
| 437 | */ |
| 438 | public function get_settings() { |
| 439 | $settings = get_option( 'inspire_checkout_fields_settings', array() ); |
| 440 | if ( ! is_array( $settings ) ) { |
| 441 | $settings = array(); |
| 442 | } |
| 443 | |
| 444 | return $this->get_settings_for_available_sections( $settings ); |
| 445 | } |
| 446 | |
| 447 | public function woocommerce_before_checkout_form() { |
| 448 | WC()->session->set( 'checkout-fields', array() ); |
| 449 | $settings = $this->get_settings(); |
| 450 | $args = array( 'settings' => $settings ); |
| 451 | include $this->plugin_path . '/views/before-checkout-form.php'; |
| 452 | } |
| 453 | |
| 454 | |
| 455 | /** |
| 456 | * @param array $settings |
| 457 | * @param array $fields |
| 458 | * @param array $new |
| 459 | * @param null|string $request_type |
| 460 | * |
| 461 | * @return array |
| 462 | */ |
| 463 | private function append_other_plugins_fields_to_checkout_fields( $settings, $fields, $new, $request_type ) { |
| 464 | if ( $request_type === null ) { |
| 465 | if ( ! empty( $fields ) && is_array( $fields ) ) { |
| 466 | foreach ( $fields as $section => $section_fields ) { |
| 467 | if ( ! empty( $section_fields ) && is_array( $section_fields ) ) { |
| 468 | foreach ( $section_fields as $key => $field ) { |
| 469 | if ( empty( $settings[ $section ][ $key ] ) ) { |
| 470 | $new[ $section ][ $key ] = $field; |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | } else { |
| 477 | foreach ( $fields as $key => $field ) { |
| 478 | if ( empty( $settings[ $request_type ][ $key ] ) ) { |
| 479 | $new[ $request_type ][ $key ] = $field; |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | return $new; |
| 485 | } |
| 486 | |
| 487 | /** |
| 488 | * Is field requirement controlled by woocommerce. |
| 489 | * |
| 490 | * @param string $field_name . |
| 491 | * |
| 492 | * @return bool |
| 493 | */ |
| 494 | private function is_field_requirement_controlled_by_woocommerce( $field_name ) { |
| 495 | return in_array( $field_name, self::FIELDS_REQUIREMENT_CONTROLLED_BY_WOOCOMMERCE, true ); |
| 496 | } |
| 497 | |
| 498 | /** |
| 499 | * @param array $fields |
| 500 | * @param null|string $request_type |
| 501 | * |
| 502 | * @return array |
| 503 | */ |
| 504 | public function getCheckoutFields( $fields, $request_type = null ) { |
| 505 | $settings = $this->get_settings(); |
| 506 | |
| 507 | $checkout_field_type = $this->get_fields(); |
| 508 | if ( ! empty( $settings ) ) { |
| 509 | $new = array(); |
| 510 | if ( isset( $fields['account'] ) ) { |
| 511 | $new['account'] = array(); |
| 512 | } |
| 513 | $priority = 0; |
| 514 | foreach ( $settings as $key => $type ) { |
| 515 | |
| 516 | if ( $key !== 'billing' && $key !== 'shipping' && $key !== 'order' ) { |
| 517 | if ( get_option( 'inspire_checkout_fields_' . $key, '0' ) == '0' ) { |
| 518 | continue; |
| 519 | } |
| 520 | } |
| 521 | if ( ! is_array( $type ) ) { |
| 522 | continue; |
| 523 | } |
| 524 | if ( $request_type == null || $request_type == $key ) { |
| 525 | if ( ! isset( $new[ $key ] ) ) { |
| 526 | $new[ $key ] = array(); |
| 527 | } |
| 528 | $fields_found = true; |
| 529 | foreach ( $type as $field_name => $field ) { |
| 530 | if ( apply_filters( 'flexible_checkout_fields_condition', true, $field ) ) { |
| 531 | if ( $field['visible'] == 0 or |
| 532 | ( ( isset( $_GET['page'] ) && $_GET['page'] == 'inspire_checkout_fields_settings' ) && $field['visible'] == 1 ) || $field['name'] == 'billing_country' || $field['name'] == 'shipping_country' ) { |
| 533 | $fcf_field = new Flexible_Checkout_Fields_Field( $field, $this ); |
| 534 | $custom_field = $fcf_field->is_custom_field(); |
| 535 | if ( isset( $fields[ $key ][ $field['name'] ] ) ) { |
| 536 | $new[ $key ][ $field['name'] ] = $fields[ $key ][ $field['name'] ]; |
| 537 | } else { |
| 538 | $new[ $key ][ $field['name'] ] = $type[ $field['name'] ]; |
| 539 | } |
| 540 | |
| 541 | if ( ! $this->is_field_requirement_controlled_by_woocommerce( $field_name ) ) { |
| 542 | if ( 1 === intval( $field['required'] ) ) { |
| 543 | $new[ $key ][ $field['name'] ]['required'] = true; |
| 544 | } else { |
| 545 | $new[ $key ][ $field['name'] ]['required'] = false; |
| 546 | if ( isset( $new[ $key ][ $field['name'] ]['validate'] ) ) { |
| 547 | unset( $new[ $key ][ $field['name'] ]['validate'] ); |
| 548 | } |
| 549 | } |
| 550 | } else { |
| 551 | if ( isset( $fields[ $key ][ $field['name'] ] ) ) { |
| 552 | $new[ $key ][ $field['name'] ]['required'] = $fields[ $key ][ $field['name'] ]['required']; |
| 553 | } |
| 554 | } |
| 555 | if ( isset( $field['label'] ) ) { |
| 556 | $new[ $key ][ $field['name'] ]['label'] = stripcslashes( wpdesk__( $field['label'], 'flexible-checkout-fields' ) ); |
| 557 | |
| 558 | // Support for fields rendered by WooCommerce |
| 559 | if ( isset( $field['type'] ) && in_array( $field['type'], array( 'text', 'textarea', 'select' ), true ) ) { |
| 560 | $new[ $key ][ $field['name'] ]['label'] = wp_kses_post( $new[ $key ][ $field['name'] ]['label'] ); |
| 561 | } |
| 562 | } |
| 563 | if ( isset( $field['placeholder'] ) ) { |
| 564 | $new[ $key ][ $field['name'] ]['placeholder'] = wpdesk__( $field['placeholder'], 'flexible-checkout-fields' ); |
| 565 | } else { |
| 566 | $new[ $key ][ $field['name'] ]['placeholder'] = ''; |
| 567 | } |
| 568 | if ( ! is_array( $field['class'] ) ) { |
| 569 | $new[ $key ][ $field['name'] ]['class'] = explode( ' ', $field['class'] ); |
| 570 | } |
| 571 | if ( ( $field['name'] == 'billing_country' || $field['name'] == 'shipping_country' ) && $field['visible'] == 1 ) { |
| 572 | $new[ $key ][ $field['name'] ]['class'][1] = "inspire_checkout_fields_hide"; |
| 573 | } |
| 574 | if ( ! $custom_field ) { |
| 575 | if ( isset( $field['validation'] ) && $field['validation'] != '' ) { |
| 576 | if ( $field['validation'] == 'none' ) { |
| 577 | unset( $new[ $key ][ $field['name'] ]['validate'] ); |
| 578 | } else { |
| 579 | $new[ $key ][ $field['name'] ]['validate'] = array( $field['validation'] ); |
| 580 | } |
| 581 | } |
| 582 | } else { |
| 583 | if ( isset( $field['validation'] ) && $field['validation'] != 'none' ) { |
| 584 | $new[ $key ][ $field['name'] ]['validate'] = array( $field['validation'] ); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | if ( ! empty( $field['type'] ) ) { |
| 589 | $new[ $key ][ $field['name'] ]['type'] = $field['type']; |
| 590 | } |
| 591 | |
| 592 | if ( $custom_field ) { |
| 593 | $new[ $key ][ $field['name'] ]['type'] = $field['type'] ?? ''; |
| 594 | |
| 595 | if ( isset( $checkout_field_type[ $field['type'] ?? '' ]['has_options'] ) && isset( $field['option'] ) ) { |
| 596 | $field_options = new Flexible_Checkout_Fields_Field_Options( $field['option'], $new[ $key ][ $field['name'] ]['placeholder'], $field['type'] ); |
| 597 | $new[ $key ][ $field['name'] ]['options'] = $field_options->get_options_as_array(); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | $custom_attributes = array(); |
| 602 | if ( isset( $new[ $key ][ $field['name'] ]['custom_attributes'] ) ) { |
| 603 | $custom_attributes = $new[ $key ][ $field['name'] ]['custom_attributes']; |
| 604 | } |
| 605 | if ( isset( $field['label'] ) ) { |
| 606 | $custom_attributes['data-qa-id'] = $field['label']; |
| 607 | } |
| 608 | |
| 609 | $new[ $key ][ $field['name'] ]['custom_attributes'] = apply_filters( 'flexible_checkout_fields_custom_attributes', $custom_attributes, $field ); |
| 610 | |
| 611 | if ( '' !== $fcf_field->get_default() ) { |
| 612 | $new[ $key ][ $field['name'] ]['default'] = wpdesk__( $fcf_field->get_default(), 'flexible-checkout-fields' ); |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | $new = $this->append_other_plugins_fields_to_checkout_fields( $settings, $fields, $new, $request_type ); |
| 621 | |
| 622 | foreach ( $new as $type => $new_fields ) { |
| 623 | $priority = 0; |
| 624 | foreach ( $new_fields as $key => $field ) { |
| 625 | $priority += 10; |
| 626 | $new[ $type ][ $key ]['priority'] = $priority; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | if ( $request_type == null ) { |
| 631 | if ( ! empty( $fields['account'] ) ) { |
| 632 | $new['account'] = $fields['account']; |
| 633 | } |
| 634 | |
| 635 | $new = $this->restore_default_city_validation( $new, $_POST, 'billing' ); |
| 636 | $new = $this->restore_default_city_validation( $new, $_POST, 'shipping' ); |
| 637 | |
| 638 | return $new; |
| 639 | } |
| 640 | if ( isset( $new[ $request_type ] ) ) { |
| 641 | $new = $this->restore_default_city_validation( $new, $_POST, $request_type ); |
| 642 | |
| 643 | return $new[ $request_type ]; |
| 644 | } else { |
| 645 | return array(); |
| 646 | } |
| 647 | } else { |
| 648 | return $fields; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | /** |
| 653 | * Restores the default validation for the city |
| 654 | * |
| 655 | * @param array $fields Fields. |
| 656 | * @param array|null $request Request. |
| 657 | * @param string $request_type the type of shipping address (billing or shipping). |
| 658 | * |
| 659 | * @return array |
| 660 | */ |
| 661 | private function restore_default_city_validation( array $fields, $request, $request_type ) { |
| 662 | |
| 663 | if ( null === $request ) { |
| 664 | $request = array(); |
| 665 | } |
| 666 | |
| 667 | $city = $request_type . '_city'; |
| 668 | $country = $request_type . '_country'; |
| 669 | |
| 670 | if ( isset( $fields[ $request_type ][ $city ]['required'] ) && isset( $request[ $country ] ) ) { |
| 671 | $slug = $request[ $country ]; |
| 672 | $countries = new WC_Countries(); |
| 673 | $locales = $countries->get_country_locale(); |
| 674 | if ( isset( $locales[ $slug ]['city']['required'] ) ) { |
| 675 | $required = $locales[ $slug ]['city']['required']; |
| 676 | if ( ! $required ) { |
| 677 | $fields[ $request_type ][ $city ]['required'] = 0; |
| 678 | $fields[ $request_type ][ $city ]['hidden'] = 1; |
| 679 | } |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | return $fields; |
| 684 | } |
| 685 | |
| 686 | public function getCheckoutUserFields( $fields, $request_type = null ) { |
| 687 | $settings = $this->get_settings(); |
| 688 | |
| 689 | $checkout_field_type = $this->get_fields(); |
| 690 | |
| 691 | $priority = 0; |
| 692 | |
| 693 | if ( ! empty( $settings[ $request_type ] ) ) { |
| 694 | foreach ( $settings[ $request_type ] as $key => $field ) { |
| 695 | |
| 696 | if ( $field['visible'] == 0 || $field['name'] === 'billing_country' || $field['name'] === 'shipping_country' || ( isset( $_GET['page'] ) && $_GET['page'] === 'inspire_checkout_fields_settings' && $field['visible'] == 1 ) ) { |
| 697 | if ( ! empty( $fields[ $key ] ) ) { |
| 698 | $new[ $key ] = $fields[ $key ]; |
| 699 | } |
| 700 | |
| 701 | if ( ! $this->is_field_requirement_controlled_by_woocommerce( $key ) ) { |
| 702 | if ( $field['required'] == 1 ) { |
| 703 | $new[ $key ]['required'] = true; |
| 704 | } else { |
| 705 | $new[ $key ]['required'] = false; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | if ( isset( $field['label'] ) ) { |
| 710 | $new[ $key ]['label'] = wpdesk__( $field['label'], 'flexible-checkout-fields' ); |
| 711 | } |
| 712 | |
| 713 | if ( isset( $field['placeholder'] ) ) { |
| 714 | $new[ $key ]['placeholder'] = wpdesk__( $field['placeholder'], 'flexible-checkout-fields' ); |
| 715 | } else { |
| 716 | $new[ $key ]['placeholder'] = ''; |
| 717 | } |
| 718 | |
| 719 | if ( isset( $field['class'] ) ) { |
| 720 | if ( is_array( $field['class'] ) ) { |
| 721 | $new[ $key ]['class'] = explode( ' ', esc_attr( implode( ' ', $field['class'] ) ) ); |
| 722 | } else { |
| 723 | $new[ $key ]['class'] = explode( ' ', esc_attr( $field['class'] ) ); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | if ( ! empty( $field['name'] ) ) { |
| 728 | if ( ( $field['name'] === 'billing_country' || $field['name'] === 'shipping_country' ) && $field['visible'] == 1 ) { |
| 729 | $new[ $key ]['class'][] = "inspire_checkout_fields_hide"; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | if ( ! empty( $field['type'] ) ) { |
| 734 | $new[ $key ]['type'] = $field['type']; |
| 735 | } |
| 736 | |
| 737 | if ( isset( $field['type'] ) && ( ! empty( $checkout_field_type[ $field['type'] ]['has_options'] ) ) && isset( $field['option'] ) ) { |
| 738 | $field_options = new Flexible_Checkout_Fields_Field_Options( $field['option'], $new[ $key ]['placeholder'], $field['type'] ); |
| 739 | $new[ $key ]['options'] = $field_options->get_options_as_array(); |
| 740 | } |
| 741 | |
| 742 | $new[ $key ]['custom_attributes'] = apply_filters( |
| 743 | 'flexible_checkout_fields_custom_attributes', |
| 744 | $field['custom_attributes'] ?? [], |
| 745 | $field |
| 746 | ); |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | /* added 02-02-2018 */ |
| 751 | foreach ( $fields as $field_key => $field ) { |
| 752 | if ( empty( $new[ $field_key ] ) ) { |
| 753 | $new[ $field_key ] = $field; |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | if ( count( $fields ) ) { |
| 758 | foreach ( $new as $key => $field ) { |
| 759 | if ( empty( $fields[ $key ] ) ) { |
| 760 | $new[ $key ]['custom_field'] = 1; |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | foreach ( $new as $key => $field ) { |
| 766 | $priority += 10; |
| 767 | $new[ $key ]['priority'] = $priority; |
| 768 | } |
| 769 | |
| 770 | return $new; |
| 771 | } else { |
| 772 | return $fields; |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | public function printCheckoutFields( $order, $request_type = null ) { |
| 777 | |
| 778 | $settings = $this->getCheckoutFields( $this->get_settings() ); |
| 779 | |
| 780 | $checkout_field_type = $this->get_fields(); |
| 781 | |
| 782 | if ( ! empty( $settings ) ) { |
| 783 | foreach ( $settings as $key => $type ) { |
| 784 | if ( $request_type == null || $request_type == $key ) { |
| 785 | $return = []; |
| 786 | foreach ( $type as $field ) { |
| 787 | if ( ( isset( $field['custom_field'] ) && $field['custom_field'] == 1 ) |
| 788 | && ( empty( $field['type'] ) || ( ! empty( $checkout_field_type[ $field['type'] ] ) && empty( $checkout_field_type[ $field['type'] ]['exclude_in_admin'] ) ) ) |
| 789 | ) { |
| 790 | if ( $value = wpdesk_get_order_meta( $order, '_' . $field['name'], true ) ) { |
| 791 | if ( isset( $field['type'] ) ) { |
| 792 | $value = apply_filters( 'flexible_checkout_fields_print_value', nl2br( $value ), $field ); |
| 793 | } |
| 794 | |
| 795 | $return[] = sprintf( |
| 796 | '<strong>%1$s</strong>: %2$s', |
| 797 | esc_html( wpdesk__( $field['label'], 'flexible-checkout-fields' ) ), |
| 798 | wp_kses_post( $value ) |
| 799 | ); |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | if ( ! empty( $return ) ) { |
| 807 | echo '<div class="address_flexible_checkout_fields"><p class="form-field form-field-wide">' . implode( '<br />', $return ) . '</p></div>'; |
| 808 | } |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | public function changeCheckoutFields( $fields ) { |
| 813 | return $this->getCheckoutFields( $fields ); |
| 814 | } |
| 815 | |
| 816 | public function changeShippingFields( $fields ) { |
| 817 | return $this->getCheckoutFields( $fields, 'shipping' ); |
| 818 | } |
| 819 | |
| 820 | public function changeBillingFields( $fields ) { |
| 821 | return $this->getCheckoutFields( $fields, 'billing' ); |
| 822 | } |
| 823 | |
| 824 | public function changeOrderFields( $fields ) { |
| 825 | return $this->getCheckoutFields( $fields, 'order' ); |
| 826 | } |
| 827 | |
| 828 | public function addCustomBillingFieldsToAdmin( $order ) { |
| 829 | $this->printCheckoutFields( $order, 'billing' ); |
| 830 | } |
| 831 | |
| 832 | public function addCustomShippingFieldsToAdmin( $order ) { |
| 833 | $this->printCheckoutFields( $order, 'shipping' ); |
| 834 | } |
| 835 | |
| 836 | public function addCustomOrderFieldsToAdmin( $order ) { |
| 837 | $this->printCheckoutFields( $order, 'order' ); |
| 838 | } |
| 839 | |
| 840 | public function addCustomFieldsBillingFields( $fields ) { |
| 841 | return $this->getCheckoutUserFields( $fields, 'billing' ); |
| 842 | } |
| 843 | |
| 844 | public function addCustomFieldsShippingFields( $fields ) { |
| 845 | return $this->getCheckoutUserFields( $fields, 'shipping' ); |
| 846 | } |
| 847 | |
| 848 | public function addCustomFieldsOrderFields( $fields ) { |
| 849 | return $this->getCheckoutUserFields( $fields, 'order' ); |
| 850 | } |
| 851 | |
| 852 | /** |
| 853 | * Update fields on checkout. |
| 854 | * |
| 855 | * @param int $order_id Order id. |
| 856 | * @param array $data Posted data. |
| 857 | */ |
| 858 | function updateCheckoutFields( $order_id, $data ) { |
| 859 | $settings = $this->get_settings(); |
| 860 | if ( ! empty( $settings ) ) { |
| 861 | $fields = array_merge( |
| 862 | isset( $settings['billing'] ) ? $settings['billing'] : array(), |
| 863 | isset( $settings['shipping'] ) ? $settings['shipping'] : array(), |
| 864 | isset( $settings['order'] ) ? $settings['order'] : array() |
| 865 | ); |
| 866 | |
| 867 | foreach ( $data as $key => $value ) { |
| 868 | if ( isset( $fields[ $key ] ) ) { |
| 869 | $fcf_field = new Flexible_Checkout_Fields_Field( $fields[ $key ], $this ); |
| 870 | if ( $fcf_field->is_custom_field() ) { |
| 871 | if ( $fcf_field->get_type() === Flexible_Checkout_Fields_Field_Type_Settings::FIELD_TYPE_TEXTAREA ) { |
| 872 | update_post_meta( $order_id, '_' . $key, sanitize_textarea_field( wp_unslash( $value ) ) ); |
| 873 | } else { |
| 874 | update_post_meta( $order_id, '_' . $key, sanitize_text_field( wp_unslash( $value ) ) ); |
| 875 | } |
| 876 | } |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | do_action( 'flexible_checkout_fields_checkout_update_order_meta', $order_id, $data ); |
| 882 | } |
| 883 | |
| 884 | public static function flexible_checkout_fields_section_settings( $key, $settings ) { |
| 885 | echo 1; |
| 886 | } |
| 887 | |
| 888 | public function woocommerce_get_country_locale_default( $address_fields ) { |
| 889 | return $address_fields; |
| 890 | } |
| 891 | |
| 892 | /** |
| 893 | * Add woocommerce screen ids. |
| 894 | * |
| 895 | * @param array $screen_ids Screen ids. |
| 896 | * |
| 897 | * @return array |
| 898 | */ |
| 899 | public function add_woocommerce_screen_ids( $screen_ids ) { |
| 900 | $screen_ids[] = 'woocommerce_page_inspire_checkout_fields_settings'; |
| 901 | |
| 902 | return $screen_ids; |
| 903 | } |
| 904 | |
| 905 | /** |
| 906 | * Admin enqueue scripts. |
| 907 | */ |
| 908 | public function admin_enqueue_scripts() { |
| 909 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 910 | if (function_exists('get_current_screen')) { |
| 911 | $current_screen = get_current_screen(); |
| 912 | } |
| 913 | |
| 914 | $deps = array( |
| 915 | 'jquery', |
| 916 | 'jquery-ui-sortable', |
| 917 | 'jquery-ui-tooltip', |
| 918 | 'jquery-ui-datepicker', |
| 919 | ); |
| 920 | wp_enqueue_script( 'inspire_checkout_fields_admin_js', trailingslashit( $this->get_plugin_assets_url() ) . 'js/admin' . $suffix . '.js', $deps, $this->scripts_version ); |
| 921 | } |
| 922 | |
| 923 | /** |
| 924 | * Frontend enqueue scripts. |
| 925 | */ |
| 926 | public function wp_enqueue_scripts() { |
| 927 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 928 | if ( is_checkout() || is_account_page() ) { |
| 929 | if ( $this->get_setting_value( 'css_disable' ) != 1 ) { |
| 930 | wp_enqueue_style( 'jquery-ui-style', trailingslashit( $this->get_plugin_assets_url() ) . 'css/jquery-ui' . $suffix . '.css', array(), $this->scripts_version ); |
| 931 | } |
| 932 | |
| 933 | wp_enqueue_style( 'inspire_checkout_fields_public_style', trailingslashit( $this->get_plugin_assets_url() ) . 'css/front' . $suffix . '.css', array(), $this->scripts_version ); |
| 934 | } |
| 935 | if ( is_checkout() || is_account_page() ) { |
| 936 | add_action( 'wp_enqueue_scripts', array( $this, 'wp_localize_jquery_ui_datepicker' ), 1000 ); |
| 937 | |
| 938 | $deps = array( |
| 939 | 'jquery', |
| 940 | 'jquery-ui-datepicker', |
| 941 | ); |
| 942 | wp_register_script( 'inspire_checkout_fields_checkout_js', trailingslashit( $this->get_plugin_assets_url() ) . 'js/checkout' . $suffix . '.js', $deps, $this->scripts_version ); |
| 943 | $translation_array = array( |
| 944 | 'uploading' => __( 'Uploading file...', 'flexible-checkout-fields' ), |
| 945 | ); |
| 946 | wp_localize_script( 'inspire_checkout_fields_checkout_js', 'words', $translation_array ); |
| 947 | wp_enqueue_script( 'inspire_checkout_fields_checkout_js' ); |
| 948 | wp_enqueue_script( 'jquery-ui-datepicker' ); |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | |
| 953 | function wp_localize_jquery_ui_datepicker() { |
| 954 | global $wp_locale; |
| 955 | global $wp_version; |
| 956 | |
| 957 | if ( ! wp_script_is( 'jquery-ui-datepicker', 'enqueued' ) || version_compare( $wp_version, '4.6' ) != - 1 ) { |
| 958 | return; |
| 959 | } |
| 960 | |
| 961 | // Convert the PHP date format into jQuery UI's format. |
| 962 | $datepicker_date_format = str_replace( |
| 963 | array( |
| 964 | 'd', |
| 965 | 'j', |
| 966 | 'l', |
| 967 | 'z', // Day. |
| 968 | 'F', |
| 969 | 'M', |
| 970 | 'n', |
| 971 | 'm', // Month. |
| 972 | 'Y', |
| 973 | 'y' // Year. |
| 974 | ), |
| 975 | array( |
| 976 | 'dd', |
| 977 | 'd', |
| 978 | 'DD', |
| 979 | 'o', |
| 980 | 'MM', |
| 981 | 'M', |
| 982 | 'm', |
| 983 | 'mm', |
| 984 | 'yy', |
| 985 | 'y' |
| 986 | ), |
| 987 | get_option( 'date_format' ) |
| 988 | ); |
| 989 | |
| 990 | $datepicker_defaults = wp_json_encode( array( |
| 991 | 'closeText' => __( 'Close' ), |
| 992 | 'currentText' => __( 'Today' ), |
| 993 | 'monthNames' => array_values( $wp_locale->month ), |
| 994 | 'monthNamesShort' => array_values( $wp_locale->month_abbrev ), |
| 995 | 'nextText' => __( 'Next' ), |
| 996 | 'prevText' => __( 'Previous' ), |
| 997 | 'dayNames' => array_values( $wp_locale->weekday ), |
| 998 | 'dayNamesShort' => array_values( $wp_locale->weekday_abbrev ), |
| 999 | 'dayNamesMin' => array_values( $wp_locale->weekday_initial ), |
| 1000 | 'dateFormat' => $datepicker_date_format, |
| 1001 | 'firstDay' => absint( get_option( 'start_of_week' ) ), |
| 1002 | 'isRTL' => $wp_locale->is_rtl(), |
| 1003 | ) ); |
| 1004 | |
| 1005 | wp_add_inline_script( 'jquery-ui-datepicker', "jQuery(document).ready(function(jQuery){jQuery.datepicker.setDefaults({$datepicker_defaults});});" ); |
| 1006 | } |
| 1007 | |
| 1008 | /** |
| 1009 | * Links filter. |
| 1010 | * |
| 1011 | * @param array $links Links. |
| 1012 | * |
| 1013 | * @return array |
| 1014 | */ |
| 1015 | public function links_filter( $links ) { |
| 1016 | $plugin_links = array( |
| 1017 | '<a href="' . admin_url( 'admin.php?page=inspire_checkout_fields_settings' ) . '">' . __( 'Settings', 'flexible-checkout-fields' ) . '</a>', |
| 1018 | '<a href="' . esc_url( apply_filters( 'flexible_checkout_fields/short_url', '#', 'fcf-settings-row-action-docs' ) ) . '" target="_blank">' . __( 'Docs', 'flexible-checkout-fields' ) . '</a>', |
| 1019 | '<a href="' . esc_url( apply_filters( 'flexible_checkout_fields/short_url', '#', 'fcf-settings-row-action-support' ) ) . '" target="_blank">' . __( 'Support', 'flexible-checkout-fields' ) . '</a>', |
| 1020 | ); |
| 1021 | |
| 1022 | if ( ! wpdesk_is_plugin_active( 'flexible-checkout-fields-pro/flexible-checkout-fields-pro.php' ) ) { |
| 1023 | $plugin_links[] = '<a href="' . esc_url( apply_filters( 'flexible_checkout_fields/short_url', '#', 'fcf-settings-row-action-upgrade' ) ) . '" target="_blank" style="color:#d64e07;font-weight:bold;">' . __( 'Upgrade', 'flexible-checkout-fields' ) . '</a>'; |
| 1024 | } |
| 1025 | |
| 1026 | return array_merge( $plugin_links, $links ); |
| 1027 | } |
| 1028 | |
| 1029 | } |
| 1030 |