| 1 |
<?php |
<?php |
| 2 |
// $Id: adsense_injector.module,v 1.1.2.6.2.3 2008/12/16 09:28:12 hswong3i Exp $ |
// $Id: adsense_injector.module,v 1.1.2.6.2.3.2.5 2008/12/17 09:56:09 hswong3i Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 6 |
* Inject adsense ads into node content automatically. |
* Inject adsense ads into node content automatically. |
| 7 |
*/ |
*/ |
| 8 |
|
|
|
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>'); |
|
|
|
|
|
/** |
|
|
* Prefix for variable table entries - append node type name, store as boolean |
|
|
* value, nonzero means insert ad content. |
|
|
*/ |
|
|
define('ADSENSE_INJECTOR_INSERT_AD_NODETYPE', 'adsense_injector_nodetype_'); |
|
|
|
|
|
/** |
|
|
* Count words in a string. |
|
|
* |
|
|
* @param $str |
|
|
* Target string. |
|
|
* @param $max |
|
|
* Maximum number of words we care about. Return value will never exceed |
|
|
* this. |
|
|
* @return |
|
|
* The count of words, where delimiter is one or more spaces. |
|
|
* @todo Efficiency, find better way to do this. |
|
|
*/ |
|
|
function _adsense_injector_count_words($str, $max) { |
|
|
// lifted from node.module node_validate() function. |
|
|
return count(explode(' ', $str, $max)); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Get the minimum node body wordcount for insertion. |
|
|
* |
|
|
* May be content-type specific, but at present, it's global to all node |
|
|
* types. |
|
|
* |
|
|
* @param $nodetype |
|
|
* The node type. |
|
|
* @param $defval |
|
|
* The default value. |
|
|
* @return |
|
|
* The minimum insertion wordcount. |
|
|
*/ |
|
|
function _adsense_injector_minwords_cfg($nodetype, $defval = 75) { |
|
|
return variable_get('adsense_injector_body_minwords', $defval); |
|
|
} |
|
|
|
|
| 9 |
/** |
/** |
| 10 |
* Implementation of hook_nodeapi(). |
* Implementation of hook_nodeapi(). |
| 11 |
* |
* |
| 15 |
* @todo: Evaluate efficiency of string concat vs. sprintf, other methods. |
* @todo: Evaluate efficiency of string concat vs. sprintf, other methods. |
| 16 |
*/ |
*/ |
| 17 |
function adsense_injector_nodeapi(&$node, $op, $teaser, $page) { |
function adsense_injector_nodeapi(&$node, $op, $teaser, $page) { |
| 18 |
|
// We only consider content types which are enabled for inline adsense. |
| 19 |
|
$node_types = variable_get('adsense_injector_nodes', array()); |
| 20 |
|
if (empty($node_types[$node->type])) { |
| 21 |
|
return; |
| 22 |
|
} |
| 23 |
|
|
| 24 |
// insert an ad into the body. |
// insert an ad into the body. |
| 25 |
if ($op == 'alter') { |
if ($op == 'alter') { |
| 26 |
if (module_exists('adsense') && _adsense_page_match() && variable_get(ADSENSE_INJECTOR_INSERT_AD_NODETYPE . $node->type, FALSE)) { |
if ($page && variable_get('adsense_injector_body_view', TRUE)) { |
| 27 |
if ($page) { |
// Get the minimum node body wordcount for insertion. |
| 28 |
if (variable_get('adsense_injector_insert_body_ad', ADSENSE_INJECTOR_INSERT_BODY_AD_DEFAULT)) { |
$minwords = variable_get('adsense_injector_body_view_minwords', 75); |
| 29 |
$body = $node->body; |
// Count words in a string. |
| 30 |
$minwords = _adsense_injector_minwords_cfg($node->type); |
// lifted from node.module node_validate() function. |
| 31 |
$wordcount = _adsense_injector_count_words($body, $minwords); |
$wordcount = count(explode(' ', $node->body, $minwords)); |
| 32 |
if ($wordcount >= $minwords) { |
if ($wordcount >= $minwords) { |
|
$template = variable_get('adsense_injector_body_template', ADSENSE_INJECTOR_BODY_INSERTION_TEMPLATE_DEFAULT); |
|
|
// Process adsense module tags in the template text, if enabled and possible. |
|
|
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); |
|
| 33 |
// Process adsense module tags in the template text, if enabled and possible. |
// Process adsense module tags in the template text, if enabled and possible. |
| 34 |
if (function_exists('_adsense_process_tags')) { |
$template = _adsense_process_tags(variable_get('adsense_injector_body_view_template', '<div style="float: right; margin: 0; padding: 0 1em .25em 0;">[adsense:250x250:0123456789]</div>%body<br class="clear"/>[adsense:728x90:0123456789]')); |
| 35 |
$template = _adsense_process_tags($template); |
$node->body = strtr($template, array('%body' => $node->body)); |
|
} |
|
|
else { |
|
|
watchdog('adsense_injector', 'adsense module function _adsense_process_tags() not found', WATCHDOG_ERROR); |
|
|
} |
|
|
$node->body = strtr($template, array('%teaser' => $node->teaser)); |
|
| 36 |
} |
} |
| 37 |
|
else { |
| 38 |
|
$node->body .= "<!-- adsense_injector: node body word count ($wordcount) is insufficient ($minwords required), so we won't insert an ad. -->"; |
| 39 |
|
} |
| 40 |
|
} |
| 41 |
|
elseif ($teaser && variable_get('adsense_injector_list_view', FALSE)) { |
| 42 |
|
// Process adsense module tags in the template text, if enabled and possible. |
| 43 |
|
$template = _adsense_process_tags(variable_get('adsense_injector_list_view_template', '%teaser<br class="clear"/>[adsense:728x90:0123456789]')); |
| 44 |
|
$node->teaser = strtr($template, array('%teaser' => $node->teaser)); |
| 45 |
} |
} |
| 46 |
} |
} |
| 47 |
} |
} |