OS : Linux
PHP Version : 7.4.33
Software : name = 'Mailchimp';
$this->type = 'mailchimp';
$this->icon = 'fa-envelope-o';
$this->order = 21;
$this->defaults = [
[
'label' => __('Sign-up to our newsletter?', 'mailchimp-for-wp'),
'value' => '1',
'default' => '',
],
];
}
/**
* Field options panel inside the builder.
*
* @since 1.0.0
* @param array $field
*/
public function field_options($field)
{
//--------------------------------------------------------------------//
// Basic field options
//--------------------------------------------------------------------//
// Options open markup
$this->field_option('basic-options', $field, [ 'markup' => 'open' ]);
// Mailchimp list
$this->field_option_mailchimp_list($field);
// Choices
$this->field_option_choices($field);
// Description
$this->field_option('description', $field);
// Required toggle
$this->field_option('required', $field);
// Options close markup
$this->field_option('basic-options', $field, [ 'markup' => 'close' ]);
//--------------------------------------------------------------------//
// Advanced field options
//--------------------------------------------------------------------//
// Options open markup
$this->field_option('advanced-options', $field, [ 'markup' => 'open' ]);
// Custom CSS classes
$this->field_option('css', $field);
// Options close markup
$this->field_option('advanced-options', $field, [ 'markup' => 'close' ]);
}
private function field_option_mailchimp_list($field)
{
$mailchimp = new MC4WP_MailChimp();
// Field option label
$tooltip = __('Select the Mailchimp list to subscribe to.', 'mailchimp-for-wp');
$option_label = $this->field_element(
'label',
$field,
[
'slug' => 'mailchimp-list',
'value' => __('Mailchimp list', 'mailchimp-for-wp'),
'tooltip' => $tooltip,
],
false
);
$option_select = sprintf('';
// Field option row (markup) including label and input.
$output = $this->field_element(
'row',
$field,
[
'slug' => 'mailchimp-list',
'content' => $option_label . $option_select,
]
);
}
private function field_option_choices($field)
{
$tooltip = __('Set your sign-up label text and whether it should be pre-checked.', 'mailchimp-for-wp');
$values = ! empty($field['choices']) ? $field['choices'] : $this->defaults;
$class = ! empty($field['show_values']) && (int) $field['show_values'] === 1 ? 'show-values' : '';
$class .= ! empty($dynamic) ? ' wpforms-hidden' : '';
// Field option label
$option_label = $this->field_element(
'label',
$field,
[
'slug' => 'mailchimp-checkbox',
'value' => __('Sign-up checkbox', 'mailchimp-for-wp'),
'tooltip' => $tooltip,
],
false
);
// Field option choices inputs
$option_choices = sprintf('', $class, $field['id'], $this->type);
foreach ($values as $key => $value) {
$default = ! empty($value['default']) ? $value['default'] : '';
$option_choices .= sprintf('
';
// Field option row (markup) including label and input.
$output = $this->field_element(
'row',
$field,
[
'slug' => 'choices',
'content' => $option_label . $option_choices,
]
);
}
/**
* Field preview inside the builder.
*
* @since 1.0.0
* @param array $field
*/
public function field_preview($field)
{
$values = ! empty($field['choices']) ? $field['choices'] : $this->defaults;
// Field checkbox elements
echo '';
// Notify if currently empty
if (empty($values)) {
$values = [ 'label' => __('(empty)', 'wpforms') ];
}
// Individual checkbox options
foreach ($values as $key => $value) {
$default = isset($value['default']) ? $value['default'] : '';
$selected = checked('1', $default, false);
printf('
';
// Dynamic population is enabled and contains more than 20 items
if (isset($total) && $total > 20) {
echo '
All %d choices will be displayed when viewing the form.', 'wpforms'), absint($total));
echo '', $field_id, $field_class);
foreach ($choices as $key => $choice) {
$selected = isset($choice['default']) ? '1' : '0';
$depth = isset($choice['depth']) ? absint($choice['depth']) : 1;
printf('
';
}
/**
* Formats and sanitizes field.
*
* @since 1.0.2
* @param int $field_id
* @param array $field_submit
* @param array $form_data
*/
public function format($field_id, $field_submit, $form_data)
{
$field = $form_data['fields'][ $field_id ];
$choice = array_pop($field['choices']);
$name = sanitize_text_field($choice['label']);
$data = [
'name' => $name,
'value' => empty($field_submit) ? __('No', 'mailchimp-for-wp') : __('Yes', 'mailchimp-for-wp'),
'value_raw' => $field_submit,
'id' => absint($field_id),
'type' => $this->type,
];
wpforms()->process->fields[ $field_id ] = $data;
}
}