Exemplo n.º 1
0
function wpcf7_mch_subscribe($obj)
{
    $cf7_mch = get_option('cf7_mch_' . $obj->id());
    //var_dump($obj->id());
    //exit(0);
    $submission = WPCF7_Submission::get_instance();
    $logfileEnabled = $cf7_mch['logfileEnabled'];
    $logfileEnabled = is_null($logfileEnabled) ? false : $logfileEnabled;
    if ($cf7_mch) {
        $subscribe = false;
        $regex = '/\\[\\s*([a-zA-Z_][0-9a-zA-Z:._-]*)\\s*\\]/';
        $callback = array(&$obj, 'cf7_mch_callback');
        $email = cf7_mch_tag_replace($regex, $cf7_mch['email'], $submission->get_posted_data());
        $name = cf7_mch_tag_replace($regex, $cf7_mch['name'], $submission->get_posted_data());
        $lists = cf7_mch_tag_replace($regex, $cf7_mch['list'], $submission->get_posted_data());
        $listarr = explode(',', $lists);
        $merge_vars = array('FNAME' => $name);
        // *x1
        // *x2
        $parts = explode(" ", $name);
        if (count($parts) > 1) {
            // *x3
            $lastname = array_pop($parts);
            $firstname = implode(" ", $parts);
            $merge_vars = array('FNAME' => $firstname, 'LNAME' => $lastname);
        } else {
            // *x4
            $merge_vars = array('FNAME' => $name);
            // *x5
        }
        if (isset($cf7_mch['accept']) && strlen($cf7_mch['accept']) != 0) {
            $accept = cf7_mch_tag_replace($regex, $cf7_mch['accept'], $submission->get_posted_data());
            if ($accept != $cf7_mch['accept']) {
                if (strlen($accept) > 0) {
                    $subscribe = true;
                }
            }
        } else {
            $subscribe = true;
        }
        for ($i = 1; $i <= 20; $i++) {
            if (isset($cf7_mch['CustomKey' . $i]) && isset($cf7_mch['CustomValue' . $i]) && strlen(trim($cf7_mch['CustomValue' . $i])) != 0) {
                $CustomFields[] = array('Key' => trim($cf7_mch['CustomKey' . $i]), 'Value' => cf7_mch_tag_replace($regex, trim($cf7_mch['CustomValue' . $i]), $submission->get_posted_data()));
                $NameField = trim($cf7_mch['CustomKey' . $i]);
                $NameField = strtr($NameField, "[", "");
                $NameField = strtr($NameField, "]", "");
                $merge_vars = $merge_vars + array($NameField => cf7_mch_tag_replace($regex, trim($cf7_mch['CustomValue' . $i]), $submission->get_posted_data()));
            }
        }
        if (isset($cf7_mch['confsubs']) && strlen($cf7_mch['confsubs']) != 0) {
            $mce_csu = true;
        } else {
            $mce_csu = false;
        }
        if ($subscribe && $email != $cf7_mch['email']) {
            if (!class_exists('Mailchimp')) {
                require_once SPARTAN_MCE_PLUGIN_DIR . '/api/Mailchimp.php';
            }
            $wrap = new Mailchimp($cf7_mch['api']);
            $Mailchimp = new Mailchimp($cf7_mch['api']);
            $Mailchimp_Lists = new Mailchimp_Lists($Mailchimp);
            // *x6
            try {
                foreach ($listarr as $listid) {
                    $listid = trim($listarr[0]);
                    $result = $wrap->lists->subscribe($listid, array('email' => $email), $merge_vars, 'html', $mce_csu, true, false, false);
                }
                $mch_debug_logger = new mch_Debug_Logger();
                $mch_debug_logger->log_mch_debug('Submission mail - Result: Sent Mail Ok ', 1, $logfileEnabled);
            } catch (Exception $e) {
                //echo 'Error, check your error log file for details';
                $mch_debug_logger = new mch_Debug_Logger();
                $mch_debug_logger->log_mch_debug('Submission mail - Result: ' . $e->getMessage(), 4, $logfileEnabled);
            }
        }
    }
}
function wpcf7_mch_subscribe($obj)
{
    $cf7_mch = get_option('cf7_mch_' . $obj->id());
    $submission = WPCF7_Submission::get_instance();
    if ($cf7_mch) {
        $subscribe = false;
        $regex = '/\\[\\s*([a-zA-Z_][0-9a-zA-Z:._-]*)\\s*\\]/';
        $callback = array(&$obj, 'cf7_mch_callback');
        $email = cf7_mch_tag_replace($regex, $cf7_mch['email'], $submission->get_posted_data());
        $name = cf7_mch_tag_replace($regex, $cf7_mch['name'], $submission->get_posted_data());
        $lists = cf7_mch_tag_replace($regex, $cf7_mch['list'], $submission->get_posted_data());
        $listarr = explode(',', $lists);
        $merge_vars = array('FNAME' => $name);
        //By default the key label for the name must be FNAME
        if (isset($cf7_mch['accept']) && strlen($cf7_mch['accept']) != 0) {
            $accept = cf7_mch_tag_replace($regex, $cf7_mch['accept'], $submission->get_posted_data());
            if ($accept != $cf7_mch['accept']) {
                if (strlen($accept) > 0) {
                    $subscribe = true;
                }
            }
        } else {
            $subscribe = true;
        }
        for ($i = 1; $i <= 20; $i++) {
            if (isset($cf7_mch['CustomKey' . $i]) && isset($cf7_mch['CustomValue' . $i]) && strlen(trim($cf7_mch['CustomValue' . $i])) != 0) {
                $CustomFields[] = array('Key' => trim($cf7_mch['CustomKey' . $i]), 'Value' => cf7_mch_tag_replace($regex, trim($cf7_mch['CustomValue' . $i]), $submission->get_posted_data()));
                $NameField = trim($cf7_mch['CustomKey' . $i]);
                $NameField = strtr($NameField, "[", "");
                $NameField = strtr($NameField, "]", "");
                $merge_vars = $merge_vars + array($NameField => cf7_mch_tag_replace($regex, trim($cf7_mch['CustomValue' . $i]), $submission->get_posted_data()));
            }
        }
        if (isset($cf7_mch['resubscribeoption']) && strlen($cf7_mch['resubscribeoption']) != 0) {
            $ResubscribeOption = true;
        } else {
            $ResubscribeOption = false;
        }
        if ($subscribe && $email != $cf7_mch['email']) {
            if (!class_exists('Mailchimp')) {
                require_once SPARTAN_MCE_PLUGIN_DIR . '/api/Mailchimp.php';
            }
            $wrap = new Mailchimp($cf7_mch['api']);
            $Mailchimp = new Mailchimp($cf7_mch['api']);
            $Mailchimp_Lists = new Mailchimp_Lists($Mailchimp);
            // check if subscribed
            try {
                foreach ($listarr as $listid) {
                    $listid = trim($listarr[0]);
                    $result = $wrap->lists->subscribe($listid, array('email' => $email), $merge_vars, false, false, false, false);
                }
            } catch (Exception $e) {
                //echo 'Error, check your error log file for details';
                error_log($e->getMessage(), 0);
                error_log($e->getMessage(), 1);
            }
        }
    }
}