View source
<?php
define('ADSENSE_INJECTOR_INSERT_BODY_AD_DEFAULT', TRUE);
define('ADSENSE_INJECTOR_BODY_INSERTION_TEMPLATE_DEFAULT', '<div class="ad-auto-inserted" style="float:left; margin: 0 1em .25em 0;">[adsense:120x240:1:1]</div>%body<br class="clear"/>[adsense:468x60:1:1]');
define('ADSENSE_INJECTOR_BODY_MINWORDS_DEFAULT', 75);
define('ADSENSE_INJECTOR_APPEND_IN_LISTVIEW_DEFAULT', FALSE);
define('ADSENSE_INJECTOR_LISTVIEW_INSERTION_TEMPLATE_DEFAULT', '%teaser<div class="adsense-injector-list-ad">[adsense:468x60:1:1]</div>');
define('ADSENSE_INJECTOR_INSERT_AD_NODETYPE', 'adsense_injector_nodetype_');
function _adsense_injector_count_words($str, $max) {
return count(explode(' ', $str, $max));
}
function _adsense_injector_minwords_cfg($nodetype, $defval = 75) {
return variable_get('adsense_injector_body_minwords', $defval);
}
function adsense_injector_nodeapi(&$node, $op, $teaser, $page) {
if ($op == 'alter') {
if (module_exists('adsense') && _adsense_page_match() && variable_get(ADSENSE_INJECTOR_INSERT_AD_NODETYPE . $node->type, FALSE)) {
if ($page) {
if (variable_get('adsense_injector_insert_body_ad', ADSENSE_INJECTOR_INSERT_BODY_AD_DEFAULT)) {
$body = $node->body;
$minwords = _adsense_injector_minwords_cfg($node->type);
$wordcount = _adsense_injector_count_words($body, $minwords);
if ($wordcount >= $minwords) {
$template = variable_get('adsense_injector_body_template', ADSENSE_INJECTOR_BODY_INSERTION_TEMPLATE_DEFAULT);
if (function_exists('_adsense_process_tags')) {
$template = _adsense_process_tags($template);
}
else {
watchdog('adsense_injector', 'adsense module function _adsense_process_tags() not found', WATCHDOG_ERROR);
}
$node->body = strtr($template, array(
'%body' => $body,
));
}
else {
$node->body = "<!-- adsense_injector: node body word count ({$wordcount}) is insufficient ({$minwords} required), so we won't insert an ad. -->" . $body;
}
}
}
elseif ($teaser && variable_get('adsense_injector_append_in_listview', ADSENSE_INJECTOR_APPEND_IN_LISTVIEW_DEFAULT)) {
$template = variable_get('adsense_injector_listview_insertion_template', ADSENSE_INJECTOR_LISTVIEW_INSERTION_TEMPLATE_DEFAULT);
if (function_exists('_adsense_process_tags')) {
$template = _adsense_process_tags($template);
}
else {
watchdog('adsense_injector', 'adsense module function _adsense_process_tags() not found', WATCHDOG_ERROR);
}
$node->body = strtr($template, array(
'%teaser' => $node->teaser,
));
}
}
}
}
function adsense_injector_menu() {
$items['admin/settings/adsense_injector'] = array(
'title' => 'AdSense Injector',
'description' => 'Insert Google AdSense ads into full node views automatically.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'adsense_injector_admin_settings',
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'adsense_injector.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}