Plugin Directory

Changeset 3092380


Ignore:
Timestamp:
05/25/2024 04:45:12 AM (22 months ago)
Author:
seebeen
Message:

Update to version 3.6.0 from GitHub

Location:
serbian-addons-for-woocommerce
Files:
8 edited
1 copied

Legend:

Unmodified
Added
Removed
  • serbian-addons-for-woocommerce/tags/3.6.0/lib/Checkout/Field_Validator.php

    r3018381 r3092380  
    99namespace Oblak\WooCommerce\Serbian_Addons\Checkout;
    1010
     11use Oblak\WP\Decorators\Action;
    1112use Oblak\WP\Decorators\Hookable;
    12 
    13 use function Oblak\validateMB;
    14 use function Oblak\validatePIB;
    1513
    1614/**
     
    1917#[Hookable( 'woocommerce_init', 99 )]
    2018class Field_Validator {
    21 
    2219    /**
    23      * Fields to validate
    24      *
    25      * @var array
    26      */
    27     private static $fields_to_check = array(
    28         'billing_company',
    29         'billing_pib',
    30         'billing_mb',
    31     );
    32 
    33     /**
    34      * Class constructor
     20     *  Constructor
    3521     */
    3622    public function __construct() {
    37         add_filter( 'woocommerce_after_save_address_validation', array( $this, 'validate_saved_address' ), 99, 2 );
    38         add_filter( 'woocommerce_after_checkout_validation', array( $this, 'validate_checkout_fields' ), 99, 2 );
     23        if ( \class_exists( '\XWP\Hook\Invoker' ) ) {
     24            return;
     25        }
     26
     27        \xwp_invoke_hooked_methods( $this );
    3928    }
    40 
    4129    /**
    4230     * Adds custom validation to billing address field saving
    4331     *
    44      * @param  int    $user_id      Current User ID.
    45      * @param  string $load_address Address type being edited - billing or shipping.
     32     * @param  int    $user_id Current User ID.
     33     * @param  string $type    Address type being edited - billing or shipping.
    4634     */
    47     public function validate_saved_address( $user_id, $load_address ) {
    48         if ( 'shipping' === $load_address ) {
     35    #[Action( 'woocommerce_after_save_address_validation', 99 )]
     36    public function validate_saved_address( int $user_id, string $type ) {
     37        // phpcs:ignore WordPress.Security.NonceVerification.Missing
     38        $posted = \wc_clean( \wp_unslash( $_POST ) );
     39
     40        // If we're not validating billing, we don't need to do anything.
     41        if ( ! $this->can_validate( $posted, $type ) ) {
    4942            return;
    5043        }
    5144
    52         $post_data = wc_clean( wp_unslash( $_POST ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
    53         $notices   = WC()->session->get( 'wc_notices', array() );
    54         $errors    = $notices['error'] ?? array();
     45        $validators = $this->get_field_validators( \current_filter() );
     46        $notices    = $this->filter_notices( \array_keys( $validators ) );
    5547
    56         // Unset all errors if they pertain to our fields.
    57         foreach ( $errors as $index => $error_notice ) {
    58             if ( in_array( $error_notice['data']['id'], self::$fields_to_check, true ) ) {
    59                 unset( $errors[ $index ] );
     48        foreach ( $validators as $field => $args ) {
     49            if ( $args['validator']( $posted[ $field ] ) ) {
     50                continue;
    6051            }
    61         }
    6252
    63         // If the customer is a person, we don't need to validate anything else. Reset the notices, and bailout.
    64         if ( 'person' === $post_data['billing_type'] ) {
    65             $notices['error'] = $errors;
    66             WC()->session->set( 'wc_notices', $notices );
    67 
    68             return;
    69         }
    70 
    71         if ( ! validateMB( $post_data['billing_mb'] ) ) {
    72             $errors[] = array(
    73                 'notice' => __( 'Company number is invalid', 'serbian-addons-for-woocommerce' ),
     53            $notices['error'][] = array(
    7454                'data'   => array(
    75                     'id' => 'billing_mb',
     55                    'id' => $field,
    7656                ),
     57                'notice' => $args['message'],
    7758            );
    7859        }
     60        \WC()->session->set( 'wc_notices', $notices );
     61    }
    7962
    80         if ( ! validatePIB( $post_data['billing_pib'] ) ) {
    81             $errors[] = array(
    82                 'notice' => __( 'Company Tax Number is invalid', 'serbian-addons-for-woocommerce' ),
    83                 'data'   => array(
    84                     'id' => 'billing_pib',
    85                 ),
    86             );
    87         }
    88 
    89         $notices['error'] = $errors;
    90 
    91         WC()->session->set( 'wc_notices', $notices );
    92     }
    9363
    9464    /**
     
    9868     * @param  \WP_Error $error Error object.
    9969     */
     70    #[Action( 'woocommerce_after_checkout_validation', 0 )]
    10071    public function validate_checkout_fields( $data, $error ) {
    101         foreach ( self::$fields_to_check as $field ) {
    102             if ( ! empty( $error->get_all_error_data( $field . '_required' ) ) ) {
    103                 $error->remove( $field . '_required' );
    104             }
     72        $fields = $this->get_field_validators( \current_filter() );
     73
     74        foreach ( \array_keys( $fields ) as $field ) {
     75            $error->remove( $field . '_required' );
    10576        }
    10677
    107         if ( 'company' !== $data['billing_type'] || 'RS' !== $data['billing_country'] ) {
     78        if ( ! $this->can_validate( $data ) ) {
    10879            return;
    10980        }
    11081
    111         if ( '' === $data['billing_company'] ) {
    112             $error->add( 'billing_company_required', __( 'Company name is required', 'serbian-addons-for-woocommerce' ) );
    113         }
    114         if ( ! validateMB( $data['billing_mb'] ) ) {
    115             $error->add( 'billing_mb_required', __( 'Company number is invalid', 'serbian-addons-for-woocommerce' ) );
    116         }
     82        foreach ( $fields as $field => $args ) {
     83            if ( $args['validator']( $data[ $field ] ) ) {
     84                continue;
     85            }
    11786
    118         if ( ! validatePIB( $data['billing_pib'] ) ) {
    119             $error->add( 'billing_pib_required', __( 'Company Tax Number is invalid', 'serbian-addons-for-woocommerce' ) );
     87            $error->add( $args['code'], $args['message'], array( 'id' => $field ) );
    12088        }
    12189    }
     90
     91    /**
     92     * Checks if the current address can be validated.
     93     *
     94     * @param  array  $fields    Address fields.
     95     * @param  string $addr_type Address type being validated.
     96     * @return bool
     97     */
     98    protected function can_validate( array $fields, string $addr_type = 'billing' ): bool {
     99        $type    = $fields['billing_type'] ??= '';
     100        $country = $fields['billing_country'] ??= '';
     101
     102        return 'billing' === $addr_type && 'company' === $type && 'RS' === $country;
     103    }
     104
     105    /**
     106     * Returns the field validators for the given action.
     107     *
     108     * @param  string $action Action being performed.
     109     * @return array
     110     */
     111    protected function get_field_validators( string $action ) {
     112        $args = array(
     113            'billing_company' => array(
     114                'code'      => 'billing_company_required',
     115                'message'   => \__( 'Company name is required', 'serbian-addons-for-woocommerce' ),
     116                'validator' => static fn( $val ) => '' !== $val,
     117            ),
     118            'billing_mb'      => array(
     119                'code'      => 'billing_mb_validation',
     120                'message'   => \__( 'Company number is invalid', 'serbian-addons-for-woocommerce' ),
     121                'validator' => '\Oblak\validateMB',
     122            ),
     123            'billing_pib'     => array(
     124                'code'      => 'billing_pib_validation',
     125                'message'   => \__( 'Company Tax Number is invalid', 'serbian-addons-for-woocommerce' ),
     126                'validator' => '\Oblak\validatePIB',
     127            ),
     128        );
     129
     130        /**
     131         * Returns the validation arguments for the given action.
     132         *
     133         * @param array  $args   Validation arguments.
     134         * @param string $action Action being performed.
     135         *
     136         * @return array
     137         *
     138         * @since 3.6.0
     139         */
     140        return \apply_filters( 'wcrs_field_validators', $args, $action );
     141    }
     142
     143    /**
     144     * Filters out notices for fields that have been validated.
     145     *
     146     * @param  array $fields Fields that have been validated.
     147     * @return array
     148     */
     149    protected function filter_notices( array $fields ): array {
     150        $notices = \WC()->session->get( 'wc_notices', array() );
     151
     152        $notices['error'] = \array_filter(
     153            $notices['error'] ?? array(),
     154            static fn( $e ) => ! \in_array( $e['data']['id'] ?? '', $fields, true )
     155        );
     156
     157        return $notices;
     158    }
    122159}
  • serbian-addons-for-woocommerce/tags/3.6.0/lib/Order/Field_Display.php

    r3018381 r3092380  
    99namespace Oblak\WooCommerce\Serbian_Addons\Order;
    1010
    11 use Oblak\WP\Abstracts\Hook_Runner;
     11use Oblak\WP\Decorators\Filter;
    1212use Oblak\WP\Decorators\Hookable;
    1313use WC_Customer;
     
    1818 */
    1919#[Hookable( 'woocommerce_init', 99 )]
    20 class Field_Display extends Hook_Runner {
     20class Field_Display {
     21    /**
     22     *  Constructor
     23     */
     24    public function __construct() {
     25        if ( \class_exists( '\XWP\Hook\Invoker' ) ) {
     26            return;
     27        }
     28
     29        \xwp_invoke_hooked_methods( $this );
     30    }
    2131    /**
    2232     * Modifies the address format for Serbia to include necessary company information
     
    2434     * @param  string[] $formats Address formats.
    2535     * @return string[]          Modified address formats
    26      *
    27      * @hook     woocommerce_localisation_address_formats
    28      * @type     filter
    29      * @priority filter:woocommerce_serbian_localization_address_priority:100
    30      */
     36     */
     37    #[Filter( 'woocommerce_localisation_address_formats', 'wcrs_localization_address_priority' )]
    3138    public function modify_address_format( $formats ) {
    32         add_filter( 'woocommerce_formatted_address_force_country_display', '__return_true' );
     39        \add_filter( 'woocommerce_formatted_address_force_country_display', '__return_true' );
    3340
    3441        $formats['RS'] = "{name}\n{company}\n{mb}\n{pib}\n{address_1}\n{address_2}\n{postcode} {city}, {state} {country}";
    3542
    36         if ( WCSRB()->get_settings( 'general', 'remove_unneeded_fields' ) ) {
    37             $formats['RS'] = strtr(
    38                 $formats['RS'],
    39                 array(
    40                     '{state}'     => '',
    41                     '{address_2}' => '',
    42                 )
    43             );
     43        if ( \WCSRB()->get_settings( 'general', 'remove_unneeded_fields' ) ) {
     44            $formats['RS'] = \str_replace( array( '{state}', '{address_2}' ), '', $formats['RS'] );
    4445        }
    4546
     
    5859     * @param  array    $args          Address data.
    5960     * @return string[]                Modified replacements array
    60      *
    61      * @hook     woocommerce_formatted_address_replacements
    62      * @type     filter
    63      * @priority 99
    64      */
     61     */
     62    #[Filter( 'woocommerce_formatted_address_replacements', 99 )]
    6563    public function modify_address_replacements( $replacements, $args ) {
    6664        $replacements['{type}'] = $args['type'] ?? "\n";
     
    8078     * @param  string $address_type Address type (billing or shipping).
    8179     * @return array                Modified address data array
    82      *
    83      * @hook     woocommerce_my_account_my_address_formatted_address
    84      * @type     filter
    85      * @priority 99
    86      */
     80     */
     81    #[Filter( 'woocommerce_my_account_my_address_formatted_address', 99 )]
    8782    public function modify_account_formatted_address( $address, $customer_id, $address_type ) {
    8883        if ( 'billing' !== $address_type ) {
     
    9287        $customer = new WC_Customer( $customer_id );
    9388
    94         $user_type   = ! empty( $customer->get_meta( 'billing_type', true ) ) ? $customer->get_meta( 'billing_type', true ) : 'person';
     89        // phpcs:ignore Universal.Operators.DisallowShortTernary.Found
     90        $user_type   = $customer->get_meta( 'billing_type', true ) ?: 'person';
    9591        $company_num = $customer->get_meta( 'billing_mb', true );
    9692        $company_tax = $customer->get_meta( 'billing_pib', true );
     
    107103     * @param  WC_Order $order   Order object.
    108104     * @return array             Modified address data array
    109      *
    110      * @hook     woocommerce_order_formatted_billing_address
    111      * @type     filter
    112      * @priority 99
    113      */
     105     */
     106    #[Filter( 'woocommerce_order_formatted_billing_address', 99 )]
    114107    public function modify_order_formatted_address( $address, $order ) {
    115108        return $this->address_modifier(
     
    117110            $order->get_meta( '_billing_type', true ),
    118111            $order->get_meta( '_billing_mb', true ),
    119             $order->get_meta( '_billing_pib', true )
     112            $order->get_meta( '_billing_pib', true ),
    120113        );
    121114    }
     
    145138        $address['last_name']  = "\n";
    146139
    147         $address['mb']  = sprintf(
     140        if ( $company_number ) {
     141            $address['mb'] = \sprintf(
     142                '%s: %s',
     143                \_x( 'Company Number', 'Address display', 'serbian-addons-for-woocommerce' ),
     144                $company_number,
     145            );
     146        }
     147        $address['pib'] = \sprintf(
    148148            '%s: %s',
    149             _x( 'Company Number', 'Address display', 'serbian-addons-for-woocommerce' ),
    150             $company_number
    151         );
    152         $address['pib'] = sprintf(
    153             '%s: %s',
    154             _x( 'Tax Identification Number', 'Address display', 'serbian-addons-for-woocommerce' ),
    155             $tax_number
     149            \_x( 'Tax Identification Number', 'Address display', 'serbian-addons-for-woocommerce' ),
     150            $tax_number,
    156151        );
    157152
     
    165160     * @param  WC_Order $order Order object.
    166161     * @return string          Modified Buyer name
    167      *
    168      * @hook     woocommerce_admin_order_buyer_name
    169      * @type     filter
    170      * @priority 99
    171      */
     162     */
     163    #[Filter( 'woocommerce_admin_order_buyer_name', 99 )]
    172164    public function modify_order_buyer_name( $buyer, $order ) {
    173         return ( 'RS' === $order->get_billing_country() && 'company' === $order->get_meta( '_billing_type', true ) )
     165        return 'RS' === $order->get_billing_country() && 'company' === $order->get_meta( '_billing_type', true )
    174166            ? $order->get_billing_company()
    175167            : $buyer;
    176168    }
     169
     170    /**
     171     * Adds the company information to the customer meta fields.
     172     *
     173     * @param  array $fields Customer meta fields.
     174     * @return array         Modified customer meta fields
     175     */
     176    #[Filter( 'woocommerce_customer_meta_fields' )]
     177    public function modify_customer_meta_fields( array $fields ): array {
     178        $billing = array();
     179
     180        foreach ( $fields['billing']['fields'] as $field => $args ) {
     181            if ( 'billing_company' !== $field ) {
     182                $billing[ $field ] = $args;
     183                continue;
     184            }
     185
     186            $billing['billing_type'] = array(
     187                'label'   => \__( 'Customer type', 'serbian-addons-for-woocommerce' ),
     188                'options' => \Oblak\WooCommerce\Serbian_Addons\Utils\get_entity_types(),
     189                'type'    => 'select',
     190            );
     191
     192            $billing[ $field ] = $args;
     193
     194            $billing['billing_mb'] = array(
     195                'label' => \__( 'Company Number', 'serbian-addons-for-woocommerce' ),
     196                'type'  => 'text',
     197            );
     198
     199            $billing['billing_pib'] = array(
     200                'label' => \__( 'Tax Number', 'serbian-addons-for-woocommerce' ),
     201                'type'  => 'text',
     202            );
     203        }
     204
     205        $fields['billing']['fields'] = $billing;
     206
     207        return $fields;
     208    }
    177209}
  • serbian-addons-for-woocommerce/tags/3.6.0/readme.txt

    r3080934 r3092380  
    88WC requires at least: 8.0
    99WC tested up to: 8.3
    10 Stable tag: 3.5.10
     10Stable tag: 3.6.0
    1111License: GPLv2 or later
    1212License URI: http://www.gnu.org/licenses/gpl-2.0.html
  • serbian-addons-for-woocommerce/tags/3.6.0/serbian-addons-for-woocommerce.php

    r3080934 r3092380  
    44 * Plugin URI:           https://oblak.studio/open-source/srpski-woocommerce
    55 * Description:          Various addons and tweaks that make WooCommerce compatible with Serbian bureaucracy.
    6  * Version:              3.5.10
     6 * Version:              3.6.0
    77 * Requires PHP:         8.0
    88 * Author:               Oblak Studio
     
    2525defined( 'WCRS_VERSION' ) || define( 'WCRS_VERSION', '3.4.0' );
    2626
    27 add_action(
    28     'woocommerce_loaded',
    29     static function () {
    30         require __DIR__ . '/vendor/autoload_packages.php';
    31         \WCSRB();
    32     },
    33     20,
    34 );
     27require __DIR__ . '/vendor/autoload_packages.php';
     28
     29add_action( 'woocommerce_loaded', 'WCSRB', 0 );
  • serbian-addons-for-woocommerce/trunk/lib/Checkout/Field_Validator.php

    r3018381 r3092380  
    99namespace Oblak\WooCommerce\Serbian_Addons\Checkout;
    1010
     11use Oblak\WP\Decorators\Action;
    1112use Oblak\WP\Decorators\Hookable;
    12 
    13 use function Oblak\validateMB;
    14 use function Oblak\validatePIB;
    1513
    1614/**
     
    1917#[Hookable( 'woocommerce_init', 99 )]
    2018class Field_Validator {
    21 
    2219    /**
    23      * Fields to validate
    24      *
    25      * @var array
    26      */
    27     private static $fields_to_check = array(
    28         'billing_company',
    29         'billing_pib',
    30         'billing_mb',
    31     );
    32 
    33     /**
    34      * Class constructor
     20     *  Constructor
    3521     */
    3622    public function __construct() {
    37         add_filter( 'woocommerce_after_save_address_validation', array( $this, 'validate_saved_address' ), 99, 2 );
    38         add_filter( 'woocommerce_after_checkout_validation', array( $this, 'validate_checkout_fields' ), 99, 2 );
     23        if ( \class_exists( '\XWP\Hook\Invoker' ) ) {
     24            return;
     25        }
     26
     27        \xwp_invoke_hooked_methods( $this );
    3928    }
    40 
    4129    /**
    4230     * Adds custom validation to billing address field saving
    4331     *
    44      * @param  int    $user_id      Current User ID.
    45      * @param  string $load_address Address type being edited - billing or shipping.
     32     * @param  int    $user_id Current User ID.
     33     * @param  string $type    Address type being edited - billing or shipping.
    4634     */
    47     public function validate_saved_address( $user_id, $load_address ) {
    48         if ( 'shipping' === $load_address ) {
     35    #[Action( 'woocommerce_after_save_address_validation', 99 )]
     36    public function validate_saved_address( int $user_id, string $type ) {
     37        // phpcs:ignore WordPress.Security.NonceVerification.Missing
     38        $posted = \wc_clean( \wp_unslash( $_POST ) );
     39
     40        // If we're not validating billing, we don't need to do anything.
     41        if ( ! $this->can_validate( $posted, $type ) ) {
    4942            return;
    5043        }
    5144
    52         $post_data = wc_clean( wp_unslash( $_POST ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
    53         $notices   = WC()->session->get( 'wc_notices', array() );
    54         $errors    = $notices['error'] ?? array();
     45        $validators = $this->get_field_validators( \current_filter() );
     46        $notices    = $this->filter_notices( \array_keys( $validators ) );
    5547
    56         // Unset all errors if they pertain to our fields.
    57         foreach ( $errors as $index => $error_notice ) {
    58             if ( in_array( $error_notice['data']['id'], self::$fields_to_check, true ) ) {
    59                 unset( $errors[ $index ] );
     48        foreach ( $validators as $field => $args ) {
     49            if ( $args['validator']( $posted[ $field ] ) ) {
     50                continue;
    6051            }
    61         }
    6252
    63         // If the customer is a person, we don't need to validate anything else. Reset the notices, and bailout.
    64         if ( 'person' === $post_data['billing_type'] ) {
    65             $notices['error'] = $errors;
    66             WC()->session->set( 'wc_notices', $notices );
    67 
    68             return;
    69         }
    70 
    71         if ( ! validateMB( $post_data['billing_mb'] ) ) {
    72             $errors[] = array(
    73                 'notice' => __( 'Company number is invalid', 'serbian-addons-for-woocommerce' ),
     53            $notices['error'][] = array(
    7454                'data'   => array(
    75                     'id' => 'billing_mb',
     55                    'id' => $field,
    7656                ),
     57                'notice' => $args['message'],
    7758            );
    7859        }
     60        \WC()->session->set( 'wc_notices', $notices );
     61    }
    7962
    80         if ( ! validatePIB( $post_data['billing_pib'] ) ) {
    81             $errors[] = array(
    82                 'notice' => __( 'Company Tax Number is invalid', 'serbian-addons-for-woocommerce' ),
    83                 'data'   => array(
    84                     'id' => 'billing_pib',
    85                 ),
    86             );
    87         }
    88 
    89         $notices['error'] = $errors;
    90 
    91         WC()->session->set( 'wc_notices', $notices );
    92     }
    9363
    9464    /**
     
    9868     * @param  \WP_Error $error Error object.
    9969     */
     70    #[Action( 'woocommerce_after_checkout_validation', 0 )]
    10071    public function validate_checkout_fields( $data, $error ) {
    101         foreach ( self::$fields_to_check as $field ) {
    102             if ( ! empty( $error->get_all_error_data( $field . '_required' ) ) ) {
    103                 $error->remove( $field . '_required' );
    104             }
     72        $fields = $this->get_field_validators( \current_filter() );
     73
     74        foreach ( \array_keys( $fields ) as $field ) {
     75            $error->remove( $field . '_required' );
    10576        }
    10677
    107         if ( 'company' !== $data['billing_type'] || 'RS' !== $data['billing_country'] ) {
     78        if ( ! $this->can_validate( $data ) ) {
    10879            return;
    10980        }
    11081
    111         if ( '' === $data['billing_company'] ) {
    112             $error->add( 'billing_company_required', __( 'Company name is required', 'serbian-addons-for-woocommerce' ) );
    113         }
    114         if ( ! validateMB( $data['billing_mb'] ) ) {
    115             $error->add( 'billing_mb_required', __( 'Company number is invalid', 'serbian-addons-for-woocommerce' ) );
    116         }
     82        foreach ( $fields as $field => $args ) {
     83            if ( $args['validator']( $data[ $field ] ) ) {
     84                continue;
     85            }
    11786
    118         if ( ! validatePIB( $data['billing_pib'] ) ) {
    119             $error->add( 'billing_pib_required', __( 'Company Tax Number is invalid', 'serbian-addons-for-woocommerce' ) );
     87            $error->add( $args['code'], $args['message'], array( 'id' => $field ) );
    12088        }
    12189    }
     90
     91    /**
     92     * Checks if the current address can be validated.
     93     *
     94     * @param  array  $fields    Address fields.
     95     * @param  string $addr_type Address type being validated.
     96     * @return bool
     97     */
     98    protected function can_validate( array $fields, string $addr_type = 'billing' ): bool {
     99        $type    = $fields['billing_type'] ??= '';
     100        $country = $fields['billing_country'] ??= '';
     101
     102        return 'billing' === $addr_type && 'company' === $type && 'RS' === $country;
     103    }
     104
     105    /**
     106     * Returns the field validators for the given action.
     107     *
     108     * @param  string $action Action being performed.
     109     * @return array
     110     */
     111    protected function get_field_validators( string $action ) {
     112        $args = array(
     113            'billing_company' => array(
     114                'code'      => 'billing_company_required',
     115                'message'   => \__( 'Company name is required', 'serbian-addons-for-woocommerce' ),
     116                'validator' => static fn( $val ) => '' !== $val,
     117            ),
     118            'billing_mb'      => array(
     119                'code'      => 'billing_mb_validation',
     120                'message'   => \__( 'Company number is invalid', 'serbian-addons-for-woocommerce' ),
     121                'validator' => '\Oblak\validateMB',
     122            ),
     123            'billing_pib'     => array(
     124                'code'      => 'billing_pib_validation',
     125                'message'   => \__( 'Company Tax Number is invalid', 'serbian-addons-for-woocommerce' ),
     126                'validator' => '\Oblak\validatePIB',
     127            ),
     128        );
     129
     130        /**
     131         * Returns the validation arguments for the given action.
     132         *
     133         * @param array  $args   Validation arguments.
     134         * @param string $action Action being performed.
     135         *
     136         * @return array
     137         *
     138         * @since 3.6.0
     139         */
     140        return \apply_filters( 'wcrs_field_validators', $args, $action );
     141    }
     142
     143    /**
     144     * Filters out notices for fields that have been validated.
     145     *
     146     * @param  array $fields Fields that have been validated.
     147     * @return array
     148     */
     149    protected function filter_notices( array $fields ): array {
     150        $notices = \WC()->session->get( 'wc_notices', array() );
     151
     152        $notices['error'] = \array_filter(
     153            $notices['error'] ?? array(),
     154            static fn( $e ) => ! \in_array( $e['data']['id'] ?? '', $fields, true )
     155        );
     156
     157        return $notices;
     158    }
    122159}
  • serbian-addons-for-woocommerce/trunk/lib/Order/Field_Display.php

    r3018381 r3092380  
    99namespace Oblak\WooCommerce\Serbian_Addons\Order;
    1010
    11 use Oblak\WP\Abstracts\Hook_Runner;
     11use Oblak\WP\Decorators\Filter;
    1212use Oblak\WP\Decorators\Hookable;
    1313use WC_Customer;
     
    1818 */
    1919#[Hookable( 'woocommerce_init', 99 )]
    20 class Field_Display extends Hook_Runner {
     20class Field_Display {
     21    /**
     22     *  Constructor
     23     */
     24    public function __construct() {
     25        if ( \class_exists( '\XWP\Hook\Invoker' ) ) {
     26            return;
     27        }
     28
     29        \xwp_invoke_hooked_methods( $this );
     30    }
    2131    /**
    2232     * Modifies the address format for Serbia to include necessary company information
     
    2434     * @param  string[] $formats Address formats.
    2535     * @return string[]          Modified address formats
    26      *
    27      * @hook     woocommerce_localisation_address_formats
    28      * @type     filter
    29      * @priority filter:woocommerce_serbian_localization_address_priority:100
    30      */
     36     */
     37    #[Filter( 'woocommerce_localisation_address_formats', 'wcrs_localization_address_priority' )]
    3138    public function modify_address_format( $formats ) {
    32         add_filter( 'woocommerce_formatted_address_force_country_display', '__return_true' );
     39        \add_filter( 'woocommerce_formatted_address_force_country_display', '__return_true' );
    3340
    3441        $formats['RS'] = "{name}\n{company}\n{mb}\n{pib}\n{address_1}\n{address_2}\n{postcode} {city}, {state} {country}";
    3542
    36         if ( WCSRB()->get_settings( 'general', 'remove_unneeded_fields' ) ) {
    37             $formats['RS'] = strtr(
    38                 $formats['RS'],
    39                 array(
    40                     '{state}'     => '',
    41                     '{address_2}' => '',
    42                 )
    43             );
     43        if ( \WCSRB()->get_settings( 'general', 'remove_unneeded_fields' ) ) {
     44            $formats['RS'] = \str_replace( array( '{state}', '{address_2}' ), '', $formats['RS'] );
    4445        }
    4546
     
    5859     * @param  array    $args          Address data.
    5960     * @return string[]                Modified replacements array
    60      *
    61      * @hook     woocommerce_formatted_address_replacements
    62      * @type     filter
    63      * @priority 99
    64      */
     61     */
     62    #[Filter( 'woocommerce_formatted_address_replacements', 99 )]
    6563    public function modify_address_replacements( $replacements, $args ) {
    6664        $replacements['{type}'] = $args['type'] ?? "\n";
     
    8078     * @param  string $address_type Address type (billing or shipping).
    8179     * @return array                Modified address data array
    82      *
    83      * @hook     woocommerce_my_account_my_address_formatted_address
    84      * @type     filter
    85      * @priority 99
    86      */
     80     */
     81    #[Filter( 'woocommerce_my_account_my_address_formatted_address', 99 )]
    8782    public function modify_account_formatted_address( $address, $customer_id, $address_type ) {
    8883        if ( 'billing' !== $address_type ) {
     
    9287        $customer = new WC_Customer( $customer_id );
    9388
    94         $user_type   = ! empty( $customer->get_meta( 'billing_type', true ) ) ? $customer->get_meta( 'billing_type', true ) : 'person';
     89        // phpcs:ignore Universal.Operators.DisallowShortTernary.Found
     90        $user_type   = $customer->get_meta( 'billing_type', true ) ?: 'person';
    9591        $company_num = $customer->get_meta( 'billing_mb', true );
    9692        $company_tax = $customer->get_meta( 'billing_pib', true );
     
    107103     * @param  WC_Order $order   Order object.
    108104     * @return array             Modified address data array
    109      *
    110      * @hook     woocommerce_order_formatted_billing_address
    111      * @type     filter
    112      * @priority 99
    113      */
     105     */
     106    #[Filter( 'woocommerce_order_formatted_billing_address', 99 )]
    114107    public function modify_order_formatted_address( $address, $order ) {
    115108        return $this->address_modifier(
     
    117110            $order->get_meta( '_billing_type', true ),
    118111            $order->get_meta( '_billing_mb', true ),
    119             $order->get_meta( '_billing_pib', true )
     112            $order->get_meta( '_billing_pib', true ),
    120113        );
    121114    }
     
    145138        $address['last_name']  = "\n";
    146139
    147         $address['mb']  = sprintf(
     140        if ( $company_number ) {
     141            $address['mb'] = \sprintf(
     142                '%s: %s',
     143                \_x( 'Company Number', 'Address display', 'serbian-addons-for-woocommerce' ),
     144                $company_number,
     145            );
     146        }
     147        $address['pib'] = \sprintf(
    148148            '%s: %s',
    149             _x( 'Company Number', 'Address display', 'serbian-addons-for-woocommerce' ),
    150             $company_number
    151         );
    152         $address['pib'] = sprintf(
    153             '%s: %s',
    154             _x( 'Tax Identification Number', 'Address display', 'serbian-addons-for-woocommerce' ),
    155             $tax_number
     149            \_x( 'Tax Identification Number', 'Address display', 'serbian-addons-for-woocommerce' ),
     150            $tax_number,
    156151        );
    157152
     
    165160     * @param  WC_Order $order Order object.
    166161     * @return string          Modified Buyer name
    167      *
    168      * @hook     woocommerce_admin_order_buyer_name
    169      * @type     filter
    170      * @priority 99
    171      */
     162     */
     163    #[Filter( 'woocommerce_admin_order_buyer_name', 99 )]
    172164    public function modify_order_buyer_name( $buyer, $order ) {
    173         return ( 'RS' === $order->get_billing_country() && 'company' === $order->get_meta( '_billing_type', true ) )
     165        return 'RS' === $order->get_billing_country() && 'company' === $order->get_meta( '_billing_type', true )
    174166            ? $order->get_billing_company()
    175167            : $buyer;
    176168    }
     169
     170    /**
     171     * Adds the company information to the customer meta fields.
     172     *
     173     * @param  array $fields Customer meta fields.
     174     * @return array         Modified customer meta fields
     175     */
     176    #[Filter( 'woocommerce_customer_meta_fields' )]
     177    public function modify_customer_meta_fields( array $fields ): array {
     178        $billing = array();
     179
     180        foreach ( $fields['billing']['fields'] as $field => $args ) {
     181            if ( 'billing_company' !== $field ) {
     182                $billing[ $field ] = $args;
     183                continue;
     184            }
     185
     186            $billing['billing_type'] = array(
     187                'label'   => \__( 'Customer type', 'serbian-addons-for-woocommerce' ),
     188                'options' => \Oblak\WooCommerce\Serbian_Addons\Utils\get_entity_types(),
     189                'type'    => 'select',
     190            );
     191
     192            $billing[ $field ] = $args;
     193
     194            $billing['billing_mb'] = array(
     195                'label' => \__( 'Company Number', 'serbian-addons-for-woocommerce' ),
     196                'type'  => 'text',
     197            );
     198
     199            $billing['billing_pib'] = array(
     200                'label' => \__( 'Tax Number', 'serbian-addons-for-woocommerce' ),
     201                'type'  => 'text',
     202            );
     203        }
     204
     205        $fields['billing']['fields'] = $billing;
     206
     207        return $fields;
     208    }
    177209}
  • serbian-addons-for-woocommerce/trunk/readme.txt

    r3080934 r3092380  
    88WC requires at least: 8.0
    99WC tested up to: 8.3
    10 Stable tag: 3.5.10
     10Stable tag: 3.6.0
    1111License: GPLv2 or later
    1212License URI: http://www.gnu.org/licenses/gpl-2.0.html
  • serbian-addons-for-woocommerce/trunk/serbian-addons-for-woocommerce.php

    r3080934 r3092380  
    44 * Plugin URI:           https://oblak.studio/open-source/srpski-woocommerce
    55 * Description:          Various addons and tweaks that make WooCommerce compatible with Serbian bureaucracy.
    6  * Version:              3.5.10
     6 * Version:              3.6.0
    77 * Requires PHP:         8.0
    88 * Author:               Oblak Studio
     
    2525defined( 'WCRS_VERSION' ) || define( 'WCRS_VERSION', '3.4.0' );
    2626
    27 add_action(
    28     'woocommerce_loaded',
    29     static function () {
    30         require __DIR__ . '/vendor/autoload_packages.php';
    31         \WCSRB();
    32     },
    33     20,
    34 );
     27require __DIR__ . '/vendor/autoload_packages.php';
     28
     29add_action( 'woocommerce_loaded', 'WCSRB', 0 );
Note: See TracChangeset for help on using the changeset viewer.