first commit
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
<p>
|
||||
<?php
|
||||
/* translators: %s links to the Gravity Forms overview page */
|
||||
echo sprintf( __( 'To integrate with Gravity Forms, add the "Mailchimp for WordPress" field to <a href="%s">one of your Gravity Forms forms</a>.', 'mailchimp-for-wp' ), admin_url( 'admin.php?page=gf_edit_forms' ) );
|
||||
?>
|
||||
</p>
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
defined( 'ABSPATH' ) or exit;
|
||||
|
||||
mc4wp_register_integration( 'gravity-forms', 'MC4WP_Gravity_Forms_Integration', true );
|
||||
|
||||
if ( class_exists( 'GF_Fields' ) ) {
|
||||
GF_Fields::register( new MC4WP_Gravity_Forms_Field() );
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
class MC4WP_Gravity_Forms_Field extends GF_Field {
|
||||
|
||||
public $type = 'mailchimp';
|
||||
|
||||
/**
|
||||
* Returns the field markup; including field label, description, validation, and the form editor admin buttons.
|
||||
*
|
||||
* The {FIELD} placeholder will be replaced in GFFormDisplay::get_field_content with the markup returned by GF_Field::get_field_input().
|
||||
*
|
||||
* @param string|array $value The field value. From default/dynamic population, $_POST, or a resumed incomplete submission.
|
||||
* @param bool $force_frontend_label Should the frontend label be displayed in the admin even if an admin label is configured.
|
||||
* @param array $form The Form Object currently being processed.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_field_content( $value, $force_frontend_label, $form ) {
|
||||
$validation_message = ( $this->failed_validation && ! empty( $this->validation_message ) ) ? sprintf( "<div class='gfield_description validation_message'>%s</div>", $this->validation_message ) : '';
|
||||
$is_form_editor = $this->is_form_editor();
|
||||
$is_entry_detail = $this->is_entry_detail();
|
||||
$is_admin = $is_form_editor || $is_entry_detail;
|
||||
|
||||
$admin_buttons = $this->get_admin_buttons();
|
||||
|
||||
$description = $this->get_description( $this->description, 'gfield_description' );
|
||||
if ( $this->is_description_above( $form ) ) {
|
||||
$clear = $is_admin ? "<div class='gf_clear'></div>" : '';
|
||||
$field_content = sprintf( "%s%s{FIELD}%s$clear", $admin_buttons, $description, $validation_message );
|
||||
} else {
|
||||
$field_content = sprintf( '%s{FIELD}%s%s', $admin_buttons, $description, $validation_message );
|
||||
}
|
||||
|
||||
return $field_content;
|
||||
}
|
||||
|
||||
public function get_form_editor_field_title() {
|
||||
return esc_attr__( 'Mailchimp for WordPress', 'mailchimp-for-wp' );
|
||||
}
|
||||
|
||||
public function get_form_editor_field_settings() {
|
||||
return array(
|
||||
'label_setting',
|
||||
'description_setting',
|
||||
'css_class_setting',
|
||||
'mailchimp_list_setting',
|
||||
'mailchimp_double_optin',
|
||||
'mailchimp_precheck',
|
||||
'rules_setting',
|
||||
);
|
||||
}
|
||||
|
||||
public function get_field_input( $form, $value = '', $entry = null ) {
|
||||
$form_id = absint( $form['id'] );
|
||||
$is_entry_detail = $this->is_entry_detail();
|
||||
$is_form_editor = $this->is_form_editor();
|
||||
|
||||
$id = $this->id;
|
||||
$field_id = $is_entry_detail || $is_form_editor || 0 === (int) $form_id ? "input_$id" : 'input_' . $form_id . "_$id";
|
||||
$disabled_text = $is_form_editor ? 'disabled="disabled"' : '';
|
||||
|
||||
return sprintf( "<div class='ginput_container ginput_container_checkbox'><ul class='gfield_checkbox' id='%s'>%s</ul></div>", esc_attr( $field_id ), $this->get_checkbox_choices( $value, $disabled_text, $form_id ) );
|
||||
}
|
||||
|
||||
private function apply_mc4wp_options_filters( $options ) {
|
||||
$options = apply_filters( 'mc4wp_gravity-forms_integration_options', $options );
|
||||
$options = apply_filters( 'mc4wp_integration_gravity-forms_options', $options );
|
||||
return $options;
|
||||
}
|
||||
|
||||
|
||||
public function get_checkbox_choices( $value, $disabled_text, $form_id = 0 ) {
|
||||
$choices = '';
|
||||
$is_entry_detail = $this->is_entry_detail();
|
||||
$is_form_editor = $this->is_form_editor();
|
||||
|
||||
$options = array(
|
||||
'label' => $this->get_field_label( false, $value ),
|
||||
'precheck' => isset( $this->mailchimp_precheck ) ? $this->mailchimp_precheck : false,
|
||||
);
|
||||
$options = $this->apply_mc4wp_options_filters( $options );
|
||||
|
||||
// generate html
|
||||
$choice = array(
|
||||
'text' => $options['label'],
|
||||
'value' => '1',
|
||||
'isSelected' => $options['precheck'],
|
||||
);
|
||||
|
||||
$input_id = $this->id;
|
||||
if ( $is_entry_detail || $is_form_editor || 0 === (int) $form_id ) {
|
||||
$id = $this->id;
|
||||
} else {
|
||||
$id = $form_id . '_' . $this->id;
|
||||
}
|
||||
|
||||
if ( ! isset( $_GET['gf_token'] ) && empty( $_POST ) && rgar( $choice, 'isSelected' ) ) {
|
||||
$checked = "checked='checked'";
|
||||
} elseif ( is_array( $value ) && RGFormsModel::choice_value_match( $this, $choice, rgget( $input_id, $value ) ) ) {
|
||||
$checked = "checked='checked'";
|
||||
} elseif ( ! is_array( $value ) && RGFormsModel::choice_value_match( $this, $choice, $value ) ) {
|
||||
$checked = "checked='checked'";
|
||||
} else {
|
||||
$checked = '';
|
||||
}
|
||||
|
||||
$tabindex = $this->get_tabindex();
|
||||
$choice_value = $choice['value'];
|
||||
$choice_value = esc_attr( $choice_value );
|
||||
$choice_markup = "<li class='gchoice_{$id}'>
|
||||
<input name='input_{$input_id}' type='checkbox' value='{$choice_value}' {$checked} id='choice_{$id}' {$tabindex} {$disabled_text} />
|
||||
<label for='choice_{$id}' id='label_{$id}'>{$choice['text']}</label>
|
||||
</li>";
|
||||
|
||||
$choices .= gf_apply_filters(
|
||||
array(
|
||||
'gform_field_choice_markup_pre_render',
|
||||
$this->formId,
|
||||
$this->id,
|
||||
),
|
||||
$choice_markup,
|
||||
$choice,
|
||||
$this,
|
||||
$value
|
||||
);
|
||||
|
||||
return gf_apply_filters( array( 'gform_field_choices', $this->formId, $this->id ), $choices, $this );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
defined( 'ABSPATH' ) or exit;
|
||||
|
||||
/**
|
||||
* Class MC4WP_Ninja_Forms_Integration
|
||||
*
|
||||
* @ignore
|
||||
*/
|
||||
class MC4WP_Gravity_Forms_Integration extends MC4WP_Integration {
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $name = 'Gravity Forms';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $description = 'Subscribe visitors from your Gravity Forms forms.';
|
||||
|
||||
|
||||
/**
|
||||
* Add hooks
|
||||
*/
|
||||
public function add_hooks() {
|
||||
add_action( 'gform_field_standard_settings', array( $this, 'settings_fields' ), 10, 2 );
|
||||
add_action( 'gform_editor_js', array( $this, 'editor_js' ) );
|
||||
add_action( 'gform_after_submission', array( $this, 'after_submission' ), 10, 2 );
|
||||
}
|
||||
|
||||
public function after_submission( $submission, $form ) {
|
||||
$subscribe = false;
|
||||
$email_address = '';
|
||||
$mailchimp_list_id = '';
|
||||
$double_optin = $this->options['double_optin'];
|
||||
|
||||
// find email field & checkbox value
|
||||
foreach ( $form['fields'] as $field ) {
|
||||
if ( $field->type === 'email' && empty( $email_address ) && ! empty( $submission[ $field->id ] ) ) {
|
||||
$email_address = $submission[ $field->id ];
|
||||
}
|
||||
|
||||
if ( $field->type === 'mailchimp' && ! empty( $submission[ $field->id ] ) ) {
|
||||
$subscribe = true;
|
||||
$mailchimp_list_id = $field->mailchimp_list;
|
||||
|
||||
if ( isset( $field->mailchimp_double_optin ) ) {
|
||||
$double_optin = $field->mailchimp_double_optin;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $subscribe || empty( $email_address ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// override integration settings with field options
|
||||
$orig_options = $this->options;
|
||||
$this->options['lists'] = array( $mailchimp_list_id );
|
||||
$this->options['double_optin'] = $double_optin;
|
||||
|
||||
// perform the sign-up
|
||||
$this->subscribe( array( 'EMAIL' => $email_address ), $submission['form_id'] );
|
||||
|
||||
// revert back to original options in case request lives on
|
||||
$this->options = $orig_options;
|
||||
}
|
||||
|
||||
public function editor_js() {
|
||||
?>
|
||||
<script type="text/javascript">
|
||||
jQuery(document).on('gform_load_field_settings', function(evt, field) {
|
||||
jQuery('#field_mailchimp_list').val(field.mailchimp_list || '');
|
||||
jQuery('#field_mailchimp_double_optin').val(field.mailchimp_double_optin || "1");
|
||||
jQuery('#field_mailchimp_precheck').val(field.mailchimp_precheck || "0");
|
||||
});
|
||||
</script>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function settings_fields( $pos, $form_id ) {
|
||||
if ( $pos !== 0 ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$mailchimp = new MC4WP_MailChimp();
|
||||
$lists = $mailchimp->get_lists();
|
||||
?>
|
||||
<li class="mailchimp_list_setting field_setting">
|
||||
<label for="field_mailchimp_list" class="section_label">
|
||||
<?php esc_html_e( 'Mailchimp list', 'mailchimp-for-wp' ); ?>
|
||||
</label>
|
||||
<select id="field_mailchimp_list" onchange="SetFieldProperty('mailchimp_list', this.value)">
|
||||
<option value="" disabled><?php _e( 'Select a Mailchimp list', 'mailchimp-for-wp' ); ?></option>
|
||||
<?php
|
||||
foreach ( $lists as $list ) {
|
||||
echo sprintf( '<option value="%s">%s</option>', $list->id, $list->name );
|
||||
}
|
||||
?>
|
||||
</select>
|
||||
<p class="help">
|
||||
<?php echo __( 'Select the list(s) to which people who check the checkbox should be subscribed.', 'mailchimp-for-wp' ); ?>
|
||||
</p>
|
||||
</li>
|
||||
<li class="mailchimp_double_optin field_setting">
|
||||
<label for="field_mailchimp_double_optin" class="section_label">
|
||||
<?php esc_html_e( 'Double opt-in?', 'mailchimp-for-wp' ); ?>
|
||||
</label>
|
||||
<select id="field_mailchimp_double_optin" onchange="SetFieldProperty('mailchimp_double_optin', this.value)">
|
||||
<option value="1"><?php echo __( 'Yes', 'mailchimp-for-wp' ); ?></option>
|
||||
<option value="0"><?php echo __( 'No', 'mailchimp-for-wp' ); ?></option>
|
||||
</select>
|
||||
<p class="help">
|
||||
<?php _e( 'Select "yes" if you want people to confirm their email address before being subscribed (recommended)', 'mailchimp-for-wp' ); ?>
|
||||
</p>
|
||||
</li>
|
||||
<li class="mailchimp_precheck field_setting">
|
||||
<label for="field_mailchimp_precheck" class="section_label">
|
||||
<?php esc_html_e( 'Pre-check the checkbox?', 'mailchimp-for-wp' ); ?>
|
||||
</label>
|
||||
<select id="field_mailchimp_precheck" onchange="SetFieldProperty('mailchimp_precheck', this.value)">
|
||||
<option value="1"><?php echo __( 'Yes', 'mailchimp-for-wp' ); ?></option>
|
||||
<option value="0"><?php echo __( 'No', 'mailchimp-for-wp' ); ?></option>
|
||||
</select>
|
||||
<p class="help">
|
||||
<?php
|
||||
_e( 'Select "yes" if the checkbox should be pre-checked.', 'mailchimp-for-wp' );
|
||||
echo '<br />';
|
||||
printf( __( '<strong>Warning: </strong> enabling this may affect your <a href="%s">GDPR compliance</a>.', 'mailchimp-for-wp' ), 'https://www.mc4wp.com/kb/gdpr-compliance/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=integrations-page' );
|
||||
?>
|
||||
</p>
|
||||
</li>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function is_installed() {
|
||||
return class_exists( 'GF_Field' ) && class_exists( 'GF_Fields' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 3.0
|
||||
* @return array
|
||||
*/
|
||||
public function get_ui_elements() {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $form_id
|
||||
* @return string
|
||||
*/
|
||||
public function get_object_link( $form_id ) {
|
||||
return '<a href="' . admin_url( sprintf( 'admin.php?page=gf_edit_forms&id=%d', $form_id ) ) . '">Gravity Forms</a>';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user