';
$edit_field_html = $this->settings_display( $field_id, $campaign_data );
// Prepare to return compiled results.
wp_send_json_success(
[
'form_id' => isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0,
'field_id' => absint( $field_id ),
'field' => $field,
'preview' => $preview,
'options' => $options,
'max' => $this->max_allowed,
'edit_field_html' => $edit_field_html,
'wysiwyg' => $this->html_field,
]
);
}
/**
* Create the button for the 'Add Fields' tab, inside the form editor.
*
* @since 1.8.0
*
* @param array $fields List of form fields with their data.
*
* @return array
*/
public function field_button( $fields ) {
// Don't add if it is already added.
if ( isset( $fields[ $this->edit_section ]['fields'][ $this->type ] ) ) {
return $fields;
}
// Add field information to fields array.
$fields[ $this->edit_section ]['fields'][ $this->type ] = [
'order' => $this->order,
'name' => $this->name,
'type' => $this->type,
'icon' => $this->icon,
];
// Wipe hands clean.
return $fields;
}
/**
* Create the field options panel. Used by subclasses.
*
* @since 1.8.0
*
* @param array $field Field data and settings.
*/
abstract public function field_options( $field );
/**
* Create the field preview. Used by subclasses.
*
* @since 1.8.0
*
* @param array $field Field data and settings.
* @param array $campaign_data Campaign data and settings.
* @param integer $field_id Field ID.
*/
abstract public function field_preview( $field, $campaign_data, $field_id );
/**
* Display the field input elements on the frontend.
*
* @since 1.8.0
*
* @param array $field Field data and settings.
* @param array $field_atts Field attributes.
* @param array $form_data Form data and settings.
*/
abstract public function field_display( $field, $field_atts, $form_data );
/**
* Create the settings display panel. Used by subclasses.
*
* @since 1.8.0
*
* @param array $field campaign_data Campaign data and settings.
*/
abstract public function settings_display( $field, $campaign_data );
/**
* Display field input errors if present.
*
* @since 1.8.0
*
* @param string $key Input key.
* @param array $field Field data and settings.
*/
public function field_display_error( $key, $field ) {
// Need an error.
if ( empty( $field['properties']['error']['value'][ $key ] ) ) {
return;
}
printf(
'',
esc_attr( $field['properties']['inputs'][ $key ]['id'] ),
esc_html( $field['properties']['error']['value'][ $key ] )
);
}
/**
* Wrap field HTML title - a universal title function!
*
* @since 1.8.0
*
* @param string $title Field title.
*/
public function field_title( $title = false ) {
return '
' . $title . '
';
}
/**
* Wrap field HTML with a container that allows a (mostly) universal way of aligning and controlling visuals of the field in the preview area.
*
* @since 1.8.0
*
* @param string $html Field HTML.
* @param array $field_data Field data and settings.
*/
public function field_wrapper( $html = '', $field_data = false ) {
$align = isset( $field_data['align'] ) ? esc_attr( $field_data['align'] ) : $this->align_default;
$columns = isset( $field_data['columns'] ) ? intval( $field_data['columns'] ) : 1;
$width_percentage = isset( $field_data['width_percentage'] ) ? absint( $field_data['width_percentage'] ) : 100;
$css_classes = array(
'charitable-preview-field-container',
'charitable-preview-align-' . $align,
'charitable-preview-columns-' . $columns,
);
$html = '
' . $html . '
';
return $html;
}
/**
* Wrap field HTML with a container that allows a (mostly) universal way of aligning and controlling visuals of the field in the preview area.
*
* @since 1.8.0
*
* @param string $html Field HTML.
* @param array $field_data Field data and settings.
*/
public function field_display_wrapper( $html, $field_data = false ) {
$align = isset( $field_data['align'] ) ? esc_attr( $field_data['align'] ) : $this->align_default;
$columns = isset( $field_data['columns'] ) ? intval( $field_data['columns'] ) : 1;
$width_percentage = isset( $field_data['width_percentage'] ) ? absint( $field_data['width_percentage'] ) : 100;
$custom_css_class = isset( $field_data['css_class'] ) && '' !== trim( $field_data['css_class'] ) ? esc_attr( $field_data['css_class'] ) : false;
$css_classes = array(
'charitable-campaign-field-container',
'charitable-campaign-align-' . $align,
'charitable-campaign-columns-' . $columns,
);
if ( $custom_css_class ) {
$css_classes[] = $custom_css_class;
}
$html = '
' . $html . '
';
return $html;
}
/**
* Displays a preview area because the field can't be totally functional in a preview state (say on a preview campaign page)
*
* @since 1.8.0
*
* @param string $field_type Field type.
* @param array $field_data Any field data.
* @param array $campaign_data Form data and settings.
*/
public function show_preview( $field_type, $field_data = false, $campaign_data = false ) {
$field_label = ucwords( str_replace( '-', ' ', $field_type ) );
$html = '
';
/* translators: %s is the field label */
$html .= '
' . sprintf( esc_html__( 'The %s field cannot be rendered in preview mode.', 'charitable' ), $field_label ) . '
';
return apply_filters( 'charitable_campaign_builder_preview_html', $html, $field_type, $field_data, $campaign_data );
}
/**
* Keep a number within a certain range.
*
* @param integer $n The number.
* @param integer $min Min.
* @param integer $max Max.
* @return integer
*/
public function keep_in_range( $n, $min = 0, $max = 100 ) {
return max( min( $max, $n ), $min );
}
/**
* Get field name for ajax error message.
*
* @since 1.8.0
*
* @param string $name Field name for error triggered.
* @param array $field Field settings.
* @param array $props List of properties.
* @param string $error Error message.
*
* @return string
*/
public function ajax_error_field_name( $name, $field, $props, $error ) { // phpcs:ignore
if ( $name ) {
return $name;
}
$input = isset( $props['inputs']['primary'] ) ? $props['inputs']['primary'] : end( $props['inputs'] );
return (string) isset( $input['attr']['name'] ) ? $input['attr']['name'] : '';
}
}
endif;