You are here

function adsense_revenue_sharing_basic_choose_client in Google AdSense integration 8

Provide the applicable Publisher ID, based on the configured probabilities.

Parameters

string $format: Format of the ad.

Return value

string|null If format is set, returns NULL. Otherwise, based on the configured percentage, returns either the Publisher ID of the current page's author or of the owner of the site.

1 call to adsense_revenue_sharing_basic_choose_client()
adsense_revenue_sharing_basic_adsense in oldcode/revenue_sharing_basic/adsense_revenue_sharing_basic.module
Interface of the Publisher ID modules.

File

oldcode/revenue_sharing_basic/adsense_revenue_sharing_basic.module, line 72
Main file of the adsense_revenue_sharing_basic module.

Code

function adsense_revenue_sharing_basic_choose_client($format = '') {
  if (!empty($format)) {

    // This module can't handle the selection of an appropriate Slot ID
    // Give up!
    return NULL;
  }
  $user = Drupal::currentUser();
  $site_client = Drupal::config('adsense.settings')
    ->get('adsense_basic_id');
  $config = Drupal::config('adsense_revenue_sharing_basic.settings');
  $info = adsense_revenue_sharing_basic_get_node_info();
  if (empty($info) || !$config
    ->get('node_type.' . $info['type'])) {
    return $site_client;
  }
  $percents[0] = $config
    ->get('percentage_author');

  /** @var \Drupal\user\Entity\User $author_user */
  $author_user = User::load($info['uid']);
  foreach ($author_user
    ->getRoles(TRUE) as $role) {
    $percents[$role] = $config
      ->get('percentage_role.' . $role);
  }
  arsort($percents);
  $percent_author = array_shift($percents);

  // Toss the dice and see who gets their ad displayed.
  $random = mt_rand(1, 100);
  if ($random <= $percent_author) {
    $client = adsense_revenue_sharing_basic_get_profile_client_id($info['uid']);
  }
  else {
    $client = $site_client;
  }

  // Last check to see that we have a valid client.
  // Check that the current user doesn't view ads with it's own Publisher ID.
  if (!$client || $client == adsense_revenue_sharing_basic_get_profile_client_id($user
    ->id())) {
    $client = $site_client;
  }
  return $client;
}