MailPoet'; printf( __( '%s plugin requires %s plugin, Please activate %s first to using %s.', 'add-on-gravity-forms-mailpoet' ), $name, $mp_link, $mp_link, $name ); ?>

$notice

"; } delete_option( 'gf_new_mailpoet_plugin_deferred_admin_notices' ); } } register_deactivation_hook( __FILE__, 'gf_new_mailpoet_plugin_deactivation' ); function gf_new_mailpoet_plugin_deactivation() { delete_option( 'gf_new_mailpoet_plugin_deferred_admin_notices' ); } /** * array_column alternatives for PHP version less than PHP 5.5 * this file / function has no effect on php version greater than php 5.5 */ include plugin_dir_path( __FILE__ ) . '/array_column.php'; /** * Mailpoet class load */ use MailPoet\Models\Subscriber; use MailPoet\Models\Segment; use MailPoet\Subscribers\ConfirmationEmailMailer; use MailPoet\Subscribers\NewSubscriberNotificationMailer; use MailPoet\Models\CustomField; /* * Add a MailPoet custom field in each gravity form field * */ add_action( 'gform_field_standard_settings', 'my_standard_settings', 10, 2 ); function my_standard_settings( $position, $form_id ) { //create settings on position 25 (right after Field Label) if ( $position == 25 ) { ?>
  • ' . PHP_EOL; foreach ( GF_Fields::get_all() as $gf_field ) { echo 'fieldSettings.' . $gf_field->type . ' += ", .mpcf_settings";' . PHP_EOL; } //binding to the load field settings event to initialize echo "jQuery(document).on('gform_load_field_settings', function(event, field, form){ if (typeof field['mpcfName'] !== 'undefined') { jQuery('#mcf_field_name').attr('value', field['mpcfName']); } else { jQuery('#mcf_field_name').attr('value', ''); } });"; echo '' . PHP_EOL; } ); add_filter( 'gform_tooltips', 'add_encryption_tooltips' ); function add_encryption_tooltips( $tooltips ) { $fields = CustomField::findMany(); $results = array(); foreach ( $fields as $field ) { $results[ 'cf_' . $field->id ] = $field->name; } if ( ! empty( $results ) ) { $tooltips['form_field_encrypt_value'] = ''; foreach ( $results as $key => $value ) { $tooltips['form_field_encrypt_value'] .= __("MailPoet custom field name: ", "add-on-gravity-forms-mailpoet") . "" . $value . "" . "
    " . __("MailPoet custom field ID : ", "add-on-gravity-forms-mailpoet") . "" . $key . "" . "

    "; } } else { $tooltips['form_field_encrypt_value'] = __("No MailPoet custom field available", "add-on-gravity-forms-mailpoet"); } return $tooltips; } /** * Add mailpoet list to choice. */ add_action( 'gform_predefined_choices', 'mailpoet_predefiend_list' ); function mailpoet_predefiend_list( $choices ) { $ret = array(); $segments = Segment::where_not_equal( 'type', Segment::TYPE_WP_USERS )->findArray(); foreach ( $segments as $s_key => $s_val ) { $ret['Mailpoet List'][] = $s_val['name'] . '|' . $s_val['id']; } foreach ( $choices as $key => $value ) { $ret[ $key ] = $value; } return $ret; } /** * Set default input */ add_action( 'gform_editor_js_set_default_values', 'mailpoet_list_set_default' ); function mailpoet_list_set_default() { $segments = Segment::where_not_equal( 'type', Segment::TYPE_WP_USERS )->findArray(); $choice = '['; foreach ( $segments as $key => $value ) { $choice .= 'new Choice("' . $value["name"] . '","' . $value["id"] . '"), '; } $choice .= '];'; if ( empty( $segments ) ) { $choice = "[new Choice('List one'), new Choice('List two'), new Choice('Please set a list')];"; } ?> case "mailpoet": field.label = "Subscribe"; field.choices = break; id; $email = rgar( $entry, $email_id ); if ( empty( $email ) ) { return; } $subscriber = Subscriber::findOne( $email ); /* Deleted this code in version 1.1.6 to fix existing list id replace issue*/ /* if ( false !== $subscriber ){ $segments = $subscriber->segments()->findArray(); if ( !empty($segments) ){ return; } }*/ /* Deleted code end. Version 1.1.6 */ $subscriber_data = array( 'email' => $email ); // extract name $name_key = array_search( 'name', array_column( $form['fields'], 'type' ) ); if ( false === $name_key ) { $name_key = array_search( 'name', array_column( array_map( 'get_object_vars', $form['fields'] ), 'type' ) ); } if ( is_integer( $name_key ) ) { $fname_id = array_search( 'First', array_column( $form['fields'][ $name_key ]->inputs, 'label' ) ); $fname_id = $form['fields'][ $name_key ]->inputs[ $fname_id ]['id']; $lname_id = array_search( 'Last', array_column( $form['fields'][ $name_key ]->inputs, 'label' ) ); $lname_id = $form['fields'][ $name_key ]->inputs[ $lname_id ]['id']; $first_name = rgar( $entry, $fname_id ); $last_name = rgar( $entry, $lname_id ); $subscriber_data['first_name'] = $first_name; $subscriber_data['last_name'] = $last_name; } // extract mailpoet list ids $mp_key = array_search( 'mailpoet', array_column( $form['fields'], 'type' ) ); if ( false === $mp_key ) { $mp_key = array_search( 'mailpoet', array_column( array_map( 'get_object_vars', $form['fields'] ), 'type' ) ); } if ( ! is_integer( $mp_key ) ) { return; } $mp_id = (array) $form['fields'][ $mp_key ]; $mp_id = array_column( $mp_id['inputs'], 'id' ); $mp_list = []; foreach ( $mp_id as $key => $value ) { $lst = rgar( $entry, $value ); if ( ! empty( $lst ) ) { if ( is_integer( $lst ) || is_numeric( $lst ) ) { $mp_list[] = $lst; } else { $list = Segment::where( 'name', $lst )->findArray(); if ( ! empty( $list ) ) { $list = array_shift( $list ); $mp_list[] = isset( $list['id'] ) ? $list['id'] : null; } } } } // subscribe to if ( ! empty( $mp_list ) ) { /* Added this to fix old list replace issue. Version 1.1.6 */ //If user is already subscribed then get the existed list id. if ( $subscriber ) { $subscriber->withSubscriptions(); $old_lists = $subscriber->subscriptions; foreach ( $old_lists as $key => $value ) { $list_ids[] = $value['segment_id']; } $mp_list = array_merge( $mp_list, $list_ids ); } /* Fix added end. Version 1.1.6 */ $subscriber_data['segments'] = $mp_list; $subscriber = Subscriber::createOrUpdate( $subscriber_data ); $sagments = $subscriber->segments()->findMany(); $notification_sender = new NewSubscriberNotificationMailer(); if ( ! empty( $subscriber ) ) { $subscriber_status = Subscriber::whereEqual( 'email', $email )->whereEqual( 'status', 'subscribed' )->findArray(); //If the subscriber is new, send email confirmation to subscribe for the list. Otherwise, if subscriber already confirm the email for another list and subscribe to the new list, don't send confirmation email again. if ( empty( $subscriber_status[0]['confirmed_ip'] ) ) { $confirmation_sender = new ConfirmationEmailMailer(); $confirmation_sender->sendConfirmationEmail( $subscriber ); } //Notify the admin that existed subscriber subscribed to the new list else { $notification_sender->send( $subscriber, $sagments ); } } } }