]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Adsense/AdsensePlugin.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / plugins / Adsense / AdsensePlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Plugin for Google Adsense
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Ads
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @copyright 2010 StatusNet Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * Plugin to add Google Adsense to StatusNet sites
36  *
37  * This plugin lets you add Adsense ad units to your StatusNet site.
38  *
39  * We support the 4 ad sizes for the Universal Ad Platform (UAP):
40  *
41  *     Medium Rectangle
42  *     (Small) Rectangle
43  *     Leaderboard
44  *     Wide Skyscraper
45  *
46  * They fit in different places on the default theme. Some themes
47  * might interact quite poorly with this plugin.
48  *
49  * To enable advertising, you must sign up with Google Adsense and
50  * get a client ID.
51  *
52  *     https://www.google.com/adsense/
53  *
54  * You'll also need to create an Adsense for Content unit in one
55  * of the four sizes described above. At the end of the process,
56  * note the "google_ad_client" and "google_ad_slot" values in the
57  * resultant Javascript.
58  *
59  * Add the plugin to config.php like so:
60  *
61  *     addPlugin('Adsense', array('client' => 'Your client ID',
62  *                                'rectangle' => 'slot'));
63  *
64  * Here, your client ID is the value of google_ad_client and the
65  * slot is the value of google_ad_slot. Note that if you create
66  * a different size, you'll need to provide different arguments:
67  * 'mediumRectangle', 'leaderboard', or 'wideSkyscraper'.
68  *
69  * If for some reason your ad server is different from the default,
70  * use the 'adScript' parameter to set the full path to the ad script.
71  *
72  * @category Plugin
73  * @package  StatusNet
74  * @author   Evan Prodromou <evan@status.net>
75  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
76  * @link     http://status.net/
77  *
78  * @seeAlso  UAPPlugin
79  */
80 class AdsensePlugin extends UAPPlugin
81 {
82     public $adScript = 'http://pagead2.googlesyndication.com/pagead/show_ads.js';
83     public $client   = null;
84
85     function initialize()
86     {
87         parent::initialize();
88
89         // A little bit of chicanery so we avoid overwriting values that
90         // are passed in with the constructor
91         foreach (array('mediumRectangle', 'rectangle', 'leaderboard', 'wideSkyscraper', 'adScript', 'client') as $setting) {
92             $value = common_config('adsense', strtolower($setting));
93             if (!empty($value)) { // not found
94                 $this->$setting = $value;
95             }
96         }
97     }
98
99     /**
100      * Show a medium rectangle 'ad'
101      *
102      * @param Action $action Action being shown
103      *
104      * @return void
105      */
106     protected function showMediumRectangle($action)
107     {
108         $this->showAdsenseCode($action, 300, 250, $this->mediumRectangle);
109     }
110
111     /**
112      * Show a rectangle 'ad'
113      *
114      * @param Action $action Action being shown
115      *
116      * @return void
117      */
118     protected function showRectangle($action)
119     {
120         $this->showAdsenseCode($action, 180, 150, $this->rectangle);
121     }
122
123     /**
124      * Show a wide skyscraper ad
125      *
126      * @param Action $action Action being shown
127      *
128      * @return void
129      */
130     protected function showWideSkyscraper($action)
131     {
132         $this->showAdsenseCode($action, 160, 600, $this->wideSkyscraper);
133     }
134
135     /**
136      * Show a leaderboard ad
137      *
138      * @param Action $action Action being shown
139      *
140      * @return void
141      */
142     protected function showLeaderboard($action)
143     {
144         $this->showAdsenseCode($action, 728, 90, $this->leaderboard);
145     }
146
147     /**
148      * Output the bits of JavaScript code to show Adsense
149      *
150      * @param Action  $action Action being shown
151      * @param integer $width  Width of the block
152      * @param integer $height Height of the block
153      * @param string  $slot   Slot identifier
154      *
155      * @return void
156      */
157     protected function showAdsenseCode($action, $width, $height, $slot)
158     {
159         $code  = 'google_ad_client = "'.$this->client.'"; ';
160         $code .= 'google_ad_slot = "'.$slot.'"; ';
161         $code .= 'google_ad_width = '.$width.'; ';
162         $code .= 'google_ad_height = '.$height.'; ';
163
164         $action->inlineScript($code);
165
166         $action->script($this->adScript);
167     }
168
169     function onRouterInitialized(URLMapper $m)
170     {
171         $m->connect('panel/adsense',
172                     array('action' => 'adsenseadminpanel'));
173
174         return true;
175     }
176
177     function onEndAdminPanelNav(Menu $menu) {
178         if (AdminPanelAction::canAdmin('adsense')) {
179             // TRANS: Menu item title/tooltip
180             $menu_title = _m('AdSense configuration');
181             // TRANS: Menu item for site administration
182             $menu->out->menuItem(common_local_url('adsenseadminpanel'), _m('MENU','AdSense'),
183                                  $menu_title, $action_name == 'adsenseadminpanel', 'nav_adsense_admin_panel');
184         }
185         return true;
186     }
187
188     function onPluginVersion(array &$versions)
189     {
190         $versions[] = array('name' => 'BlankAdPlugin',
191                             'version' => GNUSOCIAL_VERSION,
192                             'author' => 'Evan Prodromou',
193                             'homepage' => 'http://status.net/wiki/Plugin:Adsense',
194                             'rawdescription' =>
195                             // TRANS: Plugin description.
196                             _m('Plugin to add Google AdSense to StatusNet sites.'));
197         return true;
198     }
199 }