Inject adsense into content

Bevan - May 3, 2007 - 16:11
Project:Adsense Injector
Version:5.x-2.5-1
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:needs work
Description

This module doesn't do what the name suggests, and what I understood from the description; that is inject adsense INTO the content, e.g. after paragraph x. So I rewrote it.

Attached patch file provides two placeholders for use in adsense_injector settings 'Node body ad insertion template': %content1 and %content2. Run the patch file then set this in the settings: %content1[adsense:300x250:1:6]%content2 -- or similar.

The patch puts all the content up to the end of the third paragraph in %content1, and the remainder in %content2. I also cleaned up some duplicate code in the module and tidied up a bit in general.

It uses and requires DOM so needs php5 (domXML might work in php 4 but isn't tested yet).

This patch could be further developed to allow for multiple adsense injections, e.g. %content1[adsense]%content2[adsense]%content3. Or for a different Html element to be counted instead of p.

This patch needs testing. I don't use it on teasers or teaser lists at all, so I haven't tested that yet.

A new settings is now required to define how many paragraphs should go in %content1.

A known issue is that if the content has entities (e.g. ©) then loading the body into DOM fails. Several php warning messages will be left in watchdog and/or on screen (depending on your error handling settings in drupal). Additionally this patch will leave it's own warning message in watchdog with the offending string/content. This issue is related to how entities must be defined in XML. This is the most useful pointer I could find; http://nz.php.net/manual/en/ref.dom.php#57274. Probably a bit more time and patience would do it.

Another probable issue is when the input markup is invalid. I have not tested this as our markup comes through tinyMCE, so is usually pretty good. I tried running the content through tidy, but it was unnecessary for our case. I have left the code in, but commented it out in adsense_injector_xmlstrtodom() for further experimentation. Perhaps this could be configured in settings also.

AttachmentSize
adsense injector.patch6.88 KB

#1

grateful_drupal_user - May 3, 2007 - 16:33

Thanks for the submission.

I had considered using the DOM manipulation routines found in PHP5 but didn't want to create a dependency on PHP5. So, I need to find a solution that works well or degrades gracefully when PHP5 and the required libraries are not present.

Good point about valid HTML & entities.

#2

Bevan - May 4, 2007 - 05:40

Re php5 dependency, I don't think becoming php 5 dependent is too much of an issue when php5 has been out for almost three years. My advice for anyone who would have a problem with this is upgrade your server, and if you can't do it your self and your hosting company or sysadmin can't or won't upgrade, then get a new host.

If it's something that absolutely must be maintained a simple approach is to use phpversion() to determine whether to enable "inline injection" as this feature could be nicknamed, and leave a drupal_message() notice on adsense_injection settings page to let the user know that they're running php4 and therefore missing out on this feature.

Or if someone has the time this feature could be reimplemented for php4 using DOM XML which (at a glance) seems very similar to php5's DOM extension in regards to features, classes, methods and properties, although implemented in a slightly different way. Often this difference seems to be as simple as procedural functions instead of OO methods. This would bring the feature to both versions of php, but at a cost of maintainability. I'm happy to maintain the php5 code.

As alternatives to phpversion(), domxml_version() and function_exists() could be used to determain which php extension is used however advantages would probably be minimal as one would need to manually configure and compile php to get DOM XML in php5 or DOM in php4.

#3

Bevan - May 4, 2007 - 06:04

It turns out (says the boss) that some nodes require 2 long paragraphs in %content1, and other nodes require 3. I'm considering implementing either;

* a node-edit field over-ride for the 'paragraphs in %content1 setting' to set this to something else on some nodes
* some logic that determines when enough paragraphs have been placed in %content1 and it should go on to %content2. This would basically turn the test for whether this $child_node goes into %content1 or %content2, from $i>0 into something like;

<?php

$word_limit
= variable_get('word_limit', 50);
if (
word_count($content['parts'][1]) >> $word_limit) {
 
// put this $child_node in %content1
}
else {
 
// put this $child_node in %content2
}
?>

Any thoughts? The former is quicker, but the latter is a more general solution.

#4

Bevan - May 4, 2007 - 06:08

Woops. I meant:

<?php
$word_limit
= variable_get('word_limit', 50);
if (
word_count($content['parts'][1]) >> $word_limit) {
 
// put this $child_node in %content1
}
else {
 
// put this $child_node in %content2
}
?>

#5

Bevan - May 9, 2007 - 00:35

This has become a lower priority for us, but is something that I'll probably continue developing (and fixing) in a number of weeks, when we need it again.

Interestingly, there has been a lot of debate recently about whether drupal should continue to support php4. http://www.nicklewis.org/node/911 http://willy.boerland.com/myblog/and_yet_anoter_drupal_php_4_5_rant http://cmsreport.com/node/898 http://buytaert.net/php-is-dead-long-live-php

I definitely agree with Nick Lewis http://www.nicklewis.org/node/911 and Dries last post: http://buytaert.net/php-is-dead-long-live-php on this matter.

#6

Bevan - August 31, 2007 - 05:13

Now that drupal has adopted a policy on php5 support, we can add this feature to drupal 6 with a php5 dependency! :)

#7

fhelmschrott - November 23, 2007 - 06:32

Is there any update on the real adsense injection into the content?

#8

Bevan - November 24, 2007 - 01:58
Status:needs review» needs work

No. But this should be rerolled for adsense.module's drupal 6 release as a php5-only feature. I currently have no motivation for this, so please take it on someone else! :)

Version needs to be set to 6.x-1.x-dev once it's created

#9

lost305 - June 2, 2009 - 17:32

Does this work with the newest version of Adsense Injector currently (adsense_injector-6.x-2.7) ??

This is much needed for people like me because every node starts off with a picture and having a picture and an ad squashes the text and it looks amateurish.

If this could work it would be the best thing yet!

Thanks for your time.

#10

Bevan - June 3, 2009 - 02:13

Given it's over 2 years old now, almost certainly not.

#11

lost305 - June 22, 2009 - 15:27

Oh well. Google closed my gmail account with no warning or explanation and now I can't access my Adsense account.

#12

netdream - August 25, 2009 - 17:02

Maybe it's not the best way of doing that... but... why not use JQuery?

This example moves the div I called #adsense-body1 right after second paragraph of the content...

I created this in the "Node body ad insertion"

<div id="adsense-body1" style="display:block;float:right;margin: 5px 10px 5px 10px;">[adsense:250x250:1234567890]</div>
%body

Then, i added this to the scripts of my template:

$(document).ready(
    function() {
$('#adsense-body1').siblings('p:eq(2)').after($('#adsense-body1'));
    }
);

#13

asterix - October 29, 2009 - 15:04

Here's another approach (methods for Drupal 5.x and 6.x) - inserting an ad region automatically into the middle of your body content after a specified number of paragraphs: http://linkth.at/sq

 
 

Drupal is a registered trademark of Dries Buytaert.