Plugin Directory

Changeset 527409


Ignore:
Timestamp:
04/04/2012 06:26:20 PM (14 years ago)
Author:
bhamrick
Message:

updated options, activation hook

Location:
janrain-capture/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • janrain-capture/trunk/janrain_capture.php

    r525225 r527409  
    77Plugin URI: http://www.janrain.com/
    88Description: Collect, store and leverage user profile data from social networks in a flexible, lightweight hosted database.
    9 Version: 0.0.1
     9Version: 0.0.2
    1010Author: Janrain
    1111Author URI: http://www.janrain.com/
     
    2424   */
    2525    function init() {
    26       $this->path = dirname(__FILE__);
     26      $this->path = plugin_dir_path(__FILE__);
    2727      $this->name = 'janrain_capture';
    2828      $this->url = WP_PLUGIN_URL.'/janrain-capture';
    2929
     30      register_activation_hook(__FILE__, array(&$this, 'activate'));
    3031      require_once $this->path . '/janrain_capture_api.php';
    31       require_once $this->path . '/janrain_capture_ui.php';
    3232
    3333      if (is_admin()) {
     
    4848        add_shortcode($this->name, array(&$this, 'shortcode'));
    4949      }
    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();
    5263    }
    5364
     
    7586          // Lookup user based on returned uuid
    7687          $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']);
    7888          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))
    82103              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');
    84106          } else {
    85107            $user = $exists[0];
    86108            $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');
    87111          }
    88112          if (!$api->update_user_meta($user_id))
     
    160184   */
    161185    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;
    162191      $api = new JanrainCaptureApi($this->name);
    163192      $user_entity = $api->load_user_entity();
    164193      if (is_array($user_entity)) {
    165         if (!$api->update_user_meta())
     194        if (!$api->update_user_meta($user_id))
    166195          throw new Exception('Janrain Capture: Failed to update user meta');
    167196        $user_entity = $user_entity['result'];
    168197        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');
    181200      } else {
    182201        throw new Exception('Janrain Capture: Could not retrieve user entity');
     
    184203      echo '1';
    185204      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      }
    186268    }
    187269
     
    193275    function refresh_token() {
    194276      $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';
    196278      die();
    197279    }
  • janrain-capture/trunk/janrain_capture_admin.php

    r525225 r527409  
    2323    $this->postMessage = array('class'=>'', 'message'=>'');
    2424    $this->fields = array(
     25      // Main Screen Fields
    2526      array(
    2627        'name' => $this->name . '_main_options',
     
    3334        'title' => 'Application Domain',
    3435        'description' => 'Your Capture application domain (e.g. demo.janraincapture.com)',
     36        'required' => true,
    3537        'default' => '',
    3638        'type' => 'text',
     
    4244        'title' => 'API Client ID',
    4345        'description' => 'Your Capture Client ID',
     46        'required' => true,
    4447        'default' => '',
    4548        'type' => 'text',
     
    5154        'title' => 'API Client Secret',
    5255        'description' => 'Your Capture Client Secret',
     56        'required' => true,
    5357        'default' => '',
    5458        'type' => 'text',
     
    97101        'screen' => 'options',
    98102        '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'
    99254      )
    100255    );
     
    105260
    106261  /**
     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  /**
    107274   * Method bound to the admin_menu action.
    108275   */
    109276  function admin_menu() {
    110277    $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'));
    112283  }
    113284
     
    119290    $args->title = 'Janrain Capture Options';
    120291    $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';
    121312    $this->printAdmin($args);
    122313  }
     
    167358  function printField($field) {
    168359    $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']) {
    171363      case 'text':
    172364        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>
    175367          <td>
    176368            <input type="text" name="{$field['name']}" value="$value" style="width:200px" />
     
    182374      case 'long-text':
    183375        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>
    186378          <td>
    187379            <input type="text" name="{$field['name']}" value="$value" style="width:400px" />
     
    193385      case 'password':
    194386        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>
    197389          <td>
    198390            <input type="password" name="{$field['name']}" value="$value" style="width:150px" />
     
    205397        sort($field['options']);
    206398        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>
    209401          <td>
    210402              <select name="{$field['name']}" value="$value">
     
    222414ENDSELECT;
    223415        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>
     426CHECKBOX;
     427        break;
    224428      case 'title':
    225429        echo <<<TITLE
    226         <tr valign="top">
     430        <tr>
    227431          <td colspan="2">
    228             <h2 class="title">{$field['title']}</h2>
     432            <h3 class="title">{$field['title']}</h3>
    229433          </td>
    230434        </tr>
     
    240444    if (isset($_POST[$this->name . '_action'])) {
    241445      foreach($this->fields as $field) {
     446        if ($field['screen'] != $_POST[$this->name . '_action'])
     447          continue;
     448
    242449        if (isset($_POST[$field['name']])) {
    243450          $value = $_POST[$field['name']];
     
    247454            $value = preg_replace($field['validate'], '', $value);
    248455          update_option($field['name'], $value);
     456        } else if ($field['type'] == 'checkbox') {
     457          $value = '0';
     458          update_option($field['name'], $value);
    249459        }
    250460      }
  • janrain-capture/trunk/janrain_capture_ui.php

    r525225 r527409  
    1919    $this->name = $name;
    2020    if (!is_admin()) {
    21       add_action('wp_head', array($this, 'head'));
     21      add_action('wp_head', array(&$this, 'head'));
    2222      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      }
    2628    }
    2729  }
     
    3133   */
    3234  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');   
    3539  }
    3640
     
    3943   */
    4044  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');
    4247    $bp_js_path = get_option($this->name . '_bp_js_path');
    4348    $bp_server_base_url = get_option($this->name . '_bp_server_base_url');
     
    6873      $xdcomm = admin_url('admin-ajax.php') . '?action=' . $this->name . '_xdcomm';
    6974      $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('/');
    7176      $sso_addr = esc_url('https://' . $sso_addr);
    7277      echo <<<SSO
Note: See TracChangeset for help on using the changeset viewer.