]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Adsense/AdsensePlugin.php
Merge remote branch 'statusnet/1.0.x' into msn-plugin
[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
81 class AdsensePlugin extends UAPPlugin
82 {
83     public $adScript = 'http://pagead2.googlesyndication.com/pagead/show_ads.js';
84     public $client   = null;
85
86     function initialize()
87     {
88         parent::initialize();
89
90         // A little bit of chicanery so we avoid overwriting values that
91         // are passed in with the constructor
92
93         foreach (array('mediumRectangle', 'rectangle', 'leaderboard', 'wideSkyscraper', 'adScript', 'client') as $setting) {
94             $value = common_config('adsense', strtolower($setting));
95             if (!empty($value)) { // not found
96                 $this->$setting = $value;
97             }
98         }
99     }
100
101     /**
102      * Show a medium rectangle 'ad'
103      *
104      * @param Action $action Action being shown
105      *
106      * @return void
107      */
108
109     protected function showMediumRectangle($action)
110     {
111         $this->showAdsenseCode($action, 300, 250, $this->mediumRectangle);
112     }
113
114     /**
115      * Show a rectangle 'ad'
116      *
117      * @param Action $action Action being shown
118      *
119      * @return void
120      */
121
122     protected function showRectangle($action)
123     {
124         $this->showAdsenseCode($action, 180, 150, $this->rectangle);
125     }
126
127     /**
128      * Show a wide skyscraper ad
129      *
130      * @param Action $action Action being shown
131      *
132      * @return void
133      */
134
135     protected function showWideSkyscraper($action)
136     {
137         $this->showAdsenseCode($action, 160, 600, $this->wideSkyscraper);
138     }
139
140     /**
141      * Show a leaderboard ad
142      *
143      * @param Action $action Action being shown
144      *
145      * @return void
146      */
147
148     protected function showLeaderboard($action)
149     {
150         $this->showAdsenseCode($action, 728, 90, $this->leaderboard);
151     }
152
153     /**
154      * Output the bits of JavaScript code to show Adsense
155      *
156      * @param Action  $action Action being shown
157      * @param integer $width  Width of the block
158      * @param integer $height Height of the block
159      * @param string  $slot   Slot identifier
160      *
161      * @return void
162      */
163
164     protected function showAdsenseCode($action, $width, $height, $slot)
165     {
166         $code  = 'google_ad_client = "'.$this->client.'"; ';
167         $code .= 'google_ad_slot = "'.$slot.'"; ';
168         $code .= 'google_ad_width = '.$width.'; ';
169         $code .= 'google_ad_height = '.$height.'; ';
170
171         $action->inlineScript($code);
172
173         $action->script($this->adScript);
174     }
175
176     function onRouterInitialized($m)
177     {
178         $m->connect('admin/adsense',
179                     array('action' => 'adsenseadminpanel'));
180
181         return true;
182     }
183
184     function onAutoload($cls)
185     {
186         $dir = dirname(__FILE__);
187
188         switch ($cls)
189         {
190         case 'AdsenseadminpanelAction':
191             require_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
192             return false;
193         default:
194             return true;
195         }
196     }
197
198     function onEndAdminPanelNav($menu) {
199         if (AdminPanelAction::canAdmin('adsense')) {
200             // TRANS: Menu item title/tooltip
201             $menu_title = _('Adsense configuration');
202             // TRANS: Menu item for site administration
203             $menu->out->menuItem(common_local_url('adsenseadminpanel'), _('Adsense'),
204                                  $menu_title, $action_name == 'adsenseadminpanel', 'nav_adsense_admin_panel');
205         }
206         return true;
207     }
208 }