" . __("Settings") . ""; array_unshift($links, $link); } return $links; } function adinj_options($reset=false){ global $adinj_data; if (empty($adinj_data) || $reset !== false){ $adinj_data = get_option('adinj_options'); } return $adinj_data; } function adinj_option($option){ $ops = adinj_options(); return $ops[$option]; } // TODO make the cookie domain from wp-config.php accessible to script //$cookie_domain = COOKIE_DOMAIN; // TODO test //var adinj_cookie_domain = "$cookie_domain"; //JS line. TODO test function adinj_print_referrers_hook(){ // TODO can re-enable this check once the widget ads are factored in. //if (adinj_ads_completely_disabled_from_page()) return; if (adinj_ticked('sevisitors_only')){ $referrer_list = adinj_quote_list('ad_referrers'); echo << SCRIPT; } } function adinj_quote_list($option){ $ops = adinj_options(); $list = $ops[$option]; // I'm sure this whole thing could be done with a much simpler single // line of PHP - but right now my brain isn't up to thinking about it! $lines = explode("\n", $list); foreach ($lines as $line){ $stripped_lines[] = preg_replace("/\/\/.*/", "", $line); } $list = implode(" ", $stripped_lines); $list = preg_replace("/'/", "", $list); $referrers = preg_split("/[\s,]+/", $list, -1, PREG_SPLIT_NO_EMPTY); if (empty($referrers)) return ''; foreach ($referrers as $referrer){ $newlist[] = "'" . $referrer . "'"; } return implode(", ", $newlist); } function adinj_addsevjs_hook(){ // TODO can re-enable this check once the widget ads are factored in. //if (adinj_ads_completely_disabled_from_page()) return; if (!adinj_ticked('sevisitors_only')) return; // Put the search engine detection / cookie setting script in the footer wp_enqueue_script('adinj_sev', WP_PLUGIN_URL.'/ad-injection/adinj-sev.js', NULL, NULL, true); } function adinj_get_mfunc_code($adfile, $options = array()){ $options = adinj_formatting_options($adfile, "string", $options); return "\n "; } // adname could either be the name of the adfile or the database row function adinj_formatting_options($adname, $output_type="string", $options = array()){ $ops = adinj_options(); $align = ""; $clear = ""; $margin_top = ""; $margin_bottom = ""; $padding_top = ""; $padding_bottom = ""; if (preg_match("/random_[\d+]/i", $adname)){ $align = $ops['rnd_align']; $clear = $ops['rnd_clear']; $margin_top = $ops['rnd_margin_top']; $margin_bottom = $ops['rnd_margin_bottom']; } else if (preg_match("/top_[\d+]/i", $adname)){ $align = $ops['top_align']; $clear = $ops['top_clear']; $margin_top = $ops['top_margin_top']; $margin_bottom = $ops['top_margin_bottom']; } else if (preg_match("/bottom_[\d+]/i", $adname)){ $align = $ops['bottom_align']; $clear = $ops['bottom_clear']; $margin_top = $ops['bottom_margin_top']; $margin_bottom = $ops['bottom_margin_bottom']; } else if (preg_match("/widget_[\d+]/i", $adname)){ $align = $options['align']; $clear = $options['clear']; $margin_top = $options['margin_top']; $margin_bottom = $options['margin_bottom']; $padding_top = $options['padding_top']; $padding_bottom = $options['padding_bottom']; } if (adinj_disabled($align)) $align = ""; if (adinj_disabled($clear)) $clear = ""; if (adinj_disabled($margin_top)) $margin_top = ""; if (adinj_disabled($margin_bottom)) $margin_bottom = ""; if (adinj_disabled($padding_top)) $padding_top = ""; if (adinj_disabled($padding_bottom)) $padding_bottom = ""; if ($output_type == "string"){ return "'align' => '$align', 'clear' => '$clear', 'margin_top' => '$margin_top', 'margin_bottom' => '$margin_bottom', 'padding_top' => '$padding_top', 'padding_bottom' => '$padding_bottom'"; } else { return array('align' => $align, 'clear' => $clear, 'margin_top' => $margin_top, 'margin_bottom' => $margin_bottom, 'padding_top' => $padding_top, 'padding_bottom' => $padding_bottom); } } function adinj_ad_code_eval($ad){ if (strlen($ad) == 0) return $ad; if (stripos($ad, ' "; } return adinj_ad_code_eval($ad); } function adinj_add_tags($ad, $prefix, $func=NULL){ $ops = adinj_options(); if (!adinj_disabled($ops[$prefix . 'align']) || !adinj_disabled($ops[$prefix . 'clear']) || !adinj_disabled($ops[$prefix . 'margin_top']) || !adinj_disabled($ops[$prefix . 'margin_bottom'])) { $clear = ""; $top = ""; $bottom = ""; if (!adinj_disabled($ops[$prefix . 'clear'])) $clear="clear:" . $ops[$prefix . 'clear'] . ";"; if (!adinj_disabled($ops[$prefix . 'margin_top'])) $top="margin-top:" . $ops[$prefix . 'margin_top'] . "px;"; if (!adinj_disabled($ops[$prefix . 'margin_bottom'])) $bottom="margin-bottom:" . $ops[$prefix . 'margin_bottom'] . "px;"; $cssrules = $clear . $top . $bottom; if ($ops[$prefix . 'align'] == 'left'){ $div = "
ADCODE

"; } else if ($ops[$prefix . 'align'] == 'center'){ $div = "
ADCODE
"; } else if ($ops[$prefix . 'align'] == 'right'){ $div = "
ADCODE

"; } else if ($ops[$prefix . 'align'] == 'float left'){ $div = "
ADCODE
"; } else if ($ops[$prefix . 'align'] == 'float right'){ $div = "
ADCODE
"; } else { $div = "
ADCODE
"; } if (empty($func)){ return str_replace("ADCODE", $ad, $div); } else { $ad = str_replace("ADCODE", "\$ad", $div); return "function $func(\$ad) { return \"$ad\"; }"; } } if (!empty($func)){ return "function $func(\$ad){return \$ad;}"; } return $ad; } function adinj_ad_code_random(){ $ops = adinj_options(); $ad = $ops['ad_code_random_1']; if (empty($ad)) return false; if ($ops['ad_insertion_mode'] == 'mfunc'){ $ad = adinj_get_mfunc_code(ADINJ_AD_RANDOM_FILE); } else { $ad = adshow_add_formatting($ad, adinj_formatting_options('ad_code_random_1', "array")); } return adinj_ad_code_eval($ad); } function adinj_ad_code_top(){ $ops = adinj_options(); $ad = $ops['ad_code_top_1']; if (empty($ad)) return ""; if ($ops['ad_insertion_mode'] == 'mfunc'){ $ad = adinj_get_mfunc_code(ADINJ_AD_TOP_FILE); } else { $ad = adshow_add_formatting($ad, adinj_formatting_options('ad_code_top_1', "array")); } global $adinj_total_all_ads_used; ++$adinj_total_all_ads_used; return adinj_ad_code_eval($ad); } function adinj_ad_code_bottom(){ $ops = adinj_options(); $ad = $ops['ad_code_bottom_1']; if (empty($ad)) return ""; if ($ops['ad_insertion_mode'] == 'mfunc'){ $ad = adinj_get_mfunc_code(ADINJ_AD_BOTTOM_FILE); } else { $ad = adshow_add_formatting($ad, adinj_formatting_options('ad_code_bottom_1', "array")); } global $adinj_total_all_ads_used; ++$adinj_total_all_ads_used; return adinj_ad_code_eval($ad); } function read_ad_from_file($ad_path){ $contents = ""; if (file_exists($ad_path)){ $contents = file_get_contents($ad_path); if ($contents === false) return "Error: can't read from file: $ad_path"; } return $contents; } // Based on: http://www.wprecipes.com/wordpress-hack-how-to-display-ads-on-old-posts-only // Only use for pages and posts. Not for archives, categories, home page, etc. function adinj_is_old_post(){ $ops = adinj_options(); $days = $ops['ads_on_page_older_than']; if ($days == 0) return true; if(is_single() || is_page()) { $current_date = time(); $offset = $days * 60*60*24; $post_date = get_the_time('U'); if(($post_date + $offset) > $current_date){ return false; } else { return true; } } return false; } function adinj_adverts_disabled_flag(){ $custom_fields = get_post_custom(); if (isset($custom_fields['disable_adverts'])){ $disable_adverts = $custom_fields['disable_adverts']; return ($disable_adverts[0] == 1); } return 0; } function adinj($content, $message){ if (!adinj_ticked('debug_mode')) return $content; global $adinj_total_rand_ads_used, $adinj_total_all_ads_used; $path = ADINJ_AD_PATH; $ops = adinj_options(); $mode = $ops['ad_insertion_mode']; return $content." \n"; } function adinj_ads_completely_disabled_from_page($content=NULL){ $ops = adinj_options(); if ($ops['ads_enabled'] == 'off' || $ops['ads_enabled'] == ''){ return "NOADS: Ads are not switched on"; } if ($ops['ads_enabled'] == 'test' && !current_user_can('activate_plugins')){ return "NOADS: test mode enabled - ads only shown admins"; } // check for ads on certain page types being disabled if ((is_home() && adinj_ticked('exclude_home')) || (is_page() && adinj_ticked('exclude_page')) || (is_single() && adinj_ticked('exclude_single')) || (is_archive() && adinj_ticked('exclude_archive')) || is_search() || is_404()){ return "NOADS: excluded from this post type-".get_post_type(); } // if disable_adverts==1 if (adinj_adverts_disabled_flag()) return "NOADS: adverts_disabled_flag"; // no ads on old posts/pages if rule is enabled if((is_page() || is_single()) && !adinj_is_old_post()) return "NOADS: !is_old_post"; $category_ok = adinj_allowed_in_category('global'); if (!$category_ok) return "NOADS: blocked from category"; $tag_ok = adinj_allowed_in_tag('global'); if (!$tag_ok) return "NOADS: blocked from tag"; // manual ad disabling tags if ($content == NULL) return false; if (strpos($content, "") !== false) return "NOADS: noadsense tag"; // 'Adsense Injection' tag if (strpos($content, "") !== false) return "NOADS: no-adsense tag"; // 'Whydowork Adsense' tag if (stripos($content,'') !== false) return "NOADS: NoAds tag"; // 'Quick Adsense' tag if (stripos($content,'') !== false) return "NOADS: OffAds tag"; // 'Quick Adsense' tag return false; } function adinj_allowed_in_category($scope){ $ops = adinj_options(); $cat_list = $ops[$scope.'_category_condition_entries']; $cat_array = preg_split("/[\s,]+/", $cat_list, -1, PREG_SPLIT_NO_EMPTY); if (empty($cat_array)) return true; $cat_mode = $ops[$scope.'_category_condition_mode']; global $post; foreach(get_the_category($post->ID) as $allcats) { $postcat = $allcats->category_nicename; if (in_array($postcat, $cat_array)){ if ($cat_mode == ADINJ_ONLY_SHOW_IN){ return true; } else if ($cat_mode == ADINJ_NEVER_SHOW_IN){ return false; } } } if ($cat_mode == ADINJ_ONLY_SHOW_IN){ return false; } else if ($cat_mode == ADINJ_NEVER_SHOW_IN){ return true; } echo (""); return true; } function adinj_allowed_in_tag($scope){ $ops = adinj_options(); $tag_list = $ops[$scope.'_tag_condition_entries']; $tag_array = preg_split("/[\s,]+/", $tag_list, -1, PREG_SPLIT_NO_EMPTY); if (empty($tag_array)) return true; $tag_mode = $ops[$scope.'_tag_condition_mode']; global $post; foreach(get_the_tags($post->ID) as $alltags) { $posttag = $alltags->slug; if (in_array($posttag, $tag_array)){ if ($tag_mode == ADINJ_ONLY_SHOW_IN){ return true; } else if ($tag_mode == ADINJ_NEVER_SHOW_IN){ return false; } } } if ($tag_mode == ADINJ_ONLY_SHOW_IN){ return false; } else if ($tag_mode == ADINJ_NEVER_SHOW_IN){ return true; } echo (""); return true; } function adinj_inject_hook($content){ global $adinj_total_rand_ads_used; if(is_page() || is_single()){ // TODO hack for no random ads bug $adinj_total_rand_ads_used = 0; } 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 = adshow_show_adverts(); if ($showads !== true){ return adinj($content, "NOADS: ad blocked at run time reason=$showads"); // TODO alt content } } $ad_include = adinj_ad_code_include(); # Ad sandwich mode if(is_page() || is_single()){ if(stripos($content, "") !== false) return adinj($ad_include.adinj_ad_code_top().$content.adinj_ad_code_bottom(), "Ads=adsandwich"); if(stripos($content, "") !== 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, "", $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, "", $debugtags); if (count($split) == 2){ $content = $split[0]; $content_adfree_footer = $split[1]; } $split = adinj_split_by_tag($content, "", $debugtags); if (count($split) == 2){ $content_adfree_header = $split[0]; $content = $split[1]; } $split = adinj_split_by_tag($content, "", $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 = "

"; if(stripos($content, $paragraphmarker) === false) return adinj($content, "no </p> 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_split_by_tag($content, $tag, &$debugtags){ $ret = array(); if (strpos($content, $tag) !== false){ if ($debugtags !== false) $debugtags .= "$tag, "; $content_split = explode($tag, $content, 2); $ret[] = $content_split[0]; if (count($content_split) == 2){ $ret[] = $content_split[1]; } } return $ret; } function adinj_num_rand_ads_to_insert($content_length){ global $adinj_total_rand_ads_used; // a page can be more than one post $ops = adinj_options(); if (is_single() || is_page()){ $max_num_rand_ads_to_insert = $ops['max_num_of_ads'] - $adinj_total_rand_ads_used; } else if (is_home()){ $max_num_rand_ads_to_insert = $ops['max_num_of_ads_home_page'] - $adinj_total_rand_ads_used; } else { return 0; //TODO Allow ads in other page types later } if ($max_num_rand_ads_to_insert <= 0) { return 0; } if(!is_single() && !is_page()) { // If there are multiple posts on page only show one ad per post // This rule from 'Adsense Injection'. return 1; } $length = $content_length; if (adinj_do_rule_if($ops['no_random_ads_if_shorter_than'], '>', $length)){ return 0; } if (adinj_do_rule_if($ops['one_ad_if_shorter_than'], '>', $length)){ return 1; } if (adinj_do_rule_if($ops['two_ads_if_shorter_than'], '>', $length)){ return min(2, $max_num_rand_ads_to_insert); } if (adinj_do_rule_if($ops['three_ads_if_shorter_than'], '>', $length)){ return min(3, $max_num_rand_ads_to_insert); } return $max_num_rand_ads_to_insert; } function adinj_do_rule_if($rule_value, $condition, $content_length){ if ($rule_value == ADINJ_ALWAYS_SHOW) return true; if (adinj_disabled($rule_value)) return false; if ($condition == '>'){ return ($rule_value > $content_length); } else if ($condition == '<'){ return ($rule_value < $content_length); } else { die("adinj_do_rule_if bad condition: $condition"); } } function adinj_disabled($value){ return $value === ADINJ_RULE_DISABLED || $value === ADINJ_DISABLED || $value === ''; } function adinj_ticked($option){ $ops = adinj_options(); if (!empty($ops[$option])) return 'checked="checked"'; return false; } // Widget support require_once('ad-injection-widget.php'); add_action('widgets_init', 'adinj_widgets_init'); function adinj_widgets_init() { register_widget('Ad_Injection_Widget'); } // activate register_activation_hook(__FILE__, 'adinj_activate_hook'); // Content injection add_action('wp_enqueue_scripts', 'adinj_addsevjs_hook'); add_filter('the_content', 'adinj_inject_hook'); add_action('wp_footer', 'adinj_print_referrers_hook'); ?>