View source
<?php
namespace Drupal\adsense;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class AdsenseAdBase extends PluginBase implements AdsenseAdInterface, ContainerFactoryPluginInterface {
use StringTranslationTrait;
protected $configFactory;
protected $moduleHandler;
protected $currentUser;
public function __construct(array $configuration, $plugin_id = '', $plugin_definition = NULL, ConfigFactoryInterface $config_factory = NULL, ModuleHandlerInterface $module_handler = NULL, AccountProxyInterface $current_user = NULL) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $config_factory ?: \Drupal::configFactory();
$this->moduleHandler = $module_handler ?: \Drupal::moduleHandler();
$this->currentUser = $current_user ?: \Drupal::currentUser();
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('config.factory'), $container
->get('module_handler'), $container
->get('current_user'));
}
public static function createAd(array $args) {
$version = 1;
$is_search = FALSE;
if (!empty($args['format']) && substr($args['format'], 0, 10) == 'Search Box') {
$is_search = TRUE;
switch ($args['format']) {
case 'Search Box':
$version = 1;
break;
case 'Search Box v2':
$version = 2;
break;
}
}
$needs_slot = !empty($args['slot']);
$manager = \Drupal::service('plugin.manager.adsensead');
$plugins = $manager
->getDefinitions();
foreach ($plugins as $plugin) {
if ($plugin['isSearch'] == $is_search && $plugin['needsSlot'] == $needs_slot && $plugin['version'] == $version) {
return $manager
->createInstance($plugin['id'], $args);
}
}
return NULL;
}
public function display(array $classes = []) {
$config = $this->configFactory
->get('adsense.settings');
$libraries = [
'adsense/adsense.css',
];
$text = '';
if ($this
->isDisabled($text)) {
return [
'#type' => 'inline_template',
'#template' => '<!-- adsense: {{ comment }} -->',
'#context' => [
'comment' => $text,
],
];
}
elseif ($config
->get('adsense_test_mode') || $this->currentUser
->hasPermission('show adsense placeholders')) {
$content = $this
->getAdPlaceholder();
return [
'#theme' => 'adsense_ad',
'#content' => $content['#content'],
'#width' => isset($content['#width']) ? $content['#width'] : NULL,
'#height' => isset($content['#height']) ? $content['#height'] : NULL,
'#format' => $content['#format'],
'#classes' => array_merge([
'adsense-placeholder',
], $classes),
'#attached' => [
'library' => $libraries,
],
];
}
else {
if ($config
->get('adsense_unblock_ads')) {
$libraries[] = 'adsense/adsense.unblock';
}
$content = $this
->getAdContent();
return [
'#theme' => 'adsense_ad',
'#content' => $content,
'#width' => isset($content['#width']) ? $content['#width'] : NULL,
'#height' => isset($content['#height']) ? $content['#height'] : NULL,
'#format' => isset($content['#format']) ? $content['#format'] : NULL,
'#classes' => $classes,
'#attached' => [
'library' => $libraries,
],
];
}
}
public static function isDisabled(&$text = '') {
$account = \Drupal::currentUser();
$config = \Drupal::config('adsense.settings');
if (!$config
->get('adsense_basic_id')) {
$text = 'no publisher id configured.';
}
elseif ($config
->get('adsense_disable')) {
$text = 'adsense disabled.';
}
elseif ($account
->id() != 1 && $account
->hasPermission('hide adsense')) {
$text = 'disabled for current user.';
}
else {
return FALSE;
}
return TRUE;
}
public static function adsenseLanguages() {
return [
'ar' => t('Arabic'),
'bg' => t('Bulgarian'),
'ca' => t('Catalan'),
'zh-Hans' => t('Chinese Simplified'),
'zh-TW' => t('Chinese Traditional'),
'hr' => t('Croatian'),
'cs' => t('Czech'),
'da' => t('Danish'),
'nl' => t('Dutch'),
'en' => t('English'),
'et' => t('Estonian'),
'fi' => t('Finnish'),
'fr' => t('French'),
'de' => t('German'),
'el' => t('Greek'),
'iw' => t('Hebrew'),
'hi' => t('Hindi'),
'hu' => t('Hungarian'),
'is' => t('Icelandic'),
'in' => t('Indonesian'),
'it' => t('Italian'),
'ja' => t('Japanese'),
'ko' => t('Korean'),
'lv' => t('Latvian'),
'lt' => t('Lithuanian'),
'no' => t('Norwegian'),
'pl' => t('Polish'),
'pt' => t('Portuguese'),
'ro' => t('Romanian'),
'ru' => t('Russian'),
'sr' => t('Serbian'),
'sk' => t('Slovak'),
'sl' => t('Slovenian'),
'es' => t('Spanish'),
'sv' => t('Swedish'),
'th' => t('Thai'),
'tr' => t('Turkish'),
'uk' => t('Ukrainian'),
'vi' => t('Vietnamese'),
];
}
}