Changeset 3092380
- Timestamp:
- 05/25/2024 04:45:12 AM (22 months ago)
- Location:
- serbian-addons-for-woocommerce
- Files:
-
- 8 edited
- 1 copied
-
tags/3.6.0 (copied) (copied from serbian-addons-for-woocommerce/trunk)
-
tags/3.6.0/lib/Checkout/Field_Validator.php (modified) (3 diffs)
-
tags/3.6.0/lib/Order/Field_Display.php (modified) (10 diffs)
-
tags/3.6.0/readme.txt (modified) (1 diff)
-
tags/3.6.0/serbian-addons-for-woocommerce.php (modified) (2 diffs)
-
trunk/lib/Checkout/Field_Validator.php (modified) (3 diffs)
-
trunk/lib/Order/Field_Display.php (modified) (10 diffs)
-
trunk/readme.txt (modified) (1 diff)
-
trunk/serbian-addons-for-woocommerce.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
serbian-addons-for-woocommerce/tags/3.6.0/lib/Checkout/Field_Validator.php
r3018381 r3092380 9 9 namespace Oblak\WooCommerce\Serbian_Addons\Checkout; 10 10 11 use Oblak\WP\Decorators\Action; 11 12 use Oblak\WP\Decorators\Hookable; 12 13 use function Oblak\validateMB;14 use function Oblak\validatePIB;15 13 16 14 /** … … 19 17 #[Hookable( 'woocommerce_init', 99 )] 20 18 class Field_Validator { 21 22 19 /** 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 35 21 */ 36 22 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 ); 39 28 } 40 41 29 /** 42 30 * Adds custom validation to billing address field saving 43 31 * 44 * @param int $user_id Current User ID.45 * @param string $ load_addressAddress 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. 46 34 */ 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 ) ) { 49 42 return; 50 43 } 51 44 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 ) ); 55 47 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; 60 51 } 61 }62 52 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( 74 54 'data' => array( 75 'id' => 'billing_mb',55 'id' => $field, 76 56 ), 57 'notice' => $args['message'], 77 58 ); 78 59 } 60 \WC()->session->set( 'wc_notices', $notices ); 61 } 79 62 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 }93 63 94 64 /** … … 98 68 * @param \WP_Error $error Error object. 99 69 */ 70 #[Action( 'woocommerce_after_checkout_validation', 0 )] 100 71 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' ); 105 76 } 106 77 107 if ( 'company' !== $data['billing_type'] || 'RS' !== $data['billing_country']) {78 if ( ! $this->can_validate( $data ) ) { 108 79 return; 109 80 } 110 81 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 } 117 86 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 ) ); 120 88 } 121 89 } 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 } 122 159 } -
serbian-addons-for-woocommerce/tags/3.6.0/lib/Order/Field_Display.php
r3018381 r3092380 9 9 namespace Oblak\WooCommerce\Serbian_Addons\Order; 10 10 11 use Oblak\WP\ Abstracts\Hook_Runner;11 use Oblak\WP\Decorators\Filter; 12 12 use Oblak\WP\Decorators\Hookable; 13 13 use WC_Customer; … … 18 18 */ 19 19 #[Hookable( 'woocommerce_init', 99 )] 20 class Field_Display extends Hook_Runner { 20 class 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 } 21 31 /** 22 32 * Modifies the address format for Serbia to include necessary company information … … 24 34 * @param string[] $formats Address formats. 25 35 * @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' )] 31 38 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' ); 33 40 34 41 $formats['RS'] = "{name}\n{company}\n{mb}\n{pib}\n{address_1}\n{address_2}\n{postcode} {city}, {state} {country}"; 35 42 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'] ); 44 45 } 45 46 … … 58 59 * @param array $args Address data. 59 60 * @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 )] 65 63 public function modify_address_replacements( $replacements, $args ) { 66 64 $replacements['{type}'] = $args['type'] ?? "\n"; … … 80 78 * @param string $address_type Address type (billing or shipping). 81 79 * @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 )] 87 82 public function modify_account_formatted_address( $address, $customer_id, $address_type ) { 88 83 if ( 'billing' !== $address_type ) { … … 92 87 $customer = new WC_Customer( $customer_id ); 93 88 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'; 95 91 $company_num = $customer->get_meta( 'billing_mb', true ); 96 92 $company_tax = $customer->get_meta( 'billing_pib', true ); … … 107 103 * @param WC_Order $order Order object. 108 104 * @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 )] 114 107 public function modify_order_formatted_address( $address, $order ) { 115 108 return $this->address_modifier( … … 117 110 $order->get_meta( '_billing_type', true ), 118 111 $order->get_meta( '_billing_mb', true ), 119 $order->get_meta( '_billing_pib', true ) 112 $order->get_meta( '_billing_pib', true ), 120 113 ); 121 114 } … … 145 138 $address['last_name'] = "\n"; 146 139 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( 148 148 '%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, 156 151 ); 157 152 … … 165 160 * @param WC_Order $order Order object. 166 161 * @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 )] 172 164 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 ) 174 166 ? $order->get_billing_company() 175 167 : $buyer; 176 168 } 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 } 177 209 } -
serbian-addons-for-woocommerce/tags/3.6.0/readme.txt
r3080934 r3092380 8 8 WC requires at least: 8.0 9 9 WC tested up to: 8.3 10 Stable tag: 3. 5.1010 Stable tag: 3.6.0 11 11 License: GPLv2 or later 12 12 License 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 4 4 * Plugin URI: https://oblak.studio/open-source/srpski-woocommerce 5 5 * Description: Various addons and tweaks that make WooCommerce compatible with Serbian bureaucracy. 6 * Version: 3. 5.106 * Version: 3.6.0 7 7 * Requires PHP: 8.0 8 8 * Author: Oblak Studio … … 25 25 defined( 'WCRS_VERSION' ) || define( 'WCRS_VERSION', '3.4.0' ); 26 26 27 add_action( 28 'woocommerce_loaded', 29 static function () { 30 require __DIR__ . '/vendor/autoload_packages.php'; 31 \WCSRB(); 32 }, 33 20, 34 ); 27 require __DIR__ . '/vendor/autoload_packages.php'; 28 29 add_action( 'woocommerce_loaded', 'WCSRB', 0 ); -
serbian-addons-for-woocommerce/trunk/lib/Checkout/Field_Validator.php
r3018381 r3092380 9 9 namespace Oblak\WooCommerce\Serbian_Addons\Checkout; 10 10 11 use Oblak\WP\Decorators\Action; 11 12 use Oblak\WP\Decorators\Hookable; 12 13 use function Oblak\validateMB;14 use function Oblak\validatePIB;15 13 16 14 /** … … 19 17 #[Hookable( 'woocommerce_init', 99 )] 20 18 class Field_Validator { 21 22 19 /** 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 35 21 */ 36 22 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 ); 39 28 } 40 41 29 /** 42 30 * Adds custom validation to billing address field saving 43 31 * 44 * @param int $user_id Current User ID.45 * @param string $ load_addressAddress 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. 46 34 */ 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 ) ) { 49 42 return; 50 43 } 51 44 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 ) ); 55 47 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; 60 51 } 61 }62 52 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( 74 54 'data' => array( 75 'id' => 'billing_mb',55 'id' => $field, 76 56 ), 57 'notice' => $args['message'], 77 58 ); 78 59 } 60 \WC()->session->set( 'wc_notices', $notices ); 61 } 79 62 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 }93 63 94 64 /** … … 98 68 * @param \WP_Error $error Error object. 99 69 */ 70 #[Action( 'woocommerce_after_checkout_validation', 0 )] 100 71 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' ); 105 76 } 106 77 107 if ( 'company' !== $data['billing_type'] || 'RS' !== $data['billing_country']) {78 if ( ! $this->can_validate( $data ) ) { 108 79 return; 109 80 } 110 81 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 } 117 86 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 ) ); 120 88 } 121 89 } 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 } 122 159 } -
serbian-addons-for-woocommerce/trunk/lib/Order/Field_Display.php
r3018381 r3092380 9 9 namespace Oblak\WooCommerce\Serbian_Addons\Order; 10 10 11 use Oblak\WP\ Abstracts\Hook_Runner;11 use Oblak\WP\Decorators\Filter; 12 12 use Oblak\WP\Decorators\Hookable; 13 13 use WC_Customer; … … 18 18 */ 19 19 #[Hookable( 'woocommerce_init', 99 )] 20 class Field_Display extends Hook_Runner { 20 class 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 } 21 31 /** 22 32 * Modifies the address format for Serbia to include necessary company information … … 24 34 * @param string[] $formats Address formats. 25 35 * @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' )] 31 38 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' ); 33 40 34 41 $formats['RS'] = "{name}\n{company}\n{mb}\n{pib}\n{address_1}\n{address_2}\n{postcode} {city}, {state} {country}"; 35 42 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'] ); 44 45 } 45 46 … … 58 59 * @param array $args Address data. 59 60 * @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 )] 65 63 public function modify_address_replacements( $replacements, $args ) { 66 64 $replacements['{type}'] = $args['type'] ?? "\n"; … … 80 78 * @param string $address_type Address type (billing or shipping). 81 79 * @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 )] 87 82 public function modify_account_formatted_address( $address, $customer_id, $address_type ) { 88 83 if ( 'billing' !== $address_type ) { … … 92 87 $customer = new WC_Customer( $customer_id ); 93 88 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'; 95 91 $company_num = $customer->get_meta( 'billing_mb', true ); 96 92 $company_tax = $customer->get_meta( 'billing_pib', true ); … … 107 103 * @param WC_Order $order Order object. 108 104 * @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 )] 114 107 public function modify_order_formatted_address( $address, $order ) { 115 108 return $this->address_modifier( … … 117 110 $order->get_meta( '_billing_type', true ), 118 111 $order->get_meta( '_billing_mb', true ), 119 $order->get_meta( '_billing_pib', true ) 112 $order->get_meta( '_billing_pib', true ), 120 113 ); 121 114 } … … 145 138 $address['last_name'] = "\n"; 146 139 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( 148 148 '%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, 156 151 ); 157 152 … … 165 160 * @param WC_Order $order Order object. 166 161 * @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 )] 172 164 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 ) 174 166 ? $order->get_billing_company() 175 167 : $buyer; 176 168 } 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 } 177 209 } -
serbian-addons-for-woocommerce/trunk/readme.txt
r3080934 r3092380 8 8 WC requires at least: 8.0 9 9 WC tested up to: 8.3 10 Stable tag: 3. 5.1010 Stable tag: 3.6.0 11 11 License: GPLv2 or later 12 12 License URI: http://www.gnu.org/licenses/gpl-2.0.html -
serbian-addons-for-woocommerce/trunk/serbian-addons-for-woocommerce.php
r3080934 r3092380 4 4 * Plugin URI: https://oblak.studio/open-source/srpski-woocommerce 5 5 * Description: Various addons and tweaks that make WooCommerce compatible with Serbian bureaucracy. 6 * Version: 3. 5.106 * Version: 3.6.0 7 7 * Requires PHP: 8.0 8 8 * Author: Oblak Studio … … 25 25 defined( 'WCRS_VERSION' ) || define( 'WCRS_VERSION', '3.4.0' ); 26 26 27 add_action( 28 'woocommerce_loaded', 29 static function () { 30 require __DIR__ . '/vendor/autoload_packages.php'; 31 \WCSRB(); 32 }, 33 20, 34 ); 27 require __DIR__ . '/vendor/autoload_packages.php'; 28 29 add_action( 'woocommerce_loaded', 'WCSRB', 0 );
Note: See TracChangeset
for help on using the changeset viewer.