Plugin Directory

Changeset 1509658


Ignore:
Timestamp:
10/07/2016 01:01:03 AM (9 years ago)
Author:
emfluencekc
Message:

v2.1: Add submission notifications

Location:
wp-emfluence/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • wp-emfluence/trunk/readme.txt

    r1428611 r1509658  
    22Tags: email, email marketing, emailmarketing, emfluence, api, marketing automation, widget, email widget, email signup, mailing list, newsletter, form, automation
    33Requires at least: 3.0.1
    4 Tested up to: 4.5.2
    5 Stable tag: 2.0
     4Tested up to: 4.6.1
     5Stable tag: 2.1
    66License: GPLv2 or later
    77License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    5252== Changelog ==
    5353
     54= 2.1 =
     55* Admins can choose to have submissions also sent to a notification email address.
     56
    5457= 2.0 =
    5558* Contacts are added to any groups selected by the admin. Private groups can be selected.
  • wp-emfluence/trunk/widget.php

    r1421095 r1509658  
    234234        } else {
    235235          // SUCCESS!
     236          $this->send_notification($instance, $data);
    236237          if(!empty($instance['success'])) $message = nl2br(wp_kses_post($instance['success']));
    237238          if(empty($message)) {
     
    329330      );
    330331    return;
     332  }
     333
     334  /**
     335   * If a notification target has been provided, send the submission data to it.
     336   * @param $instance
     337   * @param $data
     338   */
     339  protected function send_notification($instance, $data) {
     340    if(empty($instance['notify'])) return;
     341    $message = 'This is an automated notification. The following submission was received:' . "\n\n";
     342    foreach($data as $field=>$val) {
     343      if($field == 'customFields') {
     344        foreach($val as $custom_id=>$custom_val) {
     345          $instance_key = str_replace('custom', 'custom_', $custom_id);
     346          $label = $instance['fields'][$instance_key]['label'];
     347          if($instance['fields'][$instance_key]['type'] === 'true-false') {
     348            $custom_val['value'] = $custom_val['value'] ? 'Yes' : 'No';
     349          }
     350          if($instance['fields'][$instance_key]['type'] === 'textarea') {
     351            $label .= "\n";
     352          }
     353          $message .= $label . ' ' . $custom_val['value'] . "\n";
     354        }
     355        continue;
     356      }
     357      $message .= $field . ': ' . $this->recursively_convert_to_string($val);
     358    }
     359    wp_mail(
     360        $instance['notify'],
     361        'New email signup form submission for "' . $instance['title'] . '"',
     362        $message
     363        );
     364  }
     365
     366  /**
     367   * Format an array for use in a plain text message.
     368   * For use in the email notification.
     369   * @param $data
     370   * @param int $level
     371   * @return string
     372   */
     373  protected function recursively_convert_to_string($data, $level = 0) {
     374
     375    if(is_string($data)) return $data . "\n";
     376    if(!is_array($data)) return '';
     377    $out = "\n";
     378    foreach($data as $key=>$val) {
     379      $out .= str_repeat('-', $level) . ' ' . $key . ': ' . $this->recursively_convert_to_string($val, $level + 1);
     380    }
     381    return $out;
     382
     383  }
     384
     385  /**
     386   * Convert a potential CSV of email addresses to an array of trimmed email addresses.
     387   * @param string $address_string
     388   * @return array
     389   */
     390  protected function explode_emails($address_string) {
     391    $emails = explode(',', $address_string);
     392    foreach($emails as &$email) {
     393      $email = trim($email);
     394    }
     395    return array_filter($emails);
    331396  }
    332397
     
    390455          <textarea id="' . $this->get_field_id( 'success' ) . '" name="' . $this->get_field_name( 'success' ) . '" style="width:100%;" >' . $instance['success'] . '</textarea>
    391456          NOTE: If you set the success message here, any theme template file emfluence/success.php will be ignored.
     457        </p>
     458      </div>' . "\n";
     459    return $output;
     460  }
     461
     462  /**
     463   * @param $instance
     464   * @return string
     465   */
     466  protected function form_template_notification($instance) {
     467    $validation = '';
     468    if(!empty($instance['notify'])) {
     469      $emails = $this->explode_emails($instance['notify']);
     470      foreach($emails as $email) {
     471        if(!is_email($email)) {
     472          $validation = '<div class="validation error">The email address you entered is not valid</div>';
     473        }
     474      }
     475    }
     476    $output = '
     477      <h3>' . __('Notification') . '</h3>
     478      <div class="text_display">
     479        <p>
     480          <label for="' . $this->get_field_id( 'notify' ) . '">' . __('Email Address') . ':</label>
     481          <input type="text" id="' . $this->get_field_id( 'notify' ) . '" name="' . $this->get_field_name( 'notify' ) . '" value="' . $instance['notify'] . '" style="width:100%;" />
     482          (Leave blank to disable notification)
     483          ' . $validation .'
    392484        </p>
    393485      </div>' . "\n";
     
    630722        'submit' => __('Signup'),
    631723        'fields' => array(),
     724        'notify' => ''
    632725    );
    633726
     
    776869    $output .= $this->form_template_basic_fields($defaults, $instance);
    777870    $output .= $this->form_template_custom_variables($defaults, $instance);
     871    $output .= $this->form_template_notification($instance);
    778872
    779873    // Output the datalist for groups just once
     
    863957    $instance['submit'] = stripslashes($new_instance['submit']);
    864958    $instance['success'] = stripslashes($new_instance['success']);
     959    $instance['notify'] = stripslashes($new_instance['notify']);
    865960
    866961    // If the current user isn't allowed to use unfiltered HTML, filter it
Note: See TracChangeset for help on using the changeset viewer.