$found_module = true;
 $codeModule = '';
 while ($found_module) {
     $found_module = false;
     if (strpos($bodyTemplate, '[start module ' . ($count_modules + 1) . ']') !== false) {
         $found_module = true;
         $codeModule .= '<div class="insertable"><img src="' . $url_template . $Knews_plugin->post_safe('template') . '/modules/' . ($mobile ? 'm_' : '') . 'module' . $Knews_plugin->post_safe('vp_' . $Knews_plugin->post_safe('template')) . '_' . ($count_modules + 1) . '.jpg" width="220" height="90" alt="" /><div class="html_content">';
         $extracted_module = knews_cut_code('<!--[start module ' . ($count_modules + 1) . ']-->', '<!--[end module ' . ($count_modules + 1) . ']-->', $bodyTemplate, true);
         $codeModule .= $extracted_module . '</div></div>';
         if (strpos($extracted_module, 'dont_cut_module') === false) {
             $bodyTemplate = knews_extract_code('<!--[start module ' . ($count_modules + 1) . ']-->', '<!--[end module ' . ($count_modules + 1) . ']-->', $bodyTemplate, true);
         }
         $count_modules++;
     }
 }
 $containerModulesTemplate = knews_cut_code('<!--[open_insertion_container_start]-->', '<!--[close_insertion_container_start]-->', $bodyTemplate, true) . knews_cut_code('<!--[open_insertion_container_end]-->', '<!--[close_insertion_container_end]-->', $bodyTemplate, true);
 $bodyTemplate = knews_iterative_extract_code('<!--[open_ignore_code]-->', '<!--[close_ignore_code]-->', $bodyTemplate, true);
 $bodyTemplate = knews_iterative_extract_code('<!--[', ']-->', $bodyTemplate, true);
 $codeTemplate = str_replace('  ', ' ', $codeTemplate);
 $date = $Knews_plugin->get_mysql_date();
 if (!knews_is_utf8($bodyTemplate)) {
     $bodyTemplate = utf8_encode($bodyTemplate);
 }
 if (!knews_is_utf8($headTemplate)) {
     $headTemplate = utf8_encode($headTemplate);
 }
 if (!knews_is_utf8($codeModule)) {
     $codeModule = utf8_encode($codeModule);
 }
 $bodyTemplate = esc_sql($Knews_plugin->htmlentities_corrected($bodyTemplate));
 $headTemplate = esc_sql($Knews_plugin->htmlentities_corrected($headTemplate));
function knews_create_news($aj, $pend_posts, $news, $fp, $mobile, $mobile_news_id)
{
    global $Knews_plugin, $knewsOptions, $wpdb;
    $news_mod = $news[0]->html_mailing;
    //Cut the newsletter into modules
    $news_mod = explode('<!--[start module]-->', $news_mod);
    $news_mod2 = array();
    $first = true;
    foreach ($news_mod as $nm) {
        if ($first) {
            $first = false;
            $news_mod2[] = $nm;
        } else {
            $cut_nm = explode('<!--[end module]-->', $nm);
            $news_mod2[] = $cut_nm[0];
            $news_mod2[] = $cut_nm[1];
        }
    }
    $news_mod_map = array();
    $news_mod_map_duplicated = array();
    $total_posts = 0;
    foreach ($news_mod2 as $nm) {
        $found = true;
        $n = 1;
        while ($found) {
            $found = false;
            if (strpos($nm, '%the_title_' . $n . '%') !== false || strpos($nm, '%the_excerpt_' . $n . '%') !== false || strpos($nm, '%the_permalink_' . $n . '%') !== false || strpos($nm, '%the_content_' . $n . '%') !== false) {
                $found = true;
                $n++;
            }
        }
        $news_mod_map[] = $n - 1;
        $news_mod_map_duplicated[] = false;
        $total_posts = $total_posts + $n - 1;
    }
    /*for ($a=1; $a<10; $a++) {
    		$news_mod = str_replace('%the_permalink_' . $a . '%', '%the_permalink%', $news_mod);
    		$news_mod = str_replace('%the_title_' . $a . '%', '%the_title%', $news_mod);
    		$news_mod = str_replace('%the_excerpt_' . $a . '%', '%the_excerpt%', $news_mod);
    		$news_mod = str_replace('%the_content_' . $a . '%', '%the_content%', $news_mod);
    	}*/
    $s = 0;
    while ($total_posts < count($pend_posts) && $total_posts != 0) {
        if ($news_mod_map[$s] != 0 && !$news_mod_map_duplicated[$s]) {
            knews_debug('- enlarging the newsletter, have ' . $total_posts . ' and must have ' . count($pend_posts) . "\r\n");
            array_splice($news_mod2, $s + 1, 0, array('</td></tr><tr class="droppable"><td>' . $news_mod2[$s]));
            array_splice($news_mod_map, $s + 1, 0, array($news_mod_map[$s]));
            array_splice($news_mod_map_duplicated, $s + 1, 0, array(true));
            $total_posts = $total_posts + $news_mod_map[$s];
            knews_debug('- module duplicated, news now can contain ' . $total_posts . " posts inside\r\n");
            $s++;
        }
        $s++;
        if ($s >= count($news_mod_map)) {
            $s = 0;
        }
    }
    if ($total_posts >= count($pend_posts)) {
        $subject = $news[0]->subject;
        $most_recent = 0;
        foreach ($pend_posts as $pp) {
            global $post;
            $post = get_post($pp->ID);
            //qTranslate support
            $permalink = get_permalink();
            if (KNEWS_MULTILANGUAGE && $knewsOptions['multilanguage_knews'] == 'qt' && function_exists('qtrans_convertURL')) {
                $post = qtrans_use($aj->lang, $post, false);
                $permalink = qtrans_convertURL($permalink, $aj->lang, true);
            }
            //End qT
            setup_postdata($post);
            $excerpt_length = apply_filters('excerpt_length', 55);
            $excerpt = (string) get_the_excerpt();
            $content = (string) get_the_content();
            $title = get_the_title();
            knews_debug('- including post: ' . $title . "\r\n");
            if ($knewsOptions['apply_filters_on'] == '1') {
                $content = apply_filters('the_content', $content);
            }
            $content = strip_shortcodes($content);
            $content = knews_iterative_extract_code('<script', '</script>', $content, true);
            $content = knews_iterative_extract_code('<fb:like', '</fb:like>', $content, true);
            $content = str_replace(']]>', ']]>', $content);
            $content = strip_tags($content);
            if ($excerpt == '') {
                $excerpt = $content;
            }
            $words = explode(' ', $content, $excerpt_length + 1);
            if (count($words) > $excerpt_length) {
                array_pop($words);
                //array_push($words, '[...]');
                $excerpt = implode(' ', $words) . '...';
            }
            $content = nl2br($content);
            $words = explode(' ', $excerpt, $excerpt_length + 1);
            if (count($words) > $excerpt_length) {
                array_pop($words);
                //array_push($words, '[...]');
                $excerpt = implode(' ', $words) . '...';
            }
            /*$content = $pp->post_content;
            
            			$excerpt = strip_shortcodes( $content );
            			if ($knewsOptions['apply_filters_on']=='1') $excerpt = apply_filters('the_content', $excerpt);
            			$excerpt = knews_iterative_extract_code('<script', '</script>', $excerpt, true);
            			$excerpt = knews_iterative_extract_code('<fb:like', '</fb:like>', $excerpt, true);
            			$excerpt = str_replace(']]>', ']]>', $excerpt);
            			$excerpt = strip_tags($excerpt);
            			$excerpt_length = apply_filters('excerpt_length', 55);
            			$words = explode(' ', $excerpt, $excerpt_length + 1);
            			if (count($words) > $excerpt_length) {
            				array_pop($words);
            				//array_push($words, '[...]');
            				$excerpt = implode(' ', $words);
            			}
            			//$excerpt = nl2br($excerpt);
            			if ($knewsOptions['apply_filters_on']=='1') $content = apply_filters('the_content', $content);
            			*/
            //$title = $pp->post_title;
            //$permalink = get_permalink($pp->ID);
            $s = 0;
            while ($news_mod_map[$s] == 0 && $s < count($news_mod_map)) {
                $s++;
            }
            $n = 1;
            $found = false;
            while (!$found && $n < 20) {
                if (strpos($news_mod2[$s], '%the_title_' . $n . '%') !== false || strpos($news_mod2[$s], '%the_excerpt_' . $n . '%') !== false || strpos($news_mod2[$s], '%the_permalink_' . $n . '%') !== false) {
                    $found = true;
                } else {
                    $n++;
                }
            }
            if ($found) {
                $news_mod_map[$s]--;
                $news_mod2[$s] = str_replace('%the_permalink_' . $n . '%', $permalink, $news_mod2[$s]);
                $news_mod2[$s] = str_replace('%the_title_' . $n . '%', $title, $news_mod2[$s]);
                $news_mod2[$s] = str_replace('%the_excerpt_' . $n . '%', $excerpt, $news_mod2[$s]);
                $news_mod2[$s] = str_replace('%the_content_' . $n . '%', $content, $news_mod2[$s]);
                $subject = str_replace('%the_title_1%', $title, $subject);
                //pro
                $look_img = explode('<img', $news_mod2[$s]);
                $news_mod2[$s] = '';
                foreach ($look_img as $look_one_img) {
                    $short_look = explode('>', $look_one_img);
                    if (strpos($short_look[0], 'the_feat_img_' . $n) !== false) {
                        //Lets to replace the image
                        $short_look[0] = str_replace(' ', '', $short_look[0]);
                        $short_look[0] = str_replace("'", '"', $short_look[0]);
                        $short_look[0] = strtolower($short_look[0]);
                        $width = knews_cut_code('width="', '"', $short_look[0], true);
                        $height = knews_cut_code('height="', '"', $short_look[0], true);
                        if ($image = knews_automate_img($pp->ID, $width, $height)) {
                            knews_debug('- image resized: ' . $image . "\r\n");
                            $old_src = knews_cut_code('src="', '"', $short_look[0], true);
                            $look_one_img = str_ireplace($old_src, $image, $look_one_img);
                        } else {
                            knews_debug('- image resize failed (or not featured image found) into post: ' . $title . "\r\n");
                        }
                    }
                    if ($news_mod2[$s] != '') {
                        $news_mod2[$s] .= '<img';
                    }
                    $news_mod2[$s] .= $look_one_img;
                }
                //fi pro
                knews_debug('- included: ' . $title . "\r\n");
                if ($most_recent == 0) {
                    $most_recent = $Knews_plugin->sql2time($pp->post_modified);
                }
                if ($most_recent < $Knews_plugin->sql2time($pp->post_modified)) {
                    $most_recent = $Knews_plugin->sql2time($pp->post_modified);
                }
                $query = "INSERT INTO " . KNEWS_AUTOMATED_POSTS . " (id_automated, id_post, when_scheduled, id_news) VALUES (" . $aj->id . ", " . $pp->ID . ", '" . $Knews_plugin->get_mysql_date() . "', " . "0)";
                $result = $wpdb->query($query);
            }
        }
        if ($most_recent != 0) {
            $news_mod = '';
            foreach ($news_mod2 as $nm) {
                $news_mod .= $nm;
            }
            $news_mod = str_replace('<span class="chooser"></span>', '', $news_mod);
            //Netejem exces
            for ($n = 1; $n < 10; $n++) {
                //$news_mod = str_replace('%the_permalink_' . $n . '%', '#', $news_mod);
                $news_mod = str_replace('%the_title_' . $n . '%', '', $news_mod);
                $news_mod = str_replace('%the_excerpt_' . $n . '%', '', $news_mod);
                $news_mod = str_replace('%the_content_' . $n . '%', '', $news_mod);
                $news_mod = knews_iterative_deleteTag('a', '%the_permalink_' . $n . '%', $news_mod);
                $news_mod = knews_iterative_deleteTag('img', 'the_feat_img_' . $n . '%', $news_mod);
            }
            knews_debug('- saving the created newsletter' . "\r\n");
            $sql = "INSERT INTO " . KNEWS_NEWSLETTERS . "(name, subject, created, modified, template, html_mailing, html_head, html_modules, html_container, lang, automated, mobile, id_mobile, newstype) VALUES ('" . esc_sql($news[0]->name) . " (" . date('d/m/Y') . ")', '" . esc_sql($subject) . "', '" . $Knews_plugin->get_mysql_date() . "', '" . $Knews_plugin->get_mysql_date() . "','" . $news[0]->template . "','" . esc_sql($news_mod) . "','" . esc_sql($news[0]->html_head) . "','" . esc_sql($news[0]->html_modules) . "','" . esc_sql($news[0]->html_container) . "', '" . $news[0]->lang . "', 1, " . ($mobile ? '1' : '0') . ", " . $mobile_news_id . ", 'automated')";
            $results = $wpdb->query($sql);
            $id_newsletter = $Knews_plugin->real_insert_id();
            $query = "UPDATE " . KNEWS_AUTOMATED_POSTS . " SET id_news=" . $id_newsletter . " WHERE id_news=0";
            $results = $wpdb->query($query);
            if ($aj->every_mode == 1) {
                $last_run = "'" . $Knews_plugin->get_mysql_date($most_recent) . "'";
            } else {
                $last_run = "'" . $Knews_plugin->get_mysql_date() . "'";
            }
            $query = "UPDATE " . KNEWS_AUTOMATED . " SET last_run=" . $last_run . ", run_yet=1 WHERE id=" . $aj->id . " ";
            $results = $wpdb->query($query);
            if ($mobile) {
                return $id_newsletter;
            }
            knews_debug('- scheduling the submit' . "\r\n");
            $query = "SELECT DISTINCT(" . KNEWS_USERS . ".id) FROM " . KNEWS_USERS . ", " . KNEWS_USERS_PER_LISTS . " WHERE " . KNEWS_USERS . ".id=" . KNEWS_USERS_PER_LISTS . ".id_user AND " . KNEWS_USERS . ".state='2' AND " . KNEWS_USERS_PER_LISTS . ".id_list=" . $aj->target_id;
            $batch_opts = array('minute' => date("i"), 'hour' => date("H"), 'day' => date("d"), 'month' => date("m"), 'year' => date("Y"), 'paused' => $aj->auto == 1 ? 0 : 1, 'priority' => 4, 'strict_control' => '', 'emails_at_once' => $aj->emails_at_once, 'id_smtp' => $aj->id_smtp);
            require KNEWS_DIR . "/includes/submit_batch.php";
            knews_debug('- all done' . "\r\n");
        }
    } else {
        knews_debug('- the newsletter kept more posts of wich there to submit, wait for more' . "\r\n");
    }
}
<?php

if ($Knews_plugin) {
    if (!$Knews_plugin->initialized) {
        $Knews_plugin->init();
    }
    require_once KNEWS_DIR . '/includes/knews_util.php';
    global $wpdb;
    $query = "SELECT * FROM " . KNEWS_NEWSLETTERS . " WHERE id=" . $id_newsletter;
    $results = $wpdb->get_results($query);
    $theSubject = $results[0]->subject;
    $theHtml = $results[0]->html_head . '<body>' . $results[0]->html_mailing . '</body></html>';
    $title = knews_cut_code('<title>', '</title>', $theHtml, false);
    $theHtml = str_replace($title, '<title>' . $theSubject . ' - ' . get_bloginfo('title') . '</title>', $theHtml);
    //Remove some shit from WYSIWYG editor
    $theHtml = str_replace($results[0]->html_container, '', $theHtml);
    $theHtml = str_replace('<span class="handler"></span>', '', $theHtml);
    $theHtml = str_replace('<!--[start module]-->', '', $theHtml);
    $theHtml = str_replace('<!--[end module]-->', '', $theHtml);
    $theHtml = knews_iterative_extract_code('<!--[start option', ']-->', $theHtml, true);
    $theHtml = knews_iterative_extract_code('<!--[end option', ']-->', $theHtml, true);
    $theHtml = str_replace("\r\n\r\n", "\r\n", $theHtml);
    $theHtml = preg_replace('/(?:(?:\\r\\n|\\r|\\n)\\s*){2}/s', "\n\n", $theHtml);
    $cut_lines = explode("\n", $theHtml);
    $theHtml = '';
    foreach ($cut_lines as $cl) {
        if (strlen($cl) > 250) {
            //echo 'tallarem: ' . htmlentities($cl) . '<br>';
            while (strlen($cl) > 250) {
                $seek = strpos($cl, ' ', 200);
                if ($seek !== false) {