b2evolution PHP Cross Reference Blogging Systems

Source: /plugins/adsense_plugin/_adsense.plugin.php - 244 lines - 7042 bytes - Summary - Text - Print

Description: This file implements the AdSense plugin for b2evolution b2evolution - {@link http://b2evolution.net/} Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}

   1  <?php
   2  /**
   3   * This file implements the AdSense plugin for b2evolution
   4   *
   5   * b2evolution - {@link http://b2evolution.net/}
   6   * Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}
   7   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}
   8   *
   9   * @package plugins
  10   */
  11  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  12  
  13  /**
  14   * Replaces AdSense markup in HTML.
  15   *
  16   * @todo Remove it in XML Feeds?
  17   *
  18   * @package plugins
  19   */
  20  class adsense_plugin extends Plugin
  21  {
  22      var $code = 'evo_adsense';
  23      var $name = 'AdSense';
  24      var $priority = 10;
  25      var $group = 'rendering';
  26      var $help_url = 'http://b2evolution.net/blog-ads/adsense-plugin.php';
  27      var $short_desc;
  28      var $long_desc;
  29      var $version = '5.0.0';
  30      var $number_of_installs = 1;
  31  
  32      /**
  33       * Init
  34       */
  35  	function PluginInit( & $params )
  36      {
  37          $this->short_desc = T_('Easy AdSense insertion into posts.');
  38          $this->long_desc = T_('<p>This plugin allows you to easily insert AdSense into your posts (simply type [adsense:], or use the toolbar button in expert mode).</p>
  39  <p>The plugin will stop expanding AdSense blocks when a limit is reached (3 by default).</p>
  40  <p>Look for version 2.0 for multiple AdSense format support.</p>');
  41      }
  42  
  43  
  44      /**
  45       * Get the settings that the plugin can use.
  46       *
  47       * Those settings are transfered into a Settings member object of the plugin
  48       * and can be edited in the backoffice (Settings / Plugins).
  49       *
  50       * @see Plugin::GetDefaultSettings()
  51       * @see PluginSettings
  52       * @see Plugin::PluginSettingsValidateSet()
  53       * @return array
  54       */
  55  	function GetDefaultSettings( & $params )
  56      {
  57  
  58          $r = array(
  59              'adsense_block' => array(
  60                      'label' => 'Default AdSense block',
  61                      'type' => 'html_textarea',
  62                      'cols' => 60,
  63                      'rows' => 10,
  64                      'defaultvalue' => '<div style="float:right; clear:both; margin:5px;">
  65  
  66  <!-- Paste from here... -->
  67  <div style="border:1px solid red; width:150px; padding:3px;"><strong>You must copy/paste your AdSense code in here.</strong> You can do this on the Plugin config page.</div>
  68  <!-- ...to here -->
  69  
  70  </div>',
  71                      'note' => 'Copy/Paste your AdSense code from Google into here. You can surround it with some CSS for decoration and/or positionning.',
  72                  ),
  73              'max_blocks_in_content' => array(
  74                      'label' => 'Default Max # of blocks',
  75                      'type' => 'integer',
  76                      'size' => 2,
  77                      'maxlength' => 2,
  78                      'defaultvalue' => 3,
  79                      'note' => T_('Maximum number of AdSense blocks the plugin should expand in post contents. Google terms typically set the limit to 3. You may wish to set it to less if you add blocks into the sidebar.'),
  80                  ),
  81              );
  82  
  83          return $r;
  84      }
  85  
  86  
  87      /**
  88       * Define here default collection/blog settings that are to be made available in the backoffice.
  89       *
  90       * @return array See {@link Plugin::GetDefaultSettings()}.
  91       */
  92  	function get_coll_setting_definitions( & $params )
  93      {
  94          $r = array(
  95              'coll_adsense_block' => array(
  96                      'label' => 'AdSense block',
  97                      'type' => 'html_textarea',
  98                      'cols' => 60,
  99                      'rows' => 10,
 100                      'defaultvalue' => $this->Settings->get('adsense_block'),
 101                      'note' => 'Copy/Paste your AdSense code from Google into here. You can surround it with some CSS for decoration and/or positionning.',
 102                  ),
 103              'coll_max_blocks_in_content' => array(
 104                      'label' => 'Max # of blocks',
 105                      'type' => 'integer',
 106                      'size' => 2,
 107                      'maxlength' => 2,
 108                      'defaultvalue' => $this->Settings->get('max_blocks_in_content'),
 109                      'note' => T_('Maximum number of AdSense blocks the plugin should expand in post contents. Google terms typically set the limit to 3. You may wish to set it to less if you add blocks into the sidebar.'),
 110                  ),
 111              );
 112  
 113          return array_merge( parent::get_coll_setting_definitions( $params ), $r );
 114      }
 115  
 116  
 117      /**
 118       * Comments out the adsense tags so that they don't get worked on by other renderers like Auto-P
 119       *
 120       * @param mixed $params
 121       */
 122  	function FilterItemContents( & $params )
 123      {
 124          $content = & $params['content'];
 125  
 126          $content = preg_replace( '~\[(adsense:)\]~', '<!-- [$1] -->', $content );
 127  
 128          return true;
 129      }
 130  
 131  
 132      /**
 133       * Changes the commented out tags into something that is visible to the editor
 134       *
 135       * @param mixed $params
 136       */
 137  	function UnfilterItemContents( & $params )
 138      {
 139          $content = & $params['content'];
 140  
 141          $content = preg_replace( '~<!-- \[(adsense:)\] -->~', '[$1]', $content );
 142  
 143          return true;
 144      }
 145  
 146  
 147      /**
 148       * Event handler: Called when rendering item/post contents as HTML. (CACHED)
 149       *
 150       * The rendered content will be *cached* and the cached content will be reused on subsequent displays.
 151       * Use {@link DisplayItemAsHtml()} instead if you want to do rendering at display time.
 152       *
 153        * Note: You have to change $params['data'] (which gets passed by reference).
 154       *
 155       * @param array Associative array of parameters
 156       *   - 'data': the data (by reference). You probably want to modify this.
 157       *   - 'format': see {@link format_to_output()}. Only 'htmlbody' and 'entityencoded' will arrive here.
 158       *   - 'Item': the {@link Item} object which gets rendered.
 159       * @return boolean Have we changed something?
 160       */
 161  	function RenderItemAsHtml( & $params )
 162      {
 163          // Dummy placeholder. Without it the plugin would ne be considered to be a renderer...
 164          return false;
 165      }
 166  
 167  
 168      /**
 169       * Perform rendering (at display time, i-e: NOT cached)
 170       *
 171       * @todo does this actually get fed out in the xml feeds?
 172       *
 173       * @see Plugin::DisplayItemAsHtml()
 174       */
 175  	function DisplayItemAsHtml( & $params )
 176      {
 177          $content = & $params['data'];
 178  
 179          $content = replace_content_outcode( '~<!-- \[adsense:\] -->~', array( $this, 'DisplayItem_callback' ), $content, 'replace_content_callback' );
 180  
 181          return true;
 182      }
 183  
 184  
 185      /**
 186       *
 187       */
 188  	function DisplayItem_callback( $matches )
 189      {
 190          global $Blog;
 191  
 192            /**
 193           * How many blocks already displayed?
 194           */
 195          static $adsense_blocks_counter = 0;
 196  
 197          $adsense_blocks_counter++;
 198  
 199          if( $adsense_blocks_counter > $this->Settings->get( 'max_blocks_in_content' ) )
 200          {
 201              return '<!-- Adsense block #'.$adsense_blocks_counter.' not displayed since it exceed the limit of '
 202                              .$this->get_coll_setting( 'coll_max_blocks_in_content', $Blog ).' -->';
 203          }
 204  
 205          return $this->get_coll_setting( 'coll_adsense_block', $Blog );
 206      }
 207  
 208      /**
 209       * Filter out adsense tags from XML content.
 210       *
 211       * @see Plugin::RenderItemAsXml()
 212       */
 213  	function DisplayItemAsXml( & $params )
 214      {
 215          $content = & $params['data'];
 216  
 217          $content = preg_replace( '~\[adsense:\]~', '', $content );
 218  
 219          return true;
 220      }
 221  
 222      /**
 223       * Display a toolbar in admin
 224       *
 225       * @param array Associative array of parameters
 226       * @return boolean did we display a toolbar?
 227       */
 228  	function AdminDisplayToolbar( & $params )
 229      {
 230          if( $params['edit_layout'] == 'simple' )
 231          {    // This is too complex for simple mode, don't display it:
 232              return false;
 233          }
 234  
 235          echo '<div class="edit_toolbar">';
 236          echo '<input type="button" id="adsense_default" title="'.T_('Insert AdSense block').'" class="quicktags" onclick="textarea_wrap_selection( b2evoCanvas, \'[adsense:]\', \'\', 1 );" value="'.T_('AdSense').'" />';
 237          echo '</div>';
 238  
 239          return true;
 240      }
 241  
 242  }
 243  
 244  ?>

title

Description

title

Description

title

Description

title

title

Body