function hm_time_foursqaure_options_input($user_id, $table_row, $input_text) { // Set Foursquare username input $foursquare_user_id = get_user_meta($user_id, 'hm_time_foursquare_user_id', true); if (empty($foursquare_user_id)) { $foursquare_link_template = '<a href="https://foursquare.com/oauth2/authenticate?client_id=%s&response_type=code&redirect_uri=%s" class="button">Link to Foursqaure</a>'; $client_id = hm_time_options('foursquare_client_id'); $redirect_uri = hm_time_options('foursquare_redirect_uri'); $foursquare_input = sprintf($foursquare_link_template, $client_id, $redirect_uri); $foursquare_desc = __('Please connect your foursqaure account so we can get sent user push notifications.'); } else { $foursquare_input = __('Your profile is already connected to an Foursquare account.'); $foursquare_desc = __('Please go to <a href="https://foursquare.com/settings/connections">Foursquare settings page</a> and disconnect the HM Time app.'); } printf($table_row, 'hm_time_foursquare_id', __('Foursquare Account'), $foursquare_input, $foursquare_desc); }
function hm_time_timezone_settings($user_id, $table_row, $input_text) { echo '<h3>' . __('Time Zone') . '</h3> <table class="form-table">'; $hm_time_set_method_array = hm_time_timezone_options(); // only need to display set method if there is more than one option if (1 < count($hm_time_set_method_array)) { $hm_time_set_method_input = '<p><label><input type="radio" name="%1$s" value="%2$s" %3$s/> %4$s</label></p>'; $hm_time_set_method_value = get_user_meta($user_id, 'hm_time_set_method', true); $hm_time_set_method = ''; foreach ($hm_time_set_method_array as $sm_value => $sm_label) { if (empty($hm_time_set_method_value)) { $hm_time_set_method_value = 'manual'; $default_set_method = hm_time_options('default_set_method'); if (!empty($default_set_method)) { $hm_time_set_method_value = $default_set_method; // set the default value } } $sm_saved = $hm_time_set_method_value === $sm_value ? 'checked=checked' : ''; $hm_time_set_method .= sprintf($hm_time_set_method_input, 'hm_time_set_method', $sm_value, $sm_saved, $sm_label); } printf($table_row, 'hm_time_set_method', __('Set method'), $hm_time_set_method, __('Please select how you want your timezone to be updated')); } else { update_user_meta($user_id, 'hm_time_set_method', 'manual'); } do_action('hm_time_add_options', $user_id, $table_row, $input_text); // Set timezone manually $hm_time_manual_value = get_user_meta($user_id, 'hm_time_timezone', true); $locations = hm_time_locations(); $hm_time_manual_inputs = ''; foreach ($locations as $lkey => $lvalue) { $hm_time_manual_inputs .= '<optgroup label="' . $lkey . '">'; if (is_array($lvalue)) { foreach ($lvalue as $value => $label) { $selected = $hm_time_manual_value == $value ? 'selected="selected"' : ''; $hm_time_manual_inputs .= '<option value="' . $value . '" ' . $selected . '>' . $label . '</option>'; } } $hm_time_manual_inputs .= '</optgroup>'; } $hm_time_manual = '<select name="hm_time_timezone">' . $hm_time_manual_inputs . '</select>'; printf($table_row, 'hm_time_timezone', __('Manual Selection'), $hm_time_manual, __('Please select your timezone')); echo '</table>'; }
/** * Callback for when a user links their profile to their foursquare account */ public function get_code($code = null) { $user_id = get_current_user_id(); $user_id = 1; // override because using vagrnt share to log in causes the site to revert back to its local url. if (empty($code)) { return; } $options = hm_time_options(); $client_id = $options['foursquare_client_id']; $client_secret = $options['foursquare_client_secret']; $push_version = $options['foursquare_push_version']; $registered_redirect_uri = site_url('/wp-json/hm-time', 'https'); $registered_redirect_uri = $options['foursquare_redirect_uri']; // debug mode $access_token_url = 'https://foursquare.com/oauth2/access_token?client_id=' . $client_id . '&client_secret=' . $client_secret . '&grant_type=authorization_code&redirect_uri=' . $registered_redirect_uri . '&code=' . $code; $access_token_json = wp_remote_get($access_token_url); $access_token_decoded = json_decode($access_token_json['body']); if ($access_token_decoded->error) { return $access_token_decoded->error; // Need to send back an error to the user saying that foursquare auth failed. } $access_token = $access_token_decoded->access_token; if (is_string($access_token)) { if (is_int($user_id) && 0 != $user_id) { update_user_meta($user_id, 'hm_time_foursquare_access_token', $access_token); } } // get user details $user_details_url = 'https://api.foursquare.com/v2/users/self?oauth_token=' . $access_token . '&v=' . $push_version; $user_details_json = wp_remote_get($user_details_url); $user_details_decoded = json_decode($user_details_json['body']); if ($user_details_decoded->error) { return 'Error with user details'; // Need to send back an error to the user saying that foursquare auth failed. } // store foursquare user id update_user_meta($user_id, 'hm_time_foursquare_user_id', $user_details_decoded->response->user->id); $response = json_ensure_response('success'); $response->set_status(201); $response->header('Location', $registered_redirect_uri); }
function hm_time_geoip_lookup($hostname = null) { if (empty($hostname)) { if (!empty($_SERVER['HTTP_CLIENT_IP'])) { $hostname = $_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { //to check ip is pass from proxy $hostname = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $hostname = $_SERVER['REMOTE_ADDR']; } } $maxmind_user_id = hm_time_options('geoip_user_id', 'string'); $maxmind_user_id = (int) $maxmind_user_id; $maxmind_license_key = hm_time_options('geoip_license_key'); $client = new Client($maxmind_user_id, $maxmind_license_key); try { $record = $client->omni($hostname); } catch (GeoIp2\Exception\AddressNotFoundException $e) { return false; } return $record; }
public function default_set_method_input() { $set_method_array = hm_time_timezone_options(); $set_method = ''; // only need to display set method if there is more than one option if (1 < count($set_method_array)) { $set_method_input = '<p><label><input type="radio" name="%1$s" value="%2$s" %3$s/> %4$s</label></p>'; $global_default_method = hm_time_options('default_set_method'); $stored_default_method = 'manual'; if (!empty($global_default_method)) { $stored_default_method = $global_default_method; } foreach ($set_method_array as $sm_value => $sm_label) { $sm_saved = $stored_default_method === $sm_value ? 'checked=checked' : ''; $set_method .= sprintf($set_method_input, 'hm_time_options[default_set_method]', $sm_value, $sm_saved, $sm_label); } $set_method .= '<span class="description">Please select how you want your timezone to be updated</span>'; } else { $set_method = '<p>Only method currently is <strong>Manual</strong>. Please insert Foursqaure app or GeoIP api details to have more options.</p>'; } echo $set_method; }