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