Skip to content

Commit

Permalink
feat: Reimplemented Field handling
Browse files Browse the repository at this point in the history
  • Loading branch information
seebeen committed Sep 25, 2024
1 parent 1fa222f commit e1beb40
Show file tree
Hide file tree
Showing 22 changed files with 796 additions and 604 deletions.
1 change: 1 addition & 0 deletions .github/workflows/reusable_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ jobs:
name: Publish
runs-on: ubuntu-latest
needs: release
if: ${{ inputs.dry_run == false }}
steps:
- name: Download Package
uses: actions/download-artifact@v4
Expand Down
1 change: 1 addition & 0 deletions assets/scripts/@types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ declare global {
const _: _.UnderscoreStatic;

interface Window {
wc_address_i18n_params: any;
wp: {
template: (id: string) => _.CompiledTemplate;
};
Expand Down
70 changes: 61 additions & 9 deletions assets/scripts/frontend/controllers/address-page.controller.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,77 @@
const $ = jQuery;

export default class AddressPageController {
private $inputs: JQuery<HTMLInputElement>;
private selector = '.entity-type-control input';
private locale: Record<string, string>;

public init(): void {
this.$inputs = $('.entity-type-control input[type="radio"]');
this.locale = JSON.parse(window.wc_address_i18n_params.locale_fields);
}

public finalize(): void {
this.$inputs.on('click', ({ currentTarget }) =>
this.toggleFields($(currentTarget)),
$(document.body).on('change refresh', this.selector, (e) => {
console.log('change refresh');
this.toggleEntityType($(e.target));
});

$(document.body).on('country_to_state_changing', () => {
window.setTimeout(
() => this.toggleEntityType($(`${this.selector}:checked`)),
100,
);
console.log('country_to_state_changing');
});
}

private toggleEntityType($input: JQuery<HTMLInputElement>): void {
const isCompany = $input.val() === 'company';

console.log($input.is(':checked'), $input.val());

this.isRequired($<HTMLParagraphElement>('.entity-type-toggle'), isCompany);
}

private isRequired(
$field: JQuery<HTMLParagraphElement>,
required: boolean,
): void {
$field.find('input').prop({
'aria-required': required,
disabled: !required,
});

if (required) {
$field.find('label .optional').remove();
$field.addClass('shown validate-required');

if ($field.find('label .required').length === 0) {
$field
.find('label')
.append(
`<abbr class="required" title="${window.wc_address_i18n_params.i18n_required_text}">*</abbr>`,
);
}

return;
}

$field.find('label .required').remove();
$field.removeClass(
'shown validate-required woocommerce-invalid woocommerce-invalid-required-field',
);
this.toggleFields(this.$inputs.filter(':checked'));

if ($field.find('label .optional').length === 0) {
$field
.find('label')
.append(
`<abbr class="optional" title="${window.wc_address_i18n_params.i18n_optional_text}">${window.wc_address_i18n_params.i18n_optional_text}</abbr>`,
);
}
}

private toggleFields($toggle: JQuery<HTMLInputElement>): void {
const isPerson = $toggle.attr('value') === 'person';

$('.hide-if-person').toggleClass('shown', !isPerson).find('input').prop({
required: !isPerson,
disabled: isPerson,
});
$('.hide-if-person').toggleClass('shown', !isPerson);
}
}
6 changes: 3 additions & 3 deletions assets/styles/components/_billing_fields.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
}
}

.hide-if-person {
display: none;
.entity-type-toggle {
display: none !important;

&.shown {
display: block;
display: block !important;
}
}
}
Expand Down
16 changes: 11 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@
},
"autoload": {
"psr-4": {
"Oblak\\WooCommerce\\Serbian_Addons\\": "lib"
"Oblak\\WCSRB\\": "lib/"
},
"classmap": [
"lib/Admin/",
"lib/Gateway/",
"lib/QR/"
],
"files": [
"lib/Utils/wcsrb-core.php",
"lib/Utils/wcsrb-helpers.php",
"lib/Utils/wcsrb-payment-slip.php",
"lib/Utils/wcsrb-settings.php"
"lib/Functions/wcsrb-address-field-fns.php",
"lib/Functions/wcsrb-core.php",
"lib/Functions/wcsrb-helpers.php",
"lib/Functions/wcsrb-payment-slip.php",
"lib/Functions/wcsrb-settings.php"
]
},
"config": {
Expand Down
2 changes: 1 addition & 1 deletion config/assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
'base_uri' => plugins_url( 'dist', WCRS_PLUGIN_BASE ),
'id' => 'wcrs',
'manifest' => 'assets.php',
'priority' => 50,
'priority' => 500,
'version' => WCRS_VERSION,
);
60 changes: 60 additions & 0 deletions lib/Admin/Edit_User_Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Edit_User_Controller class file.
*
* @package Serbian Addons for WooCommerce
*/

namespace Oblak\WCSRB\Admin;

use Oblak\WP\Abstracts\Hook_Caller;
use Oblak\WP\Decorators\Filter;
use Oblak\WP\Decorators\Hookable;

/**
* Changes the fields on the checkout page
*
* @since 3.8.0
*/
#[Hookable( 'admin_init', 99 )]
class Edit_User_Controller extends Hook_Caller {
/**
* Adds the company information to the customer meta fields.
*
* @param array $fields Customer meta fields.
* @return array Modified customer meta fields
*/
#[Filter( 'woocommerce_customer_meta_fields' )]
public function modify_customer_meta_fields( array $fields ): array {
$company = \array_search( 'billing_company', \array_keys( $fields['billing']['fields'] ), true );

//phpcs:disable SlevomatCodingStandard.Arrays.AlphabeticallySortedByKeys.IncorrectKeyOrder
$fields['billing']['fields'] = \array_merge(
\array_slice( $fields['billing']['fields'], 0, $company ),
array(
'billing_type' => array(
'description' => '',
'label' => \__( 'Customer type', 'serbian-addons-for-woocommerce' ),
'options' => \wcsrb_get_entity_types(),
'type' => 'select',
),
'billing_company' => $fields['billing']['fields']['billing_company'],
'billing_mb' => array(
'description' => '',
'label' => \__( 'Company Number', 'serbian-addons-for-woocommerce' ),
'type' => 'text',
),
'billing_pib' => array(
'description' => '',
'label' => \__( 'Tax Number', 'serbian-addons-for-woocommerce' ),
'type' => 'text',
),

),
\array_slice( $fields['billing']['fields'], $company ),
);
//phpcs:enable SlevomatCodingStandard.Arrays.AlphabeticallySortedByKeys.IncorrectKeyOrder

return $fields;
}
}
41 changes: 30 additions & 11 deletions lib/Serbian_WooCommerce.php → lib/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
* @package Serbian Addons for WooCommerce
*/

namespace Oblak\WooCommerce\Serbian_Addons;
namespace Oblak\WCSRB;

use Oblak\WCSRB\Services\Field_Validator;
use Oblak\WooCommerce\Serbian_Addons as Legacy;
use Oblak\WP\Decorators\Action;
use Oblak\WP\Decorators\Filter;
use Oblak\WP\Traits\Hook_Processor_Trait;
Expand All @@ -16,7 +18,7 @@
/**
* Main plugin class
*/
class Serbian_WooCommerce {
class App {
use Hook_Processor_Trait;
use Settings_API_Methods;
use Singleton;
Expand All @@ -29,6 +31,13 @@ class Serbian_WooCommerce {
*/
public string $version = WCRS_VERSION;

/**
* Field validator instance.
*
* @var Field_Validator
*/
protected Field_Validator $validator;

/**
* Private constructor
*/
Expand All @@ -43,11 +52,12 @@ protected function __construct() {
*/
protected function get_dependencies(): array {
return array(
Admin\Admin_Core::class,
Core\Template_Extender::class,
Checkout\Field_Customizer::class,
Checkout\Field_Validator::class,
Order\Field_Display::class,
Admin\Edit_User_Controller::class,
Core\Address_Display_Controller::class,
Core\Address_Field_Controller::class,
Core\Address_Validate_Controller::class,
Utils\Template_Extender::class,
Legacy\Admin\Admin_Core::class,
);
}

Expand All @@ -63,9 +73,9 @@ public function run_hooks() {
*/
#[Action( tag: 'plugins_loaded', priority: 1000 )]
public function on_plugins_loaded() {
Core\Installer::instance()->init();
Utils\Installer::instance()->init();

$s = \load_plugin_textdomain(
\load_plugin_textdomain(
domain: 'serbian-addons-for-woocommerce',
plugin_rel_path: \dirname( WCRS_PLUGIN_BASE ) . '/languages',
);
Expand All @@ -78,7 +88,7 @@ public function on_plugins_loaded() {
public function load_plugin_settings() {
try {
$this->load_options( 'wcsrb_settings' );
} catch ( \Exception ) {
} catch ( \Exception | \Error ) {
\wc_get_logger()->critical(
'Failed to load plugin settings',
array(
Expand Down Expand Up @@ -133,7 +143,7 @@ public function declare_hpos_compatibility() {
*/
#[Filter( tag: 'woocommerce_payment_gateways', priority: 50 )]
public function add_payment_gateways( $gateways ) {
$gateways[] = Gateway\Gateway_Payment_Slip::class;
$gateways[] = Legacy\Gateway\Gateway_Payment_Slip::class;
return $gateways;
}

Expand Down Expand Up @@ -173,4 +183,13 @@ public function check_asset_necessity( bool $load, string $script ) {
default => $load,
};
}

/**
* Gets the field validator instance.
*
* @return Field_Validator
*/
public function validator(): Field_Validator {
return $this->validator ??= new Field_Validator();
}
}
Loading

0 comments on commit e1beb40

Please sign in to comment.