'); // Global variables $adinj_total_top_ads_used = 0; $adinj_total_random_ads_used = 0; $adinj_total_bottom_ads_used = 0; $adinj_data = array(); require_once(ADINJ_PATH . '/adshow.php'); if (is_admin()){ require_once(ADINJ_PATH . '/ad-injection-admin.php'); } function adinj_admin_menu_hook(){ $options_page = add_options_page('Ad Injection', 'Ad Injection', 'manage_options', basename(__FILE__), 'adinj_options_page'); add_action("admin_print_scripts-".$options_page, "adinj_admin_print_scripts_main"); add_action("admin_print_scripts-widgets.php", "adinj_admin_print_scripts_widgets"); } function adinj_options_link_hook($links, $file) { static $this_plugin; if (!$this_plugin) $this_plugin = plugin_basename(__FILE__); if ($file == $this_plugin){ $link = "" . __("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]; } 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') && !adinj_ticked('block_keywords')) return; // Put the search engine detection / cookie setting script in the footer // TODO would be better to use plugin version, but that only seems accessible in admin $version = adinj_db_version(adinj_options()); wp_enqueue_script('adinj_sev', WP_PLUGIN_URL.'/ad-injection/adinj-sev.js?v='.$version, NULL, NULL, false); } // 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; $sevisitors = adinj_ticked('sevisitors_only'); $block = adinj_ticked('block_keywords'); if (!$sevisitors && !$block) return; echo <<\n"; } 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_get_ad_code($adtype, $ads_db){ $ops = adinj_options(); $ads_live = NULL; $ads_split = NULL; $alt_live = NULL; $alt_split = NULL; $formatting = NULL; if (adinj_mfunc_mode()){ adinj_live_ads_array($adtype, $ads_db, $ads_live, $ads_split, 'string'); adinj_live_ads_array($adtype.'_alt', $ads_db, $alt_live, $alt_split, 'string'); $formatting = adinj_formatting_options($adtype, $ads_db, 'string'); } else { $ads_live = array(); $ads_split = array(); $alt_live = array(); $alt_split = array(); adinj_live_ads_array($adtype, $ads_db, $ads_live, $ads_split, 'array'); adinj_live_ads_array($adtype.'_alt', $ads_db, $alt_live, $alt_split, 'array'); $formatting = adinj_formatting_options($adtype, $ads_db, 'array'); } if (empty($ads_live) && empty($alt_live)){ return ""; } if (adinj_mfunc_mode()){ return adinj_ad_code_eval("\n "); } // else dynamic ad if (adshow_show_adverts() !== true){ $adname = adshow_pick_value($alt_live, $alt_split); } else { $adname = adshow_pick_value($ads_live, $ads_split); } $ad = $ads_db[$adname]; if (empty($ad)){ return ""; } $ad = adshow_add_formatting($ad, $formatting); return "".adinj_ad_code_eval($ad); } function adinj_ad_code_random(){ return adinj_get_ad_code('random', adinj_options()); } function adinj_ad_code_top(){ return adinj_get_ad_code('top', adinj_options()); } function adinj_ad_code_bottom(){ return adinj_get_ad_code('bottom', adinj_options()); } function adinj_ad_code_footer(){ return adinj_get_ad_code('footer', adinj_options()); } /** Old: ad_code_random_1 <-> ad_random_1.txt New: ad_code_random_1:ad_code_random_1_split <-> ad_random_1.txt ad_code_random_alt_1:ad_code_random_alt_1_split <-> ad_random-alt_1.txt */ function adinj_live_ads_array($type, $ads_option, &$ads, &$split, $output_type="string"){ $op_stem = ""; $file_stem = ""; if ($type == 'random' || $type == 'top' || $type == 'bottom' || $type == 'footer' || $type == 'random_alt' || $type == 'top_alt' || $type == 'bottom_alt' || $type == 'footer_alt'){ $op_stem = 'ad_code_'.$type.'_'; $file_stem = 'ad_'.$type.'_'; } else if (preg_match("/widget_[\d+]/i", $type)){ if (strpos($type, '_alt') === false){ $op_stem = 'advert_'; } else { $op_stem = 'advert_alt_'; } $file_stem = 'ad_'.$type.'_'; } // DB with support for ad rotation for ($i=1; $i<=10; ++$i){ $ad_name = $op_stem.$i; if (!array_key_exists($ad_name.'_split', $ads_option)) return; $split_val = $ads_option[$ad_name.'_split']; if (!empty($ads_option[$ad_name]) && is_numeric($split_val) && $split_val > 0){ if ($output_type == "string"){ if (!empty($ads)) $ads .= ","; $ads .= "'".$file_stem."$i.txt'"; if (!empty($split)) $split .= ","; $split .= $split_val; } else { $ads[] = $ad_name; $split[] = $split_val; } } } } function adinj_formatting_options($adtype, $ops, $output_type="string"){ $align = ""; $clear = ""; $margin_top = ""; $margin_bottom = ""; $padding_top = ""; $padding_bottom = ""; $prefix = ''; if ($adtype == 'random') $prefix = 'rnd_'; if ($adtype == 'top') $prefix = 'top_'; if ($adtype == 'bottom') $prefix = 'bottom_'; if ($adtype == 'footer') $prefix = 'footer_'; //widgets have no prefix $align = isset($ops[$prefix.'align']) ? $ops[$prefix.'align'] : ""; $clear = isset($ops[$prefix.'clear']) ? $ops[$prefix.'clear'] : ""; $margin_top = $ops[$prefix.'margin_top']; $margin_bottom = $ops[$prefix.'margin_bottom']; $padding_top = $ops[$prefix.'padding_top']; $padding_bottom = $ops[$prefix.'padding_bottom']; if (adinj_not_set($align)) $align = ""; if (adinj_not_set($clear)) $clear = ""; if (adinj_not_set($margin_top)) $margin_top = ""; if (adinj_not_set($margin_bottom)) $margin_bottom = ""; if (adinj_not_set($padding_top)) $padding_top = ""; if (adinj_not_set($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 ""; } 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($adtype){ $ops = adinj_options(); if ($adtype == 'widget'){ $days = $ops['widgets_on_page_older_than']; } else { $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_top_ads_used, $adinj_total_random_ads_used, $adinj_total_bottom_ads_used; $ops = adinj_options(); $rnd_start_at = $ops['random_ads_start_at']; $rnd_start_mode = $ops['random_ads_start_mode']; if ($rnd_start_mode == 'after') $rnd_start_mode = 'at or after'; $rnd_start_unit = $ops['random_ads_start_unit']; $rnd_end_at = $ops['random_ads_end_at']; $rnd_end_mode = $ops['random_ads_end_mode']; $rnd_end_unit = $ops['random_ads_end_unit']; $rnd_end_unit_dir = $ops['random_ads_end_direction']; $mode = $ops['ad_insertion_mode']; $posttype = get_post_type() . ' ('; if (is_archive()) $posttype .= ' archive'; if (is_front_page()) $posttype .= ' front'; if (is_home()) $posttype .= ' home'; if (is_page()) $posttype .= ' page'; if (is_search()) $posttype .= ' search'; if (is_single()) $posttype .= ' single'; if (is_404()) $posttype .= ' 404'; $posttype .= ')'; if(is_single() || is_page()) { $currentdate = time(); $postdate = get_the_time('U'); $currentday = $currentdate / 24 / 60 / 60; $postday = $postdate / 24 / 60 / 60; } else { $currentdate = ""; $postdate = ""; $currentday = ""; $postday = ""; } return $content." \n"; } // todo check exclude options for hardcoded ads function adinj_excluded_by_tick_box($prefix){ if (is_front_page() && adinj_ticked($prefix.'exclude_front') || is_home() && adinj_ticked($prefix.'exclude_home') || is_search() && adinj_ticked($prefix.'exclude_search') || is_404() && adinj_ticked($prefix.'exclude_404') || is_page() && adinj_ticked($prefix.'exclude_page') || is_single() && adinj_ticked($prefix.'exclude_single') || is_archive() && adinj_ticked($prefix.'exclude_archive')){ return true; } return false; } function adinj_ads_completely_disabled_from_page($adtype, $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_front_page() && adinj_ticked('exclude_front')){ return "NOADS: excluded from front page"; } if (is_home() && adinj_ticked('exclude_home')){ return "NOADS: excluded from home page"; } if (is_search() && adinj_ticked('exclude_search')){ return "NOADS: excluded from search pages"; } if (is_404() && adinj_ticked('exclude_404')){ return "NOADS: excluded from 404 pages"; } if ((is_page() && adinj_ticked('exclude_page')) || (is_single() && adinj_ticked('exclude_single')) || (is_archive() && adinj_ticked('exclude_archive'))){ return "NOADS: excluded from this page 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($adtype)) return "NOADS: !is_old_post"; if (!adinj_allowed_in_category('global', $ops)) return "NOADS: globally blocked from category"; if (!adinj_allowed_in_tag('global', $ops)) return "NOADS: globally blocked from tag"; if (!adinj_allowed_in_author('global', $ops)) return "NOADS: globally blocked from author"; if (!adinj_allowed_in_id('global', $ops)) return "NOADS: globally blocked from post/page id"; // manual ad disabling tags if ($content == NULL) return false; if (stripos($content, "") !== false) return "NOADS: noadsense tag"; // 'Adsense Injection' tag if (stripos($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_list($all_entries, $config_entries, $mode, $func){ if (is_array($all_entries)){ foreach($all_entries as $entry){ $string = $func($entry); $decoded = rawurldecode($string); //allow UTF-8 encoded strings if (in_array($string, $config_entries) || in_array($decoded, $config_entries)){ if (adinj_mode_only_show_in($mode)){ return true; } else if (adinj_mode_never_show_in($mode)){ return false; } } } } if (adinj_mode_only_show_in($mode)){ return false; } else if (adinj_mode_never_show_in($mode)){ return true; } echo (""); return true; } function adinj_mode_only_show_in($mode){ return ($mode == ADINJ_ONLY_SHOW_IN || $mode == 'o'); } function adinj_mode_never_show_in($mode){ return ($mode == ADINJ_NEVER_SHOW_IN || $mode == 'n'); } function adinj_allowed_in_category($scope, $ops){ $conditions = adinj_split_comma_list($ops[$scope.'_category_condition_entries']); if (empty($conditions)) return true; $mode = $ops[$scope.'_category_condition_mode']; // widget ads and footer ad if (!in_the_loop() && adinj_mode_only_show_in($mode) && !(is_single() || is_category())){ return false; } if (in_the_loop() && adinj_mode_only_show_in($mode) && !(is_single() || is_home() || is_category())){ return false; } $categories = array(); global $post; if (in_the_loop() && (is_single() || is_home())){ $categories = get_the_category($post->ID); } else if (!in_the_loop() && is_single()){ $cat_ids = wp_get_object_terms($post->ID, 'category', 'fields=all'); foreach($cat_ids as $id){ $categories[] = get_category($id); } } else if (is_category()){ $categories[] = get_category(get_query_var('cat')); } // else cat array is empty return adinj_allowed_in_list($categories, $conditions, $mode, 'adinj_category_nicename'); } function adinj_allowed_in_tag($scope, $ops){ $conditions = adinj_split_comma_list($ops[$scope.'_tag_condition_entries']); if (empty($conditions)) return true; $mode = $ops[$scope.'_tag_condition_mode']; if (!in_the_loop() && adinj_mode_only_show_in($mode) && !(is_single() || is_tag())){ return false; } if (in_the_loop() && adinj_mode_only_show_in($mode) && !(is_single() || is_home() || is_tag())){ return false; } $tags = array(); global $post; if (in_the_loop() && (is_single() || is_home())){ $tags = get_the_tags($post->ID); } else if (!in_the_loop() && is_single()){ $tag_ids = wp_get_object_terms($post->ID, 'post_tag', 'fields=all'); foreach($tag_ids as $id){ $tags[] = get_tag($id); } } else if (is_tag()){ $tags[] = get_tag(get_query_var('tag_id')); } // else tag array is empty return adinj_allowed_in_list($tags, $conditions, $mode, 'adinj_tag_slug'); } function adinj_allowed_in_author($scope, $ops){ $conditions = adinj_split_comma_list($ops[$scope.'_author_condition_entries']); if (empty($conditions)) return true; $mode = $ops[$scope.'_author_condition_mode']; if (!in_the_loop() && adinj_mode_only_show_in($mode) && !(is_single() || is_page() || is_author())){ return false; } if (in_the_loop()&& adinj_mode_only_show_in($mode) && !(is_single() || is_page() || is_home() || is_author())){ return false; } $user = array(); if (is_single() || is_page() || is_home()){ $data = get_the_author_meta('user_login'); // works in and out of the loop $user[] = $data; //need to make it into array } else if (is_author()){ $curauth = get_userdata(get_query_var('author')); $user[] = $curauth->user_login; } // else author array is empty return adinj_allowed_in_list($user, $conditions, $mode, 'adinj_author_data'); } function adinj_allowed_in_id($scope, $ops){ $condition_entries = isset($ops[$scope.'_id_condition_entries']) ? $ops[$scope.'_id_condition_entries'] : ""; $conditions = adinj_split_comma_list($condition_entries); if (empty($conditions)) return true; $postid = -1; if (in_the_loop()){ global $post; $postid = $post->ID; } else { global $wp_query; $postid = $wp_query->post->ID; } $mode = $ops[$scope.'_id_condition_mode']; return adinj_allowed_in_list(array($postid), $conditions, $mode, 'adinj_post_id'); } //function parameters function adinj_category_nicename($category){ return $category->category_nicename; } function adinj_tag_slug($tag){ return $tag->slug; } function adinj_author_data($data){ return $data; } function adinj_post_id($data){ return $data; } function adinj_split_comma_list($list){ $list = preg_split("/[,]+/", $list, -1, PREG_SPLIT_NO_EMPTY); array_walk($list, 'adinj_trim_value'); return $list; } function adinj_trim_value(&$value){ $value = trim($value); } function adinj_footer_hook(){ if (is_feed()) return; // TODO feed specific ads if (adinj_num_footer_ads_to_insert() <= 0) return; echo adinj_ad_code_footer(); } function adinj_debug_on(){ return adinj_ticked('debug_mode'); } 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_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, "") !== false) return adinj($ad_include.$topad.$content.$bottomad, "Ads=sandwich" . $debug); if(stripos($content, "") !== 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, '
', $prevpos);
$next_pre_open = adinj_stripos($content, '', $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, '', $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, '', $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, '', $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, '