AdsensePlugin.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. /**
  33. * Plugin to add Google Adsense to StatusNet sites
  34. *
  35. * This plugin lets you add Adsense ad units to your StatusNet site.
  36. *
  37. * We support the 4 ad sizes for the Universal Ad Platform (UAP):
  38. *
  39. * Medium Rectangle
  40. * (Small) Rectangle
  41. * Leaderboard
  42. * Wide Skyscraper
  43. *
  44. * They fit in different places on the default theme. Some themes
  45. * might interact quite poorly with this plugin.
  46. *
  47. * To enable advertising, you must sign up with Google Adsense and
  48. * get a client ID.
  49. *
  50. * https://www.google.com/adsense/
  51. *
  52. * You'll also need to create an Adsense for Content unit in one
  53. * of the four sizes described above. At the end of the process,
  54. * note the "google_ad_client" and "google_ad_slot" values in the
  55. * resultant Javascript.
  56. *
  57. * Add the plugin to config.php like so:
  58. *
  59. * addPlugin('Adsense', array('client' => 'Your client ID',
  60. * 'rectangle' => 'slot'));
  61. *
  62. * Here, your client ID is the value of google_ad_client and the
  63. * slot is the value of google_ad_slot. Note that if you create
  64. * a different size, you'll need to provide different arguments:
  65. * 'mediumRectangle', 'leaderboard', or 'wideSkyscraper'.
  66. *
  67. * If for some reason your ad server is different from the default,
  68. * use the 'adScript' parameter to set the full path to the ad script.
  69. *
  70. * @category Plugin
  71. * @package StatusNet
  72. * @author Evan Prodromou <evan@status.net>
  73. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  74. * @link http://status.net/
  75. *
  76. * @seeAlso UAPPlugin
  77. */
  78. class AdsensePlugin extends UAPPlugin
  79. {
  80. public $adScript = 'http://pagead2.googlesyndication.com/pagead/show_ads.js';
  81. public $client = null;
  82. function initialize()
  83. {
  84. parent::initialize();
  85. // A little bit of chicanery so we avoid overwriting values that
  86. // are passed in with the constructor
  87. foreach (array('mediumRectangle', 'rectangle', 'leaderboard', 'wideSkyscraper', 'adScript', 'client') as $setting) {
  88. $value = common_config('adsense', strtolower($setting));
  89. if (!empty($value)) { // not found
  90. $this->$setting = $value;
  91. }
  92. }
  93. }
  94. /**
  95. * Show a medium rectangle 'ad'
  96. *
  97. * @param Action $action Action being shown
  98. *
  99. * @return void
  100. */
  101. protected function showMediumRectangle($action)
  102. {
  103. $this->showAdsenseCode($action, 300, 250, $this->mediumRectangle);
  104. }
  105. /**
  106. * Show a rectangle 'ad'
  107. *
  108. * @param Action $action Action being shown
  109. *
  110. * @return void
  111. */
  112. protected function showRectangle($action)
  113. {
  114. $this->showAdsenseCode($action, 180, 150, $this->rectangle);
  115. }
  116. /**
  117. * Show a wide skyscraper ad
  118. *
  119. * @param Action $action Action being shown
  120. *
  121. * @return void
  122. */
  123. protected function showWideSkyscraper($action)
  124. {
  125. $this->showAdsenseCode($action, 160, 600, $this->wideSkyscraper);
  126. }
  127. /**
  128. * Show a leaderboard ad
  129. *
  130. * @param Action $action Action being shown
  131. *
  132. * @return void
  133. */
  134. protected function showLeaderboard($action)
  135. {
  136. $this->showAdsenseCode($action, 728, 90, $this->leaderboard);
  137. }
  138. /**
  139. * Output the bits of JavaScript code to show Adsense
  140. *
  141. * @param Action $action Action being shown
  142. * @param integer $width Width of the block
  143. * @param integer $height Height of the block
  144. * @param string $slot Slot identifier
  145. *
  146. * @return void
  147. */
  148. protected function showAdsenseCode($action, $width, $height, $slot)
  149. {
  150. $code = 'google_ad_client = "'.$this->client.'"; ';
  151. $code .= 'google_ad_slot = "'.$slot.'"; ';
  152. $code .= 'google_ad_width = '.$width.'; ';
  153. $code .= 'google_ad_height = '.$height.'; ';
  154. $action->inlineScript($code);
  155. $action->script($this->adScript);
  156. }
  157. function onRouterInitialized($m)
  158. {
  159. $m->connect('panel/adsense',
  160. array('action' => 'adsenseadminpanel'));
  161. return true;
  162. }
  163. function onEndAdminPanelNav($menu) {
  164. if (AdminPanelAction::canAdmin('adsense')) {
  165. // TRANS: Menu item title/tooltip
  166. $menu_title = _m('AdSense configuration');
  167. // TRANS: Menu item for site administration
  168. $menu->out->menuItem(common_local_url('adsenseadminpanel'), _m('MENU','AdSense'),
  169. $menu_title, $action_name == 'adsenseadminpanel', 'nav_adsense_admin_panel');
  170. }
  171. return true;
  172. }
  173. function onPluginVersion(&$versions)
  174. {
  175. $versions[] = array('name' => 'BlankAdPlugin',
  176. 'version' => GNUSOCIAL_VERSION,
  177. 'author' => 'Evan Prodromou',
  178. 'homepage' => 'http://status.net/wiki/Plugin:Adsense',
  179. 'rawdescription' =>
  180. // TRANS: Plugin description.
  181. _m('Plugin to add Google AdSense to StatusNet sites.'));
  182. return true;
  183. }
  184. }