/**
 * 
 * Disparar E-mails
 * @param int $id comment id
 * @param int $comment_parent
 * @param int $comment_post
 * @param string $tipo
 */
function delibera_mailer_mailer($id, $comment_parent, $comment_post, $tipo = 'reply')
{
    require ABSPATH . WPINC . '/pluggable.php';
    add_filter('wp_mail_content_type', create_function('', 'return "text/html"; '));
    $comment = get_comment($id);
    $post = get_post($comment_post);
    $comment_parent = get_comment($comment_parent);
    if ($post->post_status == 'publish') {
        $options_plugin_delibera = delibera_get_config();
        if ($options_plugin_delibera["reposta-por-email"] == "N") {
            return false;
        }
        $subject_default = htmlspecialchars_decode($options_plugin_delibera["mailer_{$tipo}_assunto"]);
        $mensage_default = '';
        $users = get_users();
        if (!is_array($users)) {
            $users = array();
        }
        $seguiram = delibera_get_quem_seguiu($post->ID, 'ids');
        foreach ($users as $user) {
            if (user_can($user->ID, 'votar') && isset($user->user_email)) {
                $segue = array_search($user->ID, $seguiram);
                $user_notificacoes = get_user_meta($user->ID, 'delibera_resposta_email', true);
                if ($user_notificacoes == "N" && !$segue) {
                    continue;
                } else {
                    $mensage_tmp = $mensage_default;
                    $subject_tmp = $subject_default;
                    if (function_exists('qtrans_enableLanguage')) {
                        $lang = get_user_meta($user->ID, 'user_idioma', true);
                        if (strlen($lang) == 0) {
                            $lang = defined('WPLANG') && strlen(WPLANG) > 0 ? WPLANG : get_locale();
                        }
                        if (array_key_exists("mailer_{$tipo}-{$lang}", $options_plugin_delibera)) {
                            $mensage_tmp = htmlspecialchars_decode($options_plugin_delibera["mailer_{$tipo}-{$lang}"]) . delibera_notificar_get_mensagem_link($post);
                        }
                        if (array_key_exists("mailer_{$tipo}_assunto-{$lang}", $options_plugin_delibera)) {
                            $subject_tmp = htmlspecialchars_decode($options_plugin_delibera["mailer_{$tipo}_assunto-{$lang}"]);
                        }
                    }
                    $key = delibera_mailer_generateKey($user->ID, $id, get_current_blog_id());
                    $from = $options_plugin_delibera['delibera_mailer_from'];
                    $from_server = $options_plugin_delibera['delibera_mailer_from_server'];
                    $header = $headers = 'From: ' . $from . "\r\n" . 'Reply-To: ' . $key . '@' . $from_server . "\r\n" . 'X-Mailer: PHP/' . phpversion();
                    wp_mail($user->user_email, $subject_tmp . ' ' . get_the_title($comment_post), $mensage_tmp . get_comment_text($id), $header);
                }
            }
        }
    }
}
function delibera_notificar_representantes($mensage, $tipo, $post = false, $users = false, $link = false)
{
    require ABSPATH . WPINC . '/pluggable.php';
    add_filter('wp_mail_content_type', create_function('', 'return "text/html"; '));
    if ($post === false) {
        $post = get_post();
    }
    if (is_numeric($post)) {
        $post = get_post($post);
    }
    if ($post->post_status == 'publish') {
        $options_plugin_delibera = delibera_get_config();
        if ($options_plugin_delibera["notificacoes"] == "N" || !array_key_exists("{$tipo}-enabled", $options_plugin_delibera) || $options_plugin_delibera["{$tipo}-enabled"] == "N") {
            return false;
        }
        $subject_default = htmlspecialchars_decode($options_plugin_delibera["{$tipo}_assunto"]);
        $mensage_default = htmlspecialchars_decode($options_plugin_delibera[$tipo]) . $mensage . delibera_notificar_get_mensagem_link($post, $link);
        if (!is_array($users)) {
            $users = get_users();
        }
        if (!is_array($users)) {
            $users = array();
        }
        $autor_id = get_current_user_id();
        $seguiram = delibera_get_quem_seguiu($post->ID, 'ids');
        foreach ($users as $user) {
            if (user_can($user->ID, 'votar') && isset($user->user_email) && $user->ID != $autor_id) {
                $segue = array_search($user->ID, $seguiram);
                $user_notificacoes = get_user_meta($user->ID, 'delibera_notificacoes_email', true);
                if (!$segue && ($user_notificacoes == "N" || get_user_meta($user->ID, "{$tipo}-enabled", true) == "N")) {
                    continue;
                }
                $mensage_tmp = $mensage_default;
                $subject_tmp = $subject_default;
                $lang = get_user_meta($user->ID, 'user_idioma', true);
                if (strlen($lang) == 0) {
                    $lang = defined('WPLANG') && strlen(WPLANG) > 0 ? WPLANG : get_locale();
                }
                if (array_key_exists("{$tipo}-{$lang}", $options_plugin_delibera)) {
                    $mensage_tmp = htmlspecialchars_decode($options_plugin_delibera["{$tipo}-{$lang}"]) . $mensage;
                }
                if (array_key_exists("{$tipo}_assunto-{$lang}", $options_plugin_delibera)) {
                    $subject_tmp = htmlspecialchars_decode($options_plugin_delibera["{$tipo}_assunto-{$lang}"]);
                }
                $subject_tmp = delibera_notificar_replace_vars($subject_tmp, $user, $post);
                $mensage_tmp = delibera_notificar_replace_vars($mensage_tmp, $user, $post);
                wp_mail($user->user_email, $subject_tmp, $mensage_tmp);
            }
        }
    }
}