Changeset 527409
- Timestamp:
- 04/04/2012 06:26:20 PM (14 years ago)
- Location:
- janrain-capture/trunk
- Files:
-
- 3 edited
-
janrain_capture.php (modified) (7 diffs)
-
janrain_capture_admin.php (modified) (14 diffs)
-
janrain_capture_ui.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
janrain-capture/trunk/janrain_capture.php
r525225 r527409 7 7 Plugin URI: http://www.janrain.com/ 8 8 Description: Collect, store and leverage user profile data from social networks in a flexible, lightweight hosted database. 9 Version: 0.0. 19 Version: 0.0.2 10 10 Author: Janrain 11 11 Author URI: http://www.janrain.com/ … … 24 24 */ 25 25 function init() { 26 $this->path = dirname(__FILE__);26 $this->path = plugin_dir_path(__FILE__); 27 27 $this->name = 'janrain_capture'; 28 28 $this->url = WP_PLUGIN_URL.'/janrain-capture'; 29 29 30 register_activation_hook(__FILE__, array(&$this, 'activate')); 30 31 require_once $this->path . '/janrain_capture_api.php'; 31 require_once $this->path . '/janrain_capture_ui.php';32 32 33 33 if (is_admin()) { … … 48 48 add_shortcode($this->name, array(&$this, 'shortcode')); 49 49 } 50 51 $ui = new JanrainCaptureUi($this->name); 50 if (get_option($this->name . '_ui_enabled') != '0') { 51 require_once $this->path . '/janrain_capture_ui.php'; 52 $ui = new JanrainCaptureUi($this->name); 53 } 54 } 55 56 /** 57 * Method bound to register_activation_hook. 58 */ 59 function activate() { 60 require_once plugin_dir_path(__FILE__) . '/janrain_capture_admin.php'; 61 $admin = new JanrainCaptureAdmin($this->name); 62 $admin->activate(); 52 63 } 53 64 … … 75 86 // Lookup user based on returned uuid 76 87 $exists = get_users(array('blog_id'=>$GLOBALS['blog_id'], 'meta_key' => $this->name . '_uuid', 'meta_value' => $user_entity['uuid'])); 77 // TODO: username_exists($user_entity['displayName']);78 88 if (count($exists)<1) { 79 $random_password = wp_generate_password($length=12, $include_standard_special_chars=false); 80 $user_id = wp_create_user($user_entity['displayName'], $random_password, $user_entity['email']); 81 if (!add_user_meta($user_id, $this->name . '_uuid', $user_entity['uuid'], true)) { 89 $user_attrs = array(); 90 $user_attrs['user_pass'] = wp_generate_password($length=12, $include_standard_special_chars=false); 91 if (get_option($this->name . '_user_email')) 92 $user_attrs['user_email'] = esc_sql($this->get_field(get_option($this->name . '_user_email'), $user_entity)); 93 if (get_option($this->name . '_user_login')) 94 $user_attrs['user_login'] = esc_sql($this->get_field(get_option($this->name . '_user_login'), $user_entity)); 95 if (get_option($this->name . '_user_nicename')) 96 $user_attrs['user_nicename'] = esc_sql($this->get_field(get_option($this->name . '_user_nicename'), $user_entity)); 97 if (get_option($this->name . '_user_display_name')) 98 $user_attrs['display_name'] = esc_sql($this->get_field(get_option($this->name . '_user_display_name'), $user_entity)); 99 $user_id = wp_insert_user($user_attrs); 100 if (!$user_id) 101 throw new Exception('Janrain Capture: Failed to create new user'); 102 if (!add_user_meta($user_id, $this->name . '_uuid', $user_entity['uuid'], true)) 82 103 throw new Exception('Janrain Capture: Failed to set uuid on new user'); 83 } 104 if (!$this->update_user_data($user_id, $user_entity, true)) 105 throw new Exception('Janrain Capture: Failed to update user data'); 84 106 } else { 85 107 $user = $exists[0]; 86 108 $user_id = $user->ID; 109 if (!$this->update_user_data($user_id, $user_entity)) 110 throw new Exception('Janrain Capture: Failed to update user data'); 87 111 } 88 112 if (!$api->update_user_meta($user_id)) … … 160 184 */ 161 185 function profile_update() { 186 $current_user = wp_get_current_user(); 187 if (!$current_user->ID) 188 throw new Exception('Janrain Capture: Must be logged in to update profile'); 189 190 $user_id = $current_user->ID; 162 191 $api = new JanrainCaptureApi($this->name); 163 192 $user_entity = $api->load_user_entity(); 164 193 if (is_array($user_entity)) { 165 if (!$api->update_user_meta( ))194 if (!$api->update_user_meta($user_id)) 166 195 throw new Exception('Janrain Capture: Failed to update user meta'); 167 196 $user_entity = $user_entity['result']; 168 197 do_action($this->name . '_user_entity_loaded', $user_entity); 169 $exists = get_users(array('blog_id'=>$GLOBALS['blog_id'], 'meta_key' => $this->name . '_uuid', 'meta_value' => $user_entity['uuid'])); 170 if (count($exists)>0) { 171 $user = $exists[0]; 172 wp_update_user(array( 173 'ID' => $user->ID, 174 'user_nicename' => $user_entity['displayName'], 175 'display_name' => $user_entity['displayName'], 176 'user_email' => $user_entity['email'] 177 )); 178 } else { 179 throw new Exception('Janrain Capture: Could not update - user does not exist'); 180 } 198 if (!$this->update_user_data($user_id, $user_entity)) 199 throw new Exception('Janrain Capture: Failed to update user data'); 181 200 } else { 182 201 throw new Exception('Janrain Capture: Could not retrieve user entity'); … … 184 203 echo '1'; 185 204 die(); 205 } 206 207 /** 208 * Method used for updating user data with returned Capture user data 209 * 210 * @param int $user_id 211 * The ID of the user to update 212 * @param array $user_entity 213 * The user entity returned from Capture 214 * @return boolean 215 * Success or failure 216 */ 217 function update_user_data($user_id, $user_entity, $meta_only=false) { 218 if (!$user_id || !is_array($user_entity)) 219 throw new Exception('Janrain Capture: Not a valid User ID or User Entity'); 220 221 $results = array(); 222 if ($meta_only !== true) { 223 $user_attrs = array('ID' => $user_id); 224 if (get_option($this->name . '_user_email')) 225 $user_attrs['user_email'] = esc_sql($this->get_field(get_option($this->name . '_user_email'), $user_entity)); 226 if (get_option($this->name . '_user_nicename')) 227 $user_attrs['user_nicename'] = esc_sql($this->get_field(get_option($this->name . '_user_nicename'), $user_entity)); 228 if (get_option($this->name . '_user_display_name')) 229 $user_attrs['display_name'] = esc_sql($this->get_field(get_option($this->name . '_user_display_name'), $user_entity)); 230 $userdata = wp_update_user($user_attrs); 231 $results[] = ($userdata->ID > 0); 232 } 233 234 $metas = array('first_name', 'last_name', 'url', 'aim', 'yim', 'jabber', 'description'); 235 foreach($metas as $meta) { 236 $key = get_option($this->name . '_user_' . $meta); 237 if (!empty($key)) { 238 $val = $this->get_field($key, $user_entity); 239 if (!empty($val)) 240 $results[] = update_user_meta($user_id, $meta, $val); 241 } 242 } 243 return !array_search(false, $results); 244 } 245 246 /** 247 * Method used for retrieving a field value 248 * 249 * @param string $name 250 * The name of the field to retrieve 251 * @param array $user_entity 252 * The user entity returned from Capture 253 * @return string 254 * Value retrieved from Capture 255 */ 256 function get_field($name, $user_entity) { 257 if (strpos($name, '.')) { 258 $names = explode('.', $name); 259 $value = $user_entity; 260 foreach ($names as $n) { 261 $value = $value[$n]; 262 } 263 return $value; 264 } 265 else { 266 return $user_entity[$name]; 267 } 186 268 } 187 269 … … 193 275 function refresh_token() { 194 276 $api = new JanrainCaptureApi($this->name); 195 echo $api->refresh_access_token() ? '1' : '-1';277 echo ($api->refresh_access_token() && $api->update_user_meta()) ? '1' : '-1'; 196 278 die(); 197 279 } -
janrain-capture/trunk/janrain_capture_admin.php
r525225 r527409 23 23 $this->postMessage = array('class'=>'', 'message'=>''); 24 24 $this->fields = array( 25 // Main Screen Fields 25 26 array( 26 27 'name' => $this->name . '_main_options', … … 33 34 'title' => 'Application Domain', 34 35 'description' => 'Your Capture application domain (e.g. demo.janraincapture.com)', 36 'required' => true, 35 37 'default' => '', 36 38 'type' => 'text', … … 42 44 'title' => 'API Client ID', 43 45 'description' => 'Your Capture Client ID', 46 'required' => true, 44 47 'default' => '', 45 48 'type' => 'text', … … 51 54 'title' => 'API Client Secret', 52 55 'description' => 'Your Capture Client Secret', 56 'required' => true, 53 57 'default' => '', 54 58 'type' => 'text', … … 97 101 'screen' => 'options', 98 102 'validate' => '/[^a-z0-9\.:\/\&\?\=\%]+/i' 103 ), 104 105 // Data Mapping Screen Fields 106 array( 107 'name' => $this->name . '_standard_fields', 108 'title' => 'Standard WordPress User Fields', 109 'type' => 'title', 110 'screen' => 'data' 111 ), 112 array( 113 'name' => $this->name . '_user_email', 114 'title' => 'Email', 115 'required' => true, 116 'default' => 'email', 117 'type' => 'text', 118 'screen' => 'data', 119 'validate' => '/[^a-z0-9\._-]+/i' 120 ), 121 array( 122 'name' => $this->name . '_user_login', 123 'title' => 'Username', 124 'description' => 'Usernames cannot be changed.', 125 'required' => true, 126 'default' => 'uuid', 127 'type' => 'text', 128 'screen' => 'data', 129 'validate' => '/[^a-z0-9\._-]+/i' 130 ), 131 array( 132 'name' => $this->name . '_user_nicename', 133 'title' => 'Nickname', 134 'required' => true, 135 'default' => 'displayName', 136 'type' => 'text', 137 'screen' => 'data', 138 'validate' => '/[^a-z0-9\._-]+/i' 139 ), 140 array( 141 'name' => $this->name . '_user_display_name', 142 'title' => 'Display Name', 143 'required' => true, 144 'default' => 'displayName', 145 'type' => 'text', 146 'screen' => 'data', 147 'validate' => '/[^a-z0-9\._-]+/i' 148 ), 149 array( 150 'name' => $this->name . '_optional_fields', 151 'title' => 'Optional User Fields', 152 'type' => 'title', 153 'screen' => 'data' 154 ), 155 array( 156 'name' => $this->name . '_user_first_name', 157 'title' => 'First Name', 158 'default' => 'givenName', 159 'type' => 'text', 160 'screen' => 'data', 161 'validate' => '/[^a-z0-9\._-]+/i' 162 ), 163 array( 164 'name' => $this->name . '_user_last_name', 165 'title' => 'Last Name', 166 'default' => 'familyName', 167 'type' => 'text', 168 'screen' => 'data', 169 'validate' => '/[^a-z0-9\._-]+/i' 170 ), 171 array( 172 'name' => $this->name . '_user_url', 173 'title' => 'Website', 174 'default' => '', 175 'type' => 'text', 176 'screen' => 'data', 177 'validate' => '/[^a-z0-9\._-]+/i' 178 ), 179 array( 180 'name' => $this->name . '_user_aim', 181 'title' => 'AIM', 182 'default' => '', 183 'type' => 'text', 184 'screen' => 'data', 185 'validate' => '/[^a-z0-9\._-]+/i' 186 ), 187 array( 188 'name' => $this->name . '_user_yim', 189 'title' => 'Yahoo IM', 190 'default' => '', 191 'type' => 'text', 192 'screen' => 'data', 193 'validate' => '/[^a-z0-9\._-]+/i' 194 ), 195 array( 196 'name' => $this->name . '_user_jabber', 197 'title' => 'Jabber / Google Talk', 198 'default' => '', 199 'type' => 'text', 200 'screen' => 'data', 201 'validate' => '/[^a-z0-9\._-]+/i' 202 ), 203 array( 204 'name' => $this->name . '_user_description', 205 'title' => 'Biographical Info', 206 'default' => 'aboutMe', 207 'type' => 'text', 208 'screen' => 'data', 209 'validate' => '/[^a-z0-9\._-]+/i' 210 ), 211 212 // UI Screen Fields 213 array( 214 'name' => $this->name . '_ui_enabled', 215 'title' => 'Enable UI Features', 216 'description' => 'You can disable all UI features if you prefer to write your own', 217 'default' => '1', 218 'type' => 'checkbox', 219 'screen' => 'ui', 220 'validate' => '/[^0-9]+/i' 221 ), 222 array( 223 'name' => $this->name . '_ui_options', 224 'title' => 'Other Options', 225 'type' => 'title', 226 'screen' => 'ui' 227 ), 228 array( 229 'name' => $this->name . '_ui_native_links', 230 'title' => 'Override Native Links', 231 'description' => 'Replace native Login & Profile links with Capture links', 232 'default' => '1', 233 'type' => 'checkbox', 234 'screen' => 'ui', 235 'validate' => '/[^0-9]+/i' 236 ), 237 array( 238 'name' => $this->name . '_ui_colorbox', 239 'title' => 'Load Colorbox', 240 'description' => 'You can use the colorbox JS & CSS bundled in our plugin or use your own', 241 'default' => '1', 242 'type' => 'checkbox', 243 'screen' => 'ui', 244 'validate' => '/[^0-9]+/i' 245 ), 246 array( 247 'name' => $this->name . '_ui_capture_js', 248 'title' => 'Load Capture JS', 249 'description' => 'The included Capture JS relies on Colorbox. You may want to disable it and use your own.', 250 'default' => '1', 251 'type' => 'checkbox', 252 'screen' => 'ui', 253 'validate' => '/[^0-9]+/i' 99 254 ) 100 255 ); … … 105 260 106 261 /** 262 * Method bound to register_activation_hook. 263 */ 264 function activate() { 265 foreach($this->fields as $field) { 266 if (!empty($field['default'])) { 267 if (get_option($field['name']) === false) 268 update_option($field['name'], $field['default']); 269 } 270 } 271 } 272 273 /** 107 274 * Method bound to the admin_menu action. 108 275 */ 109 276 function admin_menu() { 110 277 $optionsPage = add_menu_page(__('Janrain Capture'), __('Janrain Capture'), 111 'manage_options', $this->name, array($this, 'options')); 278 'manage_options', $this->name, array(&$this, 'options')); 279 $dataPage = add_submenu_page($this->name, __('Janrain Capture'), __('Data Mapping'), 280 'manage_options', $this->name . '_data', array(&$this, 'data')); 281 $uiPage = add_submenu_page($this->name, __('Janrain Capture'), __('UI Options'), 282 'manage_options', $this->name . '_ui', array(&$this, 'ui')); 112 283 } 113 284 … … 119 290 $args->title = 'Janrain Capture Options'; 120 291 $args->action = 'options'; 292 $this->printAdmin($args); 293 } 294 295 /** 296 * Method bound to the Janrain Capture data menu. 297 */ 298 function data() { 299 $args = new stdClass; 300 $args->title = 'Data Mapping Options'; 301 $args->action = 'data'; 302 $this->printAdmin($args); 303 } 304 305 /** 306 * Method bound to the Janrain Capture data menu. 307 */ 308 function ui() { 309 $args = new stdClass; 310 $args->title = 'UI Options'; 311 $args->action = 'ui'; 121 312 $this->printAdmin($args); 122 313 } … … 167 358 function printField($field) { 168 359 $value = get_option($field['name']); 169 $value = $value ? $value : $field['default']; 170 switch ($field['type']){ 360 $value = ($value !== false) ? $value : $field['default']; 361 $r = (isset($field['required']) && $field['required'] == true) ? ' <span class="description">(required)</span>' : ''; 362 switch ($field['type']) { 171 363 case 'text': 172 364 echo <<<TEXT 173 <tr valign="top">174 <th scope="row">{$field['title']}</th>365 <tr> 366 <th><label for="{$field['name']}">{$field['title']}$r</label></th> 175 367 <td> 176 368 <input type="text" name="{$field['name']}" value="$value" style="width:200px" /> … … 182 374 case 'long-text': 183 375 echo <<<LONGTEXT 184 <tr valign="top">185 <th scope="row">{$field['title']}</th>376 <tr> 377 <th><label for="{$field['name']}">{$field['title']}$r</label></th> 186 378 <td> 187 379 <input type="text" name="{$field['name']}" value="$value" style="width:400px" /> … … 193 385 case 'password': 194 386 echo <<<PASSWORD 195 <tr valign="top">196 <th scope="row">{$field['title']}</th>387 <tr> 388 <th><label for="{$field['name']}">{$field['title']}$r</label></th> 197 389 <td> 198 390 <input type="password" name="{$field['name']}" value="$value" style="width:150px" /> … … 205 397 sort($field['options']); 206 398 echo <<<SELECT 207 <tr valign="top">208 <th scope="row">{$field['title']}</th>399 <tr> 400 <th><label for="{$field['name']}">{$field['title']}$r</label></th> 209 401 <td> 210 402 <select name="{$field['name']}" value="$value"> … … 222 414 ENDSELECT; 223 415 break; 416 case 'checkbox': 417 $checked = ($value == '1') ? ' checked="checked"' : ''; 418 echo <<<CHECKBOX 419 <tr> 420 <th><label for="{$field['name']}">{$field['title']}$r</label></th> 421 <td> 422 <input type="checkbox" name="{$field['name']}" value="1"$checked /> 423 <span class="description">{$field['description']}</span> 424 </td> 425 </tr> 426 CHECKBOX; 427 break; 224 428 case 'title': 225 429 echo <<<TITLE 226 <tr valign="top">430 <tr> 227 431 <td colspan="2"> 228 <h 2 class="title">{$field['title']}</h2>432 <h3 class="title">{$field['title']}</h3> 229 433 </td> 230 434 </tr> … … 240 444 if (isset($_POST[$this->name . '_action'])) { 241 445 foreach($this->fields as $field) { 446 if ($field['screen'] != $_POST[$this->name . '_action']) 447 continue; 448 242 449 if (isset($_POST[$field['name']])) { 243 450 $value = $_POST[$field['name']]; … … 247 454 $value = preg_replace($field['validate'], '', $value); 248 455 update_option($field['name'], $value); 456 } else if ($field['type'] == 'checkbox') { 457 $value = '0'; 458 update_option($field['name'], $value); 249 459 } 250 460 } -
janrain-capture/trunk/janrain_capture_ui.php
r525225 r527409 19 19 $this->name = $name; 20 20 if (!is_admin()) { 21 add_action('wp_head', array( $this, 'head'));21 add_action('wp_head', array(&$this, 'head')); 22 22 add_action('wp_enqueue_scripts', array(&$this, 'registerScripts')); 23 add_filter('loginout', array(&$this, 'loginout')); 24 add_filter('logout_url', array(&$this, 'logout_url'), 10, 2); 25 add_filter('admin_url', array(&$this, 'admin_url'), 10, 3); 23 if (get_option($this->name . '_ui_native_links') != '0') { 24 add_filter('loginout', array(&$this, 'loginout')); 25 add_filter('logout_url', array(&$this, 'logout_url'), 10, 2); 26 add_filter('admin_url', array(&$this, 'admin_url'), 10, 3); 27 } 26 28 } 27 29 } … … 31 33 */ 32 34 function registerScripts() { 33 wp_enqueue_script('colorbox', WP_PLUGIN_URL . '/janrain-capture/colorbox/jquery.colorbox.js', array('jquery')); 34 wp_enqueue_script($this->name . '_main_script', WP_PLUGIN_URL . '/janrain-capture/janrain_capture_ui.js'); 35 if (get_option($this->name . '_ui_colorbox') != '0') 36 wp_enqueue_script('colorbox', WP_PLUGIN_URL . '/janrain-capture/colorbox/jquery.colorbox.js', array('jquery')); 37 if (get_option($this->name . '_ui_capture_js') != '0') 38 wp_enqueue_script($this->name . '_main_script', WP_PLUGIN_URL . '/janrain-capture/janrain_capture_ui.js'); 35 39 } 36 40 … … 39 43 */ 40 44 function head() { 41 wp_enqueue_style('colorbox', WP_PLUGIN_URL . '/janrain-capture/colorbox/colorbox.css'); 45 if (get_option($this->name . '_ui_colorbox') != '0') 46 wp_enqueue_style('colorbox', WP_PLUGIN_URL . '/janrain-capture/colorbox/colorbox.css'); 42 47 $bp_js_path = get_option($this->name . '_bp_js_path'); 43 48 $bp_server_base_url = get_option($this->name . '_bp_server_base_url'); … … 68 73 $xdcomm = admin_url('admin-ajax.php') . '?action=' . $this->name . '_xdcomm'; 69 74 $redirect_uri = admin_url('admin-ajax.php') . '?action=' . $this->name . '_redirect_uri'; 70 $logout = admin_url('admin-ajax.php') . '?action=' . $this->name . '_logout';75 $logout = wp_logout_url('/'); 71 76 $sso_addr = esc_url('https://' . $sso_addr); 72 77 echo <<<SSO
Note: See TracChangeset
for help on using the changeset viewer.