File: //home/globfdxw/www/wp-content/plugins/wpforms-pdf/src/Notifications/Fields/Address.php
<?php
namespace WPFormsPDF\Notifications\Fields;
/**
* Class Address field.
*
* @since 1.0.0
*/
class Address {
/**
* Class constructor.
*
* @since 1.0.0
*
* @return void
*/
public function __construct() {
$this->hooks();
}
/**
* Register hooks.
*
* @since 1.0.0
*
* @return void
*/
public function hooks(): void {
add_filter(
'wpforms_pdf_notifications_fields_field_html_value_address',
[ $this, 'field_html_value' ],
100,
4
);
}
/**
* Get field HTML value.
*
* @since 1.0.0
*
* @param string|mixed $value Field value.
* @param array $field Field data.
* @param array $form_data Form data.
* @param array $template Template data.
*
* @return string
* @noinspection PhpUnusedParameterInspection
*/
public function field_html_value( $value, array $field, array $form_data, array $template ): string {
$value = (string) $value;
$template_slug = $template['slug'] ?? '';
if ( $template_slug !== 'document-application' ) {
return $value;
}
$field = wp_parse_args(
$field,
[
'id' => '',
'address1' => '',
'address2' => '',
'city' => '',
'state' => '',
'postal' => '',
'country' => '',
]
);
if ( empty( $field['id'] ) ) {
return $value;
}
// Regenerate address field value.
$html = sprintf(
'<table>
<tr><td colspan="2">%1$s</td></tr>
%2$s
<tr><td>%3$s</td><td>%4$s</td></tr>
<tr><td>%5$s</td>%6$s</tr>
</table>',
esc_html( $field['address1'] ),
$this->get_line_2( $field ),
esc_html( $field['city'] ),
esc_html( $field['state'] ),
esc_html( $field['postal'] ),
$this->get_country( $field )
);
// Remove line breaks.
return preg_replace( '/\r?\n/', '', $html );
}
/**
* Get the second line of the address.
*
* @since 1.0.0
*
* @param array $field Field data.
*
* @return string
*/
protected function get_line_2( array $field ): string {
$line_2 = '';
if ( ! empty( $field['address2'] ) ) {
$line_2 = sprintf(
'<tr><td colspan="2">%1$s</td></tr>',
esc_html( $field['address2'] )
);
}
return $line_2;
}
/**
* Get the country of the address.
*
* @since 1.0.0
*
* @param array $field Field data.
*
* @return string
*/
protected function get_country( array $field ): string {
$country = '';
if ( ! empty( $field['country'] ) ) {
$country = sprintf(
'<td>%1$s</td>',
esc_html( $field['country'] )
);
}
return $country;
}
}