You are here

class OldCodeAd in Google AdSense integration 8

Provides an AdSense old code ad unit.

Plugin annotation


@AdsenseAd(
  id = "oldcode",
  name = @Translation("Old code ads"),
  isSearch = FALSE,
  needsSlot = FALSE
)

Hierarchy

Expanded class hierarchy of OldCodeAd

File

oldcode/src/Plugin/AdsenseAd/OldCodeAd.php, line 18

Namespace

Drupal\adsense_oldcode\Plugin\AdsenseAd
View source
class OldCodeAd extends ContentAdBase {

  /**
   * Ad style (key to configured styles).
   *
   * @var int
   */
  private $style;

  /**
   * Ad Channel.
   *
   * @var string
   */
  private $channel;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id = '', $plugin_definition = NULL, $config_factory = NULL, $module_handler = NULL, $current_user = NULL) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $config_factory, $module_handler, $current_user);
    $fo = !empty($configuration['format']) ? $configuration['format'] : '';
    $st = !empty($configuration['style']) ? $configuration['style'] : 1;
    $ch = !empty($configuration['channel']) ? $configuration['channel'] : '';
    if ($st < 1 || $st > ADSENSE_OLDCODE_MAX_GROUPS) {

      // Default to 1 if an invalid style is supplied.
      $st = 1;
    }
    if (substr($fo, 0, 10) != 'Search Box' && !empty($fo)) {
      $this->format = $fo;
      $this->style = $st;
      $this->channel = $ch;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getAdPlaceholder() {
    if (!empty($this->format)) {
      $client = PublisherId::get();

      // Get width and height from the format.
      list($width, $height) = $this
        ->dimensions($this->format);
      $content = $this->configFactory
        ->get('adsense.settings')
        ->get('adsense_placeholder_text');
      $content .= "\nclient = {$client}\nformat = {$this->format}\nwidth = {$width}\nheight = {$height}\nstyle = {$this->style}\nchannel = {$this->channel}";
      return [
        '#content' => [
          '#markup' => nl2br($content),
        ],
        '#format' => $this->format,
        '#width' => $width,
        '#height' => $height,
      ];
    }
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function getAdContent() {
    if (!empty($this->format)) {
      $ad = $this
        ->adsenseAdFormats($this->format);
      if ($ad != NULL) {
        $core_config = $this->configFactory
          ->get('adsense.settings');
        $oldcode_config = $this->configFactory
          ->get('adsense_oldcode.settings');
        $client = PublisherId::get();
        $this->moduleHandler
          ->alter('adsense', $client);

        // Get width and height from the format.
        list($width, $height) = $this
          ->dimensions($this->format);
        switch ($oldcode_config
          ->get('adsense_ad_type_' . $this->style)) {
          case 0:
            $type = 'text';
            break;
          case 1:
            $type = 'image';
            break;
          default:
            $type = 'text_image';
            break;
        }
        $alt = $oldcode_config
          ->get('adsense_alt_' . $this->style);
        $alt_info = $oldcode_config
          ->get('adsense_alt_info_' . $this->style);
        return [
          '#theme' => 'adsense_oldcode',
          '#client' => $client,
          '#alt_url' => $alt == 1 ? $alt_info : '',
          '#alt_color' => $alt == 2 ? $alt_info : '',
          '#width' => $width,
          '#height' => $height,
          '#format' => $ad['code'],
          '#type' => $ad['type'] == ADSENSE_OLDCODE_TYPE_AD ? $type : '',
          '#channel' => $oldcode_config
            ->get('adsense_ad_channel_' . $this->channel),
          '#border' => mb_substr($oldcode_config
            ->get('adsense_color_border_' . $this->style), 1),
          '#bg' => mb_substr($oldcode_config
            ->get('adsense_color_bg_' . $this->style), 1),
          '#link' => mb_substr($oldcode_config
            ->get('adsense_color_link_' . $this->style), 1),
          '#text' => mb_substr($oldcode_config
            ->get('adsense_color_text_' . $this->style), 1),
          '#url' => mb_substr($oldcode_config
            ->get('adsense_color_url_' . $this->style), 1),
          '#features' => $oldcode_config
            ->get('adsense_ui_features_' . $this->style),
          '#secret' => $core_config
            ->get('adsense_secret_language'),
        ];
      }
    }
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public static function adsenseAdFormats($key = NULL) {
    $ads = [
      // Top performing ad sizes.
      '300x250' => [
        'type' => ADSENSE_OLDCODE_TYPE_AD,
        'desc' => t('Medium Rectangle'),
        'code' => '300x250_as',
      ],
      '336x280' => [
        'type' => ADSENSE_OLDCODE_TYPE_AD,
        'desc' => t('Large Rectangle'),
        'code' => '336x280_as',
      ],
      '728x90' => [
        'type' => ADSENSE_OLDCODE_TYPE_AD,
        'desc' => t('Leaderboard'),
        'code' => '728x90_as',
      ],
      // Other supported ad sizes.
      '468x60' => [
        'type' => ADSENSE_OLDCODE_TYPE_AD,
        'desc' => t('Banner'),
        'code' => '468x60_as',
      ],
      '234x60' => [
        'type' => ADSENSE_OLDCODE_TYPE_AD,
        'desc' => t('Half Banner'),
        'code' => '234x60_as',
      ],
      '120x600' => [
        'type' => ADSENSE_OLDCODE_TYPE_AD,
        'desc' => t('Skyscraper'),
        'code' => '120x600_as',
      ],
      '120x240' => [
        'type' => ADSENSE_OLDCODE_TYPE_AD,
        'desc' => t('Vertical Banner'),
        'code' => '120x240_as',
      ],
      '160x600' => [
        'type' => ADSENSE_OLDCODE_TYPE_AD,
        'desc' => t('Wide Skyscraper'),
        'code' => '160x600_as',
      ],
      '250x250' => [
        'type' => ADSENSE_OLDCODE_TYPE_AD,
        'desc' => t('Square'),
        'code' => '250x250_as',
      ],
      '200x200' => [
        'type' => ADSENSE_OLDCODE_TYPE_AD,
        'desc' => t('Small Square'),
        'code' => '200x200_as',
      ],
      '180x150' => [
        'type' => ADSENSE_OLDCODE_TYPE_AD,
        'desc' => t('Small Rectangle'),
        'code' => '180x150_as',
      ],
      '125x125' => [
        'type' => ADSENSE_OLDCODE_TYPE_AD,
        'desc' => t('Button'),
        'code' => '125x125_as',
      ],
    ];
    if (!empty($key)) {
      return array_key_exists($key, $ads) ? $ads[$key] : NULL;
    }
    else {
      return $ads;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AdsenseAdBase::$configFactory protected property Config factory.
AdsenseAdBase::$currentUser protected property Current user.
AdsenseAdBase::$moduleHandler protected property Module handler.
AdsenseAdBase::adsenseLanguages public static function List of available languages. Overrides AdsenseAdInterface::adsenseLanguages
AdsenseAdBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
AdsenseAdBase::createAd public static function Creates the ad object, as specified by the definitions in the parameter. Overrides AdsenseAdInterface::createAd
AdsenseAdBase::display public function Display ad HTML.
AdsenseAdBase::isDisabled public static function Check if ads display is disabled.
ContentAdBase::$format protected property Ad Format.
ContentAdBase::dimensions public static function Provides the width and height of the ad.
ContentAdBase::getFormat public function Gets the ad format.
OldCodeAd::$channel private property Ad Channel.
OldCodeAd::$style private property Ad style (key to configured styles).
OldCodeAd::adsenseAdFormats public static function This is the array that holds all ad formats. Overrides AdsenseAdInterface::adsenseAdFormats
OldCodeAd::getAdContent public function Return the ad content. Overrides AdsenseAdInterface::getAdContent
OldCodeAd::getAdPlaceholder public function Return the ad placeholder. Overrides AdsenseAdInterface::getAdPlaceholder
OldCodeAd::__construct public function Creates a new AdsenseAdBase instance. Overrides AdsenseAdBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.