zaus / forms-3rdparty-integration

Wordpress Plugin - Forms 3rdparty Integration - a plugin to help integrate 3rdparty services with common WP Forms plugins like Contact Form 7 and Gravity Forms
http://wordpress.org/plugins/forms-3rdparty-integration/
47 stars 14 forks source link

Question on Adding Filters #46

Closed PthPndr closed 8 years ago

PthPndr commented 8 years ago

I ran across this plugin and it is exactly what we're looking for. I was able to setup the services with no issues but am having problems trying to setup a filter. I am not a developer so I am unsure about a lot of this.

We are using Gravity Forms and MailChimp to capture a user's birthday information. The Gravity Forms date format is mm/dd/yyyy and MailChimp wants mm/dd.

I ran across the code below which will do what I want but it is for the MailChimp's add-on for Gravity Forms. Is there a way to repurpose it for forms-3rdparty-integration?

        add_filter( 'gform_mailchimp_field_value', 'change_birthday', 10, 4 );
        function change_birthday( $value, $form_id, $field_id, $entry ) {
            //not the id for the birthday field, return value unchanged
            if ( $field_id != 4 ) {
                return $value;
            }

            $month = date( 'm', strtotime( $value ) ); //get month
            $day = date( 'd', strtotime( $value ) ); //get day
            $date = $month . '/' . $day; //build date into format needed by MailChimp
            return $date;       
        }

I can either submit mm/dd to MailChimp or I believe I can submit the mm and dd separately. Any help or point in the right direction would be greatly appreciated.

zaus commented 8 years ago

Just hook to ...service_filter_post to alter mapped values:

class Forms3rdpartyDateFormatting {
    const B = 'Forms3rdPartyIntegration';

    private $_field; // where to look for the expected field

    function __construct() {
        // hook even later than xpost so we can manipulate final output
        add_filter(self::B.'service_filter_post', array(&$this, 'adjust_format'), 11, 5); // don't actually need all 5, just the $post

        $this->_field = 'your-date-field'; // or get from a configurable wp_option?
    }

    public function adjust_format($post, $service, $form, $sid, $submission) {
        $date = $post[$this->_field];

        // do your parsing + formatting

        $post[$this->_field] = $date;

        return $post;
    }
}