/** * Submit handler for the shareaholic_apps_configuration form * When submitted, update the location settings * */ function shareaholic_apps_configuration_form_submit($form, &$form_state) { $settings = ShareaholicUtilities::get_settings(); if (empty($settings['recommendations']) || empty($settings['share_buttons'])) { return; } $form_input = $form_state['input']; $page_types = ShareaholicUtilities::page_types(); foreach ($page_types as $key => $page_type) { foreach (array('share_buttons', 'recommendations') as $app) { foreach (array('above', 'below') as $location) { $location_name = "{$page_type->type}_{$location}_content"; if ($location === 'above' && $app === 'recommendations') { continue; } if (!isset($form_input[$app][$location_name]) || !isset($form_input[$app]["{$location_name}_location_id"]) || $form_input[$app][$location_name] !== 'on') { $settings[$app][$location_name] = 'off'; } else { $settings[$app][$location_name] = 'on'; $settings['location_name_ids'][$app][$location_name] = $form_input[$app]["{$location_name}_location_id"]; } } } } ShareaholicUtilities::set_settings($settings); ShareaholicUtilities::log_event('UpdatedSettings'); drupal_set_message(t('Shareaholic Settings Saved'), 'status'); }
private static function send_with_drupal($url, $options, $ignore_error) { ShareaholicUtilities::log($url); ShareaholicUtilities::log($options); ShareaholicUtilities::log('-----------------'); $request = array(); $result = array(); $request['method'] = isset($options['method']) ? $options['method'] : 'GET'; $request['headers'] = isset($options['headers']) ? $options['headers'] : array(); $request['max_redirects'] = isset($options['redirection']) ? $options['redirection'] : 5; $request['timeout'] = isset($options['timeout']) ? $options['timeout'] : 15; $request['headers']['User-Agent'] = isset($options['user-agent']) ? $options['user-agent'] : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0'; if (isset($options['body'])) { if (isset($request['headers']['Content-Type']) && $request['headers']['Content-Type'] === 'application/json') { $request['data'] = json_encode($options['body']); } else { $request['data'] = http_build_query($options['body']); } } else { $request['body'] = NULL; } $response = drupal_http_request($url, $request); if (isset($response->error)) { ShareaholicUtilities::log($response->error); if (!$ignore_error) { ShareaholicUtilities::log_event('CurlRequestFailure', array('error_message' => $response->error, 'url' => $url)); } return false; } $result['headers'] = $response->headers; $result['body'] = $response->data; $result['response'] = array('code' => $response->code, 'message' => $response->status_message); return $result; }
private static function send_with_wp($url, $options, $ignore_error) { $request = array(); $result = array(); $request['method'] = isset($options['method']) ? $options['method'] : 'GET'; $request['headers'] = isset($options['headers']) ? $options['headers'] : array(); $request['redirection'] = isset($options['redirection']) ? $options['redirection'] : 5; $request['timeout'] = isset($options['timeout']) ? $options['timeout'] : 15; $request['user-agent'] = isset($options['user-agent']) ? $options['user-agent'] : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0'; if (isset($options['body'])) { if (isset($request['headers']['Content-Type']) && $request['headers']['Content-Type'] === 'application/json') { $request['body'] = json_encode($options['body']); } else { $request['body'] = $options['body']; } } else { $request['body'] = NULL; } $request['sslverify'] = false; $response = wp_remote_request($url, $request); if (is_wp_error($response)) { $error_message = $response->get_error_message(); ShareaholicUtilities::log($error_message); if (!$ignore_error) { ShareaholicUtilities::log_event('HttpRequestFailure', array('error_message' => $error_message, 'url' => $url)); } return false; } return $response; }
/** * 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)); } }
/** * Sends an event when the user has updated * the Drupal module */ public static function update_check() { $version = ShareaholicUtilities::get_option('version'); if (ShareaholicUtilities::get_option('api_key') && $version != ShareaholicUtilities::get_version()) { ShareaholicUtilities::set_version(ShareaholicUtilities::get_version()); ShareaholicUtilities::log_event('Upgrade', array('previous_plugin_version' => $version)); } }
/** * * Performs a request using the methods built into WordPress, which account for * various PHP eccenctricities. * * @param string $url * @param array $data an associative array of the data * @param string $data_type either an empty string or 'json' * @param bool $ignore_error whether to log a networking error * @param string $method the HTTP verb to be used * * @return mixed the returned data json decoded or false */ private static function send_request_with_wp($url, $data, $data_type, $ignore_error, $method) { ShareaholicUtilities::log($url); ShareaholicUtilities::log($data); ShareaholicUtilities::log($data_type); ShareaholicUtilities::log($method); ShareaholicUtilities::log('-----------------'); $timeout = 15; $useragent = 'WordPress/' . get_bloginfo('version') . '; ' . 'PHP/' . phpversion() . '; ' . 'SHR_WP/' . Shareaholic::VERSION . '; ' . get_bloginfo('url'); if ($method == 'GET') { $response = wp_remote_get($url, array('body' => $data, 'sslverify' => false, 'user-agent' => $useragent, 'timeout' => $timeout)); } elseif ($method == 'POST') { $request = array(); if ($data_type == 'json') { $request['headers'] = array('Content-Type' => 'application/json'); $request['body'] = json_encode($data); } else { $request['body'] = $data; } $request['headers']['Accept'] = 'application/json'; $request['sslverify'] = false; $request['timeout'] = $timeout; $response = wp_remote_post($url, $request); } if (is_wp_error($response)) { $error_message = $response->get_error_message(); ShareaholicUtilities::log($error_message); if (!$ignore_error) { ShareaholicUtilities::log_event('CurlRequestFailure', array('error_message' => $error_message, 'url' => $url)); } return false; } else { if (is_array($response) && array_key_exists('body', $response)) { $body = $response['body']; $response['body'] = ShareaholicUtilities::object_to_array(json_decode($body)) ? ShareaholicUtilities::object_to_array(json_decode($body)) : $body; return $response; } } }
/** * This function fires when the plugin is uninstalled. */ public function uninstall() { ShareaholicUtilities::log_event("Uninstall"); delete_option('shareaholic_settings'); }
/** * Log reasons for a failure of a response. * * Checks if the code is not a 20*, the response body * is not an array, and whether the response object * was false. Sends the appropriate logging message. * * @param string $name the name of the event to log * @param mixed $response the response object */ public static function log_bad_response($name, $response) { if ($response && is_array($response) && !preg_match('/20*/', $response['response']['code'])) { ShareaholicUtilities::log_event($name, array('reason' => 'the response was a ' . $response['response']['code'])); } elseif ($response && !is_array($response)) { $thing = preg_replace('/\\n/', '', var_export($response, true)); ShareaholicUtilities::log_event($name, array('reason' => 'the publisher configuration was not an array, it was this ' . $thing)); } }
/** * 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)); }
/** * Checks bad response and logs errors if any * * @return boolean */ public static function has_bad_response($response, $type, $json_parse = FALSE) { if (!$response) { ShareaholicUtilities::log_event($type, array('reason' => 'there was no response')); return true; } $response = (array) $response; if (isset($response['error'])) { $error_message = print_r($response['error'], true); ShareaholicUtilities::log_event($type, array('reason' => 'there was an error: ' . $error_message)); return true; } if (!preg_match('/20*/', $response['code'])) { ShareaholicUtilities::log_event($type, array('reason' => 'the server responded with code ' . $response['code'])); return true; } if ($json_parse && json_decode($response['data']) === NULL) { ShareaholicUtilities::log_event($type, array('reason' => 'could not parse JSON. The response was: ' . $response['data'])); return true; } return false; }