',
esc_attr( $columns_class ),
$columns_html // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
);
}
/**
* Generate layout field preview column content.
*
* @since 1.8.9
*
* @param array $column Column data.
*
* @return string Column content HTML.
*/
public function field_preview_column_content( $column ): string {
$content = $this->get_field_preview_column_plus_placeholder_template();
if ( empty( $column['fields'] ) || ! is_array( $column['fields'] ) ) {
return $content;
}
$panel_fields_obj = WPForms_Builder_Panel_Fields::instance();
ob_start();
foreach ( $column['fields'] as $field_id ) {
if ( empty( $panel_fields_obj->form_data['fields'][ $field_id ] ) ) {
continue;
}
$panel_fields_obj->preview_single_field( $panel_fields_obj->form_data['fields'][ $field_id ], [] );
}
$content .= ob_get_clean();
return $content;
}
/**
* Process layout fields data before saving the form.
*
* @since 1.8.9
*
* @param array $form Form array which is usable with `wp_update_post()`.
* @param array $data Data retrieved from $_POST and processed.
* @param array $args Empty by default. May contain custom data not intended to be saved, but used for processing.
*
* @return array
* @noinspection PhpMissingParamTypeInspection
* @noinspection PhpUnusedParameterInspection
*/
public function save_form_args( $form, $data, $args ): array {
$form = (array) $form;
// Get a filtered form content.
$form_data = json_decode( stripslashes( $form['post_content'] ), true );
if ( empty( $form_data['fields'] ) || empty( $args['context'] ) || $args['context'] !== 'save_form' ) {
return $form;
}
foreach ( (array) $form_data['fields'] as $id => $field ) {
// Process only Layout-based fields.
if ( empty( $field['type'] ) || $field['type'] !== $this->field_obj->type ) {
continue;
}
// Decode columns data from JSON.
if ( isset( $field['columns-json'] ) ) {
$field['columns'] = $this->decode_columns_json( $field['columns-json'] );
// Do not need to store JSON.
unset( $field['columns-json'] );
}
// Set defaults to some field options.
// For example, we don't have a Label option in the Form Builder.
$form_data['fields'][ $id ] = wp_parse_args( $field, $this->field_obj->defaults );
}
$form['post_content'] = wpforms_encode( $form_data );
return $form;
}
/**
* Decode columns JSON data.
*
* @since 1.8.9
*
* @param string $json Columns JSON data.
*
* @return array
*/
private function decode_columns_json( string $json ): array {
$columns = (array) json_decode( $json, true );
// Ensure that each column has the `fields` array.
foreach ( $columns as $c => $column ) {
$columns[ $c ]['fields'] = $column['fields'] ?? [];
}
return $columns;
}
/**
* Pass localized strings to builder.
*
* @since 1.8.9
*
* @param array|mixed $strings All strings that will be passed to builder.
* @param WP_Post $form Form object.
*
* @return array
* @noinspection HtmlUnknownTarget
* @noinspection PhpMissingParamTypeInspection
* @noinspection PhpUnusedParameterInspection
*/
public function get_localized_strings( $strings, $form ): array { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
$strings = (array) $strings;
$legacy_layout_notice_text = sprintf(
wp_kses( /* translators: %1$s - WPForms.com URL to a related doc. */
__( 'We’ve added a new field to help you build advanced form layouts more easily. Give the Layout Field a try! Layout CSS classes are still supported. Learn More', 'wpforms' ),
[
'a' => [
'href' => [],
'target' => [],
'rel' => [],
],
]
),
esc_url(
wpforms_utm_link( 'https://wpforms.com/docs/how-to-use-the-layout-field-in-wpforms/', 'Field Options', 'How to Use the Layout Field Documentation' )
)
);
$strings['layout'] = [
'not_allowed_fields' => $this->field_obj->get_not_allowed_fields(),
/* translators: %s - field name. */
'not_allowed_alert_text' => esc_html__( 'The %s field can’t be placed inside a Layout field.', 'wpforms' ),
'empty_label' => esc_html__( 'Layout', 'wpforms' ),
'got_it' => esc_html__( 'Got it!', 'wpforms' ),
'size_notice_text' => esc_html__( 'Field size cannot be changed when used in a layout.', 'wpforms' ),
'size_notice_tooltip' => esc_html__( 'When a field is placed inside a column, the field size always equals the column width.', 'wpforms' ),
'dont_show_again' => esc_html__( 'Don’t Show Again', 'wpforms' ),
'legacy_layout_notice_title' => esc_html__( 'Layouts Have Moved!', 'wpforms' ),
'legacy_layout_notice_text' => $legacy_layout_notice_text,
'enabled_cf_alert_text' => esc_html__( 'Conversational Forms cannot be enabled because your form contains a Layout field.', 'wpforms' ),
'field_add_cf_alert_text' => esc_html__( 'The Layout field cannot be used when Conversational Forms is enabled.', 'wpforms' ),
'delete_confirm' => esc_html__( 'Are you sure you want to delete the Layout field? Deleting this field will also delete the fields inside it.', 'wpforms' ),
'cl_notice_text' => esc_html__( 'Cannot be enabled because the Layout field contains Conditional Logic.', 'wpforms' ),
'cl_notice_text_grp' => esc_html__( 'Conditional Logic has been disabled because the Layout field contains Conditional Logic.', 'wpforms' ),
];
return $strings;
}
/**
* Get template for the column "plus" placeholder.
*
* @since 1.8.9
*
* @return string
*/
private function get_field_preview_column_plus_placeholder_template(): string {
return sprintf(
'
%1$s
',
esc_html__( 'Add Fields', 'wpforms' )
);
}
/**
* Output template for the column "plus" placeholder.
*
* @since 1.8.9
*/
public function field_preview_column_plus_placeholder_template() {
?>