Changes to Google Adsense plugin

Creating and modifying plugins.
Post Reply
hyejohn
Regular
Posts: 37
Joined: Mon Jun 22, 2009 4:04 pm
Location: Michigan, USA
Contact:

Changes to Google Adsense plugin

Post by hyejohn »

I made some changes to the Google Adsense Plugin to take advantage of the custom ad-box builds that you can use now. Just wanted to share!

The new php file:

Code: Select all

<?php


if (IN_serendipity !== true) {
    die ("Don't hack!");
}

// Probe for a language include with constants. Still include defines later on, if some constants were missing
$probelang = dirname(__FILE__) . '/' . $serendipity['charset'] . 'lang_' . $serendipity['lang'] . '.inc.php';
if (file_exists($probelang)) {
    include $probelang;
}

include dirname(__FILE__) . '/lang_en.inc.php';

class serendipity_plugin_google_adsense extends serendipity_plugin
{
    var $title = PLUGIN_ADSENSE_NAME;

    function introspect(&$propbag)
    {
        global $serendipity;

        $this->title = $this->get_config('title', $this->title);

        $propbag->add('name',          PLUGIN_ADSENSE_NAME);
        $propbag->add('description',   PLUGIN_ADSENSE_DESC);
        $propbag->add('stackable',     true);
        $propbag->add('author',        'Jim Dabell');
        $propbag->add('version',       '0.2');
        $propbag->add('requirements',  array(
            'serendipity' => '0.8',
            'smarty'      => '2.6.7',
            'php'         => '4.1.0'
        ));
        $propbag->add('groups', array('FRONTEND_VIEWS'));
        $propbag->add('configuration', array('client',
                                             'slot',
                                             'format',
                                             'type',
                                             'channel',
                                             'number'));
    }

    function introspect_config_item($name, &$propbag)
    {
        switch($name) {
        	case 'client':
        		$propbag->add('type', 'string');
                $propbag->add('name', PLUGIN_ADSENSE_CLIENT_NAME);
                $propbag->add('description', PLUGIN_ADSENSE_CLIENT_DESCRIPTION);
                $propbag->add('default', '');
        	break;
        	case 'slot':
        		$propbag->add('type', 'string');
                $propbag->add('name', PLUGIN_ADSENSE_SLOT_NAME);
                $propbag->add('description', PLUGIN_ADSENSE_SLOT_DESCRIPTION);
                $propbag->add('default', '');
        	break;
            case 'number':
                $propbag->add('type', 'select');
                $propbag->add('name', PLUGIN_ADSENSE_NUMBER_NAME);
                $propbag->add('description', PLUGIN_ADSENSE_NUMBER_DESCRIPTION);
                $propbag->add('default', '1');
                $propbag->add('select_values', array('1'=>1,
                                                '2'=>2,
                                                '3'=>3,
                                                '4'=>4,
                                                '5'=>5));
                break;
        	case 'format':
        		$propbag->add('type', 'select');
                $propbag->add('name', PLUGIN_ADSENSE_FORMAT_NAME);
                $propbag->add('description', PLUGIN_ADSENSE_FORMAT_DESCRIPTION);
                $propbag->add('default', '160x600_as');
                $propbag->add('select_values', array(
                	'728x90_as'=>PLUGIN_ADSENSE_FORMAT_LEADERBOARD,
                	'468x60_as'=>PLUGIN_ADSENSE_FORMAT_BANNER,
                	'300x250_as'=>PLUGIN_ADSENSE_FORMAT_MEDIUM_RECTANGLE,
                	'160x600_as'=>PLUGIN_ADSENSE_FORMAT_WIDE_SKYSCRAPER,
                	'120x600_as'=>PLUGIN_ADSENSE_FORMAT_SKYSCRAPER));
        	break;
        	case 'type':
        		$propbag->add('type', 'select');
                $propbag->add('name', PLUGIN_ADSENSE_TYPE_NAME);
                $propbag->add('description', PLUGIN_ADSENSE_TYPE_DESCRIPTION);
                $propbag->add('default', 'text_image');
                $propbag->add('select_values', array(
                	'text_image'=>PLUGIN_ADSENSE_TYPE_TEXT_AND_IMAGES,
                	'text'=>PLUGIN_ADSENSE_TYPE_TEXT_ONLY,
                	'image'=>PLUGIN_ADSENSE_TYPE_IMAGES_ONLY));
        	break;
        	case 'channel':
        		$propbag->add('type', 'string');
                $propbag->add('name', PLUGIN_ADSENSE_CHANNEL_NAME);
                $propbag->add('description', PLUGIN_ADSENSE_CHANNEL_DESCRIPTION);
                $propbag->add('default', '');
        	break;
            default:
                    return false;
        }
        return true;
    }

    function generate_content(&$title)
    {
     global $serendipity;
        $client      = $this->get_config('client');
        $slot        = $this->get_config('slot');
        $format      = $this->get_config('format');
        $width       = intval($format, 10);
        $height      = intval(substr(stristr($format, 'x'), 1), 10);
        $type        = $this->get_config('type');
        $channel     = $this->get_config('channel');
        $number      = $this->get_config('number');

        if ($slot == '')
        {
        for($i = 0; $i < $number ; $i++)
        {
		    echo("<script type='text/javascript'><!--\n");
		    echo("google_ad_client = '$client';\n");
		    echo("google_ad_width = $width;\n");
		    echo("google_ad_height = $height;\n");
		    echo("google_ad_format = '$format';\n");
		    echo("google_ad_type = '$type';\n");
		    echo("google_ad_channel = '$channel';\n");
		    echo("//--></script>\n");
		    echo('<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">');
		    echo('</script>');
            echo('<br />');
        }
        } else {
        for($i = 0; $i < $number ; $i++)
        {
          echo("<script type='text/javascript'><!--\n");
		    echo("google_ad_client = '$client';\n");
		    echo("google_ad_slot = '$slot';\n");
		    echo("google_ad_width = $width;\n");
		    echo("google_ad_height = $height;\n");
		    echo("//--></script>\n");
		    echo('<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">');
		    echo('</script>');
            echo('<br />');
           }
     }
    }
}

/* vim: set sts=4 ts=4 expandtab : */
and the new lang file (sorry, I only speak English!):

Code: Select all

<?php

/**
 *  @version $Revision: 1.2 $
 *  @author Translator Name <yourmail@example.com>
 *  EN-Revision: Revision of lang_en.inc.php
 */

@define('PLUGIN_ADSENSE_NAME', 'AdSense');
@define('PLUGIN_ADSENSE_DESC', 'Displays adverts from Google.');

@define('PLUGIN_ADSENSE_CLIENT_NAME', 'Client ID');
@define('PLUGIN_ADSENSE_CLIENT_DESCRIPTION', 'This is your client identification that Google gives you, starting with "pub-".');

@define('PLUGIN_ADSENSE_SLOT_NAME', 'SLOT ID');
@define('PLUGIN_ADSENSE_SLOT_DESCRIPTION', 'Optional SLOT id.');

@define('PLUGIN_ADSENSE_WIDTH', 'Width');

@define('PLUGIN_ADSENSE_HEIGHT', 'Height');

@define('PLUGIN_ADSENSE_FORMAT_NAME', 'Format');
@define('PLUGIN_ADSENSE_FORMAT_DESCRIPTION', 'What format ads do you want?');
@define('PLUGIN_ADSENSE_FORMAT_LEADERBOARD', '728 x 90 Leaderboard');
@define('PLUGIN_ADSENSE_FORMAT_BANNER', '468 x 60 Banner');
@define('PLUGIN_ADSENSE_FORMAT_MEDIUM_RECTANGLE', '300 x 250 Medium Rectangle');
@define('PLUGIN_ADSENSE_FORMAT_WIDE_SKYSCRAPER', '160 x 600 Wide Skyscraper');
@define('PLUGIN_ADSENSE_FORMAT_SKYSCRAPER', '120 x 600 Skyscraper');

@define('PLUGIN_ADSENSE_TYPE_NAME', 'Type');
@define('PLUGIN_ADSENSE_TYPE_DESCRIPTION', 'Do you want text ads or image ads?');

@define('PLUGIN_ADSENSE_TYPE_TEXT_AND_IMAGES', 'Text and image ads');
@define('PLUGIN_ADSENSE_TYPE_TEXT_ONLY', 'Text ads only');
@define('PLUGIN_ADSENSE_TYPE_IMAGES_ONLY', 'Images ads only');

@define('PLUGIN_ADSENSE_CHANNEL_NAME', 'Channel');
@define('PLUGIN_ADSENSE_CHANNEL_DESCRIPTION', 'Optionally distinguish between different ads.  This should be the number Google gives you, not the name of the channel.');

@define('PLUGIN_ADSENSE_NUMBER_NAME', 'Number');
@define('PLUGIN_ADSENSE_NUMBER_DESCRIPTION', 'Number of ads to show, with a <BR> tag in between');

?>
~John
That Photographer guy in Michigan
hyejohn
Regular
Posts: 37
Joined: Mon Jun 22, 2009 4:04 pm
Location: Michigan, USA
Contact:

Re: Changes to Google Adsense plugin

Post by hyejohn »

As used on my sites:
therightphotographer.com
Put up this weekend-my own template based on bulletproof, terrafirma and another that uses a menubar. I also added lightbox2 for the gallery plugin as part of the template, but this site doesn't have any photos, so you won't see it there. (- I'm not quite done with the template yet.)

and

therightmodel.com (My first attempt with Serendipity.)
~John
That Photographer guy in Michigan
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Changes to Google Adsense plugin

Post by garvinhicking »

Hi!

Great, many thanks for sharing your patch! I've committed and uploaded it.

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
marcolino1961
Regular
Posts: 5
Joined: Sun Sep 06, 2009 11:19 am

Re: Changes to Google Adsense plugin

Post by marcolino1961 »

Sorry, I can't find ADSense plugin in the repository (with Spartacus): has it been revoked? do I miss something?
hyejohn
Regular
Posts: 37
Joined: Mon Jun 22, 2009 4:04 pm
Location: Michigan, USA
Contact:

Re: Changes to Google Adsense plugin

Post by hyejohn »

It's a sidebar plugin under Frontend: Views

http://spartacus.s9y.org/index.php?mode ... D_VIEWS_en
~John
That Photographer guy in Michigan
Post Reply