/** * The updating function. This should create a whole configuration * object including share buttons and recommendations as well as * site name and domain to send back to us. */ public static function update() { $version = ShareaholicUtilities::get_version(); $sexybookmarks_configuration = get_option('SexyBookmarks'); $classicbookmarks_configuration = get_option('ShareaholicClassicBookmarks'); $recommendations_configuration = get_option('ShareaholicRecommendations'); $top_bar_configuration = get_option('ShareaholicTopBar'); $new_share_buttons_configuration = self::transform_sexybookmarks_configuration($sexybookmarks_configuration); $new_classicbookmarks_locations = self::transform_classicbookmarks_locations($classicbookmarks_configuration, $sexybookmarks_configuration); $new_top_bar_configuration = self::transform_top_bar_configuration($top_bar_configuration); $location_names = array_map(array('self', 'grab_location_iterator'), $new_share_buttons_configuration['locations_attributes']); // if sexybookmarks are off or not on the bottom if ($sexybookmarks_configuration['sexybookmark'] != '1' || !(bool) preg_grep('/below/', $location_names)) { // then merge in the classic bookmark locations $new_share_buttons_configuration = array_merge($new_share_buttons_configuration, $new_classicbookmarks_locations); } elseif ($sexybookmarks_configuration['sexybookmark'] != '1' || !(bool) preg_grep('/above/', $location_names)) { $new_share_buttons_configuration = array_merge($new_share_buttons_configuration, $new_top_bar_configuration); } $new_recommendations_configuration = array('locations_attributes' => self::transform_recommendations_configuration($recommendations_configuration)); $new_recommendations_configuration = isset($new_recommendations_configuration) ? $new_recommendations_configuration : null; $verification_key = md5(mt_rand()); list($turned_on_share_buttons_location_names, $turned_off_share_buttons_location_names) = self::pad_locations($new_share_buttons_configuration); list($turned_on_recommendations_location_names, $turned_off_recommendations_location_names) = self::pad_locations($new_recommendations_configuration); $new_configuration = array('configuration_publisher' => array('share_buttons_attributes' => $new_share_buttons_configuration, 'recommendations_attributes' => $new_recommendations_configuration, 'site_name' => ShareaholicUtilities::site_name(), 'domain' => ShareaholicUtilities::site_url(), 'verification_key' => $verification_key, 'platform_id' => '12', 'language_id' => ShareaholicUtilities::site_language())); $shortener_configuration = isset($sexybookmarks_configuration['shorty']) ? self::transform_shortener_configuration($sexybookmarks_configuration) : array(); $new_configuration['configuration_publisher'] = array_merge($new_configuration['configuration_publisher'], $shortener_configuration); $response = ShareaholicCurl::post(Shareaholic::API_URL . '/publisher_tools/anonymous', $new_configuration, 'json'); if ($response && preg_match('/20*/', $response['response']['code'])) { ShareaholicUtilities::log_event('6To7ConversionSuccess', array('the_posted_json' => $new_configuration, 'the_created_api_key' => $response['body']['api_key'], 'SexyBookmarks' => $sexybookmarks_configuration, 'ShareaholicClassicBookmarks' => $classicbookmarks_configuration, 'ShareaholicRecommendations' => $recommendations_configuration)); ShareaholicUtilities::update_options(array('api_key' => $response['body']['api_key'], 'version' => Shareaholic::VERSION, 'verification_key' => $verification_key, 'location_name_ids' => $response['body']['location_name_ids'])); ShareaholicUtilities::turn_on_locations(array('share_buttons' => array_flip($turned_on_share_buttons_location_names), 'recommendations' => array_flip($turned_on_recommendations_location_names)), array('share_buttons' => array_flip($turned_off_share_buttons_location_names), 'recommendations' => array_flip($turned_off_recommendations_location_names))); self::transform_wordpress_specific_settings(); self::cleanup_v6_options(); } else { ShareaholicUtilities::log_event('6To7ConversionFailed', array('the_posted_json' => $new_configuration, 'SexyBookmarks' => $sexybookmarks_configuration, 'ShareaholicClassicBookmarks' => $classicbookmarks_configuration, 'ShareaholicRecommendations' => $recommendations_configuration)); } }
/** * Given an object, set the default on/off locations * for share buttons and recommendations * */ public static function set_default_location_settings($location_name_ids) { $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(); $turned_on_share_buttons_keys = array(); foreach ($turned_on_share_buttons_locations as $loc) { $turned_on_share_buttons_keys[] = $loc['name']; } $turned_on_recommendations_keys = array(); foreach ($turned_on_recommendations_locations as $loc) { $turned_on_recommendations_keys[] = $loc['name']; } $turned_off_share_buttons_keys = array(); foreach ($turned_off_share_buttons_locations as $loc) { $turned_off_share_buttons_keys[] = $loc['name']; } $turned_off_recommendations_keys = array(); foreach ($turned_off_recommendations_locations as $loc) { $turned_off_recommendations_keys[] = $loc['name']; } $turn_on = array('share_buttons' => self::associative_array_slice($location_name_ids['share_buttons'], $turned_on_share_buttons_keys), 'recommendations' => self::associative_array_slice($location_name_ids['recommendations'], $turned_on_recommendations_keys)); $turn_off = array('share_buttons' => self::associative_array_slice($location_name_ids['share_buttons'], $turned_off_share_buttons_keys), 'recommendations' => self::associative_array_slice($location_name_ids['recommendations'], $turned_off_recommendations_keys)); ShareaholicUtilities::turn_on_locations($turn_on, $turn_off); }
/** * 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 = array(array('name' => 'post_below_content', 'counter' => 'badge-counter'), array('name' => 'page_below_content', 'counter' => 'badge-counter'), array('name' => 'index_below_content', 'counter' => 'badge-counter'), array('name' => 'category_below_content', 'counter' => 'badge-counter')); $turned_off_share_buttons_locations = array(array('name' => 'post_above_content', 'counter' => 'badge-counter'), array('name' => 'page_above_content', 'counter' => 'badge-counter'), array('name' => 'index_above_content', 'counter' => 'badge-counter'), array('name' => 'category_above_content', 'counter' => 'badge-counter')); $turned_on_recommendations_locations = array(array('name' => 'post_below_content'), array('name' => 'page_below_content')); $turned_off_recommendations_locations = array(array('name' => 'index_below_content'), array('name' => 'category_below_content')); $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'])) { $turned_on_share_buttons_keys = array(); foreach ($turned_on_share_buttons_locations as $loc) { $turned_on_share_buttons_keys[] = $loc['name']; } $turned_on_recommendations_keys = array(); foreach ($turned_on_recommendations_locations as $loc) { $turned_on_recommendations_keys[] = $loc['name']; } $turned_off_share_buttons_keys = array(); foreach ($turned_off_share_buttons_locations as $loc) { $turned_off_share_buttons_keys[] = $loc['name']; } $turned_off_recommendations_keys = array(); foreach ($turned_off_recommendations_locations as $loc) { $turned_off_recommendations_keys[] = $loc['name']; } $turn_on = array('share_buttons' => self::associative_array_slice($response['body']['location_name_ids']['share_buttons'], $turned_on_share_buttons_keys), 'recommendations' => self::associative_array_slice($response['body']['location_name_ids']['recommendations'], $turned_on_recommendations_keys)); $turn_off = array('share_buttons' => self::associative_array_slice($response['body']['location_name_ids']['share_buttons'], $turned_off_share_buttons_keys), 'recommendations' => self::associative_array_slice($response['body']['location_name_ids']['recommendations'], $turned_off_recommendations_keys)); ShareaholicUtilities::turn_on_locations($turn_on, $turn_off); 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(); } }
/** * 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 string */ public static function get_or_create_api_key() { $api_key = self::get_option('api_key'); if ($api_key) { 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 = array(); $turned_off_recommendations_locations = array(); $turned_on_share_buttons_locations = array(); $turned_off_share_buttons_locations = array(); foreach ($page_types as $key => $page_type) { $page_type_name = $page_type->type; if ($page_type_name === 'article' || $page_type_name === 'page') { $turned_on_recommendations_locations[] = array('name' => $page_type_name . '_below_content'); } else { $turned_off_recommendations_locations[] = array('name' => $page_type_name . '_below_content'); } $turned_on_share_buttons_locations[] = array('name' => $page_type_name . '_below_content'); $turned_off_share_buttons_locations[] = array('name' => $page_type_name . '_above_content'); } $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/x-www-form-urlencoded'), 'data' => http_build_query($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'])) { $turned_on_recommendations_keys = array(); foreach ($turned_on_recommendations_locations as $loc) { $turned_on_recommendations_keys[] = $loc['name']; } $turned_on_share_buttons_keys = array(); foreach ($turned_on_share_buttons_locations as $loc) { $turned_on_share_buttons_keys[] = $loc['name']; } $turned_off_recommendations_keys = array(); foreach ($turned_off_recommendations_locations as $loc) { $turned_off_recommendations_keys[] = $loc['name']; } $turned_off_share_buttons_keys = array(); foreach ($turned_off_share_buttons_locations as $loc) { $turned_off_share_buttons_keys[] = $loc['name']; } $turn_on = array('share_buttons' => self::associative_array_slice($json_response['location_name_ids']['share_buttons'], $turned_on_share_buttons_keys), 'recommendations' => self::associative_array_slice($json_response['location_name_ids']['recommendations'], $turned_on_recommendations_keys)); $turn_off = array('share_buttons' => self::associative_array_slice($json_response['location_name_ids']['share_buttons'], $turned_off_share_buttons_keys), 'recommendations' => self::associative_array_slice($json_response['location_name_ids']['recommendations'], $turned_off_recommendations_keys)); ShareaholicUtilities::turn_on_locations($turn_on, $turn_off); ShareaholicContentManager::single_domain_worker(); } else { ShareaholicUtilities::log_event('FailedToCreateApiKey', array('reason' => 'no location name ids the response was: ' . $response['data'])); } }