=> false, 'filterable' => false, 'max' => '', 'min' => '', ); } public function get_defaults() { return $this->get_default_fields(); } public function get_default_fields() { $fields = array(); /** * Exclude from form actions to fix issue in myaccount edit billing and shipping form save */ $form_action = Helpers::get_form_action(); if ( 'account' !== $form_action && 'additional' !== $this->prefix ) { $prefix = sprintf( '%s_', $this->prefix ); /** * Fix nesting level */ remove_all_filters( 'woocommerce_' . $prefix . 'fields' ); $i = 0; foreach ( WC()->countries->get_address_fields( '', $prefix ) as $key => $field ) { $field['id'] = $i; $field['key'] = $key; $field['name'] = str_replace( $prefix, '', $key ); $fields[ $i ] = $field; $i++; } } return $fields; } public function get_fields() { // (is_array($fields = $this->get_items())) { $fields = $this->get_items(); if ( count( $fields ) ) { foreach ( $fields as $field_id => $field ) { $fields[ $field_id ] = apply_filters( 'wooccm_checkout_field_filter', $this->sanitize_field( $field_id, $field, $fields ), $field_id ); } uasort( $fields, array( __CLASS__, 'order_fields' ) ); $fields = apply_filters( 'wooccm_' . $this->prefix . '_fields', $fields ); } // } return $fields; } public function update_fields( $fields ) { if ( is_array( $fields ) ) { foreach ( $fields as $field_id => $field ) { if ( ! array_key_exists( 'name', $field ) ) { return false; } } // reorder array based on ids ksort( $fields ); if ( $this->save_items( $fields ) ) { return $fields; } } return false; } public function delete_fields() { $this->delete_checkout_field(); $this->delete(); $this->save_items( $this->get_default_fields() ); } protected function delete_checkout_field( $field_id = null ) { $fields_data = $this->get_fields(); if ( null !== $field_id ) { foreach ( $fields_data as $field ) { if ( $field['id'] == $field_id ) { $fields_data = array( $field ); continue; } } } $users = get_users( array( 'fields' => array( 'ID' ) ) ); foreach ( $users as $user ) { foreach ( $fields_data as $field ) { delete_user_meta( $user->ID, $field['key'] ); } } } // Field // --------------------------------------------------------------------------- public function add_field( $field_data ) { return $this->add_item( $field_data ); } public function get_field( $field_id ) { return $this->get_item( $field_id ); } public function update_field( $field_data ) { return $this->update_item( $field_data ); } public function delete_field( $field_id ) { $this->delete_checkout_field( $field_id ); return $this->delete_item( $field_id ); } // Sanitize public function sanitize_field( $field_id, $field, $fields ) { $field['id'] = $field_id; if ( empty( $field['name'] ) ) { $field['name'] = $this->get_name( $field_id ); if ( $this->duplicated_name( $field['name'], $fields ) ) { $field['name'] .= 'b'; } } $field['key'] = $this->get_key( $this->prefix, $field['name'] ); if ( empty( $field['position'] ) && is_array( $field['class'] ) ) { $position = array_intersect( (array) $field['class'], array( 'form-row-wide', 'form-row-first', 'form-row-last' ) ); if ( isset( $position[0] ) ) { $field['position'] = $position[0]; } else { $field['position'] = 'form-row-wide'; } } if ( empty( $field['order'] ) ) { $field['order'] = $field_id + 1; } if ( ! empty( $field['conditional_parent_key'] ) ) { if ( strpos( $field['conditional_parent_key'], $this->prefix ) === false ) { $field['conditional_parent_key'] = sprintf( '%s_%s', $this->prefix, $field['conditional_parent_key'] ); } if ( $field['conditional_parent_key'] == $field['key'] ) { $field['conditional_parent_key'] = ''; } } if ( is_array( $field['options'] ) && count( $field['options'] ) > 1 ) { uasort( $field['options'], array( __CLASS__, 'order_fields' ) ); } // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash $regex = $field['validate_regex']; wp_unslash( $field ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash $field['validate_regex'] = $regex; return $field; } }