Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Blocks/Domain/Services
File: CheckoutFieldsFrontend.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Automattic\WooCommerce\Blocks\Domain\Services;
[2] Fix | Delete
[3] Fix | Delete
use Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFieldsSchema\DocumentObject;
[4] Fix | Delete
use WC_Customer;
[5] Fix | Delete
use WC_Order;
[6] Fix | Delete
[7] Fix | Delete
/**
[8] Fix | Delete
* Service class managing checkout fields and its related extensibility points on the frontend.
[9] Fix | Delete
*/
[10] Fix | Delete
class CheckoutFieldsFrontend {
[11] Fix | Delete
[12] Fix | Delete
/**
[13] Fix | Delete
* Checkout field controller.
[14] Fix | Delete
*
[15] Fix | Delete
* @var CheckoutFields
[16] Fix | Delete
*/
[17] Fix | Delete
private $checkout_fields_controller;
[18] Fix | Delete
[19] Fix | Delete
/**
[20] Fix | Delete
* Sets up core fields.
[21] Fix | Delete
*
[22] Fix | Delete
* @param CheckoutFields $checkout_fields_controller Instance of the checkout field controller.
[23] Fix | Delete
*/
[24] Fix | Delete
public function __construct( CheckoutFields $checkout_fields_controller ) {
[25] Fix | Delete
$this->checkout_fields_controller = $checkout_fields_controller;
[26] Fix | Delete
}
[27] Fix | Delete
[28] Fix | Delete
/**
[29] Fix | Delete
* Initialize hooks. This is not run Store API requests.
[30] Fix | Delete
*/
[31] Fix | Delete
public function init() {
[32] Fix | Delete
// Show custom checkout fields on the order details page.
[33] Fix | Delete
add_action( 'woocommerce_order_details_after_customer_address', array( $this, 'render_order_address_fields' ), 10, 2 );
[34] Fix | Delete
add_action( 'woocommerce_order_details_after_customer_details', array( $this, 'render_order_other_fields' ), 10 );
[35] Fix | Delete
[36] Fix | Delete
// Show custom checkout fields on the My Account page.
[37] Fix | Delete
add_action( 'woocommerce_my_account_after_my_address', array( $this, 'render_address_fields' ), 10, 1 );
[38] Fix | Delete
[39] Fix | Delete
// Edit account form under my account (for contact details).
[40] Fix | Delete
add_filter( 'woocommerce_edit_account_form_fields', array( $this, 'edit_account_form_fields' ), 10, 1 );
[41] Fix | Delete
add_action( 'woocommerce_save_account_details', array( $this, 'save_account_form_fields' ), 10, 1 );
[42] Fix | Delete
[43] Fix | Delete
// Edit address form under my account.
[44] Fix | Delete
add_filter( 'woocommerce_address_to_edit', array( $this, 'edit_address_fields' ), 10, 2 );
[45] Fix | Delete
add_action( 'woocommerce_customer_save_address', array( $this, 'save_address_fields' ), 10, 4 );
[46] Fix | Delete
}
[47] Fix | Delete
[48] Fix | Delete
/**
[49] Fix | Delete
* Render custom fields.
[50] Fix | Delete
*
[51] Fix | Delete
* @param array $fields List of additional fields with values.
[52] Fix | Delete
* @return string
[53] Fix | Delete
*/
[54] Fix | Delete
protected function render_additional_fields( $fields ) {
[55] Fix | Delete
return ! empty( $fields ) ? '<dl class="wc-block-components-additional-fields-list">' . implode( '', array_map( array( $this, 'render_additional_field' ), $fields ) ) . '</dl>' : '';
[56] Fix | Delete
}
[57] Fix | Delete
[58] Fix | Delete
/**
[59] Fix | Delete
* Render custom field.
[60] Fix | Delete
*
[61] Fix | Delete
* @param array $field An additional field and value.
[62] Fix | Delete
* @return string
[63] Fix | Delete
*/
[64] Fix | Delete
protected function render_additional_field( $field ) {
[65] Fix | Delete
return sprintf(
[66] Fix | Delete
'<dt>%1$s</dt><dd>%2$s</dd>',
[67] Fix | Delete
esc_html( $field['label'] ),
[68] Fix | Delete
esc_html( $field['value'] )
[69] Fix | Delete
);
[70] Fix | Delete
}
[71] Fix | Delete
[72] Fix | Delete
/**
[73] Fix | Delete
* Renders address fields on the order details page.
[74] Fix | Delete
*
[75] Fix | Delete
* @param string $address_type Type of address (billing or shipping).
[76] Fix | Delete
* @param WC_Order $order Order object.
[77] Fix | Delete
*/
[78] Fix | Delete
public function render_order_address_fields( $address_type, $order ) {
[79] Fix | Delete
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
[80] Fix | Delete
echo $this->render_additional_fields( $this->checkout_fields_controller->get_order_additional_fields_with_values( $order, 'address', $address_type, 'view' ) );
[81] Fix | Delete
}
[82] Fix | Delete
[83] Fix | Delete
/**
[84] Fix | Delete
* Renders additional fields on the order details page.
[85] Fix | Delete
*
[86] Fix | Delete
* @param WC_Order $order Order object.
[87] Fix | Delete
*/
[88] Fix | Delete
public function render_order_other_fields( $order ) {
[89] Fix | Delete
$fields = array_merge(
[90] Fix | Delete
$this->checkout_fields_controller->get_order_additional_fields_with_values( $order, 'contact', 'other', 'view' ),
[91] Fix | Delete
$this->checkout_fields_controller->get_order_additional_fields_with_values( $order, 'order', 'other', 'view' ),
[92] Fix | Delete
);
[93] Fix | Delete
[94] Fix | Delete
$context = array(
[95] Fix | Delete
'caller' => 'CheckoutFieldsFrontend::render_order_other_fields',
[96] Fix | Delete
'order' => $order,
[97] Fix | Delete
);
[98] Fix | Delete
[99] Fix | Delete
$fields = $this->checkout_fields_controller->filter_fields_for_order_confirmation( $fields, $context );
[100] Fix | Delete
[101] Fix | Delete
if ( ! $fields ) {
[102] Fix | Delete
return;
[103] Fix | Delete
}
[104] Fix | Delete
[105] Fix | Delete
echo '<section class="wc-block-order-confirmation-additional-fields-wrapper">';
[106] Fix | Delete
echo '<h2>' . esc_html__( 'Additional information', 'woocommerce' ) . '</h2>';
[107] Fix | Delete
echo $this->render_additional_fields( $fields ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
[108] Fix | Delete
echo '</section>';
[109] Fix | Delete
}
[110] Fix | Delete
[111] Fix | Delete
/**
[112] Fix | Delete
* Renders address fields on the account page.
[113] Fix | Delete
*
[114] Fix | Delete
* @param string $address_type Type of address (billing or shipping).
[115] Fix | Delete
*/
[116] Fix | Delete
public function render_address_fields( $address_type ) {
[117] Fix | Delete
if ( ! in_array( $address_type, array( 'billing', 'shipping' ), true ) ) {
[118] Fix | Delete
return;
[119] Fix | Delete
}
[120] Fix | Delete
[121] Fix | Delete
$customer = new WC_Customer( get_current_user_id() );
[122] Fix | Delete
[123] Fix | Delete
$document_object = new DocumentObject();
[124] Fix | Delete
$document_object->set_customer( $customer );
[125] Fix | Delete
$document_object->set_context( $address_type . '_address' );
[126] Fix | Delete
$fields = $this->checkout_fields_controller->get_contextual_fields_for_location( 'address', $document_object );
[127] Fix | Delete
[128] Fix | Delete
if ( ! $fields || ! $customer ) {
[129] Fix | Delete
return;
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
foreach ( $fields as $key => $field ) {
[133] Fix | Delete
$value = $this->checkout_fields_controller->format_additional_field_value(
[134] Fix | Delete
$this->checkout_fields_controller->get_field_from_object( $key, $customer, $address_type ),
[135] Fix | Delete
$field
[136] Fix | Delete
);
[137] Fix | Delete
[138] Fix | Delete
if ( ! $value ) {
[139] Fix | Delete
continue;
[140] Fix | Delete
}
[141] Fix | Delete
[142] Fix | Delete
printf( '<br><strong>%s</strong>: %s', wp_kses_post( $field['label'] ), wp_kses_post( $value ) );
[143] Fix | Delete
}
[144] Fix | Delete
}
[145] Fix | Delete
[146] Fix | Delete
/**
[147] Fix | Delete
* Adds additional contact fields to the My Account edit account form.
[148] Fix | Delete
*/
[149] Fix | Delete
public function edit_account_form_fields() {
[150] Fix | Delete
$customer = new WC_Customer( get_current_user_id() );
[151] Fix | Delete
[152] Fix | Delete
$document_object = new DocumentObject();
[153] Fix | Delete
$document_object->set_customer( $customer );
[154] Fix | Delete
$document_object->set_context( 'contact' );
[155] Fix | Delete
$fields = $this->checkout_fields_controller->get_contextual_fields_for_location( 'contact', $document_object );
[156] Fix | Delete
[157] Fix | Delete
foreach ( $fields as $key => $field ) {
[158] Fix | Delete
$field_key = CheckoutFields::get_group_key( 'other' ) . $key;
[159] Fix | Delete
$form_field = $field;
[160] Fix | Delete
$form_field['id'] = $field_key;
[161] Fix | Delete
$form_field['value'] = $this->checkout_fields_controller->get_field_from_object( $key, $customer, 'contact' );
[162] Fix | Delete
[163] Fix | Delete
if ( 'select' === $field['type'] ) {
[164] Fix | Delete
$form_field['options'] = array_column( $field['options'], 'label', 'value' );
[165] Fix | Delete
}
[166] Fix | Delete
[167] Fix | Delete
if ( 'checkbox' === $field['type'] ) {
[168] Fix | Delete
$form_field['checked_value'] = '1';
[169] Fix | Delete
$form_field['unchecked_value'] = '0';
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
woocommerce_form_field( $field_key, $form_field, wc_get_post_data_by_key( $key, $form_field['value'] ) );
[173] Fix | Delete
}
[174] Fix | Delete
}
[175] Fix | Delete
[176] Fix | Delete
/**
[177] Fix | Delete
* Adds additional address fields to the My Account edit address form.
[178] Fix | Delete
*
[179] Fix | Delete
* @param array $address Address fields.
[180] Fix | Delete
* @param string $address_type Type of address (billing or shipping).
[181] Fix | Delete
* @return array Updated address fields.
[182] Fix | Delete
*/
[183] Fix | Delete
public function edit_address_fields( $address, $address_type ) {
[184] Fix | Delete
$customer = new WC_Customer( get_current_user_id() );
[185] Fix | Delete
[186] Fix | Delete
$document_object = new DocumentObject();
[187] Fix | Delete
$document_object->set_customer( $customer );
[188] Fix | Delete
$document_object->set_context( $address_type . '_address' );
[189] Fix | Delete
$fields = $this->checkout_fields_controller->get_contextual_fields_for_location( 'address', $document_object );
[190] Fix | Delete
[191] Fix | Delete
foreach ( $fields as $key => $field ) {
[192] Fix | Delete
$field_key = CheckoutFields::get_group_key( $address_type ) . $key;
[193] Fix | Delete
$address[ $field_key ] = $field;
[194] Fix | Delete
$address[ $field_key ]['value'] = $this->checkout_fields_controller->get_field_from_object( $key, $customer, $address_type );
[195] Fix | Delete
[196] Fix | Delete
if ( 'select' === $field['type'] ) {
[197] Fix | Delete
$address[ $field_key ]['options'] = array_column( $field['options'], 'label', 'value' );
[198] Fix | Delete
[199] Fix | Delete
// If a placeholder is set, add a placeholder option if it doesn't exist already.
[200] Fix | Delete
if (
[201] Fix | Delete
! empty( $address[ $field_key ]['placeholder'] )
[202] Fix | Delete
&& ! array_key_exists( '', $address[ $field_key ]['options'] )
[203] Fix | Delete
) {
[204] Fix | Delete
$address[ $field_key ]['options'] = array( '' => $address[ $field_key ]['placeholder'] ) + $address[ $field_key ]['options'];
[205] Fix | Delete
}
[206] Fix | Delete
}
[207] Fix | Delete
[208] Fix | Delete
if ( 'checkbox' === $field['type'] ) {
[209] Fix | Delete
$address[ $field_key ]['checked_value'] = '1';
[210] Fix | Delete
$address[ $field_key ]['unchecked_value'] = '0';
[211] Fix | Delete
}
[212] Fix | Delete
}
[213] Fix | Delete
[214] Fix | Delete
return $address;
[215] Fix | Delete
}
[216] Fix | Delete
[217] Fix | Delete
/**
[218] Fix | Delete
* Validates and saves additional address fields to the customer object on the My Account page.
[219] Fix | Delete
*
[220] Fix | Delete
* Customer is not provided by this hook so we handle save here.
[221] Fix | Delete
*
[222] Fix | Delete
* @param integer $user_id User ID.
[223] Fix | Delete
*/
[224] Fix | Delete
public function save_account_form_fields( $user_id ) {
[225] Fix | Delete
try {
[226] Fix | Delete
$customer = new WC_Customer( $user_id );
[227] Fix | Delete
$result = $this->update_additional_fields_for_customer( $customer, 'contact', 'other' );
[228] Fix | Delete
[229] Fix | Delete
if ( is_wp_error( $result ) ) {
[230] Fix | Delete
foreach ( $result->get_error_messages() as $error_message ) {
[231] Fix | Delete
wc_add_notice( $error_message, 'error' );
[232] Fix | Delete
}
[233] Fix | Delete
}
[234] Fix | Delete
[235] Fix | Delete
$customer->save();
[236] Fix | Delete
} catch ( \Exception $e ) {
[237] Fix | Delete
wc_add_notice(
[238] Fix | Delete
sprintf(
[239] Fix | Delete
/* translators: %s: Error message. */
[240] Fix | Delete
__( 'An error occurred while saving account details: %s', 'woocommerce' ),
[241] Fix | Delete
esc_html( $e->getMessage() )
[242] Fix | Delete
),
[243] Fix | Delete
'error'
[244] Fix | Delete
);
[245] Fix | Delete
}
[246] Fix | Delete
}
[247] Fix | Delete
[248] Fix | Delete
/**
[249] Fix | Delete
* For the My Account page, save address fields. This uses the Store API endpoint for saving addresses so
[250] Fix | Delete
* extensibility hooks are consistent across the codebase.
[251] Fix | Delete
*
[252] Fix | Delete
* The caller saves the customer object if there are no errors. Nonces are checked before this method executes.
[253] Fix | Delete
*
[254] Fix | Delete
* @param integer $user_id User ID.
[255] Fix | Delete
* @param string $address_type Type of address (billing or shipping).
[256] Fix | Delete
* @param array $address Address fields.
[257] Fix | Delete
* @param WC_Customer $customer Customer object.
[258] Fix | Delete
*/
[259] Fix | Delete
public function save_address_fields( $user_id, $address_type, $address = [], $customer = null ) {
[260] Fix | Delete
try {
[261] Fix | Delete
$customer = $customer ?? new WC_Customer( $user_id );
[262] Fix | Delete
$result = $this->update_additional_fields_for_customer( $customer, 'address', $address_type );
[263] Fix | Delete
[264] Fix | Delete
if ( is_wp_error( $result ) ) {
[265] Fix | Delete
foreach ( $result->get_error_messages() as $error_message ) {
[266] Fix | Delete
wc_add_notice( $error_message, 'error' );
[267] Fix | Delete
}
[268] Fix | Delete
}
[269] Fix | Delete
[270] Fix | Delete
$customer->save();
[271] Fix | Delete
} catch ( \Exception $e ) {
[272] Fix | Delete
wc_add_notice(
[273] Fix | Delete
sprintf(
[274] Fix | Delete
/* translators: %s: Error message. */
[275] Fix | Delete
__( 'An error occurred while saving address details: %s', 'woocommerce' ),
[276] Fix | Delete
esc_html( $e->getMessage() )
[277] Fix | Delete
),
[278] Fix | Delete
'error'
[279] Fix | Delete
);
[280] Fix | Delete
}
[281] Fix | Delete
}
[282] Fix | Delete
[283] Fix | Delete
/**
[284] Fix | Delete
* Get posted additional field values.
[285] Fix | Delete
*
[286] Fix | Delete
* @param string $location The location to get fields for.
[287] Fix | Delete
* @param string $group The group to get fields for.
[288] Fix | Delete
* @param boolean $sanitize Whether to sanitize the field values.
[289] Fix | Delete
* @return array The posted field values and sanitized field values.
[290] Fix | Delete
*/
[291] Fix | Delete
protected function get_posted_additional_field_values( $location, $group, $sanitize = true ) {
[292] Fix | Delete
$additional_fields = $this->checkout_fields_controller->get_fields_for_location( $location );
[293] Fix | Delete
$field_values = [];
[294] Fix | Delete
[295] Fix | Delete
// phpcs:disable WordPress.Security.NonceVerification.Missing
[296] Fix | Delete
foreach ( $additional_fields as $field_key => $field_data ) {
[297] Fix | Delete
$post_key = CheckoutFields::get_group_key( $group ) . $field_key;
[298] Fix | Delete
$field_values[ $field_key ] = wc_clean( wp_unslash( $_POST[ $post_key ] ?? '' ) );
[299] Fix | Delete
[300] Fix | Delete
if ( $sanitize ) {
[301] Fix | Delete
$field_values[ $field_key ] = $this->checkout_fields_controller->sanitize_field( $field_key, $field_values[ $field_key ] );
[302] Fix | Delete
}
[303] Fix | Delete
}
[304] Fix | Delete
// phpcs:enable WordPress.Security.NonceVerification.Missing
[305] Fix | Delete
return $field_values;
[306] Fix | Delete
}
[307] Fix | Delete
[308] Fix | Delete
/**
[309] Fix | Delete
* Validate and save additional fields for a given customer.
[310] Fix | Delete
*
[311] Fix | Delete
* @param WC_Customer $customer Customer object.
[312] Fix | Delete
* @param string $location Location to save fields for.
[313] Fix | Delete
* @param string $group Group to save fields for.
[314] Fix | Delete
* @return true|\WP_Error True if successful, \WP_Error if there are errors.
[315] Fix | Delete
*/
[316] Fix | Delete
protected function update_additional_fields_for_customer( $customer, $location, $group ) {
[317] Fix | Delete
// Get all values from the POST request before validating.
[318] Fix | Delete
$field_values = $this->get_posted_additional_field_values( $location, $group, false ); // These values are used to see if required fields have values.
[319] Fix | Delete
$sanitized_field_values = $this->get_posted_additional_field_values( $location, $group ); // These values are used to validate custom rules, generate the document object, and save fields to the account.
[320] Fix | Delete
[321] Fix | Delete
$document_object = new DocumentObject(
[322] Fix | Delete
[
[323] Fix | Delete
'customer' => [
[324] Fix | Delete
( 'address' === $location ? $group . '_address' : 'additional_fields' ) => $sanitized_field_values,
[325] Fix | Delete
],
[326] Fix | Delete
]
[327] Fix | Delete
);
[328] Fix | Delete
$document_object->set_customer( $customer );
[329] Fix | Delete
$document_object->set_context( 'address' === $location ? $group . '_address' : $location );
[330] Fix | Delete
$fields = $this->checkout_fields_controller->get_contextual_fields_for_location( $location, $document_object );
[331] Fix | Delete
[332] Fix | Delete
// Holds values to be persisted to the customer object.
[333] Fix | Delete
$persist_fields = [];
[334] Fix | Delete
$errors = new \WP_Error();
[335] Fix | Delete
[336] Fix | Delete
// Validate individual fields agains the document object. Errors are added to the $errors object, and each field is validated regardless of other field errors.
[337] Fix | Delete
foreach ( $fields as $field_key => $field ) {
[338] Fix | Delete
$field_value = $field_values[ $field_key ];
[339] Fix | Delete
[340] Fix | Delete
if ( empty( $field_value ) ) {
[341] Fix | Delete
if ( true === $field['required'] ) {
[342] Fix | Delete
$errors->add(
[343] Fix | Delete
'required_field',
[344] Fix | Delete
/* translators: %s: is the field label */
[345] Fix | Delete
sprintf( __( '%s is required', 'woocommerce' ), '<strong>' . $field['label'] . '</strong>' )
[346] Fix | Delete
);
[347] Fix | Delete
continue;
[348] Fix | Delete
}
[349] Fix | Delete
$persist_fields[ $field_key ] = '';
[350] Fix | Delete
continue;
[351] Fix | Delete
}
[352] Fix | Delete
[353] Fix | Delete
$sanitized_field_value = $sanitized_field_values[ $field_key ];
[354] Fix | Delete
$valid_check = $this->checkout_fields_controller->validate_field( $field, $sanitized_field_value );
[355] Fix | Delete
[356] Fix | Delete
if ( is_wp_error( $valid_check ) && $valid_check->has_errors() ) {
[357] Fix | Delete
// Get one error message from the WP_Error object per field to avoid overlapping error messages.
[358] Fix | Delete
$errors->add( $valid_check->get_error_code(), $valid_check->get_error_message() );
[359] Fix | Delete
continue;
[360] Fix | Delete
}
[361] Fix | Delete
[362] Fix | Delete
$persist_fields[ $field_key ] = $sanitized_field_value;
[363] Fix | Delete
}
[364] Fix | Delete
[365] Fix | Delete
// Validate all fields for this location (this runs custom validation callbacks). If an error is found, no values will be persisted to the customer object.
[366] Fix | Delete
$location_validation = $this->checkout_fields_controller->validate_fields_for_location( $sanitized_field_values, $location, $group );
[367] Fix | Delete
[368] Fix | Delete
if ( is_wp_error( $location_validation ) && $location_validation->has_errors() ) {
[369] Fix | Delete
$errors->merge_from( $location_validation );
[370] Fix | Delete
return $errors;
[371] Fix | Delete
}
[372] Fix | Delete
[373] Fix | Delete
foreach ( $persist_fields as $field_key => $field_value ) {
[374] Fix | Delete
$this->checkout_fields_controller->persist_field_for_customer( $field_key, $field_value, $customer, $group );
[375] Fix | Delete
}
[376] Fix | Delete
[377] Fix | Delete
return $errors->has_errors() ? $errors : true;
[378] Fix | Delete
}
[379] Fix | Delete
}
[380] Fix | Delete
[381] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function