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 widget($args, $instance)
 {
     if (adinj_ads_completely_disabled_from_page()) {
         return;
     }
     $options = adinj_options();
     if (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')) {
         return;
     }
     if ($options['ad_insertion_mode'] == 'direct_dynamic') {
         if (adinj_show_adverts() !== true) {
             return;
         }
     }
     extract($args);
     $title = apply_filters('widget_title', $instance['title']);
     $advert = $instance['advert'];
     echo $before_widget;
     if ($title) {
         echo $before_title . $title . $after_title;
     }
     $adcode = "";
     $include = "";
     if ($options['ad_insertion_mode'] == 'mfunc') {
         $include = adinj_ad_code_include();
         $adcode = adinj_get_mfunc_code($this->get_ad_file_name());
     } else {
         $adcode = $advert;
     }
     $adcode = adinj_ad_code_eval($adcode);
     if ($advert) {
         echo $include;
         echo $adcode;
     }
     echo $after_widget;
 }
Esempio n. 3
0
function adinj_inject_hook($content)
{
    if (is_feed()) {
        return $content;
    }
    $reason = adinj_ads_completely_disabled_from_page($content);
    if ($reason !== false) {
        return adinj($content, $reason);
    }
    $ops = adinj_options();
    if ($ops['ad_insertion_mode'] == 'direct_dynamic') {
        $showads = adinj_show_adverts();
        if ($showads !== true) {
            return adinj($content, "NOADS: ad blocked at run time reason={$showads}");
        }
    }
    $ad_include = adinj_ad_code_include();
    # Ad sandwich mode
    if (is_page() || is_single()) {
        if (stripos($content, "<!--adsandwich-->") !== false) {
            return adinj($ad_include . adinj_ad_code_top() . $content . adinj_ad_code_bottom(), "Ads=adsandwich");
        }
        if (stripos($content, "<!--adfooter-->") !== false) {
            return adinj($content . $ad_include . adinj_ad_code_bottom(), "Ads=adfooter");
        }
    }
    # Insert top and bottom ads if necesary
    $length = strlen($content);
    if (is_page() || is_single()) {
        if (adinj_do_rule_if($ops['top_ad_if_longer_than'], '<', $length)) {
            $content = $ad_include . adinj_ad_code_top() . $content;
            $ad_include = false;
        }
        if (adinj_do_rule_if($ops['bottom_ad_if_longer_than'], '<', $length)) {
            $content = $content . adinj_ad_code_bottom();
        }
    }
    $num_rand_ads_to_insert = adinj_num_rand_ads_to_insert($length);
    if ($num_rand_ads_to_insert <= 0) {
        return adinj($content, "all ads used up");
    }
    $ad = adinj_ad_code_random();
    if (empty($ad)) {
        return adinj($content, "no random ad defined");
    }
    if ($ad_include !== false) {
        $content = $ad_include . $content;
    }
    $debug_on = $ops['debug_mode'];
    if (!$debug_on) {
        $debugtags = false;
    }
    $content_adfree_header = "";
    $content_adfree_footer = "";
    // TODO add docs explaining the significance of leaving blank lines
    // before or after these tags
    # 'Adsense Injection' tag compatibility
    $split = adinj_split_by_tag($content, "<!--adsensestart-->", $debugtags);
    if (count($split) == 2) {
        $content_adfree_header = $split[0];
        $content = $split[1];
    }
    # Use the same naming convention for the end tag
    $split = adinj_split_by_tag($content, "<!--adsenseend-->", $debugtags);
    if (count($split) == 2) {
        $content = $split[0];
        $content_adfree_footer = $split[1];
    }
    $split = adinj_split_by_tag($content, "<!--adstart-->", $debugtags);
    if (count($split) == 2) {
        $content_adfree_header = $split[0];
        $content = $split[1];
    }
    $split = adinj_split_by_tag($content, "<!--adend-->", $debugtags);
    if (count($split) == 2) {
        $content = $split[0];
        $content_adfree_footer = $split[1];
    }
    // TODO add note explaining that start tags are processed before the 'first
    // paragraph ad
    // Move onto random ad insertions
    $paragraphmarker = "</p>";
    if (stripos($content, $paragraphmarker) === false) {
        return adinj($content, "no &lt;/p&gt; tags");
    }
    if ($debug_on) {
        $debug = "\nTags=" . htmlentities($debugtags);
    }
    // Generate a list of all potential injection points
    if ($debug_on) {
        $debug .= "\nPotential positions: ";
    }
    $potential_inj_positions = array();
    $prevpos = -1;
    while (($prevpos = stripos($content, $paragraphmarker, $prevpos + 1)) !== false) {
        $potential_inj_positions[] = $prevpos + strlen($paragraphmarker);
        if ($debug_on) {
            $debug .= $prevpos . ", ";
        }
    }
    if ($debug_on) {
        $debug .= "\npotential_inj_positions:" . sizeof($potential_inj_positions);
    }
    if (sizeof($potential_inj_positions) == 0) {
        return adinj($content, "Error: no potential inj positions");
    }
    $inj_positions = array();
    if (adinj_ticked('first_paragraph_ad')) {
        $inj_positions[] = array_shift($potential_inj_positions);
        --$num_rand_ads_to_insert;
    }
    // Pick the correct number of random injection points
    if (sizeof($potential_inj_positions) > 0 && $num_rand_ads_to_insert > 0) {
        if (!adinj_ticked('multiple_ads_at_same_position')) {
            // Each ad is inserted into a unique position
            if (sizeof($potential_inj_positions) < $num_rand_ads_to_insert) {
                $debug .= "\nnum_rand_ads_to_insert requested={$num_rand_ads_to_insert}. But restricted to " . sizeof($potential_inj_positions) . " due to limited injection points.";
                $num_rand_ads_to_insert = sizeof($potential_inj_positions);
            }
            $rand_positions = array_rand(array_flip($potential_inj_positions), $num_rand_ads_to_insert);
            if ($num_rand_ads_to_insert == 1) {
                // Convert it back into an array
                $inj_positions[] = $rand_positions;
            } else {
                $inj_positions = array_merge($inj_positions, $rand_positions);
            }
            foreach ($inj_positions as $pos) {
                if ($debug_on) {
                    $debug = $pos . ", " . $debug;
                }
            }
        } else {
            // Multiple ads may be inserted at the same position
            $injections = 0;
            while ($injections++ < $num_rand_ads_to_insert) {
                $rnd = array_rand($potential_inj_positions);
                if ($debug_on) {
                    $debug = $potential_inj_positions[$rnd] . ", " . $debug;
                }
                $inj_positions[] = $potential_inj_positions[$rnd];
            }
        }
    }
    if (sizeof($inj_positions) == 0) {
        return adinj($content_adfree_header . $content . $content_adfree_footer, "Error: No ad injection positions: " . $debug);
    }
    // Sort positions
    sort($inj_positions);
    // Insert ads in reverse order
    global $adinj_total_rand_ads_used, $adinj_total_all_ads_used;
    for ($adnum = sizeof($inj_positions) - 1; $adnum >= 0; $adnum--) {
        $content = substr_replace($content, $ad, $inj_positions[$adnum], 0);
        ++$adinj_total_rand_ads_used;
        ++$adinj_total_all_ads_used;
    }
    return adinj($content_adfree_header . $content . $content_adfree_footer, "Ads injected: " . $debug);
}
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);
}