Ejemplo n.º 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();
     }
 }
Ejemplo n.º 2
0
 /**
  * This function fires when the plugin is deactivated.
  */
 public function deactivate()
 {
     ShareaholicUtilities::log_event("Deactivate");
     ShareaholicUtilities::clear_cache();
     ShareaholicCron::deactivate();
 }
Ejemplo n.º 3
0
 /**
  * The function for the advanced admin section
  */
 public static function advanced_admin()
 {
     $settings = ShareaholicUtilities::get_settings();
     $api_key = ShareaholicUtilities::get_or_create_api_key();
     $action = str_replace('%7E', '~', $_SERVER['REQUEST_URI']);
     if (!ShareaholicUtilities::has_accepted_terms_of_service()) {
         ShareaholicUtilities::load_template('terms_of_service_modal', array('image_url' => SHAREAHOLIC_ASSET_DIR . 'img'));
     }
     if (isset($_POST['reset_settings']) && $_POST['reset_settings'] == 'Y' && check_admin_referer($action, 'nonce_field')) {
         ShareaholicUtilities::destroy_settings();
         echo "<div class='updated settings_updated'><p><strong>" . sprintf(__('Settings successfully reset. Refresh this page to complete the reset.', 'shareaholic')) . "</strong></p></div>";
     }
     if (isset($_POST['already_submitted']) && $_POST['already_submitted'] == 'Y' && check_admin_referer($action, 'nonce_field')) {
         echo "<div class='updated settings_updated'><p><strong>" . sprintf(__('Settings successfully saved', 'shareaholic')) . "</strong></p></div>";
         foreach (array('disable_tracking', 'disable_og_tags', 'disable_admin_bar_menu', 'disable_debug_info', 'disable_internal_share_counts_api') as $setting) {
             if (isset($settings[$setting]) && !isset($_POST['shareaholic'][$setting]) && $settings[$setting] == 'on') {
                 $_POST['shareaholic'][$setting] = 'off';
             } elseif (!isset($_POST['shareaholic'][$setting])) {
                 $_POST['shareaholic'][$setting] = array();
             }
         }
         if (isset($_POST['shareaholic']['api_key']) && $_POST['shareaholic']['api_key'] != $api_key) {
             ShareaholicUtilities::get_new_location_name_ids($_POST['shareaholic']['api_key']);
         }
         if (isset($_POST['shareaholic']['api_key'])) {
             ShareaholicUtilities::update_options(array('api_key' => $_POST['shareaholic']['api_key']));
         }
         if (isset($_POST['shareaholic']['disable_tracking'])) {
             ShareaholicUtilities::update_options(array('disable_tracking' => $_POST['shareaholic']['disable_tracking']));
         }
         if (isset($_POST['shareaholic']['disable_og_tags'])) {
             ShareaholicUtilities::update_options(array('disable_og_tags' => $_POST['shareaholic']['disable_og_tags']));
         }
         if (isset($_POST['shareaholic']['disable_admin_bar_menu'])) {
             ShareaholicUtilities::update_options(array('disable_admin_bar_menu' => $_POST['shareaholic']['disable_admin_bar_menu']));
         }
         if (isset($_POST['shareaholic']['disable_debug_info'])) {
             ShareaholicUtilities::update_options(array('disable_debug_info' => $_POST['shareaholic']['disable_debug_info']));
         }
         if (isset($_POST['shareaholic']['disable_internal_share_counts_api'])) {
             ShareaholicUtilities::update_options(array('disable_internal_share_counts_api' => $_POST['shareaholic']['disable_internal_share_counts_api']));
         }
         ShareaholicUtilities::log_event("UpdatedSettings");
         // clear cache after settings update
         ShareaholicUtilities::clear_cache();
     }
     ShareaholicUtilities::load_template('advanced_settings', array('settings' => ShareaholicUtilities::get_settings(), 'action' => $action));
 }