'; } } function adsoptimal_loop_end(&$wp_query) { if (get_option('adsoptimal_enable_desktop_ad', 'false') == 'false') return; if (get_option('adsoptimal_footer_ad_type', '') == '') return; if (is_single() && get_option('adsoptimal_enable_post_ad', 'true') == 'false') return; if (!is_single() && get_option('adsoptimal_enable_page_ad', 'true') == 'false') return; global $wp_the_query; if (($wp_query === $wp_the_query) && !is_admin() && !is_feed() && !is_robots() && !is_trackback()) { $size = split('x', get_option('adsoptimal_footer_ad_type', '')); echo '
'; } } class SharedParams { public $countWord = 0; public $foundDot = false; public $insertCount = 0; public $targetAt = 100; function __construct() { $this->targetAt = intval(get_option('adsoptimal_content_ad_every', '100')); } } function addContentAd(DOMNode $domNode, $sharedParams) { foreach ($domNode->childNodes as $node) { if ($sharedParams->insertCount >= 2) return; if ($node->nodeType == XML_TEXT_NODE) { $splitedText = split(' ', $node->nodeValue); for ($i=0; $icountWord += 1; if ($sharedParams->countWord >= $sharedParams->targetAt) { if ($sharedParams->foundDot) { $splitedText[$i] = '[ADSOPTIMAL_AD_TAG]'.$splitedText[$i]; $sharedParams->insertCount += 1; $sharedParams->countWord = 0; $sharedParams->foundDot = false; } elseif (strpos($splitedText[$i], '.') !== false) { $sharedParams->foundDot = true; } } } $node->nodeValue = join(' ', $splitedText); } if($node->hasChildNodes()) { addContentAd($node, $sharedParams); } } } function adsoptimal_content($content = '') { if (get_option('adsoptimal_enable_desktop_ad', 'false') == 'false') return $content; if (get_option('adsoptimal_content_ad_type', '') == '') return $content; if (is_single() && get_option('adsoptimal_enable_post_ad', 'true') == 'false') return $content; if (!is_single() && get_option('adsoptimal_enable_page_ad', 'true') == 'false') return $content; if (!is_single()) return $content; if (!is_admin() && !is_feed() && !is_robots() && !is_trackback()) { $size = split('x', get_option('adsoptimal_content_ad_type', '')); $adHtml = '
'; $sharedParams = new SharedParams(); $dom = new DOMDocument(); $libxml_previous_state = libxml_use_internal_errors(true); $dom->loadHTML('
'.$content.'
'); libxml_clear_errors(); libxml_use_internal_errors($libxml_previous_state); addContentAd($dom, $sharedParams); $html = $dom->saveHTML(); $html = str_replace('[ADSOPTIMAL_AD_TAG]', $adHtml, $html); return $html; } else return $content; } ?>