Exemple #1
0
 /**
  * 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. That action
  * is wrapped in a mutex to keep two requests from
  * trying to create new api keys at the same time.
  *
  * @return string
  */
 public static function get_or_create_api_key()
 {
     $api_key = self::get_option('api_key');
     if ($api_key) {
         return $api_key;
     }
     if (!self::is_locked('get_or_create_api_key')) {
         self::set_lock('get_or_create_api_key');
         $old_settings = self::get_settings();
         delete_option('shareaholic_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());
         $turned_on_share_buttons_locations = self::get_default_sb_on_locations();
         $turned_off_share_buttons_locations = self::get_default_sb_off_locations();
         $turned_on_recommendations_locations = self::get_default_rec_on_locations();
         $turned_off_recommendations_locations = self::get_default_rec_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);
         $data = array('configuration_publisher' => array('verification_key' => $verification_key, 'site_name' => self::site_name(), 'domain' => self::site_url(), 'platform_id' => '12', 'language_id' => self::site_language(), 'shortener' => 'shrlc', 'recommendations_attributes' => array('locations_attributes' => $recommendations_attributes), 'share_buttons_attributes' => array('locations_attributes' => $share_buttons_attributes)));
         $response = ShareaholicCurl::post(Shareaholic::API_URL . '/publisher_tools/anonymous', $data, 'json');
         if ($response && preg_match('/20*/', $response['response']['code'])) {
             self::update_options(array('api_key' => $response['body']['api_key'], 'verification_key' => $verification_key, 'location_name_ids' => $response['body']['location_name_ids']));
             if (isset($response['body']['location_name_ids']) && is_array($response['body']['location_name_ids'])) {
                 self::set_default_location_settings($response['body']['location_name_ids']);
                 ShareaholicUtilities::clear_cache();
             } else {
                 ShareaholicUtilities::log_bad_response('FailedToCreateApiKey', $response);
             }
         } else {
             add_action('admin_notices', array('ShareaholicAdmin', 'failed_to_create_api_key'));
             ShareaholicUtilities::log_bad_response('FailedToCreateApiKey', $response);
         }
         self::unlock('get_or_create_api_key');
     } else {
         usleep(100000);
         self::get_or_create_api_key();
     }
 }