示例#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
  *
  * @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']));
     }
 }
示例#2
0
文件: node.php 项目: reasonat/women
/**
 * Implements hook_node_insert().
 * When a node is created, notify CM to scrape its details
 * and clear FB cache
 *
 * @param $node The node that has been created
 */
function shareaholic_node_insert($node)
{
    ShareaholicContentSettings::insert($node);
    ShareaholicContentManager::single_page_worker($node);
    ShareaholicUtilities::clear_fb_opengraph($node);
}
/**
 * Implements hook_node_insert().
 * When a node is created, notify CM to scrape its details
 * and clear FB cache
 *
 * @param $node The node that has been created
 */
function shareaholic_node_insert($node)
{
    ShareaholicContentSettings::insert($node);
    ShareaholicContentManager::single_page_worker($node);
}