function widget($args, $instance)
 {
     if (adinj_ads_completely_disabled_from_page('widget')) {
         return;
     }
     if (is_front_page() && adinj_ticked('widget_exclude_front') || is_home() && adinj_ticked('widget_exclude_home') || is_page() && adinj_ticked('widget_exclude_page') || is_single() && adinj_ticked('widget_exclude_single') || is_archive() && adinj_ticked('widget_exclude_archive') || is_search() && adinj_ticked('widget_exclude_search') || is_404() && adinj_ticked('widget_exclude_404')) {
         return;
     }
     if (is_front_page() && adinj_ticked('exclude_front', $instance) || is_home() && adinj_ticked('exclude_home', $instance) || is_page() && adinj_ticked('exclude_page', $instance) || is_single() && adinj_ticked('exclude_single', $instance) || is_archive() && adinj_ticked('exclude_archive', $instance) || is_search() && adinj_ticked('exclude_search', $instance) || is_404() && adinj_ticked('exclude_404', $instance)) {
         return;
     }
     if (!adinj_allowed_in_category('widget', $instance)) {
         return;
     }
     if (!adinj_allowed_in_tag('widget', $instance)) {
         return;
     }
     if (!adinj_allowed_in_author('widget', $instance)) {
         return;
     }
     if (!adinj_allowed_in_id('widget', $instance)) {
         return;
     }
     extract($args);
     $ops = adinj_options();
     $include = "";
     if (adinj_mfunc_mode()) {
         $include = adinj_ad_code_include();
     }
     $title = apply_filters('widget_title', $instance['title']);
     // The old 'non upgraded' db will be passed here if the widget hasn't
     // been resaved. We can't upgrade as doing so would mean we'd have to
     // re-save the widget files - which we can't do as we can't re-write
     // the settings back to the db (at least not without a bit more work)
     $adcode = adinj_get_ad_code('widget_' . $this->get_id(), $instance);
     echo $before_widget;
     if ($title) {
         echo $before_title . $title . $after_title;
     }
     if (!empty($adcode)) {
         echo $include;
         echo $adcode;
     }
     echo $after_widget;
 }
function adinj_install_db()
{
    $new_options = adinj_default_options();
    // Dynamic defaults.
    if (!is_supported_caching_plugin_active()) {
        $new_options['ad_insertion_mode'] = 'direct';
    }
    adinj_update_options($new_options);
    if (adinj_mfunc_mode()) {
        // Create config file.
        adinj_write_config_file();
    }
}
Example #3
0
function adinj_insertion_mode_box($ops)
{
    adinj_postbox_start(__("Ad insertion mode and dynamic ad display restrictions", 'adinj'), 'restrictions');
    ?>
	<h4>Ad insertion mode</h4>
	<blockquote>
	<input type="radio" name="ad_insertion_mode" value="direct" <?php 
    if (adinj_direct_mode()) {
        echo 'checked="checked"';
    }
    ?>
 /> <b>direct: Direct ad code insertion <font color="green">(recommended)</font></b> - Adds are directly inserted into the page. Note that dynamic features (the ones in this section) will not work if you are using a caching plugin as dynamic features require that the page is dynamically generated each time a user views it.<br />
	
	<input type="radio" name="ad_insertion_mode" value="mfunc" <?php 
    if (adinj_mfunc_mode()) {
        echo 'checked="checked"';
    }
    ?>
 /> <b>mfunc: Insert ads using cache compatible mfunc tags <font color="red">(not recommended)</font></b> - unless you are 100% certain you understand mfunc mode and know what you are doing!<br />
	
	<?php 
    if (!adinj_mfunc_mode()) {
        ?>
	<script type="text/javascript">
	document.write('<style type="text/css" media="screen">#caching_plugin_msg { display: none; }</style>');
	</script>
	<?php 
    }
    ?>
	<br />
	
	<div id="caching_plugin_msg" class="caching_plugin_msg">
	Dynamic features may work with older versions of WP Super Cache, W3 Total Cache and WP Cache. Only select this mode if you are using an older version of one of those caching plugins that you know supports mfunc and want to use dynamic features (IP / referrer restriction, alt content and ad roatation). If you aren't using dynamic features or don't understand what mfunc is then select direct mode. This mode is left in for people who know exactly what mfunc mode is and understand the risks of using it. If you have to ask then use direct mode.
	<?php 
    if (!is_supported_caching_plugin_active()) {
        echo '<p><b><span style="font-size:10px;color:red;">Note: A supported caching plugin does not appear to be active. If you are not using WP Super Cache / W3 Total Cache / WP Cache you should use one of the direct insertion modes below.</span></b></p>';
    }
    ?>
	<?php 
    if (is_plugin_active('wp-super-cache/wp-cache.php')) {
        adinj_wp_super_cache_msg();
    } else {
        if (is_plugin_active('w3-total-cache/w3-total-cache.php')) {
            adinj_w3_total_cache_msg();
        } else {
            if (is_plugin_active('wp-cache/wp-cache.php')) {
                adinj_wp_cache_msg();
            }
        }
    }
    adinj_unknown_cache_msg();
    ?>
	</div>
	
	<br />
	
	
	</blockquote>

	<p></p>
	
	<script type="text/javascript">
	jQuery(document).ready(function(){
	jQuery('input[name=ad_insertion_mode]:radio').change(function() {
		if (jQuery('input[name=ad_insertion_mode]:checked').val() == "direct"){
			jQuery('.caching_plugin_msg').slideUp(300);
		} else { // mfunc
			jQuery('.caching_plugin_msg').slideDown(300);
		}
		return true;
		});
	});
	</script>
	
	
	<h4><a name="dynamic"></a>Show ads only to visitors from these search engines or websites (dynamic feature)</h4>
	
	<blockquote>
	<?php 
    adinj_add_checkbox('sevisitors_only');
    _e("Only show ads to visitors from these websites (customise search engine or website referrers below). When a user visits from one of these referrers they will see ads <b>for the next hour</b> (sets a 1 hour cookie).", 'adinj');
    ?>
<br />
	<textarea name="ad_referrers" rows="2" cols="<?php 
    adinj_table_width('dynamic');
    ?>
"><?php 
    echo $ops['ad_referrers'];
    ?>
</textarea>
	<p>Comma separated list e.g.: <br /><code>.google., .bing., .yahoo., .ask., search?, search., facebook.com, t.co</code></p>
	</blockquote>

	
	<h4>Blocked referrer keywords (dynamic feature)</h4>
	
	<blockquote>
	<?php 
    adinj_add_checkbox('block_keywords');
    _e("Don't show ads to visitors from external sites if the referrer contains one of these keywords or substrings.", 'adinj');
    ?>
 If a blocked string is found block ads for: <?php 
    adinj_selection_box("block_ads_for_hours", array('1' => '1 hour', '2' => '2 hours', '3' => '3 hours', '6' => '6 hours', '24' => '1 day', '48' => '2 days', '72' => '3 days', '168' => '1 week', '720' => '30 days', '8760' => '1 year'));
    ?>
<br />
	<textarea name="blocked_keywords" rows="2" cols="<?php 
    adinj_table_width('dynamic');
    ?>
"><?php 
    echo $ops['blocked_keywords'];
    ?>
</textarea>
	<p>Comma separated list e.g.: <br /><code>facebook.com, .org, james+bond</code></p>
	</blockquote>

	
	<h4>Blocked IP addresses (dynamic feature)</h4>
	
	<blockquote>
	<?php 
    adinj_add_checkbox('block_ips');
    _e("Exclude ads from these IP addresses.", 'adinj');
    ?>
<br />
	<textarea name="blocked_ips" rows="4" cols="<?php 
    adinj_table_width('dynamic');
    ?>
"><?php 
    echo $ops['blocked_ips'];
    ?>
</textarea>
	<p>Comma separated list e.g.: <br /><code>0.0.0.1, 0.0.0.2</code></p>
	<p>Or you can list one IP per line with optional comments e.g.</p>
	<code style="padding:0px 0px">192.168.0.1<br />0.0.0.2<br /><?php 
    echo $_SERVER['REMOTE_ADDR'];
    ?>
 //my ip<br />0.0.0.3</code>
	
	<p>For reference your current IP address is <code><?php 
    echo $_SERVER['REMOTE_ADDR'];
    ?>
</code></p>
	</blockquote>

	
	
	
	<?php 
    adinj_postbox_end();
}
function adinj_content_hook($content)
{
    if (is_feed()) {
        return $content;
    }
    // TODO feed specific ads
    if (!in_the_loop()) {
        return $content;
    }
    // Don't insert ads into meta description tags
    $ops = adinj_options();
    if (empty($ops)) {
        return $content;
    }
    $debug_on = adinj_debug_on();
    $debug = "";
    if ($debug_on) {
        echo "<!--adinj Ad Injection debug mode on-->";
    }
    adinj_upgrade_db_if_necessary();
    global $adinj_total_random_ads_used, $adinj_total_top_ads_used, $adinj_total_bottom_ads_used;
    if (!is_archive() && (is_page() || is_single())) {
        // On single page the_content may be called more than once - e.g. for
        // description meta tag and for content.
        $adinj_total_top_ads_used = 0;
        $adinj_total_random_ads_used = 0;
        $adinj_total_bottom_ads_used = 0;
    }
    $reason = adinj_ads_completely_disabled_from_page('in-content', $content);
    if ($reason !== false) {
        return adinj($content, $reason);
    }
    if ($debug_on && adinj_direct_mode()) {
        $showads = adshow_show_adverts();
        if ($showads !== true) {
            $debug .= "\nNOADS: ad blocked at run time reason={$showads}";
        }
    }
    $topad = adinj_ad_code_top();
    if (empty($topad)) {
        if ($debug_on) {
            $debug .= "\nNo top ad defined in any of the ad code boxes";
        }
    }
    $randomad = adinj_ad_code_random();
    if (empty($randomad)) {
        if ($debug_on) {
            $debug .= "\nNo random ad defined in any of the ad code boxes";
        }
    }
    $bottomad = adinj_ad_code_bottom();
    if (empty($bottomad)) {
        if ($debug_on) {
            $debug .= "\nNo bottom ad defined in any of the ad code boxes";
        }
    }
    if (empty($topad) && empty($randomad) && empty($bottomad)) {
        return adinj($content, "None of top, random or bottom ads are defined.");
    }
    $ad_include = "";
    if (adinj_mfunc_mode()) {
        $ad_include = adinj_ad_code_include();
    }
    # Ad sandwich mode
    if (is_page() || is_single()) {
        if (stripos($content, "<!--adsandwich-->") !== false) {
            return adinj($ad_include . $topad . $content . $bottomad, "Ads=sandwich" . $debug);
        }
        if (stripos($content, "<!--adfooter-->") !== false) {
            return adinj($content . $ad_include . $bottomad, "Ads=footer" . $debug);
        }
    }
    # Get content length for ad placement settings
    $rawlength = strlen($content);
    $length = 0;
    if ($ops['content_length_unit'] == 'all') {
        $length = $rawlength;
    } else {
        if ($ops['content_length_unit'] == 'viewable') {
            $length = strlen(strip_tags($content));
        } else {
            $length = str_word_count_utf8(strip_tags($content));
        }
    }
    # Record original paragraph positions
    $original_paragraph_positions = array();
    $prevpos = -1;
    while (($prevpos = stripos($content, ADINJ_PARA, $prevpos + 1)) !== false) {
        $valid = true;
        if (adinj_ticked('exclude_ads_from_block_tags')) {
            $next_blockquote_open = adinj_stripos($content, '<blockquote', $prevpos);
            $next_blockquote_close = adinj_stripos($content, '</blockquote>', $prevpos);
            $next_pre_open = adinj_stripos($content, '<pre', $prevpos);
            $next_pre_close = adinj_stripos($content, '</pre>', $prevpos);
            $valid = ($next_blockquote_open == $next_blockquote_close || $next_blockquote_open > $prevpos && $next_blockquote_open <= $next_blockquote_close) && ($next_pre_open == $next_pre_close || $next_pre_open > $prevpos && $next_pre_open <= $next_pre_close);
            if (!$valid) {
                continue;
            }
        }
        if (adinj_ticked('exclude_ads_from_div_tags')) {
            $next_open = adinj_stripos($content, '<div', $prevpos);
            $next_close = adinj_stripos($content, '</div>', $prevpos);
            $valid = $next_open == $next_close || $next_open > $prevpos && $next_open <= $next_close;
            if (!$valid) {
                continue;
            }
        }
        if (adinj_ticked('exclude_ads_from_list_tags')) {
            $next_open = adinj_stripos($content, '<ol', $prevpos);
            $next_close = adinj_stripos($content, '</ol>', $prevpos);
            $valid = $next_open == $next_close || $next_open > $prevpos && $next_open <= $next_close;
            if (!$valid) {
                continue;
            }
        }
        if (adinj_ticked('exclude_ads_from_list_tags')) {
            $next_open = adinj_stripos($content, '<ul', $prevpos);
            $next_close = adinj_stripos($content, '</ul>', $prevpos);
            $valid = $next_open == $next_close || $next_open > $prevpos && $next_open <= $next_close;
            if (!$valid) {
                continue;
            }
        }
        if (adinj_ticked('exclude_ads_from_form_tags')) {
            $next_open = adinj_stripos($content, '<form', $prevpos);
            $next_close = adinj_stripos($content, '</form>', $prevpos);
            $valid = $next_open == $next_close || $next_open > $prevpos && $next_open <= $next_close;
            if (!$valid) {
                continue;
            }
        }
        if (adinj_ticked('exclude_ads_from_table_tags')) {
            $next_open = adinj_stripos($content, '<table', $prevpos);
            $next_close = adinj_stripos($content, '</table>', $prevpos);
            $valid = $next_open == $next_close || $next_open > $prevpos && $next_open <= $next_close;
            if (!$valid) {
                continue;
            }
        }
        if ($valid) {
            $next_exclude_open = adinj_stripos($content, '<!--adinj_exclude_start-->', $prevpos);
            $next_exclude_close = adinj_stripos($content, '<!--adinj_exclude_end-->', $prevpos);
            if ($next_exclude_open == $next_exclude_close || $next_exclude_open > $prevpos && $next_exclude_open <= $next_exclude_close) {
                $original_paragraph_positions[] = $prevpos + strlen(ADINJ_PARA);
            }
        }
    }
    $paracount = count($original_paragraph_positions);
    if ($debug_on) {
        $debug .= "\nContent length={$length} (" . $ops['content_length_unit'] . ") Raw character length={$rawlength} Paragraph count={$paracount}";
    }
    if ($paracount == 0) {
        if ($debug_on) {
            $debug .= "\nWarning: No paragraph (&lt;/p&gt;) tags found.\n\n\tYour theme or one of your plugins may have changed the priority of the wpautop\n\n\tfilter so it is getting run later than expected (after Ad Injection has run).\n\n\tYou can try modifying the priority setting of Ad Injection's wp_content filter\n\n\tfrom the settings screen.\n\n\tTry 100, or if that fails 200!";
        }
    }
    # Positions to insert ads
    $top_ad_paragraph = -1;
    $random_ad_paragraphs = array();
    $bottom_ad_paragraph = -1;
    $fixed_top = stripos($content, "<!--topad-->");
    $fixed_random = stripos($content, "<!--randomad-->");
    $fixed_bottom = stripos($content, "<!--bottomad-->");
    # Find top ad position
    if ($fixed_top === false) {
        if (adinj_num_top_ads_to_insert($length, $debug) > 0) {
            $top_ad_paragraph = adinj_get_paragraph('top', $content, $original_paragraph_positions, $debug);
        }
    }
    if ($debug_on) {
        $debug .= "\nTop ad paragraph: {$top_ad_paragraph}";
    }
    # Find bottom ad position
    if ($fixed_bottom === false) {
        if (adinj_num_bottom_ads_to_insert($length, $debug) > 0) {
            $bottom_ad_paragraph = adinj_get_paragraph('bottom', $content, $original_paragraph_positions, $debug);
        }
        if ($bottom_ad_paragraph !== -1) {
            $bottom_ad_paragraph = $paracount - $bottom_ad_paragraph;
        }
    }
    if ($debug_on) {
        $debug .= "\nBottom ad paragraph: {$bottom_ad_paragraph}";
    }
    # Find random ad positions
    if ($fixed_random === false) {
        $random_ad_paragraphs = adinj_get_random_paragraphs($content, $length, $original_paragraph_positions, $debug);
        $random_ads_to_insert_count = sizeof($random_ad_paragraphs);
        if ($random_ads_to_insert_count == 0) {
            if ($debug_on) {
                $debug .= "\nWarning: No random ad injection positions";
            }
        }
    }
    # Insert the adverts into the content. Scan through the paragraph list in reverse order.
    $adpos = count($random_ad_paragraphs);
    $bottomadsetting = $ops['bottom_ad_position'];
    for ($i = $paracount; $i > 0; --$i) {
        if ($i === $bottom_ad_paragraph && $bottomadsetting != 0) {
            $content = substr_replace($content, $bottomad, $original_paragraph_positions[$i - 1], 0);
            ++$adinj_total_bottom_ads_used;
        }
        for ($j = $adpos - 1; $j >= 0; --$j) {
            $rnd = $random_ad_paragraphs[$j];
            if ($i == $rnd) {
                if (adinj_ticked('rnd_reselect_ad_per_position_in_post')) {
                    $randomad = adinj_ad_code_random();
                }
                $content = substr_replace($content, $randomad, $original_paragraph_positions[$rnd - 1], 0);
                ++$adinj_total_random_ads_used;
                --$adpos;
            } else {
                break;
            }
        }
        if ($i === $top_ad_paragraph) {
            $content = substr_replace($content, $topad, $original_paragraph_positions[$i - 1], 0);
            ++$adinj_total_top_ads_used;
        }
    }
    if ($top_ad_paragraph === 0) {
        // default is special case
        $content = $topad . $content;
        ++$adinj_total_top_ads_used;
    }
    if ($bottom_ad_paragraph !== -1 && $bottomadsetting == 0) {
        // default is special case as themes can't be trusted to close the final paragraph
        $content = $content . $bottomad;
        ++$adinj_total_bottom_ads_used;
    }
    if ($fixed_top) {
        adinj_insert_fixed_ad($content, $topad, 'top', $adinj_total_top_ads_used, $debug);
    }
    if ($fixed_random) {
        adinj_insert_fixed_ad($content, $randomad, 'random', $adinj_total_random_ads_used, $debug);
    }
    if ($fixed_bottom) {
        adinj_insert_fixed_ad($content, $bottomad, 'bottom', $adinj_total_bottom_ads_used, $debug);
    }
    $content = $ad_include . $content;
    return adinj($content, "Ad Injection in-content injections complete!" . $debug);
}