You are here

public static function ShareaholicUtilities::get_or_create_api_key in Share Buttons, Related Posts, Content Analytics - Shareaholic 8

Same name and namespace in other branches
  1. 7.3 utilities.php \ShareaholicUtilities::get_or_create_api_key()

Returns the api key or creates a new one.

It first checks the database. If the key is not found (or is an empty string or empty array or anything that evaluates to false) then we will attempt to make a new one by POSTing to the anonymous configuration endpoint

Return value

string

3 calls to ShareaholicUtilities::get_or_create_api_key()
shareaholic_failure_modal_form_submit in includes/shareaholic_failure_modal.php
Submit handler for the shareaholic_failure_modal When submitted, try to create an api key for the user
shareaholic_install in ./shareaholic.install
Implements hook_install(). When the user installs the module, log the event
shareaholic_tos_modal_form_submit in includes/shareaholic_tos_modal.php
Submit handler for the ToS modal: update values in the database By storing 'true' for shareaholic_has_accepted_tos and get or create an api key if it does not already exists

File

./utilities.php, line 179

Class

ShareaholicUtilities

Code

public static function get_or_create_api_key() {
  $api_key = self::get_option('api_key');

  // ensure api key set is atleast 30 characters, if not, retry to set new api key
  if ($api_key && strlen($api_key) > 30) {
    return $api_key;
  }

  // destroy the shareaholic settings except certain flags
  $old_settings = self::get_settings();
  self::destroy_settings();

  // restore any old settings that should be preserved between resets
  if (isset($old_settings['share_counts_connect_check'])) {
    self::update_options(array(
      'share_counts_connect_check' => $old_settings['share_counts_connect_check'],
    ));
  }
  $verification_key = md5(mt_rand());
  $page_types = self::page_types();
  $turned_on_recommendations_locations = self::get_default_rec_on_locations();
  $turned_off_recommendations_locations = self::get_default_rec_off_locations();
  $turned_on_share_buttons_locations = self::get_default_sb_on_locations();
  $turned_off_share_buttons_locations = self::get_default_sb_off_locations();
  $share_buttons_attributes = array_merge($turned_on_share_buttons_locations, $turned_off_share_buttons_locations);
  $recommendations_attributes = array_merge($turned_on_recommendations_locations, $turned_off_recommendations_locations);
  $post_data = array(
    'configuration_publisher' => array(
      'verification_key' => $verification_key,
      'site_name' => self::site_name(),
      'domain' => self::site_url(),
      'platform_id' => '2',
      'language_id' => self::site_language(),
      'shortener' => 'shrlc',
      'recommendations_attributes' => array(
        'locations_attributes' => $recommendations_attributes,
      ),
      'share_buttons_attributes' => array(
        'locations_attributes' => $share_buttons_attributes,
      ),
    ),
  );
  $response = drupal_http_request(self::API_URL . '/publisher_tools/anonymous', array(
    'method' => 'POST',
    'headers' => array(
      'Content-Type' => 'application/json',
    ),
    'data' => json_encode($post_data),
  ));
  if (self::has_bad_response($response, 'FailedToCreateApiKey', true)) {
    return NULL;
  }
  $response = (array) $response;
  $json_response = json_decode($response['data'], true);
  self::update_options(array(
    'version' => self::get_version(),
    'api_key' => $json_response['api_key'],
    'verification_key' => $verification_key,
    'location_name_ids' => $json_response['location_name_ids'],
  ));
  if (isset($json_response['location_name_ids']) && is_array($json_response['location_name_ids']) && isset($json_response['location_name_ids']['recommendations']) && isset($json_response['location_name_ids']['share_buttons'])) {
    self::set_default_location_settings($json_response['location_name_ids']);
    ShareaholicContentManager::single_domain_worker();
  }
  else {
    ShareaholicUtilities::log_event('FailedToCreateApiKey', array(
      'reason' => 'no location name ids the response was: ' . $response['data'],
    ));
  }
}